8 The analyzer contains a number of checkers which can aid in debugging. Enable
9 them by using the "-analyzer-checker=" flag, followed by the name of the
13 General Analysis Dumpers
14 ========================
16 These checkers are used to dump the results of various infrastructural analyses
17 to stderr. Some checkers also have "view" variants, which will display a graph
18 using a 'dot' format viewer (such as Graphviz on macOS) instead.
20 - debug.DumpCallGraph, debug.ViewCallGraph: Show the call graph generated for
21 the current translation unit. This is used to determine the order in which to
22 analyze functions when inlining is enabled.
24 - debug.DumpCFG, debug.ViewCFG: Show the CFG generated for each top-level
25 function being analyzed.
27 - debug.DumpDominators: Shows the dominance tree for the CFG of each top-level
30 - debug.DumpLiveVars: Show the results of live variable analysis for each
31 top-level function being analyzed.
33 - debug.DumpLiveExprs: Show the results of live expression analysis for each
34 top-level function being analyzed.
36 - debug.ViewExplodedGraph: Show the Exploded Graphs generated for the
37 analysis of different functions in the input translation unit. When there
38 are several functions analyzed, display one graph per function. Beware
39 that these graphs may grow very large, even for small functions.
44 These checkers print information about the path taken by the analyzer engine.
46 - debug.DumpCalls: Prints out every function or method call encountered during a
47 path traversal. This is indented to show the call stack, but does NOT do any
48 special handling of branches, meaning different paths could end up
51 - debug.DumpTraversal: Prints the name of each branch statement encountered
52 during a path traversal ("IfStmt", "WhileStmt", etc). Currently used to check
53 whether the analysis engine is doing BFS or DFS.
59 These checkers will print out information about the analyzer state in the form
60 of analysis warnings. They are intended for use with the -verify functionality
63 - debug.TaintTest: Prints out the word "tainted" for every expression that
64 carries taint. At the time of this writing, taint was only introduced by the
65 checks under experimental.security.taint.TaintPropagation; this checker may
66 eventually move to the security.taint package.
68 - debug.ExprInspection: Responds to certain function calls, which are modeled
69 after builtins. These function calls should affect the program state other
70 than the evaluation of their arguments; to use them, you will need to declare
71 them within your test file. The available functions are described below.
73 (FIXME: debug.ExprInspection should probably be renamed, since it no longer only
74 inspects expressions.)
80 - ``void clang_analyzer_eval(bool);``
82 Prints TRUE if the argument is known to have a non-zero value, FALSE if the
83 argument is known to have a zero or null value, and UNKNOWN if the argument
84 isn't sufficiently constrained on this path. You can use this to test other
85 values by using expressions like "x == 5". Note that this functionality is
86 currently DISABLED in inlined functions, since different calls to the same
87 inlined function could provide different information, making it difficult to
88 write proper -verify directives.
90 In C, the argument can be typed as 'int' or as '_Bool'.
94 clang_analyzer_eval(x); // expected-warning{{UNKNOWN}}
96 clang_analyzer_eval(x); // expected-warning{{TRUE}}
99 - ``void clang_analyzer_checkInlined(bool);``
101 If a call occurs within an inlined function, prints TRUE or FALSE according to
102 the value of its argument. If a call occurs outside an inlined function,
105 The intended use of this checker is to assert that a function is inlined at
106 least once (by passing 'true' and expecting a warning), or to assert that a
107 function is never inlined (by passing 'false' and expecting no warning). The
108 argument is technically unnecessary but is intended to clarify intent.
110 You might wonder why we can't print TRUE if a function is ever inlined and
111 FALSE if it is not. The problem is that any inlined function could conceivably
112 also be analyzed as a top-level function (in which case both TRUE and FALSE
113 would be printed), depending on the value of the -analyzer-inlining option.
115 In C, the argument can be typed as 'int' or as '_Bool'.
120 clang_analyzer_checkInlined(true); // expected-warning{{TRUE}}
125 clang_analyzer_checkInlined(false); // no-warning (not inlined)
126 int value = inlined();
127 // This assertion will not be valid if the previous call was not inlined.
128 clang_analyzer_eval(value == 42); // expected-warning{{TRUE}}
131 - ``void clang_analyzer_warnIfReached();``
133 Generate a warning if this line of code gets reached by the analyzer.
138 clang_analyzer_warnIfReached(); // expected-warning{{REACHABLE}}
141 clang_analyzer_warnIfReached(); // no-warning
144 - ``void clang_analyzer_numTimesReached();``
146 Same as above, but include the number of times this call expression
147 gets reached by the analyzer during the current analysis.
151 for (int x = 0; x < 3; ++x) {
152 clang_analyzer_numTimesReached(); // expected-warning{{3}}
155 - ``void clang_analyzer_warnOnDeadSymbol(int);``
157 Subscribe for a delayed warning when the symbol that represents the value of
158 the argument is garbage-collected by the analyzer.
160 When calling 'clang_analyzer_warnOnDeadSymbol(x)', if value of 'x' is a
161 symbol, then this symbol is marked by the ExprInspection checker. Then,
162 during each garbage collection run, the checker sees if the marked symbol is
163 being collected and issues the 'SYMBOL DEAD' warning if it does.
164 This way you know where exactly, up to the line of code, the symbol dies.
166 It is unlikely that you call this function after the symbol is already dead,
167 because the very reference to it as the function argument prevents it from
168 dying. However, if the argument is not a symbol but a concrete value,
169 no warning would be issued.
174 int x = generate_some_integer();
175 clang_analyzer_warnOnDeadSymbol(x);
176 } while(0); // expected-warning{{SYMBOL DEAD}}
179 - ``void clang_analyzer_explain(a single argument of any type);``
181 This function explains the value of its argument in a human-readable manner
182 in the warning message. You can make as many overrides of its prototype
183 in the test code as necessary to explain various integral, pointer,
184 or even record-type values. To simplify usage in C code (where overloading
185 the function declaration is not allowed), you may append an arbitrary suffix
186 to the function name, without affecting functionality.
190 void clang_analyzer_explain(int);
191 void clang_analyzer_explain(void *);
194 void clang_analyzer_explain_int(int);
196 void foo(int param, void *ptr) {
197 clang_analyzer_explain(param); // expected-warning{{argument 'param'}}
198 clang_analyzer_explain_int(param); // expected-warning{{argument 'param'}}
200 clang_analyzer_explain(ptr); // expected-warning{{memory address '0'}}
203 - ``void clang_analyzer_dump( /* a single argument of any type */);``
205 Similar to clang_analyzer_explain, but produces a raw dump of the value,
206 same as SVal::dump().
210 void clang_analyzer_dump(int);
212 clang_analyzer_dump(x); // expected-warning{{reg_$0<x>}}
215 - ``size_t clang_analyzer_getExtent(void *);``
217 This function returns the value that represents the extent of a memory region
218 pointed to by the argument. This value is often difficult to obtain otherwise,
219 because no valid code that produces this value. However, it may be useful
220 for testing purposes, to see how well does the analyzer model region extents.
226 size_t xs = clang_analyzer_getExtent(&x);
227 clang_analyzer_explain(xs); // expected-warning{{'4'}}
228 size_t ys = clang_analyzer_getExtent(&y);
229 clang_analyzer_explain(ys); // expected-warning{{'8'}}
232 - ``void clang_analyzer_printState();``
234 Dumps the current ProgramState to the stderr. Quickly lookup the program state
235 at any execution point without ViewExplodedGraph or re-compiling the program.
236 This is not very useful for writing tests (apart from testing how ProgramState
237 gets printed), but useful for debugging tests. Also, this method doesn't
238 produce a warning, so it gets printed on the console before all other
239 ExprInspection warnings.
245 clang_analyzer_printState(); // Read the stderr!
248 - ``void clang_analyzer_hashDump(int);``
250 The analyzer can generate a hash to identify reports. To debug what information
251 is used to calculate this hash it is possible to dump the hashed string as a
252 warning of an arbitrary expression using the function above.
258 clang_analyzer_hashDump(x); // expected-warning{{hashed string for x}}
261 - ``void clang_analyzer_denote(int, const char *);``
263 Denotes symbols with strings. A subsequent call to clang_analyzer_express()
264 will expresses another symbol in terms of these string. Useful for testing
265 relationships between different symbols.
270 clang_analyzer_denote(x, "$x");
271 clang_analyzer_express(x + 1); // expected-warning{{$x + 1}}
274 - ``void clang_analyzer_express(int);``
276 See clang_analyzer_denote().
278 - ``void clang_analyzer_isTainted(a single argument of any type);``
280 Queries the analyzer whether the expression used as argument is tainted or not.
281 This is useful in tests, where we don't want to issue warning for all tainted
282 expressions but only check for certain expressions.
283 This would help to reduce the *noise* that the `TaintTest` debug checker would
284 introduce and let you focus on the `expected-warning`'s that you really care
291 clang_analyzer_isTainted(n); // expected-warning{{NO}}
293 clang_analyzer_isTainted(n); // expected-warning{{YES}}
294 clang_analyzer_isTainted(n + 2); // expected-warning{{YES}}
295 clang_analyzer_isTainted(n > 0); // expected-warning{{YES}}
296 int next_tainted_value = n; // no-warning
300 - ``clang_analyzer_dumpExtent(a single argument of any type)``
301 - ``clang_analyzer_dumpElementCount(a single argument of any type)``
303 Dumps out the extent and the element count of the argument.
309 clang_analyzer_dumpExtent(a); // expected-warning {{8 S64b}}
310 clang_analyzer_dumpElementCount(a); // expected-warning {{2 S64b}}
313 - ``clang_analyzer_value(a single argument of integer or pointer type)``
315 Prints an associated value for the given argument.
316 Supported argument types are integers, enums and pointers.
317 The value can be represented either as a range set or as a concrete integer.
318 For the rest of the types function prints ``n/a`` (aka not available).
320 **Note:** This function will print nothing for clang built with Z3 constraint manager.
321 This may cause crashes of your tests. To manage this use one of the test constraining
324 * llvm-lit commands ``REQUIRES no-z3`` or ``UNSUPPORTED z3`` `See for details. <https://llvm.org/docs/TestingGuide.html#constraining-test-execution>`_
326 * a preprocessor directive ``#ifndef ANALYZER_CM_Z3``
328 * a clang command argument ``-analyzer-constraints=range``
332 void print(char c, unsigned u) {
333 clang_analyzer_value(c); // expected-warning {{8s:{ [-128, 127] }}}
335 clang_analyzer_value(u); // expected-warning {{32u:{ [0, 41], [43, 4294967295] }}}
337 clang_analyzer_value(u); // expected-warning {{32u:42}}
343 The debug.Stats checker collects various information about the analysis of each
344 function, such as how many blocks were reached and if the analyzer timed out.
346 There is also an additional -analyzer-stats flag, which enables various
347 statistics within the analyzer engine. Note the Stats checker (which produces at
348 least one bug report per function) may actually change the values reported by
351 Output testing checkers
352 =======================
354 - debug.ReportStmts reports a warning at **every** statement, making it a very
355 useful tool for testing thoroughly bug report construction and output