blob: 084c73e4567183a3c0f477e79f59b107f959e95c [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
Andrew Williams042e0602022-01-27 13:56:2334To easily run these checks against Chromium code via clang-tidy, follow
35[these](clang_tidy.md#evaluating_running-clang_tidy-across-chromium)
36instructions to pull down `tricium_clang_tidy.py` and then pass the following
37argument when invoking the script (`-*` disables all checks and then the
38remaining check name globs enable each category of checks):
39```
40--tidy_checks="-*,clang-analyzer-core*,clang-analyzer-cplusplus*,clang-analyzer-unix*,clang-analyzer-deadcode*"
41```
42A full list of Clang analyzer checks can be found in the
43[Clang-Tidy Checks List](https://clang.llvm.org/extra/clang-tidy/checks/list.html).
44
Kevin Marshall089565ec2017-07-13 02:57:2145## Addressing false positives
46
Nico Weberdb1eca92019-03-27 17:06:1947Some of the errors you encounter will be false positives, which occurs when the
48static analyzer naively follows codepaths which are practically impossible to
49hit at runtime. Fortunately, we have a tool at our disposal for guiding the
50analyzer away from impossible codepaths: assertion handlers like
51DCHECK/CHECK/LOG(FATAL). The analyzer won't check the codepaths which we
52assert are unreachable.
Kevin Marshall089565ec2017-07-13 02:57:2153
Nico Weberdb1eca92019-03-27 17:06:1954An example would be that if the analyzer detected the function argument
55`*my_ptr` might be null and dereferencing it would potentially segfault, you
56would see the error `warning: Dereference of null pointer (loaded from variable
57'my_ptr')`. If you know for a fact that my_ptr will not be null in practice,
58then you can place an assert at the top of the function: `DCHECK(my_ptr)`. The
59analyzer will no longer generate the warning.
Kevin Marshall089565ec2017-07-13 02:57:2160
61Be mindful about only specifying assertions which are factually correct! Don't
62DCHECK recklessly just to quiet down the analyzer. :)
63
64Other types of false positives and their suppressions:
65* Unreachable code paths. To suppress, add the `ANALYZER_SKIP_THIS_PATH();`
66 directive to the relevant code block.
Avi Drissman44d8af452022-01-25 19:08:2067* Dead stores. To suppress, use `[[maybe_unused]]`. This also suppresses dead
68 store warnings on conventional builds without static analysis enabled!
Kevin Marshall089565ec2017-07-13 02:57:2169
Nico Weberdb1eca92019-03-27 17:06:1970See the definitions of the `ANALYZER_*` macros in base/logging.h for more
Kevin Marshall089565ec2017-07-13 02:57:2171detailed information about how the annotations are implemented.
72
73## Logging bugs
74
75If you find any issues with the static analyzer, or find Chromium code behaving
76badly with the analyzer, please check the `Infra>CodeAnalysis` CrBug component
77to look for known issues, or file a bug if it is a new problem.