Ted Kremenek | 9265204 | 2010-02-19 08:14:02 +0000 | [diff] [blame] | 1 | <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" |
| 2 | "http://www.w3.org/TR/html4/strict.dtd"> |
| 3 | <html> |
| 4 | <head> |
Jordan Rose | a2d5f0c | 2013-01-08 19:29:37 +0000 | [diff] [blame] | 5 | <title>Running the analyzer within Xcode</title> |
Benjamin Kramer | eaa262b | 2012-01-15 15:26:07 +0000 | [diff] [blame] | 6 | <link type="text/css" rel="stylesheet" href="content.css"> |
| 7 | <link type="text/css" rel="stylesheet" href="menu.css"> |
Ted Kremenek | 9265204 | 2010-02-19 08:14:02 +0000 | [diff] [blame] | 8 | <script type="text/javascript" src="scripts/menu.js"></script> |
Ted Kremenek | 9265204 | 2010-02-19 08:14:02 +0000 | [diff] [blame] | 9 | </head> |
| 10 | <body> |
| 11 | |
| 12 | <div id="page"> |
| 13 | <!--#include virtual="menu.html.incl"--> |
| 14 | <div id="content"> |
| 15 | |
Jordan Rose | a2d5f0c | 2013-01-08 19:29:37 +0000 | [diff] [blame] | 16 | <h1>Running the analyzer within Xcode</h1> |
Ted Kremenek | 9265204 | 2010-02-19 08:14:02 +0000 | [diff] [blame] | 17 | |
| 18 | <table style="margin-top:0px" width="100%" border="0" cellpadding="0px" cellspacing="0"> |
| 19 | <tr><td> |
| 20 | |
| 21 | <h3>What is it?</h3> |
Jordan Rose | a2d5f0c | 2013-01-08 19:29:37 +0000 | [diff] [blame] | 22 | |
| 23 | <p>Since Xcode 3.2, users have been able to run the Clang Static Analyzer |
| 24 | <a |
Jordan Rose | 76a74c5 | 2015-09-18 16:12:16 +0000 | [diff] [blame] | 25 | href="https://developer.apple.com/library/ios/recipes/xcode_help-source_editor/chapters/Analyze.html#//apple_ref/doc/uid/TP40009975-CH4-SW1">directly |
Ted Kremenek | 9265204 | 2010-02-19 08:14:02 +0000 | [diff] [blame] | 26 | within Xcode</a>.</p> |
| 27 | |
| 28 | <p>It integrates directly with the Xcode build system and |
| 29 | presents 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 Kramer | eaa262b | 2012-01-15 15:26:07 +0000 | [diff] [blame] | 36 | <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 Kremenek | 9265204 | 2010-02-19 08:14:02 +0000 | [diff] [blame] | 39 | </td></tr></table> |
| 40 | |
Ted Kremenek | 9265204 | 2010-02-19 08:14:02 +0000 | [diff] [blame] | 41 | <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, |
Jordan Rose | a2d5f0c | 2013-01-08 19:29:37 +0000 | [diff] [blame] | 48 | consider using <a href="scan-build.html"><b>scan-build</b></a>. |
Ted Kremenek | 9265204 | 2010-02-19 08:14:02 +0000 | [diff] [blame] | 49 | </ul> |
Ted Kremenek | 3efee52 | 2010-02-19 17:33:30 +0000 | [diff] [blame] | 50 | |
Ted Kremenek | 9265204 | 2010-02-19 08:14:02 +0000 | [diff] [blame] | 51 | |
| 52 | <h2>Getting Started</h2> |
| 53 | |
Jordan Rose | a2d5f0c | 2013-01-08 19:29:37 +0000 | [diff] [blame] | 54 | <p>Xcode is available as a free download from Apple on the <a |
| 55 | href="https://itunes.apple.com/us/app/xcode/id497799835?mt=12">Mac |
| 56 | App Store</a>, with <a |
Jordan Rose | 76a74c5 | 2015-09-18 16:12:16 +0000 | [diff] [blame] | 57 | href="https://developer.apple.com/library/ios/recipes/xcode_help-source_editor/chapters/Analyze.html#//apple_ref/doc/uid/TP40009975-CH4-SW1">instructions |
Jordan Rose | a2d5f0c | 2013-01-08 19:29:37 +0000 | [diff] [blame] | 58 | available</a> for using the analyzer.</p> |
Ted Kremenek | 9265204 | 2010-02-19 08:14:02 +0000 | [diff] [blame] | 59 | |
Jordan Rose | a2d5f0c | 2013-01-08 19:29:37 +0000 | [diff] [blame] | 60 | <h2>Using open source analyzer builds with Xcode</h2> |
Ted Kremenek | 9265204 | 2010-02-19 08:14:02 +0000 | [diff] [blame] | 61 | |
| 62 | <p>By default, Xcode uses the version of <tt>clang</tt> that came bundled with |
Jordan Rose | a2d5f0c | 2013-01-08 19:29:37 +0000 | [diff] [blame] | 63 | it to analyze your code. It is possible to change Xcode's behavior to use an |
| 64 | alternate version of <tt>clang</tt> for this purpose while continuing to use |
| 65 | the <tt>clang</tt> that came with Xcode for compiling projects.</p> |
Ted Kremenek | 9265204 | 2010-02-19 08:14:02 +0000 | [diff] [blame] | 66 | |
| 67 | <h3>Why try open source builds?</h3> |
| 68 | |
| 69 | <p>The advantage of using open source analyzer builds (provided on this website) |
| 70 | is that they are often newer than the analyzer provided with Xcode, and thus can |
| 71 | contain bug fixes, new checks, or simply better analysis.</p> |
| 72 | |
| 73 | <p>On the other hand, new checks can be experimental, with results of variable |
| 74 | quality. Users are encouraged to <a href="filing_bugs.html">file bug reports</a> |
| 75 | (for any version of the analyzer) where they encounter false positives or other |
| 76 | issues.</p> |
| 77 | |
| 78 | <h3>set-xcode-analyzer</h3> |
| 79 | |
| 80 | <p>Starting with analyzer build checker-234, analyzer builds contain a command |
| 81 | line utility called <tt>set-xcode-analyzer</tt> that allows users to change what |
Jordan Rose | a2d5f0c | 2013-01-08 19:29:37 +0000 | [diff] [blame] | 82 | copy of <tt>clang</tt> that Xcode uses for analysis:</p> |
Ted Kremenek | 9265204 | 2010-02-19 08:14:02 +0000 | [diff] [blame] | 83 | |
| 84 | <pre class="code_example"> |
| 85 | $ <b>set-xcode-analyzer -h</b> |
| 86 | Usage: set-xcode-analyzer [options] |
| 87 | |
| 88 | Options: |
| 89 | -h, --help show this help message and exit |
| 90 | --use-checker-build=PATH |
| 91 | Use the Clang located at the provided absolute path, |
| 92 | e.g. /Users/foo/checker-1 |
| 93 | --use-xcode-clang Use the Clang bundled with Xcode |
| 94 | </pre> |
| 95 | |
| 96 | <p>Operationally, <b>set-xcode-analyzer</b> edits Xcode's configuration files |
Jordan Rose | a2d5f0c | 2013-01-08 19:29:37 +0000 | [diff] [blame] | 97 | to point it to use the version of <tt>clang</tt> you specify for static |
| 98 | analysis. Within this model it provides you two basic modes:</p> |
Ted Kremenek | 9265204 | 2010-02-19 08:14:02 +0000 | [diff] [blame] | 99 | |
| 100 | <ul> |
| 101 | <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 | <li><b>--use-checker-build</b>: Switch Xcode to using the <tt>clang</tt> provided by the specified analyzer build.</li> |
| 103 | </ul> |
| 104 | |
Ted Kremenek | 47537b6 | 2011-01-31 20:45:38 +0000 | [diff] [blame] | 105 | <h4>Things to keep in mind</h4> |
| 106 | |
| 107 | <ul> |
Jordan Rose | a2d5f0c | 2013-01-08 19:29:37 +0000 | [diff] [blame] | 108 | <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 | <b><tt>sudo</tt></b> in order to have write privileges to modify the Xcode |
| 110 | configuration files.</li> |
Ted Kremenek | 47537b6 | 2011-01-31 20:45:38 +0000 | [diff] [blame] | 111 | </ul> |
| 112 | |
Ted Kremenek | 9265204 | 2010-02-19 08:14:02 +0000 | [diff] [blame] | 113 | <h4>Examples</h4> |
| 114 | |
Jordan Rose | a2d5f0c | 2013-01-08 19:29:37 +0000 | [diff] [blame] | 115 | <p><b>Example 1</b>: Telling Xcode to use checker-235:</p> |
Ted Kremenek | 9265204 | 2010-02-19 08:14:02 +0000 | [diff] [blame] | 116 | |
| 117 | <pre class="code_example"> |
| 118 | $ pwd |
| 119 | /tmp |
| 120 | $ tar xjf checker-235.tar.bz2 |
Ted Kremenek | 47537b6 | 2011-01-31 20:45:38 +0000 | [diff] [blame] | 121 | $ sudo checker-235/set-xcode-analyzer --use-checker-build=/tmp/checker-235 |
Ted Kremenek | 9265204 | 2010-02-19 08:14:02 +0000 | [diff] [blame] | 122 | </pre> |
| 123 | |
Ted Kremenek | 353394f | 2010-02-19 08:24:37 +0000 | [diff] [blame] | 124 | <p>Note that you typically won't install an analyzer build in <tt>/tmp</tt>, but |
| 125 | the point of this example is that <tt>set-xcode-analyzer</tt> just wants a full |
| 126 | path to an untarred analyzer build.</p> |
| 127 | |
| 128 | <p><b>Example 2</b>: Telling Xcode to use a very specific version of <tt>clang</tt>:</p> |
Ted Kremenek | 9265204 | 2010-02-19 08:14:02 +0000 | [diff] [blame] | 129 | |
| 130 | <pre class="code_example"> |
Ted Kremenek | 47537b6 | 2011-01-31 20:45:38 +0000 | [diff] [blame] | 131 | $ sudo set-xcode-analyzer --use-checker-build=~/mycrazyclangbuild/bin/clang |
Ted Kremenek | 9265204 | 2010-02-19 08:14:02 +0000 | [diff] [blame] | 132 | </pre> |
| 133 | |
Ted Kremenek | 353394f | 2010-02-19 08:24:37 +0000 | [diff] [blame] | 134 | <p><b>Example 3</b>: Resetting Xcode to its default behavior:</p> |
Ted Kremenek | 9265204 | 2010-02-19 08:14:02 +0000 | [diff] [blame] | 135 | |
| 136 | <pre class="code_example"> |
Ted Kremenek | 47537b6 | 2011-01-31 20:45:38 +0000 | [diff] [blame] | 137 | $ sudo set-xcode-analyzer --use-xcode-clang |
Ted Kremenek | 9265204 | 2010-02-19 08:14:02 +0000 | [diff] [blame] | 138 | </pre> |
| 139 | |
| 140 | </div> |
| 141 | </div> |
| 142 | </body> |
| 143 | </html> |
| 144 | |