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<bool> DisplayMacroExpansions; + /// \sa shouldDisplayMacroExpansionsAsEvents + Optional<bool> DisplayMacroExpansionsAsEvents; + /// \sa shouldAggressivelySimplifyBinaryOperation Optional<bool> 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) << "<dict>\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) << "<dict>\n"; + ++indent; - Indent(o, indent) << "<key>kind</key><string>macro_expansion</string>\n"; + Indent(o, indent) << "<key>kind</key><string>macro_expansion</string>\n"; - // Output the location. - FullSourceLoc L = P.getLocation().asLocation(); + // Output the location. + FullSourceLoc L = P.getLocation().asLocation(); - Indent(o, indent) << "<key>location</key>\n"; - EmitLocation(o, SM, L, FM, indent); + Indent(o, indent) << "<key>location</key>\n"; + EmitLocation(o, SM, L, FM, indent); - // Output the ranges (if any). - ArrayRef<SourceRange> Ranges = P.getRanges(); - EmitRanges(o, Ranges, FM, PP, indent); + // Output the ranges (if any). + ArrayRef<SourceRange> Ranges = P.getRanges(); + EmitRanges(o, Ranges, FM, PP, indent); - // Output the macro name. - Indent(o, indent) << "<key>name</key>"; - EmitString(o, EI.MacroName) << '\n'; + // Output the macro name. + Indent(o, indent) << "<key>name</key>"; + EmitString(o, EI.MacroName) << '\n'; - // Output what it expands into. - Indent(o, indent) << "<key>expansion</key>"; - EmitString(o, EI.Expansion) << '\n'; + // Output what it expands into. + Indent(o, indent) << "<key>expansion</key>"; + EmitString(o, EI.Expansion) << '\n'; - // Finish up. - --indent; - Indent(o, indent); - o << "</dict>\n"; + // Finish up. + --indent; + Indent(o, indent); + o << "</dict>\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 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> +<plist version="1.0"> +<dict> + <key>clang_version</key> +<string>clang version 8.0.0 (http://mainstream.inf.elte.hu/Szelethus/clang ec097c9a35733c2c703e8ff03ea9b6ae345bf3f3) (https://github.com/llvm-mirror/llvm 1ffbf26a1a0a190d69327af875a3337b74a2ce82)</string> + <key>diagnostics</key> + <array> + <dict> + <key>path</key> + <array> + <dict> + <key>kind</key><string>control</string> + <key>edges</key> + <array> + <dict> + <key>start</key> + <array> + <dict> + <key>line</key><integer>34</integer> + <key>col</key><integer>3</integer> + <key>file</key><integer>0</integer> + </dict> + <dict> + <key>line</key><integer>34</integer> + <key>col</key><integer>5</integer> + <key>file</key><integer>0</integer> + </dict> + </array> + <key>end</key> + <array> + <dict> + <key>line</key><integer>35</integer> + <key>col</key><integer>3</integer> + <key>file</key><integer>0</integer> + </dict> + <dict> + <key>line</key><integer>35</integer> + <key>col</key><integer>21</integer> + <key>file</key><integer>0</integer> + </dict> + </array> + </dict> + </array> + </dict> + <dict> + <key>kind</key><string>event</string> + <key>location</key> + <dict> + <key>line</key><integer>35</integer> + <key>col</key><integer>3</integer> + <key>file</key><integer>0</integer> + </dict> + <key>depth</key><integer>0</integer> + <key>extended_message</key> + <string>Expanding macro '' to ''</string> + <key>message</key> + <string>Expanding macro '' to ''</string> + </dict> + <dict> + <key>kind</key><string>event</string> + <key>location</key> + <dict> + <key>line</key><integer>35</integer> + <key>col</key><integer>3</integer> + <key>file</key><integer>0</integer> + </dict> + <key>ranges</key> + <array> + <array> + <dict> + <key>line</key><integer>35</integer> + <key>col</key><integer>3</integer> + <key>file</key><integer>0</integer> + </dict> + <dict> + <key>line</key><integer>35</integer> + <key>col</key><integer>21</integer> + <key>file</key><integer>0</integer> + </dict> + </array> + </array> + <key>depth</key><integer>0</integer> + <key>extended_message</key> + <string>Null pointer value stored to 'ptr'</string> + <key>message</key> + <string>Null pointer value stored to 'ptr'</string> + </dict> + <dict> + <key>kind</key><string>control</string> + <key>edges</key> + <array> + <dict> + <key>start</key> + <array> + <dict> + <key>line</key><integer>36</integer> + <key>col</key><integer>3</integer> + <key>file</key><integer>0</integer> + </dict> + <dict> + <key>line</key><integer>36</integer> + <key>col</key><integer>3</integer> + <key>file</key><integer>0</integer> + </dict> + </array> + <key>end</key> + <array> + <dict> + <key>line</key><integer>36</integer> + <key>col</key><integer>8</integer> + <key>file</key><integer>0</integer> + </dict> + <dict> + <key>line</key><integer>36</integer> + <key>col</key><integer>8</integer> + <key>file</key><integer>0</integer> + </dict> + </array> + </dict> + </array> + </dict> + <dict> + <key>kind</key><string>event</string> + <key>location</key> + <dict> + <key>line</key><integer>36</integer> + <key>col</key><integer>8</integer> + <key>file</key><integer>0</integer> + </dict> + <key>ranges</key> + <array> + <array> + <dict> + <key>line</key><integer>36</integer> + <key>col</key><integer>4</integer> + <key>file</key><integer>0</integer> + </dict> + <dict> + <key>line</key><integer>36</integer> + <key>col</key><integer>6</integer> + <key>file</key><integer>0</integer> + </dict> + </array> + </array> + <key>depth</key><integer>0</integer> + <key>extended_message</key> + <string>Dereference of null pointer (loaded from variable 'ptr')</string> + <key>message</key> + <string>Dereference of null pointer (loaded from variable 'ptr')</string> + </dict> + </array> + <key>description</key><string>Dereference of null pointer (loaded from variable 'ptr')</string> + <key>category</key><string>Logic error</string> + <key>type</key><string>Dereference of null pointer</string> + <key>check_name</key><string>core.NullDereference</string> + <!-- This hash is experimental and going to change! --> + <key>issue_hash_content_of_line_in_context</key><string>f8fbc46cc5afbb056d92bd3d3d702781</string> + <key>issue_context_kind</key><string>function</string> + <key>issue_context</key><string>nonFunctionLikeMacroTest</string> + <key>issue_hash_function_offset</key><string>3</string> + <key>location</key> + <dict> + <key>line</key><integer>36</integer> + <key>col</key><integer>8</integer> + <key>file</key><integer>0</integer> + </dict> + <key>ExecutedLines</key> + <dict> + <key>0</key> + <array> + <integer>33</integer> + <integer>34</integer> + <integer>35</integer> + <integer>36</integer> + </array> + </dict> + </dict> + <dict> + <key>path</key> + <array> + <dict> + <key>kind</key><string>control</string> + <key>edges</key> + <array> + <dict> + <key>start</key> + <array> + <dict> + <key>line</key><integer>47</integer> + <key>col</key><integer>3</integer> + <key>file</key><integer>0</integer> + </dict> + <dict> + <key>line</key><integer>47</integer> + <key>col</key><integer>5</integer> + <key>file</key><integer>0</integer> + </dict> + </array> + <key>end</key> + <array> + <dict> + <key>line</key><integer>48</integer> + <key>col</key><integer>3</integer> + <key>file</key><integer>0</integer> + </dict> + <dict> + <key>line</key><integer>48</integer> + <key>col</key><integer>39</integer> + <key>file</key><integer>0</integer> + </dict> + </array> + </dict> + </array> + </dict> + <dict> + <key>kind</key><string>event</string> + <key>location</key> + <dict> + <key>line</key><integer>48</integer> + <key>col</key><integer>3</integer> + <key>file</key><integer>0</integer> + </dict> + <key>depth</key><integer>0</integer> + <key>extended_message</key> + <string>Expanding macro '' to ''</string> + <key>message</key> + <string>Expanding macro '' to ''</string> + </dict> + <dict> + <key>kind</key><string>event</string> + <key>location</key> + <dict> + <key>line</key><integer>48</integer> + <key>col</key><integer>3</integer> + <key>file</key><integer>0</integer> + </dict> + <key>ranges</key> + <array> + <array> + <dict> + <key>line</key><integer>48</integer> + <key>col</key><integer>3</integer> + <key>file</key><integer>0</integer> + </dict> + <dict> + <key>line</key><integer>48</integer> + <key>col</key><integer>39</integer> + <key>file</key><integer>0</integer> + </dict> + </array> + </array> + <key>depth</key><integer>0</integer> + <key>extended_message</key> + <string>Null pointer value stored to 'ptr'</string> + <key>message</key> + <string>Null pointer value stored to 'ptr'</string> + </dict> + <dict> + <key>kind</key><string>control</string> + <key>edges</key> + <array> + <dict> + <key>start</key> + <array> + <dict> + <key>line</key><integer>49</integer> + <key>col</key><integer>3</integer> + <key>file</key><integer>0</integer> + </dict> + <dict> + <key>line</key><integer>49</integer> + <key>col</key><integer>3</integer> + <key>file</key><integer>0</integer> + </dict> + </array> + <key>end</key> + <array> + <dict> + <key>line</key><integer>49</integer> + <key>col</key><integer>8</integer> + <key>file</key><integer>0</integer> + </dict> + <dict> + <key>line</key><integer>49</integer> + <key>col</key><integer>8</integer> + <key>file</key><integer>0</integer> + </dict> + </array> + </dict> + </array> + </dict> + <dict> + <key>kind</key><string>event</string> + <key>location</key> + <dict> + <key>line</key><integer>49</integer> + <key>col</key><integer>8</integer> + <key>file</key><integer>0</integer> + </dict> + <key>ranges</key> + <array> + <array> + <dict> + <key>line</key><integer>49</integer> + <key>col</key><integer>4</integer> + <key>file</key><integer>0</integer> + </dict> + <dict> + <key>line</key><integer>49</integer> + <key>col</key><integer>6</integer> + <key>file</key><integer>0</integer> + </dict> + </array> + </array> + <key>depth</key><integer>0</integer> + <key>extended_message</key> + <string>Dereference of null pointer (loaded from variable 'ptr')</string> + <key>message</key> + <string>Dereference of null pointer (loaded from variable 'ptr')</string> + </dict> + </array> + <key>description</key><string>Dereference of null pointer (loaded from variable 'ptr')</string> + <key>category</key><string>Logic error</string> + <key>type</key><string>Dereference of null pointer</string> + <key>check_name</key><string>core.NullDereference</string> + <!-- This hash is experimental and going to change! --> + <key>issue_hash_content_of_line_in_context</key><string>d5eba61193b41c27fc7b2705cbd607ba</string> + <key>issue_context_kind</key><string>function</string> + <key>issue_context</key><string>nonFunctionLikeNestedMacroTest</string> + <key>issue_hash_function_offset</key><string>3</string> + <key>location</key> + <dict> + <key>line</key><integer>49</integer> + <key>col</key><integer>8</integer> + <key>file</key><integer>0</integer> + </dict> + <key>ExecutedLines</key> + <dict> + <key>0</key> + <array> + <integer>46</integer> + <integer>47</integer> + <integer>48</integer> + <integer>49</integer> + </array> + </dict> + </dict> + </array> + <key>files</key> + <array> + <string>/home/eumakri/Documents/2codechecker_dev_env/llvm/tools/clang/test/Analysis/plist-macros-with-expansion.cpp</string> + </array> +</dict> +</plist> \ 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 @@ <plist version="1.0"> <dict> <key>clang_version</key> -<string>clang version 8.0.0 (http://mainstream.inf.elte.hu/Szelethus/clang 80e1678b9f598ca78bb3b71cf546a63414a37b11) (https://github.com/llvm-mirror/llvm 1ffbf26a1a0a190d69327af875a3337b74a2ce82)</string> +<string>clang version 8.0.0 (http://mainstream.inf.elte.hu/Szelethus/clang ec097c9a35733c2c703e8ff03ea9b6ae345bf3f3) (https://github.com/llvm-mirror/llvm 1ffbf26a1a0a190d69327af875a3337b74a2ce82)</string> <key>diagnostics</key> <array> <dict> @@ -17,12 +17,12 @@ <key>start</key> <array> <dict> - <key>line</key><integer>25</integer> + <key>line</key><integer>34</integer> <key>col</key><integer>3</integer> <key>file</key><integer>0</integer> </dict> <dict> - <key>line</key><integer>25</integer> + <key>line</key><integer>34</integer> <key>col</key><integer>5</integer> <key>file</key><integer>0</integer> </dict> @@ -30,12 +30,12 @@ <key>end</key> <array> <dict> - <key>line</key><integer>26</integer> + <key>line</key><integer>35</integer> <key>col</key><integer>3</integer> <key>file</key><integer>0</integer> </dict> <dict> - <key>line</key><integer>26</integer> + <key>line</key><integer>35</integer> <key>col</key><integer>21</integer> <key>file</key><integer>0</integer> </dict> @@ -47,7 +47,7 @@ <key>kind</key><string>macro_expansion</string> <key>location</key> <dict> - <key>line</key><integer>26</integer> + <key>line</key><integer>35</integer> <key>col</key><integer>3</integer> <key>file</key><integer>0</integer> </dict> @@ -58,7 +58,7 @@ <key>kind</key><string>event</string> <key>location</key> <dict> - <key>line</key><integer>26</integer> + <key>line</key><integer>35</integer> <key>col</key><integer>3</integer> <key>file</key><integer>0</integer> </dict> @@ -66,12 +66,12 @@ <array> <array> <dict> - <key>line</key><integer>26</integer> + <key>line</key><integer>35</integer> <key>col</key><integer>3</integer> <key>file</key><integer>0</integer> </dict> <dict> - <key>line</key><integer>26</integer> + <key>line</key><integer>35</integer> <key>col</key><integer>21</integer> <key>file</key><integer>0</integer> </dict> @@ -91,12 +91,12 @@ <key>start</key> <array> <dict> - <key>line</key><integer>27</integer> + <key>line</key><integer>36</integer> <key>col</key><integer>3</integer> <key>file</key><integer>0</integer> </dict> <dict> - <key>line</key><integer>27</integer> + <key>line</key><integer>36</integer> <key>col</key><integer>3</integer> <key>file</key><integer>0</integer> </dict> @@ -104,12 +104,12 @@ <key>end</key> <array> <dict> - <key>line</key><integer>27</integer> + <key>line</key><integer>36</integer> <key>col</key><integer>8</integer> <key>file</key><integer>0</integer> </dict> <dict> - <key>line</key><integer>27</integer> + <key>line</key><integer>36</integer> <key>col</key><integer>8</integer> <key>file</key><integer>0</integer> </dict> @@ -121,7 +121,7 @@ <key>kind</key><string>event</string> <key>location</key> <dict> - <key>line</key><integer>27</integer> + <key>line</key><integer>36</integer> <key>col</key><integer>8</integer> <key>file</key><integer>0</integer> </dict> @@ -129,12 +129,12 @@ <array> <array> <dict> - <key>line</key><integer>27</integer> + <key>line</key><integer>36</integer> <key>col</key><integer>4</integer> <key>file</key><integer>0</integer> </dict> <dict> - <key>line</key><integer>27</integer> + <key>line</key><integer>36</integer> <key>col</key><integer>6</integer> <key>file</key><integer>0</integer> </dict> @@ -158,7 +158,7 @@ <key>issue_hash_function_offset</key><string>3</string> <key>location</key> <dict> - <key>line</key><integer>27</integer> + <key>line</key><integer>36</integer> <key>col</key><integer>8</integer> <key>file</key><integer>0</integer> </dict> @@ -166,10 +166,10 @@ <dict> <key>0</key> <array> - <integer>24</integer> - <integer>25</integer> - <integer>26</integer> - <integer>27</integer> + <integer>33</integer> + <integer>34</integer> + <integer>35</integer> + <integer>36</integer> </array> </dict> </dict> @@ -184,12 +184,12 @@ <key>start</key> <array> <dict> - <key>line</key><integer>38</integer> + <key>line</key><integer>47</integer> <key>col</key><integer>3</integer> <key>file</key><integer>0</integer> </dict> <dict> - <key>line</key><integer>38</integer> + <key>line</key><integer>47</integer> <key>col</key><integer>5</integer> <key>file</key><integer>0</integer> </dict> @@ -197,12 +197,12 @@ <key>end</key> <array> <dict> - <key>line</key><integer>39</integer> + <key>line</key><integer>48</integer> <key>col</key><integer>3</integer> <key>file</key><integer>0</integer> </dict> <dict> - <key>line</key><integer>39</integer> + <key>line</key><integer>48</integer> <key>col</key><integer>39</integer> <key>file</key><integer>0</integer> </dict> @@ -214,7 +214,7 @@ <key>kind</key><string>macro_expansion</string> <key>location</key> <dict> - <key>line</key><integer>39</integer> + <key>line</key><integer>48</integer> <key>col</key><integer>3</integer> <key>file</key><integer>0</integer> </dict> @@ -225,7 +225,7 @@ <key>kind</key><string>event</string> <key>location</key> <dict> - <key>line</key><integer>39</integer> + <key>line</key><integer>48</integer> <key>col</key><integer>3</integer> <key>file</key><integer>0</integer> </dict> @@ -233,12 +233,12 @@ <array> <array> <dict> - <key>line</key><integer>39</integer> + <key>line</key><integer>48</integer> <key>col</key><integer>3</integer> <key>file</key><integer>0</integer> </dict> <dict> - <key>line</key><integer>39</integer> + <key>line</key><integer>48</integer> <key>col</key><integer>39</integer> <key>file</key><integer>0</integer> </dict> @@ -258,12 +258,12 @@ <key>start</key> <array> <dict> - <key>line</key><integer>40</integer> + <key>line</key><integer>49</integer> <key>col</key><integer>3</integer> <key>file</key><integer>0</integer> </dict> <dict> - <key>line</key><integer>40</integer> + <key>line</key><integer>49</integer> <key>col</key><integer>3</integer> <key>file</key><integer>0</integer> </dict> @@ -271,12 +271,12 @@ <key>end</key> <array> <dict> - <key>line</key><integer>40</integer> + <key>line</key><integer>49</integer> <key>col</key><integer>8</integer> <key>file</key><integer>0</integer> </dict> <dict> - <key>line</key><integer>40</integer> + <key>line</key><integer>49</integer> <key>col</key><integer>8</integer> <key>file</key><integer>0</integer> </dict> @@ -288,7 +288,7 @@ <key>kind</key><string>event</string> <key>location</key> <dict> - <key>line</key><integer>40</integer> + <key>line</key><integer>49</integer> <key>col</key><integer>8</integer> <key>file</key><integer>0</integer> </dict> @@ -296,12 +296,12 @@ <array> <array> <dict> - <key>line</key><integer>40</integer> + <key>line</key><integer>49</integer> <key>col</key><integer>4</integer> <key>file</key><integer>0</integer> </dict> <dict> - <key>line</key><integer>40</integer> + <key>line</key><integer>49</integer> <key>col</key><integer>6</integer> <key>file</key><integer>0</integer> </dict> @@ -325,7 +325,7 @@ <key>issue_hash_function_offset</key><string>3</string> <key>location</key> <dict> - <key>line</key><integer>40</integer> + <key>line</key><integer>49</integer> <key>col</key><integer>8</integer> <key>file</key><integer>0</integer> </dict> @@ -333,10 +333,10 @@ <dict> <key>0</key> <array> - <integer>37</integer> - <integer>38</integer> - <integer>39</integer> - <integer>40</integer> + <integer>46</integer> + <integer>47</integer> + <integer>48</integer> + <integer>49</integer> </array> </dict> </dict> 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*);