1e5dd7070Spatrick========================== 2e5dd7070SpatrickSource-based Code Coverage 3e5dd7070Spatrick========================== 4e5dd7070Spatrick 5e5dd7070Spatrick.. contents:: 6e5dd7070Spatrick :local: 7e5dd7070Spatrick 8e5dd7070SpatrickIntroduction 9e5dd7070Spatrick============ 10e5dd7070Spatrick 11e5dd7070SpatrickThis document explains how to use clang's source-based code coverage feature. 12e5dd7070SpatrickIt's called "source-based" because it operates on AST and preprocessor 13e5dd7070Spatrickinformation directly. This allows it to generate very precise coverage data. 14e5dd7070Spatrick 15e5dd7070SpatrickClang ships two other code coverage implementations: 16e5dd7070Spatrick 17e5dd7070Spatrick* :doc:`SanitizerCoverage` - A low-overhead tool meant for use alongside the 18e5dd7070Spatrick various sanitizers. It can provide up to edge-level coverage. 19e5dd7070Spatrick 20e5dd7070Spatrick* gcov - A GCC-compatible coverage implementation which operates on DebugInfo. 21e5dd7070Spatrick This is enabled by ``-ftest-coverage`` or ``--coverage``. 22e5dd7070Spatrick 23e5dd7070SpatrickFrom this point onwards "code coverage" will refer to the source-based kind. 24e5dd7070Spatrick 25e5dd7070SpatrickThe code coverage workflow 26e5dd7070Spatrick========================== 27e5dd7070Spatrick 28e5dd7070SpatrickThe code coverage workflow consists of three main steps: 29e5dd7070Spatrick 30e5dd7070Spatrick* Compiling with coverage enabled. 31e5dd7070Spatrick 32e5dd7070Spatrick* Running the instrumented program. 33e5dd7070Spatrick 34e5dd7070Spatrick* Creating coverage reports. 35e5dd7070Spatrick 36e5dd7070SpatrickThe next few sections work through a complete, copy-'n-paste friendly example 37e5dd7070Spatrickbased on this program: 38e5dd7070Spatrick 39e5dd7070Spatrick.. code-block:: cpp 40e5dd7070Spatrick 41e5dd7070Spatrick % cat <<EOF > foo.cc 42e5dd7070Spatrick #define BAR(x) ((x) || (x)) 43e5dd7070Spatrick template <typename T> void foo(T x) { 44e5dd7070Spatrick for (unsigned I = 0; I < 10; ++I) { BAR(I); } 45e5dd7070Spatrick } 46e5dd7070Spatrick int main() { 47e5dd7070Spatrick foo<int>(0); 48e5dd7070Spatrick foo<float>(0); 49e5dd7070Spatrick return 0; 50e5dd7070Spatrick } 51e5dd7070Spatrick EOF 52e5dd7070Spatrick 53e5dd7070SpatrickCompiling with coverage enabled 54e5dd7070Spatrick=============================== 55e5dd7070Spatrick 56e5dd7070SpatrickTo compile code with coverage enabled, pass ``-fprofile-instr-generate 57e5dd7070Spatrick-fcoverage-mapping`` to the compiler: 58e5dd7070Spatrick 59e5dd7070Spatrick.. code-block:: console 60e5dd7070Spatrick 61e5dd7070Spatrick # Step 1: Compile with coverage enabled. 62e5dd7070Spatrick % clang++ -fprofile-instr-generate -fcoverage-mapping foo.cc -o foo 63e5dd7070Spatrick 64e5dd7070SpatrickNote that linking together code with and without coverage instrumentation is 65e5dd7070Spatricksupported. Uninstrumented code simply won't be accounted for in reports. 66e5dd7070Spatrick 67e5dd7070SpatrickRunning the instrumented program 68e5dd7070Spatrick================================ 69e5dd7070Spatrick 70e5dd7070SpatrickThe next step is to run the instrumented program. When the program exits it 71e5dd7070Spatrickwill write a **raw profile** to the path specified by the ``LLVM_PROFILE_FILE`` 72e5dd7070Spatrickenvironment variable. If that variable does not exist, the profile is written 73e5dd7070Spatrickto ``default.profraw`` in the current directory of the program. If 74e5dd7070Spatrick``LLVM_PROFILE_FILE`` contains a path to a non-existent directory, the missing 75e5dd7070Spatrickdirectory structure will be created. Additionally, the following special 76e5dd7070Spatrick**pattern strings** are rewritten: 77e5dd7070Spatrick 78e5dd7070Spatrick* "%p" expands out to the process ID. 79e5dd7070Spatrick 80e5dd7070Spatrick* "%h" expands out to the hostname of the machine running the program. 81e5dd7070Spatrick 82*a9ac8606Spatrick* "%t" expands out to the value of the ``TMPDIR`` environment variable. On 83*a9ac8606Spatrick Darwin, this is typically set to a temporary scratch directory. 84*a9ac8606Spatrick 85e5dd7070Spatrick* "%Nm" expands out to the instrumented binary's signature. When this pattern 86e5dd7070Spatrick is specified, the runtime creates a pool of N raw profiles which are used for 87e5dd7070Spatrick on-line profile merging. The runtime takes care of selecting a raw profile 88e5dd7070Spatrick from the pool, locking it, and updating it before the program exits. If N is 89e5dd7070Spatrick not specified (i.e the pattern is "%m"), it's assumed that ``N = 1``. N must 90e5dd7070Spatrick be between 1 and 9. The merge pool specifier can only occur once per filename 91e5dd7070Spatrick pattern. 92e5dd7070Spatrick 93e5dd7070Spatrick* "%c" expands out to nothing, but enables a mode in which profile counter 94e5dd7070Spatrick updates are continuously synced to a file. This means that if the 95e5dd7070Spatrick instrumented program crashes, or is killed by a signal, perfect coverage 96e5dd7070Spatrick information can still be recovered. Continuous mode does not support value 97e5dd7070Spatrick profiling for PGO, and is only supported on Darwin at the moment. Support for 98ec727ea7Spatrick Linux may be mostly complete but requires testing, and support for Windows 99ec727ea7Spatrick may require more extensive changes: please get involved if you are interested 100ec727ea7Spatrick in porting this feature. 101e5dd7070Spatrick 102e5dd7070Spatrick.. code-block:: console 103e5dd7070Spatrick 104e5dd7070Spatrick # Step 2: Run the program. 105e5dd7070Spatrick % LLVM_PROFILE_FILE="foo.profraw" ./foo 106e5dd7070Spatrick 107ec727ea7SpatrickNote that continuous mode is also used on Fuchsia where it's the only supported 108ec727ea7Spatrickmode, but the implementation is different. The Darwin and Linux implementation 109ec727ea7Spatrickrelies on padding and the ability to map a file over the existing memory 110ec727ea7Spatrickmapping which is generally only available on POSIX systems and isn't suitable 111ec727ea7Spatrickfor other platforms. 112ec727ea7Spatrick 113ec727ea7SpatrickOn Fuchsia, we rely on the ability to relocate counters at runtime using a 114ec727ea7Spatricklevel of indirection. On every counter access, we add a bias to the counter 115ec727ea7Spatrickaddress. This bias is stored in ``__llvm_profile_counter_bias`` symbol that's 116ec727ea7Spatrickprovided by the profile runtime and is initially set to zero, meaning no 117ec727ea7Spatrickrelocation. The runtime can map the profile into memory at arbitrary locations, 118ec727ea7Spatrickand set bias to the offset between the original and the new counter location, 119ec727ea7Spatrickat which point every subsequent counter access will be to the new location, 120ec727ea7Spatrickwhich allows updating profile directly akin to the continuous mode. 121ec727ea7Spatrick 122ec727ea7SpatrickThe advantage of this approach is that doesn't require any special OS support. 123ec727ea7SpatrickThe disadvantage is the extra overhead due to additional instructions required 124ec727ea7Spatrickfor each counter access (overhead both in terms of binary size and performance) 125ec727ea7Spatrickplus duplication of counters (i.e. one copy in the binary itself and another 126ec727ea7Spatrickcopy that's mapped into memory). This implementation can be also enabled for 127ec727ea7Spatrickother platforms by passing the ``-runtime-counter-relocation`` option to the 128ec727ea7Spatrickbackend during compilation. 129ec727ea7Spatrick 130ec727ea7Spatrick.. code-block:: console 131ec727ea7Spatrick 132ec727ea7Spatrick % clang++ -fprofile-instr-generate -fcoverage-mapping -mllvm -runtime-counter-relocation foo.cc -o foo 133ec727ea7Spatrick 134e5dd7070SpatrickCreating coverage reports 135e5dd7070Spatrick========================= 136e5dd7070Spatrick 137e5dd7070SpatrickRaw profiles have to be **indexed** before they can be used to generate 138e5dd7070Spatrickcoverage reports. This is done using the "merge" tool in ``llvm-profdata`` 139e5dd7070Spatrick(which can combine multiple raw profiles and index them at the same time): 140e5dd7070Spatrick 141e5dd7070Spatrick.. code-block:: console 142e5dd7070Spatrick 143e5dd7070Spatrick # Step 3(a): Index the raw profile. 144e5dd7070Spatrick % llvm-profdata merge -sparse foo.profraw -o foo.profdata 145e5dd7070Spatrick 146e5dd7070SpatrickThere are multiple different ways to render coverage reports. The simplest 147e5dd7070Spatrickoption is to generate a line-oriented report: 148e5dd7070Spatrick 149e5dd7070Spatrick.. code-block:: console 150e5dd7070Spatrick 151e5dd7070Spatrick # Step 3(b): Create a line-oriented coverage report. 152e5dd7070Spatrick % llvm-cov show ./foo -instr-profile=foo.profdata 153e5dd7070Spatrick 154e5dd7070SpatrickThis report includes a summary view as well as dedicated sub-views for 155e5dd7070Spatricktemplated functions and their instantiations. For our example program, we get 156e5dd7070Spatrickdistinct views for ``foo<int>(...)`` and ``foo<float>(...)``. If 157e5dd7070Spatrick``-show-line-counts-or-regions`` is enabled, ``llvm-cov`` displays sub-line 158e5dd7070Spatrickregion counts (even in macro expansions): 159e5dd7070Spatrick 160e5dd7070Spatrick.. code-block:: none 161e5dd7070Spatrick 162e5dd7070Spatrick 1| 20|#define BAR(x) ((x) || (x)) 163e5dd7070Spatrick ^20 ^2 164e5dd7070Spatrick 2| 2|template <typename T> void foo(T x) { 165e5dd7070Spatrick 3| 22| for (unsigned I = 0; I < 10; ++I) { BAR(I); } 166e5dd7070Spatrick ^22 ^20 ^20^20 167e5dd7070Spatrick 4| 2|} 168e5dd7070Spatrick ------------------ 169e5dd7070Spatrick | void foo<int>(int): 170e5dd7070Spatrick | 2| 1|template <typename T> void foo(T x) { 171e5dd7070Spatrick | 3| 11| for (unsigned I = 0; I < 10; ++I) { BAR(I); } 172e5dd7070Spatrick | ^11 ^10 ^10^10 173e5dd7070Spatrick | 4| 1|} 174e5dd7070Spatrick ------------------ 175e5dd7070Spatrick | void foo<float>(int): 176e5dd7070Spatrick | 2| 1|template <typename T> void foo(T x) { 177e5dd7070Spatrick | 3| 11| for (unsigned I = 0; I < 10; ++I) { BAR(I); } 178e5dd7070Spatrick | ^11 ^10 ^10^10 179e5dd7070Spatrick | 4| 1|} 180e5dd7070Spatrick ------------------ 181e5dd7070Spatrick 182*a9ac8606SpatrickIf ``--show-branches=count`` and ``--show-expansions`` are also enabled, the 183*a9ac8606Spatricksub-views will show detailed branch coverage information in addition to the 184*a9ac8606Spatrickregion counts: 185*a9ac8606Spatrick 186*a9ac8606Spatrick.. code-block:: none 187*a9ac8606Spatrick 188*a9ac8606Spatrick ------------------ 189*a9ac8606Spatrick | void foo<float>(int): 190*a9ac8606Spatrick | 2| 1|template <typename T> void foo(T x) { 191*a9ac8606Spatrick | 3| 11| for (unsigned I = 0; I < 10; ++I) { BAR(I); } 192*a9ac8606Spatrick | ^11 ^10 ^10^10 193*a9ac8606Spatrick | ------------------ 194*a9ac8606Spatrick | | | 1| 10|#define BAR(x) ((x) || (x)) 195*a9ac8606Spatrick | | | ^10 ^1 196*a9ac8606Spatrick | | | ------------------ 197*a9ac8606Spatrick | | | | Branch (1:17): [True: 9, False: 1] 198*a9ac8606Spatrick | | | | Branch (1:24): [True: 0, False: 1] 199*a9ac8606Spatrick | | | ------------------ 200*a9ac8606Spatrick | ------------------ 201*a9ac8606Spatrick | | Branch (3:23): [True: 10, False: 1] 202*a9ac8606Spatrick | ------------------ 203*a9ac8606Spatrick | 4| 1|} 204*a9ac8606Spatrick ------------------ 205*a9ac8606Spatrick 206*a9ac8606Spatrick 207e5dd7070SpatrickTo generate a file-level summary of coverage statistics instead of a 208e5dd7070Spatrickline-oriented report, try: 209e5dd7070Spatrick 210e5dd7070Spatrick.. code-block:: console 211e5dd7070Spatrick 212e5dd7070Spatrick # Step 3(c): Create a coverage summary. 213e5dd7070Spatrick % llvm-cov report ./foo -instr-profile=foo.profdata 214*a9ac8606Spatrick Filename Regions Missed Regions Cover Functions Missed Functions Executed Lines Missed Lines Cover Branches Missed Branches Cover 215*a9ac8606Spatrick -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 216*a9ac8606Spatrick /tmp/foo.cc 13 0 100.00% 3 0 100.00% 13 0 100.00% 12 2 83.33% 217*a9ac8606Spatrick -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 218*a9ac8606Spatrick TOTAL 13 0 100.00% 3 0 100.00% 13 0 100.00% 12 2 83.33% 219e5dd7070Spatrick 220e5dd7070SpatrickThe ``llvm-cov`` tool supports specifying a custom demangler, writing out 221e5dd7070Spatrickreports in a directory structure, and generating html reports. For the full 222e5dd7070Spatricklist of options, please refer to the `command guide 223e5dd7070Spatrick<https://llvm.org/docs/CommandGuide/llvm-cov.html>`_. 224e5dd7070Spatrick 225e5dd7070SpatrickA few final notes: 226e5dd7070Spatrick 227e5dd7070Spatrick* The ``-sparse`` flag is optional but can result in dramatically smaller 228e5dd7070Spatrick indexed profiles. This option should not be used if the indexed profile will 229e5dd7070Spatrick be reused for PGO. 230e5dd7070Spatrick 231e5dd7070Spatrick* Raw profiles can be discarded after they are indexed. Advanced use of the 232e5dd7070Spatrick profile runtime library allows an instrumented program to merge profiling 233e5dd7070Spatrick information directly into an existing raw profile on disk. The details are 234e5dd7070Spatrick out of scope. 235e5dd7070Spatrick 236e5dd7070Spatrick* The ``llvm-profdata`` tool can be used to merge together multiple raw or 237e5dd7070Spatrick indexed profiles. To combine profiling data from multiple runs of a program, 238e5dd7070Spatrick try e.g: 239e5dd7070Spatrick 240e5dd7070Spatrick .. code-block:: console 241e5dd7070Spatrick 242e5dd7070Spatrick % llvm-profdata merge -sparse foo1.profraw foo2.profdata -o foo3.profdata 243e5dd7070Spatrick 244e5dd7070SpatrickExporting coverage data 245e5dd7070Spatrick======================= 246e5dd7070Spatrick 247e5dd7070SpatrickCoverage data can be exported into JSON using the ``llvm-cov export`` 248e5dd7070Spatricksub-command. There is a comprehensive reference which defines the structure of 249e5dd7070Spatrickthe exported data at a high level in the llvm-cov source code. 250e5dd7070Spatrick 251e5dd7070SpatrickInterpreting reports 252e5dd7070Spatrick==================== 253e5dd7070Spatrick 254*a9ac8606SpatrickThere are five statistics tracked in a coverage summary: 255e5dd7070Spatrick 256e5dd7070Spatrick* Function coverage is the percentage of functions which have been executed at 257e5dd7070Spatrick least once. A function is considered to be executed if any of its 258e5dd7070Spatrick instantiations are executed. 259e5dd7070Spatrick 260e5dd7070Spatrick* Instantiation coverage is the percentage of function instantiations which 261e5dd7070Spatrick have been executed at least once. Template functions and static inline 262e5dd7070Spatrick functions from headers are two kinds of functions which may have multiple 263*a9ac8606Spatrick instantiations. This statistic is hidden by default in reports, but can be 264*a9ac8606Spatrick enabled via the ``-show-instantiation-summary`` option. 265e5dd7070Spatrick 266e5dd7070Spatrick* Line coverage is the percentage of code lines which have been executed at 267e5dd7070Spatrick least once. Only executable lines within function bodies are considered to be 268e5dd7070Spatrick code lines. 269e5dd7070Spatrick 270e5dd7070Spatrick* Region coverage is the percentage of code regions which have been executed at 271e5dd7070Spatrick least once. A code region may span multiple lines (e.g in a large function 272e5dd7070Spatrick body with no control flow). However, it's also possible for a single line to 273e5dd7070Spatrick contain multiple code regions (e.g in "return x || y && z"). 274e5dd7070Spatrick 275*a9ac8606Spatrick* Branch coverage is the percentage of "true" and "false" branches that have 276*a9ac8606Spatrick been taken at least once. Each branch is tied to individual conditions in the 277*a9ac8606Spatrick source code that may each evaluate to either "true" or "false". These 278*a9ac8606Spatrick conditions may comprise larger boolean expressions linked by boolean logical 279*a9ac8606Spatrick operators. For example, "x = (y == 2) || (z < 10)" is a boolean expression 280*a9ac8606Spatrick that is comprised of two individual conditions, each of which evaluates to 281*a9ac8606Spatrick either true or false, producing four total branch outcomes. 282*a9ac8606Spatrick 283*a9ac8606SpatrickOf these five statistics, function coverage is usually the least granular while 284*a9ac8606Spatrickbranch coverage is the most granular. 100% branch coverage for a function 285*a9ac8606Spatrickimplies 100% region coverage for a function. The project-wide totals for each 286e5dd7070Spatrickstatistic are listed in the summary. 287e5dd7070Spatrick 288e5dd7070SpatrickFormat compatibility guarantees 289e5dd7070Spatrick=============================== 290e5dd7070Spatrick 291e5dd7070Spatrick* There are no backwards or forwards compatibility guarantees for the raw 292e5dd7070Spatrick profile format. Raw profiles may be dependent on the specific compiler 293e5dd7070Spatrick revision used to generate them. It's inadvisable to store raw profiles for 294e5dd7070Spatrick long periods of time. 295e5dd7070Spatrick 296e5dd7070Spatrick* Tools must retain **backwards** compatibility with indexed profile formats. 297e5dd7070Spatrick These formats are not forwards-compatible: i.e, a tool which uses format 298e5dd7070Spatrick version X will not be able to understand format version (X+k). 299e5dd7070Spatrick 300e5dd7070Spatrick* Tools must also retain **backwards** compatibility with the format of the 301e5dd7070Spatrick coverage mappings emitted into instrumented binaries. These formats are not 302e5dd7070Spatrick forwards-compatible. 303e5dd7070Spatrick 304e5dd7070Spatrick* The JSON coverage export format has a (major, minor, patch) version triple. 305e5dd7070Spatrick Only a major version increment indicates a backwards-incompatible change. A 306e5dd7070Spatrick minor version increment is for added functionality, and patch version 307e5dd7070Spatrick increments are for bugfixes. 308e5dd7070Spatrick 309*a9ac8606SpatrickImpact of llvm optimizations on coverage reports 310*a9ac8606Spatrick================================================ 311*a9ac8606Spatrick 312*a9ac8606Spatrickllvm optimizations (such as inlining or CFG simplification) should have no 313*a9ac8606Spatrickimpact on coverage report quality. This is due to the fact that the mapping 314*a9ac8606Spatrickfrom source regions to profile counters is immutable, and is generated before 315*a9ac8606Spatrickthe llvm optimizer kicks in. The optimizer can't prove that profile counter 316*a9ac8606Spatrickinstrumentation is safe to delete (because it's not: it affects the profile the 317*a9ac8606Spatrickprogram emits), and so leaves it alone. 318*a9ac8606Spatrick 319*a9ac8606SpatrickNote that this coverage feature does not rely on information that can degrade 320*a9ac8606Spatrickduring the course of optimization, such as debug info line tables. 321*a9ac8606Spatrick 322e5dd7070SpatrickUsing the profiling runtime without static initializers 323e5dd7070Spatrick======================================================= 324e5dd7070Spatrick 325e5dd7070SpatrickBy default the compiler runtime uses a static initializer to determine the 326e5dd7070Spatrickprofile output path and to register a writer function. To collect profiles 327e5dd7070Spatrickwithout using static initializers, do this manually: 328e5dd7070Spatrick 329e5dd7070Spatrick* Export a ``int __llvm_profile_runtime`` symbol from each instrumented shared 330e5dd7070Spatrick library and executable. When the linker finds a definition of this symbol, it 331e5dd7070Spatrick knows to skip loading the object which contains the profiling runtime's 332e5dd7070Spatrick static initializer. 333e5dd7070Spatrick 334e5dd7070Spatrick* Forward-declare ``void __llvm_profile_initialize_file(void)`` and call it 335e5dd7070Spatrick once from each instrumented executable. This function parses 336e5dd7070Spatrick ``LLVM_PROFILE_FILE``, sets the output path, and truncates any existing files 337e5dd7070Spatrick at that path. To get the same behavior without truncating existing files, 338e5dd7070Spatrick pass a filename pattern string to ``void __llvm_profile_set_filename(char 339e5dd7070Spatrick *)``. These calls can be placed anywhere so long as they precede all calls 340e5dd7070Spatrick to ``__llvm_profile_write_file``. 341e5dd7070Spatrick 342e5dd7070Spatrick* Forward-declare ``int __llvm_profile_write_file(void)`` and call it to write 343e5dd7070Spatrick out a profile. This function returns 0 when it succeeds, and a non-zero value 344e5dd7070Spatrick otherwise. Calling this function multiple times appends profile data to an 345e5dd7070Spatrick existing on-disk raw profile. 346e5dd7070Spatrick 347e5dd7070SpatrickIn C++ files, declare these as ``extern "C"``. 348e5dd7070Spatrick 349*a9ac8606SpatrickUsing the profiling runtime without a filesystem 350*a9ac8606Spatrick------------------------------------------------ 351*a9ac8606Spatrick 352*a9ac8606SpatrickThe profiling runtime also supports freestanding environments that lack a 353*a9ac8606Spatrickfilesystem. The runtime ships as a static archive that's structured to make 354*a9ac8606Spatrickdependencies on a hosted environment optional, depending on what features 355*a9ac8606Spatrickthe client application uses. 356*a9ac8606Spatrick 357*a9ac8606SpatrickThe first step is to export ``__llvm_profile_runtime``, as above, to disable 358*a9ac8606Spatrickthe default static initializers. Instead of calling the ``*_file()`` APIs 359*a9ac8606Spatrickdescribed above, use the following to save the profile directly to a buffer 360*a9ac8606Spatrickunder your control: 361*a9ac8606Spatrick 362*a9ac8606Spatrick* Forward-declare ``uint64_t __llvm_profile_get_size_for_buffer(void)`` and 363*a9ac8606Spatrick call it to determine the size of the profile. You'll need to allocate a 364*a9ac8606Spatrick buffer of this size. 365*a9ac8606Spatrick 366*a9ac8606Spatrick* Forward-declare ``int __llvm_profile_write_buffer(char *Buffer)`` and call it 367*a9ac8606Spatrick to copy the current counters to ``Buffer``, which is expected to already be 368*a9ac8606Spatrick allocated and big enough for the profile. 369*a9ac8606Spatrick 370*a9ac8606Spatrick* Optionally, forward-declare ``void __llvm_profile_reset_counters(void)`` and 371*a9ac8606Spatrick call it to reset the counters before entering a specific section to be 372*a9ac8606Spatrick profiled. This is only useful if there is some setup that should be excluded 373*a9ac8606Spatrick from the profile. 374*a9ac8606Spatrick 375*a9ac8606SpatrickIn C++ files, declare these as ``extern "C"``. 376*a9ac8606Spatrick 377e5dd7070SpatrickCollecting coverage reports for the llvm project 378e5dd7070Spatrick================================================ 379e5dd7070Spatrick 380e5dd7070SpatrickTo prepare a coverage report for llvm (and any of its sub-projects), add 381e5dd7070Spatrick``-DLLVM_BUILD_INSTRUMENTED_COVERAGE=On`` to the cmake configuration. Raw 382e5dd7070Spatrickprofiles will be written to ``$BUILD_DIR/profiles/``. To prepare an html 383e5dd7070Spatrickreport, run ``llvm/utils/prepare-code-coverage-artifact.py``. 384e5dd7070Spatrick 385e5dd7070SpatrickTo specify an alternate directory for raw profiles, use 386e5dd7070Spatrick``-DLLVM_PROFILE_DATA_DIR``. To change the size of the profile merge pool, use 387e5dd7070Spatrick``-DLLVM_PROFILE_MERGE_POOL_SIZE``. 388e5dd7070Spatrick 389e5dd7070SpatrickDrawbacks and limitations 390e5dd7070Spatrick========================= 391e5dd7070Spatrick 392e5dd7070Spatrick* Prior to version 2.26, the GNU binutils BFD linker is not able link programs 393e5dd7070Spatrick compiled with ``-fcoverage-mapping`` in its ``--gc-sections`` mode. Possible 394e5dd7070Spatrick workarounds include disabling ``--gc-sections``, upgrading to a newer version 395e5dd7070Spatrick of BFD, or using the Gold linker. 396e5dd7070Spatrick 397e5dd7070Spatrick* Code coverage does not handle unpredictable changes in control flow or stack 398e5dd7070Spatrick unwinding in the presence of exceptions precisely. Consider the following 399e5dd7070Spatrick function: 400e5dd7070Spatrick 401e5dd7070Spatrick .. code-block:: cpp 402e5dd7070Spatrick 403e5dd7070Spatrick int f() { 404e5dd7070Spatrick may_throw(); 405e5dd7070Spatrick return 0; 406e5dd7070Spatrick } 407e5dd7070Spatrick 408e5dd7070Spatrick If the call to ``may_throw()`` propagates an exception into ``f``, the code 409e5dd7070Spatrick coverage tool may mark the ``return`` statement as executed even though it is 410e5dd7070Spatrick not. A call to ``longjmp()`` can have similar effects. 411e5dd7070Spatrick 412e5dd7070SpatrickClang implementation details 413e5dd7070Spatrick============================ 414e5dd7070Spatrick 415e5dd7070SpatrickThis section may be of interest to those wishing to understand or improve 416e5dd7070Spatrickthe clang code coverage implementation. 417e5dd7070Spatrick 418e5dd7070SpatrickGap regions 419e5dd7070Spatrick----------- 420e5dd7070Spatrick 421e5dd7070SpatrickGap regions are source regions with counts. A reporting tool cannot set a line 422e5dd7070Spatrickexecution count to the count from a gap region unless that region is the only 423e5dd7070Spatrickone on a line. 424e5dd7070Spatrick 425e5dd7070SpatrickGap regions are used to eliminate unnatural artifacts in coverage reports, such 426e5dd7070Spatrickas red "unexecuted" highlights present at the end of an otherwise covered line, 427e5dd7070Spatrickor blue "executed" highlights present at the start of a line that is otherwise 428e5dd7070Spatricknot executed. 429e5dd7070Spatrick 430*a9ac8606SpatrickBranch regions 431*a9ac8606Spatrick-------------- 432*a9ac8606SpatrickWhen viewing branch coverage details in source-based file-level sub-views using 433*a9ac8606Spatrick``--show-branches``, it is recommended that users show all macro expansions 434*a9ac8606Spatrick(using option ``--show-expansions``) since macros may contain hidden branch 435*a9ac8606Spatrickconditions. The coverage summary report will always include these macro-based 436*a9ac8606Spatrickboolean expressions in the overall branch coverage count for a function or 437*a9ac8606Spatricksource file. 438*a9ac8606Spatrick 439*a9ac8606SpatrickBranch coverage is not tracked for constant folded branch conditions since 440*a9ac8606Spatrickbranches are not generated for these cases. In the source-based file-level 441*a9ac8606Spatricksub-view, these branches will simply be shown as ``[Folded - Ignored]`` so that 442*a9ac8606Spatrickusers are informed about what happened. 443*a9ac8606Spatrick 444*a9ac8606SpatrickBranch coverage is tied directly to branch-generating conditions in the source 445*a9ac8606Spatrickcode. Users should not see hidden branches that aren't actually tied to the 446*a9ac8606Spatricksource code. 447*a9ac8606Spatrick 448*a9ac8606Spatrick 449e5dd7070SpatrickSwitch statements 450e5dd7070Spatrick----------------- 451e5dd7070Spatrick 452e5dd7070SpatrickThe region mapping for a switch body consists of a gap region that covers the 453e5dd7070Spatrickentire body (starting from the '{' in 'switch (...) {', and terminating where the 454e5dd7070Spatricklast case ends). This gap region has a zero count: this causes "gap" areas in 455e5dd7070Spatrickbetween case statements, which contain no executable code, to appear uncovered. 456e5dd7070Spatrick 457e5dd7070SpatrickWhen a switch case is visited, the parent region is extended: if the parent 458e5dd7070Spatrickregion has no start location, its start location becomes the start of the case. 459e5dd7070SpatrickThis is used to support switch statements without a ``CompoundStmt`` body, in 460e5dd7070Spatrickwhich the switch body and the single case share a count. 461e5dd7070Spatrick 462e5dd7070SpatrickFor switches with ``CompoundStmt`` bodies, a new region is created at the start 463e5dd7070Spatrickof each switch case. 464*a9ac8606Spatrick 465*a9ac8606SpatrickBranch regions are also generated for each switch case, including the default 466*a9ac8606Spatrickcase. If there is no explicitly defined default case in the source code, a 467*a9ac8606Spatrickbranch region is generated to correspond to the implicit default case that is 468*a9ac8606Spatrickgenerated by the compiler. The implicit branch region is tied to the line and 469*a9ac8606Spatrickcolumn number of the switch statement condition since no source code for the 470*a9ac8606Spatrickimplicit case exists. 471