blob: ac75a04754708e5465df85941418cbcf66f30518 [file] [log] [blame]
Ted Kremenek0f399962010-02-19 08:14:02 +00001<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
2 "http://www.w3.org/TR/html4/strict.dtd">
3<html>
4<head>
5 <title>Build and Analyze: running the analyzer within Xcode</title>
Benjamin Kramer665a8dc2012-01-15 15:26:07 +00006 <link type="text/css" rel="stylesheet" href="content.css">
7 <link type="text/css" rel="stylesheet" href="menu.css">
Ted Kremenek0f399962010-02-19 08:14:02 +00008 <script type="text/javascript" src="scripts/menu.js"></script>
9 <script type="text/javascript" src="scripts/dbtree.js"></script>
10</head>
11<body>
12
13<div id="page">
14<!--#include virtual="menu.html.incl"-->
15<div id="content">
16
17<h1>Build and Analyze: running the analyzer within Xcode</h1>
18
19<table style="margin-top:0px" width="100%" border="0" cellpadding="0px" cellspacing="0">
20<tr><td>
21
22<h3>What is it?</h3>
23<p><i>Build and Analyze</i> is an Xcode feature (introduced in Xcode 3.2) that
24allows users to run the Clang Static Analyzer <a
25href="http://developer.apple.com/mac/library/featuredarticles/StaticAnalysis/index.html">directly
26within Xcode</a>.</p>
27
28<p>It integrates directly with the Xcode build system and
29presents analysis results directly within Xcode's editor.</p>
30
31<h3>Can I use the open source analyzer builds with Xcode?</h3>
32
33<p><b>Yes</b>. Instructions are included below.</p>
34
35</td>
Benjamin Kramer665a8dc2012-01-15 15:26:07 +000036<td style="padding-left:10px; text-align:center">
37 <a href="images/analyzer_xcode.png"><img src="images/analyzer_xcode.png" width="620px" alt="analyzer in xcode"></a>
38<br><b>Viewing static analyzer results in Xcode</b>
Ted Kremenek0f399962010-02-19 08:14:02 +000039</td></tr></table>
40
Ted Kremenek0f399962010-02-19 08:14:02 +000041<h3>Key features:</h3>
42<ul>
43 <li><b>Integrated workflow:</b> Results are integrated within Xcode. There is
44 no experience of using a separate tool, and activating the analyzer requires a
45 single keystroke or mouse click.</li>
46 <li><b>Transparency:</b> Works effortlessly with Xcode projects (including iPhone projects).
47 <li><b>Cons:</b> Doesn't work well with non-Xcode projects. For those,
Ted Kremenek4d9d49a2010-02-19 17:33:30 +000048 consider using <a href="/scan-build.html"><b>scan-build</b></a>.
Ted Kremenek0f399962010-02-19 08:14:02 +000049</ul>
Ted Kremenek4d9d49a2010-02-19 17:33:30 +000050
Ted Kremenek0f399962010-02-19 08:14:02 +000051
52<h2>Getting Started</h2>
53
54<p>Xcode 3.2 is available as a free download from Apple, with <a
55href="http://developer.apple.com/mac/library/featuredarticles/StaticAnalysis/index.html">instructions available</a>
56for using <i>Build and Analyze</i>.</p>
57
58<h2>Using open source analyzer builds with <i>Build and Analyze</i></h2>
59
60<p>By default, Xcode uses the version of <tt>clang</tt> that came bundled with
61it to provide the results for <i>Build and Analyze</i>. It is possible to change
62Xcode's behavior to use an alternate version of <tt>clang</tt> for this purpose
63while continuing to use the <tt>clang</tt> that came with Xcode for compiling
64projects.</p>
65
66<h3>Why try open source builds?</h3>
67
68<p>The advantage of using open source analyzer builds (provided on this website)
69is that they are often newer than the analyzer provided with Xcode, and thus can
70contain bug fixes, new checks, or simply better analysis.</p>
71
72<p>On the other hand, new checks can be experimental, with results of variable
73quality. Users are encouraged to <a href="filing_bugs.html">file bug reports</a>
74(for any version of the analyzer) where they encounter false positives or other
75issues.</p>
76
77<h3>set-xcode-analyzer</h3>
78
79<p>Starting with analyzer build checker-234, analyzer builds contain a command
80line utility called <tt>set-xcode-analyzer</tt> that allows users to change what
81copy of <tt>clang</tt> that Xcode uses for <i>Build and Analyze</i>:</p>
82
83<pre class="code_example">
84$ <b>set-xcode-analyzer -h</b>
85Usage: set-xcode-analyzer [options]
86
87Options:
88 -h, --help show this help message and exit
89 --use-checker-build=PATH
90 Use the Clang located at the provided absolute path,
91 e.g. /Users/foo/checker-1
92 --use-xcode-clang Use the Clang bundled with Xcode
93</pre>
94
95<p>Operationally, <b>set-xcode-analyzer</b> edits Xcode's configuration files
96(in <tt>/Developer</tt>) to point it to use the version of <tt>clang</tt> you
97specify for static analysis. Within this model it provides you two basic modes:</p>
98
99<ul>
100 <li><b>--use-xcode-clang</b>: Switch Xcode (back) to using the <tt>clang</tt> that came bundled with it for static analysis.</li>
101 <li><b>--use-checker-build</b>: Switch Xcode to using the <tt>clang</tt> provided by the specified analyzer build.</li>
102</ul>
103
Ted Kremenek4c551b62011-01-31 20:45:38 +0000104<h4>Things to keep in mind</h4>
105
106<ul>
107<li>You should quit Xcode prior to running <tt>set-xcode-analyzer</tt>.</li>
108<li>You will need to run <tt>set-xcode-analyzer</tt> under <b><tt>sudo</tt></b>
109 in order to have write privileges to modify the Xcode configuration files.</li>
110</ul>
111
Ted Kremenek0f399962010-02-19 08:14:02 +0000112<h4>Examples</h4>
113
Ted Kremenek8c0b9642010-02-19 08:24:37 +0000114<p><b>Example 1</b>: Telling Xcode to use checker-235 for <i>Build and Analyze</i>:</p>
Ted Kremenek0f399962010-02-19 08:14:02 +0000115
116<pre class="code_example">
117$ pwd
118/tmp
119$ tar xjf checker-235.tar.bz2
Ted Kremenek4c551b62011-01-31 20:45:38 +0000120$ sudo checker-235/set-xcode-analyzer --use-checker-build=/tmp/checker-235
Ted Kremenek0f399962010-02-19 08:14:02 +0000121</pre>
122
Ted Kremenek8c0b9642010-02-19 08:24:37 +0000123<p>Note that you typically won't install an analyzer build in <tt>/tmp</tt>, but
124the point of this example is that <tt>set-xcode-analyzer</tt> just wants a full
125path to an untarred analyzer build.</p>
126
127<p><b>Example 2</b>: Telling Xcode to use a very specific version of <tt>clang</tt>:</p>
Ted Kremenek0f399962010-02-19 08:14:02 +0000128
129<pre class="code_example">
Ted Kremenek4c551b62011-01-31 20:45:38 +0000130$ sudo set-xcode-analyzer --use-checker-build=~/mycrazyclangbuild/bin/clang
Ted Kremenek0f399962010-02-19 08:14:02 +0000131</pre>
132
Ted Kremenek8c0b9642010-02-19 08:24:37 +0000133<p><b>Example 3</b>: Resetting Xcode to its default behavior:</p>
Ted Kremenek0f399962010-02-19 08:14:02 +0000134
135<pre class="code_example">
Ted Kremenek4c551b62011-01-31 20:45:38 +0000136$ sudo set-xcode-analyzer --use-xcode-clang
Ted Kremenek0f399962010-02-19 08:14:02 +0000137</pre>
138
139</div>
140</div>
141</body>
142</html>
143