• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
2          "http://www.w3.org/TR/html4/strict.dtd">
3<html>
4<head>
5  <title>FAQ and How to Deal with Common False Positives</title>
6  <link type="text/css" rel="stylesheet" href="menu.css">
7  <link type="text/css" rel="stylesheet" href="content.css">
8  <script type="text/javascript" src="scripts/menu.js"></script>
9  <style type="text/css">
10    tr:first-child { width:20%; }
11  </style>
12</head>
13<body>
14
15<div id="page">
16<!--#include virtual="menu.html.incl"-->
17
18<div id="content">
19
20<h1>FAQ and How to Deal with Common False Positives</h1>
21
22<ol>
23  <li><a href="#custom_assert">How do I tell the analyzer that I do not want the bug being
24reported here since my custom error handler will safely end the execution before
25the bug is reached?</a></li>
26  <li><a href="#null_pointer">The analyzer reports a null dereference, but I know that the
27pointer is never null. How can I tell the analyzer that a pointer can never be
28null?</a></li>
29  <li><a href="#dead_store">How do I tell the static analyzer that I don't care about a specific dead store?</a></li>
30  <li><a href="#unused_ivar">How do I tell the static analyzer that I don't care about a specific unused instance variable in Objective C?</a></li>
31  <li><a href="#unlocalized_string">How do I tell the static analyzer that I don't care about a specific unlocalized string?</a></li>
32  <li><a href="#use_assert">The analyzer assumes that a loop body is never entered.  How can I tell it that the loop body will be entered at least once?</a></li>
33  <li><a href="#suppress_issue">How can I suppress a specific analyzer warning?</a></li>
34  <li><a href="#exclude_code">How can I selectively exclude code the analyzer examines?</a></li>
35</ol>
36
37
38<h4 id="custom_assert" class="faq">Q: How do I tell the analyzer that I do not want the bug being
39reported here since my custom error handler will safely end the execution before
40the bug is reached?</h4>
41
42<img src="images/example_custom_assert.png" alt="example custom assert">
43
44<p>You can tell the analyzer that this path is unreachable by teaching it about your <a href = "annotations.html#custom_assertions" >custom assertion handlers</a>. For example, you can modify the code segment as following.</p>
45
46<pre class="code_example">
47void customAssert() <span class="code_highlight">__attribute__((analyzer_noreturn))</span>;
48int foo(int *b) {
49  if (!b)
50    customAssert();
51  return *b;
52}</pre>
53
54
55<h4 id="null_pointer" class="faq">Q: The analyzer reports a null dereference, but I know that the
56pointer is never null. How can I tell the analyzer that a pointer can never be
57null?</h4>
58
59<img src="images/example_null_pointer.png" alt="example null pointer">
60
61<p>The reason the analyzer often thinks that a pointer can be null is because the preceding code checked compared it against null. So if you are absolutely sure that it cannot be null, remove the preceding check and, preferably, add an assertion as well. For example, in the code segment above, it will be sufficient to remove the <tt>if (!b)</tt> check. </p>
62
63<pre class="code_example">
64void usePointer(int *b);
65int foo(int *b) {
66  usePointer(b);
67  return *b;
68}</pre>
69
70<h4 id="dead_store" class="faq">Q: How do I tell the static analyzer that I don't care about a specific dead store?</h4>
71
72<p>When the analyzer sees that a value stored into a variable is never used, it's going to produce a message similar to this one:
73<pre class="code_example">Value stored to 'x' is never read</pre>
74You can use the <tt>(void)x;</tt> idiom to acknowledge that there is a dead store in your code but you do not want it to be reported in the future.</p>
75
76<h4 id="unused_ivar" class="faq">Q: How do I tell the static analyzer that I don't care about a specific unused instance variable in Objective C?</h4>
77
78<p>When the analyzer sees that a value stored into a variable is never used, it is going to produce a message similar to this one:
79<pre class="code_example">Instance variable 'commonName' in class 'HappyBird' is never used by the methods in its @implementation</pre>
80You can add <tt>__attribute__((unused))</tt> to the instance variable declaration to suppress the warning.</p>
81
82<h4 id="unlocalized_string" class="faq">Q: How do I tell the static analyzer that I don't care about a specific unlocalized string?</h4>
83
84<p>When the analyzer sees that an unlocalized string is passed to a method that will present that string to the user, it is going to produce a message similar to this one:
85<pre class="code_example">User-facing text should use localized string macro</pre>
86
87If your project deliberately uses unlocalized user-facing strings (for example, in a debugging UI that is never shown to users), you can suppress the analyzer warnings (and document your intent) with a function that just returns its input but is annotated to return a localized string:
88<pre class="code_example">
89__attribute__((annotate("returns_localized_nsstring")))
90static inline NSString *LocalizationNotNeeded(NSString *s) {
91  return s;
92}
93</pre>
94
95You can then call this function when creating your debugging UI:
96<pre class="code_example">
97[field setStringValue:LocalizationNotNeeded(@"Debug")];
98</pre>
99
100Some projects may also find it useful to use NSLocalizedString but add "DNL" or "Do Not Localize" to the string contents as a convention:
101<pre class="code_example">
102UILabel *testLabel = [[UILabel alloc] init];
103NSString *s = NSLocalizedString(@"Hello &lt;Do Not Localize&gt;", @"For debug purposes");
104[testLabel setText:s];
105</pre>
106</p>
107
108<h4 id="use_assert" class="faq">Q: The analyzer assumes that a loop body is never entered.  How can I tell it that the loop body will be entered at least once?</h4>
109
110<img src="images/example_use_assert.png" alt="example use assert">
111
112<p> In the contrived example above, the analyzer has detected that the body of
113the loop is never entered for the case where <tt>length <= 0</tt>. In this
114particular example, you may know that the loop will always be entered because
115the input parameter <tt>length</tt> will be greater than zero in all calls to this
116function. You can teach the analyzer facts about your code as well as document
117it by using assertions. By adding <tt>assert(length > 0)</tt> in the beginning
118of the function, you tell the analyzer that your code is never expecting a zero
119or a negative value, so it won't need to test the correctness of those paths.
120</p>
121
122<pre class="code_example">
123int foo(int length) {
124  int x = 0;
125  <span class="code_highlight">assert(length > 0);</span>
126  for (int i = 0; i < length; i++)
127    x += 1;
128  return length/x;
129}
130</pre>
131
132<h4 id="suppress_issue" class="faq">Q: How can I suppress a specific analyzer warning?</h4>
133
134<p>There is currently no solid mechanism for suppressing an analyzer warning,
135although this is currently being investigated. When you encounter an analyzer
136bug/false positive, check if it's one of the issues discussed above or if the
137analyzer <a href = "annotations.html#custom_assertions" >annotations</a> can
138resolve the issue. Second, please <a href = "filing_bugs.html">report it</a> to
139help us improve user experience. As the last resort, consider using <tt>__clang_analyzer__</tt> macro
140<a href = "faq.html#exclude_code" >described below</a>.</p>
141
142<h4 id="exclude_code" class="faq">Q: How can I selectively exclude code the analyzer examines?</h4>
143
144<p>When the static analyzer is using clang to parse source files, it implicitly
145defines the preprocessor macro <tt>__clang_analyzer__</tt>. One can use this
146macro to selectively exclude code the analyzer examines. Here is an example:
147
148<pre class="code_example">
149#ifndef __clang_analyzer__
150// Code not to be analyzed
151#endif
152</pre>
153
154This usage is discouraged because it makes the code dead to the analyzer from
155now on. Instead, we prefer that users file bugs against the analyzer when it flags
156false positives.
157</p>
158
159</div>
160</div>
161</body>
162</html>
163
164