• Home
  • History
  • Annotate
  • 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="#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>
32  <li><a href="#suppress_issue">How can I suppress a specific analyzer warning?</a></li>
33  <li><a href="#exclude_code">How can I selectively exclude code the analyzer examines?</a></li>
34</ol>
35
36
37<h4 id="custom_assert" class="faq">Q: How do I tell the analyzer that I do not want the bug being
38reported here since my custom error handler will safely end the execution before
39the bug is reached?</h4>
40
41<img src="images/example_custom_assert.png" alt="example custom assert">
42
43<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>
44
45<pre class="code_example">
46void customAssert() <span class="code_highlight">__attribute__((analyzer_noreturn))</span>;
47int foo(int *b) {
48  if (!b)
49    customAssert();
50  return *b;
51}</pre>
52
53
54<h4 id="null_pointer" class="faq">Q: The analyzer reports a null dereference, but I know that the
55pointer is never null. How can I tell the analyzer that a pointer can never be
56null?</h4>
57
58<img src="images/example_null_pointer.png" alt="example null pointer">
59
60<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>
61
62<pre class="code_example">
63void usePointer(int *b);
64int foo(int *b) {
65  usePointer(b);
66  return *b;
67}</pre>
68
69<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>
70
71<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:
72<pre class="code_example">Value stored to 'x' is never read</pre>
73You 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>
74
75<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>
76
77<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:
78<pre class="code_example">Instance variable 'commonName' in class 'HappyBird' is never used by the methods in its @implementation</pre>
79You can add <tt>__attribute__((unused))</tt> to the instance variable declaration to suppress the warning.</p>
80
81<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>
82
83<img src="images/example_use_assert.png" alt="example use assert">
84
85<p> In the contrived example above, the analyzer has detected that the body of
86the loop is never entered for the case where <tt>length <= 0</tt>. In this
87particular example, you may know that the loop will always be entered because
88the input parameter <tt>length</tt> will be greater than zero in all calls to this
89function. You can teach the analyzer facts about your code as well as document
90it by using assertions. By adding <tt>assert(length > 0)</tt> in the beginning
91of the function, you tell the analyzer that your code is never expecting a zero
92or a negative value, so it won't need to test the correctness of those paths.
93</p>
94
95<pre class="code_example">
96int foo(int length) {
97  int x = 0;
98  <span class="code_highlight">assert(length > 0);</span>
99  for (int i = 0; i < length; i++)
100    x += 1;
101  return length/x;
102}
103</pre>
104
105<h4 id="suppress_issue" class="faq">Q: How can I suppress a specific analyzer warning?</h4>
106
107<p>There is currently no solid mechanism for suppressing an analyzer warning,
108although this is currently being investigated. When you encounter an analyzer
109bug/false positive, check if it's one of the issues discussed above or if the
110analyzer <a href = "annotations.html#custom_assertions" >annotations</a> can
111resolve the issue. Second, please <a href = "filing_bugs.html">report it</a> to
112help us improve user experience. As the last resort, consider using <tt>__clang_analyzer__</tt> macro
113<a href = "faq.html#exclude_code" >described below</a>.</p>
114
115<h4 id="exclude_code" class="faq">Q: How can I selectively exclude code the analyzer examines?</h4>
116
117<p>When the static analyzer is using clang to parse source files, it implicitly
118defines the preprocessor macro <tt>__clang_analyzer__</tt>. One can use this
119macro to selectively exclude code the analyzer examines. Here is an example:
120
121<pre class="code_example">
122#ifndef __clang_analyzer__
123// Code not to be analyzed
124#endif
125</pre>
126
127This usage is discouraged because it makes the code dead to the analyzer from
128now on. Instead, we prefer that users file bugs against the analyzer when it flags
129false positives.
130</p>
131
132</div>
133</div>
134</body>
135</html>
136
137