Skip to content

Commit f4431e7

Browse files
committed
Add backend-specific EXCLUDE options
The LLVM backedn excludes by regex, and the LCOV backend excludes by glob, which are incompatible with each other. Thus, to better accomodate workflows that may utilize either/both backends, add new LLVM_EXCLUDE and LCOV_EXCLUDE that apply only to that backend. The original EXCLUDE option will still apply to both and can be useful for items such as full file paths.
1 parent 1ac63a9 commit f4431e7

File tree

2 files changed

+49
-9
lines changed

2 files changed

+49
-9
lines changed

README.md

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -192,11 +192,11 @@ To enable any code coverage instrumentation/targets, the single CMake option of
192192
From this point, there are two primary methods for adding instrumentation to targets:
193193
1. A blanket instrumentation by calling `add_code_coverage()`, where all targets in that directory and all subdirectories are automatically instrumented.
194194
2. Per-target instrumentation by calling `target_code_coverage(<TARGET_NAME>)`, where the target is given and thus only that target is instrumented and executables have ccov* targets created. This applies to both libraries and executables.
195-
3. Automatically add coverage for each target with `-DCCOV_TARGETS_HOOK=On` and `-DCCOV_TARGETS_HOOK_ARGS=...` for default values, requires `add_code_coverage()` or similar.
195+
3. Automatically add coverage for each target with `-DCCOV_TARGETS_HOOK=ON` and `-DCCOV_TARGETS_HOOK_ARGS=...` for default values, requires `add_code_coverage()` or similar.
196196

197197
To add coverage targets, such as calling `make ccov` to generate the actual coverage information for perusal or consumption, call `target_code_coverage(<TARGET_NAME>)` on an *executable* target.
198198

199-
**NOTE:** For more options, please check the actual [`code-coverage.cmake`](code-coverage.cmake) file.
199+
**NOTE:** For more options, please check the documentation for each function in the [`code-coverage.cmake`](code-coverage.cmake) file.
200200

201201
#### Example 1 - All targets instrumented
202202

@@ -235,10 +235,21 @@ target_code_coverage(theExe EXCLUDE non_covered.cpp) # As an executable target,
235235
#### Example 3: Target added to the 'ccov' and 'ccov-all' targets
236236

237237
```
238-
add_code_coverage_all_targets(EXCLUDE test/*) # Adds the 'ccov-all' target set and sets it to exclude all files in test/ folders.
238+
# Adds the 'ccov-all' target set and sets it to exclude a specific
239+
# file and all files in test/ folders.
240+
add_code_coverage_all_targets(
241+
EXCLUDE non_covered.cpp
242+
LCOV_EXCLUDE test/*
243+
LLVM_EXCLUDE test/.*)
239244
240245
add_executable(theExe main.cpp non_covered.cpp)
241-
target_code_coverage(theExe AUTO ALL EXCLUDE non_covered.cpp test/*) # As an executable target, adds to the 'ccov' and ccov-all' targets, and the reports will exclude the non-covered.cpp file, and any files in a test/ folder.
246+
# As an executable target, adds to the 'ccov' and ccov-all' targets, and
247+
# the reports will exclude the non-covered.cpp file, and any files in a test/ folder.
248+
target_code_coverage(theExe AUTO ALL
249+
EXCLUDE non_covered.cpp # a file path which applies to both backends
250+
LCOV_EXCLUDE test/* # the GCC/lcov backend excludes by glob
251+
LLVM_EXCLUDE test/.* # the clang/LLVM backend exclude by regext
252+
)
242253
```
243254

244255
#### Example 4: Hook all targets

code-coverage.cmake

Lines changed: 34 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -61,13 +61,18 @@
6161
#
6262
# ~~~
6363
# add_executable(theExe main.cpp non_covered.cpp)
64-
# target_code_coverage(theExe EXCLUDE non_covered.cpp test/*) # As an executable target, the reports will exclude the non-covered.cpp file, and any files in a test/ folder.
64+
# target_code_coverage(theExe
65+
# EXCLUDE non_covered.cpp
66+
# LCOV_EXCLUDE test/*
67+
# LLVM_EXCLUDE test/.*) # As an executable target, the reports will exclude the non-covered.cpp file, and any files in a test/ folder.
6568
# ~~~
6669
#
6770
# Example 3: Target added to the 'ccov' and 'ccov-all' targets
6871
#
6972
# ~~~
70-
# add_code_coverage_all_targets(EXCLUDE test/*) # Adds the 'ccov-all' target set and sets it to exclude all files in test/ folders.
73+
# add_code_coverage_all_targets(
74+
# LCOV_EXCLUDE test/*
75+
# LLVM_EXCLUDE test/.*)# Adds the 'ccov-all' target set and sets it to exclude all files in test/ folders.
7176
#
7277
# add_executable(theExe main.cpp non_covered.cpp)
7378
# target_code_coverage(theExe AUTO ALL EXCLUDE non_covered.cpp test/*) # As an executable target, adds to the 'ccov' and ccov-all' targets, and the reports will exclude the non-covered.cpp file, and any files in a test/ folder.
@@ -234,7 +239,9 @@ endif()
234239
# ALL - Adds the target to the 'ccov-all-*' targets created by a prior call to `add_code_coverage_all_targets` Effective on executable targets.
235240
# EXTERNAL - For GCC's lcov, allows the profiling of 'external' files from the processing directory
236241
# COVERAGE_TARGET_NAME - For executables ONLY, changes the outgoing target name so instead of `ccov-${TARGET_NAME}` it becomes `ccov-${COVERAGE_TARGET_NAME}`.
237-
# EXCLUDE <PATTERNS> - Excludes files of the patterns provided from coverage. Note that GCC/lcov excludes by glob pattern, and clang/LLVM excludes via regex! **These do not copy to the 'all' targets.**
242+
# EXCLUDE <PATTERNS> - Excludes files of the patterns provided from coverage. Added to any lcov/llvm specific excludes. sNote that GCC/lcov excludes by glob pattern, and clang/LLVM excludes via regex! **These do not copy to the 'all' targets.**
243+
# LLVM_EXCLUDE <PATTERNS> - Excludes files that match the provided patterns, LLVM excludes by regex patterns.
244+
# LCOV_EXCLUDE <PATTERNS> - Excludes files that match the provided patterns. LCOV exclude by glob patterns.
238245
# OBJECTS <TARGETS> - For executables ONLY, if the provided targets are static or shared libraries, adds coverage information to the output
239246
# PRE_ARGS <ARGUMENTS> - For executables ONLY, prefixes given arguments to the associated ccov-run-${TARGET_NAME} executable call ($<PRE_ARGS> ccov-*)
240247
# ARGS <ARGUMENTS> - For executables ONLY, appends the given arguments to the associated ccov-run-${TARGET_NAME} executable call (ccov-* $<ARGS>)
@@ -247,7 +254,8 @@ function(target_code_coverage TARGET_NAME)
247254
# Argument parsing
248255
set(options AUTO ALL EXTERNAL PUBLIC INTERFACE PLAIN)
249256
set(single_value_keywords COVERAGE_TARGET_NAME)
250-
set(multi_value_keywords EXCLUDE OBJECTS PRE_ARGS ARGS)
257+
set(multi_value_keywords EXCLUDE LLVM_EXCLUDE LCOV_EXCLUDE OBJECTS PRE_ARGS
258+
ARGS)
251259
cmake_parse_arguments(
252260
target_code_coverage "${options}" "${single_value_keywords}"
253261
"${multi_value_keywords}" ${ARGN})
@@ -392,11 +400,16 @@ function(target_code_coverage TARGET_NAME)
392400
DEPENDS ${target_code_coverage_COVERAGE_TARGET_NAME}.profraw)
393401

394402
# Ignore regex only works on LLVM >= 7
403+
set(EXCLUDE_REGEX)
395404
if(LLVM_COV_VERSION VERSION_GREATER_EQUAL "7.0.0")
396405
foreach(EXCLUDE_ITEM ${target_code_coverage_EXCLUDE})
397406
set(EXCLUDE_REGEX ${EXCLUDE_REGEX}
398407
-ignore-filename-regex='${EXCLUDE_ITEM}')
399408
endforeach()
409+
foreach(EXCLUDE_ITEM ${target_code_coverage_LLVM_EXCLUDE})
410+
set(EXCLUDE_REGEX ${EXCLUDE_REGEX}
411+
-ignore-filename-regex='${EXCLUDE_ITEM}')
412+
endforeach()
400413
endif()
401414

402415
# Print out details of the coverage information to the command line
@@ -451,10 +464,15 @@ function(target_code_coverage TARGET_NAME)
451464
)
452465

453466
# Generate exclusion string for use
467+
set(EXCLUDE_REGEX)
454468
foreach(EXCLUDE_ITEM ${target_code_coverage_EXCLUDE})
455469
set(EXCLUDE_REGEX ${EXCLUDE_REGEX} --remove ${COVERAGE_INFO}
456470
'${EXCLUDE_ITEM}')
457471
endforeach()
472+
foreach(EXCLUDE_ITEM ${target_code_coverage_LCOV_EXCLUDE})
473+
set(EXCLUDE_REGEX ${EXCLUDE_REGEX} --remove ${COVERAGE_INFO}
474+
'${EXCLUDE_ITEM}')
475+
endforeach()
458476

459477
if(EXCLUDE_REGEX)
460478
set(EXCLUDE_COMMAND ${LCOV_PATH} ${EXCLUDE_REGEX} --output-file
@@ -639,14 +657,16 @@ endfunction()
639657
#
640658
# Optional Parameters:
641659
# EXCLUDE <PATTERNS> - Excludes files of the patterns provided from coverage. Note that GCC/lcov excludes by glob pattern, and clang/LLVM excludes via regex!
660+
# LLVM_EXCLUDE <PATTERNS> - Excludes files that match the provided patterns, LLVM excludes by regex patterns.
661+
# LCOV_EXCLUDE <PATTERNS> - Excludes files that match the provided patterns. LCOV exclude by glob patterns.
642662
# ~~~
643663
function(add_code_coverage_all_targets)
644664
if(NOT CODE_COVERAGE)
645665
return()
646666
endif()
647667

648668
# Argument parsing
649-
set(multi_value_keywords EXCLUDE)
669+
set(multi_value_keywords EXCLUDE LLVM_EXCLUDE LCOV_EXCLUDE)
650670
cmake_parse_arguments(add_code_coverage_all_targets "" ""
651671
"${multi_value_keywords}" ${ARGN})
652672

@@ -691,11 +711,16 @@ function(add_code_coverage_all_targets)
691711
endif()
692712

693713
# Regex exclude only available for LLVM >= 7
714+
set(EXCLUDE_REGEX)
694715
if(LLVM_COV_VERSION VERSION_GREATER_EQUAL "7.0.0")
695716
foreach(EXCLUDE_ITEM ${add_code_coverage_all_targets_EXCLUDE})
696717
set(EXCLUDE_REGEX ${EXCLUDE_REGEX}
697718
-ignore-filename-regex='${EXCLUDE_ITEM}')
698719
endforeach()
720+
foreach(EXCLUDE_ITEM ${add_code_coverage_all_targets_LLVM_EXCLUDE})
721+
set(EXCLUDE_REGEX ${EXCLUDE_REGEX}
722+
-ignore-filename-regex='${EXCLUDE_ITEM}')
723+
endforeach()
699724
endif()
700725

701726
# Print summary of the code coverage information to the command line
@@ -781,6 +806,10 @@ function(add_code_coverage_all_targets)
781806
set(EXCLUDE_REGEX ${EXCLUDE_REGEX} --remove ${COVERAGE_INFO}
782807
'${EXCLUDE_ITEM}')
783808
endforeach()
809+
foreach(EXCLUDE_ITEM ${add_code_coverage_all_targets_LCOV_EXCLUDE})
810+
set(EXCLUDE_REGEX ${EXCLUDE_REGEX} --remove ${COVERAGE_INFO}
811+
'${EXCLUDE_ITEM}')
812+
endforeach()
784813

785814
if(EXCLUDE_REGEX)
786815
set(EXCLUDE_COMMAND ${LCOV_PATH} ${EXCLUDE_REGEX} --output-file

0 commit comments

Comments
 (0)