rdevlin.cronin | be2898eb | 2016-07-13 01:20:36 | [diff] [blame] | 1 | # Clang Tidy |
| 2 | |
| 3 | [TOC] |
| 4 | |
| 5 | ## Danger, Will Robinson! |
| 6 | |
| 7 | Support for `clang-tidy` in Chromium is very experimental, and is somewhat |
| 8 | painful to use. We are exploring making it easier and integrating with existing |
| 9 | tools, but aren't there yet. If you don't want to wait and enjoy tinkering, |
| 10 | forge 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++ |
| 15 | “linter” tool. Its purpose is to provide an extensible framework for diagnosing |
| 16 | and fixing typical programming errors, like style violations, interface misuse, |
| 17 | or bugs that can be deduced via static analysis. |
| 18 | |
| 19 | ## Setting Up |
| 20 | |
Daniel McArdle | b26068f | 2019-03-07 16:29:32 | [diff] [blame^] | 21 | ### Automatic Setup |
| 22 | |
| 23 | The script [clang_tidy_tool.py](../tools/clang/scripts/clang_tidy_tool.py) will |
| 24 | automatically fetch, build, and invoke `clang-tidy`. To do this manually, follow |
| 25 | the steps in the next section. |
| 26 | |
| 27 | ### Manual Setup |
| 28 | |
rdevlin.cronin | be2898eb | 2016-07-13 01:20:36 | [diff] [blame] | 29 | In addition to a full Chromium checkout, you need the clang-tidy binary. We |
| 30 | recommend checking llvm's clang source and building the clang-tidy binary |
| 31 | directly. Instructions for getting started with clang are available from |
| 32 | [llvm](http://clang.llvm.org/get_started.html). You'll need to get llvm, |
| 33 | clang, and the extra clang tools (you won't need Compiler-RT or libcxx). |
| 34 | If you don't have it, you'll also need to install cmake as a part of this |
| 35 | process. |
| 36 | |
| 37 | Instead of building with `"Unix Makefiles"`, generate build files for Ninja with |
| 38 | ``` |
Daniel McArdle | b26068f | 2019-03-07 16:29:32 | [diff] [blame^] | 39 | cmake -GNinja \ |
| 40 | -DLLVM_ENABLE_PROJECTS=clang;clang-tools-extra \ |
| 41 | -DCMAKE_BUILD_TYPE=Release \ |
| 42 | ../llvm |
rdevlin.cronin | be2898eb | 2016-07-13 01:20:36 | [diff] [blame] | 43 | ``` |
| 44 | |
| 45 | Then, instead of using `make`, use ninja to build the clang-tidy binary with |
| 46 | ``` |
| 47 | ninja clang-tidy |
| 48 | ``` |
| 49 | |
| 50 | This binary will be at (build)/bin/clang-tidy. |
| 51 | |
| 52 | If you intend to use the `fix` feature of clang-tidy, you'll also need to build |
| 53 | the `clang-apply-replacements` binary. |
| 54 | ``` |
| 55 | ninja clang-apply-replacements |
| 56 | ``` |
| 57 | |
| 58 | ## Running clang-tidy |
| 59 | |
| 60 | Running clang-tidy is (hopefully) simple. |
jdoerrie | a1f7d04 | 2018-10-01 16:54:44 | [diff] [blame] | 61 | 1. Build chrome normally.\* Note that [Jumbo builds](jumbo.md) are not |
| 62 | supported. |
rdevlin.cronin | be2898eb | 2016-07-13 01:20:36 | [diff] [blame] | 63 | ``` |
| 64 | ninja -C out/Release chrome |
| 65 | ``` |
| 66 | 2. Generate the compilation database |
| 67 | ``` |
Takuto Ikuta | 2800dd8 | 2018-04-24 09:19:37 | [diff] [blame] | 68 | tools/clang/scripts/generate_compdb.py -p out/Release > out/Release/compile_commands.json |
rdevlin.cronin | be2898eb | 2016-07-13 01:20:36 | [diff] [blame] | 69 | ``` |
| 70 | 3. Enter the build directory. |
| 71 | ``` |
| 72 | cd out/Release |
| 73 | ``` |
| 74 | 4. Run clang-tidy. |
| 75 | ``` |
treib | 52d7e48 | 2016-09-26 21:46:35 | [diff] [blame] | 76 | <PATH_TO_LLVM_SRC>/tools/clang/tools/extra/clang-tidy/tool/run-clang-tidy.py \ |
Takuto Ikuta | 2800dd8 | 2018-04-24 09:19:37 | [diff] [blame] | 77 | -p . \# Set the root project directory, where compile_commands.json is. |
rdevlin.cronin | be2898eb | 2016-07-13 01:20:36 | [diff] [blame] | 78 | # Set the clang-tidy binary path, if it's not in your $PATH. |
| 79 | -clang-tidy-binary <PATH_TO_LLVM_BUILD>/bin/clang-tidy \ |
| 80 | # Set the clang-apply-replacements binary path, if it's not in your $PATH |
| 81 | # and you are using the `fix` behavior of clang-tidy. |
| 82 | -clang-apply-replacements-binary \ |
| 83 | <PATH_TO_LLVM_BUILD>/bin/clang-apply-replacements \ |
| 84 | # The checks to employ in the build. Use `-*` to omit default checks. |
| 85 | -checks=<CHECKS> \ |
| 86 | -header-filter=<FILTER> \# Optional, limit results to only certain files. |
| 87 | -fix \# Optional, used if you want to have clang-tidy auto-fix errors. |
| 88 | chrome/browser # The path to the files you want to check. |
| 89 | |
| 90 | Copy-Paste Friendly (though you'll still need to stub in the variables): |
treib | 52d7e48 | 2016-09-26 21:46:35 | [diff] [blame] | 91 | <PATH_TO_LLVM_SRC>/tools/clang/tools/extra/clang-tidy/tool/run-clang-tidy.py \ |
Takuto Ikuta | 2800dd8 | 2018-04-24 09:19:37 | [diff] [blame] | 92 | -p . \ |
rdevlin.cronin | be2898eb | 2016-07-13 01:20:36 | [diff] [blame] | 93 | -clang-tidy-binary <PATH_TO_LLVM_BUILD>/bin/clang-tidy \ |
| 94 | -clang-apply-replacements-binary \ |
| 95 | <PATH_TO_LLVM_BUILD>/bin/clang-apply-replacements \ |
| 96 | -checks=<CHECKS> \ |
| 97 | -header-filter=<FILTER> \ |
| 98 | -fix \ |
| 99 | chrome/browser |
| 100 | ``` |
| 101 | |
jdoerrie | a1f7d04 | 2018-10-01 16:54:44 | [diff] [blame] | 102 | \*It's not clear which, if any, `gn` flags outside of `use_jumbo_build` may |
| 103 | cause issues for `clang-tidy`. I've had no problems building a component release |
| 104 | build, both with and without goma. if you run into issues, let us know! |
rdevlin.cronin | be2898eb | 2016-07-13 01:20:36 | [diff] [blame] | 105 | |
rdevlin.cronin | be2898eb | 2016-07-13 01:20:36 | [diff] [blame] | 106 | ## Questions |
| 107 | |
| 108 | Questions? Reach out to rdevlin.cronin@chromium.org or thakis@chromium.org. |
| 109 | Discoveries? Update the doc! |