blob: 6d0409b763215a71239349e8145dd2cf2b376775 [file] [log] [blame] [view]
andybons6eaa0c0d2015-08-26 20:12:521# Clang Tool Refactoring
andybons3322f762015-08-24 21:37:092
andybons6eaa0c0d2015-08-26 20:12:523[TOC]
andybons3322f762015-08-24 21:37:094
dchengce2375e2016-01-12 01:09:075## Introduction
6
7Clang tools can help with global refactorings of Chromium code. Clang tools can
8take advantage of clang's AST to perform refactorings that would be impossible
9with 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
andybons6eaa0c0d2015-08-26 20:12:5215## Caveats
andybons3322f762015-08-24 21:37:0916
dchengce2375e2016-01-12 01:09:0717An invocation of the clang tool runs on one build config. Code that only
18compiles on one platform or code that is guarded by a set of compile-time flags
19can be problematic. Performing a global refactoring typically requires running
20the tool once in each build config with code that needs to be updated.
21
22Other minor issues:
23
24* Requires a git checkout.
25* Requires [some hacks to run on Windows](https://codereview.chromium.org/718873004).
andybons6eaa0c0d2015-08-26 20:12:5226
27## Prerequisites
28
dchengce2375e2016-01-12 01:09:0729A Chromium checkout created with `fetch` should have everything needed.
andybons6eaa0c0d2015-08-26 20:12:5230
dchengce2375e2016-01-12 01:09:0731For convenience, add `third_party/llvm-build/Release+Asserts/bin` to `$PATH`.
andybons6eaa0c0d2015-08-26 20:12:5232
dchengce2375e2016-01-12 01:09:0733## Writing the tool
andybons6eaa0c0d2015-08-26 20:12:5234
dchengce2375e2016-01-12 01:09:0735LLVM 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).
37It is generally easiest to use one of the already-written tools as the base for
38writing a new tool.
andybons6eaa0c0d2015-08-26 20:12:5239
dchengce2375e2016-01-12 01:09:0740Chromium clang tools generally follow this pattern:
41
421. Instantiate a [`clang::ast_matchers::MatchFinder`](
43 http://clang.llvm.org/doxygen/classclang_1_1ast__matchers_1_1MatchFinder.html).
442. Call `addMatcher()` to register [`clang::ast_matchers::MatchFinder::MatchCallback`](
45 http://clang.llvm.org/doxygen/classclang_1_1ast__matchers_1_1MatchFinder_1_1MatchCallback.html) actions to execute when [matching](http://clang.llvm.org/docs/LibASTMatchersReference.html) the AST.
463. Create a new `clang::tooling::FrontendActionFactory` from the `MatchFinder`.
474. 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).
495. Serialize generated [`clang::tooling::Replacement`](
50 http://clang.llvm.org/doxygen/classclang_1_1tooling_1_1Replacement.html)s to
51 `stdout`.
52
53Other useful references when writing the tool:
54
55* [Clang doxygen reference](http://clang.llvm.org/doxygen/index.html)
56* [Tutorial for building tools using LibTooling and LibASTMatchers](http://clang.llvm.org/docs/LibASTMatchersTutorial.html)
57
58### Edit serialization format
59```
60==== BEGIN EDITS ====
61r:::path/to/file1:::offset1:::length1:::replacement text
62r:::path/to/file2:::offset2:::length2:::replacement text
63
64 ...
65
66==== END EDITS ====
andybons3322f762015-08-24 21:37:0967```
68
dchengce2375e2016-01-12 01:09:0769The header and footer are required. Each line between the header and footer
70represents one edit. Fields are separated by `:::`, and the first field must
71be `r` (for replacement). In the future, this may be extended to handle header
72insertion/removal. A deletion is an edit with no replacement text.
andybons6eaa0c0d2015-08-26 20:12:5273
dchengce2375e2016-01-12 01:09:0774The edits are applied by [`run_tool.py`](#Running), which understands certain
75conventions:
76
77* The tool should munge newlines in replacement text to `\0`. The script
78 knows to translate `\0` back to newlines when applying edits.
79* When removing an element from a 'list' (e.g. function parameters,
80 initializers), the tool should emit a deletion for just the element. The
81 script understands how to extend the deletion to remove commas, etc. as
82 needed.
83
84TODO: Document more about `SourceLocation` and how spelling loc differs from
85expansion loc, etc.
86
87### Why not RefactoringTool?
88While clang has a [`clang::tooling::RefactoringTool`](
89http://clang.llvm.org/doxygen/classclang_1_1tooling_1_1RefactoringTool.html) to
90automatically apply the generated replacements and save the results, it doesn't
91work well for Chromium:
92
93* Clang tools run actions serially, so runtime scales poorly to tens of
94 thousands of files.
95* A parsing error in any file (quite common in NaCl source) prevents any of
96 the generated replacements from being applied.
97
98## Building
99Synopsis:
andybons6eaa0c0d2015-08-26 20:12:52100```shell
Nico Webere250e6a2015-12-02 19:31:56101tools/clang/scripts/update.py --force-local-build --without-android \
dchengce2375e2016-01-12 01:09:07102 --tools blink_gc_plugin plugins rewrite_to_chrome_style
103```
104Running this command builds the [Oilpan plugin](https://chromium.googlesource.com/chromium/src/+/master/tools/clang/blink_gc_plugin/),
105the [Chrome style
106plugin](https://chromium.googlesource.com/chromium/src/+/master/tools/clang/plugins/),
107and 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
108subdirectories in
109[//tools/clang](https://chromium.googlesource.com/chromium/src/+/master/tools/clang).
110Generally, `--tools` should always include `blink_gc_plugin` and `plugins`: otherwise, Chromium won't build.
111
112## Running
113First, build all chromium targets to avoid failures due to missing dependecies
114that are generated as part of the build:
115```shell
116ninja -C out/Debug
andybons3322f762015-08-24 21:37:09117```
andybons6eaa0c0d2015-08-26 20:12:52118
dchengce2375e2016-01-12 01:09:07119Then run the actual tool:
120```
121tools/clang/scripts/run_tool.py <toolname> \
122 --generate-compdb
123 out/Debug <path 1> <path 2> ...
124```
andybons6eaa0c0d2015-08-26 20:12:52125
dchengce2375e2016-01-12 01:09:07126`--generate-compdb` can be omitted if the compile DB was already generated and
127the list of build flags and source files has not changed since generation.
128
129`<path 1>`, `<path 2>`, etc are optional arguments to filter the files to run
130the tool across. This is helpful when sharding global refactorings into smaller
131chunks. For example, the following command will run the `empty_string` tool
132across just the files in `//base`:
133
134```shell
135tools/clang/scripts/run_tool.py empty_string \
136 --generated-compdb \
137 out/Debug base
138```
139
140## Debugging
141Dumping the AST for a file:
andybons6eaa0c0d2015-08-26 20:12:52142```shell
andybons3322f762015-08-24 21:37:09143clang++ -cc1 -ast-dump foo.cc
144```
145
dchengce2375e2016-01-12 01:09:07146Using `clang-query` to dynamically test matchers (requires checking out
147and building [clang-tools-extras](https://github.com/llvm-mirror/clang-tools-extra)):
andybons6eaa0c0d2015-08-26 20:12:52148```shell
dchengce2375e2016-01-12 01:09:07149clang-query -p path/to/compdb base/memory/ref_counted.cc
andybons3322f762015-08-24 21:37:09150```
151
dchengce2375e2016-01-12 01:09:07152`printf` debugging:
153```c++
154 clang::Decl* decl = result.Nodes.getNodeAs<clang::Decl>("decl");
155 decl->dumpColor();
156 clang::Stmt* stmt = result.Nodes.getNodeAs<clang::Stmt>("stmt");
157 stmt->dumpColor();
andybons3322f762015-08-24 21:37:09158```
dchengce2375e2016-01-12 01:09:07159By default, the script hides the output of the tool. The easiest way to change
160that is to `return 1` from the `main()` function of the clang tool.
andybons6eaa0c0d2015-08-26 20:12:52161
162## Testing
dchengce2375e2016-01-12 01:09:07163Synposis:
andybons6eaa0c0d2015-08-26 20:12:52164```shell
andybons3322f762015-08-24 21:37:09165test_tool.py <tool name>
166```
andybons6eaa0c0d2015-08-26 20:12:52167
dchengce2375e2016-01-12 01:09:07168The name of the tool binary and the subdirectory for the tool in
169`//tools/clang` must match. The test runner finds all files that match the
170pattern `//tools/clang/<tool name>/tests/*-original.cc`, runs the tool across
171those files, and compared it to the `*-expected.cc` version. If there is a
172mismatch, the result is saved in `*-actual.cc`.