Skip to content

Commit 6a7700a

Browse files
committed
[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 llvmorg-19-init-11775-g702a2b627ff4
1 parent 1118c2e commit 6a7700a

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"
@@ -325,16 +326,26 @@ class CoverageMappingBuilder {
325326

326327
llvm::SmallSet<FileID, 8> Visited;
327328
SmallVector<std::pair<SourceLocation, unsigned>, 8> FileLocs;
328-
for (const auto &Region : SourceRegions) {
329+
for (auto &Region : SourceRegions) {
329330
SourceLocation Loc = Region.getBeginLoc();
331+
332+
// Replace Loc with FileLoc if it is expanded with system headers.
333+
if (!SystemHeadersCoverage && SM.isInSystemMacro(Loc)) {
334+
auto BeginLoc = SM.getSpellingLoc(Loc);
335+
auto EndLoc = SM.getSpellingLoc(Region.getEndLoc());
336+
if (SM.isWrittenInSameFile(BeginLoc, EndLoc)) {
337+
Loc = SM.getFileLoc(Loc);
338+
Region.setStartLoc(Loc);
339+
Region.setEndLoc(SM.getFileLoc(Region.getEndLoc()));
340+
}
341+
}
342+
330343
FileID File = SM.getFileID(Loc);
331344
if (!Visited.insert(File).second)
332345
continue;
333346

334-
// Do not map FileID's associated with system headers unless collecting
335-
// coverage from system headers is explicitly enabled.
336-
if (!SystemHeadersCoverage && SM.isInSystemHeader(SM.getSpellingLoc(Loc)))
337-
continue;
347+
assert(SystemHeadersCoverage ||
348+
!SM.isInSystemHeader(SM.getSpellingLoc(Loc)));
338349

339350
unsigned Depth = 0;
340351
for (SourceLocation Parent = getIncludeOrExpansionLoc(Loc);
@@ -816,6 +827,10 @@ struct CounterCoverageMappingBuilder
816827
/// A stack of currently live regions.
817828
llvm::SmallVector<SourceMappingRegion> RegionStack;
818829

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

@@ -1041,7 +1056,10 @@ struct CounterCoverageMappingBuilder
10411056
// region onto RegionStack but immediately pop it (which adds it to the
10421057
// function's SourceRegions) because it doesn't apply to any other source
10431058
// code other than the Condition.
1044-
if (CodeGenFunction::isInstrumentedCondition(C)) {
1059+
// With !SystemHeadersCoverage, binary logical ops in system headers may be
1060+
// treated as instrumentable conditions.
1061+
if (CodeGenFunction::isInstrumentedCondition(C) ||
1062+
LeafExprSet.count(CodeGenFunction::stripCond(C))) {
10451063
MCDCConditionID ID = MCDCBuilder.getCondID(C);
10461064
MCDCConditionID TrueID = IDPair.TrueID;
10471065
MCDCConditionID FalseID = IDPair.FalseID;
@@ -1977,7 +1995,20 @@ struct CounterCoverageMappingBuilder
19771995
subtractCounters(ParentCount, TrueCount));
19781996
}
19791997

1998+
/// Check if E belongs to system headers.
1999+
bool isExprInSystemHeader(const BinaryOperator *E) const {
2000+
return (!SystemHeadersCoverage &&
2001+
SM.isInSystemHeader(SM.getSpellingLoc(E->getOperatorLoc())) &&
2002+
SM.isInSystemHeader(SM.getSpellingLoc(E->getBeginLoc())) &&
2003+
SM.isInSystemHeader(SM.getSpellingLoc(E->getEndLoc())));
2004+
}
2005+
19802006
void VisitBinLAnd(const BinaryOperator *E) {
2007+
if (isExprInSystemHeader(E)) {
2008+
LeafExprSet.insert(E);
2009+
return;
2010+
}
2011+
19812012
bool IsRootNode = MCDCBuilder.isIdle();
19822013

19832014
// Keep track of Binary Operator and assign MCDC condition IDs.
@@ -2031,6 +2062,11 @@ struct CounterCoverageMappingBuilder
20312062
}
20322063

20332064
void VisitBinLOr(const BinaryOperator *E) {
2065+
if (isExprInSystemHeader(E)) {
2066+
LeafExprSet.insert(E);
2067+
return;
2068+
}
2069+
20342070
bool IsRootNode = MCDCBuilder.isIdle();
20352071

20362072
// 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)