Preprocessor::~Preprocessor() { assert(BacktrackPositions.empty() && "EnableBacktrack/Backtrack imbalance!"); IncludeMacroStack.clear(); // Destroy any macro definitions. while (MacroInfoChain *I = MIChainHead) { MIChainHead = I->Next; I->~MacroInfoChain(); } // Free any cached macro expanders. // This populates MacroArgCache, so all TokenLexers need to be destroyed // before the code below that frees up the MacroArgCache list. std::fill(TokenLexerCache, TokenLexerCache + NumCachedTokenLexers, nullptr); CurTokenLexer.reset(); while (DeserializedMacroInfoChain *I = DeserialMIChainHead) { DeserialMIChainHead = I->Next; I->~DeserializedMacroInfoChain(); } // Free any cached MacroArgs. for (MacroArgs *ArgList = MacroArgCache; ArgList;) ArgList = ArgList->deallocate(); // Delete the header search info, if we own it. if (OwnsHeaderSearch) delete &HeaderInfo; }
Preprocessor::~Preprocessor() { assert(BacktrackPositions.empty() && "EnableBacktrack/Backtrack imbalance!"); IncludeMacroStack.clear(); // Free any macro definitions. for (MacroInfoChain *I = MIChainHead ; I ; I = I->Next) I->MI.Destroy(); // Free any cached macro expanders. // This populates MacroArgCache, so all TokenLexers need to be destroyed // before the code below that frees up the MacroArgCache list. for (unsigned i = 0, e = NumCachedTokenLexers; i != e; ++i) delete TokenLexerCache[i]; CurTokenLexer.reset(); for (DeserializedMacroInfoChain *I = DeserialMIChainHead ; I ; I = I->Next) I->MI.Destroy(); // Free any cached MacroArgs. for (MacroArgs *ArgList = MacroArgCache; ArgList;) ArgList = ArgList->deallocate(); // Release pragma information. delete PragmaHandlers; // Delete the scratch buffer info. delete ScratchBuf; // Delete the header search info, if we own it. if (OwnsHeaderSearch) delete &HeaderInfo; delete Callbacks; }
Preprocessor::~Preprocessor() { assert(BacktrackPositions.empty() && "EnableBacktrack/Backtrack imbalance!"); while (!IncludeMacroStack.empty()) { delete IncludeMacroStack.back().TheLexer; delete IncludeMacroStack.back().TheTokenLexer; IncludeMacroStack.pop_back(); } // Free any macro definitions. for (MacroInfoChain *I = MIChainHead ; I ; I = I->Next) I->MI.Destroy(); // Free any cached macro expanders. for (unsigned i = 0, e = NumCachedTokenLexers; i != e; ++i) delete TokenLexerCache[i]; // Free any cached MacroArgs. for (MacroArgs *ArgList = MacroArgCache; ArgList; ) ArgList = ArgList->deallocate(); // Release pragma information. delete PragmaHandlers; // Delete the scratch buffer info. delete ScratchBuf; // Delete the header search info, if we own it. if (OwnsHeaderSearch) delete &HeaderInfo; delete Callbacks; }
/// MacroArgs ctor function - This destroys the vector passed in. MacroArgs *MacroArgs::create(const MacroInfo *MI, ArrayRef<Token> UnexpArgTokens, bool VarargsElided, Preprocessor &PP) { assert(MI->isFunctionLike() && "Can't have args for an object-like macro!"); MacroArgs **ResultEnt = nullptr; unsigned ClosestMatch = ~0U; // See if we have an entry with a big enough argument list to reuse on the // free list. If so, reuse it. for (MacroArgs **Entry = &PP.MacroArgCache; *Entry; Entry = &(*Entry)->ArgCache) if ((*Entry)->NumUnexpArgTokens >= UnexpArgTokens.size() && (*Entry)->NumUnexpArgTokens < ClosestMatch) { ResultEnt = Entry; // If we have an exact match, use it. if ((*Entry)->NumUnexpArgTokens == UnexpArgTokens.size()) break; // Otherwise, use the best fit. ClosestMatch = (*Entry)->NumUnexpArgTokens; } MacroArgs *Result; if (!ResultEnt) { // Allocate memory for a MacroArgs object with the lexer tokens at the end. Result = (MacroArgs *)malloc(sizeof(MacroArgs) + UnexpArgTokens.size() * sizeof(Token)); // Construct the MacroArgs object. new (Result) MacroArgs(UnexpArgTokens.size(), VarargsElided, MI->getNumParams()); } else { Result = *ResultEnt; // Unlink this node from the preprocessors singly linked list. *ResultEnt = Result->ArgCache; Result->NumUnexpArgTokens = UnexpArgTokens.size(); Result->VarargsElided = VarargsElided; Result->NumMacroArgs = MI->getNumParams(); } // Copy the actual unexpanded tokens to immediately after the result ptr. if (!UnexpArgTokens.empty()) std::copy(UnexpArgTokens.begin(), UnexpArgTokens.end(), const_cast<Token*>(Result->getUnexpArgument(0))); return Result; }
/// MacroArgs ctor function - This destroys the vector passed in. MacroArgs *MacroArgs::create(const MacroInfo *MI, const Token *UnexpArgTokens, unsigned NumToks, bool VarargsElided) { assert(MI->isFunctionLike() && "Can't have args for an object-like macro!"); // Allocate memory for the MacroArgs object with the lexer tokens at the end. MacroArgs *Result = (MacroArgs*)malloc(sizeof(MacroArgs) + NumToks*sizeof(Token)); // Construct the macroargs object. new (Result) MacroArgs(NumToks, VarargsElided); // Copy the actual unexpanded tokens to immediately after the result ptr. if (NumToks) memcpy(const_cast<Token*>(Result->getUnexpArgument(0)), UnexpArgTokens, NumToks*sizeof(Token)); return Result; }
Preprocessor::~Preprocessor() { assert(BacktrackPositions.empty() && "EnableBacktrack/Backtrack imbalance!"); while (!IncludeMacroStack.empty()) { delete IncludeMacroStack.back().TheLexer; delete IncludeMacroStack.back().TheTokenLexer; IncludeMacroStack.pop_back(); } // Free any macro definitions. for (llvm::DenseMap<IdentifierInfo*, MacroInfo*>::iterator I = Macros.begin(), E = Macros.end(); I != E; ++I) { // We don't need to free the MacroInfo objects directly. These // will be released when the BumpPtrAllocator 'BP' object gets // destroyed. We still need to run the dtor, however, to free // memory alocated by MacroInfo. I->second->Destroy(BP); I->first->setHasMacroDefinition(false); } // Free any cached macro expanders. for (unsigned i = 0, e = NumCachedTokenLexers; i != e; ++i) delete TokenLexerCache[i]; // Free any cached MacroArgs. for (MacroArgs *ArgList = MacroArgCache; ArgList; ) ArgList = ArgList->deallocate(); // Release pragma information. delete PragmaHandlers; // Delete the scratch buffer info. delete ScratchBuf; // Delete the header search info, if we own it. if (OwnsHeaderSearch) delete &HeaderInfo; delete Callbacks; }
/// HandleMacroExpandedIdentifier - If an identifier token is read that is to be /// expanded as a macro, handle it and return the next token as 'Identifier'. bool Preprocessor::HandleMacroExpandedIdentifier(Token &Identifier, MacroInfo *MI) { // If this is a macro expansion in the "#if !defined(x)" line for the file, // then the macro could expand to different things in other contexts, we need // to disable the optimization in this case. if (CurPPLexer) CurPPLexer->MIOpt.ExpandedMacro(); // If this is a builtin macro, like __LINE__ or _Pragma, handle it specially. if (MI->isBuiltinMacro()) { if (Callbacks) Callbacks->MacroExpands(Identifier, MI, Identifier.getLocation()); ExpandBuiltinMacro(Identifier); return false; } /// Args - If this is a function-like macro expansion, this contains, /// for each macro argument, the list of tokens that were provided to the /// invocation. MacroArgs *Args = 0; // Remember where the end of the expansion occurred. For an object-like // macro, this is the identifier. For a function-like macro, this is the ')'. SourceLocation ExpansionEnd = Identifier.getLocation(); // If this is a function-like macro, read the arguments. if (MI->isFunctionLike()) { // C99 6.10.3p10: If the preprocessing token immediately after the the macro // name isn't a '(', this macro should not be expanded. if (!isNextPPTokenLParen()) return true; // Remember that we are now parsing the arguments to a macro invocation. // Preprocessor directives used inside macro arguments are not portable, and // this enables the warning. InMacroArgs = true; Args = ReadFunctionLikeMacroArgs(Identifier, MI, ExpansionEnd); // Finished parsing args. InMacroArgs = false; // If there was an error parsing the arguments, bail out. if (Args == 0) return false; ++NumFnMacroExpanded; } else { ++NumMacroExpanded; } // Notice that this macro has been used. markMacroAsUsed(MI); // Remember where the token is expanded. SourceLocation ExpandLoc = Identifier.getLocation(); if (Callbacks) Callbacks->MacroExpands(Identifier, MI, SourceRange(ExpandLoc, ExpansionEnd)); // If we started lexing a macro, enter the macro expansion body. // If this macro expands to no tokens, don't bother to push it onto the // expansion stack, only to take it right back off. if (MI->getNumTokens() == 0) { // No need for arg info. if (Args) Args->destroy(*this); // Ignore this macro use, just return the next token in the current // buffer. bool HadLeadingSpace = Identifier.hasLeadingSpace(); bool IsAtStartOfLine = Identifier.isAtStartOfLine(); Lex(Identifier); // If the identifier isn't on some OTHER line, inherit the leading // whitespace/first-on-a-line property of this token. This handles // stuff like "! XX," -> "! ," and " XX," -> " ,", when XX is // empty. if (!Identifier.isAtStartOfLine()) { if (IsAtStartOfLine) Identifier.setFlag(Token::StartOfLine); if (HadLeadingSpace) Identifier.setFlag(Token::LeadingSpace); } Identifier.setFlag(Token::LeadingEmptyMacro); ++NumFastMacroExpanded; return false; } else if (MI->getNumTokens() == 1 && isTrivialSingleTokenExpansion(MI, Identifier.getIdentifierInfo(), *this)) { // Otherwise, if this macro expands into a single trivially-expanded // token: expand it now. This handles common cases like // "#define VAL 42". // No need for arg info. if (Args) Args->destroy(*this); // Propagate the isAtStartOfLine/hasLeadingSpace markers of the macro // identifier to the expanded token. bool isAtStartOfLine = Identifier.isAtStartOfLine(); bool hasLeadingSpace = Identifier.hasLeadingSpace(); // Replace the result token. Identifier = MI->getReplacementToken(0); // Restore the StartOfLine/LeadingSpace markers. Identifier.setFlagValue(Token::StartOfLine , isAtStartOfLine); Identifier.setFlagValue(Token::LeadingSpace, hasLeadingSpace); // Update the tokens location to include both its expansion and physical // locations. SourceLocation Loc = SourceMgr.createExpansionLoc(Identifier.getLocation(), ExpandLoc, ExpansionEnd,Identifier.getLength()); Identifier.setLocation(Loc); // If this is a disabled macro or #define X X, we must mark the result as // unexpandable. if (IdentifierInfo *NewII = Identifier.getIdentifierInfo()) { if (MacroInfo *NewMI = getMacroInfo(NewII)) if (!NewMI->isEnabled() || NewMI == MI) Identifier.setFlag(Token::DisableExpand); } // Since this is not an identifier token, it can't be macro expanded, so // we're done. ++NumFastMacroExpanded; return false; } // Start expanding the macro. EnterMacro(Identifier, ExpansionEnd, Args); // Now that the macro is at the top of the include stack, ask the // preprocessor to read the next token from it. Lex(Identifier); return false; }
// // SourceTokenizerC::readArgs // // Process function-like macro arguments, preserving whitespace in TT_NONEs. // void SourceTokenizerC::readArgs(MacroArgs &args, SourceStream *in, MacroData const &data, SourcePosition const &pos, MacroArgs const *altArgs, MacroParm const *altParm) { MacroArg const *arg; MacroParm const &parm = data.first; int pdepth = 0; // If there are no arguments expected, then this is just a simple assertion. if(parm.empty()) { doAssert(SourceTokenC::create(in), SourceTokenC::TT_PAREN_C); } // Otherwise, start reading args. Each arg being a vector of tokens. // First thing is to create the first vector, though. else for(args.push_back(MacroArg());;) { // Convert each whitespace character into a TT_NONE token. // These are used by operator # to add spaces. while (std::isspace(in->peek())) { in->get(); args.back().push_back(SourceTokenC::tt_none()); } // Read the token. SourceTokenC::Reference tok = SourceTokenC::create(in); // If it's a parenthesis, terminate on the close-parenthesis that matches // the initial one that started the expansion. if(tok->type == SourceTokenC::TT_PAREN_O) ++pdepth; if(tok->type == SourceTokenC::TT_PAREN_C && !pdepth--) break; // A comma not in parentheses starts a new argument... if(tok->type == SourceTokenC::TT_COMMA && !pdepth && // ... Unless we've reached the __VA_ARGS__ segment. (args.size() < parm.size() || !parm.back().empty())) { args.push_back(MacroArg()); continue; } // If we're being called from a function-like macro expansion and we see // one of its arguments, expand it. if(tok->type == SourceTokenC::TT_NAM && altArgs && altParm && (arg = find_arg(*altArgs, *altParm, tok->data))) { for(MacroArg::const_iterator tokEnd = arg->end(), tokItr = arg->begin(); tokItr != tokEnd; ++tokItr) { args.back().push_back(*tokItr); } } else args.back().push_back(tok); } // Must not have empty args. for(MacroArgs::iterator argsEnd = args.end(), argsItr = args.begin(); argsItr != argsEnd; ++argsItr) { if(argsItr->empty()) argsItr->push_back(SourceTokenC::tt_none()); } // And of course, must have the right number of arguments. if(args.size() != parm.size()) Error_P("incorrect arg count for macro, expected %i got %i", (int)parm.size(), (int)args.size()); }
/// HandleMacroExpandedIdentifier - If an identifier token is read that is to be /// expanded as a macro, handle it and return the next token as 'Identifier'. bool Preprocessor::HandleMacroExpandedIdentifier(Token &Identifier, const MacroDefinition &M) { MacroInfo *MI = M.getMacroInfo(); // If this is a macro expansion in the "#if !defined(x)" line for the file, // then the macro could expand to different things in other contexts, we need // to disable the optimization in this case. if (CurPPLexer) CurPPLexer->MIOpt.ExpandedMacro(); // If this is a builtin macro, like __LINE__ or _Pragma, handle it specially. if (MI->isBuiltinMacro()) { ExpandBuiltinMacro(Identifier); return true; } /// Args - If this is a function-like macro expansion, this contains, /// for each macro argument, the list of tokens that were provided to the /// invocation. MacroArgs *Args = nullptr; // Remember where the end of the expansion occurred. For an object-like // macro, this is the identifier. For a function-like macro, this is the ')'. SourceLocation ExpansionEnd = Identifier.getLocation(); // If this is a function-like macro, read the arguments. if (MI->isFunctionLike()) { // Remember that we are now parsing the arguments to a macro invocation. // Preprocessor directives used inside macro arguments are not portable, and // this enables the warning. InMacroArgs = true; Args = ReadMacroCallArgumentList(Identifier, MI, ExpansionEnd); // Finished parsing args. InMacroArgs = false; // If there was an error parsing the arguments, bail out. if (!Args) return true; ++NumFnMacroExpanded; } else { ++NumMacroExpanded; } // Notice that this macro has been used. markMacroAsUsed(MI); // Remember where the token is expanded. SourceLocation ExpandLoc = Identifier.getLocation(); SourceRange ExpansionRange(ExpandLoc, ExpansionEnd); // If the macro definition is ambiguous, complain. if (M.isAmbiguous()) { Diag(Identifier, diag::warn_pp_ambiguous_macro) << Identifier.getIdentifierInfo(); Diag(MI->getDefinitionLoc(), diag::note_pp_ambiguous_macro_chosen) << Identifier.getIdentifierInfo(); M.forAllDefinitions([&](const MacroInfo *OtherMI) { if (OtherMI != MI) Diag(OtherMI->getDefinitionLoc(), diag::note_pp_ambiguous_macro_other) << Identifier.getIdentifierInfo(); }); } // If we started lexing a macro, enter the macro expansion body. // If this macro expands to no tokens, don't bother to push it onto the // expansion stack, only to take it right back off. if (MI->getNumTokens() == 0) { // No need for arg info. if (Args) Args->destroy(*this); // Propagate whitespace info as if we had pushed, then popped, // a macro context. Identifier.setFlag(Token::LeadingEmptyMacro); PropagateLineStartLeadingSpaceInfo(Identifier); ++NumFastMacroExpanded; return false; } else if (MI->getNumTokens() == 1 && isTrivialSingleTokenExpansion(MI, Identifier.getIdentifierInfo(), *this)) { // Otherwise, if this macro expands into a single trivially-expanded // token: expand it now. This handles common cases like // "#define VAL 42". // No need for arg info. if (Args) Args->destroy(*this); // Propagate the isAtStartOfLine/hasLeadingSpace markers of the macro // identifier to the expanded token. bool isAtStartOfLine = Identifier.isAtStartOfLine(); bool hasLeadingSpace = Identifier.hasLeadingSpace(); // Replace the result token. Identifier = MI->getReplacementToken(0); // Restore the StartOfLine/LeadingSpace markers. Identifier.setFlagValue(Token::StartOfLine , isAtStartOfLine); Identifier.setFlagValue(Token::LeadingSpace, hasLeadingSpace); // Update the tokens location to include both its expansion and physical // locations. SourceLocation Loc = SourceMgr.createExpansionLoc(Identifier.getLocation(), ExpandLoc, ExpansionEnd,Identifier.getLength()); Identifier.setLocation(Loc); // If this is a disabled macro or #define X X, we must mark the result as // unexpandable. if (IdentifierInfo *NewII = Identifier.getIdentifierInfo()) { if (MacroInfo *NewMI = getMacroInfo(NewII)) if (!NewMI->isEnabled() || NewMI == MI) { Identifier.setFlag(Token::DisableExpand); // Don't warn for "#define X X" like "#define bool bool" from // stdbool.h. if (NewMI != MI || MI->isFunctionLike()) Diag(Identifier, diag::pp_disabled_macro_expansion); } } // Since this is not an identifier token, it can't be macro expanded, so // we're done. ++NumFastMacroExpanded; return true; } // Start expanding the macro. EnterMacro(Identifier, ExpansionEnd, MI, Args); return false; }