1# Code Coverage 2 3Code coverage can be checked using clang's source-based coverage tools. You 4must use the GN argument `use_coverage=true`. It's recommended to do this in a 5separate output directory since the added instrumentation will affect 6performance and generate an output file every time a binary is run. You can 7read more about this in [clang's documentation]( 8http://clang.llvm.org/docs/SourceBasedCodeCoverage.html) but the 9bare minimum steps are also outlined below. You will also need to download the 10pre-built clang coverage tools, which are not downloaded by default. The 11easiest way to do this is to set a custom variable in your `.gclient` file. 12Under the "openscreen" solution, add: 13```python 14 "custom_vars": { 15 "checkout_clang_coverage_tools": True, 16 }, 17``` 18then run `gclient runhooks`. You can also run the python command from the 19`clang_coverage_tools` hook in `//DEPS` yourself or even download the tools 20manually 21([link](https://storage.googleapis.com/chromium-browser-clang-staging/)). 22 23Once you have your GN directory (we'll call it `out/coverage`) and have 24downloaded the tools, do the following to generate an HTML coverage report: 25```bash 26out/coverage/openscreen_unittests 27third_party/llvm-build/Release+Asserts/bin/llvm-profdata merge -sparse default.profraw -o foo.profdata 28third_party/llvm-build/Release+Asserts/bin/llvm-cov show out/coverage/openscreen_unittests -instr-profile=foo.profdata -format=html -output-dir=<out dir> [filter paths] 29``` 30There are a few things to note here: 31 - `default.profraw` is generated by running the instrumented code, but 32 `foo.profdata` can be any path you want. 33 - `<out dir>` should be an empty directory for placing the generated HTML 34 files. You can view the report at `<out dir>/index.html`. 35 - `[filter paths]` is a list of paths to which you want to limit the coverage 36 report. For example, you may want to limit it to cast/ or even 37 cast/streaming/. If this list is empty, all data will be in the report. 38 39The same process can be used to check the coverage of a fuzzer's corpus. Just 40add `-runs=0` to the fuzzer arguments to make sure it only runs the existing 41corpus then exits. 42