1*67e74705SXin Li<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" 2*67e74705SXin Li "http://www.w3.org/TR/html4/strict.dtd"> 3*67e74705SXin Li<html> 4*67e74705SXin Li<head> 5*67e74705SXin Li <title>Running the analyzer within Xcode</title> 6*67e74705SXin Li <link type="text/css" rel="stylesheet" href="content.css"> 7*67e74705SXin Li <link type="text/css" rel="stylesheet" href="menu.css"> 8*67e74705SXin Li <script type="text/javascript" src="scripts/menu.js"></script> 9*67e74705SXin Li</head> 10*67e74705SXin Li<body> 11*67e74705SXin Li 12*67e74705SXin Li<div id="page"> 13*67e74705SXin Li<!--#include virtual="menu.html.incl"--> 14*67e74705SXin Li<div id="content"> 15*67e74705SXin Li 16*67e74705SXin Li<h1>Running the analyzer within Xcode</h1> 17*67e74705SXin Li 18*67e74705SXin Li<table style="margin-top:0px" width="100%" border="0" cellpadding="0px" cellspacing="0"> 19*67e74705SXin Li<tr><td> 20*67e74705SXin Li 21*67e74705SXin Li<h3>What is it?</h3> 22*67e74705SXin Li 23*67e74705SXin Li<p>Since Xcode 3.2, users have been able to run the Clang Static Analyzer 24*67e74705SXin Li<a 25*67e74705SXin Lihref="https://developer.apple.com/library/ios/recipes/xcode_help-source_editor/chapters/Analyze.html#//apple_ref/doc/uid/TP40009975-CH4-SW1">directly 26*67e74705SXin Liwithin Xcode</a>.</p> 27*67e74705SXin Li 28*67e74705SXin Li<p>It integrates directly with the Xcode build system and 29*67e74705SXin Lipresents analysis results directly within Xcode's editor.</p> 30*67e74705SXin Li 31*67e74705SXin Li<h3>Can I use the open source analyzer builds with Xcode?</h3> 32*67e74705SXin Li 33*67e74705SXin Li<p><b>Yes</b>. Instructions are included below.</p> 34*67e74705SXin Li 35*67e74705SXin Li</td> 36*67e74705SXin Li<td style="padding-left:10px; text-align:center"> 37*67e74705SXin Li <a href="images/analyzer_xcode.png"><img src="images/analyzer_xcode.png" width="620px" alt="analyzer in xcode"></a> 38*67e74705SXin Li<br><b>Viewing static analyzer results in Xcode</b> 39*67e74705SXin Li</td></tr></table> 40*67e74705SXin Li 41*67e74705SXin Li<h3>Key features:</h3> 42*67e74705SXin Li<ul> 43*67e74705SXin Li <li><b>Integrated workflow:</b> Results are integrated within Xcode. There is 44*67e74705SXin Li no experience of using a separate tool, and activating the analyzer requires a 45*67e74705SXin Li single keystroke or mouse click.</li> 46*67e74705SXin Li <li><b>Transparency:</b> Works effortlessly with Xcode projects (including iPhone projects). 47*67e74705SXin Li <li><b>Cons:</b> Doesn't work well with non-Xcode projects. For those, 48*67e74705SXin Li consider using <a href="scan-build.html"><b>scan-build</b></a>. 49*67e74705SXin Li</ul> 50*67e74705SXin Li 51*67e74705SXin Li 52*67e74705SXin Li<h2>Getting Started</h2> 53*67e74705SXin Li 54*67e74705SXin Li<p>Xcode is available as a free download from Apple on the <a 55*67e74705SXin Lihref="https://itunes.apple.com/us/app/xcode/id497799835?mt=12">Mac 56*67e74705SXin LiApp Store</a>, with <a 57*67e74705SXin Lihref="https://developer.apple.com/library/ios/recipes/xcode_help-source_editor/chapters/Analyze.html#//apple_ref/doc/uid/TP40009975-CH4-SW1">instructions 58*67e74705SXin Liavailable</a> for using the analyzer.</p> 59*67e74705SXin Li 60*67e74705SXin Li<h2>Using open source analyzer builds with Xcode</h2> 61*67e74705SXin Li 62*67e74705SXin Li<p>By default, Xcode uses the version of <tt>clang</tt> that came bundled with 63*67e74705SXin Liit to analyze your code. It is possible to change Xcode's behavior to use an 64*67e74705SXin Lialternate version of <tt>clang</tt> for this purpose while continuing to use 65*67e74705SXin Lithe <tt>clang</tt> that came with Xcode for compiling projects.</p> 66*67e74705SXin Li 67*67e74705SXin Li<h3>Why try open source builds?</h3> 68*67e74705SXin Li 69*67e74705SXin Li<p>The advantage of using open source analyzer builds (provided on this website) 70*67e74705SXin Liis that they are often newer than the analyzer provided with Xcode, and thus can 71*67e74705SXin Licontain bug fixes, new checks, or simply better analysis.</p> 72*67e74705SXin Li 73*67e74705SXin Li<p>On the other hand, new checks can be experimental, with results of variable 74*67e74705SXin Liquality. Users are encouraged to <a href="filing_bugs.html">file bug reports</a> 75*67e74705SXin Li(for any version of the analyzer) where they encounter false positives or other 76*67e74705SXin Liissues.</p> 77*67e74705SXin Li 78*67e74705SXin Li<h3>set-xcode-analyzer</h3> 79*67e74705SXin Li 80*67e74705SXin Li<p>Starting with analyzer build checker-234, analyzer builds contain a command 81*67e74705SXin Liline utility called <tt>set-xcode-analyzer</tt> that allows users to change what 82*67e74705SXin Licopy of <tt>clang</tt> that Xcode uses for analysis:</p> 83*67e74705SXin Li 84*67e74705SXin Li<pre class="code_example"> 85*67e74705SXin Li$ <b>set-xcode-analyzer -h</b> 86*67e74705SXin LiUsage: set-xcode-analyzer [options] 87*67e74705SXin Li 88*67e74705SXin LiOptions: 89*67e74705SXin Li -h, --help show this help message and exit 90*67e74705SXin Li --use-checker-build=PATH 91*67e74705SXin Li Use the Clang located at the provided absolute path, 92*67e74705SXin Li e.g. /Users/foo/checker-1 93*67e74705SXin Li --use-xcode-clang Use the Clang bundled with Xcode 94*67e74705SXin Li</pre> 95*67e74705SXin Li 96*67e74705SXin Li<p>Operationally, <b>set-xcode-analyzer</b> edits Xcode's configuration files 97*67e74705SXin Lito point it to use the version of <tt>clang</tt> you specify for static 98*67e74705SXin Lianalysis. Within this model it provides you two basic modes:</p> 99*67e74705SXin Li 100*67e74705SXin Li<ul> 101*67e74705SXin Li <li><b>--use-xcode-clang</b>: Switch Xcode (back) to using the <tt>clang</tt> that came bundled with it for static analysis.</li> 102*67e74705SXin Li <li><b>--use-checker-build</b>: Switch Xcode to using the <tt>clang</tt> provided by the specified analyzer build.</li> 103*67e74705SXin Li</ul> 104*67e74705SXin Li 105*67e74705SXin Li<h4>Things to keep in mind</h4> 106*67e74705SXin Li 107*67e74705SXin Li<ul> 108*67e74705SXin Li <li>You should quit Xcode prior to running <tt>set-xcode-analyzer</tt>.</li> <li>You will need to run <tt>set-xcode-analyzer</tt> under 109*67e74705SXin Li<b><tt>sudo</tt></b> in order to have write privileges to modify the Xcode 110*67e74705SXin Liconfiguration files.</li> 111*67e74705SXin Li</ul> 112*67e74705SXin Li 113*67e74705SXin Li<h4>Examples</h4> 114*67e74705SXin Li 115*67e74705SXin Li<p><b>Example 1</b>: Telling Xcode to use checker-235:</p> 116*67e74705SXin Li 117*67e74705SXin Li<pre class="code_example"> 118*67e74705SXin Li$ pwd 119*67e74705SXin Li/tmp 120*67e74705SXin Li$ tar xjf checker-235.tar.bz2 121*67e74705SXin Li$ sudo checker-235/set-xcode-analyzer --use-checker-build=/tmp/checker-235 122*67e74705SXin Li</pre> 123*67e74705SXin Li 124*67e74705SXin Li<p>Note that you typically won't install an analyzer build in <tt>/tmp</tt>, but 125*67e74705SXin Lithe point of this example is that <tt>set-xcode-analyzer</tt> just wants a full 126*67e74705SXin Lipath to an untarred analyzer build.</p> 127*67e74705SXin Li 128*67e74705SXin Li<p><b>Example 2</b>: Telling Xcode to use a very specific version of <tt>clang</tt>:</p> 129*67e74705SXin Li 130*67e74705SXin Li<pre class="code_example"> 131*67e74705SXin Li$ sudo set-xcode-analyzer --use-checker-build=~/mycrazyclangbuild/bin/clang 132*67e74705SXin Li</pre> 133*67e74705SXin Li 134*67e74705SXin Li<p><b>Example 3</b>: Resetting Xcode to its default behavior:</p> 135*67e74705SXin Li 136*67e74705SXin Li<pre class="code_example"> 137*67e74705SXin Li$ sudo set-xcode-analyzer --use-xcode-clang 138*67e74705SXin Li</pre> 139*67e74705SXin Li 140*67e74705SXin Li</div> 141*67e74705SXin Li</div> 142*67e74705SXin Li</body> 143*67e74705SXin Li</html> 144*67e74705SXin Li 145