Skip to content

Commit d3ee2d0

Browse files
committed
[Coverage] Rework !SystemHeadersCoverage
- Introduce `LeafExprSet`, - Suppress traversing LAnd and LOr expr under system headers. - Handle LAnd and LOr as instrumented leaves to override `!isInstrumentedCondition(C)`. - Replace Loc with FileLoc if it is expanded with system headers. Fixes llvm#78920
1 parent 36e2577 commit d3ee2d0

File tree

2 files changed

+92
-6
lines changed

2 files changed

+92
-6
lines changed

clang/lib/CodeGen/CoverageMappingGen.cpp

Lines changed: 42 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include "clang/Basic/FileManager.h"
1818
#include "clang/Frontend/FrontendDiagnostic.h"
1919
#include "clang/Lex/Lexer.h"
20+
#include "llvm/ADT/DenseSet.h"
2021
#include "llvm/ADT/SmallSet.h"
2122
#include "llvm/ADT/StringExtras.h"
2223
#include "llvm/ProfileData/Coverage/CoverageMapping.h"
@@ -339,16 +340,26 @@ class CoverageMappingBuilder {
339340

340341
llvm::SmallSet<FileID, 8> Visited;
341342
SmallVector<std::pair<SourceLocation, unsigned>, 8> FileLocs;
342-
for (const auto &Region : SourceRegions) {
343+
for (auto &Region : SourceRegions) {
343344
SourceLocation Loc = Region.getBeginLoc();
345+
346+
// Replace Loc with FileLoc if it is expanded with system headers.
347+
if (!SystemHeadersCoverage && SM.isInSystemMacro(Loc)) {
348+
auto BeginLoc = SM.getSpellingLoc(Loc);
349+
auto EndLoc = SM.getSpellingLoc(Region.getEndLoc());
350+
if (SM.isWrittenInSameFile(BeginLoc, EndLoc)) {
351+
Loc = SM.getFileLoc(Loc);
352+
Region.setStartLoc(Loc);
353+
Region.setEndLoc(SM.getFileLoc(Region.getEndLoc()));
354+
}
355+
}
356+
344357
FileID File = SM.getFileID(Loc);
345358
if (!Visited.insert(File).second)
346359
continue;
347360

348-
// Do not map FileID's associated with system headers unless collecting
349-
// coverage from system headers is explicitly enabled.
350-
if (!SystemHeadersCoverage && SM.isInSystemHeader(SM.getSpellingLoc(Loc)))
351-
continue;
361+
assert(SystemHeadersCoverage ||
362+
!SM.isInSystemHeader(SM.getSpellingLoc(Loc)));
352363

353364
unsigned Depth = 0;
354365
for (SourceLocation Parent = getIncludeOrExpansionLoc(Loc);
@@ -821,6 +832,10 @@ struct CounterCoverageMappingBuilder
821832
/// A stack of currently live regions.
822833
llvm::SmallVector<SourceMappingRegion> RegionStack;
823834

835+
/// Set if the Expr should be handled as a leaf even if it is kind of binary
836+
/// logical ops (&&, ||).
837+
llvm::DenseSet<const Stmt *> LeafExprSet;
838+
824839
/// An object to manage MCDC regions.
825840
MCDCCoverageBuilder MCDCBuilder;
826841

@@ -1043,7 +1058,10 @@ struct CounterCoverageMappingBuilder
10431058
// region onto RegionStack but immediately pop it (which adds it to the
10441059
// function's SourceRegions) because it doesn't apply to any other source
10451060
// code other than the Condition.
1046-
if (CodeGenFunction::isInstrumentedCondition(C)) {
1061+
// With !SystemHeadersCoverage, binary logical ops in system headers may be
1062+
// treated as instrumentable conditions.
1063+
if (CodeGenFunction::isInstrumentedCondition(C) ||
1064+
LeafExprSet.count(CodeGenFunction::stripCond(C))) {
10471065
mcdc::Parameters BranchParams;
10481066
mcdc::ConditionID ID = MCDCBuilder.getCondID(C);
10491067
if (ID >= 0)
@@ -2064,7 +2082,20 @@ struct CounterCoverageMappingBuilder
20642082
createDecisionRegion(E, DecisionParams);
20652083
}
20662084

2085+
/// Check if E belongs to system headers.
2086+
bool isExprInSystemHeader(const BinaryOperator *E) const {
2087+
return (!SystemHeadersCoverage &&
2088+
SM.isInSystemHeader(SM.getSpellingLoc(E->getOperatorLoc())) &&
2089+
SM.isInSystemHeader(SM.getSpellingLoc(E->getBeginLoc())) &&
2090+
SM.isInSystemHeader(SM.getSpellingLoc(E->getEndLoc())));
2091+
}
2092+
20672093
void VisitBinLAnd(const BinaryOperator *E) {
2094+
if (isExprInSystemHeader(E)) {
2095+
LeafExprSet.insert(E);
2096+
return;
2097+
}
2098+
20682099
bool IsRootNode = MCDCBuilder.isIdle();
20692100

20702101
// Keep track of Binary Operator and assign MCDC condition IDs.
@@ -2119,6 +2150,11 @@ struct CounterCoverageMappingBuilder
21192150
}
21202151

21212152
void VisitBinLOr(const BinaryOperator *E) {
2153+
if (isExprInSystemHeader(E)) {
2154+
LeafExprSet.insert(E);
2155+
return;
2156+
}
2157+
21222158
bool IsRootNode = MCDCBuilder.isIdle();
21232159

21242160
// Keep track of Binary Operator and assign MCDC condition IDs.
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
// RUN: %clang_cc1 -std=c++11 -fprofile-instrument=clang -fcoverage-mapping -dump-coverage-mapping -fcoverage-mcdc -mllvm -system-headers-coverage -emit-llvm-only -o - %s | FileCheck %s --check-prefixes=CHECK,W_SYS
2+
// RUN: %clang_cc1 -std=c++11 -fprofile-instrument=clang -fcoverage-mapping -dump-coverage-mapping -fcoverage-mcdc -emit-llvm-only -o - %s | FileCheck %s --check-prefixes=CHECK,X_SYS
3+
4+
#ifdef IS_SYSHEADER
5+
6+
#pragma clang system_header
7+
#define CONST 42
8+
#define EXPR1(x) (x)
9+
#define EXPR2(x) ((x) && (x))
10+
11+
#else
12+
13+
#define IS_SYSHEADER
14+
#include __FILE__
15+
16+
// CHECK: _Z5func0i:
17+
int func0(int a) {
18+
// CHECK: Decision,File 0, [[@LINE+3]]:11 -> [[@LINE+3]]:21 = M:0, C:2
19+
// W_SYS: Expansion,File 0, [[@LINE+2]]:11 -> [[@LINE+2]]:16 = #0 (Expanded file = 1)
20+
// X_SYS: Branch,File 0, [[@LINE+1]]:11 -> [[@LINE+1]]:11 = 0, 0 [1,2,0]
21+
return (CONST && a);
22+
// W_SYS: Branch,File 1, [[@LINE-15]]:15 -> [[@LINE-15]]:17 = 0, 0 [1,2,0]
23+
// X_SYS: Branch,File 0, [[@LINE-2]]:20 -> [[@LINE-2]]:21 = #2, (#1 - #2) [2,0,0]
24+
}
25+
26+
// CHECK: _Z5func1ii:
27+
int func1(int a, int b) {
28+
// CHECK: Decision,File 0, [[@LINE+2]]:11 -> [[@LINE+2]]:21 = M:0, C:2
29+
// CHECK: Branch,File 0, [[@LINE+1]]:11 -> [[@LINE+1]]:12 = (#0 - #1), #1 [1,0,2]
30+
return (a || EXPR1(b));
31+
// W_SYS: Expansion,File 0, [[@LINE-1]]:16 -> [[@LINE-1]]:21 = #1 (Expanded file = 1)
32+
// W_SYS: Branch,File 1, [[@LINE-24]]:18 -> [[@LINE-24]]:21 = (#1 - #2), #2 [2,0,0]
33+
// X_SYS: Branch,File 0, [[@LINE-3]]:16 -> [[@LINE-3]]:16 = (#1 - #2), #2 [2,0,0]
34+
}
35+
36+
// CHECK: _Z5func2ii:
37+
int func2(int a, int b) {
38+
// W_SYS: Decision,File 0, [[@LINE+5]]:11 -> [[@LINE+5]]:28 = M:0, C:3
39+
// X_SYS: Decision,File 0, [[@LINE+4]]:11 -> [[@LINE+4]]:28 = M:0, C:2
40+
// W_SYS: Expansion,File 0, [[@LINE+3]]:11 -> [[@LINE+3]]:16 = #0 (Expanded file = 1)
41+
// W_SYS: Expansion,File 0, [[@LINE+2]]:23 -> [[@LINE+2]]:28 = #1 (Expanded file = 2)
42+
// X_SYS: Branch,File 0, [[@LINE+1]]:11 -> [[@LINE+1]]:11 = #1, (#0 - #1) [1,2,0]
43+
return (EXPR2(a) && EXPR1(a));
44+
// W_SYS: Branch,File 1, [[@LINE-35]]:19 -> [[@LINE-35]]:22 = #3, (#0 - #3) [1,3,0]
45+
// W_SYS: Branch,File 1, [[@LINE-36]]:26 -> [[@LINE-36]]:29 = #4, (#3 - #4) [3,2,0]
46+
// W_SYS: Branch,File 2, [[@LINE-38]]:18 -> [[@LINE-38]]:21 = #2, (#1 - #2) [2,0,0]
47+
// X_SYS: Branch,File 0, [[@LINE-4]]:23 -> [[@LINE-4]]:23 = #2, (#1 - #2) [2,0,0]
48+
}
49+
50+
#endif

0 commit comments

Comments
 (0)