Index: include/clang/StaticAnalyzer/Core/AnalyzerOptions.h =================================================================== --- include/clang/StaticAnalyzer/Core/AnalyzerOptions.h +++ include/clang/StaticAnalyzer/Core/AnalyzerOptions.h @@ -311,6 +311,9 @@ /// \sa shouldDisplayMacroExpansions Optional DisplayMacroExpansions; + /// \sa shouldDisplayMacroExpansionsAsEvents + Optional DisplayMacroExpansionsAsEvents; + /// \sa shouldAggressivelySimplifyBinaryOperation Optional AggressiveBinaryOperationSimplification; @@ -693,6 +696,13 @@ /// when unset. bool shouldDisplayMacroExpansions(); + /// Returns with true if macros expansions generated by the 'expand-macros' + /// option should be converted to events. + /// + /// This is controlled by the 'expand-macros-as-events' option, which defaults + /// to false when unset. + bool shouldDisplayMacroExpansionsAsEvents(); + /// Returns true if SValBuilder should rearrange comparisons and additive /// operations of symbolic expressions which consist of a sum of a symbol and /// a concrete integer into the format where symbols are on the left-hand Index: lib/StaticAnalyzer/Core/AnalyzerOptions.cpp =================================================================== --- lib/StaticAnalyzer/Core/AnalyzerOptions.cpp +++ lib/StaticAnalyzer/Core/AnalyzerOptions.cpp @@ -470,6 +470,13 @@ return DisplayMacroExpansions.getValue(); } +bool AnalyzerOptions::shouldDisplayMacroExpansionsAsEvents() { + if (!DisplayMacroExpansionsAsEvents.hasValue()) + DisplayMacroExpansionsAsEvents = + getBooleanOption("expand-macros-as-events", /*Default=*/false); + return DisplayMacroExpansionsAsEvents.getValue(); +} + bool AnalyzerOptions::shouldAggressivelySimplifyBinaryOperation() { if (!AggressiveBinaryOperationSimplification.hasValue()) AggressiveBinaryOperationSimplification = Index: lib/StaticAnalyzer/Core/PlistDiagnostics.cpp =================================================================== --- lib/StaticAnalyzer/Core/PlistDiagnostics.cpp +++ lib/StaticAnalyzer/Core/PlistDiagnostics.cpp @@ -284,33 +284,40 @@ const SourceManager &SM = PP.getSourceManager(); ExpansionInfo EI = getExpandedMacro(P.getLocation().asLocation(), PP); - Indent(o, indent) << "\n"; - ++indent; + if (AnOpts.shouldDisplayMacroExpansionsAsEvents()) { + ReportEvent(o, PathDiagnosticEventPiece(P.getLocation(), + llvm::Twine("Expanding macro '" + EI.MacroName + "' to '" + + EI.Expansion + "'").str()), + FM, PP, indent, depth); + } else { + Indent(o, indent) << "\n"; + ++indent; - Indent(o, indent) << "kindmacro_expansion\n"; + Indent(o, indent) << "kindmacro_expansion\n"; - // Output the location. - FullSourceLoc L = P.getLocation().asLocation(); + // Output the location. + FullSourceLoc L = P.getLocation().asLocation(); - Indent(o, indent) << "location\n"; - EmitLocation(o, SM, L, FM, indent); + Indent(o, indent) << "location\n"; + EmitLocation(o, SM, L, FM, indent); - // Output the ranges (if any). - ArrayRef Ranges = P.getRanges(); - EmitRanges(o, Ranges, FM, PP, indent); + // Output the ranges (if any). + ArrayRef Ranges = P.getRanges(); + EmitRanges(o, Ranges, FM, PP, indent); - // Output the macro name. - Indent(o, indent) << "name"; - EmitString(o, EI.MacroName) << '\n'; + // Output the macro name. + Indent(o, indent) << "name"; + EmitString(o, EI.MacroName) << '\n'; - // Output what it expands into. - Indent(o, indent) << "expansion"; - EmitString(o, EI.Expansion) << '\n'; + // Output what it expands into. + Indent(o, indent) << "expansion"; + EmitString(o, EI.Expansion) << '\n'; - // Finish up. - --indent; - Indent(o, indent); - o << "\n"; + // Finish up. + --indent; + Indent(o, indent); + o << "\n"; + } for (PathPieces::const_iterator I = P.subPieces.begin(), E = P.subPieces.end(); Index: test/Analysis/Inputs/expected-plists/plist-macros-with-expansion.cpp-as-events.plist =================================================================== --- /dev/null +++ test/Analysis/Inputs/expected-plists/plist-macros-with-expansion.cpp-as-events.plist @@ -0,0 +1,355 @@ + + + + + clang_version +clang version 8.0.0 (http://mainstream.inf.elte.hu/Szelethus/clang ec097c9a35733c2c703e8ff03ea9b6ae345bf3f3) (https://github.com/llvm-mirror/llvm 1ffbf26a1a0a190d69327af875a3337b74a2ce82) + diagnostics + + + path + + + kindcontrol + edges + + + start + + + line34 + col3 + file0 + + + line34 + col5 + file0 + + + end + + + line35 + col3 + file0 + + + line35 + col21 + file0 + + + + + + + kindevent + location + + line35 + col3 + file0 + + depth0 + extended_message + Expanding macro '' to '' + message + Expanding macro '' to '' + + + kindevent + location + + line35 + col3 + file0 + + ranges + + + + line35 + col3 + file0 + + + line35 + col21 + file0 + + + + depth0 + extended_message + Null pointer value stored to 'ptr' + message + Null pointer value stored to 'ptr' + + + kindcontrol + edges + + + start + + + line36 + col3 + file0 + + + line36 + col3 + file0 + + + end + + + line36 + col8 + file0 + + + line36 + col8 + file0 + + + + + + + kindevent + location + + line36 + col8 + file0 + + ranges + + + + line36 + col4 + file0 + + + line36 + 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_contextf8fbc46cc5afbb056d92bd3d3d702781 + issue_context_kindfunction + issue_contextnonFunctionLikeMacroTest + issue_hash_function_offset3 + location + + line36 + col8 + file0 + + ExecutedLines + + 0 + + 33 + 34 + 35 + 36 + + + + + path + + + kindcontrol + edges + + + start + + + line47 + col3 + file0 + + + line47 + col5 + file0 + + + end + + + line48 + col3 + file0 + + + line48 + col39 + file0 + + + + + + + kindevent + location + + line48 + col3 + file0 + + depth0 + extended_message + Expanding macro '' to '' + message + Expanding macro '' to '' + + + kindevent + location + + line48 + col3 + file0 + + ranges + + + + line48 + col3 + file0 + + + line48 + col39 + file0 + + + + depth0 + extended_message + Null pointer value stored to 'ptr' + message + Null pointer value stored to 'ptr' + + + kindcontrol + edges + + + start + + + line49 + col3 + file0 + + + line49 + col3 + file0 + + + end + + + line49 + col8 + file0 + + + line49 + col8 + file0 + + + + + + + kindevent + location + + line49 + col8 + file0 + + ranges + + + + line49 + col4 + file0 + + + line49 + 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_contextd5eba61193b41c27fc7b2705cbd607ba + issue_context_kindfunction + issue_contextnonFunctionLikeNestedMacroTest + issue_hash_function_offset3 + location + + line49 + col8 + file0 + + ExecutedLines + + 0 + + 46 + 47 + 48 + 49 + + + + + files + + /home/eumakri/Documents/2codechecker_dev_env/llvm/tools/clang/test/Analysis/plist-macros-with-expansion.cpp + + + \ No newline at end of file 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 80e1678b9f598ca78bb3b71cf546a63414a37b11) (https://github.com/llvm-mirror/llvm 1ffbf26a1a0a190d69327af875a3337b74a2ce82) +clang version 8.0.0 (http://mainstream.inf.elte.hu/Szelethus/clang ec097c9a35733c2c703e8ff03ea9b6ae345bf3f3) (https://github.com/llvm-mirror/llvm 1ffbf26a1a0a190d69327af875a3337b74a2ce82) diagnostics @@ -17,12 +17,12 @@ start - line25 + line34 col3 file0 - line25 + line34 col5 file0 @@ -30,12 +30,12 @@ end - line26 + line35 col3 file0 - line26 + line35 col21 file0 @@ -47,7 +47,7 @@ kindmacro_expansion location - line26 + line35 col3 file0 @@ -58,7 +58,7 @@ kindevent location - line26 + line35 col3 file0 @@ -66,12 +66,12 @@ - line26 + line35 col3 file0 - line26 + line35 col21 file0 @@ -91,12 +91,12 @@ start - line27 + line36 col3 file0 - line27 + line36 col3 file0 @@ -104,12 +104,12 @@ end - line27 + line36 col8 file0 - line27 + line36 col8 file0 @@ -121,7 +121,7 @@ kindevent location - line27 + line36 col8 file0 @@ -129,12 +129,12 @@ - line27 + line36 col4 file0 - line27 + line36 col6 file0 @@ -158,7 +158,7 @@ issue_hash_function_offset3 location - line27 + line36 col8 file0 @@ -166,10 +166,10 @@ 0 - 24 - 25 - 26 - 27 + 33 + 34 + 35 + 36 @@ -184,12 +184,12 @@ start - line38 + line47 col3 file0 - line38 + line47 col5 file0 @@ -197,12 +197,12 @@ end - line39 + line48 col3 file0 - line39 + line48 col39 file0 @@ -214,7 +214,7 @@ kindmacro_expansion location - line39 + line48 col3 file0 @@ -225,7 +225,7 @@ kindevent location - line39 + line48 col3 file0 @@ -233,12 +233,12 @@ - line39 + line48 col3 file0 - line39 + line48 col39 file0 @@ -258,12 +258,12 @@ start - line40 + line49 col3 file0 - line40 + line49 col3 file0 @@ -271,12 +271,12 @@ end - line40 + line49 col8 file0 - line40 + line49 col8 file0 @@ -288,7 +288,7 @@ kindevent location - line40 + line49 col8 file0 @@ -296,12 +296,12 @@ - line40 + line49 col4 file0 - line40 + line49 col6 file0 @@ -325,7 +325,7 @@ issue_hash_function_offset3 location - line40 + line49 col8 file0 @@ -333,10 +333,10 @@ 0 - 37 - 38 - 39 - 40 + 46 + 47 + 48 + 49 Index: test/Analysis/plist-macros-with-expansion.cpp =================================================================== --- test/Analysis/plist-macros-with-expansion.cpp +++ test/Analysis/plist-macros-with-expansion.cpp @@ -11,6 +11,15 @@ // Check the macro expansions from the plist output here, to make the test more // understandable. // RUN: FileCheck --input-file=%t.plist %s +// +// RUN: %clang_analyze_cc1 -analyzer-checker=core %s \ +// RUN: -analyzer-output=plist -o %t-as-events.plist \ +// RUN: -analyzer-config expand-macros=true \ +// RUN: -analyzer-config expand-macros-as-events=true +// +// We're only interested in the generated plist file in this case. +// RUN: cat %t-as-events.plist | %diff_plist \ +// RUN: %S/Inputs/expected-plists/plist-macros-with-expansion.cpp-as-events.plist void print(const void*);