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