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