blob: c264fedd6e24ac467ed2ebdbd54a4dffef4578c3 [file] [log] [blame]
Ted Kremenek92c23cb2008-06-17 06:38:07 +00001<html>
2<head>
3 <title>Information on using the Static Analyzer ("Clang Checker")</title>
4 <link type="text/css" rel="stylesheet" href="menu.css" />
5 <link type="text/css" rel="stylesheet" href="content.css" />
6 <style>
7 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>
23</head>
24<body>
25
26<!--#include virtual="menu.html.incl"-->
27
28<div id="content">
29
30<h1>Information on using the Static Analyzer</h1>
31
32<h2 id="Obtaining">Obtaining the Analyzer</h2>
33
34<p> Using the analyzer involves executing <tt>scan-build</tt> (see <a
35href="#BasicUsage">Basic Usage</a>). <tt>scan-build</tt> will first look for a
36<tt>clang</tt> executable in the same directory as <tt>scan-build</tt>, and then
37search your path.</p>
38
39<p>If one is using the analyzer directly from the Clang sources, it suffices to
40just directly execute <tt>scan-build</tt> in the <tt>utils</tt> directory. No
41other special installation is needed.</p>
42
43<h3>Packaged Builds (Mac OS X)</h3>
44
45<p>Semi-regular pre-built binaries of the analyzer are available on Mac OS X
46(10.5).</p>
47
48<p>The latest build is: <b><a
49href="http://keeda.stanford.edu/~kremenek/checker/checker-37.tar.gz">checker-37.tar.gz</a></b> (built June 16, 2008)</p>
50
51<p><b>Note: This fixes a serious bug in checker-36 where the analyzer would
52never be run, thus finding no bugs.</b></p>
53
54Packaged builds for other platforms may eventually be provided, but as the tool
55is in its early stages we are not actively promoting releases yet. If you wish
56to help contribute regular builds of the analyzer on other platforms, please
57email the <a href="http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev">Clang
58Developers' mailing list</a>.</p>
59
60<p>Packaged builds of the analyzer expand to the following files:</p>
61
62<table id="package">
63<thead><tr><td>File</td><td>Purpose</td></tr></thead>
64<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>
65<tr><td><tt>ccc-analyzer</tt></td><td>GCC interceptor (called by scan-build)</td></tr>
66<tr><td><tt>clang</tt></td><td>Static Analyzer (called by ccc-analyzer)</td><tr>
67<tr><td><tt>sorttable.js</tt></td><td>JavaScript used for displaying error reports</td></tr>
68</table>
69
70<h3 id="OtherPlatforms">Other Platforms (Building the Analyzer from Source)</h3>
71
72<p>Packaged builds simply consist of a few files from the Clang source tree,
73meaning that <b>anyone</b> who can build Clang can use the static analyzer.
74Please see the <a href="get_started.html">Getting Started</a> page for more
75details on downloading and compiling Clang.</p>
76
77<p>All files used by the analyzer (and included in packaged builds; <a
78href="#package">see above</a>) other than a compiled <tt>clang</tt> executable
79are found in the <tt>utils</tt> subdirectory in the Clang tree.</p>
80
81<h2 id="BasicUsage">Basic Usage</h2>
82
83<p>The analyzer is executed from the command-line. To run the analyzer, you will
84use <tt>scan-build</tt> to analyze the source files compiled by <tt>gcc</tt>
85during a project build.</p>
86
87<p>For example, to analyze the files compiled under a build:</p>
88
89<pre>
90 $ <b>scan-build</b> make
91 $ <b>scan-build</b> xcodebuild
92</pre>
93
94<p> In the first case <tt>scan-build</tt> analyzes the code of a project built
95with <tt>make</tt>, andin the second case <tt>scan-build</tt> analyzes a project
96built using <tt>xcodebuild</tt>. In general, the format is: </p>
97
98<pre>
99 $ <b>scan-build</b> <i>[scan-build options]</i> <b>&lt;command&gt;</b> <i>[command options]</i>
100</pre>
101
102<p> Operationally, <tt>scan-build</tt> literally runs <command> with all of the
103subsequent options passed to it. For example</p>
104
105<pre>
106 $ scan-build make <b>-j4</b>
107</pre>
108
109<p>In this example, <tt>scan-build</tt> makes no effort to interpret the options
110after the build command (in this case, <tt>make</tt>); it just passes them
111through. In general, <tt>scan-build</tt> should support parallel builds, but
112<b>not distributed builds</b>. Similarly, you can use <tt>scan-build</tt> to
113analyze specific files:
114
115<pre>
116 $ scan-build gcc -c <b>t1.c t2.c</b>
117</pre>
118
119<p>
120This example causes the files <tt>t1.c</tt> and <tt>t2.c</tt> to be analyzed.
121</p>
122
123<h3>Other Options</h3>
124
125<p>
126As mentioned above, extra options can be passed to <tt>scan-build</tt>. These
127options prefix the build command. For example:</p>
128
129<pre>
130 $ scan-build <b>-k -V</b> make
131 $ scan-build <b>-k -V</b> xcodebuild
132</pre>
133
134<p>Here are a complete list of options:</p>
135
136<table>
137 <thead><tr><td>Option</td><td>Description</td></tr></thead>
138
139 <tr><td><b>-a</b></td>
140 <td>The analysis to run. The default analysis is <i>checker-cfref</i>. Valid options are: <i>checker-cfref</i>, <i>fsyntax-only</i>.
141 These translate into options passed down to the <tt>clang</tt> executable, and currently this option is mainly used for debugging.</td></tr>
142
143 <tr><td><b>-o</b></td><td>Target directory for HTML report files. Subdirectories will be
144 created as needed to represent separate "runs" of the analyzer. If this option
145is not specified, a directory is created in <tt>/tmp</tt> to store the
146reports.</td><tr>
147
148 <tr><td><b>-h</b><br><i><nobr>(or no arguments)</nobr></i></td><td>Display <tt>scan-build</tt> options.</td></tr>
149
150 <tr><td><b>-k</b><br><nobr><b>--keep-going</b></nobr></td><td>Add a "keep on going" option to the
151 specified build command. <p>This option currently supports <tt>make</tt> and
152 <tt>xcodebuild</tt>.</p> <p>This is a convenience option; one can specify this
153 behavior directly using build options.</p></td></tr>
154
155 <tr><td><b>-v<b></td><td>Verbose output from scan-build and the analyzer. <b>A second and third
156 "-v" increases verbosity</b>, and is useful for filing bug reports against the analyzer.</td></tr>
157
158 <tr><td><b>-V</b></td><td>View analysis results in a web browser when the build command completes.</td></tr>
159</table>
160
161<p>These options can also be viewed by running <tt>scan-build</tt> with no
162arguments:</p>
163
164<pre>
165 $ <b>scan-build</b>
166
167 USAGE: scan-build [options] &lt;build command&gt; [build options]
168
169 OPTIONS:
170
171 -a - The analysis to run. The default is 'checker-cfref'.
172 Valid options are: 'checker-cfref', 'fsyntax-only'
173
174 -o - Target directory for HTML report files. Subdirectories
175 will be created as needed to represent separate "runs" of
176 the analyzer. If this option is not specified, a directory
177 is created in /tmp to store the reports.
178 <b>...</b>
179</pre>
180
181<h2 id="Output">Output of the Analyzer</h2>
182
183<p>
184The output of the analyzer is a set of HTML files, each one which represents a
185separate bug report. A single <tt>index.html</tt> file is generated for
186surveying all of the bugs. You can then just open <tt>index.html</tt> in a web
187browser to view the bug reports.
188</p>
189
190<p>
191Where the HTML files are generated is specified with a <b>-o</b> option to
192<tt>scan-build</tt>. If <b>-o</b> isn't specified, a directory in <tt>/tmp</tt>
193is created to store the files (<tt>scan-build</tt> will print a message telling
194you where they are). If you want to view the reports immediately after the build
195completes, pass <b>-V</b> to <tt>scan-build</tt>.
196</p>
197
198
199<h2 id="RecommendedUsageGuidelines">Recommended Usage Guidelines</h2>
200
201Here are a few recommendations with running the analyzer:
202
203<h3>Always Analyze a Project in its "Debug" Configuration</h3>
204
205<p>Most projects can be built in a "debug" mode that enables assertions.
206Assertions are picked up by the static analyzer to prune infeasible paths, which
207in some cases can greatly reduce the number of false positives (bogus error
208reports) emitted by the tool.</p>
209
210<h3>Pass -k to scan-build</h3>
211
212<p>While <tt>ccc-analyzer</tt> invokes <tt>gcc</tt> to compile code, any
213problems in correctly forwarding arguments to <tt>gcc</tt> may result in a build
214failure. Passing <b>-k</b> to <tt>scan-build</tt> potentially allows you to
215analyze other code in a project for which this problem doesn't occur.</p>
216
217<p> Also, it is useful to analyze a project even if not all of the source files
218are compilable. This is great when using <tt>scan-build</tt> as part of your
219compile-debug cycle.</p>
220
221<h3>Use Verbose Output when Debugging scan-build</h3>
222
223<p><tt>scan-build</tt> takes a <b>-v</b> option to emit verbose output about
224what it's doing; two <b>-v</b> options emit more information. Redirecting the
225output of <tt>scan-build</tt> to a text file (make sure to redirect standard
226error) is useful for filing bug reports against <tt>scan-build</tt> or the
227analyzer, as we can see the exact options (and files) passed to the analyzer.
228For more comprehendible logs, don't perform a parallel build.</p>
229
230<h2 id="Debugging">Debugging the Analyzer</h2>
231
232<p>This section provides information on debugging the analyzer, and troubleshooting
233it when you have problems analyzing a particular project.</p>
234
235<h3>How it Works</h3>
236
237<p>To analyze a project, <tt>scan-build</tt> simply sets the environment variable
238<tt>CC</tt> to the full path to <tt>ccc-analyzer</tt>. It also sets a few other
239environment variables to communicate to <tt>ccc-analyzer</tt> where to dump HTML
240report files.</p>
241
242<p>Some Makefiles (or equivalent project files) hardcode the compiler; for such
243projects simply overriding <tt>CC</tt> won't cause <tt>ccc-analyzer</tt> to be
244called. This will cause the compiled code <b>to not be analyzed.</b></p> If you
245find that your code isn't being analyzed, check to see if <tt>CC</tt> is
246hardcoded. If this is the case, you can hardcode it instead to the <b>full
247path</b> to <tt>ccc-analyzer</tt>.</p>
248
249<p>When applicable, you can also run <tt>./configure</tt> for a project through
250<tt>scan-build</tt> so that configure sets up the location of <tt>CC</tt> based
251on the environment passed in from <tt>scan-build</tt>:
252
253<pre>
254 $ scan-build <b>./configure</b>
255</pre>
256
257<p><tt>scan-build</tt> has special knowledge about <tt>configure</tt>, so it in
258most cases will not actually analyze the configure tests run by
259<tt>configure</tt>.</p>
260
261<p>Under the hood, <tt>ccc-analyzer</tt> directly invokes <tt>gcc</tt> to
262compile the actual code in addition to running the analyzer (which occurs by it
263calling <tt>clang</tt>). <tt>ccc-analyzer</tt> tries to correctly forward all
264the arguments over to <tt>gcc</tt>, but this may not work perfectly (please
265report bugs of this kind).
266
267<h2 id="filingbugs">Filing Bugs</h2>
268
269<p>We encourage users to file bug reports for any problems that they
270encounter.</p>
271
272<h3>Outside Apple</h3>
273
274<p>Please <a href="http://llvm.org/bugs/enter_bug.cgi?product=clang">file
275bugs</a> (against Clang) in LLVM's Bugzilla database.</p>
276
277<h3>Apple-internal Users</h3>
278
279<p>Please file bugs in Radar against the <b>llvm - clang</b> component.</p>
280
281</div>
282</body>
283</html>
284