blob: 988fdc7c0f7b42a1c66df9698e92947561c75f95 [file] [log] [blame] [view]
andybons222c4ee2015-08-25 16:51:031# The Clang Static Analyzer
andybons3322f762015-08-24 21:37:092
Kevin Marshall089565ec2017-07-13 02:57:213The Clang C/C++ compiler comes with a static analyzer which can be used to find
4bugs using path sensitive analysis. Path sensitive analysis is
5a technique that explores all the possible branches in code and
6records the codepaths that might lead to bad or undefined behavior,
7like an uninitialized reads, use after frees, pointer leaks, and so on.
andybons3322f762015-08-24 21:37:098
Kevin Marshall089565ec2017-07-13 02:57:219See the [official Clang static analyzer page](http://clang-analyzer.llvm.org/)
10for more background information.
11
Nico Weberdb1eca92019-03-27 17:06:1912We used to have a bot that continuously ran with the static analyzer,
13but people used to not look at it much.
Kevin Marshall089565ec2017-07-13 02:57:2114
Nico Weberdb1eca92019-03-27 17:06:1915The static analyzer can still be invoked with [clang-tidy](clang_tidy.md).
Kevin Marshall089565ec2017-07-13 02:57:2116
Nico Weberdb1eca92019-03-27 17:06:1917## Recommended checks
18Clang's static analyzer comes with a wide variety of checkers. Some of the
19checks aren't useful because they are intended for different languages,
20platforms, or coding conventions than the ones used for Chromium development.
Kevin Marshall089565ec2017-07-13 02:57:2121
Nico Weberdb1eca92019-03-27 17:06:1922Checkers we found useful were:
Daniel McArdleb26068f2019-03-07 16:29:3223
Nico Weberdb1eca92019-03-27 17:06:1924 -analyzer-checker=core
25 -analyzer-checker=cpp
26 -analyzer-checker=unix
27 -analyzer-checker=deadcode
Kevin Marshall089565ec2017-07-13 02:57:2128
29As of this writing, the checker suites we support are
30[core](https://clang-analyzer.llvm.org/available_checks.html#core_checkers),
31[cplusplus](https://clang-analyzer.llvm.org/available_checks.html#cplusplus_checkers), and
32[deadcode](https://clang-analyzer.llvm.org/available_checks.html#deadcode_checkers).
33
Kevin Marshall089565ec2017-07-13 02:57:2134## Addressing false positives
35
Nico Weberdb1eca92019-03-27 17:06:1936Some of the errors you encounter will be false positives, which occurs when the
37static analyzer naively follows codepaths which are practically impossible to
38hit at runtime. Fortunately, we have a tool at our disposal for guiding the
39analyzer away from impossible codepaths: assertion handlers like
40DCHECK/CHECK/LOG(FATAL). The analyzer won't check the codepaths which we
41assert are unreachable.
Kevin Marshall089565ec2017-07-13 02:57:2142
Nico Weberdb1eca92019-03-27 17:06:1943An example would be that if the analyzer detected the function argument
44`*my_ptr` might be null and dereferencing it would potentially segfault, you
45would see the error `warning: Dereference of null pointer (loaded from variable
46'my_ptr')`. If you know for a fact that my_ptr will not be null in practice,
47then you can place an assert at the top of the function: `DCHECK(my_ptr)`. The
48analyzer will no longer generate the warning.
Kevin Marshall089565ec2017-07-13 02:57:2149
50Be mindful about only specifying assertions which are factually correct! Don't
51DCHECK recklessly just to quiet down the analyzer. :)
52
53Other types of false positives and their suppressions:
54* Unreachable code paths. To suppress, add the `ANALYZER_SKIP_THIS_PATH();`
55 directive to the relevant code block.
Avi Drissman44d8af452022-01-25 19:08:2056* Dead stores. To suppress, use `[[maybe_unused]]`. This also suppresses dead
57 store warnings on conventional builds without static analysis enabled!
Kevin Marshall089565ec2017-07-13 02:57:2158
Nico Weberdb1eca92019-03-27 17:06:1959See the definitions of the `ANALYZER_*` macros in base/logging.h for more
Kevin Marshall089565ec2017-07-13 02:57:2160detailed information about how the annotations are implemented.
61
62## Logging bugs
63
64If you find any issues with the static analyzer, or find Chromium code behaving
65badly with the analyzer, please check the `Infra>CodeAnalysis` CrBug component
66to look for known issues, or file a bug if it is a new problem.