Index: lib/StaticAnalyzer/Core/PlistDiagnostics.cpp =================================================================== --- lib/StaticAnalyzer/Core/PlistDiagnostics.cpp +++ lib/StaticAnalyzer/Core/PlistDiagnostics.cpp @@ -669,29 +669,73 @@ namespace { -struct MacroNameAndInfo { +using ExpArgTokens = llvm::SmallVector; + +/// Maps unexpanded macro arguments to expanded arguments. A macro argument may +/// need to expanded further when it is nested inside another macro. +class MacroArgMap : public std::map { +public: + void expandFromPrevMacro(const MacroArgMap &Super); +}; + +struct MacroNameAndArgs { std::string Name; const MacroInfo *MI = nullptr; + llvm::Optional Args; - MacroNameAndInfo(std::string N, const MacroInfo *MI) - : Name(std::move(N)), MI(MI) {} + MacroNameAndArgs(std::string N, const MacroInfo *MI, + llvm::Optional M) + : Name(std::move(N)), MI(MI), Args(std::move(M)) {} }; } // end of anonymous namespace -/// Retrieves the name of the macro and its MacroInfo. -static MacroNameAndInfo getMacroNameAndInfo(SourceLocation ExpanLoc, +/// Retrieves the name of the macro and what it's arguments 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 MacroNameAndArgs getMacroNameAndArgs(SourceLocation ExpanLoc, const Preprocessor &PP); static ExpansionInfo getExpandedMacroImpl(SourceLocation MacroLoc, - const Preprocessor &PP) { + const Preprocessor &PP, + MacroArgMap *PrevArgs) { const SourceManager &SM = PP.getSourceManager(); - MacroNameAndInfo Info = getMacroNameAndInfo(SM.getExpansionLoc(MacroLoc), PP); + MacroNameAndArgs Info = getMacroNameAndArgs(SM.getExpansionLoc(MacroLoc), PP); std::string MacroName = std::move(Info.Name); const MacroInfo *MI = Info.MI; + llvm::Optional Args = std::move(Info.Args); + + // If this macro is function-like. + if (Args) { + // If this macro is nested inside another one, let's manually expand its + // arguments from the previous macro. + if (PrevArgs) + Args->expandFromPrevMacro(*PrevArgs); + PrevArgs = Args.getPointer(); + } else + PrevArgs = nullptr; // Iterate over the macro's tokens and stringify them. llvm::SmallString<200> ExpansionBuf; @@ -708,7 +752,7 @@ // If this token is a macro that should be expanded inside the currect // macro. if (const MacroInfo *MI = PP.getMacroInfo(II)) { - ExpansionOS << getExpandedMacroImpl(T.getLocation(), PP) + ExpansionOS << getExpandedMacroImpl(T.getLocation(), PP, PrevArgs) .Expansion; // If this is a function-like macro, skip its arguments, as @@ -738,8 +782,18 @@ } continue; } + + // If this token is the current macro's argument, we should expand it. + if (Args && Args->count(II)) { + for (const Token &ExpandedArgT : Args->at(II)) { + ExpansionOS << PP.getSpelling(ExpandedArgT) + ' '; + } + continue; + } } + // TODO: Handle tok::hash and tok::hashhash. + // If control reaches here, there's nothing left to do, print the token. ExpansionOS << PP.getSpelling(T) + ' '; } @@ -749,10 +803,10 @@ static ExpansionInfo getExpandedMacro(SourceLocation MacroLoc, const Preprocessor &PP) { - return getExpandedMacroImpl(MacroLoc, PP); + return getExpandedMacroImpl(MacroLoc, PP, /* PrevArgs */ nullptr); } -static MacroNameAndInfo getMacroNameAndInfo(SourceLocation ExpanLoc, +static MacroNameAndArgs getMacroNameAndArgs(SourceLocation ExpanLoc, const Preprocessor &PP) { const SourceManager &SM = PP.getSourceManager(); @@ -772,11 +826,101 @@ 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 }; + // Acquire the macro's arguments. + // + // The rough idea here is to lex from the first left parantheses to the last + // right parantheses, and map the macro's unexpanded arguments to what they + // will be expanded to. An expanded macro argument may contain several tokens + // (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 MacroArgs = MI->params(); + if (MacroArgs.empty()) + return { MacroName, MI, None }; + + RawLexer.LexFromRawLexer(TheTok); + assert(TheTok.is(tok::l_paren) && + "The token after the macro's identifier token should be '('!"); + + MacroArgMap Args; + + // When the macro's 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 + // parantheses, so we'll count how many parantheses aren't closed yet. + int ParanthesesDepth = 1; + + for (unsigned Index = 0; Index < MI->getNumParams(); ++Index) { + MacroArgMap::mapped_type ExpandedArgTokens; + + // Lex the first token of the next macro parameter. + RawLexer.LexFromRawLexer(TheTok); + + while (TheTok.isNot(tok::comma) || ParanthesesDepth != 1) { + assert(TheTok.isNot(tok::eof) && + "EOF encountered while looking for expanded macro args!"); + + if (TheTok.is(tok::l_paren)) + ++ParanthesesDepth; + + if (TheTok.is(tok::r_paren)) + --ParanthesesDepth; + + if (ParanthesesDepth == 0) + break; + + if (TheTok.is(tok::raw_identifier)) + PP.LookUpIdentifierInfo(TheTok); + + ExpandedArgTokens.push_back(TheTok); + RawLexer.LexFromRawLexer(TheTok); + } + + Args.insert(std::make_pair(MacroArgs[Index], ExpandedArgTokens)); + } + + // TODO: The condition really should be TheTok.is(tok::r_paren), but variadic + // macro arguments are not handled yet. + assert(TheTok.isOneOf(tok::r_paren, tok::comma) && + "Expanded macro argument acquisition failed! After the end of the loop" + " this token should be ')'!"); + + return { MacroName, MI, Args }; +} + +void MacroArgMap::expandFromPrevMacro(const MacroArgMap &Super) { + + for (value_type &Pair : *this) { + ExpArgTokens &CurrExpArgTokens = Pair.second; + + // For each token in the expanded macro argument. + auto It = CurrExpArgTokens.begin(); + while (It != CurrExpArgTokens.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 ExpArgTokens &SuperExpArgTokens = Super.at(II); + + It = CurrExpArgTokens.insert( + It, SuperExpArgTokens.begin(), SuperExpArgTokens.end()); + std::advance(It, SuperExpArgTokens.size()); + It = CurrExpArgTokens.erase(It); + } + } } 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 @@ -3,7 +3,7 @@ clang_version -clang version 8.0.0 (http://mainstream.inf.elte.hu/Szelethus/clang 54f58baf2c799080816023e7f78b92e4f9460078) (https://github.com/llvm-mirror/llvm 1ffbf26a1a0a190d69327af875a3337b74a2ce82) +clang version 8.0.0 (http://mainstream.inf.elte.hu/Szelethus/clang 650e3f54f6a63798143c7181035a285f03dccb7f) (https://github.com/llvm-mirror/llvm 1ffbf26a1a0a190d69327af875a3337b74a2ce82) diagnostics @@ -386,7 +386,7 @@ file0 nameTO_NULL - expansionsetToNull ( x ) + expansionsetToNull ( & ptr ) kindevent @@ -661,7 +661,7 @@ file0 nameTO_NULL - expansionsetToNull ( x ) + expansionsetToNull ( & a ) kindevent @@ -807,7 +807,7 @@ file0 nameDEREF - expansion{ int b ; b = 5 ; } print ( x ) ; * x + expansion{ int b ; b = 5 ; } print ( a ) ; * a kindevent @@ -867,6 +867,3184 @@ + + path + + + kindcontrol + edges + + + start + + + line105 + col3 + file0 + + + line105 + col5 + file0 + + + end + + + line106 + col3 + file0 + + + line106 + col19 + file0 + + + + + + + kindmacro_expansion + location + + line106 + col3 + file0 + + nameTO_NULL_AND_PRINT + expansiona = 0 ; print ( "Will this , cause a crash?" ) + + + kindevent + location + + line106 + col3 + file0 + + ranges + + + + line106 + col3 + file0 + + + line106 + col52 + file0 + + + + depth0 + extended_message + Null pointer value stored to 'a' + message + Null pointer value stored to 'a' + + + kindcontrol + edges + + + start + + + line107 + col3 + file0 + + + line107 + col3 + file0 + + + end + + + line107 + col6 + file0 + + + line107 + col6 + file0 + + + + + + + kindevent + location + + line107 + col6 + file0 + + ranges + + + + line107 + col4 + file0 + + + line107 + col4 + 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_context7a7344244350405a514682fe228e304e + issue_context_kindfunction + issue_contextmacroArgContainsCommaInStringTest + issue_hash_function_offset3 + location + + line107 + col6 + file0 + + ExecutedLines + + 0 + + 104 + 105 + 106 + 107 + + + + + path + + + kindcontrol + edges + + + start + + + line114 + col3 + file0 + + + line114 + col5 + file0 + + + end + + + line115 + col3 + file0 + + + line115 + col19 + file0 + + + + + + + kindmacro_expansion + location + + line115 + col3 + file0 + + nameTO_NULL_AND_PRINT + expansiona = 0 ; print ( "Will this ( cause a crash?" ) + + + kindevent + location + + line115 + col3 + file0 + + ranges + + + + line115 + col3 + file0 + + + line115 + col52 + file0 + + + + depth0 + extended_message + Null pointer value stored to 'a' + message + Null pointer value stored to 'a' + + + kindcontrol + edges + + + start + + + line116 + col3 + file0 + + + line116 + col3 + file0 + + + end + + + line116 + col6 + file0 + + + line116 + col6 + file0 + + + + + + + kindevent + location + + line116 + col6 + file0 + + ranges + + + + line116 + col4 + file0 + + + line116 + col4 + 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_context1d6d14e3f566cec02bd1f3542e3c8044 + issue_context_kindfunction + issue_contextmacroArgContainsLParenInStringTest + issue_hash_function_offset3 + location + + line116 + col6 + file0 + + ExecutedLines + + 0 + + 113 + 114 + 115 + 116 + + + + + path + + + kindcontrol + edges + + + start + + + line123 + col3 + file0 + + + line123 + col5 + file0 + + + end + + + line124 + col3 + file0 + + + line124 + col19 + file0 + + + + + + + kindmacro_expansion + location + + line124 + col3 + file0 + + nameTO_NULL_AND_PRINT + expansiona = 0 ; print ( "Will this ) cause a crash?" ) + + + kindevent + location + + line124 + col3 + file0 + + ranges + + + + line124 + col3 + file0 + + + line124 + col52 + file0 + + + + depth0 + extended_message + Null pointer value stored to 'a' + message + Null pointer value stored to 'a' + + + kindcontrol + edges + + + start + + + line125 + col3 + file0 + + + line125 + col3 + file0 + + + end + + + line125 + col6 + file0 + + + line125 + col6 + file0 + + + + + + + kindevent + location + + line125 + col6 + file0 + + ranges + + + + line125 + col4 + file0 + + + line125 + col4 + 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_context7354d762d71f0d0a3ffc9d6d827fe580 + issue_context_kindfunction + issue_contextmacroArgContainsRParenInStringTest + issue_hash_function_offset3 + location + + line125 + col6 + file0 + + ExecutedLines + + 0 + + 122 + 123 + 124 + 125 + + + + + path + + + kindcontrol + edges + + + start + + + line137 + col3 + file0 + + + line137 + col5 + file0 + + + end + + + line138 + col3 + file0 + + + line138 + col15 + file0 + + + + + + + kindmacro_expansion + location + + line138 + col3 + file0 + + nameCALL_FUNCTION + expansionsetToNull ( & a ) + + + kindevent + location + + line138 + col3 + file0 + + ranges + + + + line138 + col3 + file0 + + + line138 + col30 + file0 + + + + depth0 + extended_message + Calling 'setToNull' + message + Calling 'setToNull' + + + kindevent + location + + line50 + col1 + file0 + + depth1 + extended_message + Entered call from 'macroArgContainsLParenRParenTest' + message + Entered call from 'macroArgContainsLParenRParenTest' + + + 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 + + line138 + col3 + file0 + + ranges + + + + line138 + col3 + file0 + + + line138 + col30 + file0 + + + + depth0 + extended_message + Returning from 'setToNull' + message + Returning from 'setToNull' + + + kindcontrol + edges + + + start + + + line139 + col3 + file0 + + + line139 + col3 + file0 + + + end + + + line139 + col6 + file0 + + + line139 + col6 + file0 + + + + + + + kindevent + location + + line139 + col6 + file0 + + ranges + + + + line139 + col4 + file0 + + + line139 + col4 + 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_contextf00b6f77288a374e864a58609e9a42ea + issue_context_kindfunction + issue_contextmacroArgContainsLParenRParenTest + issue_hash_function_offset3 + location + + line139 + col6 + file0 + + ExecutedLines + + 0 + + 50 + 51 + 136 + 137 + 138 + 139 + + + + + path + + + kindcontrol + edges + + + start + + + line151 + col3 + file0 + + + line151 + col5 + file0 + + + end + + + line152 + col3 + file0 + + + line152 + col15 + file0 + + + + + + + kindmacro_expansion + location + + line152 + col3 + file0 + + nameCALL_FUNCTION + expansionsetToNullAndPrint ( & a , "Hello!" ) + + + kindevent + location + + line152 + col3 + file0 + + ranges + + + + line152 + col3 + file0 + + + line152 + col48 + file0 + + + + depth0 + extended_message + Calling 'setToNullAndPrint' + message + Calling 'setToNullAndPrint' + + + kindevent + location + + line145 + col1 + file0 + + depth1 + extended_message + Entered call from 'macroArgContainsCommaLParenRParenTest' + message + Entered call from 'macroArgContainsCommaLParenRParenTest' + + + kindcontrol + edges + + + start + + + line145 + col1 + file0 + + + line145 + col4 + file0 + + + end + + + line146 + col3 + file0 + + + line146 + col11 + file0 + + + + + + + kindevent + location + + line146 + col3 + file0 + + ranges + + + + line146 + col3 + file0 + + + line146 + col17 + file0 + + + + depth1 + extended_message + Calling 'setToNull' + message + Calling 'setToNull' + + + kindevent + location + + line50 + col1 + file0 + + depth2 + extended_message + Entered call from 'setToNullAndPrint' + message + Entered call from 'setToNullAndPrint' + + + 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 + + + + depth2 + extended_message + Null pointer value stored to 'a' + message + Null pointer value stored to 'a' + + + kindevent + location + + line146 + col3 + file0 + + ranges + + + + line146 + col3 + file0 + + + line146 + col17 + file0 + + + + depth1 + extended_message + Returning from 'setToNull' + message + Returning from 'setToNull' + + + kindcontrol + edges + + + start + + + line146 + col3 + file0 + + + line146 + col11 + file0 + + + end + + + line147 + col3 + file0 + + + line147 + col7 + file0 + + + + + + + kindevent + location + + line152 + col3 + file0 + + ranges + + + + line152 + col3 + file0 + + + line152 + col48 + file0 + + + + depth0 + extended_message + Returning from 'setToNullAndPrint' + message + Returning from 'setToNullAndPrint' + + + kindcontrol + edges + + + start + + + line153 + col3 + file0 + + + line153 + col3 + file0 + + + end + + + line153 + col6 + file0 + + + line153 + col6 + file0 + + + + + + + kindevent + location + + line153 + col6 + file0 + + ranges + + + + line153 + col4 + file0 + + + line153 + col4 + 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_contextc5805abeb71bb4edb41b49ab317439b9 + issue_context_kindfunction + issue_contextmacroArgContainsCommaLParenRParenTest + issue_hash_function_offset3 + location + + line153 + col6 + file0 + + ExecutedLines + + 0 + + 50 + 51 + 145 + 146 + 147 + 150 + 151 + 152 + 153 + + + + + path + + + kindcontrol + edges + + + start + + + line163 + col3 + file0 + + + line163 + col5 + file0 + + + end + + + line164 + col3 + file0 + + + line164 + col31 + file0 + + + + + + + kindmacro_expansion + location + + line164 + col3 + file0 + + nameCALL_FUNCTION_WITH_TWO_PARAMS + expansionsetToNullAndPrint ( & a , "Hello!" ) + + + kindevent + location + + line164 + col3 + file0 + + ranges + + + + line164 + col3 + file0 + + + line164 + col64 + file0 + + + + depth0 + extended_message + Calling 'setToNullAndPrint' + message + Calling 'setToNullAndPrint' + + + kindevent + location + + line145 + col1 + file0 + + depth1 + extended_message + Entered call from 'macroArgContainsCommaLParenRParenTest2' + message + Entered call from 'macroArgContainsCommaLParenRParenTest2' + + + kindcontrol + edges + + + start + + + line145 + col1 + file0 + + + line145 + col4 + file0 + + + end + + + line146 + col3 + file0 + + + line146 + col11 + file0 + + + + + + + kindevent + location + + line146 + col3 + file0 + + ranges + + + + line146 + col3 + file0 + + + line146 + col17 + file0 + + + + depth1 + extended_message + Calling 'setToNull' + message + Calling 'setToNull' + + + kindevent + location + + line50 + col1 + file0 + + depth2 + extended_message + Entered call from 'setToNullAndPrint' + message + Entered call from 'setToNullAndPrint' + + + 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 + + + + depth2 + extended_message + Null pointer value stored to 'a' + message + Null pointer value stored to 'a' + + + kindevent + location + + line146 + col3 + file0 + + ranges + + + + line146 + col3 + file0 + + + line146 + col17 + file0 + + + + depth1 + extended_message + Returning from 'setToNull' + message + Returning from 'setToNull' + + + kindcontrol + edges + + + start + + + line146 + col3 + file0 + + + line146 + col11 + file0 + + + end + + + line147 + col3 + file0 + + + line147 + col7 + file0 + + + + + + + kindevent + location + + line164 + col3 + file0 + + ranges + + + + line164 + col3 + file0 + + + line164 + col64 + file0 + + + + depth0 + extended_message + Returning from 'setToNullAndPrint' + message + Returning from 'setToNullAndPrint' + + + kindcontrol + edges + + + start + + + line165 + col3 + file0 + + + line165 + col3 + file0 + + + end + + + line165 + col6 + file0 + + + line165 + col6 + file0 + + + + + + + kindevent + location + + line165 + col6 + file0 + + ranges + + + + line165 + col4 + file0 + + + line165 + col4 + 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_context4014a22ef054933e6ce9be43623ea85e + issue_context_kindfunction + issue_contextmacroArgContainsCommaLParenRParenTest2 + issue_hash_function_offset3 + location + + line165 + col6 + file0 + + ExecutedLines + + 0 + + 50 + 51 + 145 + 146 + 147 + 162 + 163 + 164 + 165 + + + + + path + + + kindcontrol + edges + + + start + + + line175 + col3 + file0 + + + line175 + col5 + file0 + + + end + + + line179 + col3 + file0 + + + line179 + col13 + file0 + + + + + + + kindmacro_expansion + location + + line179 + col3 + file0 + + nameCALL_LAMBDA + expansion( [ & ptr , str ] ( ) mutable { TO_NULL ( & ptr ) ; } ) ( ) + + + kindevent + location + + line179 + col3 + file0 + + ranges + + + + line179 + col3 + file0 + + + line179 + col58 + file0 + + + + depth0 + extended_message + Calling 'operator()' + message + Calling 'operator()' + + + kindevent + location + + line179 + col3 + file0 + + depth1 + extended_message + Entered call from 'commaInBracketsTest' + message + Entered call from 'commaInBracketsTest' + + + kindmacro_expansion + location + + line179 + col3 + file0 + + nameCALL_LAMBDA + expansion( [ & ptr , str ] ( ) mutable { TO_NULL ( & ptr ) ; } ) ( ) + + + kindevent + location + + line179 + col3 + file0 + + ranges + + + + line179 + col3 + file0 + + + line179 + col58 + file0 + + + + depth1 + extended_message + Calling 'setToNull' + message + Calling 'setToNull' + + + kindevent + location + + line50 + col1 + file0 + + depth2 + extended_message + Entered call from 'operator()' + message + Entered call from 'operator()' + + + 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 + + + + depth2 + extended_message + Null pointer value stored to 'ptr' + message + Null pointer value stored to 'ptr' + + + kindevent + location + + line179 + col3 + file0 + + ranges + + + + line179 + col3 + file0 + + + line179 + col58 + file0 + + + + depth1 + extended_message + Returning from 'setToNull' + message + Returning from 'setToNull' + + + kindevent + location + + line179 + col3 + file0 + + ranges + + + + line179 + col3 + file0 + + + line179 + col58 + file0 + + + + depth0 + extended_message + Returning from 'operator()' + message + Returning from 'operator()' + + + kindcontrol + edges + + + start + + + line180 + col3 + file0 + + + line180 + col3 + file0 + + + end + + + line180 + col8 + file0 + + + line180 + col8 + file0 + + + + + + + kindevent + location + + line180 + col8 + file0 + + ranges + + + + line180 + col4 + file0 + + + line180 + 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_contexta8918c38ddfa6a991701e7d19c9cd6bb + issue_context_kindfunction + issue_contextcommaInBracketsTest + issue_hash_function_offset6 + location + + line180 + col8 + file0 + + ExecutedLines + + 0 + + 50 + 51 + 174 + 175 + 176 + 179 + 180 + + + + + path + + + kindmacro_expansion + location + + line190 + col3 + file0 + + namePASTE_CODE + expansion{ int * ptr = nullptr ; * ptr = 5 ; } + + + kindevent + location + + line190 + col3 + file0 + + ranges + + + + line190 + col3 + file0 + + + line198 + col4 + file0 + + + + depth0 + extended_message + 'ptr' initialized to a null pointer value + message + 'ptr' initialized to a null pointer value + + + kindevent + location + + line190 + col3 + file0 + + ranges + + + + line190 + col3 + file0 + + + line198 + col4 + 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_context63042e03ae0d2f3832b141a63b1d4d49 + issue_context_kindfunction + issue_contextcommaInBracesTest + issue_hash_function_offset1 + location + + line190 + col3 + file0 + + ExecutedLines + + 0 + + 189 + 190 + + + + + path + + + kindcontrol + edges + + + start + + + line212 + col3 + file0 + + + line212 + col5 + file0 + + + end + + + line214 + col3 + file0 + + + line214 + col25 + file0 + + + + + + + kindmacro_expansion + location + + line214 + col3 + file0 + + namePOTENTIALLY_EMPTY_PARAM + expansion; ptr = nullptr + + + kindevent + location + + line214 + col3 + file0 + + ranges + + + + line214 + col3 + file0 + + + line214 + col31 + file0 + + + + depth0 + extended_message + Null pointer value stored to 'ptr' + message + Null pointer value stored to 'ptr' + + + kindcontrol + edges + + + start + + + line215 + col3 + file0 + + + line215 + col3 + file0 + + + end + + + line215 + col8 + file0 + + + line215 + col8 + file0 + + + + + + + kindevent + location + + line215 + col8 + file0 + + ranges + + + + line215 + col4 + file0 + + + line215 + 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_contextcd980e278fbcd8f77bbeac79285084e2 + issue_context_kindfunction + issue_contextemptyParamTest + issue_hash_function_offset4 + location + + line215 + col8 + file0 + + ExecutedLines + + 0 + + 211 + 212 + 214 + 215 + + + + + path + + + kindcontrol + edges + + + start + + + line226 + col3 + file0 + + + line226 + col5 + file0 + + + end + + + line228 + col3 + file0 + + + line228 + col20 + file0 + + + + + + + kindmacro_expansion + location + + line228 + col3 + file0 + + nameNESTED_EMPTY_PARAM + expansion; ptr = nullptr ; + + + kindevent + location + + line228 + col3 + file0 + + ranges + + + + line228 + col3 + file0 + + + line228 + col27 + file0 + + + + depth0 + extended_message + Null pointer value stored to 'ptr' + message + Null pointer value stored to 'ptr' + + + kindcontrol + edges + + + start + + + line229 + col3 + file0 + + + line229 + col3 + file0 + + + end + + + line229 + col8 + file0 + + + line229 + col8 + file0 + + + + + + + kindevent + location + + line229 + col8 + file0 + + ranges + + + + line229 + col4 + file0 + + + line229 + 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_contextf6a5f6c93b6e3734842ddabd3d5a7341 + issue_context_kindfunction + issue_contextnestedEmptyParamTest + issue_hash_function_offset4 + location + + line229 + col8 + file0 + + ExecutedLines + + 0 + + 225 + 226 + 228 + 229 + + + + + path + + + kindcontrol + edges + + + start + + + line239 + col3 + file0 + + + line239 + col5 + file0 + + + end + + + line240 + col3 + file0 + + + line240 + col44 + file0 + + + + + + + kindmacro_expansion + location + + line240 + col3 + file0 + + nameCALL_FUNCTION_WITH_ONE_PARAM_THROUGH_MACRO + expansionsetToNull ( & ptr ) + + + kindevent + location + + line240 + col3 + file0 + + ranges + + + + line240 + col3 + file0 + + + line240 + col61 + file0 + + + + depth0 + extended_message + Calling 'setToNull' + message + Calling 'setToNull' + + + kindevent + location + + line50 + col1 + file0 + + depth1 + extended_message + Entered call from 'lParenRParenInNestedMacro' + message + Entered call from 'lParenRParenInNestedMacro' + + + 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 + + line240 + col3 + file0 + + ranges + + + + line240 + col3 + file0 + + + line240 + col61 + file0 + + + + depth0 + extended_message + Returning from 'setToNull' + message + Returning from 'setToNull' + + + kindcontrol + edges + + + start + + + line241 + col3 + file0 + + + line241 + col3 + file0 + + + end + + + line241 + col8 + file0 + + + line241 + col8 + file0 + + + + + + + kindevent + location + + line241 + col8 + file0 + + ranges + + + + line241 + col4 + file0 + + + line241 + 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_contextff00c8344e685317303e814970082d5f + issue_context_kindfunction + issue_contextlParenRParenInNestedMacro + issue_hash_function_offset3 + location + + line241 + col8 + file0 + + ExecutedLines + + 0 + + 50 + 51 + 238 + 239 + 240 + 241 + + + + + path + + + kindcontrol + edges + + + start + + + line259 + col3 + file0 + + + line259 + col5 + file0 + + + end + + + line260 + col3 + file0 + + + line260 + col22 + file0 + + + + + + + kindmacro_expansion + location + + line260 + col3 + file0 + + nameVARIADIC_SET_TO_NULL + expansionptr = nullptr ; variadicFunc ( 1 ) + + + kindevent + location + + line260 + col3 + file0 + + ranges + + + + line260 + col3 + file0 + + + line260 + col42 + file0 + + + + depth0 + extended_message + Null pointer value stored to 'ptr' + message + Null pointer value stored to 'ptr' + + + kindcontrol + edges + + + start + + + line261 + col3 + file0 + + + line261 + col3 + file0 + + + end + + + line261 + col8 + file0 + + + line261 + col8 + file0 + + + + + + + kindevent + location + + line261 + col8 + file0 + + ranges + + + + line261 + col4 + file0 + + + line261 + 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_context1b0880549df23e9ce0edb60955ad5ac1 + issue_context_kindfunction + issue_contextvariadicMacroArgumentTest + issue_hash_function_offset3 + location + + line261 + col8 + file0 + + ExecutedLines + + 0 + + 258 + 259 + 260 + 261 + + + + + path + + + kindcontrol + edges + + + start + + + line277 + col3 + file0 + + + line277 + col5 + file0 + + + end + + + line278 + col3 + file0 + + + line278 + col30 + file0 + + + + + + + kindmacro_expansion + location + + line278 + col3 + file0 + + nameDECLARE_FUNC_AND_SET_TO_NULL + expansionvoid generated_ ## whatever ( ) ; ptr = nullptr ; + + + kindevent + location + + line278 + col3 + file0 + + ranges + + + + line278 + col3 + file0 + + + line278 + col45 + file0 + + + + depth0 + extended_message + Null pointer value stored to 'ptr' + message + Null pointer value stored to 'ptr' + + + kindcontrol + edges + + + start + + + line279 + col3 + file0 + + + line279 + col3 + file0 + + + end + + + line279 + col8 + file0 + + + line279 + col8 + file0 + + + + + + + kindevent + location + + line279 + col8 + file0 + + ranges + + + + line279 + col4 + file0 + + + line279 + 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_context453ed8096f5394e74e16f965886e5623 + issue_context_kindfunction + issue_contexthashHashOperatorTest + issue_hash_function_offset3 + location + + line279 + col8 + file0 + + ExecutedLines + + 0 + + 276 + 277 + 278 + 279 + + + + + path + + + kindcontrol + edges + + + start + + + line291 + col3 + file0 + + + line291 + col5 + file0 + + + end + + + line292 + col3 + file0 + + + line292 + col11 + file0 + + + + + + + kindmacro_expansion + location + + line292 + col3 + file0 + + namePRINT_STR + expansionprint ( # Hello ) ; ptr = nullptr + + + kindevent + location + + line292 + col3 + file0 + + ranges + + + + line292 + col3 + file0 + + + line292 + col23 + file0 + + + + depth0 + extended_message + Null pointer value stored to 'ptr' + message + Null pointer value stored to 'ptr' + + + kindcontrol + edges + + + start + + + line293 + col3 + file0 + + + line293 + col3 + file0 + + + end + + + line293 + col8 + file0 + + + line293 + col8 + file0 + + + + + + + kindevent + location + + line293 + col8 + file0 + + ranges + + + + line293 + col4 + file0 + + + line293 + 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_contexte6947ee72df70243a3b4c9e9eaed0888 + issue_context_kindfunction + issue_contexthashOperatorTest + issue_hash_function_offset3 + location + + line293 + col8 + file0 + + ExecutedLines + + 0 + + 290 + 291 + 292 + 293 + + + files Index: test/Analysis/plist-macros-with-expansion.cpp =================================================================== --- test/Analysis/plist-macros-with-expansion.cpp +++ test/Analysis/plist-macros-with-expansion.cpp @@ -61,7 +61,7 @@ } // CHECK: nameTO_NULL -// CHECK: expansionsetToNull ( x ) +// CHECK: expansionsetToNull ( & ptr ) #define DOES_NOTHING(x) \ { \ @@ -81,7 +81,218 @@ } // CHECK: nameTO_NULL -// CHECK: expansionsetToNull ( x ) +// CHECK: expansionsetToNull ( & a ) // CHECK: nameDEREF -// CHECK: expansion{ int b ; b = 5 ; } print ( x ) ; * x +// CHECK: expansion{ int b ; b = 5 ; } print ( a ) ; * a + +//===----------------------------------------------------------------------===// +// Tests for macro arguments containing commas and parantheses. +// +// As of writing these tests, the algorithm expands macro arguments by lexing +// the macro's expansion location, and relies on finding tok::comma and +// tok::l_paren/tok::r_paren. +//===----------------------------------------------------------------------===// + +// Note that this commas, parantheses in strings aren't parsed as tok::comma or +// tok::l_paren/tok::r_paren, but why not test them. + +#define TO_NULL_AND_PRINT(x, str) \ + x = 0; \ + print(str) + +void macroArgContainsCommaInStringTest() { + int *a; + TO_NULL_AND_PRINT(a, "Will this , cause a crash?"); + *a = 5; // expected-warning{{Dereference of null pointer}} +} + +// CHECK: nameTO_NULL_AND_PRINT +// CHECK: expansiona = 0 ; print ( "Will this , cause a crash?" ) + +void macroArgContainsLParenInStringTest() { + int *a; + TO_NULL_AND_PRINT(a, "Will this ( cause a crash?"); + *a = 5; // expected-warning{{Dereference of null pointer}} +} + +// CHECK: nameTO_NULL_AND_PRINT +// CHECK: expansiona = 0 ; print ( "Will this ( cause a crash?" ) + +void macroArgContainsRParenInStringTest() { + int *a; + TO_NULL_AND_PRINT(a, "Will this ) cause a crash?"); + *a = 5; // expected-warning{{Dereference of null pointer}} +} + +// CHECK: nameTO_NULL_AND_PRINT +// CHECK: expansiona = 0 ; print ( "Will this ) cause a crash?" ) + +#define CALL_FUNCTION(funcCall) \ + funcCall + +// Function calls do contain both tok::comma and tok::l_paren/tok::r_paren. + +void macroArgContainsLParenRParenTest() { + int *a; + CALL_FUNCTION(setToNull(&a)); + *a = 5; // expected-warning{{Dereference of null pointer}} +} + +// CHECK: nameCALL_FUNCTION +// CHECK: expansionsetToNull ( & a ) + +void setToNullAndPrint(int **vptr, const char *str) { + setToNull(vptr); + print(str); +} + +void macroArgContainsCommaLParenRParenTest() { + int *a; + CALL_FUNCTION(setToNullAndPrint(&a, "Hello!")); + *a = 5; // expected-warning{{Dereference of null pointer}} +} + +// CHECK: nameCALL_FUNCTION +// CHECK: expansionsetToNullAndPrint ( & a , "Hello!" ) + +#define CALL_FUNCTION_WITH_TWO_PARAMS(funcCall, param1, param2) \ + funcCall(param1, param2) + +void macroArgContainsCommaLParenRParenTest2() { + int *a; + CALL_FUNCTION_WITH_TWO_PARAMS(setToNullAndPrint, &a, "Hello!"); + *a = 5; // expected-warning{{Dereference of null pointer}} +} + +// CHECK: nameCALL_FUNCTION_WITH_TWO_PARAMS +// CHECK: expansionsetToNullAndPrint ( & a , "Hello!" ) + +#define CALL_LAMBDA(l) \ + l() + +void commaInBracketsTest() { + int *ptr; + const char str[] = "Hello!"; + // You need to add parantheses around a lambda expression to compile this, + // else the comma in the capture will be parsed as divider of macro args. + CALL_LAMBDA(([&ptr, str] () mutable { TO_NULL(&ptr); })); + *ptr = 5; // expected-warning{{Dereference of null pointer}} +} + +// CHECK: nameCALL_LAMBDA +// CHECK: expansion( [ & ptr , str ] ( ) mutable { TO_NULL ( & ptr ) ; } ) ( ) + +#define PASTE_CODE(code) \ + code + +void commaInBracesTest() { + PASTE_CODE({ // expected-warning{{Dereference of null pointer}} + // NOTE: If we were to add a new variable here after a comma, we'd get a + // compilation error, so this test is mainly here to show that this was also + // investigated. + + // int *ptr = nullptr, a; + int *ptr = nullptr; + *ptr = 5; + }) +} + +// CHECK: namePASTE_CODE +// CHECK: expansion{ int * ptr = nullptr ; * ptr = 5 ; } + +// Example taken from +// https://gcc.gnu.org/onlinedocs/cpp/Macro-Arguments.html#Macro-Arguments. + +#define POTENTIALLY_EMPTY_PARAM(x, y) \ + x; \ + y = nullptr + +void emptyParamTest() { + int *ptr; + + POTENTIALLY_EMPTY_PARAM(,ptr); + *ptr = 5; // expected-warning{{Dereference of null pointer}} +} + +// CHECK: namePOTENTIALLY_EMPTY_PARAM +// CHECK: expansion; ptr = nullptr + +#define NESTED_EMPTY_PARAM(a, b) \ + POTENTIALLY_EMPTY_PARAM(a, b); + + +void nestedEmptyParamTest() { + int *ptr; + + NESTED_EMPTY_PARAM(, ptr); + *ptr = 5; // expected-warning{{Dereference of null pointer}} +} + +// CHECK: nameNESTED_EMPTY_PARAM +// CHECK: expansion; ptr = nullptr ; + +#define CALL_FUNCTION_WITH_ONE_PARAM_THROUGH_MACRO(func, param) \ + CALL_FUNCTION(func(param)) + +void lParenRParenInNestedMacro() { + int *ptr; + CALL_FUNCTION_WITH_ONE_PARAM_THROUGH_MACRO(setToNull, &ptr); + *ptr = 5; // expected-warning{{Dereference of null pointer}} +} + +// CHECK: nameCALL_FUNCTION_WITH_ONE_PARAM_THROUGH_MACRO +// CHECK: expansionsetToNull ( & ptr ) + +//===----------------------------------------------------------------------===// +// Tests for variadic macro arguments. +//===----------------------------------------------------------------------===// + +template +void variadicFunc(Args ...args); + +#define VARIADIC_SET_TO_NULL(ptr, ...) \ + ptr = nullptr; \ + variadicFunc(__VA_ARGS__) + +void variadicMacroArgumentTest() { + int *ptr; + VARIADIC_SET_TO_NULL(ptr, 1, 5, "haha!"); + *ptr = 5; // expected-warning{{Dereference of null pointer}} +} + +// TODO: Should correctly display the rest of the parameters. +// CHECK: nameVARIADIC_SET_TO_NULL +// CHECK: expansionptr = nullptr ; variadicFunc ( 1 ) + +//===----------------------------------------------------------------------===// +// Tests for # and ##. +//===----------------------------------------------------------------------===// + +#define DECLARE_FUNC_AND_SET_TO_NULL(funcName, ptr) \ + void generated_##funcName(); \ + ptr = nullptr; + +void hashHashOperatorTest() { + int *ptr; + DECLARE_FUNC_AND_SET_TO_NULL(whatever, ptr); + *ptr = 5; // expected-warning{{Dereference of null pointer}} +} + +// TODO: Should expand correctly. +// CHECK: nameDECLARE_FUNC_AND_SET_TO_NULL +// CHECK: expansionvoid generated_ ## whatever ( ) ; ptr = nullptr ; + +#define PRINT_STR(str, ptr) \ + print(#str); \ + ptr = nullptr + +void hashOperatorTest() { + int *ptr; + PRINT_STR(Hello, ptr); + *ptr = 5; // expected-warning{{Dereference of null pointer}} +} + +// TODO: Should expand correctly. +// CHECK: namePRINT_STR +// CHECK: expansionprint ( # Hello ) ; ptr = nullptr