Skip to content

Commit 702a2b6

Browse files
authored
[Coverage] Rework !SystemHeadersCoverage (#91446)
- 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 #78920
1 parent ce1a0d8 commit 702a2b6

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"
@@ -336,16 +337,26 @@ class CoverageMappingBuilder {
336337

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

345-
// Do not map FileID's associated with system headers unless collecting
346-
// coverage from system headers is explicitly enabled.
347-
if (!SystemHeadersCoverage && SM.isInSystemHeader(SM.getSpellingLoc(Loc)))
348-
continue;
358+
assert(SystemHeadersCoverage ||
359+
!SM.isInSystemHeader(SM.getSpellingLoc(Loc)));
349360

350361
unsigned Depth = 0;
351362
for (SourceLocation Parent = getIncludeOrExpansionLoc(Loc);
@@ -818,6 +829,10 @@ struct CounterCoverageMappingBuilder
818829
/// A stack of currently live regions.
819830
llvm::SmallVector<SourceMappingRegion> RegionStack;
820831

832+
/// Set if the Expr should be handled as a leaf even if it is kind of binary
833+
/// logical ops (&&, ||).
834+
llvm::DenseSet<const Stmt *> LeafExprSet;
835+
821836
/// An object to manage MCDC regions.
822837
MCDCCoverageBuilder MCDCBuilder;
823838

@@ -1040,7 +1055,10 @@ struct CounterCoverageMappingBuilder
10401055
// region onto RegionStack but immediately pop it (which adds it to the
10411056
// function's SourceRegions) because it doesn't apply to any other source
10421057
// code other than the Condition.
1043-
if (CodeGenFunction::isInstrumentedCondition(C)) {
1058+
// With !SystemHeadersCoverage, binary logical ops in system headers may be
1059+
// treated as instrumentable conditions.
1060+
if (CodeGenFunction::isInstrumentedCondition(C) ||
1061+
LeafExprSet.count(CodeGenFunction::stripCond(C))) {
10441062
mcdc::Parameters BranchParams;
10451063
mcdc::ConditionID ID = MCDCBuilder.getCondID(C);
10461064
if (ID >= 0)
@@ -2070,7 +2088,20 @@ struct CounterCoverageMappingBuilder
20702088
createDecisionRegion(E, DecisionParams);
20712089
}
20722090

2091+
/// Check if E belongs to system headers.
2092+
bool isExprInSystemHeader(const BinaryOperator *E) const {
2093+
return (!SystemHeadersCoverage &&
2094+
SM.isInSystemHeader(SM.getSpellingLoc(E->getOperatorLoc())) &&
2095+
SM.isInSystemHeader(SM.getSpellingLoc(E->getBeginLoc())) &&
2096+
SM.isInSystemHeader(SM.getSpellingLoc(E->getEndLoc())));
2097+
}
2098+
20732099
void VisitBinLAnd(const BinaryOperator *E) {
2100+
if (isExprInSystemHeader(E)) {
2101+
LeafExprSet.insert(E);
2102+
return;
2103+
}
2104+
20742105
bool IsRootNode = MCDCBuilder.isIdle();
20752106

20762107
// Keep track of Binary Operator and assign MCDC condition IDs.
@@ -2125,6 +2156,11 @@ struct CounterCoverageMappingBuilder
21252156
}
21262157

21272158
void VisitBinLOr(const BinaryOperator *E) {
2159+
if (isExprInSystemHeader(E)) {
2160+
LeafExprSet.insert(E);
2161+
return;
2162+
}
2163+
21282164
bool IsRootNode = MCDCBuilder.isIdle();
21292165

21302166
// 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+
// CHECK: Branch,File 0, [[@LINE-1]]:20 -> [[@LINE-1]]:21 = #2, (#1 - #2) [2,0,0]
23+
// W_SYS: Branch,File 1, [[@LINE-16]]:15 -> [[@LINE-16]]:17 = 0, 0 [1,2,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)