blob: 72e3d5c329f9e540dfd83b68b037132ea26fac9c [file] [log] [blame] [view]
rdevlin.croninbe2898eb2016-07-13 01:20:361# Clang Tidy
2
3[TOC]
4
5## Danger, Will Robinson!
6
7Support for `clang-tidy` in Chromium is very experimental, and is somewhat
8painful to use. We are exploring making it easier and integrating with existing
9tools, but aren't there yet. If you don't want to wait and enjoy tinkering,
10forge ahead. Otherwise, feel free to turn back now.
11
12## Introduction
13
14[clang-tidy](http://clang.llvm.org/extra/clang-tidy/) is a clang-based C++
15linter tool. Its purpose is to provide an extensible framework for diagnosing
16and fixing typical programming errors, like style violations, interface misuse,
17or bugs that can be deduced via static analysis.
18
19## Setting Up
20
Daniel McArdleb26068f2019-03-07 16:29:3221### Automatic Setup
22
23The script [clang_tidy_tool.py](../tools/clang/scripts/clang_tidy_tool.py) will
24automatically fetch, build, and invoke `clang-tidy`. To do this manually, follow
25the steps in the next section.
26
27### Manual Setup
28
rdevlin.croninbe2898eb2016-07-13 01:20:3629In addition to a full Chromium checkout, you need the clang-tidy binary. We
30recommend checking llvm's clang source and building the clang-tidy binary
31directly. Instructions for getting started with clang are available from
32[llvm](http://clang.llvm.org/get_started.html). You'll need to get llvm,
33clang, and the extra clang tools (you won't need Compiler-RT or libcxx).
Raul Tambree1c88012019-03-11 15:28:2034If you don't have it, you'll also need to install CMake as a part of this
rdevlin.croninbe2898eb2016-07-13 01:20:3635process.
36
37Instead of building with `"Unix Makefiles"`, generate build files for Ninja with
38```
Daniel McArdleb26068f2019-03-07 16:29:3239cmake -GNinja \
40 -DLLVM_ENABLE_PROJECTS=clang;clang-tools-extra \
41 -DCMAKE_BUILD_TYPE=Release \
42 ../llvm
rdevlin.croninbe2898eb2016-07-13 01:20:3643```
44
45Then, instead of using `make`, use ninja to build the clang-tidy binary with
46```
47ninja clang-tidy
48```
49
50This binary will be at (build)/bin/clang-tidy.
51
52If you intend to use the `fix` feature of clang-tidy, you'll also need to build
53the `clang-apply-replacements` binary.
54```
55ninja clang-apply-replacements
56```
57
58## Running clang-tidy
59
60Running clang-tidy is (hopefully) simple.
Dirk Pranke6e1ce9f2019-10-29 22:53:53611. Build chrome normally.
rdevlin.croninbe2898eb2016-07-13 01:20:3662```
63ninja -C out/Release chrome
64```
652. Generate the compilation database
66```
Takuto Ikuta2800dd82018-04-24 09:19:3767tools/clang/scripts/generate_compdb.py -p out/Release > out/Release/compile_commands.json
rdevlin.croninbe2898eb2016-07-13 01:20:3668```
693. Enter the build directory.
70```
71cd out/Release
72```
734. Run clang-tidy.
74```
Raul Tambree1c88012019-03-11 15:28:2075<PATH_TO_LLVM_SRC>/clang-tools-extra/clang-tidy/tool/run-clang-tidy.py \
Takuto Ikuta2800dd82018-04-24 09:19:3776 -p . \# Set the root project directory, where compile_commands.json is.
rdevlin.croninbe2898eb2016-07-13 01:20:3677 # Set the clang-tidy binary path, if it's not in your $PATH.
78 -clang-tidy-binary <PATH_TO_LLVM_BUILD>/bin/clang-tidy \
79 # Set the clang-apply-replacements binary path, if it's not in your $PATH
80 # and you are using the `fix` behavior of clang-tidy.
81 -clang-apply-replacements-binary \
82 <PATH_TO_LLVM_BUILD>/bin/clang-apply-replacements \
83 # The checks to employ in the build. Use `-*` to omit default checks.
84 -checks=<CHECKS> \
85 -header-filter=<FILTER> \# Optional, limit results to only certain files.
86 -fix \# Optional, used if you want to have clang-tidy auto-fix errors.
87 chrome/browser # The path to the files you want to check.
88
89Copy-Paste Friendly (though you'll still need to stub in the variables):
Raul Tambree1c88012019-03-11 15:28:2090<PATH_TO_LLVM_SRC>/clang-tools-extra/clang-tidy/tool/run-clang-tidy.py \
Takuto Ikuta2800dd82018-04-24 09:19:3791 -p . \
rdevlin.croninbe2898eb2016-07-13 01:20:3692 -clang-tidy-binary <PATH_TO_LLVM_BUILD>/bin/clang-tidy \
93 -clang-apply-replacements-binary \
94 <PATH_TO_LLVM_BUILD>/bin/clang-apply-replacements \
95 -checks=<CHECKS> \
96 -header-filter=<FILTER> \
97 -fix \
98 chrome/browser
99```
100
Dirk Pranke6e1ce9f2019-10-29 22:53:53101\*It's not clear which, if any, `gn` flags may cause issues for
102`clang-tidy`. I've had no problems building a component release build,
103both with and without goma. if you run into issues, let us know!
rdevlin.croninbe2898eb2016-07-13 01:20:36104
rdevlin.croninbe2898eb2016-07-13 01:20:36105## Questions
106
107Questions? Reach out to rdevlin.cronin@chromium.org or thakis@chromium.org.
108Discoveries? Update the doc!