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