Skip to content

Commit 2155195

Browse files
committed
[Coverage] Map regions from system headers
Originally, the following commit removed mapping coverage regions for system headers: 93205af It might be viable and useful to collect coverage from system headers in some systems. This patch adds --system-headers-coverage option (disabled by default) to enable collecting coverage from system headers. Differential Revision: https://reviews.llvm.org/D143304
1 parent 8028263 commit 2155195

File tree

2 files changed

+19
-8
lines changed

2 files changed

+19
-8
lines changed

clang/lib/CodeGen/CoverageMappingGen.cpp

+16-6
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,11 @@ static llvm::cl::opt<bool> EmptyLineCommentCoverage(
3737
"disable it on test)"),
3838
llvm::cl::init(true), llvm::cl::Hidden);
3939

40+
static llvm::cl::opt<bool> SystemHeadersCoverage(
41+
"system-headers-coverage",
42+
llvm::cl::desc("Enable collecting coverage from system headers"),
43+
llvm::cl::init(false), llvm::cl::Hidden);
44+
4045
using namespace clang;
4146
using namespace CodeGen;
4247
using namespace llvm::coverage;
@@ -301,8 +306,9 @@ class CoverageMappingBuilder {
301306
if (!Visited.insert(File).second)
302307
continue;
303308

304-
// Do not map FileID's associated with system headers.
305-
if (SM.isInSystemHeader(SM.getSpellingLoc(Loc)))
309+
// Do not map FileID's associated with system headers unless collecting
310+
// coverage from system headers is explicitly enabled.
311+
if (!SystemHeadersCoverage && SM.isInSystemHeader(SM.getSpellingLoc(Loc)))
306312
continue;
307313

308314
unsigned Depth = 0;
@@ -416,8 +422,10 @@ class CoverageMappingBuilder {
416422
SourceLocation LocStart = Region.getBeginLoc();
417423
assert(SM.getFileID(LocStart).isValid() && "region in invalid file");
418424

419-
// Ignore regions from system headers.
420-
if (SM.isInSystemHeader(SM.getSpellingLoc(LocStart)))
425+
// Ignore regions from system headers unless collecting coverage from
426+
// system headers is explicitly enabled.
427+
if (!SystemHeadersCoverage &&
428+
SM.isInSystemHeader(SM.getSpellingLoc(LocStart)))
421429
continue;
422430

423431
auto CovFileID = getCoverageFileID(LocStart);
@@ -1000,8 +1008,10 @@ struct CounterCoverageMappingBuilder
10001008
void VisitDecl(const Decl *D) {
10011009
Stmt *Body = D->getBody();
10021010

1003-
// Do not propagate region counts into system headers.
1004-
if (Body && SM.isInSystemHeader(SM.getSpellingLoc(getStart(Body))))
1011+
// Do not propagate region counts into system headers unless collecting
1012+
// coverage from system headers is explicitly enabled.
1013+
if (!SystemHeadersCoverage && Body &&
1014+
SM.isInSystemHeader(SM.getSpellingLoc(getStart(Body))))
10051015
return;
10061016

10071017
// Do not visit the artificial children nodes of defaulted methods. The

clang/test/CoverageMapping/system_macro.cpp

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// RUN: %clang_cc1 -mllvm -emptyline-comment-coverage=false -std=c++11 -fprofile-instrument=clang -fcoverage-mapping -dump-coverage-mapping -emit-llvm-only -main-file-name system_macro.cpp -o - %s | FileCheck %s
1+
// RUN: %clang_cc1 -mllvm -emptyline-comment-coverage=false -mllvm -system-headers-coverage -std=c++11 -fprofile-instrument=clang -fcoverage-mapping -dump-coverage-mapping -emit-llvm-only -main-file-name system_macro.cpp -o - %s | FileCheck %s
22

33
#ifdef IS_SYSHEADER
44

@@ -13,8 +13,9 @@
1313

1414
// CHECK-LABEL: doSomething
1515
void doSomething(int x) { // CHECK: File 0, [[@LINE]]:25 -> {{[0-9:]+}} = #0
16-
Func(x);
16+
Func(x); // CHECK: Expansion,File 0, [[@LINE]]:3 -> [[@LINE]]:7
1717
return;
18+
// CHECK: Expansion,File 0, [[@LINE+1]]:3 -> [[@LINE+1]]:11
1819
SomeType *f; // CHECK: File 0, [[@LINE]]:11 -> {{[0-9:]+}} = 0
1920
}
2021

0 commit comments

Comments
 (0)