diff options
Diffstat (limited to 'contrib/llvm-project/clang/lib/StaticAnalyzer/Core/PlistDiagnostics.cpp')
-rw-r--r-- | contrib/llvm-project/clang/lib/StaticAnalyzer/Core/PlistDiagnostics.cpp | 663 |
1 files changed, 58 insertions, 605 deletions
diff --git a/contrib/llvm-project/clang/lib/StaticAnalyzer/Core/PlistDiagnostics.cpp b/contrib/llvm-project/clang/lib/StaticAnalyzer/Core/PlistDiagnostics.cpp index 35e320c7755f..be19a1c118ea 100644 --- a/contrib/llvm-project/clang/lib/StaticAnalyzer/Core/PlistDiagnostics.cpp +++ b/contrib/llvm-project/clang/lib/StaticAnalyzer/Core/PlistDiagnostics.cpp @@ -11,6 +11,7 @@ //===----------------------------------------------------------------------===// #include "clang/Analysis/IssueHash.h" +#include "clang/Analysis/MacroExpansionContext.h" #include "clang/Analysis/PathDiagnostic.h" #include "clang/Basic/FileManager.h" #include "clang/Basic/PlistSupport.h" @@ -27,6 +28,7 @@ #include "llvm/ADT/Statistic.h" #include "llvm/Support/Casting.h" #include <memory> +#include <optional> using namespace clang; using namespace ento; @@ -43,6 +45,7 @@ namespace { const std::string OutputFile; const Preprocessor &PP; const cross_tu::CrossTranslationUnitContext &CTU; + const MacroExpansionContext &MacroExpansions; const bool SupportsCrossFileDiagnostics; void printBugPath(llvm::raw_ostream &o, const FIDMap &FM, @@ -52,6 +55,7 @@ namespace { PlistDiagnostics(PathDiagnosticConsumerOptions DiagOpts, const std::string &OutputFile, const Preprocessor &PP, const cross_tu::CrossTranslationUnitContext &CTU, + const MacroExpansionContext &MacroExpansions, bool supportsMultipleFiles); ~PlistDiagnostics() override {} @@ -80,14 +84,14 @@ class PlistPrinter { const FIDMap& FM; const Preprocessor &PP; const cross_tu::CrossTranslationUnitContext &CTU; + const MacroExpansionContext &MacroExpansions; llvm::SmallVector<const PathDiagnosticMacroPiece *, 0> MacroPieces; public: - PlistPrinter(const FIDMap& FM, - const Preprocessor &PP, - const cross_tu::CrossTranslationUnitContext &CTU) - : FM(FM), PP(PP), CTU(CTU) { - } + PlistPrinter(const FIDMap &FM, const Preprocessor &PP, + const cross_tu::CrossTranslationUnitContext &CTU, + const MacroExpansionContext &MacroExpansions) + : FM(FM), PP(PP), CTU(CTU), MacroExpansions(MacroExpansions) {} void ReportDiag(raw_ostream &o, const PathDiagnosticPiece& P) { ReportPiece(o, P, /*indent*/ 4, /*depth*/ 0, /*includeControlFlow*/ true); @@ -154,28 +158,17 @@ private: } // end of anonymous namespace -namespace { - -struct ExpansionInfo { - std::string MacroName; - std::string Expansion; - ExpansionInfo(std::string N, std::string E) - : MacroName(std::move(N)), Expansion(std::move(E)) {} -}; - -} // end of anonymous namespace - -/// Print coverage information to output stream {@code o}. -/// May modify the used list of files {@code Fids} by inserting new ones. +/// Print coverage information to output stream @c o. +/// May modify the used list of files @c Fids by inserting new ones. static void printCoverage(const PathDiagnostic *D, unsigned InputIndentLevel, SmallVectorImpl<FileID> &Fids, FIDMap &FM, llvm::raw_fd_ostream &o); -static ExpansionInfo -getExpandedMacro(SourceLocation MacroLoc, const Preprocessor &PP, - const cross_tu::CrossTranslationUnitContext &CTU); +static std::optional<StringRef> getExpandedMacro( + SourceLocation MacroLoc, const cross_tu::CrossTranslationUnitContext &CTU, + const MacroExpansionContext &MacroExpansions, const SourceManager &SM); //===----------------------------------------------------------------------===// // Methods of PlistPrinter. @@ -374,10 +367,8 @@ void PlistPrinter::ReportMacroSubPieces(raw_ostream &o, unsigned indent, unsigned depth) { MacroPieces.push_back(&P); - for (PathPieces::const_iterator I = P.subPieces.begin(), - E = P.subPieces.end(); - I != E; ++I) { - ReportPiece(o, **I, indent, depth, /*includeControlFlow*/ false); + for (const auto &SubPiece : P.subPieces) { + ReportPiece(o, *SubPiece, indent, depth, /*includeControlFlow*/ false); } assert(P.getFixits().size() == 0 && @@ -388,7 +379,17 @@ void PlistPrinter::ReportMacroExpansions(raw_ostream &o, unsigned indent) { for (const PathDiagnosticMacroPiece *P : MacroPieces) { const SourceManager &SM = PP.getSourceManager(); - ExpansionInfo EI = getExpandedMacro(P->getLocation().asLocation(), PP, CTU); + + SourceLocation MacroExpansionLoc = + P->getLocation().asLocation().getExpansionLoc(); + + const std::optional<StringRef> MacroName = + MacroExpansions.getOriginalText(MacroExpansionLoc); + const std::optional<StringRef> ExpansionText = + getExpandedMacro(MacroExpansionLoc, CTU, MacroExpansions, SM); + + if (!MacroName || !ExpansionText) + continue; Indent(o, indent) << "<dict>\n"; ++indent; @@ -405,11 +406,11 @@ void PlistPrinter::ReportMacroExpansions(raw_ostream &o, unsigned indent) { // Output the macro name. Indent(o, indent) << "<key>name</key>"; - EmitString(o, EI.MacroName) << '\n'; + EmitString(o, *MacroName) << '\n'; // Output what it expands into. Indent(o, indent) << "<key>expansion</key>"; - EmitString(o, EI.Expansion) << '\n'; + EmitString(o, *ExpansionText) << '\n'; // Finish up. --indent; @@ -482,8 +483,8 @@ void PlistPrinter::ReportPopUp(raw_ostream &o, // Static function definitions. //===----------------------------------------------------------------------===// -/// Print coverage information to output stream {@code o}. -/// May modify the used list of files {@code Fids} by inserting new ones. +/// Print coverage information to output stream @c o. +/// May modify the used list of files @c Fids by inserting new ones. static void printCoverage(const PathDiagnostic *D, unsigned InputIndentLevel, SmallVectorImpl<FileID> &Fids, @@ -497,12 +498,12 @@ static void printCoverage(const PathDiagnostic *D, // Mapping from file IDs to executed lines. const FilesToLineNumsMap &ExecutedLines = D->getExecutedLines(); - for (auto I = ExecutedLines.begin(), E = ExecutedLines.end(); I != E; ++I) { - unsigned FileKey = AddFID(FM, Fids, I->first); + for (const auto &[FID, Lines] : ExecutedLines) { + unsigned FileKey = AddFID(FM, Fids, FID); Indent(o, IndentLevel) << "<key>" << FileKey << "</key>\n"; Indent(o, IndentLevel) << "<array>\n"; IndentLevel++; - for (unsigned LineNo : I->second) { + for (unsigned LineNo : Lines) { Indent(o, IndentLevel); EmitInteger(o, LineNo) << "\n"; } @@ -522,8 +523,9 @@ static void printCoverage(const PathDiagnostic *D, PlistDiagnostics::PlistDiagnostics( PathDiagnosticConsumerOptions DiagOpts, const std::string &output, const Preprocessor &PP, const cross_tu::CrossTranslationUnitContext &CTU, - bool supportsMultipleFiles) + const MacroExpansionContext &MacroExpansions, bool supportsMultipleFiles) : DiagOpts(std::move(DiagOpts)), OutputFile(output), PP(PP), CTU(CTU), + MacroExpansions(MacroExpansions), SupportsCrossFileDiagnostics(supportsMultipleFiles) { // FIXME: Will be used by a later planned change. (void)this->CTU; @@ -532,36 +534,40 @@ PlistDiagnostics::PlistDiagnostics( void ento::createPlistDiagnosticConsumer( PathDiagnosticConsumerOptions DiagOpts, PathDiagnosticConsumers &C, const std::string &OutputFile, const Preprocessor &PP, - const cross_tu::CrossTranslationUnitContext &CTU) { + const cross_tu::CrossTranslationUnitContext &CTU, + const MacroExpansionContext &MacroExpansions) { // TODO: Emit an error here. if (OutputFile.empty()) return; C.push_back(new PlistDiagnostics(DiagOpts, OutputFile, PP, CTU, + MacroExpansions, /*supportsMultipleFiles=*/false)); createTextMinimalPathDiagnosticConsumer(std::move(DiagOpts), C, OutputFile, - PP, CTU); + PP, CTU, MacroExpansions); } void ento::createPlistMultiFileDiagnosticConsumer( PathDiagnosticConsumerOptions DiagOpts, PathDiagnosticConsumers &C, const std::string &OutputFile, const Preprocessor &PP, - const cross_tu::CrossTranslationUnitContext &CTU) { + const cross_tu::CrossTranslationUnitContext &CTU, + const MacroExpansionContext &MacroExpansions) { // TODO: Emit an error here. if (OutputFile.empty()) return; C.push_back(new PlistDiagnostics(DiagOpts, OutputFile, PP, CTU, + MacroExpansions, /*supportsMultipleFiles=*/true)); createTextMinimalPathDiagnosticConsumer(std::move(DiagOpts), C, OutputFile, - PP, CTU); + PP, CTU, MacroExpansions); } void PlistDiagnostics::printBugPath(llvm::raw_ostream &o, const FIDMap &FM, const PathPieces &Path) { - PlistPrinter Printer(FM, PP, CTU); + PlistPrinter Printer(FM, PP, CTU, MacroExpansions); assert(std::is_partitioned(Path.begin(), Path.end(), [](const PathDiagnosticPieceRef &E) { return E->getKind() == PathDiagnosticPiece::Note; @@ -589,8 +595,8 @@ void PlistDiagnostics::printBugPath(llvm::raw_ostream &o, const FIDMap &FM, o << " <array>\n"; - for (PathPieces::const_iterator E = Path.end(); I != E; ++I) - Printer.ReportDiag(o, **I); + for (const auto &Piece : llvm::make_range(I, Path.end())) + Printer.ReportDiag(o, *Piece); o << " </array>\n"; @@ -653,7 +659,7 @@ void PlistDiagnostics::FlushDiagnosticsImpl( // Open the file. std::error_code EC; - llvm::raw_fd_ostream o(OutputFile, EC, llvm::sys::fs::OF_Text); + llvm::raw_fd_ostream o(OutputFile, EC, llvm::sys::fs::OF_TextWithCRLF); if (EC) { llvm::errs() << "warning: could not create file: " << EC.message() << '\n'; return; @@ -798,7 +804,7 @@ void PlistDiagnostics::FlushDiagnosticsImpl( o << " <key>files</key>\n" " <array>\n"; for (FileID FID : Fids) - EmitString(o << " ", SM.getFileEntryForID(FID)->getName()) << '\n'; + EmitString(o << " ", SM.getFileEntryRefForID(FID)->getName()) << '\n'; o << " </array>\n"; if (llvm::AreStatisticsEnabled() && DiagOpts.ShouldSerializeStats) { @@ -815,570 +821,17 @@ void PlistDiagnostics::FlushDiagnosticsImpl( } //===----------------------------------------------------------------------===// -// Declarations of helper functions and data structures for expanding macros. -//===----------------------------------------------------------------------===// - -namespace { - -using ArgTokensTy = llvm::SmallVector<Token, 2>; - -} // end of anonymous namespace - -LLVM_DUMP_METHOD static void dumpArgTokensToStream(llvm::raw_ostream &Out, - const Preprocessor &PP, - const ArgTokensTy &Toks); - -namespace { -/// Maps unexpanded macro parameters to expanded arguments. A macro argument may -/// need to expanded further when it is nested inside another macro. -class MacroParamMap : public std::map<const IdentifierInfo *, ArgTokensTy> { -public: - void expandFromPrevMacro(const MacroParamMap &Super); - - LLVM_DUMP_METHOD void dump(const Preprocessor &PP) const { - dumpToStream(llvm::errs(), PP); - } - - LLVM_DUMP_METHOD void dumpToStream(llvm::raw_ostream &Out, - const Preprocessor &PP) const; -}; - -struct MacroExpansionInfo { - std::string Name; - const MacroInfo *MI = nullptr; - MacroParamMap ParamMap; - - MacroExpansionInfo(std::string N, const MacroInfo *MI, MacroParamMap M) - : Name(std::move(N)), MI(MI), ParamMap(std::move(M)) {} -}; - -class TokenPrinter { - llvm::raw_ostream &OS; - const Preprocessor &PP; - - Token PrevTok, PrevPrevTok; - TokenConcatenation ConcatInfo; - -public: - TokenPrinter(llvm::raw_ostream &OS, const Preprocessor &PP) - : OS(OS), PP(PP), ConcatInfo(PP) { - PrevTok.setKind(tok::unknown); - PrevPrevTok.setKind(tok::unknown); - } - - void printToken(const Token &Tok); -}; - -/// Wrapper around a Lexer object that can lex tokens one-by-one. Its possible -/// to "inject" a range of tokens into the stream, in which case the next token -/// is retrieved from the next element of the range, until the end of the range -/// is reached. -class TokenStream { -public: - TokenStream(SourceLocation ExpanLoc, const SourceManager &SM, - const LangOptions &LangOpts) - : ExpanLoc(ExpanLoc) { - FileID File; - unsigned Offset; - std::tie(File, Offset) = SM.getDecomposedLoc(ExpanLoc); - llvm::MemoryBufferRef MB = SM.getBufferOrFake(File); - const char *MacroNameTokenPos = MB.getBufferStart() + Offset; - - RawLexer = std::make_unique<Lexer>(SM.getLocForStartOfFile(File), LangOpts, - MB.getBufferStart(), MacroNameTokenPos, - MB.getBufferEnd()); - } - - void next(Token &Result) { - if (CurrTokenIt == TokenRange.end()) { - RawLexer->LexFromRawLexer(Result); - return; - } - Result = *CurrTokenIt; - CurrTokenIt++; - } - - void injectRange(const ArgTokensTy &Range) { - TokenRange = Range; - CurrTokenIt = TokenRange.begin(); - } - - std::unique_ptr<Lexer> RawLexer; - ArgTokensTy TokenRange; - ArgTokensTy::iterator CurrTokenIt = TokenRange.begin(); - SourceLocation ExpanLoc; -}; - -} // end of anonymous namespace - -/// The implementation method of getMacroExpansion: It prints the expansion of -/// a macro to \p Printer, and returns with the name of the macro. -/// -/// Since macros can be nested in one another, this function may call itself -/// recursively. -/// -/// Unfortunately, macro arguments have to expanded manually. To understand why, -/// observe the following example: -/// -/// #define PRINT(x) print(x) -/// #define DO_SOMETHING(str) PRINT(str) -/// -/// DO_SOMETHING("Cute panda cubs."); -/// -/// As we expand the last line, we'll immediately replace PRINT(str) with -/// print(x). The information that both 'str' and 'x' refers to the same string -/// is an information we have to forward, hence the argument \p PrevParamMap. -/// -/// To avoid infinite recursion we maintain the already processed tokens in -/// a set. This is carried as a parameter through the recursive calls. The set -/// is extended with the currently processed token and after processing it, the -/// token is removed. If the token is already in the set, then recursion stops: -/// -/// #define f(y) x -/// #define x f(x) -static std::string getMacroNameAndPrintExpansion( - TokenPrinter &Printer, SourceLocation MacroLoc, const Preprocessor &PP, - const MacroParamMap &PrevParamMap, - llvm::SmallPtrSet<IdentifierInfo *, 8> &AlreadyProcessedTokens); - -/// Retrieves the name of the macro and what it's parameters expand into -/// at \p ExpanLoc. -/// -/// For example, for the following macro expansion: -/// -/// #define SET_TO_NULL(x) x = 0 -/// #define NOT_SUSPICIOUS(a) \ -/// { \ -/// int b = 0; \ -/// } \ -/// SET_TO_NULL(a) -/// -/// int *ptr = new int(4); -/// NOT_SUSPICIOUS(&ptr); -/// *ptr = 5; -/// -/// When \p ExpanLoc references the last line, the macro name "NOT_SUSPICIOUS" -/// and the MacroArgMap map { (a, &ptr) } will be returned. -/// -/// When \p ExpanLoc references "SET_TO_NULL(a)" within the definition of -/// "NOT_SUSPICOUS", the macro name "SET_TO_NULL" and the MacroArgMap map -/// { (x, a) } will be returned. -static MacroExpansionInfo -getMacroExpansionInfo(const MacroParamMap &PrevParamMap, - SourceLocation ExpanLoc, const Preprocessor &PP); - -/// Retrieves the ')' token that matches '(' \p It points to. -static MacroInfo::tokens_iterator getMatchingRParen( - MacroInfo::tokens_iterator It, - MacroInfo::tokens_iterator End); - -/// Retrieves the macro info for \p II refers to at \p Loc. This is important -/// because macros can be redefined or undefined. -static const MacroInfo *getMacroInfoForLocation(const Preprocessor &PP, - const SourceManager &SM, - const IdentifierInfo *II, - SourceLocation Loc); - -//===----------------------------------------------------------------------===// // Definitions of helper functions and methods for expanding macros. //===----------------------------------------------------------------------===// -static ExpansionInfo -getExpandedMacro(SourceLocation MacroLoc, const Preprocessor &PP, - const cross_tu::CrossTranslationUnitContext &CTU) { - - const Preprocessor *PPToUse = &PP; - if (auto LocAndUnit = CTU.getImportedFromSourceLocation(MacroLoc)) { - MacroLoc = LocAndUnit->first; - PPToUse = &LocAndUnit->second->getPreprocessor(); - } - - llvm::SmallString<200> ExpansionBuf; - llvm::raw_svector_ostream OS(ExpansionBuf); - TokenPrinter Printer(OS, *PPToUse); - llvm::SmallPtrSet<IdentifierInfo*, 8> AlreadyProcessedTokens; - - std::string MacroName = getMacroNameAndPrintExpansion( - Printer, MacroLoc, *PPToUse, MacroParamMap{}, AlreadyProcessedTokens); - return {MacroName, std::string(OS.str())}; -} - -static std::string getMacroNameAndPrintExpansion( - TokenPrinter &Printer, SourceLocation MacroLoc, const Preprocessor &PP, - const MacroParamMap &PrevParamMap, - llvm::SmallPtrSet<IdentifierInfo *, 8> &AlreadyProcessedTokens) { - - const SourceManager &SM = PP.getSourceManager(); - - MacroExpansionInfo MExpInfo = - getMacroExpansionInfo(PrevParamMap, SM.getExpansionLoc(MacroLoc), PP); - IdentifierInfo *MacroNameII = PP.getIdentifierInfo(MExpInfo.Name); - - // TODO: If the macro definition contains another symbol then this function is - // called recursively. In case this symbol is the one being defined, it will - // be an infinite recursion which is stopped by this "if" statement. However, - // in this case we don't get the full expansion text in the Plist file. See - // the test file where "value" is expanded to "garbage_" instead of - // "garbage_value". - if (!AlreadyProcessedTokens.insert(MacroNameII).second) - return MExpInfo.Name; - - if (!MExpInfo.MI) - return MExpInfo.Name; - - // Manually expand its arguments from the previous macro. - MExpInfo.ParamMap.expandFromPrevMacro(PrevParamMap); - - // Iterate over the macro's tokens and stringify them. - for (auto It = MExpInfo.MI->tokens_begin(), E = MExpInfo.MI->tokens_end(); - It != E; ++It) { - Token T = *It; - - // If this token is not an identifier, we only need to print it. - if (T.isNot(tok::identifier)) { - Printer.printToken(T); - continue; - } - - const auto *II = T.getIdentifierInfo(); - assert(II && - "This token is an identifier but has no IdentifierInfo!"); - - // If this token is a macro that should be expanded inside the current - // macro. - if (getMacroInfoForLocation(PP, SM, II, T.getLocation())) { - getMacroNameAndPrintExpansion(Printer, T.getLocation(), PP, - MExpInfo.ParamMap, AlreadyProcessedTokens); - - // If this is a function-like macro, skip its arguments, as - // getExpandedMacro() already printed them. If this is the case, let's - // first jump to the '(' token. - auto N = std::next(It); - if (N != E && N->is(tok::l_paren)) - It = getMatchingRParen(++It, E); - continue; - } - - // If this token is the current macro's argument, we should expand it. - auto ParamToArgIt = MExpInfo.ParamMap.find(II); - if (ParamToArgIt != MExpInfo.ParamMap.end()) { - for (MacroInfo::tokens_iterator ArgIt = ParamToArgIt->second.begin(), - ArgEnd = ParamToArgIt->second.end(); - ArgIt != ArgEnd; ++ArgIt) { - - // These tokens may still be macros, if that is the case, handle it the - // same way we did above. - const auto *ArgII = ArgIt->getIdentifierInfo(); - if (!ArgII) { - Printer.printToken(*ArgIt); - continue; - } - - const auto *MI = PP.getMacroInfo(ArgII); - if (!MI) { - Printer.printToken(*ArgIt); - continue; - } - - getMacroNameAndPrintExpansion(Printer, ArgIt->getLocation(), PP, - MExpInfo.ParamMap, - AlreadyProcessedTokens); - // Peek the next token if it is a tok::l_paren. This way we can decide - // if this is the application or just a reference to a function maxro - // symbol: - // - // #define apply(f) ... - // #define func(x) ... - // apply(func) - // apply(func(42)) - auto N = std::next(ArgIt); - if (N != ArgEnd && N->is(tok::l_paren)) - ArgIt = getMatchingRParen(++ArgIt, ArgEnd); - } - continue; - } - - // If control reached here, then this token isn't a macro identifier, nor an - // unexpanded macro argument that we need to handle, print it. - Printer.printToken(T); +static std::optional<StringRef> +getExpandedMacro(SourceLocation MacroExpansionLoc, + const cross_tu::CrossTranslationUnitContext &CTU, + const MacroExpansionContext &MacroExpansions, + const SourceManager &SM) { + if (auto CTUMacroExpCtx = + CTU.getMacroExpansionContextForSourceLocation(MacroExpansionLoc)) { + return CTUMacroExpCtx->getExpandedText(MacroExpansionLoc); } - - AlreadyProcessedTokens.erase(MacroNameII); - - return MExpInfo.Name; -} - -static MacroExpansionInfo -getMacroExpansionInfo(const MacroParamMap &PrevParamMap, - SourceLocation ExpanLoc, const Preprocessor &PP) { - - const SourceManager &SM = PP.getSourceManager(); - const LangOptions &LangOpts = PP.getLangOpts(); - - // First, we create a Lexer to lex *at the expansion location* the tokens - // referring to the macro's name and its arguments. - TokenStream TStream(ExpanLoc, SM, LangOpts); - - // Acquire the macro's name. - Token TheTok; - TStream.next(TheTok); - - std::string MacroName = PP.getSpelling(TheTok); - - const auto *II = PP.getIdentifierInfo(MacroName); - assert(II && "Failed to acquire the IdentifierInfo for the macro!"); - - const MacroInfo *MI = getMacroInfoForLocation(PP, SM, II, ExpanLoc); - // assert(MI && "The macro must've been defined at it's expansion location!"); - // - // We should always be able to obtain the MacroInfo in a given TU, but if - // we're running the analyzer with CTU, the Preprocessor won't contain the - // directive history (or anything for that matter) from another TU. - // TODO: assert when we're not running with CTU. - if (!MI) - return { MacroName, MI, {} }; - - // Acquire the macro's arguments at the expansion point. - // - // The rough idea here is to lex from the first left parentheses to the last - // right parentheses, and map the macro's parameter to what they will be - // expanded to. A macro argument may contain several token (like '3 + 4'), so - // we'll lex until we find a tok::comma or tok::r_paren, at which point we - // start lexing the next argument or finish. - ArrayRef<const IdentifierInfo *> MacroParams = MI->params(); - if (MacroParams.empty()) - return { MacroName, MI, {} }; - - TStream.next(TheTok); - // When this is a token which expands to another macro function then its - // parentheses are not at its expansion locaiton. For example: - // - // #define foo(x) int bar() { return x; } - // #define apply_zero(f) f(0) - // apply_zero(foo) - // ^ - // This is not a tok::l_paren, but foo is a function. - if (TheTok.isNot(tok::l_paren)) - return { MacroName, MI, {} }; - - MacroParamMap ParamMap; - - // When the argument is a function call, like - // CALL_FN(someFunctionName(param1, param2)) - // we will find tok::l_paren, tok::r_paren, and tok::comma that do not divide - // actual macro arguments, or do not represent the macro argument's closing - // parentheses, so we'll count how many parentheses aren't closed yet. - // If ParanthesesDepth - // * = 0, then there are no more arguments to lex. - // * = 1, then if we find a tok::comma, we can start lexing the next arg. - // * > 1, then tok::comma is a part of the current arg. - int ParenthesesDepth = 1; - - // If we encounter the variadic arg, we will lex until the closing - // tok::r_paren, even if we lex a tok::comma and ParanthesesDepth == 1. - const IdentifierInfo *VariadicParamII = PP.getIdentifierInfo("__VA_ARGS__"); - if (MI->isGNUVarargs()) { - // If macro uses GNU-style variadic args, the param name is user-supplied, - // an not "__VA_ARGS__". E.g.: - // #define FOO(a, b, myvargs...) - // In this case, just use the last parameter: - VariadicParamII = *(MacroParams.rbegin()); - } - - for (const IdentifierInfo *CurrParamII : MacroParams) { - MacroParamMap::mapped_type ArgTokens; - - // One could also simply not supply a single argument to __VA_ARGS__ -- this - // results in a preprocessor warning, but is not an error: - // #define VARIADIC(ptr, ...) \ - // someVariadicTemplateFunction(__VA_ARGS__) - // - // int *ptr; - // VARIADIC(ptr); // Note that there are no commas, this isn't just an - // // empty parameter -- there are no parameters for '...'. - // In any other case, ParenthesesDepth mustn't be 0 here. - if (ParenthesesDepth != 0) { - - // Lex the first token of the next macro parameter. - TStream.next(TheTok); - - while (CurrParamII == VariadicParamII || ParenthesesDepth != 1 || - !TheTok.is(tok::comma)) { - assert(TheTok.isNot(tok::eof) && - "EOF encountered while looking for expanded macro args!"); - - if (TheTok.is(tok::l_paren)) - ++ParenthesesDepth; - - if (TheTok.is(tok::r_paren)) - --ParenthesesDepth; - - if (ParenthesesDepth == 0) - break; - - if (TheTok.is(tok::raw_identifier)) { - PP.LookUpIdentifierInfo(TheTok); - // This token is a variadic parameter: - // - // #define PARAMS_RESOLVE_TO_VA_ARGS(i, fmt) foo(i, fmt); \ - // i = 0; - // #define DISPATCH(...) \ - // PARAMS_RESOLVE_TO_VA_ARGS(__VA_ARGS__); - // // ^~~~~~~~~~~ Variadic parameter here - // - // void multipleParamsResolveToVA_ARGS(void) { - // int x = 1; - // DISPATCH(x, "LF1M healer"); // Multiple arguments are mapped to - // // a single __VA_ARGS__ parameter. - // (void)(10 / x); - // } - // - // We will stumble across this while trying to expand - // PARAMS_RESOLVE_TO_VA_ARGS. By this point, we already noted during - // the processing of DISPATCH what __VA_ARGS__ maps to, so we'll - // retrieve the next series of tokens from that. - if (TheTok.getIdentifierInfo() == VariadicParamII) { - TStream.injectRange(PrevParamMap.at(VariadicParamII)); - TStream.next(TheTok); - continue; - } - } - - ArgTokens.push_back(TheTok); - TStream.next(TheTok); - } - } else { - assert(CurrParamII == VariadicParamII && - "No more macro arguments are found, but the current parameter " - "isn't the variadic arg!"); - } - - ParamMap.emplace(CurrParamII, std::move(ArgTokens)); - } - - assert(TheTok.is(tok::r_paren) && - "Expanded macro argument acquisition failed! After the end of the loop" - " this token should be ')'!"); - - return {MacroName, MI, ParamMap}; -} - -static MacroInfo::tokens_iterator getMatchingRParen( - MacroInfo::tokens_iterator It, - MacroInfo::tokens_iterator End) { - - assert(It->is(tok::l_paren) && "This token should be '('!"); - - // Skip until we find the closing ')'. - int ParenthesesDepth = 1; - while (ParenthesesDepth != 0) { - ++It; - - assert(It->isNot(tok::eof) && - "Encountered EOF while attempting to skip macro arguments!"); - assert(It != End && - "End of the macro definition reached before finding ')'!"); - - if (It->is(tok::l_paren)) - ++ParenthesesDepth; - - if (It->is(tok::r_paren)) - --ParenthesesDepth; - } - return It; -} - -static const MacroInfo *getMacroInfoForLocation(const Preprocessor &PP, - const SourceManager &SM, - const IdentifierInfo *II, - SourceLocation Loc) { - - const MacroDirective *MD = PP.getLocalMacroDirectiveHistory(II); - if (!MD) - return nullptr; - - return MD->findDirectiveAtLoc(Loc, SM).getMacroInfo(); -} - -void MacroParamMap::expandFromPrevMacro(const MacroParamMap &Super) { - - for (value_type &Pair : *this) { - ArgTokensTy &CurrArgTokens = Pair.second; - - // For each token in the expanded macro argument. - auto It = CurrArgTokens.begin(); - while (It != CurrArgTokens.end()) { - if (It->isNot(tok::identifier)) { - ++It; - continue; - } - - const auto *II = It->getIdentifierInfo(); - assert(II); - - // Is this an argument that "Super" expands further? - if (!Super.count(II)) { - ++It; - continue; - } - - const ArgTokensTy &SuperArgTokens = Super.at(II); - - It = CurrArgTokens.insert(It, SuperArgTokens.begin(), - SuperArgTokens.end()); - std::advance(It, SuperArgTokens.size()); - It = CurrArgTokens.erase(It); - } - } -} - -void MacroParamMap::dumpToStream(llvm::raw_ostream &Out, - const Preprocessor &PP) const { - for (const std::pair<const IdentifierInfo *, ArgTokensTy> Pair : *this) { - Out << Pair.first->getName() << " -> "; - dumpArgTokensToStream(Out, PP, Pair.second); - Out << '\n'; - } -} - -static void dumpArgTokensToStream(llvm::raw_ostream &Out, - const Preprocessor &PP, - const ArgTokensTy &Toks) { - TokenPrinter Printer(Out, PP); - for (Token Tok : Toks) - Printer.printToken(Tok); -} - -void TokenPrinter::printToken(const Token &Tok) { - // TODO: Handle GNU extensions where hash and hashhash occurs right before - // __VA_ARGS__. - // cppreference.com: "some compilers offer an extension that allows ## to - // appear after a comma and before __VA_ARGS__, in which case the ## does - // nothing when the variable arguments are present, but removes the comma when - // the variable arguments are not present: this makes it possible to define - // macros such as fprintf (stderr, format, ##__VA_ARGS__)" - // FIXME: Handle named variadic macro parameters (also a GNU extension). - - // If this is the first token to be printed, don't print space. - if (PrevTok.isNot(tok::unknown)) { - // If the tokens were already space separated, or if they must be to avoid - // them being implicitly pasted, add a space between them. - if(Tok.hasLeadingSpace() || ConcatInfo.AvoidConcat(PrevPrevTok, PrevTok, - Tok)) { - // AvoidConcat doesn't check for ##, don't print a space around it. - if (PrevTok.isNot(tok::hashhash) && Tok.isNot(tok::hashhash)) { - OS << ' '; - } - } - } - - if (!Tok.isOneOf(tok::hash, tok::hashhash)) { - if (PrevTok.is(tok::hash)) - OS << '\"' << PP.getSpelling(Tok) << '\"'; - else - OS << PP.getSpelling(Tok); - } - - PrevPrevTok = PrevTok; - PrevTok = Tok; + return MacroExpansions.getExpandedText(MacroExpansionLoc); } |