<commit_before><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
          "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
  <title>Source Annotations</title>
  <link type="text/css" rel="stylesheet" href="menu.css" />
  <link type="text/css" rel="stylesheet" href="content.css" />
</head>
<body>

<!--#include virtual="menu.html.incl"-->

<div id="content">

<h1>Source Annotations</h1>

<p>The Clang frontend supports several source-level annotations in the form of
<a href="http://gcc.gnu.org/onlinedocs/gcc/Attribute-Syntax.html">GCC-style
attributes</a> and pragmas that can help make using the Clang Static Analyzer
more useful. These annotations can both help suppress false positives as well as
enhance the analyzer's ability to find bugs.</p>

<p>This page gives a practical overview of such annotations. For more technical
specifics regarding Clang-specific annotations please see the Clang's list of <a
href="http://clang.llvm.org/docs/LanguageExtensions.html">language
extensions</a>. Details of &quot;standard&quot; GCC attributes (that Clang also
supports) can be found in the <a href="http://gcc.gnu.org/onlinedocs/gcc/">GCC
manual</a>, with the majority of the relevant attributes being in the section on
<a href="http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html">function
attributes</a>.</p>

<p>Note that attributes that are labeled <b>Clang-specific</b> are not
recognized by GCC. Their use can be conditioned using preprocessor macros
(examples included on this page).</p>

<ul>
<li><a href="#generic">Annotations to Enhance Generic Checks</a></li>
  <ul>
    <li><a href="#null_checking">Null Pointer Checking</a></li>
    <ul>
      <li><a href="#attr_nonnull">Attribute 'nonnull'</a></li>
    </ul>
  </ul>
<li><a href="#macosx">Mac OS X API Annotations</a></li>
  <ul>
    <li><a href="#cocoa_mem">Cocoa &amp; Core Foundation Memory Management
      Annotations</a></li>
    <ul>
    <li><a href="#attr_ns_returns_retained">Attribute
    'ns_returns_retained'</a></li>
    <li><a href="#attr_cf_returns_retained">Attribute
    'cf_returns_retained'</a></li>
  </ul>
  </ul>
<li><a href="#custom_assertions">Custom Assertion Handlers</a></li>
  <ul>
    <li><a href="#attr_noreturn">Attribute 'noreturn'</a></li>
    <li><a href="#attr_analyzer_noreturn">Attribute 'analyzer_noreturn'</a></li>
  </ul>
</ul>

<!-- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -->
<h2 id="generic">Annotations to Enhance Generic Checks</h2>
<!-- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -->

<h3 id="null_checking">Null Pointer Checking</h3>

<h4 id="attr_nonnull">Attribute 'nonnull'</h4>

<p>The analyzer recognizes the GCC attribute 'nonnull', which indicates that a
function expects that a given function parameter is not a null pointer. Specific
details of the syntax of using the 'nonnull' attribute can be found in <a
href="http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html#index-g_t_0040code_007bnonnull_007d-function-attribute-2263">GCC's
documentation</a>.</p>

<p>Both the Clang compiler and GCC will flag warnings for simple cases where a
null pointer is directly being passed to a function with a 'nonnull' parameter
(e.g., as a constant). The analyzer extends this checking by using its deeper
symbolic analysis to track what pointer values are potentially null and then
flag warnings when they are passed in a function call via a 'nonnull'
parameter.</p>

<p><b>Example</b></p>

<pre class="code_example">
<span class="command">$ cat test.m</span>
int bar(int*p, int q, int *r) __attribute__((nonnull(1,3)));

int foo(int *p, int *q) {
   return !p ? bar(q, 2, p) 
             : bar(p, 2, q);
}
</pre>

<p>Running <tt>scan-build</tt> over this source produces the following
output:</p>

<img src="images/example_attribute_nonnull.png">

<!-- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -->
<h2 id="macosx">Mac OS X API Annotations</h2>
<!-- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -->

<h3 id="cocoa_mem">Cocoa &amp; Core Foundation Memory Management
Annotations</h3>

<!--
<p>As described in <a href="/available_checks.html#retain_release">Available
Checks</a>,
-->
<p>The analyzer supports the proper management of retain counts for
both Cocoa and Core Foundation objects. This checking is largely based on
enforcing Cocoa and Core Foundation naming conventions for Objective-C methods
(Cocoa) and C functions (Core Foundation). Not strictly following these
conventions can cause the analyzer to miss bugs or flag false positives.</p>

<p>One can educate the analyzer (and others who read your code) about methods or
functions that deviate from the Cocoa and Core Foundation conventions using the
attributes described here.</p>

<h4 id="attr_ns_returns_retained">Attribute 'ns_returns_retained'
(Clang-specific)</h4>

<p>The GCC-style (Clang-specific) attribute 'ns_returns_retained' allows one to
annotate an Objective-C method or C function as returning a retained Cocoa
object that the caller is responsible for releasing (via sending a
<tt>release</tt> message to the object).</p>

<p><b>Placing on Objective-C methods</b>: For Objective-C methods, this
annotation essentially tells the analyzer to treat the method as if its name
begins with &quot;alloc&quot; or &quot;new&quot; or contais the word
&quot;copy&quot;.</p>

<p><b>Placing on C functions</b>: For C functions returning Cocoa objects, the
analyzer typically does not make any assumptions about whether or not the object
is returned retained. Explicitly adding the 'ns_returns_retained' attribute to C
functions allows the analyzer to perform extra checking.</p>

<p><b>Important note when using Garbage Collection</b>: Note that the analyzer
interprets this attribute slightly differently when using Objective-C garbage
collection (available on Mac OS 10.5+). When analyzing Cocoa code that uses
garbage collection, &quot;alloc&quot; methods are assumed to return an object
that is managed by the garbage collector (and thus doesn't have a retain count
the caller must balance). These same assumptions are applied to methods or
functions annotated with 'ns_returns_retained'. If you are returning a Core
Foundation object (which may not be managed by the garbage collector) you should
use 'cf_returns_retained'.</p>

<p><b>Example</b></p>

<pre class="code_example">
<span class="command">$ cat test.m</span>
#import &lt;Foundation/Foundation.h&gt;

#ifndef NS_RETURNS_RETAINED
#if __clang__
<span class="code_highlight">#define NS_RETURNS_RETAINED __attribute__((ns_returns_retained))</span>
#else
#define NS_RETURNS_RETAINED
#endif
#endif

@interface MyClass : NSObject {}
- (NSString*) returnsRetained <span class="code_highlight">NS_RETURNS_RETAINED</span>;
- (NSString*) alsoReturnsRetained;
@end

@implementation MyClass
- (NSString*) returnsRetained {
  return [[NSString alloc] initWithCString:"no leak here"];
}
- (NSString*) alsoReturnsRetained {
  return [[NSString alloc] initWithCString:"flag a leak"];
}
@end
</pre>

<p>Running <tt>scan-build</tt> on this source file produces the following output:</p>

<img src="images/example_ns_returns_retained.png">

<h4 id="attr_cf_returns_retained">Attribute 'cf_returns_retained'
(Clang-specific)</h4>

<p>The GCC-style (Clang-specific) attribute 'cf_returns_retained' allows one to
annotate an Objective-C method or C function as returning a retained Core
Foundation object that the caller is responsible for releasing. 

<p><b>Placing on Objective-C methods</b>: With respect to Objective-C methods.,
this attribute is identical in its behavior and usage to 'ns_returns_retained'
except for the distinction of returning a Core Foundation object instead of a
Cocoa object. This distinction is important for two reasons:</p>

<ul>
  <li>Core Foundation objects are not automatically managed by the Objective-C
  garbage collector.</li>
  <li>Because Core Foundation is a C API, the analyzer cannot always tell that a
  pointer return value refers to a Core Foundation object. In contrast, it is
  trivial for the analyzer to recognize if a pointer refers to a Cocoa object
  (given the Objective-C type system).</p>
</ul>

<p><b>Placing on C functions</b>: When placing the attribute
'cf_returns_retained' on the declarations of C functions, the analyzer
interprets the function as:</p>

<ol>
  <li>Returning a Core Foundation Object</li>
  <li>Treating the function as if it its name
contained the keywords &quot;create&quot; or &quot;copy&quot;. This means the
returned object as a +1 retain count that must be released by the caller, either
by sending a <tt>release</tt> message (via toll-free bridging to an Objective-C
object pointer), calling <tt>CFRelease</tt> (or similar function), or using
<tt>CFMakeCollectable</tt> to register the object with the Objective-C garbage
collector.</li>
</ol>

<p><b>Example</b></p>

<p>In this example, observe the difference in output when the code is compiled
to not use garbage collection versus when it is compiled to only use garbage
collection (<tt>-fobjc-gc-only</tt>).</p>

<pre class="code_example">
<span class="command">$ cat test.m</span>
$ cat test.m
#import &lt;Cocoa/Cocoa.h&gt;

#ifndef CF_RETURNS_RETAINED
#if __clang__
<span class="code_highlight">#define CF_RETURNS_RETAINED __attribute__((cf_returns_retained))</span>
#else
#define CF_RETURNS_RETAINED
#endif
#endif

@interface MyClass : NSObject {}
- (NSDate*) returnsCFRetained <span class="code_highlight">CF_RETURNS_RETAINED</span>;
- (NSDate*) alsoReturnsRetained;
- (NSDate*) returnsNSRetained <span class="code_highlight">NS_RETURNS_RETAINED</span>;
@end

<span class="code_highlight">CF_RETURNS_RETAINED</span>
CFDateRef returnsRetainedCFDate()  {
  return CFDateCreate(0, CFAbsoluteTimeGetCurrent());
}

@implementation MyClass<commit_msg>Update cf_returns_retained/ns_returns_retained examples to use __has_feature.<commit_after><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
          "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
  <title>Source Annotations</title>
  <link type="text/css" rel="stylesheet" href="menu.css" />
  <link type="text/css" rel="stylesheet" href="content.css" />
</head>
<body>

<!--#include virtual="menu.html.incl"-->

<div id="content">

<h1>Source Annotations</h1>

<p>The Clang frontend supports several source-level annotations in the form of
<a href="http://gcc.gnu.org/onlinedocs/gcc/Attribute-Syntax.html">GCC-style
attributes</a> and pragmas that can help make using the Clang Static Analyzer
more useful. These annotations can both help suppress false positives as well as
enhance the analyzer's ability to find bugs.</p>

<p>This page gives a practical overview of such annotations. For more technical
specifics regarding Clang-specific annotations please see the Clang's list of <a
href="http://clang.llvm.org/docs/LanguageExtensions.html">language
extensions</a>. Details of &quot;standard&quot; GCC attributes (that Clang also
supports) can be found in the <a href="http://gcc.gnu.org/onlinedocs/gcc/">GCC
manual</a>, with the majority of the relevant attributes being in the section on
<a href="http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html">function
attributes</a>.</p>

<p>Note that attributes that are labeled <b>Clang-specific</b> are not
recognized by GCC. Their use can be conditioned using preprocessor macros
(examples included on this page).</p>

<ul>
<li><a href="#generic">Annotations to Enhance Generic Checks</a></li>
  <ul>
    <li><a href="#null_checking">Null Pointer Checking</a></li>
    <ul>
      <li><a href="#attr_nonnull">Attribute 'nonnull'</a></li>
    </ul>
  </ul>
<li><a href="#macosx">Mac OS X API Annotations</a></li>
  <ul>
    <li><a href="#cocoa_mem">Cocoa &amp; Core Foundation Memory Management
      Annotations</a></li>
    <ul>
    <li><a href="#attr_ns_returns_retained">Attribute
    'ns_returns_retained'</a></li>
    <li><a href="#attr_cf_returns_retained">Attribute
    'cf_returns_retained'</a></li>
  </ul>
  </ul>
<li><a href="#custom_assertions">Custom Assertion Handlers</a></li>
  <ul>
    <li><a href="#attr_noreturn">Attribute 'noreturn'</a></li>
    <li><a href="#attr_analyzer_noreturn">Attribute 'analyzer_noreturn'</a></li>
  </ul>
</ul>

<!-- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -->
<h2 id="generic">Annotations to Enhance Generic Checks</h2>
<!-- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -->

<h3 id="null_checking">Null Pointer Checking</h3>

<h4 id="attr_nonnull">Attribute 'nonnull'</h4>

<p>The analyzer recognizes the GCC attribute 'nonnull', which indicates that a
function expects that a given function parameter is not a null pointer. Specific
details of the syntax of using the 'nonnull' attribute can be found in <a
href="http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html#index-g_t_0040code_007bnonnull_007d-function-attribute-2263">GCC's
documentation</a>.</p>

<p>Both the Clang compiler and GCC will flag warnings for simple cases where a
null pointer is directly being passed to a function with a 'nonnull' parameter
(e.g., as a constant). The analyzer extends this checking by using its deeper
symbolic analysis to track what pointer values are potentially null and then
flag warnings when they are passed in a function call via a 'nonnull'
parameter.</p>

<p><b>Example</b></p>

<pre class="code_example">
<span class="command">$ cat test.m</span>
int bar(int*p, int q, int *r) __attribute__((nonnull(1,3)));

int foo(int *p, int *q) {
   return !p ? bar(q, 2, p) 
             : bar(p, 2, q);
}
</pre>

<p>Running <tt>scan-build</tt> over this source produces the following
output:</p>

<img src="images/example_attribute_nonnull.png">

<!-- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -->
<h2 id="macosx">Mac OS X API Annotations</h2>
<!-- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -->

<h3 id="cocoa_mem">Cocoa &amp; Core Foundation Memory Management
Annotations</h3>

<!--
<p>As described in <a href="/available_checks.html#retain_release">Available
Checks</a>,
-->
<p>The analyzer supports the proper management of retain counts for
both Cocoa and Core Foundation objects. This checking is largely based on
enforcing Cocoa and Core Foundation naming conventions for Objective-C methods
(Cocoa) and C functions (Core Foundation). Not strictly following these
conventions can cause the analyzer to miss bugs or flag false positives.</p>

<p>One can educate the analyzer (and others who read your code) about methods or
functions that deviate from the Cocoa and Core Foundation conventions using the
attributes described here.</p>

<h4 id="attr_ns_returns_retained">Attribute 'ns_returns_retained'
(Clang-specific)</h4>

<p>The GCC-style (Clang-specific) attribute 'ns_returns_retained' allows one to
annotate an Objective-C method or C function as returning a retained Cocoa
object that the caller is responsible for releasing (via sending a
<tt>release</tt> message to the object).</p>

<p><b>Placing on Objective-C methods</b>: For Objective-C methods, this
annotation essentially tells the analyzer to treat the method as if its name
begins with &quot;alloc&quot; or &quot;new&quot; or contais the word
&quot;copy&quot;.</p>

<p><b>Placing on C functions</b>: For C functions returning Cocoa objects, the
analyzer typically does not make any assumptions about whether or not the object
is returned retained. Explicitly adding the 'ns_returns_retained' attribute to C
functions allows the analyzer to perform extra checking.</p>

<p><b>Important note when using Garbage Collection</b>: Note that the analyzer
interprets this attribute slightly differently when using Objective-C garbage
collection (available on Mac OS 10.5+). When analyzing Cocoa code that uses
garbage collection, &quot;alloc&quot; methods are assumed to return an object
that is managed by the garbage collector (and thus doesn't have a retain count
the caller must balance). These same assumptions are applied to methods or
functions annotated with 'ns_returns_retained'. If you are returning a Core
Foundation object (which may not be managed by the garbage collector) you should
use 'cf_returns_retained'.</p>

<p><b>Example</b></p>

<pre class="code_example">
<span class="command">$ cat test.m</span>
#import &lt;Foundation/Foundation.h&gt;

#ifndef __has_feature      // Optional.
#define __has_feature(x) 0 // Compatibility with non-clang compilers.
#endif

#ifndef NS_RETURNS_RETAINED
#if __has_feature(attribute_ns_returns_retained)
<span class="code_highlight">#define NS_RETURNS_RETAINED __attribute__((ns_returns_retained))</span>
#else
#define NS_RETURNS_RETAINED
#endif
#endif

@interface MyClass : NSObject {}
- (NSString*) returnsRetained <span class="code_highlight">NS_RETURNS_RETAINED</span>;
- (NSString*) alsoReturnsRetained;
@end

@implementation MyClass
- (NSString*) returnsRetained {
  return [[NSString alloc] initWithCString:"no leak here"];
}
- (NSString*) alsoReturnsRetained {
  return [[NSString alloc] initWithCString:"flag a leak"];
}
@end
</pre>

<p>Running <tt>scan-build</tt> on this source file produces the following output:</p>

<img src="images/example_ns_returns_retained.png">

<h4 id="attr_cf_returns_retained">Attribute 'cf_returns_retained'
(Clang-specific)</h4>

<p>The GCC-style (Clang-specific) attribute 'cf_returns_retained' allows one to
annotate an Objective-C method or C function as returning a retained Core
Foundation object that the caller is responsible for releasing. 

<p><b>Placing on Objective-C methods</b>: With respect to Objective-C methods.,
this attribute is identical in its behavior and usage to 'ns_returns_retained'
except for the distinction of returning a Core Foundation object instead of a
Cocoa object. This distinction is important for two reasons:</p>

<ul>
  <li>Core Foundation objects are not automatically managed by the Objective-C
  garbage collector.</li>
  <li>Because Core Foundation is a C API, the analyzer cannot always tell that a
  pointer return value refers to a Core Foundation object. In contrast, it is
  trivial for the analyzer to recognize if a pointer refers to a Cocoa object
  (given the Objective-C type system).</p>
</ul>

<p><b>Placing on C functions</b>: When placing the attribute
'cf_returns_retained' on the declarations of C functions, the analyzer
interprets the function as:</p>

<ol>
  <li>Returning a Core Foundation Object</li>
  <li>Treating the function as if it its name
contained the keywords &quot;create&quot; or &quot;copy&quot;. This means the
returned object as a +1 retain count that must be released by the caller, either
by sending a <tt>release</tt> message (via toll-free bridging to an Objective-C
object pointer), calling <tt>CFRelease</tt> (or similar function), or using
<tt>CFMakeCollectable</tt> to register the object with the Objective-C garbage
collector.</li>
</ol>

<p><b>Example</b></p>

<p>In this example, observe the difference in output when the code is compiled
to not use garbage collection versus when it is compiled to only use garbage
collection (<tt>-fobjc-gc-only</tt>).</p>

<pre class="code_example">
<span class="command">$ cat test.m</span>
$ cat test.m
#import &lt;Cocoa/Cocoa.h&gt;

#ifndef __has_feature      // Optional.
#define __has_feature(x) 0 // Compatibility with non-clang compilers.
#endif

#ifndef CF_RETURNS_RETAINED
#if __has_feature(attribute_cf_returns_retained)
<span class="code_highlight">#define CF_RETURNS_RETAINED __attribute__((cf_returns_retained))</span>
#else
#define CF_RETURNS_RETAINED
#endif
#endif

@interface MyClass : NSObject {}
- (NSDate*) returnsCFRetained <span class="code_highlight">CF_RETURNS_RETAINED</span>;
- (NSDate*) alsoReturnsRetained;
- (NSDate*) returnsNSRetained <span class="code_highlight">NS_RETURNS_RETAINED</span>;
@end

<span class="code_highlight">CF_RETURNS_RETAINED</span>
CFDateRef returnsRetainedCFDate()  {
  return CFDateCreate(0, CFAbsoluteTimeGetCurrent());
}

@implementation MyClass
{'fraction_non_alphanumeric': 0.12775286497259591, 'fraction_numerical': 0.00458395615346288, 'mean_word_length': 4.468937329700273, 'pattern_counts': {'":': 0, '<': 413, '<?xml version=': 0, '>': 413, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 48, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5299319', 'n_tokens_mistral': 6493, 'n_tokens_neox': 6200, 'n_words': 2108}