diff --git a/clang/lib/StaticAnalyzer/Core/PlistDiagnostics.cpp b/clang/lib/StaticAnalyzer/Core/PlistDiagnostics.cpp index 7c74a917f6d0..a5950aeb9be9 100644 --- a/clang/lib/StaticAnalyzer/Core/PlistDiagnostics.cpp +++ b/clang/lib/StaticAnalyzer/Core/PlistDiagnostics.cpp @@ -16,7 +16,6 @@ #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" @@ -401,6 +400,12 @@ void PlistPrinter::ReportNote(raw_ostream &o, const PathDiagnosticNotePiece& P, // Static function definitions. //===----------------------------------------------------------------------===// +static ExpansionInfo getExpandedMacro(SourceLocation MacroLoc, + const Preprocessor &PP) { + // TODO: Implement macro expansion. + return { "", "" }; +} + /// Print coverage information to output stream {@code o}. /// May modify the used list of files {@code Fids} by inserting new ones. static void printCoverage(const PathDiagnostic *D, @@ -716,196 +721,3 @@ void PlistDiagnostics::FlushDiagnosticsImpl( // 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 getMacroNameAndPrintExpansion(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); - -/// 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) { - - llvm::SmallString<200> ExpansionBuf; - llvm::raw_svector_ostream OS(ExpansionBuf); - TokenPrinter Printer(OS, PP); - return { getMacroNameAndPrintExpansion(Printer, MacroLoc, PP), OS.str() }; -} - -static std::string getMacroNameAndPrintExpansion(TokenPrinter &Printer, - SourceLocation MacroLoc, - const Preprocessor &PP) { - - const SourceManager &SM = PP.getSourceManager(); - - MacroNameAndInfo Info = getMacroNameAndInfo(SM.getExpansionLoc(MacroLoc), PP); - 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 = - getMacroInfoForLocation(PP, SM, II, T.getLocation())) { - getMacroNameAndPrintExpansion(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 Info.Name; -} - -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 = getMacroInfoForLocation(PP, SM, II, ExpanLoc); - assert(MI && "The macro must've been defined at it's expansion location!"); - - 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; -} - -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 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; -} diff --git a/clang/test/Analysis/Inputs/expected-plists/plist-macros-with-expansion.cpp.plist b/clang/test/Analysis/Inputs/expected-plists/plist-macros-with-expansion.cpp.plist index f544765bffcc..c6f9c32a8006 100644 --- a/clang/test/Analysis/Inputs/expected-plists/plist-macros-with-expansion.cpp.plist +++ b/clang/test/Analysis/Inputs/expected-plists/plist-macros-with-expansion.cpp.plist @@ -143,8 +143,8 @@ col3 file0 - nameSET_PTR_VAR_TO_NULL - expansionptr = 0 + name + expansion descriptionDereference of null pointer (loaded from variable 'ptr') @@ -312,8 +312,8 @@ col3 file0 - nameSET_PTR_VAR_TO_NULL_WITH_NESTED_MACRO - expansionptr =0 + name + expansion descriptionDereference of null pointer (loaded from variable 'ptr') @@ -342,1047 +342,10 @@ - - path - - - kindcontrol - edges - - - start - - - line58 - col3 - file0 - - - line58 - col5 - file0 - - - end - - - line59 - col3 - file0 - - - line59 - col9 - file0 - - - - - - - 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') - - - macro_expansions - - - location - - line59 - col3 - file0 - - nameTO_NULL - expansionsetToNull(x) - - - 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 - - - - - - - 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' - - - 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') - - - macro_expansions - - - location - - line80 - col3 - file0 - - nameTO_NULL - expansionsetToNull(x) - - - location - - line81 - col3 - file0 - - nameDEREF - expansion{ int b; b = 5; } print(x); *x - - - 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 - - - - - path - - - kindcontrol - edges - - - start - - - line100 - col3 - file0 - - - line100 - col5 - file0 - - - end - - - line101 - col3 - file0 - - - line101 - col28 - file0 - - - - - - - kindevent - location - - line101 - col3 - file0 - - ranges - - - - line101 - col3 - file0 - - - line101 - col33 - file0 - - - - depth0 - extended_message - Null pointer value stored to 'ptr' - message - Null pointer value stored to 'ptr' - - - kindcontrol - edges - - - start - - - line102 - col3 - file0 - - - line102 - col3 - file0 - - - end - - - line102 - col8 - file0 - - - line102 - col8 - file0 - - - - - - - kindevent - location - - line102 - col8 - file0 - - ranges - - - - line102 - col4 - file0 - - - line102 - col6 - file0 - - - - depth0 - extended_message - Dereference of null pointer (loaded from variable 'ptr') - message - Dereference of null pointer (loaded from variable 'ptr') - - - macro_expansions - - - location - - line101 - col3 - file0 - - nameWILL_UNDEF_SET_NULL_TO_PTR - expansionptr = nullptr; - - - descriptionDereference of null pointer (loaded from variable 'ptr') - categoryLogic error - typeDereference of null pointer - check_namecore.NullDereference - - issue_hash_content_of_line_in_context79ce7ac344a15505929edba2fdf178b6 - issue_context_kindfunction - issue_contextundefinedMacroByTheEndOfParsingTest - issue_hash_function_offset3 - location - - line102 - col8 - file0 - - ExecutedLines - - 0 - - 99 - 100 - 101 - 102 - - - - - path - - - kindcontrol - edges - - - start - - - line118 - col3 - file0 - - - line118 - col5 - file0 - - - end - - - line119 - col3 - file0 - - - line119 - col42 - file0 - - - - - - - kindevent - location - - line119 - col3 - file0 - - ranges - - - - line119 - col3 - file0 - - - line119 - col47 - file0 - - - - depth0 - extended_message - Null pointer value stored to 'ptr' - message - Null pointer value stored to 'ptr' - - - kindcontrol - edges - - - start - - - line120 - col3 - file0 - - - line120 - col3 - file0 - - - end - - - line120 - col8 - file0 - - - line120 - col8 - file0 - - - - - - - kindevent - location - - line120 - col8 - file0 - - ranges - - - - line120 - col4 - file0 - - - line120 - col6 - file0 - - - - depth0 - extended_message - Dereference of null pointer (loaded from variable 'ptr') - message - Dereference of null pointer (loaded from variable 'ptr') - - - macro_expansions - - - location - - line119 - col3 - file0 - - nameWILL_REDIFINE_MULTIPLE_TIMES_SET_TO_NULL - expansionptr = nullptr; - - - descriptionDereference of null pointer (loaded from variable 'ptr') - categoryLogic error - typeDereference of null pointer - check_namecore.NullDereference - - issue_hash_content_of_line_in_contextcbbecfb64198aebb884f3729dff84896 - issue_context_kindfunction - issue_contextmacroRedefinedMultipleTimesTest - issue_hash_function_offset3 - location - - line120 - col8 - file0 - - ExecutedLines - - 0 - - 117 - 118 - 119 - 120 - - - - - path - - - kindcontrol - edges - - - start - - - line139 - col3 - file0 - - - line139 - col5 - file0 - - - end - - - line140 - col3 - file0 - - - line140 - col39 - file0 - - - - - - - kindevent - location - - line140 - col3 - file0 - - ranges - - - - line140 - col3 - file0 - - - line140 - col44 - file0 - - - - depth0 - extended_message - Null pointer value stored to 'ptr' - message - Null pointer value stored to 'ptr' - - - kindcontrol - edges - - - start - - - line141 - col3 - file0 - - - line141 - col3 - file0 - - - end - - - line141 - col8 - file0 - - - line141 - col8 - file0 - - - - - - - kindevent - location - - line141 - col8 - file0 - - ranges - - - - line141 - col4 - file0 - - - line141 - col6 - file0 - - - - depth0 - extended_message - Dereference of null pointer (loaded from variable 'ptr') - message - Dereference of null pointer (loaded from variable 'ptr') - - - macro_expansions - - - location - - line140 - col3 - file0 - - namePASS_PTR_TO_MACRO_THAT_WILL_BE_UNDEFD - expansionptr = nullptr; - - - descriptionDereference of null pointer (loaded from variable 'ptr') - categoryLogic error - typeDereference of null pointer - check_namecore.NullDereference - - issue_hash_content_of_line_in_context01684c77381713fd6c7be31ebc9b647a - issue_context_kindfunction - issue_contextundefinedMacroInsideAnotherMacroTest - issue_hash_function_offset3 - location - - line141 - col8 - file0 - - ExecutedLines - - 0 - - 138 - 139 - 140 - 141 - - - files - /home/szelethus/Documents/macro_expansion/clang/test/Analysis/plist-macros-with-expansion.cpp + /home/eumakri/Documents/2codechecker_dev_env/llvm/tools/clang/test/Analysis/plist-macros-with-expansion.cpp diff --git a/clang/test/Analysis/plist-macros-with-expansion.cpp b/clang/test/Analysis/plist-macros-with-expansion.cpp index e7819fff78cc..ff41a5f11512 100644 --- a/clang/test/Analysis/plist-macros-with-expansion.cpp +++ b/clang/test/Analysis/plist-macros-with-expansion.cpp @@ -27,8 +27,8 @@ void nonFunctionLikeMacroTest() { *ptr = 5; // expected-warning{{Dereference of null pointer}} } -// CHECK: nameSET_PTR_VAR_TO_NULL -// CHECK-NEXT: expansionptr = 0 +// CHECK: name +// CHECK-NEXT: expansion #define NULL 0 #define SET_PTR_VAR_TO_NULL_WITH_NESTED_MACRO \ @@ -40,109 +40,5 @@ void nonFunctionLikeNestedMacroTest() { *ptr = 5; // expected-warning{{Dereference of null pointer}} } -// CHECK: nameSET_PTR_VAR_TO_NULL_WITH_NESTED_MACRO -// CHECK-NEXT: 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 arguments. -// 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 arguments. -// CHECK: nameTO_NULL -// CHECK-NEXT: expansionsetToNull(x) - -// TODO: Expand arguments. -// CHECK: nameDEREF -// CHECK-NEXT: expansion{ int b; b = 5; } print(x); *x - -//===----------------------------------------------------------------------===// -// Tests for undefining and/or redifining macros. -//===----------------------------------------------------------------------===// - -#define WILL_UNDEF_SET_NULL_TO_PTR(ptr) \ - ptr = nullptr; - -void undefinedMacroByTheEndOfParsingTest() { - int *ptr; - WILL_UNDEF_SET_NULL_TO_PTR(ptr); - *ptr = 5; // expected-warning{{Dereference of null pointer}} -} - -#undef WILL_UNDEF_SET_NULL_TO_PTR - -// TODO: Expand arguments. -// CHECK: nameWILL_UNDEF_SET_NULL_TO_PTR -// CHECK-NEXT: expansionptr = nullptr; - -#define WILL_REDIFINE_MULTIPLE_TIMES_SET_TO_NULL(ptr) \ - /* Nothing */ -#undef WILL_REDIFINE_MULTIPLE_TIMES_SET_TO_NULL -#define WILL_REDIFINE_MULTIPLE_TIMES_SET_TO_NULL(ptr) \ - ptr = nullptr; - -void macroRedefinedMultipleTimesTest() { - int *ptr; - WILL_REDIFINE_MULTIPLE_TIMES_SET_TO_NULL(ptr) - *ptr = 5; // expected-warning{{Dereference of null pointer}} -} - -#undef WILL_REDIFINE_MULTIPLE_TIMES_SET_TO_NULL -#define WILL_REDIFINE_MULTIPLE_TIMES_SET_TO_NULL(ptr) \ - print("This string shouldn't be in the plist file at all. Or anywhere, " \ - "but here."); - -// TODO: Expand arguments. -// CHECK: nameWILL_REDIFINE_MULTIPLE_TIMES_SET_TO_NULL -// CHECK-NEXT: expansionptr = nullptr; - -#define WILL_UNDEF_SET_NULL_TO_PTR_2(ptr) \ - ptr = nullptr; - -#define PASS_PTR_TO_MACRO_THAT_WILL_BE_UNDEFD(ptr) \ - WILL_UNDEF_SET_NULL_TO_PTR_2(ptr) - -void undefinedMacroInsideAnotherMacroTest() { - int *ptr; - PASS_PTR_TO_MACRO_THAT_WILL_BE_UNDEFD(ptr); - *ptr = 5; // expected-warning{{Dereference of null pointer}} -} - -// TODO: Expand arguments. -// CHECK: namePASS_PTR_TO_MACRO_THAT_WILL_BE_UNDEFD -// CHECK-NEXT: expansionptr = nullptr; - -#undef WILL_UNDEF_SET_NULL_TO_PTR_2 +// CHECK: name +// CHECK-NEXT: expansion