1 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" 2 "http://www.w3.org/TR/html4/strict.dtd"> 3 <html> 4 <head> 5 <title>scan-build: running the analyzer from the command line</title> 6 <link type="text/css" rel="stylesheet" href="content.css"> 7 <link type="text/css" rel="stylesheet" href="menu.css"> 8 <script type="text/javascript" src="scripts/menu.js"></script> 9 </head> 10 <body> 11 12 <div id="page"> 13 <!--#include virtual="menu.html.incl"--> 14 <div id="content"> 15 16 <h1>scan-build: running the analyzer from the command line</h1> 17 18 <table style="margin-top:0px" width="100%" cellpadding="0px" cellspacing="0"> 19 <tr><td> 20 21 <h3>What is it?</h3> 22 <p><b>scan-build</b> is a command line utility that enables a user to run the 23 static analyzer over their codebase as part of performing a regular build (from 24 the command line).</p> 25 26 <h3>How does it work?</h3> 27 <p>During a project build, as source files are compiled they are also analyzed 28 in tandem by the static analyzer.</p> 29 30 <p>Upon completion of the build, results are then presented to the user within a 31 web browser.</p> 32 33 <h3>Will it work with any build system?</h3> 34 <p><b>scan-build</b> has little or no knowledge about how you build your code. 35 It works by overriding the <tt>CC</tt> and <tt>CXX</tt> environment variables to 36 (hopefully) change your build to use a "fake" compiler instead of the 37 one that would normally build your project. This fake compiler executes either 38 <tt>clang</tt> or <tt>gcc</tt> (depending on the platform) to compile your 39 code and then executes the static analyzer to analyze your code.</p> 40 41 <p>This "poor man's interposition" works amazingly well in many cases 42 and falls down in others. Please consult the information on this page on making 43 the best use of <b>scan-build</b>, which includes getting it to work when the 44 aforementioned hack fails to work.</p> 45 46 </td> 47 <td style="padding-left:10px; text-align:center"> 48 <img src="images/scan_build_cmd.png" width="450px" alt="scan-build"><br> 49 <a href="images/analyzer_html.png"><img src="images/analyzer_html.png" width="450px" alt="analyzer in browser"></a> 50 <br><b>Viewing static analyzer results in a web browser</b> 51 </td></tr></table> 52 53 <h2>Contents</h2> 54 55 <ul> 56 <li><a href="#scanbuild">Getting Started</a> 57 <ul> 58 <li><a href="#scanbuild_basicusage">Basic Usage</a></li> 59 <li><a href="#scanbuild_forwindowsusers">For Windows Users</a></li> 60 <li><a href="#scanbuild_otheroptions">Other Options</a></li> 61 <li><a href="#scanbuild_output">Output of scan-build</a></li> 62 </ul> 63 </li> 64 <li><a href="#recommendedguidelines">Recommended Usage Guidelines</a> 65 <ul> 66 <li><a href="#recommended_debug">Always Analyze a Project in its "Debug" Configuration</a></li> 67 <li><a href="#recommended_verbose">Use Verbose Output when Debugging scan-build</a></li> 68 <li><a href="#recommended_autoconf">Run './configure' through scan-build</a></li> 69 </ul> 70 </li> 71 <li><a href="#iphone">Analyzing iPhone Projects</a></li> 72 </ul> 73 74 <h2 id="scanbuild">Getting Started</h2> 75 76 <p>The <tt>scan-build</tt> command can be used to analyze an entire project by 77 essentially interposing on a project's build process. This means that to run the 78 analyzer using <tt>scan-build</tt>, you will use <tt>scan-build</tt> to analyze 79 the source files compiled by <tt>gcc</tt>/<tt>clang</tt> during a project build. 80 This means that any files that are not compiled will also not be analyzed.</p> 81 82 <h3 id="scanbuild_basicusage">Basic Usage</h3> 83 84 <p>Basic usage of <tt>scan-build</tt> is designed to be simple: just place the 85 word "scan-build" in front of your build command:</p> 86 87 <pre class="code_example"> 88 $ <span class="code_highlight">scan-build</span> make 89 $ <span class="code_highlight">scan-build</span> xcodebuild 90 </pre> 91 92 <p>In the first case <tt>scan-build</tt> analyzes the code of a project built 93 with <tt>make</tt> and in the second case <tt>scan-build</tt> analyzes a project 94 built using <tt>xcodebuild</tt>.<p> 95 96 <p>Here is the general format for invoking <tt>scan-build</tt>:</p> 97 98 <pre class="code_example"> 99 $ <span class="code_highlight">scan-build</span> <i>[scan-build options]</i> <span class="code_highlight"><command></span> <i>[command options]</i> 100 </pre> 101 102 <p>Operationally, <tt>scan-build</tt> literally runs <command> with all of the 103 subsequent options passed to it. For example, one can pass <tt>-j4</tt> to 104 <tt>make</tt> get a parallel build over 4 cores:</p> 105 106 <pre class="code_example"> 107 $ scan-build make <span class="code_highlight">-j4</span> 108 </pre> 109 110 <p>In almost all cases, <tt>scan-build</tt> makes no effort to interpret the 111 options after the build command; it simply passes them through. In general, 112 <tt>scan-build</tt> should support parallel builds, but <b>not distributed 113 builds</b>.</p> 114 115 <p>It is also possible to use <tt>scan-build</tt> to analyze specific 116 files:</p> 117 118 <pre class="code_example"> 119 $ scan-build gcc -c <span class="code_highlight">t1.c t2.c</span> 120 </pre> 121 122 <p>This example causes the files <tt>t1.c</tt> and <tt>t2.c</tt> to be analyzed. 123 </p> 124 125 <h3 id="scanbuild_forwindowsusers">For Windows Users</h3> 126 127 <p>Windows users must have Perl installed to use scan-build. Currently scan-build 128 is known to work with the msys perl port.</p> 129 130 <p>scan-build.bat script allows you to launch scan-build in the same way as it described in the Basic Usage section above. 131 All you need to be able to invoke scan-build from an arbitrary location is to add the path to scan-build to your PATH environment variable.</p> 132 133 <h3 id="scanbuild_otheroptions">Other Options</h3> 134 135 <p>As mentioned above, extra options can be passed to <tt>scan-build</tt>. These 136 options prefix the build command. For example:</p> 137 138 <pre class="code_example"> 139 $ scan-build <span class="code_highlight">-k -V</span> make 140 $ scan-build <span class="code_highlight">-k -V</span> xcodebuild 141 </pre> 142 143 <p>Here is a subset of useful options:</p> 144 145 <table class="options"> 146 <colgroup><col class="option"><col class="description"></colgroup> 147 <thead><tr><td>Option</td><td>Description</td></tr></thead> 148 149 <tr><td><b>-o</b></td><td>Target directory for HTML report files. Subdirectories 150 will be created as needed to represent separate "runs" of the analyzer. If this 151 option is not specified, a directory is created in <tt>/tmp</tt> to store the 152 reports.</td></tr> 153 154 <tr><td><b>-h</b><br><i>(or no arguments)</i></td><td>Display all 155 <tt>scan-build</tt> options.</td></tr> 156 157 <tr><td><b>-k</b><br><b>--keep-going</b></td><td>Add a "keep on 158 going" option to the specified build command. <p>This option currently supports 159 <tt>make</tt> and <tt>xcodebuild</tt>.</p> <p>This is a convenience option; one 160 can specify this behavior directly using build options.</p></td></tr> 161 162 <tr><td><b>-v</b></td><td>Verbose output from scan-build and the analyzer. <b>A 163 second and third "-v" increases verbosity</b>, and is useful for filing bug 164 reports against the analyzer.</td></tr> 165 166 <tr><td><b>-V</b></td><td>View analysis results in a web browser when the build 167 command completes.</td></tr> 168 169 <tr><td><b>--use-analyzer Xcode</b><br><i>or</i><br> 170 <b>--use-analyzer [path to clang]</b></td><td><tt>scan-build</tt> uses the 171 'clang' executable relative to itself for static analysis. One can override this 172 behavior with this option by using the 'clang' packaged with Xcode (on OS X) or 173 from the PATH.</p></td></tr> </table> 174 175 <p>A complete list of options can be obtained by running <tt>scan-build</tt> 176 with no arguments.</p> 177 178 <h3 id="scanbuild_output">Output of scan-build</h3> 179 180 <p> 181 The output of scan-build is a set of HTML files, each one which represents a 182 separate bug report. A single <tt>index.html</tt> file is generated for 183 surveying all of the bugs. You can then just open <tt>index.html</tt> in a web 184 browser to view the bug reports. 185 </p> 186 187 <p> 188 Where the HTML files are generated is specified with a <b>-o</b> option to 189 <tt>scan-build</tt>. If <b>-o</b> isn't specified, a directory in <tt>/tmp</tt> 190 is created to store the files (<tt>scan-build</tt> will print a message telling 191 you where they are). If you want to view the reports immediately after the build 192 completes, pass <b>-V</b> to <tt>scan-build</tt>. 193 </p> 194 195 196 <h2 id="recommendedguidelines">Recommended Usage Guidelines</h2> 197 198 <p>This section describes a few recommendations with running the analyzer.</p> 199 200 <h3 id="recommended_debug">ALWAYS analyze a project in its "debug" configuration</h3> 201 202 <p>Most projects can be built in a "debug" mode that enables assertions. 203 Assertions are picked up by the static analyzer to prune infeasible paths, which 204 in some cases can greatly reduce the number of false positives (bogus error 205 reports) emitted by the tool.</p> 206 207 <h3 id="recommend_verbose">Use verbose output when debugging scan-build</h3> 208 209 <p><tt>scan-build</tt> takes a <b>-v</b> option to emit verbose output about 210 what it's doing; two <b>-v</b> options emit more information. Redirecting the 211 output of <tt>scan-build</tt> to a text file (make sure to redirect standard 212 error) is useful for filing bug reports against <tt>scan-build</tt> or the 213 analyzer, as we can see the exact options (and files) passed to the analyzer. 214 For more comprehensible logs, don't perform a parallel build.</p> 215 216 <h3 id="recommended_autoconf">Run './configure' through scan-build</h3> 217 218 <p>If an analyzed project uses an autoconf generated <tt>configure</tt> script, 219 you will probably need to run <tt>configure</tt> script through 220 <tt>scan-build</tt> in order to analyze the project.</p> 221 222 <p><b>Example</b></p> 223 224 <pre class="code_example"> 225 $ scan-build ./configure 226 $ scan-build make 227 </pre> 228 229 <p>The reason <tt>configure</tt> also needs to be run through 230 <tt>scan-build</tt> is because <tt>scan-build</tt> scans your source files by 231 <i>interposing</i> on the compiler. This interposition is currently done by 232 <tt>scan-build</tt> temporarily setting the environment variable <tt>CC</tt> to 233 <tt>ccc-analyzer</tt>. The program <tt>ccc-analyzer</tt> acts like a fake 234 compiler, forwarding its command line arguments over to the compiler to perform 235 regular compilation and <tt>clang</tt> to perform static analysis.</p> 236 237 <p>Running <tt>configure</tt> typically generates makefiles that have hardwired 238 paths to the compiler, and by running <tt>configure</tt> through 239 <tt>scan-build</tt> that path is set to <tt>ccc-analyzer</tt>.</p> 240 241 <!-- 242 <h2 id="Debugging">Debugging the Analyzer</h2> 243 244 <p>This section provides information on debugging the analyzer, and troubleshooting 245 it when you have problems analyzing a particular project.</p> 246 247 <h3>How it Works</h3> 248 249 <p>To analyze a project, <tt>scan-build</tt> simply sets the environment variable 250 <tt>CC</tt> to the full path to <tt>ccc-analyzer</tt>. It also sets a few other 251 environment variables to communicate to <tt>ccc-analyzer</tt> where to dump HTML 252 report files.</p> 253 254 <p>Some Makefiles (or equivalent project files) hardcode the compiler; for such 255 projects simply overriding <tt>CC</tt> won't cause <tt>ccc-analyzer</tt> to be 256 called. This will cause the compiled code <b>to not be analyzed.</b></p> If you 257 find that your code isn't being analyzed, check to see if <tt>CC</tt> is 258 hardcoded. If this is the case, you can hardcode it instead to the <b>full 259 path</b> to <tt>ccc-analyzer</tt>.</p> 260 261 <p>When applicable, you can also run <tt>./configure</tt> for a project through 262 <tt>scan-build</tt> so that configure sets up the location of <tt>CC</tt> based 263 on the environment passed in from <tt>scan-build</tt>: 264 265 <pre> 266 $ scan-build <b>./configure</b> 267 </pre> 268 269 <p><tt>scan-build</tt> has special knowledge about <tt>configure</tt>, so it in 270 most cases will not actually analyze the configure tests run by 271 <tt>configure</tt>.</p> 272 273 <p>Under the hood, <tt>ccc-analyzer</tt> directly invokes <tt>gcc</tt> to 274 compile the actual code in addition to running the analyzer (which occurs by it 275 calling <tt>clang</tt>). <tt>ccc-analyzer</tt> tries to correctly forward all 276 the arguments over to <tt>gcc</tt>, but this may not work perfectly (please 277 report bugs of this kind). 278 --> 279 280 <h2 id="iphone">Analyzing iPhone Projects</h2> 281 282 <p>Conceptually Xcode projects for iPhone applications are nearly the same as 283 their cousins for desktop applications. <b>scan-build</b> can analyze these 284 projects as well, but users often encounter problems with just building their 285 iPhone projects from the command line because there are a few extra preparative 286 steps they need to take (e.g., setup code signing).</p> 287 288 <h3>Recommendation: use "Build and Analyze"</h3> 289 290 <p>The absolute easiest way to analyze iPhone projects is to use the <a 291 href="http://developer.apple.com/mac/library/featuredarticles/StaticAnalysis/index.html"><i>Build 292 and Analyze</i> feature in Xcode 3.2</a> (which is based on the Clang Static 293 Analyzer). There a user can analyze their project with the click of a button 294 without most of the setup described later.</p> 295 296 <p><a href="/xcode.html">Instructions are available</a> on this 297 website on how to use open source builds of the analyzer as a replacement for 298 the one bundled with Xcode.</p> 299 300 <h3>Using scan-build directly</h3> 301 302 <p>If you wish to use <b>scan-build</b> with your iPhone project, keep the 303 following things in mind:</p> 304 305 <ul> 306 <li>Analyze your project in the <tt>Debug</tt> configuration, either by setting 307 this as your configuration with Xcode or by passing <tt>-configuration 308 Debug</tt> to <tt>xcodebuild</tt>.</li> 309 <li>Analyze your project using the <tt>Simulator</tt> as your base SDK. It is 310 possible to analyze your code when targeting the device, but this is much 311 easier to do when using Xcode's <i>Build and Analyze</i> feature.</li> 312 <li>Check that your code signing SDK is set to the simulator SDK as well, and make sure this option is set to <tt>Don't Code Sign</tt>.</li> 313 </ul> 314 315 <p>Note that you can most of this without actually modifying your project. For 316 example, if your application targets iPhoneOS 2.2, you could run 317 <b>scan-build</b> in the following manner from the command line:</p> 318 319 <pre class="code_example"> 320 $ scan-build xcodebuild -configuration Debug -sdk iphonesimulator2.2 321 </pre> 322 323 Alternatively, if your application targets iPhoneOS 3.0: 324 325 <pre class="code_example"> 326 $ scan-build xcodebuild -configuration Debug -sdk iphonesimulator3.0 327 </pre> 328 329 <h3>Gotcha: using the right compiler</h3> 330 331 <p>Recall that <b>scan-build</b> analyzes your project by using a compiler to 332 compile the project and <tt>clang</tt> to analyze your project. The script uses 333 simple heuristics to determine which compiler should be used (it defaults to 334 <tt>clang</tt> on Darwin and <tt>gcc</tt> on other platforms). When analyzing 335 iPhone projects, <b>scan-build</b> may pick the wrong compiler than the one 336 Xcode would use to build your project. For example, this could be because 337 multiple versions of a compiler may be installed on your system, especially if 338 you are developing for the iPhone.</p> 339 340 <p>When compiling your application to run on the simulator, it is important that <b>scan-build</b> 341 finds the correct version of <tt>gcc/clang</tt>. Otherwise, you may see strange build 342 errors that only happen when you run <tt>scan-build</tt>. 343 344 <p><b>scan-build</b> provides the <tt>--use-cc</tt> and <tt>--use-c++</tt> 345 options to hardwire which compiler scan-build should use for building your code. 346 Note that although you are chiefly interested in analyzing your project, keep in 347 mind that running the analyzer is intimately tied to the build, and not being 348 able to compile your code means it won't get fully analyzed (if at all).</p> 349 350 <p>If you aren't certain which compiler Xcode uses to build your project, try 351 just running <tt>xcodebuild</tt> (without <b>scan-build</b>). You should see the 352 full path to the compiler that Xcode is using, and use that as an argument to 353 <tt>--use-cc</tt>.</p> 354 355 </div> 356 </div> 357 </body> 358 </html> 359 360