192652048STed Kremenek<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" 292652048STed Kremenek "http://www.w3.org/TR/html4/strict.dtd"> 392652048STed Kremenek<html> 492652048STed Kremenek<head> 5a2d5f0c7SJordan Rose <title>Running the analyzer within Xcode</title> 6eaa262b5SBenjamin Kramer <link type="text/css" rel="stylesheet" href="content.css"> 7eaa262b5SBenjamin Kramer <link type="text/css" rel="stylesheet" href="menu.css"> 892652048STed Kremenek <script type="text/javascript" src="scripts/menu.js"></script> 992652048STed Kremenek</head> 1092652048STed Kremenek<body> 1192652048STed Kremenek 1292652048STed Kremenek<div id="page"> 1392652048STed Kremenek<!--#include virtual="menu.html.incl"--> 1492652048STed Kremenek<div id="content"> 1592652048STed Kremenek 16a2d5f0c7SJordan Rose<h1>Running the analyzer within Xcode</h1> 1792652048STed Kremenek 1892652048STed Kremenek<table style="margin-top:0px" width="100%" border="0" cellpadding="0px" cellspacing="0"> 1992652048STed Kremenek<tr><td> 2092652048STed Kremenek 2192652048STed Kremenek<h3>What is it?</h3> 22a2d5f0c7SJordan Rose 23a2d5f0c7SJordan Rose<p>Since Xcode 3.2, users have been able to run the Clang Static Analyzer 24a2d5f0c7SJordan Rose<a 25*76a74c56SJordan Rosehref="https://developer.apple.com/library/ios/recipes/xcode_help-source_editor/chapters/Analyze.html#//apple_ref/doc/uid/TP40009975-CH4-SW1">directly 2692652048STed Kremenekwithin Xcode</a>.</p> 2792652048STed Kremenek 2892652048STed Kremenek<p>It integrates directly with the Xcode build system and 2992652048STed Kremenekpresents analysis results directly within Xcode's editor.</p> 3092652048STed Kremenek 3192652048STed Kremenek<h3>Can I use the open source analyzer builds with Xcode?</h3> 3292652048STed Kremenek 3392652048STed Kremenek<p><b>Yes</b>. Instructions are included below.</p> 3492652048STed Kremenek 3592652048STed Kremenek</td> 36eaa262b5SBenjamin Kramer<td style="padding-left:10px; text-align:center"> 37eaa262b5SBenjamin Kramer <a href="images/analyzer_xcode.png"><img src="images/analyzer_xcode.png" width="620px" alt="analyzer in xcode"></a> 38eaa262b5SBenjamin Kramer<br><b>Viewing static analyzer results in Xcode</b> 3992652048STed Kremenek</td></tr></table> 4092652048STed Kremenek 4192652048STed Kremenek<h3>Key features:</h3> 4292652048STed Kremenek<ul> 4392652048STed Kremenek <li><b>Integrated workflow:</b> Results are integrated within Xcode. There is 4492652048STed Kremenek no experience of using a separate tool, and activating the analyzer requires a 4592652048STed Kremenek single keystroke or mouse click.</li> 4692652048STed Kremenek <li><b>Transparency:</b> Works effortlessly with Xcode projects (including iPhone projects). 4792652048STed Kremenek <li><b>Cons:</b> Doesn't work well with non-Xcode projects. For those, 48a2d5f0c7SJordan Rose consider using <a href="scan-build.html"><b>scan-build</b></a>. 4992652048STed Kremenek</ul> 503efee522STed Kremenek 5192652048STed Kremenek 5292652048STed Kremenek<h2>Getting Started</h2> 5392652048STed Kremenek 54a2d5f0c7SJordan Rose<p>Xcode is available as a free download from Apple on the <a 55a2d5f0c7SJordan Rosehref="https://itunes.apple.com/us/app/xcode/id497799835?mt=12">Mac 56a2d5f0c7SJordan RoseApp Store</a>, with <a 57*76a74c56SJordan Rosehref="https://developer.apple.com/library/ios/recipes/xcode_help-source_editor/chapters/Analyze.html#//apple_ref/doc/uid/TP40009975-CH4-SW1">instructions 58a2d5f0c7SJordan Roseavailable</a> for using the analyzer.</p> 5992652048STed Kremenek 60a2d5f0c7SJordan Rose<h2>Using open source analyzer builds with Xcode</h2> 6192652048STed Kremenek 6292652048STed Kremenek<p>By default, Xcode uses the version of <tt>clang</tt> that came bundled with 63a2d5f0c7SJordan Roseit to analyze your code. It is possible to change Xcode's behavior to use an 64a2d5f0c7SJordan Rosealternate version of <tt>clang</tt> for this purpose while continuing to use 65a2d5f0c7SJordan Rosethe <tt>clang</tt> that came with Xcode for compiling projects.</p> 6692652048STed Kremenek 6792652048STed Kremenek<h3>Why try open source builds?</h3> 6892652048STed Kremenek 6992652048STed Kremenek<p>The advantage of using open source analyzer builds (provided on this website) 7092652048STed Kremenekis that they are often newer than the analyzer provided with Xcode, and thus can 7192652048STed Kremenekcontain bug fixes, new checks, or simply better analysis.</p> 7292652048STed Kremenek 7392652048STed Kremenek<p>On the other hand, new checks can be experimental, with results of variable 7492652048STed Kremenekquality. Users are encouraged to <a href="filing_bugs.html">file bug reports</a> 7592652048STed Kremenek(for any version of the analyzer) where they encounter false positives or other 7692652048STed Kremenekissues.</p> 7792652048STed Kremenek 7892652048STed Kremenek<h3>set-xcode-analyzer</h3> 7992652048STed Kremenek 8092652048STed Kremenek<p>Starting with analyzer build checker-234, analyzer builds contain a command 8192652048STed Kremenekline utility called <tt>set-xcode-analyzer</tt> that allows users to change what 82a2d5f0c7SJordan Rosecopy of <tt>clang</tt> that Xcode uses for analysis:</p> 8392652048STed Kremenek 8492652048STed Kremenek<pre class="code_example"> 8592652048STed Kremenek$ <b>set-xcode-analyzer -h</b> 8692652048STed KremenekUsage: set-xcode-analyzer [options] 8792652048STed Kremenek 8892652048STed KremenekOptions: 8992652048STed Kremenek -h, --help show this help message and exit 9092652048STed Kremenek --use-checker-build=PATH 9192652048STed Kremenek Use the Clang located at the provided absolute path, 9292652048STed Kremenek e.g. /Users/foo/checker-1 9392652048STed Kremenek --use-xcode-clang Use the Clang bundled with Xcode 9492652048STed Kremenek</pre> 9592652048STed Kremenek 9692652048STed Kremenek<p>Operationally, <b>set-xcode-analyzer</b> edits Xcode's configuration files 97a2d5f0c7SJordan Roseto point it to use the version of <tt>clang</tt> you specify for static 98a2d5f0c7SJordan Roseanalysis. Within this model it provides you two basic modes:</p> 9992652048STed Kremenek 10092652048STed Kremenek<ul> 10192652048STed Kremenek <li><b>--use-xcode-clang</b>: Switch Xcode (back) to using the <tt>clang</tt> that came bundled with it for static analysis.</li> 10292652048STed Kremenek <li><b>--use-checker-build</b>: Switch Xcode to using the <tt>clang</tt> provided by the specified analyzer build.</li> 10392652048STed Kremenek</ul> 10492652048STed Kremenek 10547537b61STed Kremenek<h4>Things to keep in mind</h4> 10647537b61STed Kremenek 10747537b61STed Kremenek<ul> 108a2d5f0c7SJordan Rose <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 109a2d5f0c7SJordan Rose<b><tt>sudo</tt></b> in order to have write privileges to modify the Xcode 110a2d5f0c7SJordan Roseconfiguration files.</li> 11147537b61STed Kremenek</ul> 11247537b61STed Kremenek 11392652048STed Kremenek<h4>Examples</h4> 11492652048STed Kremenek 115a2d5f0c7SJordan Rose<p><b>Example 1</b>: Telling Xcode to use checker-235:</p> 11692652048STed Kremenek 11792652048STed Kremenek<pre class="code_example"> 11892652048STed Kremenek$ pwd 11992652048STed Kremenek/tmp 12092652048STed Kremenek$ tar xjf checker-235.tar.bz2 12147537b61STed Kremenek$ sudo checker-235/set-xcode-analyzer --use-checker-build=/tmp/checker-235 12292652048STed Kremenek</pre> 12392652048STed Kremenek 124353394f7STed Kremenek<p>Note that you typically won't install an analyzer build in <tt>/tmp</tt>, but 125353394f7STed Kremenekthe point of this example is that <tt>set-xcode-analyzer</tt> just wants a full 126353394f7STed Kremenekpath to an untarred analyzer build.</p> 127353394f7STed Kremenek 128353394f7STed Kremenek<p><b>Example 2</b>: Telling Xcode to use a very specific version of <tt>clang</tt>:</p> 12992652048STed Kremenek 13092652048STed Kremenek<pre class="code_example"> 13147537b61STed Kremenek$ sudo set-xcode-analyzer --use-checker-build=~/mycrazyclangbuild/bin/clang 13292652048STed Kremenek</pre> 13392652048STed Kremenek 134353394f7STed Kremenek<p><b>Example 3</b>: Resetting Xcode to its default behavior:</p> 13592652048STed Kremenek 13692652048STed Kremenek<pre class="code_example"> 13747537b61STed Kremenek$ sudo set-xcode-analyzer --use-xcode-clang 13892652048STed Kremenek</pre> 13992652048STed Kremenek 14092652048STed Kremenek</div> 14192652048STed Kremenek</div> 14292652048STed Kremenek</body> 14392652048STed Kremenek</html> 144