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 | |
| 21 | In addition to a full Chromium checkout, you need the clang-tidy binary. We |
| 22 | recommend checking llvm's clang source and building the clang-tidy binary |
| 23 | directly. Instructions for getting started with clang are available from |
| 24 | [llvm](http://clang.llvm.org/get_started.html). You'll need to get llvm, |
| 25 | clang, and the extra clang tools (you won't need Compiler-RT or libcxx). |
| 26 | If you don't have it, you'll also need to install cmake as a part of this |
| 27 | process. |
| 28 | |
| 29 | Instead of building with `"Unix Makefiles"`, generate build files for Ninja with |
| 30 | ``` |
Chris Watkins | e4ffe1e2 | 2017-11-24 00:48:48 | [diff] [blame] | 31 | cmake -GNinja -DCMAKE_BUILD_TYPE=Release ../llvm |
rdevlin.cronin | be2898eb | 2016-07-13 01:20:36 | [diff] [blame] | 32 | ``` |
| 33 | |
| 34 | Then, instead of using `make`, use ninja to build the clang-tidy binary with |
| 35 | ``` |
| 36 | ninja clang-tidy |
| 37 | ``` |
| 38 | |
| 39 | This binary will be at (build)/bin/clang-tidy. |
| 40 | |
| 41 | If you intend to use the `fix` feature of clang-tidy, you'll also need to build |
| 42 | the `clang-apply-replacements` binary. |
| 43 | ``` |
| 44 | ninja clang-apply-replacements |
| 45 | ``` |
| 46 | |
| 47 | ## Running clang-tidy |
| 48 | |
| 49 | Running clang-tidy is (hopefully) simple. |
| 50 | 1. Build chrome normally.\* |
| 51 | ``` |
| 52 | ninja -C out/Release chrome |
| 53 | ``` |
| 54 | 2. Generate the compilation database |
| 55 | ``` |
Takuto Ikuta | 2800dd8 | 2018-04-24 09:19:37 | [diff] [blame^] | 56 | 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] | 57 | ``` |
| 58 | 3. Enter the build directory. |
| 59 | ``` |
| 60 | cd out/Release |
| 61 | ``` |
| 62 | 4. Run clang-tidy. |
| 63 | ``` |
treib | 52d7e48 | 2016-09-26 21:46:35 | [diff] [blame] | 64 | <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^] | 65 | -p . \# Set the root project directory, where compile_commands.json is. |
rdevlin.cronin | be2898eb | 2016-07-13 01:20:36 | [diff] [blame] | 66 | # Set the clang-tidy binary path, if it's not in your $PATH. |
| 67 | -clang-tidy-binary <PATH_TO_LLVM_BUILD>/bin/clang-tidy \ |
| 68 | # Set the clang-apply-replacements binary path, if it's not in your $PATH |
| 69 | # and you are using the `fix` behavior of clang-tidy. |
| 70 | -clang-apply-replacements-binary \ |
| 71 | <PATH_TO_LLVM_BUILD>/bin/clang-apply-replacements \ |
| 72 | # The checks to employ in the build. Use `-*` to omit default checks. |
| 73 | -checks=<CHECKS> \ |
| 74 | -header-filter=<FILTER> \# Optional, limit results to only certain files. |
| 75 | -fix \# Optional, used if you want to have clang-tidy auto-fix errors. |
| 76 | chrome/browser # The path to the files you want to check. |
| 77 | |
| 78 | Copy-Paste Friendly (though you'll still need to stub in the variables): |
treib | 52d7e48 | 2016-09-26 21:46:35 | [diff] [blame] | 79 | <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^] | 80 | -p . \ |
rdevlin.cronin | be2898eb | 2016-07-13 01:20:36 | [diff] [blame] | 81 | -clang-tidy-binary <PATH_TO_LLVM_BUILD>/bin/clang-tidy \ |
| 82 | -clang-apply-replacements-binary \ |
| 83 | <PATH_TO_LLVM_BUILD>/bin/clang-apply-replacements \ |
| 84 | -checks=<CHECKS> \ |
| 85 | -header-filter=<FILTER> \ |
| 86 | -fix \ |
| 87 | chrome/browser |
| 88 | ``` |
| 89 | |
| 90 | \*It's not clear which, if any, `gn` flags may cause issues for `clang-tidy`. |
| 91 | I've had no problems building a component release build, both with and without |
| 92 | goma. if you run into issues, let us know! |
| 93 | |
rdevlin.cronin | be2898eb | 2016-07-13 01:20:36 | [diff] [blame] | 94 | ## Questions |
| 95 | |
| 96 | Questions? Reach out to rdevlin.cronin@chromium.org or thakis@chromium.org. |
| 97 | Discoveries? Update the doc! |