blob: b95260c0e7bbff7e1caa8ff0e65b6dd51136f96d [file] [log] [blame] [view]
Yuke Liaobb571bd62018-10-31 21:51:521# The Clang Code Coverage Wrapper
2
3The Clang code coverage wrapper
4([//build/toolchain/clang_code_coverage_wrapper.py]) is a compiler wrapper that
5adds code coverage flags to the invocations of Clang C/C++ compiler, which can
6be used to instrument and generate code coverage data for a subset of the source
7files. The main use case of this wrapper is to generate code coverage reports
8for changed files at a per-CL level during try jobs.
9
10One might ask: why this compiler wrapper instead of just instrumenting all the
11source files?
12
13Efficiency is the main consideration because comparing to instrument everything,
14only instrument what-is-needed takes less time to build the test targets,
15produces binaries that are 10 times smaller, run tests twice faster and
16generates coverage reports significantly faster. An experiment was done to
17evaluate the effectiveness of instrument what-is-needed, and in the experiment,
18`unit_tests` is used as the sample test target and 8 randomly chosen C++ source
19files are used as the samples files to instrument. The results are presented in
20the following table:
21
22| Type | Build time | Binary size | Tests run time | Reports generation time |
23|--------------------|------------|-------------|----------------|-------------------------|
24| No instrumentation | 43m | 240M | 40s | 0s |
25| What-is-needed | 43m | 241M | 40s | 0.14s |
26| Everything | 53m | 2.3G | 80s | 1m10s |
27
28It can be seen from the results that the overhead introduced by instrument
29everything is huge, while it's negligible for instrument what-is-needed.
30
31## How to use the coverage wrapper?
32To get the coverage wrapper hook into your build, add the following flags to
Yuke Liao9619c712018-11-02 07:18:0933your `args.gn` file, assuming build directory is `out/Release`.
Yuke Liaobb571bd62018-10-31 21:51:5234
35```
36use_clang_coverage = true
Yuke Liao9619c712018-11-02 07:18:0937coverage_instrumentation_input_file = "//out/Release/coverage_instrumentation_input.txt"
Yuke Liaobb571bd62018-10-31 21:51:5238```
39
Yuke Liao9619c712018-11-02 07:18:0940The path to the coverage instrumentation input file should be a source root
41absolute path, and the file consists of multiple lines where each line
Yuke Liaobb571bd62018-10-31 21:51:5242represents a path to a source file, and the specified paths must be relative to
43the root build directory. e.g. `../../base/task/post_task.cc` for build
44directory `out/Release`.
45
46Then, use the [//tools/code_coverage/coverage.py] script to build, run tests and
47generate code coverage reports for the exercised files.
48
49## Caveats
50One caveat with this compiler wrapper is that it may introduce unexpected
51behaviors in incremental builds when the file path to the coverage
52instrumentation input file changes between consecutive runs, and the reason is
53that the coverage instrumentation input file is explicitly passed to the
54coverage wrapper as a command-line argument, which makes the path part of the
55Ninja commands used to compile the source files, so change of the path between
56consecutive runs causes Ninja to perform a full rebuild.
57
58Due to the reasons mentioned above, users of this script are strongly advised to
59always use the same path such as
60`${root_build_dir}/coverage_instrumentation_input.txt`.
61
62## Want to learn more about code coverage in Chromium?
63For more background information on how code coverage works in Chromium, please
64refer to [code_coverage.md]
65
66## Contacts
67
68### Reporting problems
69We're still evaluating the tools, for any breakage report and feature requests,
70please [file a bug].
71
72### Mailing list
73For questions and general discussions, please join [code-coverage group].
74
75[//build/toolchain/clang_code_coverage_wrapper.py]: ../build/toolchain/clang_code_coverage_wrapper.py
Yuke Liao43bbbcd52019-06-21 19:34:5076[code_coverage.md]: testing/code_coverage.md
Yuke Liaobb571bd62018-10-31 21:51:5277[//tools/code_coverage/coverage.py]: ../tools/code_coverage/coverage.py
Yuke Liao03c644072019-07-30 18:33:4078[file a bug]: https://bugs.chromium.org/p/chromium/issues/entry?components=Infra%3ETest%3ECodeCoverage
Yuke Liaobb571bd62018-10-31 21:51:5279[code-coverage group]: https://groups.google.com/a/chromium.org/forum/#!forum/code-coverage