blob: 73910e134a589114e1006d626fde2c0776b42a12 [file] [log] [blame]
Vedant Kumara530a362016-06-02 00:51:501==========================
2Source-based Code Coverage
3==========================
4
5.. contents::
6 :local:
7
8Introduction
9============
10
11This document explains how to use clang's source-based code coverage feature.
12It's called "source-based" because it operates on AST and preprocessor
13information directly. This allows it to generate very precise coverage data.
14
15Clang ships two other code coverage implementations:
16
17* :doc:`SanitizerCoverage` - A low-overhead tool meant for use alongside the
18 various sanitizers. It can provide up to edge-level coverage.
19
20* gcov - A GCC-compatible coverage implementation which operates on DebugInfo.
Vedant Kumar6eed0d52017-02-09 21:33:2121 This is enabled by ``-ftest-coverage`` or ``--coverage``.
Vedant Kumara530a362016-06-02 00:51:5022
23From this point onwards "code coverage" will refer to the source-based kind.
24
25The code coverage workflow
26==========================
27
28The code coverage workflow consists of three main steps:
29
Vedant Kumar0819f362016-06-02 02:25:1330* Compiling with coverage enabled.
Vedant Kumara530a362016-06-02 00:51:5031
Vedant Kumar0819f362016-06-02 02:25:1332* Running the instrumented program.
Vedant Kumara530a362016-06-02 00:51:5033
Vedant Kumar0819f362016-06-02 02:25:1334* Creating coverage reports.
Vedant Kumara530a362016-06-02 00:51:5035
36The next few sections work through a complete, copy-'n-paste friendly example
37based on this program:
38
Vedant Kumar4c1112c2016-06-02 01:15:5939.. code-block:: cpp
Vedant Kumara530a362016-06-02 00:51:5040
41 % cat <<EOF > foo.cc
42 #define BAR(x) ((x) || (x))
43 template <typename T> void foo(T x) {
44 for (unsigned I = 0; I < 10; ++I) { BAR(I); }
45 }
46 int main() {
47 foo<int>(0);
48 foo<float>(0);
49 return 0;
50 }
51 EOF
52
53Compiling with coverage enabled
54===============================
55
Vedant Kumar6c53d8f2016-06-02 02:45:5956To compile code with coverage enabled, pass ``-fprofile-instr-generate
Vedant Kumara530a362016-06-02 00:51:5057-fcoverage-mapping`` to the compiler:
58
59.. code-block:: console
60
61 # Step 1: Compile with coverage enabled.
62 % clang++ -fprofile-instr-generate -fcoverage-mapping foo.cc -o foo
63
64Note that linking together code with and without coverage instrumentation is
Vedant Kumar74c3fd12016-09-22 15:34:3365supported. Uninstrumented code simply won't be accounted for in reports.
Vedant Kumara530a362016-06-02 00:51:5066
Alan Phipps8789b7e2024-01-22 20:27:1667To compile code with Modified Condition/Decision Coverage (MC/DC) enabled,
68pass ``-fcoverage-mcdc`` in addition to the clang options specified above.
69MC/DC is an advanced form of code coverage most applicable in the embedded
70space.
71
Vedant Kumara530a362016-06-02 00:51:5072Running the instrumented program
73================================
74
75The next step is to run the instrumented program. When the program exits it
76will write a **raw profile** to the path specified by the ``LLVM_PROFILE_FILE``
Vedant Kumar0819f362016-06-02 02:25:1377environment variable. If that variable does not exist, the profile is written
78to ``default.profraw`` in the current directory of the program. If
79``LLVM_PROFILE_FILE`` contains a path to a non-existent directory, the missing
80directory structure will be created. Additionally, the following special
81**pattern strings** are rewritten:
Vedant Kumara530a362016-06-02 00:51:5082
83* "%p" expands out to the process ID.
84
85* "%h" expands out to the hostname of the machine running the program.
86
Vedant Kumar62c37272020-09-08 21:45:4187* "%t" expands out to the value of the ``TMPDIR`` environment variable. On
88 Darwin, this is typically set to a temporary scratch directory.
89
Vedant Kumarf3300c92016-06-14 00:42:1290* "%Nm" expands out to the instrumented binary's signature. When this pattern
91 is specified, the runtime creates a pool of N raw profiles which are used for
92 on-line profile merging. The runtime takes care of selecting a raw profile
93 from the pool, locking it, and updating it before the program exits. If N is
Nikolas Klauserf6d557e2023-06-26 01:59:5694 not specified (i.e the pattern is "%m"), it's assumed that ``N = 1``. The
Zequan Wu62c4c612023-05-11 21:16:1695 merge pool specifier can only occur once per filename pattern.
Vedant Kumarf3300c92016-06-14 00:42:1296
Vedant Kumard889d1e2019-09-19 18:56:4397* "%c" expands out to nothing, but enables a mode in which profile counter
98 updates are continuously synced to a file. This means that if the
99 instrumented program crashes, or is killed by a signal, perfect coverage
Vedant Kumar2492b5a2019-11-12 18:24:23100 information can still be recovered. Continuous mode does not support value
101 profiling for PGO, and is only supported on Darwin at the moment. Support for
Petr Hosekd3db13a2019-10-04 20:29:56102 Linux may be mostly complete but requires testing, and support for Windows
103 may require more extensive changes: please get involved if you are interested
104 in porting this feature.
Vedant Kumard889d1e2019-09-19 18:56:43105
Vedant Kumara530a362016-06-02 00:51:50106.. code-block:: console
107
108 # Step 2: Run the program.
109 % LLVM_PROFILE_FILE="foo.profraw" ./foo
110
Petr Hosekd3db13a2019-10-04 20:29:56111Note that continuous mode is also used on Fuchsia where it's the only supported
112mode, but the implementation is different. The Darwin and Linux implementation
113relies on padding and the ability to map a file over the existing memory
114mapping which is generally only available on POSIX systems and isn't suitable
115for other platforms.
116
Nico Weberb50431de2020-02-10 18:51:23117On Fuchsia, we rely on the ability to relocate counters at runtime using a
Petr Hosekd3db13a2019-10-04 20:29:56118level of indirection. On every counter access, we add a bias to the counter
119address. This bias is stored in ``__llvm_profile_counter_bias`` symbol that's
120provided by the profile runtime and is initially set to zero, meaning no
Nico Weberb50431de2020-02-10 18:51:23121relocation. The runtime can map the profile into memory at arbitrary locations,
Petr Hosekd3db13a2019-10-04 20:29:56122and set bias to the offset between the original and the new counter location,
123at which point every subsequent counter access will be to the new location,
Nico Weberb50431de2020-02-10 18:51:23124which allows updating profile directly akin to the continuous mode.
Petr Hosekd3db13a2019-10-04 20:29:56125
126The advantage of this approach is that doesn't require any special OS support.
127The disadvantage is the extra overhead due to additional instructions required
128for each counter access (overhead both in terms of binary size and performance)
129plus duplication of counters (i.e. one copy in the binary itself and another
130copy that's mapped into memory). This implementation can be also enabled for
131other platforms by passing the ``-runtime-counter-relocation`` option to the
132backend during compilation.
133
Aaron Ballman38213912023-01-28 13:56:22134For a program such as the `Lit <https://llvm.org/docs/CommandGuide/lit.html>`_
135testing tool which invokes other programs, it may be necessary to set
136``LLVM_PROFILE_FILE`` for each invocation. The pattern strings "%p" or "%Nm"
137may help to avoid corruption due to concurrency. Note that "%p" is also a Lit
138token and needs to be escaped as "%%p".
Flash Sheridan01f13f42023-01-28 01:51:18139
Petr Hosekd3db13a2019-10-04 20:29:56140.. code-block:: console
141
142 % clang++ -fprofile-instr-generate -fcoverage-mapping -mllvm -runtime-counter-relocation foo.cc -o foo
143
Vedant Kumara530a362016-06-02 00:51:50144Creating coverage reports
145=========================
146
Vedant Kumar0819f362016-06-02 02:25:13147Raw profiles have to be **indexed** before they can be used to generate
Vedant Kumar74c3fd12016-09-22 15:34:33148coverage reports. This is done using the "merge" tool in ``llvm-profdata``
149(which can combine multiple raw profiles and index them at the same time):
Vedant Kumara530a362016-06-02 00:51:50150
151.. code-block:: console
152
153 # Step 3(a): Index the raw profile.
154 % llvm-profdata merge -sparse foo.profraw -o foo.profdata
155
Flash Sheridan01f13f42023-01-28 01:51:18156For an example of merging multiple profiles created by testing,
157see the LLVM `coverage build script <https://github.com/llvm/llvm-zorg/blob/main/zorg/jenkins/jobs/jobs/llvm-coverage>`_.
158
Vedant Kumar74c3fd12016-09-22 15:34:33159There are multiple different ways to render coverage reports. The simplest
160option is to generate a line-oriented report:
Vedant Kumara530a362016-06-02 00:51:50161
162.. code-block:: console
163
164 # Step 3(b): Create a line-oriented coverage report.
165 % llvm-cov show ./foo -instr-profile=foo.profdata
166
Vedant Kumara530a362016-06-02 00:51:50167This report includes a summary view as well as dedicated sub-views for
168templated functions and their instantiations. For our example program, we get
169distinct views for ``foo<int>(...)`` and ``foo<float>(...)``. If
170``-show-line-counts-or-regions`` is enabled, ``llvm-cov`` displays sub-line
171region counts (even in macro expansions):
172
George Burgess IVbc8cc5ac2016-06-21 02:19:43173.. code-block:: none
Vedant Kumara530a362016-06-02 00:51:50174
Vedant Kumar9ed58022016-09-19 01:42:38175 1| 20|#define BAR(x) ((x) || (x))
Vedant Kumara530a362016-06-02 00:51:50176 ^20 ^2
177 2| 2|template <typename T> void foo(T x) {
Vedant Kumar9ed58022016-09-19 01:42:38178 3| 22| for (unsigned I = 0; I < 10; ++I) { BAR(I); }
Vedant Kumara530a362016-06-02 00:51:50179 ^22 ^20 ^20^20
Vedant Kumar9ed58022016-09-19 01:42:38180 4| 2|}
Vedant Kumara530a362016-06-02 00:51:50181 ------------------
182 | void foo<int>(int):
Vedant Kumar9ed58022016-09-19 01:42:38183 | 2| 1|template <typename T> void foo(T x) {
184 | 3| 11| for (unsigned I = 0; I < 10; ++I) { BAR(I); }
Vedant Kumara530a362016-06-02 00:51:50185 | ^11 ^10 ^10^10
Vedant Kumar9ed58022016-09-19 01:42:38186 | 4| 1|}
Vedant Kumara530a362016-06-02 00:51:50187 ------------------
188 | void foo<float>(int):
Vedant Kumar9ed58022016-09-19 01:42:38189 | 2| 1|template <typename T> void foo(T x) {
190 | 3| 11| for (unsigned I = 0; I < 10; ++I) { BAR(I); }
Vedant Kumara530a362016-06-02 00:51:50191 | ^11 ^10 ^10^10
Vedant Kumar9ed58022016-09-19 01:42:38192 | 4| 1|}
Vedant Kumara530a362016-06-02 00:51:50193 ------------------
194
Alan Phipps9f2967b2020-12-28 17:20:48195If ``--show-branches=count`` and ``--show-expansions`` are also enabled, the
196sub-views will show detailed branch coverage information in addition to the
197region counts:
198
199.. code-block:: none
200
201 ------------------
202 | void foo<float>(int):
203 | 2| 1|template <typename T> void foo(T x) {
204 | 3| 11| for (unsigned I = 0; I < 10; ++I) { BAR(I); }
205 | ^11 ^10 ^10^10
206 | ------------------
207 | | | 1| 10|#define BAR(x) ((x) || (x))
208 | | | ^10 ^1
209 | | | ------------------
210 | | | | Branch (1:17): [True: 9, False: 1]
211 | | | | Branch (1:24): [True: 0, False: 1]
212 | | | ------------------
213 | ------------------
214 | | Branch (3:23): [True: 10, False: 1]
215 | ------------------
216 | 4| 1|}
217 ------------------
218
Alan Phipps8789b7e2024-01-22 20:27:16219If the application was instrumented for Modified Condition/Decision Coverage
220(MC/DC) using the clang option ``-fcoverage-mcdc``, an MC/DC subview can be
221enabled using ``--show-mcdc`` that will show detailed MC/DC information for
222each complex condition boolean expression containing at most six conditions.
Alan Phipps9f2967b2020-12-28 17:20:48223
Vedant Kumar74c3fd12016-09-22 15:34:33224To generate a file-level summary of coverage statistics instead of a
225line-oriented report, try:
Vedant Kumara530a362016-06-02 00:51:50226
227.. code-block:: console
228
229 # Step 3(c): Create a coverage summary.
230 % llvm-cov report ./foo -instr-profile=foo.profdata
Alan Phipps9f2967b2020-12-28 17:20:48231 Filename Regions Missed Regions Cover Functions Missed Functions Executed Lines Missed Lines Cover Branches Missed Branches Cover
232 --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
233 /tmp/foo.cc 13 0 100.00% 3 0 100.00% 13 0 100.00% 12 2 83.33%
234 --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
235 TOTAL 13 0 100.00% 3 0 100.00% 13 0 100.00% 12 2 83.33%
Vedant Kumara530a362016-06-02 00:51:50236
Vedant Kumar74c3fd12016-09-22 15:34:33237The ``llvm-cov`` tool supports specifying a custom demangler, writing out
238reports in a directory structure, and generating html reports. For the full
239list of options, please refer to the `command guide
Sylvestre Ledrubc5c3f52018-11-04 17:02:00240<https://llvm.org/docs/CommandGuide/llvm-cov.html>`_.
Vedant Kumar74c3fd12016-09-22 15:34:33241
Vedant Kumara530a362016-06-02 00:51:50242A few final notes:
243
244* The ``-sparse`` flag is optional but can result in dramatically smaller
245 indexed profiles. This option should not be used if the indexed profile will
246 be reused for PGO.
247
248* Raw profiles can be discarded after they are indexed. Advanced use of the
249 profile runtime library allows an instrumented program to merge profiling
250 information directly into an existing raw profile on disk. The details are
251 out of scope.
252
253* The ``llvm-profdata`` tool can be used to merge together multiple raw or
254 indexed profiles. To combine profiling data from multiple runs of a program,
255 try e.g:
256
Vedant Kumar553a0d62016-06-02 17:19:45257 .. code-block:: console
Vedant Kumara530a362016-06-02 00:51:50258
Vedant Kumar553a0d62016-06-02 17:19:45259 % llvm-profdata merge -sparse foo1.profraw foo2.profdata -o foo3.profdata
Vedant Kumara530a362016-06-02 00:51:50260
Vedant Kumar6fe6eae2016-09-20 17:11:18261Exporting coverage data
262=======================
263
264Coverage data can be exported into JSON using the ``llvm-cov export``
265sub-command. There is a comprehensive reference which defines the structure of
266the exported data at a high level in the llvm-cov source code.
267
Vedant Kumar9ed58022016-09-19 01:42:38268Interpreting reports
269====================
270
Alan Phipps8789b7e2024-01-22 20:27:16271There are six statistics tracked in a coverage summary:
Vedant Kumar9ed58022016-09-19 01:42:38272
273* Function coverage is the percentage of functions which have been executed at
Vedant Kumar74c3fd12016-09-22 15:34:33274 least once. A function is considered to be executed if any of its
Vedant Kumar9ed58022016-09-19 01:42:38275 instantiations are executed.
276
277* Instantiation coverage is the percentage of function instantiations which
Vedant Kumar6fe6eae2016-09-20 17:11:18278 have been executed at least once. Template functions and static inline
279 functions from headers are two kinds of functions which may have multiple
Vedant Kumar0c4935b2021-02-12 20:04:57280 instantiations. This statistic is hidden by default in reports, but can be
281 enabled via the ``-show-instantiation-summary`` option.
Vedant Kumar9ed58022016-09-19 01:42:38282
283* Line coverage is the percentage of code lines which have been executed at
Vedant Kumar6fe6eae2016-09-20 17:11:18284 least once. Only executable lines within function bodies are considered to be
Vedant Kumar74c3fd12016-09-22 15:34:33285 code lines.
Vedant Kumar9ed58022016-09-19 01:42:38286
287* Region coverage is the percentage of code regions which have been executed at
Vedant Kumar74c3fd12016-09-22 15:34:33288 least once. A code region may span multiple lines (e.g in a large function
289 body with no control flow). However, it's also possible for a single line to
290 contain multiple code regions (e.g in "return x || y && z").
Vedant Kumar6fe6eae2016-09-20 17:11:18291
Alan Phipps9f2967b2020-12-28 17:20:48292* Branch coverage is the percentage of "true" and "false" branches that have
293 been taken at least once. Each branch is tied to individual conditions in the
294 source code that may each evaluate to either "true" or "false". These
295 conditions may comprise larger boolean expressions linked by boolean logical
296 operators. For example, "x = (y == 2) || (z < 10)" is a boolean expression
297 that is comprised of two individual conditions, each of which evaluates to
298 either true or false, producing four total branch outcomes.
299
Alan Phipps8789b7e2024-01-22 20:27:16300* Modified Condition/Decision Coverage (MC/DC) is the percentage of individual
301 branch conditions that have been shown to independently affect the decision
302 outcome of the boolean expression they comprise. This is accomplished using
303 the analysis of executed control flow through the expression (i.e. test
304 vectors) to show that as a condition's outcome is varied between "true" and
305 false", the decision's outcome also varies between "true" and false", while
306 the outcome of all other conditions is held fixed (or they are masked out as
307 unevaluatable, as happens in languages whose logical operators have
308 short-circuit semantics). MC/DC builds on top of branch coverage and
309 requires that all code blocks and all execution paths have been tested. This
310 statistic is hidden by default in reports, but it can be enabled via the
311 ``-show-mcdc-summary`` option as long as code was also compiled using the
312 clang option ``-fcoverage-mcdc``.
313
314 * Boolean expressions that are only comprised of one condition (and therefore
315 have no logical operators) are not included in MC/DC analysis and are
316 trivially deducible using branch coverage.
317
318Of these six statistics, function coverage is usually the least granular while
319branch coverage (with MC/DC) is the most granular. 100% branch coverage for a
320function implies 100% region coverage for a function. The project-wide totals
321for each statistic are listed in the summary.
Vedant Kumar9ed58022016-09-19 01:42:38322
Vedant Kumara530a362016-06-02 00:51:50323Format compatibility guarantees
324===============================
325
326* There are no backwards or forwards compatibility guarantees for the raw
327 profile format. Raw profiles may be dependent on the specific compiler
328 revision used to generate them. It's inadvisable to store raw profiles for
329 long periods of time.
330
331* Tools must retain **backwards** compatibility with indexed profile formats.
332 These formats are not forwards-compatible: i.e, a tool which uses format
333 version X will not be able to understand format version (X+k).
334
Vedant Kumar74c3fd12016-09-22 15:34:33335* Tools must also retain **backwards** compatibility with the format of the
336 coverage mappings emitted into instrumented binaries. These formats are not
337 forwards-compatible.
Vedant Kumar553a0d62016-06-02 17:19:45338
Vedant Kumar6fe6eae2016-09-20 17:11:18339* The JSON coverage export format has a (major, minor, patch) version triple.
340 Only a major version increment indicates a backwards-incompatible change. A
341 minor version increment is for added functionality, and patch version
342 increments are for bugfixes.
343
Vedant Kumar13bd6fb2021-02-12 20:04:27344Impact of llvm optimizations on coverage reports
345================================================
346
347llvm optimizations (such as inlining or CFG simplification) should have no
348impact on coverage report quality. This is due to the fact that the mapping
349from source regions to profile counters is immutable, and is generated before
350the llvm optimizer kicks in. The optimizer can't prove that profile counter
351instrumentation is safe to delete (because it's not: it affects the profile the
352program emits), and so leaves it alone.
353
354Note that this coverage feature does not rely on information that can degrade
355during the course of optimization, such as debug info line tables.
356
Vedant Kumarb06294d2016-06-07 22:25:29357Using the profiling runtime without static initializers
358=======================================================
359
360By default the compiler runtime uses a static initializer to determine the
361profile output path and to register a writer function. To collect profiles
362without using static initializers, do this manually:
363
Vedant Kumar32a9bfa2016-06-08 22:24:52364* Export a ``int __llvm_profile_runtime`` symbol from each instrumented shared
365 library and executable. When the linker finds a definition of this symbol, it
366 knows to skip loading the object which contains the profiling runtime's
367 static initializer.
Vedant Kumarb06294d2016-06-07 22:25:29368
Vedant Kumar32a9bfa2016-06-08 22:24:52369* Forward-declare ``void __llvm_profile_initialize_file(void)`` and call it
370 once from each instrumented executable. This function parses
371 ``LLVM_PROFILE_FILE``, sets the output path, and truncates any existing files
372 at that path. To get the same behavior without truncating existing files,
373 pass a filename pattern string to ``void __llvm_profile_set_filename(char
374 *)``. These calls can be placed anywhere so long as they precede all calls
375 to ``__llvm_profile_write_file``.
Vedant Kumarb06294d2016-06-07 22:25:29376
Vedant Kumar32a9bfa2016-06-08 22:24:52377* Forward-declare ``int __llvm_profile_write_file(void)`` and call it to write
Vedant Kumar89262b62016-06-08 22:32:03378 out a profile. This function returns 0 when it succeeds, and a non-zero value
379 otherwise. Calling this function multiple times appends profile data to an
380 existing on-disk raw profile.
Vedant Kumarb06294d2016-06-07 22:25:29381
Nico Weberb1706ca2017-01-25 16:01:32382In C++ files, declare these as ``extern "C"``.
383
Duncan P. N. Exon Smithd4ee6032021-04-21 22:00:51384Using the profiling runtime without a filesystem
385------------------------------------------------
386
387The profiling runtime also supports freestanding environments that lack a
388filesystem. The runtime ships as a static archive that's structured to make
389dependencies on a hosted environment optional, depending on what features
390the client application uses.
391
392The first step is to export ``__llvm_profile_runtime``, as above, to disable
393the default static initializers. Instead of calling the ``*_file()`` APIs
394described above, use the following to save the profile directly to a buffer
395under your control:
396
397* Forward-declare ``uint64_t __llvm_profile_get_size_for_buffer(void)`` and
398 call it to determine the size of the profile. You'll need to allocate a
399 buffer of this size.
400
401* Forward-declare ``int __llvm_profile_write_buffer(char *Buffer)`` and call it
402 to copy the current counters to ``Buffer``, which is expected to already be
403 allocated and big enough for the profile.
404
405* Optionally, forward-declare ``void __llvm_profile_reset_counters(void)`` and
406 call it to reset the counters before entering a specific section to be
407 profiled. This is only useful if there is some setup that should be excluded
408 from the profile.
409
410In C++ files, declare these as ``extern "C"``.
411
Vedant Kumar6fe6eae2016-09-20 17:11:18412Collecting coverage reports for the llvm project
413================================================
414
415To prepare a coverage report for llvm (and any of its sub-projects), add
416``-DLLVM_BUILD_INSTRUMENTED_COVERAGE=On`` to the cmake configuration. Raw
417profiles will be written to ``$BUILD_DIR/profiles/``. To prepare an html
418report, run ``llvm/utils/prepare-code-coverage-artifact.py``.
419
420To specify an alternate directory for raw profiles, use
421``-DLLVM_PROFILE_DATA_DIR``. To change the size of the profile merge pool, use
422``-DLLVM_PROFILE_MERGE_POOL_SIZE``.
423
Vedant Kumar553a0d62016-06-02 17:19:45424Drawbacks and limitations
425=========================
426
Vedant Kumar82cd7702017-06-19 21:22:05427* Prior to version 2.26, the GNU binutils BFD linker is not able link programs
Vedant Kumar1c5f3122017-06-19 21:26:04428 compiled with ``-fcoverage-mapping`` in its ``--gc-sections`` mode. Possible
429 workarounds include disabling ``--gc-sections``, upgrading to a newer version
430 of BFD, or using the Gold linker.
Vedant Kumar82cd7702017-06-19 21:22:05431
Vedant Kumar62baa4c2016-06-06 15:44:40432* Code coverage does not handle unpredictable changes in control flow or stack
433 unwinding in the presence of exceptions precisely. Consider the following
434 function:
Vedant Kumar553a0d62016-06-02 17:19:45435
436 .. code-block:: cpp
437
438 int f() {
439 may_throw();
440 return 0;
441 }
442
Vedant Kumar62baa4c2016-06-06 15:44:40443 If the call to ``may_throw()`` propagates an exception into ``f``, the code
Vedant Kumar553a0d62016-06-02 17:19:45444 coverage tool may mark the ``return`` statement as executed even though it is
Vedant Kumar62baa4c2016-06-06 15:44:40445 not. A call to ``longjmp()`` can have similar effects.
Vedant Kumar859bf4d2019-11-21 22:17:04446
447Clang implementation details
448============================
449
450This section may be of interest to those wishing to understand or improve
451the clang code coverage implementation.
452
453Gap regions
454-----------
455
456Gap regions are source regions with counts. A reporting tool cannot set a line
457execution count to the count from a gap region unless that region is the only
458one on a line.
459
460Gap regions are used to eliminate unnatural artifacts in coverage reports, such
461as red "unexecuted" highlights present at the end of an otherwise covered line,
462or blue "executed" highlights present at the start of a line that is otherwise
463not executed.
464
Alan Phipps9f2967b2020-12-28 17:20:48465Branch regions
466--------------
467When viewing branch coverage details in source-based file-level sub-views using
468``--show-branches``, it is recommended that users show all macro expansions
469(using option ``--show-expansions``) since macros may contain hidden branch
470conditions. The coverage summary report will always include these macro-based
471boolean expressions in the overall branch coverage count for a function or
472source file.
473
474Branch coverage is not tracked for constant folded branch conditions since
475branches are not generated for these cases. In the source-based file-level
476sub-view, these branches will simply be shown as ``[Folded - Ignored]`` so that
477users are informed about what happened.
478
479Branch coverage is tied directly to branch-generating conditions in the source
480code. Users should not see hidden branches that aren't actually tied to the
481source code.
482
Alan Phipps8789b7e2024-01-22 20:27:16483MC/DC Instrumentation
484---------------------
485
486When instrumenting for Modified Condition/Decision Coverage (MC/DC) using the
NAKAMURA Takumi71f8b442024-06-13 11:09:02487clang option ``-fcoverage-mcdc``, there are two hard limits.
488
489The maximum number of terms is limited to 32767, which is practical for
490handwritten expressions. To be more restrictive in order to enforce coding rules,
491use ``-Xclang -fmcdc-max-conditions=n``. Expressions with exceeded condition
492counts ``n`` will generate warnings and will be excluded in the MC/DC coverage.
493
494The number of test vectors (the maximum number of possible combinations of
495expressions) is limited to 2,147,483,646. In this case, approximately
496256MiB (==2GiB/8) is used to record test vectors.
497
498To reduce memory usage, users can limit the maximum number of test vectors per
499expression with ``-Xclang -fmcdc-max-test-vectors=m``.
500If the number of test vectors resulting from the analysis of an expression
501exceeds ``m``, a warning will be issued and the expression will be excluded
502from the MC/DC coverage.
503
504The number of test vectors ``m``, for ``n`` terms in an expression, can be
505``m <= 2^n`` in the theoretical worst case, but is usually much smaller.
506In simple cases, such as expressions consisting of a sequence of single
507operators, ``m == n+1``. For example, ``(a && b && c && d && e && f && g)``
508requires 8 test vectors.
509
510Expressions such as ``((a0 && b0) || (a1 && b1) || ...)`` can cause the
511number of test vectors to increase exponentially.
Alan Phipps8789b7e2024-01-22 20:27:16512
513Also, if a boolean expression is embedded in the nest of another boolean
514expression but separated by a non-logical operator, this is also not supported.
515For example, in ``x = (a && b && c && func(d && f))``, the ``d && f`` case
516starts a new boolean expression that is separated from the other conditions by
517the operator ``func()``. When this is encountered, a warning will be generated
518and the boolean expression will not be instrumented.
Alan Phipps9f2967b2020-12-28 17:20:48519
Vedant Kumar859bf4d2019-11-21 22:17:04520Switch statements
521-----------------
522
523The region mapping for a switch body consists of a gap region that covers the
524entire body (starting from the '{' in 'switch (...) {', and terminating where the
525last case ends). This gap region has a zero count: this causes "gap" areas in
526between case statements, which contain no executable code, to appear uncovered.
527
528When a switch case is visited, the parent region is extended: if the parent
529region has no start location, its start location becomes the start of the case.
530This is used to support switch statements without a ``CompoundStmt`` body, in
531which the switch body and the single case share a count.
532
533For switches with ``CompoundStmt`` bodies, a new region is created at the start
534of each switch case.
Alan Phipps9f2967b2020-12-28 17:20:48535
536Branch regions are also generated for each switch case, including the default
537case. If there is no explicitly defined default case in the source code, a
538branch region is generated to correspond to the implicit default case that is
539generated by the compiler. The implicit branch region is tied to the line and
540column number of the switch statement condition since no source code for the
541implicit case exists.