blob: 76e85e10bac00b4c26c7c235551a28196f2937ba [file] [log] [blame]
Ted Kremenekce2e3322008-04-25 18:44:36 +00001<html>
2<head>
3 <title>Information on using the Static Analyzer ("LLVM Checker")</title>
Ted Kremenek20c5f142008-06-11 05:26:52 +00004 <link type="text/css" rel="stylesheet" href="menu.css" />
5 <link type="text/css" rel="stylesheet" href="content.css" />
Ted Kremeneke0665ab2008-06-11 05:55:39 +00006 <style>
Ted Kremenek482c6d42008-06-11 05:28:36 +00007 thead {
8 background-color:#eee; color:#666666;
9 font-weight: bold; cursor: default;
10 text-align:center;
11 border-top: 2px solid #000000;
12 border-bottom: 2px solid #000000;
13 font-weight: bold; font-family: Verdana
14 }
15 table { border: 1px #000000 solid }
16 table { border-collapse: collapse; border-spacing: 0px }
17 table { margin-left:20px; margin-top:20px; margin-bottom:20px }
18 td { border-bottom: 1px #000000 dotted }
19 td { padding:5px; padding-left:8px; padding-right:8px }
20 td { text-align:left; font-size:9pt }
21 td.View { padding-left: 10px }
22 </style>
Ted Kremenekce2e3322008-04-25 18:44:36 +000023</head>
24<body>
25
Ted Kremenek20c5f142008-06-11 05:26:52 +000026<!--#include virtual="menu.html.incl"-->
27
28<div id="content">
29
Ted Kremenek43bfbaf2008-06-11 06:01:28 +000030<h1>Information on using the Static Analyzer</h1>
Ted Kremenekce2e3322008-04-25 18:44:36 +000031
Ted Kremenekbe6e5162008-06-11 05:25:12 +000032<p>This documents provides some notes on using the LLVM/clang static analyzer to
33find bugs in C and Objective-C programs.</p>
34
Ted Kremenekb8a522f2008-06-11 16:09:34 +000035<p>Currently the analyzer is invoked as a command-line tool. It is intended to
Ted Kremenekbc757862008-06-12 18:39:02 +000036run in tandem with a build of a project or code base. Analysis results are
Ted Kremenekb8a522f2008-06-11 16:09:34 +000037deposited in a directory as HTML files, which can then viewed using a web
Ted Kremenekbc757862008-06-12 18:39:02 +000038browser (open the generated <tt>index.html</tt> file to view a list of flagged
39errors).</p>
Ted Kremenekb8a522f2008-06-11 16:09:34 +000040
Ted Kremenekbc757862008-06-12 18:39:02 +000041<h3>Important Notes (Please Read)</h3>
42
43<p>Here are some important points we ask you to consider when using the static
44analyzer:</p>
45
46<ul>
47
48<li><b>This tool is <b>very early</b> in development.</b> There are many planned
49enhancements to improve both the precision and scope of its analysis algorithms
50as well as the kinds bugs it will find.</li>
51
52<li><b>False positives.</b> Static analysis reasons about the run-time behavior
53of a program without actually running the program. Static analysis is not
54perfect, and can falsely flag bugs in a program where the code behaves
55correctly. Because some code checks performed by the analyzer require more
56analysis than others, the frequency of false positives can vary widely between
57different checks. Our goal is to have the analyzer have a low false positive
58rate for most code on all checks, and we will reach this goal by gradually
59improving over time the analysis precision of the tool.</li>
60
61<li><b>Static analysis can be much slower than compilation.</b> The analyzer
62performs a variety of checks on code, each requiring different levels of
63analysis precision (more precision = more CPU time). While the analyzer is being
64designed to be as fast and light-weight as possible, please do not expect it to
65be as fast as compiling a program (even with optimizations enabled). Some of the
66algorithms needed to find bugs require in the worst case exponential time. The
67analyzer runs in a reasonable amount of time by both bounding the amount of
68checking work it will do as well as using clever algorithms to reduce the amount
69of work it must do to find bugs.</li>
70
71</ul>
Ted Kremenekb8a522f2008-06-11 16:09:34 +000072
73<h3>Organization</h3>
74
75<p>This page is arranged into the following sections:</p>
Ted Kremenekce2e3322008-04-25 18:44:36 +000076
77<ul>
Ted Kremenekbd317162008-06-09 14:30:01 +000078 <li><a href="#Contents">Obtaining the Analyzer</a></li>
Ted Kremenekce2e3322008-04-25 18:44:36 +000079 <li><a href="#BasicUsage">Basic Usage</a></li>
Ted Kremenek891a3532008-04-25 20:29:37 +000080 <li><a href="#Output">Output of the Analyzer</a></li>
81 <li><a href="#RecommendedUsageGuidelines">Recommended Usage Guidelines</a></li>
Ted Kremenekbc757862008-06-12 18:39:02 +000082 <li><a href="#Debugging">Debugging the Analyzer</a></li>
83 <li><a href="#filingbugs">Filing Bugs</a></li>
Ted Kremenekce2e3322008-04-25 18:44:36 +000084</ul>
85
Ted Kremenekbd317162008-06-09 14:30:01 +000086<h2 id="ReleaseContents">Obtaining the Analyzer</h2>
Ted Kremenekce2e3322008-04-25 18:44:36 +000087
Ted Kremenekbe6e5162008-06-11 05:25:12 +000088<p> Using the analyzer involves executing <tt>scan-build</tt> (see <a
89href="#BasicUsage">Basic Usage</a>). <tt>scan-build</tt> will first look for a
90<tt>clang</tt> executable in the same directory as <tt>scan-build</tt>, and then
91search your path.</p>
92
93<p>If one is using the analyzer directly from the Clang sources, it suffices to
94just directly execute <tt>scan-build</tt> in the <tt>utils</tt> directory. No
95other special installation is needed.</p>
96
Ted Kremenekd53ab9f2008-06-11 16:16:41 +000097<h3>Packaged Builds (Mac OS X)</h3>
Ted Kremenekbe6e5162008-06-11 05:25:12 +000098
Ted Kremenekbc757862008-06-12 18:39:02 +000099<p>Semi-regular pre-built binaries of the analyzer are available on Mac OS X
100(10.5).</p>
Ted Kremenekd53ab9f2008-06-11 16:16:41 +0000101
102<p>The latest build is: <b><a
Ted Kremenekbc757862008-06-12 18:39:02 +0000103href="http://keeda.stanford.edu/~kremenek/checker/checker-36.tar.gz">checker-36.tar.gz</a></b> (built June 11, 2008)</p>
Ted Kremenekd53ab9f2008-06-11 16:16:41 +0000104
Ted Kremenekbc757862008-06-12 18:39:02 +0000105Packaged builds for other platforms may eventually be provided, but as the tool
106is in its early stages we are not actively promoting releases yet. If you wish
107to help contribute regular builds of the analyzer on other platforms, please
108email the <a href="http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev">Clang
109Developers' mailing list</a>.</p>
Ted Kremenekbd317162008-06-09 14:30:01 +0000110
Ted Kremenekbe6e5162008-06-11 05:25:12 +0000111<p>Packaged builds of the analyzer expand to the following files:</p>
Ted Kremenekce2e3322008-04-25 18:44:36 +0000112
Ted Kremenekbe6e5162008-06-11 05:25:12 +0000113<table id="package">
Ted Kremenekce2e3322008-04-25 18:44:36 +0000114<thead><tr><td>File</td><td>Purpose</td></tr></thead>
115<tr><td><tt><b>scan-build</b></tt></td><td>Script for running the analyzer over a project build. <b>This is the only file you care about.</b></td></tr>
116<tr><td><tt>ccc-analyzer</tt></td><td>GCC interceptor (called by scan-build)</td></tr>
117<tr><td><tt>clang</tt></td><td>Static Analyzer (called by ccc-analyzer)</td><tr>
118<tr><td><tt>sorttable.js</tt></td><td>JavaScript used for displaying error reports</td></tr>
119</table>
120
Ted Kremenekbe6e5162008-06-11 05:25:12 +0000121<h3>Other Platforms (Building the Analyzer from Source)</h3>
122
123<p>Packaged builds simply consist of a few files from the Clang source tree,
124meaning that <b>anyone</b> who can build Clang can use the static analyzer.
125Please see the <a href="get_started.html">Getting Started</a> page for more
126details on downloading and compiling Clang.</p>
127
128<p>All files used by the analyzer (and included in packaged builds; <a
129href="#package">see above</a>) other than a compiled <tt>clang</tt> executable
130are found in the <tt>utils</tt> subdirectory in the Clang tree.</p>
Ted Kremenekbd317162008-06-09 14:30:01 +0000131
Ted Kremenekce2e3322008-04-25 18:44:36 +0000132<h2 id="BasicUsage">Basic Usage</h2>
133
134<p>The analyzer is executed from the command-line. To run the analyzer, you will
135use <tt>scan-build</tt> to analyze the source files compiled by <tt>gcc</tt>
136during a project build.</p>
137
138<p>For example, to analyze the files compiled under a build:</p>
139
140<pre>
141 $ <b>scan-build</b> make
142 $ <b>scan-build</b> xcodebuild
143</pre>
144
145<p> In the first case <tt>scan-build</tt> analyzes the code of a project built
146with <tt>make</tt>, andin the second case <tt>scan-build</tt> analyzes a project
147built using <tt>xcodebuild</tt>. In general, the format is: </p>
148
149<pre>
150 $ <b>scan-build</b> <i>[scan-build options]</i> <b>&lt;command&gt;</b> <i>[command options]</i>
151</pre>
152
153<p> Operationally, <tt>scan-build</tt> literally runs <command> with all of the
154subsequent options passed to it. For example</p>
155
156<pre>
157 $ scan-build make <b>-j4</b>
158</pre>
159
160<p>In this example, <tt>scan-build</tt> makes no effort to interpret the options
161after the build command (in this case, <tt>make</tt>); it just passes them
162through. In general, <tt>scan-build</tt> should support parallel builds, but
163<b>not distributed builds</b>. Similarly, you can use <tt>scan-build</tt> to
164analyze specific files:
165
166<pre>
167 $ scan-build gcc -c <b>t1.c t2.c</b>
168</pre>
169
170<p>
171This example causes the files <tt>t1.c</tt> and <tt>t2.c</tt> to be analyzed.
172</p>
173
174<h3>Other Options</h3>
175
176<p>
177As mentioned above, extra options can be passed to <tt>scan-build</tt>. These
178options prefix the build command. For example:</p>
179
180<pre>
181 $ scan-build <b>-k -V</b> make
182 $ scan-build <b>-k -V</b> xcodebuild
183</pre>
184
185<p>Here are a complete list of options:</p>
186
187<table>
188 <thead><tr><td>Option</td><td>Description</td></tr></thead>
189
Ted Kremenekbe6e5162008-06-11 05:25:12 +0000190 <tr><td><b>-a</b></td>
191 <td>The analysis to run. The default analysis is <i>checker-cfref</i>. Valid options are: <i>checker-cfref</i>, <i>fsyntax-only</i>.
192 These translate into options passed down to the <tt>clang</tt> executable, and currently this option is mainly used for debugging.</td></tr>
193
Ted Kremenekce2e3322008-04-25 18:44:36 +0000194 <tr><td><b>-o</b></td><td>Target directory for HTML report files. Subdirectories will be
195 created as needed to represent separate "runs" of the analyzer. If this option
196is not specified, a directory is created in <tt>/tmp</tt> to store the
197reports.</td><tr>
198
199 <tr><td><b>-h</b><br><i><nobr>(or no arguments)</nobr></i></td><td>Display <tt>scan-build</tt> options.</td></tr>
200
201 <tr><td><b>-k</b><br><nobr><b>--keep-going</b></nobr></td><td>Add a "keep on going" option to the
202 specified build command. <p>This option currently supports <tt>make</tt> and
203 <tt>xcodebuild</tt>.</p> <p>This is a convenience option; one can specify this
204 behavior directly using build options.</p></td></tr>
205
Ted Kremenekbe6e5162008-06-11 05:25:12 +0000206 <tr><td><b>-v<b></td><td>Verbose output from scan-build and the analyzer. <b>A second and third
Ted Kremenekce2e3322008-04-25 18:44:36 +0000207 "-v" increases verbosity</b>, and is useful for filing bug reports against the analyzer.</td></tr>
208
209 <tr><td><b>-V</b></td><td>View analysis results in a web browser when the build command completes.</td></tr>
210</table>
211
212<p>These options can also be viewed by running <tt>scan-build</tt> with no
213arguments:</p>
214
215<pre>
216 $ <b>scan-build</b>
217
218 USAGE: scan-build [options] &lt;build command&gt; [build options]
219
220 OPTIONS:
221
Ted Kremenekbe6e5162008-06-11 05:25:12 +0000222 -a - The analysis to run. The default is 'checker-cfref'.
223 Valid options are: 'checker-cfref', 'fsyntax-only'
224
Ted Kremenekce2e3322008-04-25 18:44:36 +0000225 -o - Target directory for HTML report files. Subdirectories
226 will be created as needed to represent separate "runs" of
227 the analyzer. If this option is not specified, a directory
228 is created in /tmp to store the reports.
229 <b>...</b>
230</pre>
231
232<h2 id="Output">Output of the Analyzer</h2>
233
234<p>
235The output of the analyzer is a set of HTML files, each one which represents a
236separate bug report. A single <tt>index.html</tt> file is generated for
237surveying all of the bugs. You can then just open <tt>index.html</tt> in a web
238browser to view the bug reports.
239</p>
240
241<p>
242Where the HTML files are generated is specified with a <b>-o</b> option to
Ted Kremenek87e795e2008-04-25 20:31:58 +0000243<tt>scan-build</tt>. If <b>-o</b> isn't specified, a directory in <tt>/tmp</tt>
Ted Kremenekce2e3322008-04-25 18:44:36 +0000244is created to store the files (<tt>scan-build</tt> will print a message telling
245you where they are). If you want to view the reports immediately after the build
246completes, pass <b>-V</b> to <tt>scan-build</tt>.
247</p>
248
249
Ted Kremenek904f1002008-04-25 20:30:34 +0000250<h2 id="RecommendedUsageGuidelines">Recommended Usage Guidelines</h2>
Ted Kremenekce2e3322008-04-25 18:44:36 +0000251
252Here are a few recommendations with running the analyzer:
253
254<h3>Always Analyze a Project in its "Debug" Configuration</h3>
255
Ted Kremenekbc757862008-06-12 18:39:02 +0000256<p>Most projects can be built in a "debug" mode that enables assertions.
257Assertions are picked up by the static analyzer to prune infeasible paths, which
258in some cases can greatly reduce the number of false positives (bogus error
259reports) emitted by the tool.</p>
Ted Kremenekce2e3322008-04-25 18:44:36 +0000260
261<h3>Pass -k to <tt>scan-build</tt></h3>
262
263<p>While <tt>ccc-analyzer</tt> invokes <tt>gcc</tt> to compile code, any
264problems in correctly forwarding arguments to <tt>gcc</tt> may result in a build
265failure. Passing <b>-k</b> to <tt>scan-build</tt> potentially allows you to
266analyze other code in a project for which this problem doesn't occur.</p>
267
268<p> Also, it is useful to analyze a project even if not all of the source files
269are compilable. This is great when using <tt>scan-build</tt> as part of your
270compile-debug cycle.</p>
271
272<h3>Use Verbose Output when Debugging <tt>scan-build</tt></h3>
273
Ted Kremenekbc757862008-06-12 18:39:02 +0000274<p><tt>scan-build</tt> takes a <b>-v</b> option to emit verbose output about
275what it's doing; two <b>-v</b> options emit more information. Redirecting the
276output of <tt>scan-build</tt> to a text file (make sure to redirect standard
277error) is useful for filing bug reports against <tt>scan-build</tt> or the
278analyzer, as we can see the exact options (and files) passed to the analyzer.
279For more comprehendible logs, don't perform a parallel build.</p>
Ted Kremenekce2e3322008-04-25 18:44:36 +0000280
281<h2 id="Debugging">Debugging the Analyzer</h2>
282
Ted Kremenekbc757862008-06-12 18:39:02 +0000283<p>This section provides information on debugging the analyzer, and troubleshooting
284it when you have problems analyzing a particular project.</p>
Ted Kremenekce2e3322008-04-25 18:44:36 +0000285
286<h3>How it Works</h3>
287
Ted Kremenekbc757862008-06-12 18:39:02 +0000288<p>To analyze a project, <tt>scan-build</tt> simply sets the environment variable
Ted Kremenekce2e3322008-04-25 18:44:36 +0000289<tt>CC</tt> to the full path to <tt>ccc-analyzer</tt>. It also sets a few other
290environment variables to communicate to <tt>ccc-analyzer</tt> where to dump HTML
Ted Kremenekbc757862008-06-12 18:39:02 +0000291report files.</p>
Ted Kremenekce2e3322008-04-25 18:44:36 +0000292
293<p>Some Makefiles (or equivalent project files) hardcode the compiler; for such
294projects simply overriding <tt>CC</tt> won't cause <tt>ccc-analyzer</tt> to be
295called. This will cause the compiled code <b>to not be analyzed.</b></p> If you
296find that your code isn't being analyzed, check to see if <tt>CC</tt> is
297hardcoded. If this is the case, you can hardcode it instead to the <b>full
298path</b> to <tt>ccc-analyzer</tt>.</p>
299
300<p>When applicable, you can also run <tt>./configure</tt> for a project through
301<tt>scan-build</tt> so that configure sets up the location of <tt>CC</tt> based
302on the environment passed in from <tt>scan-build</tt>:
303
304<pre>
305 $ scan-build <b>./configure</b>
306</pre>
307
308<p><tt>scan-build</tt> has special knowledge about <tt>configure</tt>, so it in
309most cases will not actually analyze the configure tests run by
310<tt>configure</tt>.</p>
311
312<p>Under the hood, <tt>ccc-analyzer</tt> directly invokes <tt>gcc</tt> to
313compile the actual code in addition to running the analyzer (which occurs by it
314calling <tt>clang</tt>). <tt>ccc-analyzer</tt> tries to correctly forward all
315the arguments over to <tt>gcc</tt>, but this may not work perfectly (please
316report bugs of this kind).
317
Ted Kremenekbc757862008-06-12 18:39:02 +0000318<h2 id="filingbugs">Filing Bugs</h2>
Ted Kremenekce2e3322008-04-25 18:44:36 +0000319
Ted Kremenekbc757862008-06-12 18:39:02 +0000320<p>We encourage users to file bug reports for any problems that they
321encounter.</p>
Ted Kremenekbe6e5162008-06-11 05:25:12 +0000322
Ted Kremenekbc757862008-06-12 18:39:02 +0000323<h3>Outside Apple</h3>
Ted Kremenekbe6e5162008-06-11 05:25:12 +0000324
Ted Kremenekbc757862008-06-12 18:39:02 +0000325<p>Please <a href="http://llvm.org/bugs/enter_bug.cgi?product=clang">file
326bugs</a> (against Clang) in LLVM's Bugzilla database.</p>
327
328<h3>Apple-internal Users</h3>
329
330<p>Please file bugs in Radar against the <b>llvm - clang</b> component.</p>
331
Ted Kremenekbe6e5162008-06-11 05:25:12 +0000332
Ted Kremenek20c5f142008-06-11 05:26:52 +0000333</div>
Ted Kremenekbe6e5162008-06-11 05:25:12 +0000334</body>
335</html>