xref: /minix3/external/bsd/llvm/dist/clang/docs/analyzer/DebugChecks.rst (revision f4a2713ac843a11c696ec80c0a5e3e5d80b4d338)
1*f4a2713aSLionel Sambuc============
2*f4a2713aSLionel SambucDebug Checks
3*f4a2713aSLionel Sambuc============
4*f4a2713aSLionel Sambuc
5*f4a2713aSLionel Sambuc.. contents::
6*f4a2713aSLionel Sambuc   :local:
7*f4a2713aSLionel Sambuc
8*f4a2713aSLionel SambucThe analyzer contains a number of checkers which can aid in debugging. Enable
9*f4a2713aSLionel Sambucthem by using the "-analyzer-checker=" flag, followed by the name of the
10*f4a2713aSLionel Sambucchecker.
11*f4a2713aSLionel Sambuc
12*f4a2713aSLionel Sambuc
13*f4a2713aSLionel SambucGeneral Analysis Dumpers
14*f4a2713aSLionel Sambuc========================
15*f4a2713aSLionel Sambuc
16*f4a2713aSLionel SambucThese checkers are used to dump the results of various infrastructural analyses
17*f4a2713aSLionel Sambucto stderr. Some checkers also have "view" variants, which will display a graph
18*f4a2713aSLionel Sambucusing a 'dot' format viewer (such as Graphviz on OS X) instead.
19*f4a2713aSLionel Sambuc
20*f4a2713aSLionel Sambuc- debug.DumpCallGraph, debug.ViewCallGraph: Show the call graph generated for
21*f4a2713aSLionel Sambuc  the current translation unit. This is used to determine the order in which to
22*f4a2713aSLionel Sambuc  analyze functions when inlining is enabled.
23*f4a2713aSLionel Sambuc
24*f4a2713aSLionel Sambuc- debug.DumpCFG, debug.ViewCFG: Show the CFG generated for each top-level
25*f4a2713aSLionel Sambuc  function being analyzed.
26*f4a2713aSLionel Sambuc
27*f4a2713aSLionel Sambuc- debug.DumpDominators: Shows the dominance tree for the CFG of each top-level
28*f4a2713aSLionel Sambuc  function.
29*f4a2713aSLionel Sambuc
30*f4a2713aSLionel Sambuc- debug.DumpLiveVars: Show the results of live variable analysis for each
31*f4a2713aSLionel Sambuc  top-level function being analyzed.
32*f4a2713aSLionel Sambuc
33*f4a2713aSLionel Sambuc- debug.ViewExplodedGraph: Show the Exploded Graphs generated for the
34*f4a2713aSLionel Sambuc  analysis of different functions in the input translation unit. When there
35*f4a2713aSLionel Sambuc  are several functions analyzed, display one graph per function. Beware
36*f4a2713aSLionel Sambuc  that these graphs may grow very large, even for small functions.
37*f4a2713aSLionel Sambuc
38*f4a2713aSLionel SambucPath Tracking
39*f4a2713aSLionel Sambuc=============
40*f4a2713aSLionel Sambuc
41*f4a2713aSLionel SambucThese checkers print information about the path taken by the analyzer engine.
42*f4a2713aSLionel Sambuc
43*f4a2713aSLionel Sambuc- debug.DumpCalls: Prints out every function or method call encountered during a
44*f4a2713aSLionel Sambuc  path traversal. This is indented to show the call stack, but does NOT do any
45*f4a2713aSLionel Sambuc  special handling of branches, meaning different paths could end up
46*f4a2713aSLionel Sambuc  interleaved.
47*f4a2713aSLionel Sambuc
48*f4a2713aSLionel Sambuc- debug.DumpTraversal: Prints the name of each branch statement encountered
49*f4a2713aSLionel Sambuc  during a path traversal ("IfStmt", "WhileStmt", etc). Currently used to check
50*f4a2713aSLionel Sambuc  whether the analysis engine is doing BFS or DFS.
51*f4a2713aSLionel Sambuc
52*f4a2713aSLionel Sambuc
53*f4a2713aSLionel SambucState Checking
54*f4a2713aSLionel Sambuc==============
55*f4a2713aSLionel Sambuc
56*f4a2713aSLionel SambucThese checkers will print out information about the analyzer state in the form
57*f4a2713aSLionel Sambucof analysis warnings. They are intended for use with the -verify functionality
58*f4a2713aSLionel Sambucin regression tests.
59*f4a2713aSLionel Sambuc
60*f4a2713aSLionel Sambuc- debug.TaintTest: Prints out the word "tainted" for every expression that
61*f4a2713aSLionel Sambuc  carries taint. At the time of this writing, taint was only introduced by the
62*f4a2713aSLionel Sambuc  checks under experimental.security.taint.TaintPropagation; this checker may
63*f4a2713aSLionel Sambuc  eventually move to the security.taint package.
64*f4a2713aSLionel Sambuc
65*f4a2713aSLionel Sambuc- debug.ExprInspection: Responds to certain function calls, which are modeled
66*f4a2713aSLionel Sambuc  after builtins. These function calls should affect the program state other
67*f4a2713aSLionel Sambuc  than the evaluation of their arguments; to use them, you will need to declare
68*f4a2713aSLionel Sambuc  them within your test file. The available functions are described below.
69*f4a2713aSLionel Sambuc
70*f4a2713aSLionel Sambuc(FIXME: debug.ExprInspection should probably be renamed, since it no longer only
71*f4a2713aSLionel Sambucinspects expressions.)
72*f4a2713aSLionel Sambuc
73*f4a2713aSLionel Sambuc
74*f4a2713aSLionel SambucExprInspection checks
75*f4a2713aSLionel Sambuc---------------------
76*f4a2713aSLionel Sambuc
77*f4a2713aSLionel Sambuc- void clang_analyzer_eval(bool);
78*f4a2713aSLionel Sambuc
79*f4a2713aSLionel Sambuc  Prints TRUE if the argument is known to have a non-zero value, FALSE if the
80*f4a2713aSLionel Sambuc  argument is known to have a zero or null value, and UNKNOWN if the argument
81*f4a2713aSLionel Sambuc  isn't sufficiently constrained on this path.  You can use this to test other
82*f4a2713aSLionel Sambuc  values by using expressions like "x == 5".  Note that this functionality is
83*f4a2713aSLionel Sambuc  currently DISABLED in inlined functions, since different calls to the same
84*f4a2713aSLionel Sambuc  inlined function could provide different information, making it difficult to
85*f4a2713aSLionel Sambuc  write proper -verify directives.
86*f4a2713aSLionel Sambuc
87*f4a2713aSLionel Sambuc  In C, the argument can be typed as 'int' or as '_Bool'.
88*f4a2713aSLionel Sambuc
89*f4a2713aSLionel Sambuc  Example usage::
90*f4a2713aSLionel Sambuc
91*f4a2713aSLionel Sambuc    clang_analyzer_eval(x); // expected-warning{{UNKNOWN}}
92*f4a2713aSLionel Sambuc    if (!x) return;
93*f4a2713aSLionel Sambuc    clang_analyzer_eval(x); // expected-warning{{TRUE}}
94*f4a2713aSLionel Sambuc
95*f4a2713aSLionel Sambuc
96*f4a2713aSLionel Sambuc- void clang_analyzer_checkInlined(bool);
97*f4a2713aSLionel Sambuc
98*f4a2713aSLionel Sambuc  If a call occurs within an inlined function, prints TRUE or FALSE according to
99*f4a2713aSLionel Sambuc  the value of its argument. If a call occurs outside an inlined function,
100*f4a2713aSLionel Sambuc  nothing is printed.
101*f4a2713aSLionel Sambuc
102*f4a2713aSLionel Sambuc  The intended use of this checker is to assert that a function is inlined at
103*f4a2713aSLionel Sambuc  least once (by passing 'true' and expecting a warning), or to assert that a
104*f4a2713aSLionel Sambuc  function is never inlined (by passing 'false' and expecting no warning). The
105*f4a2713aSLionel Sambuc  argument is technically unnecessary but is intended to clarify intent.
106*f4a2713aSLionel Sambuc
107*f4a2713aSLionel Sambuc  You might wonder why we can't print TRUE if a function is ever inlined and
108*f4a2713aSLionel Sambuc  FALSE if it is not. The problem is that any inlined function could conceivably
109*f4a2713aSLionel Sambuc  also be analyzed as a top-level function (in which case both TRUE and FALSE
110*f4a2713aSLionel Sambuc  would be printed), depending on the value of the -analyzer-inlining option.
111*f4a2713aSLionel Sambuc
112*f4a2713aSLionel Sambuc  In C, the argument can be typed as 'int' or as '_Bool'.
113*f4a2713aSLionel Sambuc
114*f4a2713aSLionel Sambuc  Example usage::
115*f4a2713aSLionel Sambuc
116*f4a2713aSLionel Sambuc    int inlined() {
117*f4a2713aSLionel Sambuc      clang_analyzer_checkInlined(true); // expected-warning{{TRUE}}
118*f4a2713aSLionel Sambuc      return 42;
119*f4a2713aSLionel Sambuc    }
120*f4a2713aSLionel Sambuc
121*f4a2713aSLionel Sambuc    void topLevel() {
122*f4a2713aSLionel Sambuc      clang_analyzer_checkInlined(false); // no-warning (not inlined)
123*f4a2713aSLionel Sambuc      int value = inlined();
124*f4a2713aSLionel Sambuc      // This assertion will not be valid if the previous call was not inlined.
125*f4a2713aSLionel Sambuc      clang_analyzer_eval(value == 42); // expected-warning{{TRUE}}
126*f4a2713aSLionel Sambuc    }
127*f4a2713aSLionel Sambuc
128*f4a2713aSLionel Sambuc- void clang_analyzer_warnIfReached();
129*f4a2713aSLionel Sambuc
130*f4a2713aSLionel Sambuc  Generate a warning if this line of code gets reached by the analyzer.
131*f4a2713aSLionel Sambuc
132*f4a2713aSLionel Sambuc  Example usage::
133*f4a2713aSLionel Sambuc
134*f4a2713aSLionel Sambuc    if (true) {
135*f4a2713aSLionel Sambuc      clang_analyzer_warnIfReached();  // expected-warning{{REACHABLE}}
136*f4a2713aSLionel Sambuc    }
137*f4a2713aSLionel Sambuc    else {
138*f4a2713aSLionel Sambuc      clang_analyzer_warnIfReached();  // no-warning
139*f4a2713aSLionel Sambuc    }
140*f4a2713aSLionel Sambuc
141*f4a2713aSLionel Sambuc
142*f4a2713aSLionel SambucStatistics
143*f4a2713aSLionel Sambuc==========
144*f4a2713aSLionel Sambuc
145*f4a2713aSLionel SambucThe debug.Stats checker collects various information about the analysis of each
146*f4a2713aSLionel Sambucfunction, such as how many blocks were reached and if the analyzer timed out.
147*f4a2713aSLionel Sambuc
148*f4a2713aSLionel SambucThere is also an additional -analyzer-stats flag, which enables various
149*f4a2713aSLionel Sambucstatistics within the analyzer engine. Note the Stats checker (which produces at
150*f4a2713aSLionel Sambucleast one bug report per function) may actually change the values reported by
151*f4a2713aSLionel Sambuc-analyzer-stats.
152