17330f729Sjoerg========================== 27330f729SjoergSource-based Code Coverage 37330f729Sjoerg========================== 47330f729Sjoerg 57330f729Sjoerg.. contents:: 67330f729Sjoerg :local: 77330f729Sjoerg 87330f729SjoergIntroduction 97330f729Sjoerg============ 107330f729Sjoerg 117330f729SjoergThis document explains how to use clang's source-based code coverage feature. 127330f729SjoergIt's called "source-based" because it operates on AST and preprocessor 137330f729Sjoerginformation directly. This allows it to generate very precise coverage data. 147330f729Sjoerg 157330f729SjoergClang ships two other code coverage implementations: 167330f729Sjoerg 177330f729Sjoerg* :doc:`SanitizerCoverage` - A low-overhead tool meant for use alongside the 187330f729Sjoerg various sanitizers. It can provide up to edge-level coverage. 197330f729Sjoerg 207330f729Sjoerg* gcov - A GCC-compatible coverage implementation which operates on DebugInfo. 217330f729Sjoerg This is enabled by ``-ftest-coverage`` or ``--coverage``. 227330f729Sjoerg 237330f729SjoergFrom this point onwards "code coverage" will refer to the source-based kind. 247330f729Sjoerg 257330f729SjoergThe code coverage workflow 267330f729Sjoerg========================== 277330f729Sjoerg 287330f729SjoergThe code coverage workflow consists of three main steps: 297330f729Sjoerg 307330f729Sjoerg* Compiling with coverage enabled. 317330f729Sjoerg 327330f729Sjoerg* Running the instrumented program. 337330f729Sjoerg 347330f729Sjoerg* Creating coverage reports. 357330f729Sjoerg 367330f729SjoergThe next few sections work through a complete, copy-'n-paste friendly example 377330f729Sjoergbased on this program: 387330f729Sjoerg 397330f729Sjoerg.. code-block:: cpp 407330f729Sjoerg 417330f729Sjoerg % cat <<EOF > foo.cc 427330f729Sjoerg #define BAR(x) ((x) || (x)) 437330f729Sjoerg template <typename T> void foo(T x) { 447330f729Sjoerg for (unsigned I = 0; I < 10; ++I) { BAR(I); } 457330f729Sjoerg } 467330f729Sjoerg int main() { 477330f729Sjoerg foo<int>(0); 487330f729Sjoerg foo<float>(0); 497330f729Sjoerg return 0; 507330f729Sjoerg } 517330f729Sjoerg EOF 527330f729Sjoerg 537330f729SjoergCompiling with coverage enabled 547330f729Sjoerg=============================== 557330f729Sjoerg 567330f729SjoergTo compile code with coverage enabled, pass ``-fprofile-instr-generate 577330f729Sjoerg-fcoverage-mapping`` to the compiler: 587330f729Sjoerg 597330f729Sjoerg.. code-block:: console 607330f729Sjoerg 617330f729Sjoerg # Step 1: Compile with coverage enabled. 627330f729Sjoerg % clang++ -fprofile-instr-generate -fcoverage-mapping foo.cc -o foo 637330f729Sjoerg 647330f729SjoergNote that linking together code with and without coverage instrumentation is 657330f729Sjoergsupported. Uninstrumented code simply won't be accounted for in reports. 667330f729Sjoerg 677330f729SjoergRunning the instrumented program 687330f729Sjoerg================================ 697330f729Sjoerg 707330f729SjoergThe next step is to run the instrumented program. When the program exits it 717330f729Sjoergwill write a **raw profile** to the path specified by the ``LLVM_PROFILE_FILE`` 727330f729Sjoergenvironment variable. If that variable does not exist, the profile is written 737330f729Sjoergto ``default.profraw`` in the current directory of the program. If 747330f729Sjoerg``LLVM_PROFILE_FILE`` contains a path to a non-existent directory, the missing 757330f729Sjoergdirectory structure will be created. Additionally, the following special 767330f729Sjoerg**pattern strings** are rewritten: 777330f729Sjoerg 787330f729Sjoerg* "%p" expands out to the process ID. 797330f729Sjoerg 807330f729Sjoerg* "%h" expands out to the hostname of the machine running the program. 817330f729Sjoerg 82*e038c9c4Sjoerg* "%t" expands out to the value of the ``TMPDIR`` environment variable. On 83*e038c9c4Sjoerg Darwin, this is typically set to a temporary scratch directory. 84*e038c9c4Sjoerg 857330f729Sjoerg* "%Nm" expands out to the instrumented binary's signature. When this pattern 867330f729Sjoerg is specified, the runtime creates a pool of N raw profiles which are used for 877330f729Sjoerg on-line profile merging. The runtime takes care of selecting a raw profile 887330f729Sjoerg from the pool, locking it, and updating it before the program exits. If N is 897330f729Sjoerg not specified (i.e the pattern is "%m"), it's assumed that ``N = 1``. N must 907330f729Sjoerg be between 1 and 9. The merge pool specifier can only occur once per filename 917330f729Sjoerg pattern. 927330f729Sjoerg 93*e038c9c4Sjoerg* "%c" expands out to nothing, but enables a mode in which profile counter 94*e038c9c4Sjoerg updates are continuously synced to a file. This means that if the 95*e038c9c4Sjoerg instrumented program crashes, or is killed by a signal, perfect coverage 96*e038c9c4Sjoerg information can still be recovered. Continuous mode does not support value 97*e038c9c4Sjoerg profiling for PGO, and is only supported on Darwin at the moment. Support for 98*e038c9c4Sjoerg Linux may be mostly complete but requires testing, and support for Windows 99*e038c9c4Sjoerg may require more extensive changes: please get involved if you are interested 100*e038c9c4Sjoerg in porting this feature. 101*e038c9c4Sjoerg 1027330f729Sjoerg.. code-block:: console 1037330f729Sjoerg 1047330f729Sjoerg # Step 2: Run the program. 1057330f729Sjoerg % LLVM_PROFILE_FILE="foo.profraw" ./foo 1067330f729Sjoerg 107*e038c9c4SjoergNote that continuous mode is also used on Fuchsia where it's the only supported 108*e038c9c4Sjoergmode, but the implementation is different. The Darwin and Linux implementation 109*e038c9c4Sjoergrelies on padding and the ability to map a file over the existing memory 110*e038c9c4Sjoergmapping which is generally only available on POSIX systems and isn't suitable 111*e038c9c4Sjoergfor other platforms. 112*e038c9c4Sjoerg 113*e038c9c4SjoergOn Fuchsia, we rely on the ability to relocate counters at runtime using a 114*e038c9c4Sjoerglevel of indirection. On every counter access, we add a bias to the counter 115*e038c9c4Sjoergaddress. This bias is stored in ``__llvm_profile_counter_bias`` symbol that's 116*e038c9c4Sjoergprovided by the profile runtime and is initially set to zero, meaning no 117*e038c9c4Sjoergrelocation. The runtime can map the profile into memory at arbitrary locations, 118*e038c9c4Sjoergand set bias to the offset between the original and the new counter location, 119*e038c9c4Sjoergat which point every subsequent counter access will be to the new location, 120*e038c9c4Sjoergwhich allows updating profile directly akin to the continuous mode. 121*e038c9c4Sjoerg 122*e038c9c4SjoergThe advantage of this approach is that doesn't require any special OS support. 123*e038c9c4SjoergThe disadvantage is the extra overhead due to additional instructions required 124*e038c9c4Sjoergfor each counter access (overhead both in terms of binary size and performance) 125*e038c9c4Sjoergplus duplication of counters (i.e. one copy in the binary itself and another 126*e038c9c4Sjoergcopy that's mapped into memory). This implementation can be also enabled for 127*e038c9c4Sjoergother platforms by passing the ``-runtime-counter-relocation`` option to the 128*e038c9c4Sjoergbackend during compilation. 129*e038c9c4Sjoerg 130*e038c9c4Sjoerg.. code-block:: console 131*e038c9c4Sjoerg 132*e038c9c4Sjoerg % clang++ -fprofile-instr-generate -fcoverage-mapping -mllvm -runtime-counter-relocation foo.cc -o foo 133*e038c9c4Sjoerg 1347330f729SjoergCreating coverage reports 1357330f729Sjoerg========================= 1367330f729Sjoerg 1377330f729SjoergRaw profiles have to be **indexed** before they can be used to generate 1387330f729Sjoergcoverage reports. This is done using the "merge" tool in ``llvm-profdata`` 1397330f729Sjoerg(which can combine multiple raw profiles and index them at the same time): 1407330f729Sjoerg 1417330f729Sjoerg.. code-block:: console 1427330f729Sjoerg 1437330f729Sjoerg # Step 3(a): Index the raw profile. 1447330f729Sjoerg % llvm-profdata merge -sparse foo.profraw -o foo.profdata 1457330f729Sjoerg 1467330f729SjoergThere are multiple different ways to render coverage reports. The simplest 1477330f729Sjoergoption is to generate a line-oriented report: 1487330f729Sjoerg 1497330f729Sjoerg.. code-block:: console 1507330f729Sjoerg 1517330f729Sjoerg # Step 3(b): Create a line-oriented coverage report. 1527330f729Sjoerg % llvm-cov show ./foo -instr-profile=foo.profdata 1537330f729Sjoerg 1547330f729SjoergThis report includes a summary view as well as dedicated sub-views for 1557330f729Sjoergtemplated functions and their instantiations. For our example program, we get 1567330f729Sjoergdistinct views for ``foo<int>(...)`` and ``foo<float>(...)``. If 1577330f729Sjoerg``-show-line-counts-or-regions`` is enabled, ``llvm-cov`` displays sub-line 1587330f729Sjoergregion counts (even in macro expansions): 1597330f729Sjoerg 1607330f729Sjoerg.. code-block:: none 1617330f729Sjoerg 1627330f729Sjoerg 1| 20|#define BAR(x) ((x) || (x)) 1637330f729Sjoerg ^20 ^2 1647330f729Sjoerg 2| 2|template <typename T> void foo(T x) { 1657330f729Sjoerg 3| 22| for (unsigned I = 0; I < 10; ++I) { BAR(I); } 1667330f729Sjoerg ^22 ^20 ^20^20 1677330f729Sjoerg 4| 2|} 1687330f729Sjoerg ------------------ 1697330f729Sjoerg | void foo<int>(int): 1707330f729Sjoerg | 2| 1|template <typename T> void foo(T x) { 1717330f729Sjoerg | 3| 11| for (unsigned I = 0; I < 10; ++I) { BAR(I); } 1727330f729Sjoerg | ^11 ^10 ^10^10 1737330f729Sjoerg | 4| 1|} 1747330f729Sjoerg ------------------ 1757330f729Sjoerg | void foo<float>(int): 1767330f729Sjoerg | 2| 1|template <typename T> void foo(T x) { 1777330f729Sjoerg | 3| 11| for (unsigned I = 0; I < 10; ++I) { BAR(I); } 1787330f729Sjoerg | ^11 ^10 ^10^10 1797330f729Sjoerg | 4| 1|} 1807330f729Sjoerg ------------------ 1817330f729Sjoerg 182*e038c9c4SjoergIf ``--show-branches=count`` and ``--show-expansions`` are also enabled, the 183*e038c9c4Sjoergsub-views will show detailed branch coverage information in addition to the 184*e038c9c4Sjoergregion counts: 185*e038c9c4Sjoerg 186*e038c9c4Sjoerg.. code-block:: none 187*e038c9c4Sjoerg 188*e038c9c4Sjoerg ------------------ 189*e038c9c4Sjoerg | void foo<float>(int): 190*e038c9c4Sjoerg | 2| 1|template <typename T> void foo(T x) { 191*e038c9c4Sjoerg | 3| 11| for (unsigned I = 0; I < 10; ++I) { BAR(I); } 192*e038c9c4Sjoerg | ^11 ^10 ^10^10 193*e038c9c4Sjoerg | ------------------ 194*e038c9c4Sjoerg | | | 1| 10|#define BAR(x) ((x) || (x)) 195*e038c9c4Sjoerg | | | ^10 ^1 196*e038c9c4Sjoerg | | | ------------------ 197*e038c9c4Sjoerg | | | | Branch (1:17): [True: 9, False: 1] 198*e038c9c4Sjoerg | | | | Branch (1:24): [True: 0, False: 1] 199*e038c9c4Sjoerg | | | ------------------ 200*e038c9c4Sjoerg | ------------------ 201*e038c9c4Sjoerg | | Branch (3:23): [True: 10, False: 1] 202*e038c9c4Sjoerg | ------------------ 203*e038c9c4Sjoerg | 4| 1|} 204*e038c9c4Sjoerg ------------------ 205*e038c9c4Sjoerg 206*e038c9c4Sjoerg 2077330f729SjoergTo generate a file-level summary of coverage statistics instead of a 2087330f729Sjoergline-oriented report, try: 2097330f729Sjoerg 2107330f729Sjoerg.. code-block:: console 2117330f729Sjoerg 2127330f729Sjoerg # Step 3(c): Create a coverage summary. 2137330f729Sjoerg % llvm-cov report ./foo -instr-profile=foo.profdata 214*e038c9c4Sjoerg Filename Regions Missed Regions Cover Functions Missed Functions Executed Lines Missed Lines Cover Branches Missed Branches Cover 215*e038c9c4Sjoerg -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 216*e038c9c4Sjoerg /tmp/foo.cc 13 0 100.00% 3 0 100.00% 13 0 100.00% 12 2 83.33% 217*e038c9c4Sjoerg -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 218*e038c9c4Sjoerg TOTAL 13 0 100.00% 3 0 100.00% 13 0 100.00% 12 2 83.33% 2197330f729Sjoerg 2207330f729SjoergThe ``llvm-cov`` tool supports specifying a custom demangler, writing out 2217330f729Sjoergreports in a directory structure, and generating html reports. For the full 2227330f729Sjoerglist of options, please refer to the `command guide 2237330f729Sjoerg<https://llvm.org/docs/CommandGuide/llvm-cov.html>`_. 2247330f729Sjoerg 2257330f729SjoergA few final notes: 2267330f729Sjoerg 2277330f729Sjoerg* The ``-sparse`` flag is optional but can result in dramatically smaller 2287330f729Sjoerg indexed profiles. This option should not be used if the indexed profile will 2297330f729Sjoerg be reused for PGO. 2307330f729Sjoerg 2317330f729Sjoerg* Raw profiles can be discarded after they are indexed. Advanced use of the 2327330f729Sjoerg profile runtime library allows an instrumented program to merge profiling 2337330f729Sjoerg information directly into an existing raw profile on disk. The details are 2347330f729Sjoerg out of scope. 2357330f729Sjoerg 2367330f729Sjoerg* The ``llvm-profdata`` tool can be used to merge together multiple raw or 2377330f729Sjoerg indexed profiles. To combine profiling data from multiple runs of a program, 2387330f729Sjoerg try e.g: 2397330f729Sjoerg 2407330f729Sjoerg .. code-block:: console 2417330f729Sjoerg 2427330f729Sjoerg % llvm-profdata merge -sparse foo1.profraw foo2.profdata -o foo3.profdata 2437330f729Sjoerg 2447330f729SjoergExporting coverage data 2457330f729Sjoerg======================= 2467330f729Sjoerg 2477330f729SjoergCoverage data can be exported into JSON using the ``llvm-cov export`` 2487330f729Sjoergsub-command. There is a comprehensive reference which defines the structure of 2497330f729Sjoergthe exported data at a high level in the llvm-cov source code. 2507330f729Sjoerg 2517330f729SjoergInterpreting reports 2527330f729Sjoerg==================== 2537330f729Sjoerg 254*e038c9c4SjoergThere are five statistics tracked in a coverage summary: 2557330f729Sjoerg 2567330f729Sjoerg* Function coverage is the percentage of functions which have been executed at 2577330f729Sjoerg least once. A function is considered to be executed if any of its 2587330f729Sjoerg instantiations are executed. 2597330f729Sjoerg 2607330f729Sjoerg* Instantiation coverage is the percentage of function instantiations which 2617330f729Sjoerg have been executed at least once. Template functions and static inline 2627330f729Sjoerg functions from headers are two kinds of functions which may have multiple 263*e038c9c4Sjoerg instantiations. This statistic is hidden by default in reports, but can be 264*e038c9c4Sjoerg enabled via the ``-show-instantiation-summary`` option. 2657330f729Sjoerg 2667330f729Sjoerg* Line coverage is the percentage of code lines which have been executed at 2677330f729Sjoerg least once. Only executable lines within function bodies are considered to be 2687330f729Sjoerg code lines. 2697330f729Sjoerg 2707330f729Sjoerg* Region coverage is the percentage of code regions which have been executed at 2717330f729Sjoerg least once. A code region may span multiple lines (e.g in a large function 2727330f729Sjoerg body with no control flow). However, it's also possible for a single line to 2737330f729Sjoerg contain multiple code regions (e.g in "return x || y && z"). 2747330f729Sjoerg 275*e038c9c4Sjoerg* Branch coverage is the percentage of "true" and "false" branches that have 276*e038c9c4Sjoerg been taken at least once. Each branch is tied to individual conditions in the 277*e038c9c4Sjoerg source code that may each evaluate to either "true" or "false". These 278*e038c9c4Sjoerg conditions may comprise larger boolean expressions linked by boolean logical 279*e038c9c4Sjoerg operators. For example, "x = (y == 2) || (z < 10)" is a boolean expression 280*e038c9c4Sjoerg that is comprised of two individual conditions, each of which evaluates to 281*e038c9c4Sjoerg either true or false, producing four total branch outcomes. 282*e038c9c4Sjoerg 283*e038c9c4SjoergOf these five statistics, function coverage is usually the least granular while 284*e038c9c4Sjoergbranch coverage is the most granular. 100% branch coverage for a function 285*e038c9c4Sjoergimplies 100% region coverage for a function. The project-wide totals for each 2867330f729Sjoergstatistic are listed in the summary. 2877330f729Sjoerg 2887330f729SjoergFormat compatibility guarantees 2897330f729Sjoerg=============================== 2907330f729Sjoerg 2917330f729Sjoerg* There are no backwards or forwards compatibility guarantees for the raw 2927330f729Sjoerg profile format. Raw profiles may be dependent on the specific compiler 2937330f729Sjoerg revision used to generate them. It's inadvisable to store raw profiles for 2947330f729Sjoerg long periods of time. 2957330f729Sjoerg 2967330f729Sjoerg* Tools must retain **backwards** compatibility with indexed profile formats. 2977330f729Sjoerg These formats are not forwards-compatible: i.e, a tool which uses format 2987330f729Sjoerg version X will not be able to understand format version (X+k). 2997330f729Sjoerg 3007330f729Sjoerg* Tools must also retain **backwards** compatibility with the format of the 3017330f729Sjoerg coverage mappings emitted into instrumented binaries. These formats are not 3027330f729Sjoerg forwards-compatible. 3037330f729Sjoerg 3047330f729Sjoerg* The JSON coverage export format has a (major, minor, patch) version triple. 3057330f729Sjoerg Only a major version increment indicates a backwards-incompatible change. A 3067330f729Sjoerg minor version increment is for added functionality, and patch version 3077330f729Sjoerg increments are for bugfixes. 3087330f729Sjoerg 309*e038c9c4SjoergImpact of llvm optimizations on coverage reports 310*e038c9c4Sjoerg================================================ 311*e038c9c4Sjoerg 312*e038c9c4Sjoergllvm optimizations (such as inlining or CFG simplification) should have no 313*e038c9c4Sjoergimpact on coverage report quality. This is due to the fact that the mapping 314*e038c9c4Sjoergfrom source regions to profile counters is immutable, and is generated before 315*e038c9c4Sjoergthe llvm optimizer kicks in. The optimizer can't prove that profile counter 316*e038c9c4Sjoerginstrumentation is safe to delete (because it's not: it affects the profile the 317*e038c9c4Sjoergprogram emits), and so leaves it alone. 318*e038c9c4Sjoerg 319*e038c9c4SjoergNote that this coverage feature does not rely on information that can degrade 320*e038c9c4Sjoergduring the course of optimization, such as debug info line tables. 321*e038c9c4Sjoerg 3227330f729SjoergUsing the profiling runtime without static initializers 3237330f729Sjoerg======================================================= 3247330f729Sjoerg 3257330f729SjoergBy default the compiler runtime uses a static initializer to determine the 3267330f729Sjoergprofile output path and to register a writer function. To collect profiles 3277330f729Sjoergwithout using static initializers, do this manually: 3287330f729Sjoerg 3297330f729Sjoerg* Export a ``int __llvm_profile_runtime`` symbol from each instrumented shared 3307330f729Sjoerg library and executable. When the linker finds a definition of this symbol, it 3317330f729Sjoerg knows to skip loading the object which contains the profiling runtime's 3327330f729Sjoerg static initializer. 3337330f729Sjoerg 3347330f729Sjoerg* Forward-declare ``void __llvm_profile_initialize_file(void)`` and call it 3357330f729Sjoerg once from each instrumented executable. This function parses 3367330f729Sjoerg ``LLVM_PROFILE_FILE``, sets the output path, and truncates any existing files 3377330f729Sjoerg at that path. To get the same behavior without truncating existing files, 3387330f729Sjoerg pass a filename pattern string to ``void __llvm_profile_set_filename(char 3397330f729Sjoerg *)``. These calls can be placed anywhere so long as they precede all calls 3407330f729Sjoerg to ``__llvm_profile_write_file``. 3417330f729Sjoerg 3427330f729Sjoerg* Forward-declare ``int __llvm_profile_write_file(void)`` and call it to write 3437330f729Sjoerg out a profile. This function returns 0 when it succeeds, and a non-zero value 3447330f729Sjoerg otherwise. Calling this function multiple times appends profile data to an 3457330f729Sjoerg existing on-disk raw profile. 3467330f729Sjoerg 3477330f729SjoergIn C++ files, declare these as ``extern "C"``. 3487330f729Sjoerg 349*e038c9c4SjoergUsing the profiling runtime without a filesystem 350*e038c9c4Sjoerg------------------------------------------------ 351*e038c9c4Sjoerg 352*e038c9c4SjoergThe profiling runtime also supports freestanding environments that lack a 353*e038c9c4Sjoergfilesystem. The runtime ships as a static archive that's structured to make 354*e038c9c4Sjoergdependencies on a hosted environment optional, depending on what features 355*e038c9c4Sjoergthe client application uses. 356*e038c9c4Sjoerg 357*e038c9c4SjoergThe first step is to export ``__llvm_profile_runtime``, as above, to disable 358*e038c9c4Sjoergthe default static initializers. Instead of calling the ``*_file()`` APIs 359*e038c9c4Sjoergdescribed above, use the following to save the profile directly to a buffer 360*e038c9c4Sjoergunder your control: 361*e038c9c4Sjoerg 362*e038c9c4Sjoerg* Forward-declare ``uint64_t __llvm_profile_get_size_for_buffer(void)`` and 363*e038c9c4Sjoerg call it to determine the size of the profile. You'll need to allocate a 364*e038c9c4Sjoerg buffer of this size. 365*e038c9c4Sjoerg 366*e038c9c4Sjoerg* Forward-declare ``int __llvm_profile_write_buffer(char *Buffer)`` and call it 367*e038c9c4Sjoerg to copy the current counters to ``Buffer``, which is expected to already be 368*e038c9c4Sjoerg allocated and big enough for the profile. 369*e038c9c4Sjoerg 370*e038c9c4Sjoerg* Optionally, forward-declare ``void __llvm_profile_reset_counters(void)`` and 371*e038c9c4Sjoerg call it to reset the counters before entering a specific section to be 372*e038c9c4Sjoerg profiled. This is only useful if there is some setup that should be excluded 373*e038c9c4Sjoerg from the profile. 374*e038c9c4Sjoerg 375*e038c9c4SjoergIn C++ files, declare these as ``extern "C"``. 376*e038c9c4Sjoerg 3777330f729SjoergCollecting coverage reports for the llvm project 3787330f729Sjoerg================================================ 3797330f729Sjoerg 3807330f729SjoergTo prepare a coverage report for llvm (and any of its sub-projects), add 3817330f729Sjoerg``-DLLVM_BUILD_INSTRUMENTED_COVERAGE=On`` to the cmake configuration. Raw 3827330f729Sjoergprofiles will be written to ``$BUILD_DIR/profiles/``. To prepare an html 3837330f729Sjoergreport, run ``llvm/utils/prepare-code-coverage-artifact.py``. 3847330f729Sjoerg 3857330f729SjoergTo specify an alternate directory for raw profiles, use 3867330f729Sjoerg``-DLLVM_PROFILE_DATA_DIR``. To change the size of the profile merge pool, use 3877330f729Sjoerg``-DLLVM_PROFILE_MERGE_POOL_SIZE``. 3887330f729Sjoerg 3897330f729SjoergDrawbacks and limitations 3907330f729Sjoerg========================= 3917330f729Sjoerg 3927330f729Sjoerg* Prior to version 2.26, the GNU binutils BFD linker is not able link programs 3937330f729Sjoerg compiled with ``-fcoverage-mapping`` in its ``--gc-sections`` mode. Possible 3947330f729Sjoerg workarounds include disabling ``--gc-sections``, upgrading to a newer version 3957330f729Sjoerg of BFD, or using the Gold linker. 3967330f729Sjoerg 3977330f729Sjoerg* Code coverage does not handle unpredictable changes in control flow or stack 3987330f729Sjoerg unwinding in the presence of exceptions precisely. Consider the following 3997330f729Sjoerg function: 4007330f729Sjoerg 4017330f729Sjoerg .. code-block:: cpp 4027330f729Sjoerg 4037330f729Sjoerg int f() { 4047330f729Sjoerg may_throw(); 4057330f729Sjoerg return 0; 4067330f729Sjoerg } 4077330f729Sjoerg 4087330f729Sjoerg If the call to ``may_throw()`` propagates an exception into ``f``, the code 4097330f729Sjoerg coverage tool may mark the ``return`` statement as executed even though it is 4107330f729Sjoerg not. A call to ``longjmp()`` can have similar effects. 411*e038c9c4Sjoerg 412*e038c9c4SjoergClang implementation details 413*e038c9c4Sjoerg============================ 414*e038c9c4Sjoerg 415*e038c9c4SjoergThis section may be of interest to those wishing to understand or improve 416*e038c9c4Sjoergthe clang code coverage implementation. 417*e038c9c4Sjoerg 418*e038c9c4SjoergGap regions 419*e038c9c4Sjoerg----------- 420*e038c9c4Sjoerg 421*e038c9c4SjoergGap regions are source regions with counts. A reporting tool cannot set a line 422*e038c9c4Sjoergexecution count to the count from a gap region unless that region is the only 423*e038c9c4Sjoergone on a line. 424*e038c9c4Sjoerg 425*e038c9c4SjoergGap regions are used to eliminate unnatural artifacts in coverage reports, such 426*e038c9c4Sjoergas red "unexecuted" highlights present at the end of an otherwise covered line, 427*e038c9c4Sjoergor blue "executed" highlights present at the start of a line that is otherwise 428*e038c9c4Sjoergnot executed. 429*e038c9c4Sjoerg 430*e038c9c4SjoergBranch regions 431*e038c9c4Sjoerg-------------- 432*e038c9c4SjoergWhen viewing branch coverage details in source-based file-level sub-views using 433*e038c9c4Sjoerg``--show-branches``, it is recommended that users show all macro expansions 434*e038c9c4Sjoerg(using option ``--show-expansions``) since macros may contain hidden branch 435*e038c9c4Sjoergconditions. The coverage summary report will always include these macro-based 436*e038c9c4Sjoergboolean expressions in the overall branch coverage count for a function or 437*e038c9c4Sjoergsource file. 438*e038c9c4Sjoerg 439*e038c9c4SjoergBranch coverage is not tracked for constant folded branch conditions since 440*e038c9c4Sjoergbranches are not generated for these cases. In the source-based file-level 441*e038c9c4Sjoergsub-view, these branches will simply be shown as ``[Folded - Ignored]`` so that 442*e038c9c4Sjoergusers are informed about what happened. 443*e038c9c4Sjoerg 444*e038c9c4SjoergBranch coverage is tied directly to branch-generating conditions in the source 445*e038c9c4Sjoergcode. Users should not see hidden branches that aren't actually tied to the 446*e038c9c4Sjoergsource code. 447*e038c9c4Sjoerg 448*e038c9c4Sjoerg 449*e038c9c4SjoergSwitch statements 450*e038c9c4Sjoerg----------------- 451*e038c9c4Sjoerg 452*e038c9c4SjoergThe region mapping for a switch body consists of a gap region that covers the 453*e038c9c4Sjoergentire body (starting from the '{' in 'switch (...) {', and terminating where the 454*e038c9c4Sjoerglast case ends). This gap region has a zero count: this causes "gap" areas in 455*e038c9c4Sjoergbetween case statements, which contain no executable code, to appear uncovered. 456*e038c9c4Sjoerg 457*e038c9c4SjoergWhen a switch case is visited, the parent region is extended: if the parent 458*e038c9c4Sjoergregion has no start location, its start location becomes the start of the case. 459*e038c9c4SjoergThis is used to support switch statements without a ``CompoundStmt`` body, in 460*e038c9c4Sjoergwhich the switch body and the single case share a count. 461*e038c9c4Sjoerg 462*e038c9c4SjoergFor switches with ``CompoundStmt`` bodies, a new region is created at the start 463*e038c9c4Sjoergof each switch case. 464*e038c9c4Sjoerg 465*e038c9c4SjoergBranch regions are also generated for each switch case, including the default 466*e038c9c4Sjoergcase. If there is no explicitly defined default case in the source code, a 467*e038c9c4Sjoergbranch region is generated to correspond to the implicit default case that is 468*e038c9c4Sjoerggenerated by the compiler. The implicit branch region is tied to the line and 469*e038c9c4Sjoergcolumn number of the switch statement condition since no source code for the 470*e038c9c4Sjoergimplicit case exists. 471