1 <!DOCTYPE HTML PUBLIC
"-//W3C//DTD HTML 4.01//EN"
2 "http://www.w3.org/TR/html4/strict.dtd">
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%; }
16 <!--#include virtual="menu.html.incl"-->
20 <h1>FAQ and How to Deal with Common False Positives
</h1>
23 <li><a href=
"#custom_assert">How do I tell the analyzer that I do not want the bug being
24 reported here since my custom error handler will safely end the execution before
25 the bug is reached?
</a></li>
26 <li><a href=
"#null_pointer">The analyzer reports a null dereference, but I know that the
27 pointer is never null. How can I tell the analyzer that a pointer can never be
29 <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>
30 <li><a href=
"#suppress_issue">How can I suppress a specific analyzer warning?
</a></li>
31 <li><a href=
"#exclude_code">How can I selectively exclude code the analyzer examines?
</a></li>
35 <h4 id=
"custom_assert" class=
"faq">Q: How do I tell the analyzer that I do not want the bug being
36 reported here since my custom error handler will safely end the execution before
37 the bug is reached?
</h4>
39 <img src=
"images/example_custom_assert.png" alt=
"example custom assert">
41 <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>
43 <pre class=
"code_example">
44 void customAssert()
<span class=
"code_highlight">__attribute__((analyzer_noreturn))
</span>;
52 <h4 id=
"null_pointer" class=
"faq">Q: The analyzer reports a null dereference, but I know that the
53 pointer is never null. How can I tell the analyzer that a pointer can never be
56 <img src=
"images/example_null_pointer.png" alt=
"example null pointer">
58 <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>
60 <pre class=
"code_example">
61 void usePointer(int *b);
67 <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>
69 <img src=
"images/example_use_assert.png" alt=
"example use assert">
71 <p> In the contrived example above, the analyzer has detected that the body of
72 the loop is never entered for the case where
<tt>length <=
0</tt>. In this
73 particular example, you may know that the loop will always be entered because
74 the input parameter
<tt>length
</tt> will be greater than zero in all calls to this
75 function. You can teach the analyzer facts about your code as well as document
76 it by using assertions. By adding
<tt>assert(length
> 0)
</tt> in the beginning
77 of the function, you tell the analyzer that your code is never expecting a zero
78 or a negative value, so it won't need to test the correctness of those paths.
81 <pre class=
"code_example">
84 <span class=
"code_highlight">assert(length
> 0);
</span>
85 for (int i =
0; i < length; i++)
91 <h4 id=
"suppress_issue" class=
"faq">Q: How can I suppress a specific analyzer warning?
</h4>
93 <p>There is currently no solid mechanism for suppressing an analyzer warning,
94 although this is currently being investigated. When you encounter an analyzer
95 bug/false positive, check if it's one of the issues discussed above or if the
96 analyzer
<a href =
"annotations.html#custom_assertions" >annotations
</a> can
97 resolve the issue. Second, please
<a href =
"filing_bugs.html">report it
</a> to
98 help us improve user experience. As the last resort, consider using
<tt>__clang_analyzer__
</tt> macro
99 <a href =
"faq.html#exclude_code" >described below
</a>.
</p>
101 <h4 id=
"exclude_code" class=
"faq">Q: How can I selectively exclude code the analyzer examines?
</h4>
103 <p>When the static analyzer is using clang to parse source files, it implicitly
104 defines the preprocessor macro
<tt>__clang_analyzer__
</tt>. One can use this
105 macro to selectively exclude code the analyzer examines. Here is an example:
107 <pre class=
"code_example">
108 #ifndef __clang_analyzer__
109 // Code not to be analyzed
113 This usage is discouraged because it makes the code dead to the analyzer from
114 now on. Instead, we prefer that users file bugs against the analyzer when it flags