1 =====================================
2 Cross Translation Unit (CTU) Analysis
3 =====================================
5 Normally, static analysis works in the boundary of one translation unit (TU).
6 However, with additional steps and configuration we can enable the analysis to inline the definition of a function from
14 CTU analysis can be used in a variety of ways. The importing of external TU definitions can work with pre-dumped PCH
15 files or generating the necessary AST structure on-demand, during the analysis of the main TU. Driving the static
16 analysis can also be implemented in multiple ways. The most direct way is to specify the necessary commandline options
17 of the Clang frontend manually (and generate the prerequisite dependencies of the specific import method by hand). This
18 process can be automated by other tools, like `CodeChecker <https://github.com/Ericsson/codechecker>`_ and scan-build-py
19 (preference for the former).
23 The analysis needs the PCH dumps of all the translations units used in the project.
24 These can be generated by the Clang Frontend itself, and must be arranged in a specific way in the filesystem.
25 The index, which maps symbols' USR names to PCH dumps containing them must also be generated by the
26 `clang-extdef-mapping`. Entries in the index *must* have an `.ast` suffix if the goal
27 is to use PCH-based analysis, as the lack of that extension signals that the entry is to be used as a source-file, and parsed on-demand.
28 This tool uses a :doc:`compilation database <../../JSONCompilationDatabase>` to
29 determine the compilation flags used.
30 The analysis invocation must be provided with the directory which contains the dumps and the mapping files.
35 Let's consider these source files in our minimal example:
53 And a compilation database:
59 "directory": "/path/to/your/project",
60 "command": "clang++ -c foo.cpp -o foo.o",
64 "directory": "/path/to/your/project",
65 "command": "clang++ -c main.cpp -o main.o",
70 We'd like to analyze `main.cpp` and discover the division by zero bug.
71 In order to be able to inline the definition of `foo` from `foo.cpp` first we have to generate the `AST` (or `PCH`) file
76 $ pwd $ /path/to/your/project
77 $ clang++ -emit-ast -o foo.cpp.ast foo.cpp
78 $ # Check that the .ast file is generated:
80 compile_commands.json foo.cpp.ast foo.cpp main.cpp
83 The next step is to create a CTU index file which holds the `USR` name and location of external definitions in the
84 source files in format `<USR-Length>:<USR> <File-Path>`:
88 $ clang-extdef-mapping -p . foo.cpp
89 9:c:@F@foo# /path/to/your/project/foo.cpp
90 $ clang-extdef-mapping -p . foo.cpp > externalDefMap.txt
92 We have to modify `externalDefMap.txt` to contain the name of the `.ast` files instead of the source files:
96 $ sed -i -e "s/.cpp/.cpp.ast/g" externalDefMap.txt
98 We still have to further modify the `externalDefMap.txt` file to contain relative paths:
102 $ sed -i -e "s|$(pwd)/||g" externalDefMap.txt
104 Now everything is available for the CTU analysis.
105 We have to feed Clang with CTU specific extra arguments:
110 /path/to/your/project
111 $ clang++ --analyze \
112 -Xclang -analyzer-config -Xclang experimental-enable-naive-ctu-analysis=true \
113 -Xclang -analyzer-config -Xclang ctu-dir=. \
114 -Xclang -analyzer-output=plist-multi-file \
116 main.cpp:5:12: warning: Division by zero
120 $ # The plist file with the result is generated.
122 compile_commands.json externalDefMap.txt foo.ast foo.cpp foo.cpp.ast main.cpp main.plist
125 This manual procedure is error-prone and not scalable, therefore to analyze real projects it is recommended to use
126 `CodeChecker` or `scan-build-py`.
128 Automated CTU Analysis with CodeChecker
129 #######################################
130 The `CodeChecker <https://github.com/Ericsson/codechecker>`_ project fully supports automated CTU analysis with Clang.
131 Once we have set up the `PATH` environment variable and we activated the python `venv` then it is all it takes:
135 $ CodeChecker analyze --ctu compile_commands.json -o reports
137 compile_commands.json foo.cpp foo.cpp.ast main.cpp reports/
141 ├── compiler_info.json
142 ├── foo.cpp_53f6fbf7ab7ec9931301524b551959e2.plist
143 ├── main.cpp_23db3d8df52ff0812e6e5a03071c8337.plist
145 └── unique_compile_commands.json
147 0 directories, 6 files
150 The `plist` files contain the results of the analysis, which may be viewed with the regular analysis tools.
151 E.g. one may use `CodeChecker parse` to view the results in command line:
155 $ CodeChecker parse reports
156 [HIGH] /home/egbomrt/ctu_mini_raw_project/main.cpp:5:12: Division by zero [core.DivideZero]
160 Found 1 defect(s) in main.cpp
163 ----==== Summary ====----
164 -----------------------
165 Filename | Report count
166 -----------------------
168 -----------------------
169 -----------------------
170 Severity | Report count
171 -----------------------
173 -----------------------
174 ----=================----
175 Total number of reports: 1
176 ----=================----
178 Or we can use `CodeChecker parse -e html` to export the results into HTML format:
182 $ CodeChecker parse -e html -o html_out reports
183 $ firefox html_out/index.html
185 Automated CTU Analysis with scan-build-py (don't do it)
186 #############################################################
187 We actively develop CTU with CodeChecker as the driver for this feature, `scan-build-py` is not actively developed for CTU.
188 `scan-build-py` has various errors and issues, expect it to work only with the very basic projects only.
190 Example usage of scan-build-py:
194 $ /your/path/to/llvm-project/clang/tools/scan-build-py/bin/analyze-build --ctu
195 analyze-build: Run 'scan-view /tmp/scan-build-2019-07-17-17-53-33-810365-7fqgWk' to examine bug reports.
196 $ /your/path/to/llvm-project/clang/tools/scan-view/bin/scan-view /tmp/scan-build-2019-07-17-17-53-33-810365-7fqgWk
197 Starting scan-view at: http://127.0.0.1:8181
199 [6336:6431:0717/175357.633914:ERROR:browser_process_sub_thread.cc(209)] Waited 5 ms for network service
200 Opening in existing browser session.
208 The analysis produces the necessary AST structure of external TUs during analysis. This requires the
209 exact compiler invocations for each TU, which can be generated by hand, or by tools driving the analyzer.
210 The compiler invocation is a shell command that could be used to compile the TU-s main source file.
211 The mapping from absolute source file paths of a TU to lists of compilation command segments used to
212 compile said TU are given in YAML format referred to as `invocation list`, and must be passed as an
213 analyzer-config argument.
214 The index, which maps function USR names to source files containing them must also be generated by the
215 `clang-extdef-mapping`. Entries in the index must *not* have an `.ast` suffix if the goal
216 is to use On-demand analysis, as that extension signals that the entry is to be used as an PCH-dump.
217 The mapping of external definitions implicitly uses a
218 :doc:`compilation database <../../JSONCompilationDatabase>` to determine the compilation flags used.
219 The analysis invocation must be provided with the directory which contains the mapping
220 files, and the `invocation list` which is used to determine compiler flags.
226 Let's consider these source files in our minimal example:
244 The compilation database:
250 "directory": "/path/to/your/project",
251 "command": "clang++ -c foo.cpp -o foo.o",
255 "directory": "/path/to/your/project",
256 "command": "clang++ -c main.cpp -o main.o",
261 The `invocation list`:
265 "/path/to/your/project/foo.cpp":
268 - "/path/to/your/project/foo.cpp"
270 - "/path/to/your/project/foo.o"
272 "/path/to/your/project/main.cpp":
275 - "/path/to/your/project/main.cpp"
277 - "/path/to/your/project/main.o"
279 We'd like to analyze `main.cpp` and discover the division by zero bug.
280 As we are using On-demand mode, we only need to create a CTU index file which holds the `USR` name and location of
281 external definitions in the source files in format `<USR-Length>:<USR> <File-Path>`:
285 $ clang-extdef-mapping -p . foo.cpp
286 9:c:@F@foo# /path/to/your/project/foo.cpp
287 $ clang-extdef-mapping -p . foo.cpp > externalDefMap.txt
289 Now everything is available for the CTU analysis.
290 We have to feed Clang with CTU specific extra arguments:
295 /path/to/your/project
296 $ clang++ --analyze \
297 -Xclang -analyzer-config -Xclang experimental-enable-naive-ctu-analysis=true \
298 -Xclang -analyzer-config -Xclang ctu-dir=. \
299 -Xclang -analyzer-config -Xclang ctu-invocation-list=invocations.yaml \
300 -Xclang -analyzer-output=plist-multi-file \
302 main.cpp:5:12: warning: Division by zero
306 $ # The plist file with the result is generated.
308 compile_commands.json externalDefMap.txt foo.cpp main.cpp main.plist
311 This manual procedure is error-prone and not scalable, therefore to analyze real projects it is recommended to use
312 `CodeChecker` or `scan-build-py`.
314 Automated CTU Analysis with CodeChecker
315 #######################################
316 The `CodeChecker <https://github.com/Ericsson/codechecker>`_ project fully supports automated CTU analysis with Clang.
317 Once we have set up the `PATH` environment variable and we activated the python `venv` then it is all it takes:
321 $ CodeChecker analyze --ctu --ctu-ast-loading-mode on-demand compile_commands.json -o reports
323 compile_commands.json foo.cpp main.cpp reports/
327 ├── compiler_info.json
328 ├── foo.cpp_53f6fbf7ab7ec9931301524b551959e2.plist
329 ├── main.cpp_23db3d8df52ff0812e6e5a03071c8337.plist
331 └── unique_compile_commands.json
333 0 directories, 6 files
336 The `plist` files contain the results of the analysis, which may be viewed with the regular analysis tools.
337 E.g. one may use `CodeChecker parse` to view the results in command line:
341 $ CodeChecker parse reports
342 [HIGH] /home/egbomrt/ctu_mini_raw_project/main.cpp:5:12: Division by zero [core.DivideZero]
346 Found 1 defect(s) in main.cpp
349 ----==== Summary ====----
350 -----------------------
351 Filename | Report count
352 -----------------------
354 -----------------------
355 -----------------------
356 Severity | Report count
357 -----------------------
359 -----------------------
360 ----=================----
361 Total number of reports: 1
362 ----=================----
364 Or we can use `CodeChecker parse -e html` to export the results into HTML format:
368 $ CodeChecker parse -e html -o html_out reports
369 $ firefox html_out/index.html
371 Automated CTU Analysis with scan-build-py (don't do it)
372 #######################################################
373 We actively develop CTU with CodeChecker as the driver for feature, `scan-build-py` is not actively developed for CTU.
374 `scan-build-py` has various errors and issues, expect it to work only with the very basic projects only.
376 Currently On-demand analysis is not supported with `scan-build-py`.