Ted Kremenek | cf2e304 | 2008-06-19 23:14:24 +0000 | [diff] [blame^] | 1 | <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" |
| 2 | "http://www.w3.org/TR/html4/strict.dtd"> |
Ted Kremenek | 92c23cb | 2008-06-17 06:38:07 +0000 | [diff] [blame] | 3 | <html> |
| 4 | <head> |
| 5 | <title>Information on using the Static Analyzer ("Clang Checker")</title> |
| 6 | <link type="text/css" rel="stylesheet" href="menu.css" /> |
| 7 | <link type="text/css" rel="stylesheet" href="content.css" /> |
| 8 | <style> |
| 9 | thead { |
| 10 | background-color:#eee; color:#666666; |
| 11 | font-weight: bold; cursor: default; |
| 12 | text-align:center; |
| 13 | border-top: 2px solid #000000; |
| 14 | border-bottom: 2px solid #000000; |
| 15 | font-weight: bold; font-family: Verdana |
| 16 | } |
| 17 | table { border: 1px #000000 solid } |
| 18 | table { border-collapse: collapse; border-spacing: 0px } |
| 19 | table { margin-left:20px; margin-top:20px; margin-bottom:20px } |
| 20 | td { border-bottom: 1px #000000 dotted } |
| 21 | td { padding:5px; padding-left:8px; padding-right:8px } |
| 22 | td { text-align:left; font-size:9pt } |
| 23 | td.View { padding-left: 10px } |
| 24 | </style> |
| 25 | </head> |
| 26 | <body> |
| 27 | |
| 28 | <!--#include virtual="menu.html.incl"--> |
| 29 | |
| 30 | <div id="content"> |
| 31 | |
| 32 | <h1>Information on using the Static Analyzer</h1> |
| 33 | |
| 34 | <h2 id="Obtaining">Obtaining the Analyzer</h2> |
| 35 | |
| 36 | <p> Using the analyzer involves executing <tt>scan-build</tt> (see <a |
| 37 | href="#BasicUsage">Basic Usage</a>). <tt>scan-build</tt> will first look for a |
| 38 | <tt>clang</tt> executable in the same directory as <tt>scan-build</tt>, and then |
| 39 | search your path.</p> |
| 40 | |
| 41 | <p>If one is using the analyzer directly from the Clang sources, it suffices to |
| 42 | just directly execute <tt>scan-build</tt> in the <tt>utils</tt> directory. No |
| 43 | other special installation is needed.</p> |
| 44 | |
| 45 | <h3>Packaged Builds (Mac OS X)</h3> |
| 46 | |
| 47 | <p>Semi-regular pre-built binaries of the analyzer are available on Mac OS X |
| 48 | (10.5).</p> |
| 49 | |
Ted Kremenek | 34239e9 | 2008-06-17 06:43:11 +0000 | [diff] [blame] | 50 | <p>The latest build is: |
| 51 | <!--#include virtual="latest_checker.html.incl"--> |
| 52 | </p> |
Ted Kremenek | 92c23cb | 2008-06-17 06:38:07 +0000 | [diff] [blame] | 53 | |
| 54 | <p><b>Note: This fixes a serious bug in checker-36 where the analyzer would |
| 55 | never be run, thus finding no bugs.</b></p> |
| 56 | |
| 57 | Packaged builds for other platforms may eventually be provided, but as the tool |
| 58 | is in its early stages we are not actively promoting releases yet. If you wish |
| 59 | to help contribute regular builds of the analyzer on other platforms, please |
| 60 | email the <a href="http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev">Clang |
| 61 | Developers' mailing list</a>.</p> |
| 62 | |
| 63 | <p>Packaged builds of the analyzer expand to the following files:</p> |
| 64 | |
| 65 | <table id="package"> |
| 66 | <thead><tr><td>File</td><td>Purpose</td></tr></thead> |
| 67 | <tr><td><tt><b>scan-build</b></tt></td><td>Script for running the analyzer over a project build. <b>This is the only file you care about.</b></td></tr> |
| 68 | <tr><td><tt>ccc-analyzer</tt></td><td>GCC interceptor (called by scan-build)</td></tr> |
| 69 | <tr><td><tt>clang</tt></td><td>Static Analyzer (called by ccc-analyzer)</td><tr> |
| 70 | <tr><td><tt>sorttable.js</tt></td><td>JavaScript used for displaying error reports</td></tr> |
| 71 | </table> |
| 72 | |
| 73 | <h3 id="OtherPlatforms">Other Platforms (Building the Analyzer from Source)</h3> |
| 74 | |
| 75 | <p>Packaged builds simply consist of a few files from the Clang source tree, |
| 76 | meaning that <b>anyone</b> who can build Clang can use the static analyzer. |
| 77 | Please see the <a href="get_started.html">Getting Started</a> page for more |
| 78 | details on downloading and compiling Clang.</p> |
| 79 | |
| 80 | <p>All files used by the analyzer (and included in packaged builds; <a |
| 81 | href="#package">see above</a>) other than a compiled <tt>clang</tt> executable |
| 82 | are found in the <tt>utils</tt> subdirectory in the Clang tree.</p> |
| 83 | |
| 84 | <h2 id="BasicUsage">Basic Usage</h2> |
| 85 | |
| 86 | <p>The analyzer is executed from the command-line. To run the analyzer, you will |
| 87 | use <tt>scan-build</tt> to analyze the source files compiled by <tt>gcc</tt> |
| 88 | during a project build.</p> |
| 89 | |
| 90 | <p>For example, to analyze the files compiled under a build:</p> |
| 91 | |
| 92 | <pre> |
| 93 | $ <b>scan-build</b> make |
| 94 | $ <b>scan-build</b> xcodebuild |
| 95 | </pre> |
| 96 | |
| 97 | <p> In the first case <tt>scan-build</tt> analyzes the code of a project built |
| 98 | with <tt>make</tt>, andin the second case <tt>scan-build</tt> analyzes a project |
| 99 | built using <tt>xcodebuild</tt>. In general, the format is: </p> |
| 100 | |
| 101 | <pre> |
| 102 | $ <b>scan-build</b> <i>[scan-build options]</i> <b><command></b> <i>[command options]</i> |
| 103 | </pre> |
| 104 | |
| 105 | <p> Operationally, <tt>scan-build</tt> literally runs <command> with all of the |
| 106 | subsequent options passed to it. For example</p> |
| 107 | |
| 108 | <pre> |
| 109 | $ scan-build make <b>-j4</b> |
| 110 | </pre> |
| 111 | |
| 112 | <p>In this example, <tt>scan-build</tt> makes no effort to interpret the options |
| 113 | after the build command (in this case, <tt>make</tt>); it just passes them |
| 114 | through. In general, <tt>scan-build</tt> should support parallel builds, but |
| 115 | <b>not distributed builds</b>. Similarly, you can use <tt>scan-build</tt> to |
| 116 | analyze specific files: |
| 117 | |
| 118 | <pre> |
| 119 | $ scan-build gcc -c <b>t1.c t2.c</b> |
| 120 | </pre> |
| 121 | |
| 122 | <p> |
| 123 | This example causes the files <tt>t1.c</tt> and <tt>t2.c</tt> to be analyzed. |
| 124 | </p> |
| 125 | |
| 126 | <h3>Other Options</h3> |
| 127 | |
| 128 | <p> |
| 129 | As mentioned above, extra options can be passed to <tt>scan-build</tt>. These |
| 130 | options prefix the build command. For example:</p> |
| 131 | |
| 132 | <pre> |
| 133 | $ scan-build <b>-k -V</b> make |
| 134 | $ scan-build <b>-k -V</b> xcodebuild |
| 135 | </pre> |
| 136 | |
| 137 | <p>Here are a complete list of options:</p> |
| 138 | |
| 139 | <table> |
| 140 | <thead><tr><td>Option</td><td>Description</td></tr></thead> |
| 141 | |
| 142 | <tr><td><b>-a</b></td> |
| 143 | <td>The analysis to run. The default analysis is <i>checker-cfref</i>. Valid options are: <i>checker-cfref</i>, <i>fsyntax-only</i>. |
| 144 | These translate into options passed down to the <tt>clang</tt> executable, and currently this option is mainly used for debugging.</td></tr> |
| 145 | |
| 146 | <tr><td><b>-o</b></td><td>Target directory for HTML report files. Subdirectories will be |
| 147 | created as needed to represent separate "runs" of the analyzer. If this option |
| 148 | is not specified, a directory is created in <tt>/tmp</tt> to store the |
| 149 | reports.</td><tr> |
| 150 | |
| 151 | <tr><td><b>-h</b><br><i><nobr>(or no arguments)</nobr></i></td><td>Display <tt>scan-build</tt> options.</td></tr> |
| 152 | |
| 153 | <tr><td><b>-k</b><br><nobr><b>--keep-going</b></nobr></td><td>Add a "keep on going" option to the |
| 154 | specified build command. <p>This option currently supports <tt>make</tt> and |
| 155 | <tt>xcodebuild</tt>.</p> <p>This is a convenience option; one can specify this |
| 156 | behavior directly using build options.</p></td></tr> |
| 157 | |
| 158 | <tr><td><b>-v<b></td><td>Verbose output from scan-build and the analyzer. <b>A second and third |
| 159 | "-v" increases verbosity</b>, and is useful for filing bug reports against the analyzer.</td></tr> |
| 160 | |
| 161 | <tr><td><b>-V</b></td><td>View analysis results in a web browser when the build command completes.</td></tr> |
| 162 | </table> |
| 163 | |
| 164 | <p>These options can also be viewed by running <tt>scan-build</tt> with no |
| 165 | arguments:</p> |
| 166 | |
| 167 | <pre> |
| 168 | $ <b>scan-build</b> |
| 169 | |
| 170 | USAGE: scan-build [options] <build command> [build options] |
| 171 | |
| 172 | OPTIONS: |
| 173 | |
| 174 | -a - The analysis to run. The default is 'checker-cfref'. |
| 175 | Valid options are: 'checker-cfref', 'fsyntax-only' |
| 176 | |
| 177 | -o - Target directory for HTML report files. Subdirectories |
| 178 | will be created as needed to represent separate "runs" of |
| 179 | the analyzer. If this option is not specified, a directory |
| 180 | is created in /tmp to store the reports. |
| 181 | <b>...</b> |
| 182 | </pre> |
| 183 | |
| 184 | <h2 id="Output">Output of the Analyzer</h2> |
| 185 | |
| 186 | <p> |
| 187 | The output of the analyzer is a set of HTML files, each one which represents a |
| 188 | separate bug report. A single <tt>index.html</tt> file is generated for |
| 189 | surveying all of the bugs. You can then just open <tt>index.html</tt> in a web |
| 190 | browser to view the bug reports. |
| 191 | </p> |
| 192 | |
| 193 | <p> |
| 194 | Where the HTML files are generated is specified with a <b>-o</b> option to |
| 195 | <tt>scan-build</tt>. If <b>-o</b> isn't specified, a directory in <tt>/tmp</tt> |
| 196 | is created to store the files (<tt>scan-build</tt> will print a message telling |
| 197 | you where they are). If you want to view the reports immediately after the build |
| 198 | completes, pass <b>-V</b> to <tt>scan-build</tt>. |
| 199 | </p> |
| 200 | |
| 201 | |
| 202 | <h2 id="RecommendedUsageGuidelines">Recommended Usage Guidelines</h2> |
| 203 | |
| 204 | Here are a few recommendations with running the analyzer: |
| 205 | |
| 206 | <h3>Always Analyze a Project in its "Debug" Configuration</h3> |
| 207 | |
| 208 | <p>Most projects can be built in a "debug" mode that enables assertions. |
| 209 | Assertions are picked up by the static analyzer to prune infeasible paths, which |
| 210 | in some cases can greatly reduce the number of false positives (bogus error |
| 211 | reports) emitted by the tool.</p> |
| 212 | |
| 213 | <h3>Pass -k to scan-build</h3> |
| 214 | |
| 215 | <p>While <tt>ccc-analyzer</tt> invokes <tt>gcc</tt> to compile code, any |
| 216 | problems in correctly forwarding arguments to <tt>gcc</tt> may result in a build |
| 217 | failure. Passing <b>-k</b> to <tt>scan-build</tt> potentially allows you to |
| 218 | analyze other code in a project for which this problem doesn't occur.</p> |
| 219 | |
| 220 | <p> Also, it is useful to analyze a project even if not all of the source files |
| 221 | are compilable. This is great when using <tt>scan-build</tt> as part of your |
| 222 | compile-debug cycle.</p> |
| 223 | |
| 224 | <h3>Use Verbose Output when Debugging scan-build</h3> |
| 225 | |
| 226 | <p><tt>scan-build</tt> takes a <b>-v</b> option to emit verbose output about |
| 227 | what it's doing; two <b>-v</b> options emit more information. Redirecting the |
| 228 | output of <tt>scan-build</tt> to a text file (make sure to redirect standard |
| 229 | error) is useful for filing bug reports against <tt>scan-build</tt> or the |
| 230 | analyzer, as we can see the exact options (and files) passed to the analyzer. |
| 231 | For more comprehendible logs, don't perform a parallel build.</p> |
| 232 | |
| 233 | <h2 id="Debugging">Debugging the Analyzer</h2> |
| 234 | |
| 235 | <p>This section provides information on debugging the analyzer, and troubleshooting |
| 236 | it when you have problems analyzing a particular project.</p> |
| 237 | |
| 238 | <h3>How it Works</h3> |
| 239 | |
| 240 | <p>To analyze a project, <tt>scan-build</tt> simply sets the environment variable |
| 241 | <tt>CC</tt> to the full path to <tt>ccc-analyzer</tt>. It also sets a few other |
| 242 | environment variables to communicate to <tt>ccc-analyzer</tt> where to dump HTML |
| 243 | report files.</p> |
| 244 | |
| 245 | <p>Some Makefiles (or equivalent project files) hardcode the compiler; for such |
| 246 | projects simply overriding <tt>CC</tt> won't cause <tt>ccc-analyzer</tt> to be |
| 247 | called. This will cause the compiled code <b>to not be analyzed.</b></p> If you |
| 248 | find that your code isn't being analyzed, check to see if <tt>CC</tt> is |
| 249 | hardcoded. If this is the case, you can hardcode it instead to the <b>full |
| 250 | path</b> to <tt>ccc-analyzer</tt>.</p> |
| 251 | |
| 252 | <p>When applicable, you can also run <tt>./configure</tt> for a project through |
| 253 | <tt>scan-build</tt> so that configure sets up the location of <tt>CC</tt> based |
| 254 | on the environment passed in from <tt>scan-build</tt>: |
| 255 | |
| 256 | <pre> |
| 257 | $ scan-build <b>./configure</b> |
| 258 | </pre> |
| 259 | |
| 260 | <p><tt>scan-build</tt> has special knowledge about <tt>configure</tt>, so it in |
| 261 | most cases will not actually analyze the configure tests run by |
| 262 | <tt>configure</tt>.</p> |
| 263 | |
| 264 | <p>Under the hood, <tt>ccc-analyzer</tt> directly invokes <tt>gcc</tt> to |
| 265 | compile the actual code in addition to running the analyzer (which occurs by it |
| 266 | calling <tt>clang</tt>). <tt>ccc-analyzer</tt> tries to correctly forward all |
| 267 | the arguments over to <tt>gcc</tt>, but this may not work perfectly (please |
| 268 | report bugs of this kind). |
| 269 | |
| 270 | <h2 id="filingbugs">Filing Bugs</h2> |
| 271 | |
| 272 | <p>We encourage users to file bug reports for any problems that they |
| 273 | encounter.</p> |
| 274 | |
| 275 | <h3>Outside Apple</h3> |
| 276 | |
| 277 | <p>Please <a href="http://llvm.org/bugs/enter_bug.cgi?product=clang">file |
| 278 | bugs</a> (against Clang) in LLVM's Bugzilla database.</p> |
| 279 | |
| 280 | <h3>Apple-internal Users</h3> |
| 281 | |
| 282 | <p>Please file bugs in Radar against the <b>llvm - clang</b> component.</p> |
| 283 | |
| 284 | </div> |
| 285 | </body> |
| 286 | </html> |
| 287 | |