andybons | 6eaa0c0d | 2015-08-26 20:12:52 | [diff] [blame] | 1 | # Clang Tool Refactoring |
andybons | 3322f76 | 2015-08-24 21:37:09 | [diff] [blame] | 2 | |
andybons | 6eaa0c0d | 2015-08-26 20:12:52 | [diff] [blame] | 3 | [TOC] |
andybons | 3322f76 | 2015-08-24 21:37:09 | [diff] [blame] | 4 | |
dcheng | ce2375e | 2016-01-12 01:09:07 | [diff] [blame] | 5 | ## Introduction |
| 6 | |
| 7 | Clang tools can help with global refactorings of Chromium code. Clang tools can |
| 8 | take advantage of clang's AST to perform refactorings that would be impossible |
| 9 | with a traditional find-and-replace regexp: |
| 10 | |
| 11 | * Constructing `scoped_ptr<T>` from `NULL`: <https://crbug.com/173286> |
| 12 | * Implicit conversions of `scoped_refptr<T>` to `T*`: <https://crbug.com/110610> |
| 13 | * Rename everything in Blink to follow Chromium style: <https://crbug.com/563793> |
| 14 | |
andybons | 6eaa0c0d | 2015-08-26 20:12:52 | [diff] [blame] | 15 | ## Caveats |
andybons | 3322f76 | 2015-08-24 21:37:09 | [diff] [blame] | 16 | |
dcheng | ce2375e | 2016-01-12 01:09:07 | [diff] [blame] | 17 | An invocation of the clang tool runs on one build config. Code that only |
| 18 | compiles on one platform or code that is guarded by a set of compile-time flags |
| 19 | can be problematic. Performing a global refactoring typically requires running |
| 20 | the tool once in each build config with code that needs to be updated. |
| 21 | |
| 22 | Other minor issues: |
| 23 | |
| 24 | * Requires a git checkout. |
| 25 | * Requires [some hacks to run on Windows](https://codereview.chromium.org/718873004). |
andybons | 6eaa0c0d | 2015-08-26 20:12:52 | [diff] [blame] | 26 | |
| 27 | ## Prerequisites |
| 28 | |
dcheng | ce2375e | 2016-01-12 01:09:07 | [diff] [blame] | 29 | A Chromium checkout created with `fetch` should have everything needed. |
andybons | 6eaa0c0d | 2015-08-26 20:12:52 | [diff] [blame] | 30 | |
dcheng | ce2375e | 2016-01-12 01:09:07 | [diff] [blame] | 31 | For convenience, add `third_party/llvm-build/Release+Asserts/bin` to `$PATH`. |
andybons | 6eaa0c0d | 2015-08-26 20:12:52 | [diff] [blame] | 32 | |
dcheng | ce2375e | 2016-01-12 01:09:07 | [diff] [blame] | 33 | ## Writing the tool |
andybons | 6eaa0c0d | 2015-08-26 20:12:52 | [diff] [blame] | 34 | |
dcheng | ce2375e | 2016-01-12 01:09:07 | [diff] [blame] | 35 | LLVM uses C++11 and CMake. Source code for Chromium clang tools lives in |
| 36 | [//tools/clang](https://chromium.googlesource.com/chromium/src/tools/clang/+/master). |
| 37 | It is generally easiest to use one of the already-written tools as the base for |
| 38 | writing a new tool. |
andybons | 6eaa0c0d | 2015-08-26 20:12:52 | [diff] [blame] | 39 | |
dcheng | ce2375e | 2016-01-12 01:09:07 | [diff] [blame] | 40 | Chromium clang tools generally follow this pattern: |
| 41 | |
danakj | ef9f1fa | 2016-01-16 00:37:28 | [diff] [blame] | 42 | 1. Instantiate a [`clang::ast_matchers::MatchFinder`](http://clang.llvm.org/doxygen/classclang_1_1ast__matchers_1_1MatchFinder.html). |
| 43 | 2. Call `addMatcher()` to register [`clang::ast_matchers::MatchFinder::MatchCallback`](http://clang.llvm.org/doxygen/classclang_1_1ast__matchers_1_1MatchFinder_1_1MatchCallback.html) |
| 44 | actions to execute when [matching](http://clang.llvm.org/docs/LibASTMatchersReference.html) |
| 45 | the AST. |
dcheng | ce2375e | 2016-01-12 01:09:07 | [diff] [blame] | 46 | 3. Create a new `clang::tooling::FrontendActionFactory` from the `MatchFinder`. |
| 47 | 4. Run the action across the specified files with |
| 48 | [`clang::tooling::ClangTool::run`](http://clang.llvm.org/doxygen/classclang_1_1tooling_1_1ClangTool.html#acec91f63b45ac7ee2d6c94cb9c10dab3). |
danakj | ef9f1fa | 2016-01-16 00:37:28 | [diff] [blame] | 49 | 5. Serialize generated [`clang::tooling::Replacement`](http://clang.llvm.org/doxygen/classclang_1_1tooling_1_1Replacement.html)s |
| 50 | to `stdout`. |
dcheng | ce2375e | 2016-01-12 01:09:07 | [diff] [blame] | 51 | |
| 52 | Other useful references when writing the tool: |
| 53 | |
| 54 | * [Clang doxygen reference](http://clang.llvm.org/doxygen/index.html) |
| 55 | * [Tutorial for building tools using LibTooling and LibASTMatchers](http://clang.llvm.org/docs/LibASTMatchersTutorial.html) |
| 56 | |
| 57 | ### Edit serialization format |
| 58 | ``` |
| 59 | ==== BEGIN EDITS ==== |
| 60 | r:::path/to/file1:::offset1:::length1:::replacement text |
| 61 | r:::path/to/file2:::offset2:::length2:::replacement text |
| 62 | |
| 63 | ... |
| 64 | |
| 65 | ==== END EDITS ==== |
andybons | 3322f76 | 2015-08-24 21:37:09 | [diff] [blame] | 66 | ``` |
| 67 | |
dcheng | ce2375e | 2016-01-12 01:09:07 | [diff] [blame] | 68 | The header and footer are required. Each line between the header and footer |
| 69 | represents one edit. Fields are separated by `:::`, and the first field must |
| 70 | be `r` (for replacement). In the future, this may be extended to handle header |
| 71 | insertion/removal. A deletion is an edit with no replacement text. |
andybons | 6eaa0c0d | 2015-08-26 20:12:52 | [diff] [blame] | 72 | |
dcheng | ce2375e | 2016-01-12 01:09:07 | [diff] [blame] | 73 | The edits are applied by [`run_tool.py`](#Running), which understands certain |
| 74 | conventions: |
| 75 | |
| 76 | * The tool should munge newlines in replacement text to `\0`. The script |
| 77 | knows to translate `\0` back to newlines when applying edits. |
| 78 | * When removing an element from a 'list' (e.g. function parameters, |
| 79 | initializers), the tool should emit a deletion for just the element. The |
| 80 | script understands how to extend the deletion to remove commas, etc. as |
| 81 | needed. |
| 82 | |
| 83 | TODO: Document more about `SourceLocation` and how spelling loc differs from |
| 84 | expansion loc, etc. |
| 85 | |
| 86 | ### Why not RefactoringTool? |
danakj | ef9f1fa | 2016-01-16 00:37:28 | [diff] [blame] | 87 | While clang has a [`clang::tooling::RefactoringTool`](http://clang.llvm.org/doxygen/classclang_1_1tooling_1_1RefactoringTool.html) |
| 88 | to automatically apply the generated replacements and save the results, it |
| 89 | doesn't work well for Chromium: |
dcheng | ce2375e | 2016-01-12 01:09:07 | [diff] [blame] | 90 | |
| 91 | * Clang tools run actions serially, so runtime scales poorly to tens of |
| 92 | thousands of files. |
| 93 | * A parsing error in any file (quite common in NaCl source) prevents any of |
| 94 | the generated replacements from being applied. |
| 95 | |
| 96 | ## Building |
| 97 | Synopsis: |
danakj | ef9f1fa | 2016-01-16 00:37:28 | [diff] [blame] | 98 | |
andybons | 6eaa0c0d | 2015-08-26 20:12:52 | [diff] [blame] | 99 | ```shell |
danakj | 30d0f8c9 | 2016-01-28 00:26:33 | [diff] [blame] | 100 | tools/clang/scripts/update.py --bootstrap --force-local-build --without-android \ |
dcheng | ce2375e | 2016-01-12 01:09:07 | [diff] [blame] | 101 | --tools blink_gc_plugin plugins rewrite_to_chrome_style |
| 102 | ``` |
danakj | ef9f1fa | 2016-01-16 00:37:28 | [diff] [blame] | 103 | |
dcheng | ce2375e | 2016-01-12 01:09:07 | [diff] [blame] | 104 | Running this command builds the [Oilpan plugin](https://chromium.googlesource.com/chromium/src/+/master/tools/clang/blink_gc_plugin/), |
| 105 | the [Chrome style |
| 106 | plugin](https://chromium.googlesource.com/chromium/src/+/master/tools/clang/plugins/), |
| 107 | and the [Blink to Chrome style rewriter](https://chromium.googlesource.com/chromium/src/+/master/tools/clang/rewrite_to_chrome_style/). Additional arguments to `--tools` should be the name of |
| 108 | subdirectories in |
| 109 | [//tools/clang](https://chromium.googlesource.com/chromium/src/+/master/tools/clang). |
| 110 | Generally, `--tools` should always include `blink_gc_plugin` and `plugins`: otherwise, Chromium won't build. |
| 111 | |
danakj | 30d0f8c9 | 2016-01-28 00:26:33 | [diff] [blame] | 112 | It is important to use --bootstrap as there appear to be [bugs](https://crbug.com/580745) |
| 113 | in the clang library this script produces if you build it with gcc, which is the default. |
| 114 | |
dcheng | ce2375e | 2016-01-12 01:09:07 | [diff] [blame] | 115 | ## Running |
| 116 | First, build all chromium targets to avoid failures due to missing dependecies |
| 117 | that are generated as part of the build: |
danakj | ef9f1fa | 2016-01-16 00:37:28 | [diff] [blame] | 118 | |
dcheng | ce2375e | 2016-01-12 01:09:07 | [diff] [blame] | 119 | ```shell |
| 120 | ninja -C out/Debug |
andybons | 3322f76 | 2015-08-24 21:37:09 | [diff] [blame] | 121 | ``` |
andybons | 6eaa0c0d | 2015-08-26 20:12:52 | [diff] [blame] | 122 | |
dcheng | ce2375e | 2016-01-12 01:09:07 | [diff] [blame] | 123 | Then run the actual tool: |
Daniel Cheng | 9ce2a30 | 2016-01-16 01:17:57 | [diff] [blame] | 124 | |
| 125 | ```shell |
dcheng | ce2375e | 2016-01-12 01:09:07 | [diff] [blame] | 126 | tools/clang/scripts/run_tool.py <toolname> \ |
| 127 | --generate-compdb |
| 128 | out/Debug <path 1> <path 2> ... |
| 129 | ``` |
andybons | 6eaa0c0d | 2015-08-26 20:12:52 | [diff] [blame] | 130 | |
dcheng | ce2375e | 2016-01-12 01:09:07 | [diff] [blame] | 131 | `--generate-compdb` can be omitted if the compile DB was already generated and |
| 132 | the list of build flags and source files has not changed since generation. |
| 133 | |
| 134 | `<path 1>`, `<path 2>`, etc are optional arguments to filter the files to run |
| 135 | the tool across. This is helpful when sharding global refactorings into smaller |
| 136 | chunks. For example, the following command will run the `empty_string` tool |
| 137 | across just the files in `//base`: |
| 138 | |
| 139 | ```shell |
| 140 | tools/clang/scripts/run_tool.py empty_string \ |
| 141 | --generated-compdb \ |
| 142 | out/Debug base |
| 143 | ``` |
| 144 | |
| 145 | ## Debugging |
| 146 | Dumping the AST for a file: |
Daniel Cheng | 9ce2a30 | 2016-01-16 01:17:57 | [diff] [blame] | 147 | |
andybons | 6eaa0c0d | 2015-08-26 20:12:52 | [diff] [blame] | 148 | ```shell |
andybons | 3322f76 | 2015-08-24 21:37:09 | [diff] [blame] | 149 | clang++ -cc1 -ast-dump foo.cc |
| 150 | ``` |
| 151 | |
dcheng | ce2375e | 2016-01-12 01:09:07 | [diff] [blame] | 152 | Using `clang-query` to dynamically test matchers (requires checking out |
| 153 | and building [clang-tools-extras](https://github.com/llvm-mirror/clang-tools-extra)): |
Daniel Cheng | 9ce2a30 | 2016-01-16 01:17:57 | [diff] [blame] | 154 | |
andybons | 6eaa0c0d | 2015-08-26 20:12:52 | [diff] [blame] | 155 | ```shell |
dcheng | ce2375e | 2016-01-12 01:09:07 | [diff] [blame] | 156 | clang-query -p path/to/compdb base/memory/ref_counted.cc |
andybons | 3322f76 | 2015-08-24 21:37:09 | [diff] [blame] | 157 | ``` |
| 158 | |
dcheng | ce2375e | 2016-01-12 01:09:07 | [diff] [blame] | 159 | `printf` debugging: |
Daniel Cheng | 9ce2a30 | 2016-01-16 01:17:57 | [diff] [blame] | 160 | |
dcheng | ce2375e | 2016-01-12 01:09:07 | [diff] [blame] | 161 | ```c++ |
| 162 | clang::Decl* decl = result.Nodes.getNodeAs<clang::Decl>("decl"); |
| 163 | decl->dumpColor(); |
| 164 | clang::Stmt* stmt = result.Nodes.getNodeAs<clang::Stmt>("stmt"); |
| 165 | stmt->dumpColor(); |
andybons | 3322f76 | 2015-08-24 21:37:09 | [diff] [blame] | 166 | ``` |
Daniel Cheng | 9ce2a30 | 2016-01-16 01:17:57 | [diff] [blame] | 167 | |
dcheng | ce2375e | 2016-01-12 01:09:07 | [diff] [blame] | 168 | By default, the script hides the output of the tool. The easiest way to change |
| 169 | that is to `return 1` from the `main()` function of the clang tool. |
andybons | 6eaa0c0d | 2015-08-26 20:12:52 | [diff] [blame] | 170 | |
| 171 | ## Testing |
dcheng | ce2375e | 2016-01-12 01:09:07 | [diff] [blame] | 172 | Synposis: |
Daniel Cheng | 9ce2a30 | 2016-01-16 01:17:57 | [diff] [blame] | 173 | |
andybons | 6eaa0c0d | 2015-08-26 20:12:52 | [diff] [blame] | 174 | ```shell |
lukasza | 1333f1b9 | 2016-08-27 00:24:43 | [diff] [blame] | 175 | tools/clang/scripts/test_tool.py <tool name> |
andybons | 3322f76 | 2015-08-24 21:37:09 | [diff] [blame] | 176 | ``` |
andybons | 6eaa0c0d | 2015-08-26 20:12:52 | [diff] [blame] | 177 | |
dcheng | ce2375e | 2016-01-12 01:09:07 | [diff] [blame] | 178 | The name of the tool binary and the subdirectory for the tool in |
| 179 | `//tools/clang` must match. The test runner finds all files that match the |
| 180 | pattern `//tools/clang/<tool name>/tests/*-original.cc`, runs the tool across |
| 181 | those files, and compared it to the `*-expected.cc` version. If there is a |
| 182 | mismatch, the result is saved in `*-actual.cc`. |