blob: 5184aeddfef49f9597492f6c461537741f21ea66 [file] [log] [blame]
Ted Kremenek591b9072009-06-08 21:21:24 +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>Source Annotations</title>
6 <link type="text/css" rel="stylesheet" href="menu.css" />
7 <link type="text/css" rel="stylesheet" href="content.css" />
Ted Kremenekf4aed5f2010-02-12 21:05:44 +00008 <script type="text/javascript" src="scripts/menu.js"></script>
9 <script type="text/javascript" src="scripts/dbtree.js"></script>
Ted Kremenek591b9072009-06-08 21:21:24 +000010</head>
11<body>
12
Ted Kremenek8bebc6e2010-02-09 23:05:59 +000013<div id="page">
Ted Kremenek591b9072009-06-08 21:21:24 +000014<!--#include virtual="menu.html.incl"-->
15
16<div id="content">
17
18<h1>Source Annotations</h1>
19
20<p>The Clang frontend supports several source-level annotations in the form of
21<a href="http://gcc.gnu.org/onlinedocs/gcc/Attribute-Syntax.html">GCC-style
22attributes</a> and pragmas that can help make using the Clang Static Analyzer
23more useful. These annotations can both help suppress false positives as well as
24enhance the analyzer's ability to find bugs.</p>
25
26<p>This page gives a practical overview of such annotations. For more technical
27specifics regarding Clang-specific annotations please see the Clang's list of <a
28href="http://clang.llvm.org/docs/LanguageExtensions.html">language
29extensions</a>. Details of &quot;standard&quot; GCC attributes (that Clang also
30supports) can be found in the <a href="http://gcc.gnu.org/onlinedocs/gcc/">GCC
31manual</a>, with the majority of the relevant attributes being in the section on
32<a href="http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html">function
33attributes</a>.</p>
34
35<p>Note that attributes that are labeled <b>Clang-specific</b> are not
36recognized by GCC. Their use can be conditioned using preprocessor macros
37(examples included on this page).</p>
38
Ted Kremenekf4aed5f2010-02-12 21:05:44 +000039<h4>Specific Topics</h4>
40
41<ul id="collapsetree" class="dbtree onclick multiple">
42<li><a href="#generic">Annotations to Enhance Generic Checks</a>
Ted Kremenek591b9072009-06-08 21:21:24 +000043 <ul>
Ted Kremenekf4aed5f2010-02-12 21:05:44 +000044 <li><a href="#null_checking"><span>Null Pointer Checking</span></a>
Ted Kremenek591b9072009-06-08 21:21:24 +000045 <ul>
Ted Kremenekf4aed5f2010-02-12 21:05:44 +000046 <li><a href="#attr_nonnull"><span>Attribute 'nonnull'</span></a></li>
Ted Kremenek591b9072009-06-08 21:21:24 +000047 </ul>
Ted Kremenekf4aed5f2010-02-12 21:05:44 +000048 </li>
Ted Kremenek591b9072009-06-08 21:21:24 +000049 </ul>
Ted Kremenekf4aed5f2010-02-12 21:05:44 +000050</li>
51<li><a href="#macosx">Mac OS X API Annotations</a>
Ted Kremenek591b9072009-06-08 21:21:24 +000052 <ul>
Ted Kremenekf4aed5f2010-02-12 21:05:44 +000053 <li><a href="#cocoa_mem">Cocoa &amp; Core Foundation Memory Management Annotations</a>
Ted Kremenek591b9072009-06-08 21:21:24 +000054 <ul>
Ted Kremenekf4aed5f2010-02-12 21:05:44 +000055 <li><a href="#attr_ns_returns_retained">Attribute 'ns_returns_retained'</a></li>
Ted Kremeneka238f872010-05-14 18:13:43 +000056 <li><a href="#attr_ns_returns_not_retained">Attribute 'ns_returns_not_retained'</a></li>
Ted Kremenekf4aed5f2010-02-12 21:05:44 +000057 <li><a href="#attr_cf_returns_retained">Attribute 'cf_returns_retained'</a></li>
Ted Kremeneka238f872010-05-14 18:13:43 +000058 <li><a href="#attr_cf_returns_not_retained">Attribute 'cf_returns_not_retained'</a></li>
Ted Kremenekf4aed5f2010-02-12 21:05:44 +000059 </ul>
60 </li>
Ted Kremenek591b9072009-06-08 21:21:24 +000061 </ul>
Ted Kremenekf4aed5f2010-02-12 21:05:44 +000062</li>
63<li><a href="#custom_assertions">Custom Assertion Handlers</a>
Ted Kremenek591b9072009-06-08 21:21:24 +000064 <ul>
65 <li><a href="#attr_noreturn">Attribute 'noreturn'</a></li>
66 <li><a href="#attr_analyzer_noreturn">Attribute 'analyzer_noreturn'</a></li>
67 </ul>
Ted Kremenekf4aed5f2010-02-12 21:05:44 +000068 </li>
Ted Kremenek591b9072009-06-08 21:21:24 +000069</ul>
70
71<!-- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -->
72<h2 id="generic">Annotations to Enhance Generic Checks</h2>
73<!-- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -->
74
75<h3 id="null_checking">Null Pointer Checking</h3>
76
77<h4 id="attr_nonnull">Attribute 'nonnull'</h4>
78
79<p>The analyzer recognizes the GCC attribute 'nonnull', which indicates that a
80function expects that a given function parameter is not a null pointer. Specific
81details of the syntax of using the 'nonnull' attribute can be found in <a
82href="http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html#index-g_t_0040code_007bnonnull_007d-function-attribute-2263">GCC's
83documentation</a>.</p>
84
85<p>Both the Clang compiler and GCC will flag warnings for simple cases where a
86null pointer is directly being passed to a function with a 'nonnull' parameter
87(e.g., as a constant). The analyzer extends this checking by using its deeper
88symbolic analysis to track what pointer values are potentially null and then
89flag warnings when they are passed in a function call via a 'nonnull'
90parameter.</p>
91
92<p><b>Example</b></p>
93
94<pre class="code_example">
95<span class="command">$ cat test.m</span>
96int bar(int*p, int q, int *r) __attribute__((nonnull(1,3)));
97
98int foo(int *p, int *q) {
99 return !p ? bar(q, 2, p)
100 : bar(p, 2, q);
101}
Ted Kremenek591b9072009-06-08 21:21:24 +0000102</pre>
103
Ted Kremenek42291dd2009-06-24 19:17:54 +0000104<p>Running <tt>scan-build</tt> over this source produces the following
105output:</p>
106
Ted Kremenek5ceab862009-06-24 19:20:24 +0000107<img src="images/example_attribute_nonnull.png">
Ted Kremenek42291dd2009-06-24 19:17:54 +0000108
Ted Kremenek591b9072009-06-08 21:21:24 +0000109<!-- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -->
110<h2 id="macosx">Mac OS X API Annotations</h2>
111<!-- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -->
112
113<h3 id="cocoa_mem">Cocoa &amp; Core Foundation Memory Management
114Annotations</h3>
115
Ted Kremenekcb41f3e2009-06-09 01:32:41 +0000116<!--
Ted Kremenek591b9072009-06-08 21:21:24 +0000117<p>As described in <a href="/available_checks.html#retain_release">Available
Ted Kremenekcb41f3e2009-06-09 01:32:41 +0000118Checks</a>,
119-->
120<p>The analyzer supports the proper management of retain counts for
Ted Kremenek591b9072009-06-08 21:21:24 +0000121both Cocoa and Core Foundation objects. This checking is largely based on
122enforcing Cocoa and Core Foundation naming conventions for Objective-C methods
123(Cocoa) and C functions (Core Foundation). Not strictly following these
124conventions can cause the analyzer to miss bugs or flag false positives.</p>
125
126<p>One can educate the analyzer (and others who read your code) about methods or
127functions that deviate from the Cocoa and Core Foundation conventions using the
128attributes described here.</p>
129
130<h4 id="attr_ns_returns_retained">Attribute 'ns_returns_retained'
131(Clang-specific)</h4>
132
133<p>The GCC-style (Clang-specific) attribute 'ns_returns_retained' allows one to
134annotate an Objective-C method or C function as returning a retained Cocoa
135object that the caller is responsible for releasing (via sending a
136<tt>release</tt> message to the object).</p>
137
138<p><b>Placing on Objective-C methods</b>: For Objective-C methods, this
139annotation essentially tells the analyzer to treat the method as if its name
140begins with &quot;alloc&quot; or &quot;new&quot; or contais the word
141&quot;copy&quot;.</p>
142
143<p><b>Placing on C functions</b>: For C functions returning Cocoa objects, the
144analyzer typically does not make any assumptions about whether or not the object
145is returned retained. Explicitly adding the 'ns_returns_retained' attribute to C
146functions allows the analyzer to perform extra checking.</p>
147
148<p><b>Important note when using Garbage Collection</b>: Note that the analyzer
149interprets this attribute slightly differently when using Objective-C garbage
150collection (available on Mac OS 10.5+). When analyzing Cocoa code that uses
151garbage collection, &quot;alloc&quot; methods are assumed to return an object
152that is managed by the garbage collector (and thus doesn't have a retain count
153the caller must balance). These same assumptions are applied to methods or
154functions annotated with 'ns_returns_retained'. If you are returning a Core
155Foundation object (which may not be managed by the garbage collector) you should
156use 'cf_returns_retained'.</p>
157
158<p><b>Example</b></p>
159
160<pre class="code_example">
161<span class="command">$ cat test.m</span>
162#import &lt;Foundation/Foundation.h&gt;
163
Ted Kremenek21375a32009-07-17 00:25:49 +0000164#ifndef __has_feature // Optional.
165#define __has_feature(x) 0 // Compatibility with non-clang compilers.
166#endif
167
Ted Kremenek591b9072009-06-08 21:21:24 +0000168#ifndef NS_RETURNS_RETAINED
Ted Kremenek21375a32009-07-17 00:25:49 +0000169#if __has_feature(attribute_ns_returns_retained)
Ted Kremenek591b9072009-06-08 21:21:24 +0000170<span class="code_highlight">#define NS_RETURNS_RETAINED __attribute__((ns_returns_retained))</span>
171#else
172#define NS_RETURNS_RETAINED
173#endif
174#endif
175
176@interface MyClass : NSObject {}
177- (NSString*) returnsRetained <span class="code_highlight">NS_RETURNS_RETAINED</span>;
178- (NSString*) alsoReturnsRetained;
179@end
180
181@implementation MyClass
182- (NSString*) returnsRetained {
183 return [[NSString alloc] initWithCString:"no leak here"];
184}
185- (NSString*) alsoReturnsRetained {
186 return [[NSString alloc] initWithCString:"flag a leak"];
187}
188@end
Ted Kremenek591b9072009-06-08 21:21:24 +0000189</pre>
190
Ted Kremenek7d277e22009-06-24 18:50:14 +0000191<p>Running <tt>scan-build</tt> on this source file produces the following output:</p>
192
193<img src="images/example_ns_returns_retained.png">
194
Ted Kremeneka238f872010-05-14 18:13:43 +0000195<h4 id="attr_ns_returns_not_retained">Attribute 'ns_returns_not_retained'
196(Clang-specific)</h4>
197
198<p>The 'ns_returns_not_retained' attribute is the complement of '<a
199href="#attr_ns_returns_retained">ns_returns_retained</a>'. Where a function or
200method may appear to obey the Cocoa conventions and return a retained Cocoa
201object, this attribute can be used to indicate that the object reference
202returned should not be considered as an &quot;owning&quot; reference being
203returned to the caller.</p>
204
205<p>Usage is identical to <a
206href="#attr_ns_returns_retained">ns_returns_retained</a>. When using the
207attribute, be sure to declare it within the proper macro that checks for
208its availability, as it is not available in earlier versions of the analyzer:</p>
209
210<pre class="code_example">
211<span class="command">$ cat test.m</span>
212#ifndef __has_feature // Optional.
213#define __has_feature(x) 0 // Compatibility with non-clang compilers.
214#endif
215
216#ifndef NS_RETURNS_NOT_RETAINED
217#if __has_feature(attribute_ns_returns_not_retained)
218<span class="code_highlight">#define NS_RETURNS_NOT_RETAINED __attribute__((ns_returns_not_retained))</span>
219#else
220#define NS_RETURNS_NOT_RETAINED
221#endif
222#endif
223</pre>
224
Ted Kremenek591b9072009-06-08 21:21:24 +0000225<h4 id="attr_cf_returns_retained">Attribute 'cf_returns_retained'
226(Clang-specific)</h4>
227
228<p>The GCC-style (Clang-specific) attribute 'cf_returns_retained' allows one to
229annotate an Objective-C method or C function as returning a retained Core
230Foundation object that the caller is responsible for releasing.
231
232<p><b>Placing on Objective-C methods</b>: With respect to Objective-C methods.,
233this attribute is identical in its behavior and usage to 'ns_returns_retained'
234except for the distinction of returning a Core Foundation object instead of a
235Cocoa object. This distinction is important for two reasons:</p>
236
237<ul>
238 <li>Core Foundation objects are not automatically managed by the Objective-C
239 garbage collector.</li>
240 <li>Because Core Foundation is a C API, the analyzer cannot always tell that a
241 pointer return value refers to a Core Foundation object. In contrast, it is
242 trivial for the analyzer to recognize if a pointer refers to a Cocoa object
243 (given the Objective-C type system).</p>
244</ul>
245
246<p><b>Placing on C functions</b>: When placing the attribute
247'cf_returns_retained' on the declarations of C functions, the analyzer
248interprets the function as:</p>
249
250<ol>
251 <li>Returning a Core Foundation Object</li>
252 <li>Treating the function as if it its name
253contained the keywords &quot;create&quot; or &quot;copy&quot;. This means the
254returned object as a +1 retain count that must be released by the caller, either
255by sending a <tt>release</tt> message (via toll-free bridging to an Objective-C
256object pointer), calling <tt>CFRelease</tt> (or similar function), or using
257<tt>CFMakeCollectable</tt> to register the object with the Objective-C garbage
258collector.</li>
259</ol>
260
261<p><b>Example</b></p>
262
263<p>In this example, observe the difference in output when the code is compiled
264to not use garbage collection versus when it is compiled to only use garbage
265collection (<tt>-fobjc-gc-only</tt>).</p>
266
267<pre class="code_example">
268<span class="command">$ cat test.m</span>
269$ cat test.m
270#import &lt;Cocoa/Cocoa.h&gt;
271
Ted Kremenek21375a32009-07-17 00:25:49 +0000272#ifndef __has_feature // Optional.
273#define __has_feature(x) 0 // Compatibility with non-clang compilers.
274#endif
275
Ted Kremenek591b9072009-06-08 21:21:24 +0000276#ifndef CF_RETURNS_RETAINED
Ted Kremenek21375a32009-07-17 00:25:49 +0000277#if __has_feature(attribute_cf_returns_retained)
Ted Kremenek591b9072009-06-08 21:21:24 +0000278<span class="code_highlight">#define CF_RETURNS_RETAINED __attribute__((cf_returns_retained))</span>
279#else
280#define CF_RETURNS_RETAINED
281#endif
282#endif
283
284@interface MyClass : NSObject {}
285- (NSDate*) returnsCFRetained <span class="code_highlight">CF_RETURNS_RETAINED</span>;
286- (NSDate*) alsoReturnsRetained;
287- (NSDate*) returnsNSRetained <span class="code_highlight">NS_RETURNS_RETAINED</span>;
288@end
289
290<span class="code_highlight">CF_RETURNS_RETAINED</span>
291CFDateRef returnsRetainedCFDate() {
292 return CFDateCreate(0, CFAbsoluteTimeGetCurrent());
293}
294
295@implementation MyClass
296- (NSDate*) returnsCFRetained {
297 return (NSDate*) returnsRetainedCFDate(); // No leak.
298}
299
300- (NSDate*) alsoReturnsRetained {
301 return (NSDate*) returnsRetainedCFDate(); // Always report a leak.
302}
303
304- (NSDate*) returnsNSRetained {
305 return (NSDate*) returnsRetainedCFDate(); // Report a leak when using GC.
306}
307@end
Ted Kremenek591b9072009-06-08 21:21:24 +0000308</pre>
309
Ted Kremenek6fe1ef22009-06-24 19:04:37 +0000310<p>Running <tt>scan-build</tt> on this example produces the following output:</p>
311
312<img src="images/example_cf_returns_retained.png">
313
314</p>When the above code is compiled using Objective-C garbage collection (i.e.,
315code is compiled with the flag <tt>-fobjc-gc</tt> or <tt>-fobjc-gc-only</tt>),
316<tt>scan-build</tt> produces both the above error (with slightly different text
317to indicate the code uses garbage collection) as well as the following warning,
318which indicates a leak that occurs <em>only</em> when using garbage
319collection:</p>
320
321<img src="images/example_cf_returns_retained_gc.png">
322
Ted Kremeneka238f872010-05-14 18:13:43 +0000323<h4 id="attr_cf_returns_not_retained">Attribute 'cf_returns_not_retained'
324(Clang-specific)</h4>
325
326<p>The 'cf_returns_not_retained' attribute is the complement of '<a
327href="#attr_cf_returns_retained">cf_returns_retained</a>'. Where a function or
328method may appear to obey the Core Foundation or Cocoa conventions and return
329a retained Core Foundation object, this attribute can be used to indicate that
330the object reference returned should not be considered as an
331&quot;owning&quot; reference being returned to the caller.</p>
332
333<p>Usage is identical to <a
334href="#attr_cf_returns_retained">cf_returns_retained</a>. When using the
335attribute, be sure to declare it within the proper macro that checks for
336its availability, as it is not available in earlier versions of the analyzer:</p>
337
338<pre class="code_example">
339<span class="command">$ cat test.m</span>
340#ifndef __has_feature // Optional.
341#define __has_feature(x) 0 // Compatibility with non-clang compilers.
342#endif
343
344#ifndef CF_RETURNS_NOT_RETAINED
345#if __has_feature(attribute_cf_returns_not_retained)
346<span class="code_highlight">#define CF_RETURNS_NOT_RETAINED __attribute__((cf_returns_not_retained))</span>
347#else
348#define CF_RETURNS_NOT_RETAINED
349#endif
350#endif
351</pre>
352
Ted Kremenek591b9072009-06-08 21:21:24 +0000353<!-- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -->
354<h2 id="custom_assertions">Custom Assertion Handlers</h2>
355<!-- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -->
356
357<p>The analyzer exploits code assertions by pruning off paths where the
358assertion condition is false. The idea is capture any program invariants
359specified in the assertion that the developer may know but is not immediately
360apparent in the code itself. In this way assertions make implicit assumptions
361explicit in the code, which not only makes the analyzer more accurate when
362finding bugs, but can help others better able to understand your code as well.
363It can also help remove certain kinds of analyzer false positives by pruning off
364false paths.</p>
365
366<p>In order to exploit assertions, however, the analyzer must understand when it
367encounters an &quot;assertion handler.&quot; Typically assertions are
368implemented with a macro, with the macro performing a check for the assertion
369condition and, when the check fails, calling an assertion handler. For example, consider the following code
370fragment:</p>
371
372<pre class="code_example">
373void foo(int *p) {
374 assert(p != NULL);
375}
376</pre>
377
378<p>When this code is preprocessed on Mac OS X it expands to the following:</p>
379
380<pre class="code_example">
381void foo(int *p) {
382 (__builtin_expect(!(p != NULL), 0) ? __assert_rtn(__func__, "t.c", 4, "p != NULL") : (void)0);
383}
384</pre>
385
386<p>In this example, the assertion handler is <tt>__assert_rtn</tt>. When called,
387most assertion handlers typically print an error and terminate the program. The
388analyzer can exploit such semantics by ending the analysis of a path once it
389hits a call to an assertion handler.</p>
390
391<p>The trick, however, is that the analyzer needs to know that a called function
392is an assertion handler; otherwise the analyzer might assume the function call
393returns and it will continue analyzing the path where the assertion condition
394failed. This can lead to false positives, as the assertion condition usually
395implies a safety condition (e.g., a pointer is not null) prior to performing
396some action that depends on that condition (e.g., dereferencing a pointer).</p>
397
398<p>The analyzer knows about several well-known assertion handlers, but can
399automatically infer if a function should be treated as an assertion handler if
400it is annotated with the 'noreturn' attribute or the (Clang-specific)
401'analyzer_noreturn' attribute.</p>
402
403<h4 id="attr_noreturn">Attribute 'noreturn'</h4>
404
405<p>The 'noreturn' attribute is a GCC-attribute that can be placed on the
406declarations of functions. It means exactly what its name implies: a function
407with a 'noreturn' attribute should never return.</p>
408
409<p>Specific details of the syntax of using the 'noreturn' attribute can be found
410in <a
411href="http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html#index-g_t_0040code_007bnoreturn_007d-function-attribute-2264">GCC's
412documentation</a>.</p>
413
414<p>Not only does the analyzer exploit this information when pruning false paths,
415but the compiler also takes it seriously and will generate different code (and
416possibly better optimized) under the assumption that the function does not
417return.</p>
418
419<p><b>Example</b></p>
420
421<p>On Mac OS X, the function prototype for <tt>__assert_rtn</tt> (declared in
422<tt>assert.h</tt>) is specifically annotated with the 'noreturn' attribute:</p>
423
424<pre class="code_example">
425void __assert_rtn(const char *, const char *, int, const char *) <span class="code_highlight">__attribute__((__noreturn__))</span>;
426</pre>
427
428<h4 id="attr_analyzer_noreturn">Attribute 'analyzer_noreturn' (Clang-specific)</h4>
429
430<p>The Clang-specific 'analyzer_noreturn' attribute is almost identical to
431'noreturn' except that it is ignored by the compiler for the purposes of code
432generation.</p>
433
434<p>This attribute is useful for annotating assertion handlers that actually
435<em>can</em> return, but for the purpose of using the analyzer we want to
436pretend that such functions do not return.</p>
437
438<p>Because this attribute is Clang-specific, its use should be conditioned with
439the use of preprocessor macros.</p>
440
441<p><b>Example</b>
442
443<pre class="code_example">
444#ifndef CLANG_ANALYZER_NORETURN
445#if __clang__
446<span class="code_highlight">#define CLANG_ANALYZER_NORETURN __attribute__((analyzer_noreturn))</span>
447#else
448#define CLANG_ANALYZER_NORETURN
449#endif
450
451void my_assert_rtn(const char *, const char *, int, const char *) <span class="code_highlight">CLANG_ANALYZER_NORETURN</span>;
452</pre>
453
454</div>
Ted Kremenek8bebc6e2010-02-09 23:05:59 +0000455</div>
Ted Kremenek591b9072009-06-08 21:21:24 +0000456</body>
457</html>
458