Index: lib/StaticAnalyzer/Core/PlistDiagnostics.cpp =================================================================== --- lib/StaticAnalyzer/Core/PlistDiagnostics.cpp +++ lib/StaticAnalyzer/Core/PlistDiagnostics.cpp @@ -16,6 +16,7 @@ #include "clang/Basic/SourceManager.h" #include "clang/Basic/Version.h" #include "clang/Lex/Preprocessor.h" +#include "clang/Lex/TokenConcatenation.h" #include "clang/Rewrite/Core/HTMLRewrite.h" #include "clang/StaticAnalyzer/Core/AnalyzerOptions.h" #include "clang/StaticAnalyzer/Core/BugReporter/PathDiagnostic.h" @@ -269,10 +270,7 @@ } // end of anonymous namespace static ExpansionInfo getExpandedMacro(SourceLocation MacroLoc, - const Preprocessor &PP) { - // TODO: Implement macro expansion. - return { "", "" }; -} + const Preprocessor &PP); static void ReportMacro(raw_ostream &o, const PathDiagnosticMacroPiece& P, @@ -665,3 +663,176 @@ // Finish. o << "\n"; } + +//===----------------------------------------------------------------------===// +// Declarations of helper functions and data structures for expanding macros. +//===----------------------------------------------------------------------===// + +namespace { + +struct MacroNameAndInfo { + std::string Name; + const MacroInfo *MI = nullptr; + + MacroNameAndInfo(std::string N, const MacroInfo *MI) + : Name(std::move(N)), MI(MI) {} +}; + +/// Helper class for printing tokens. +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); +}; + +} // end of anonymous namespace + +static std::string getExpandedMacroImpl(TokenPrinter &Printer, + SourceLocation MacroLoc, + const Preprocessor &PP); + +/// Retrieves the name of the macro and its MacroInfo. +static MacroNameAndInfo getMacroNameAndInfo(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); + +//===----------------------------------------------------------------------===// +// Definitions of helper functions and methods for expanding macros. +//===----------------------------------------------------------------------===// + +static ExpansionInfo getExpandedMacro(SourceLocation MacroLoc, + const Preprocessor &PP) { + + llvm::SmallString<200> ExpansionBuf; + llvm::raw_svector_ostream OS(ExpansionBuf); + TokenPrinter Printer(OS, PP); + return { getExpandedMacroImpl(Printer, MacroLoc, PP), OS.str() }; +} + +static std::string getExpandedMacroImpl(TokenPrinter &Printer, + SourceLocation MacroLoc, + const Preprocessor &PP) { + + const SourceManager &SM = PP.getSourceManager(); + + MacroNameAndInfo Info = getMacroNameAndInfo(SM.getExpansionLoc(MacroLoc), PP); + std::string MacroName = std::move(Info.Name); + const MacroInfo *MI = Info.MI; + + // Iterate over the macro's tokens and stringify them. + for (auto It = MI->tokens_begin(), E = 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 currect + // macro. + if (const MacroInfo *MI = PP.getMacroInfo(II)) { + getExpandedMacroImpl(Printer, T.getLocation(), PP); + + // If this is a function-like macro, skip its arguments, as + // getExpandedMacro() already printed them. If this is the case, let's + // first jumo to the '(' token. + if (MI->getNumParams() != 0) + It = getMatchingRParen(++It, E); + continue; + } + + // If control reached here, then this token isn't a macro identifier, print + // it. + Printer.printToken(T); + } + + return MacroName; +} + +static MacroNameAndInfo getMacroNameAndInfo(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. + std::pair LocInfo = SM.getDecomposedLoc(ExpanLoc); + const llvm::MemoryBuffer *MB = SM.getBuffer(LocInfo.first); + const char *MacroNameTokenPos = MB->getBufferStart() + LocInfo.second; + + Lexer RawLexer(SM.getLocForStartOfFile(LocInfo.first), LangOpts, + MB->getBufferStart(), MacroNameTokenPos, MB->getBufferEnd()); + + // Acquire the macro's name. + Token TheTok; + RawLexer.LexFromRawLexer(TheTok); + + std::string MacroName = PP.getSpelling(TheTok); + + const auto *II = PP.getIdentifierInfo(MacroName); + assert(II && "Failed to acquire the IndetifierInfo for the macro!"); + const MacroInfo *MI = PP.getMacroInfo(II); + assert(MI && "This IdentifierInfo should refer to a macro!"); + + return { MacroName, MI }; +} + +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 ParanthesesDepth = 1; + while (ParanthesesDepth != 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)) + ++ParanthesesDepth; + + if (It->is(tok::r_paren)) + --ParanthesesDepth; + } + return It; +} + +inline void TokenPrinter::printToken(const Token &Tok) { + // If the tokens were already space separated, or if they must be to avoid + // them being implicitly pasted, add a space between them. + // If this is the first token to be printed, don't print space. + if (PrevTok.isNot(tok::unknown) && (Tok.hasLeadingSpace() || + ConcatInfo.AvoidConcat(PrevPrevTok, PrevTok, Tok))) + OS << ' '; + + OS << PP.getSpelling(Tok); + + PrevPrevTok = PrevTok; + PrevTok = Tok; +} Index: test/Analysis/Inputs/expected-plists/plist-macros-with-expansion.cpp.plist =================================================================== --- test/Analysis/Inputs/expected-plists/plist-macros-with-expansion.cpp.plist +++ test/Analysis/Inputs/expected-plists/plist-macros-with-expansion.cpp.plist @@ -49,8 +49,8 @@ col3 file0 - name - expansion + nameSET_PTR_VAR_TO_NULL + expansion ptr = 0 kindevent @@ -216,8 +216,8 @@ col3 file0 - name - expansion + nameSET_PTR_VAR_TO_NULL_WITH_NESTED_MACRO + expansion ptr =0 kindevent @@ -338,6 +338,533 @@ + + path + + + kindcontrol + edges + + + start + + + line58 + col3 + file0 + + + line58 + col5 + file0 + + + end + + + line59 + col3 + file0 + + + line59 + col9 + file0 + + + + + + + kindmacro_expansion + location + + line59 + col3 + file0 + + nameTO_NULL + expansion setToNull(x) + + + kindevent + location + + line59 + col3 + file0 + + ranges + + + + line59 + col3 + file0 + + + line59 + col15 + file0 + + + + depth0 + extended_message + Calling 'setToNull' + message + Calling 'setToNull' + + + kindevent + location + + line50 + col1 + file0 + + depth1 + extended_message + Entered call from 'functionLikeMacroTest' + message + Entered call from 'functionLikeMacroTest' + + + kindcontrol + edges + + + start + + + line50 + col1 + file0 + + + line50 + col4 + file0 + + + end + + + line51 + col3 + file0 + + + line51 + col3 + file0 + + + + + + + kindevent + location + + line51 + col3 + file0 + + ranges + + + + line51 + col3 + file0 + + + line51 + col17 + file0 + + + + depth1 + extended_message + Null pointer value stored to 'ptr' + message + Null pointer value stored to 'ptr' + + + kindevent + location + + line59 + col3 + file0 + + ranges + + + + line59 + col3 + file0 + + + line59 + col15 + file0 + + + + depth0 + extended_message + Returning from 'setToNull' + message + Returning from 'setToNull' + + + kindcontrol + edges + + + start + + + line60 + col3 + file0 + + + line60 + col3 + file0 + + + end + + + line60 + col8 + file0 + + + line60 + col8 + file0 + + + + + + + kindevent + location + + line60 + col8 + file0 + + ranges + + + + line60 + col4 + file0 + + + line60 + col6 + file0 + + + + depth0 + extended_message + Dereference of null pointer (loaded from variable 'ptr') + message + Dereference of null pointer (loaded from variable 'ptr') + + + descriptionDereference of null pointer (loaded from variable 'ptr') + categoryLogic error + typeDereference of null pointer + check_namecore.NullDereference + + issue_hash_content_of_line_in_context370a457744311752aac789447b4ef16c + issue_context_kindfunction + issue_contextfunctionLikeMacroTest + issue_hash_function_offset3 + location + + line60 + col8 + file0 + + ExecutedLines + + 0 + + 50 + 51 + 57 + 58 + 59 + 60 + + + + + path + + + kindcontrol + edges + + + start + + + line79 + col3 + file0 + + + line79 + col5 + file0 + + + end + + + line80 + col3 + file0 + + + line80 + col9 + file0 + + + + + + + kindmacro_expansion + location + + line80 + col3 + file0 + + nameTO_NULL + expansion setToNull(x) + + + kindevent + location + + line80 + col3 + file0 + + ranges + + + + line80 + col3 + file0 + + + line80 + col13 + file0 + + + + depth0 + extended_message + Calling 'setToNull' + message + Calling 'setToNull' + + + kindevent + location + + line50 + col1 + file0 + + depth1 + extended_message + Entered call from 'functionLikeNestedMacroTest' + message + Entered call from 'functionLikeNestedMacroTest' + + + kindcontrol + edges + + + start + + + line50 + col1 + file0 + + + line50 + col4 + file0 + + + end + + + line51 + col3 + file0 + + + line51 + col3 + file0 + + + + + + + kindevent + location + + line51 + col3 + file0 + + ranges + + + + line51 + col3 + file0 + + + line51 + col17 + file0 + + + + depth1 + extended_message + Null pointer value stored to 'a' + message + Null pointer value stored to 'a' + + + kindevent + location + + line80 + col3 + file0 + + ranges + + + + line80 + col3 + file0 + + + line80 + col13 + file0 + + + + depth0 + extended_message + Returning from 'setToNull' + message + Returning from 'setToNull' + + + kindmacro_expansion + location + + line81 + col3 + file0 + + nameDEREF + expansion { int b; b = 5; } print(x); *x + + + kindevent + location + + line81 + col12 + file0 + + ranges + + + + line81 + col3 + file0 + + + line81 + col10 + file0 + + + + depth0 + extended_message + Dereference of null pointer (loaded from variable 'a') + message + Dereference of null pointer (loaded from variable 'a') + + + descriptionDereference of null pointer (loaded from variable 'a') + categoryLogic error + typeDereference of null pointer + check_namecore.NullDereference + + issue_hash_content_of_line_in_context873802674657bba4565f64c7bbf0ded9 + issue_context_kindfunction + issue_contextfunctionLikeNestedMacroTest + issue_hash_function_offset3 + location + + line81 + col12 + file0 + + ExecutedLines + + 0 + + 50 + 51 + 78 + 79 + 80 + 81 + + + files Index: test/Analysis/plist-macros-with-expansion.cpp =================================================================== --- test/Analysis/plist-macros-with-expansion.cpp +++ test/Analysis/plist-macros-with-expansion.cpp @@ -27,8 +27,8 @@ *ptr = 5; // expected-warning{{Dereference of null pointer}} } -// CHECK: name -// CHECK: expansion +// CHECK: nameSET_PTR_VAR_TO_NULL +// CHECK: expansionptr = 0 #define NULL 0 #define SET_PTR_VAR_TO_NULL_WITH_NESTED_MACRO \ @@ -40,5 +40,51 @@ *ptr = 5; // expected-warning{{Dereference of null pointer}} } -// CHECK: name -// CHECK: expansion +// CHECK: nameSET_PTR_VAR_TO_NULL_WITH_NESTED_MACRO +// CHECK: expansionptr =0 + +//===----------------------------------------------------------------------===// +// Tests for function-like macro expansions. +//===----------------------------------------------------------------------===// + +void setToNull(int **vptr) { + *vptr = nullptr; +} + +#define TO_NULL(x) \ + setToNull(x) + +void functionLikeMacroTest() { + int *ptr; + TO_NULL(&ptr); + *ptr = 5; // expected-warning{{Dereference of null pointer}} +} + +// TODO: Expand argumnts. +// CHECK: nameTO_NULL +// CHECK: expansionsetToNull(x) + +#define DOES_NOTHING(x) \ + { \ + int b; \ + b = 5; \ + } \ + print(x) + +#define DEREF(x) \ + DOES_NOTHING(x); \ + *x + +void functionLikeNestedMacroTest() { + int *a; + TO_NULL(&a); + DEREF(a) = 5; // expected-warning{{Dereference of null pointer}} +} + +// TODO: Expand argumnts. +// CHECK: nameTO_NULL +// CHECK: expansionsetToNull(x) + +// TODO: Expand argumnts. +// CHECK: nameDEREF +// CHECK: expansion{ int b; b = 5; } print(x); *x