Skip to content

Commit 13035b2

Browse files
committed
[MC/DC][Coverage] Add assertions into emitSourceRegions()
`emitSourceRegions()` has bugs to emit malformed MC/DC coverage mappings. They were detected in `llvm-cov` as the crash. Detect inconsistencies earlier in `clang` with assertions. * mcdc-system-headers.cpp covers llvm#78920. * mcdc-scratch-space.c covers llvm#87000.
1 parent 36e2577 commit 13035b2

File tree

3 files changed

+98
-5
lines changed

3 files changed

+98
-5
lines changed

clang/lib/CodeGen/CoverageMappingGen.cpp

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -190,11 +190,21 @@ class SourceMappingRegion {
190190

191191
bool isBranch() const { return FalseCount.has_value(); }
192192

193+
bool isMCDCBranch() const {
194+
const auto *BranchParams = std::get_if<mcdc::BranchParameters>(&MCDCParams);
195+
assert(BranchParams == nullptr || BranchParams->ID >= 0);
196+
return (BranchParams != nullptr);
197+
}
198+
199+
const auto &getMCDCBranchParams() const {
200+
return mcdc::getParams<const mcdc::BranchParameters>(MCDCParams);
201+
}
202+
193203
bool isMCDCDecision() const {
194204
const auto *DecisionParams =
195205
std::get_if<mcdc::DecisionParameters>(&MCDCParams);
196-
assert(!DecisionParams || DecisionParams->NumConditions > 0);
197-
return DecisionParams;
206+
assert(DecisionParams == nullptr || DecisionParams->NumConditions > 0);
207+
return (DecisionParams != nullptr);
198208
}
199209

200210
const auto &getMCDCDecisionParams() const {
@@ -464,13 +474,19 @@ class CoverageMappingBuilder {
464474
// Ignore regions from system headers unless collecting coverage from
465475
// system headers is explicitly enabled.
466476
if (!SystemHeadersCoverage &&
467-
SM.isInSystemHeader(SM.getSpellingLoc(LocStart)))
477+
SM.isInSystemHeader(SM.getSpellingLoc(LocStart))) {
478+
assert(!Region.isMCDCBranch() && !Region.isMCDCDecision() &&
479+
"Don't suppress the condition in system headers");
468480
continue;
481+
}
469482

470483
auto CovFileID = getCoverageFileID(LocStart);
471484
// Ignore regions that don't have a file, such as builtin macros.
472-
if (!CovFileID)
485+
if (!CovFileID) {
486+
assert(!Region.isMCDCBranch() && !Region.isMCDCDecision() &&
487+
"Don't suppress the condition in non-file regions");
473488
continue;
489+
}
474490

475491
SourceLocation LocEnd = Region.getEndLoc();
476492
assert(SM.isWrittenInSameFile(LocStart, LocEnd) &&
@@ -480,8 +496,11 @@ class CoverageMappingBuilder {
480496
// This not only suppresses redundant regions, but sometimes prevents
481497
// creating regions with wrong counters if, for example, a statement's
482498
// body ends at the end of a nested macro.
483-
if (Filter.count(std::make_pair(LocStart, LocEnd)))
499+
if (Filter.count(std::make_pair(LocStart, LocEnd))) {
500+
assert(!Region.isMCDCBranch() && !Region.isMCDCDecision() &&
501+
"Don't suppress the condition");
484502
continue;
503+
}
485504

486505
// Find the spelling locations for the mapping region.
487506
SpellingRegion SR{SM, LocStart, LocEnd};
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// RUN: %clang_cc1 -triple %itanium_abi_triple -std=c99 -fcoverage-mcdc -fprofile-instrument=clang -fcoverage-mapping -dump-coverage-mapping -emit-llvm-only %s
2+
// XFAIL: *
3+
// REQUIRES: asserts
4+
5+
int builtin_macro0(int a) {
6+
return (__LINE__
7+
&& a);
8+
}
9+
10+
int builtin_macro1(int a) {
11+
return (a
12+
|| __LINE__);
13+
}
14+
15+
#define PRE(x) pre_##x
16+
17+
int pre0(int pre_a, int b_post) {
18+
return (PRE(a)
19+
&& b_post);
20+
}
21+
22+
#define POST(x) x##_post
23+
24+
int post0(int pre_a, int b_post) {
25+
return (pre_a
26+
|| POST(b));
27+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
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
2+
3+
// Will crash w/o -system-headers-coverage
4+
// RUN: not --crash %clang_cc1 -std=c++11 -fprofile-instrument=clang -fcoverage-mapping -dump-coverage-mapping -fcoverage-mcdc -emit-llvm-only -o - %s
5+
// REQUIRES: asserts
6+
7+
#ifdef IS_SYSHEADER
8+
9+
#pragma clang system_header
10+
#define CONST 42
11+
#define EXPR1(x) (x)
12+
#define EXPR2(x) ((x) * (x))
13+
14+
#else
15+
16+
#define IS_SYSHEADER
17+
#include __FILE__
18+
19+
// CHECK: _Z5func0i:
20+
int func0(int a) {
21+
// CHECK: Decision,File 0, [[@LINE+2]]:11 -> [[@LINE+2]]:21 = M:0, C:2
22+
// CHECK: Expansion,File 0, [[@LINE+1]]:11 -> [[@LINE+1]]:16 = #0 (Expanded file = 1)
23+
return (CONST && a);
24+
// CHECK: Branch,File 0, [[@LINE-1]]:20 -> [[@LINE-1]]:21 = #2, (#1 - #2) [2,0,0]
25+
// CHECK: Branch,File 1, [[@LINE-15]]:15 -> [[@LINE-15]]:17 = 0, 0 [1,2,0]
26+
}
27+
28+
// CHECK: _Z5func1ii:
29+
int func1(int a, int b) {
30+
// CHECK: Decision,File 0, [[@LINE+2]]:11 -> [[@LINE+2]]:21 = M:0, C:2
31+
// CHECK: Branch,File 0, [[@LINE+1]]:11 -> [[@LINE+1]]:12 = (#0 - #1), #1 [1,0,2]
32+
return (a || EXPR1(b));
33+
// CHECK: Expansion,File 0, [[@LINE-1]]:16 -> [[@LINE-1]]:21 = #1 (Expanded file = 1)
34+
// CHECK: Branch,File 1, [[@LINE-23]]:18 -> [[@LINE-23]]:21 = (#1 - #2), #2 [2,0,0]
35+
}
36+
37+
// CHECK: _Z5func2ii:
38+
int func2(int a, int b) {
39+
// Decision,File 0, [[@LINE+3]]:11 -> [[@LINE+3]]:28 = M:0, C:2
40+
// Expansion,File 0, [[@LINE+2]]:11 -> [[@LINE+2]]:16 = #0 (Expanded file = 1)
41+
// Expansion,File 0, [[@LINE+1]]:23 -> [[@LINE+1]]:28 = #1 (Expanded file = 2)
42+
return (EXPR2(a) && EXPR1(a));
43+
// CHECK: Branch,File 1, [[@LINE-31]]:18 -> [[@LINE-31]]:29 = #1, (#0 - #1) [1,2,0]
44+
// CHECK: Branch,File 2, [[@LINE-33]]:18 -> [[@LINE-33]]:21 = #2, (#1 - #2) [2,0,0]
45+
}
46+
47+
#endif

0 commit comments

Comments
 (0)