Skip to content

Commit 968ef43

Browse files
chapuniwhentojump
authored andcommitted
[MC/DC][Coverage] Workaround for ## conditions
A synthesized identifier with `##` is emitted to `<scratch space>`. `llvm-cov` cannot handle `<scratch space> since it doesn't have actual files. As a workaround, peel `<scratch space>` to the actual definition if the definition is present. This affects predefined built-in macros, like __LINE__. Fixes #87000
1 parent 92631a4 commit 968ef43

File tree

4 files changed

+56
-5
lines changed

4 files changed

+56
-5
lines changed

clang/lib/CodeGen/CoverageMappingGen.cpp

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -339,8 +339,18 @@ class CoverageMappingBuilder {
339339

340340
llvm::SmallSet<FileID, 8> Visited;
341341
SmallVector<std::pair<SourceLocation, unsigned>, 8> FileLocs;
342-
for (const auto &Region : SourceRegions) {
342+
for (auto &Region : SourceRegions) {
343343
SourceLocation Loc = Region.getBeginLoc();
344+
345+
// Replace Region with its definition if it is in <scratch space>.
346+
while (Loc.isMacroID() &&
347+
SM.isWrittenInScratchSpace(SM.getSpellingLoc(Loc))) {
348+
auto ExpansionRange = SM.getImmediateExpansionRange(Loc);
349+
Loc = ExpansionRange.getBegin();
350+
Region.setStartLoc(Loc);
351+
Region.setEndLoc(ExpansionRange.getEnd());
352+
}
353+
344354
FileID File = SM.getFileID(Loc);
345355
if (!Visited.insert(File).second)
346356
continue;

clang/test/CoverageMapping/builtinmacro.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
// CHECK: filename
66
const char *filename (const char *name) { // CHECK-NEXT: File 0, [[@LINE]]:41 -> [[@LINE+3]]:2 = #0
7-
static const char this_file[] = __FILE__;
7+
static const char this_file[] = __FILE__; // CHECK-NEXT: File 0, [[@LINE]]:35 -> [[@LINE]]:35 = #0
88
return this_file;
99
}
1010

clang/test/CoverageMapping/macros.c

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -80,12 +80,14 @@ void func7(void) { // CHECK-NEXT: File 0, [[@LINE]]:18 -> [[@LINE+6]]:2 = #0
8080
int kk,ll; // CHECK-NEXT: File 0, [[@LINE+1]]:7 -> [[@LINE+1]]:8 = #0
8181
if (k) // CHECK-NEXT: Branch,File 0, [[@LINE]]:7 -> [[@LINE]]:8 = #1
8282
m(k); // CHECK-NEXT: Gap,File 0, [[@LINE-1]]:9 -> [[@LINE]]:5 = #1
83-
else // CHECK-NEXT: Expansion,File 0, [[@LINE-1]]:5 -> [[@LINE-1]]:6 = #0
83+
else // CHECK-NEXT: Expansion,File 0, [[@LINE-1]]:5 -> [[@LINE-1]]:6 = #1
8484
l = m(l); // CHECK-NEXT: Gap,File 0, [[@LINE-2]]:7 -> [[@LINE]]:5 = (#0 - #1)
8585
} // CHECK-NEXT: File 0, [[@LINE-1]]:5 -> [[@LINE-1]]:10 = (#0 - #1)
8686
// CHECK-NEXT: Expansion,File 0, [[@LINE-2]]:9 -> [[@LINE-2]]:10 = (#0 - #1)
87-
// CHECK-NEXT: File 1, [[@LINE-9]]:14 -> [[@LINE-9]]:18 = #0
88-
// CHECK-NEXT: File 2, [[@LINE-10]]:14 -> [[@LINE-10]]:15 = (#0 - #1)
87+
// CHECK-NEXT: File 1, [[@LINE-9]]:14 -> [[@LINE-9]]:17 = #1
88+
// CHECK-NEXT: File 1, [[@LINE-10]]:14 -> [[@LINE-10]]:18 = #0
89+
// CHECK-NEXT: File 2, [[@LINE-11]]:14 -> [[@LINE-11]]:17 = (#0 - #1)
90+
// CHECK-NEXT: File 2, [[@LINE-12]]:14 -> [[@LINE-12]]:15 = (#0 - #1)
8991

9092
int main(int argc, const char *argv[]) {
9193
func();
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// RUN: %clang_cc1 -triple %itanium_abi_triple -std=c99 -fcoverage-mcdc -fprofile-instrument=clang -fcoverage-mapping -dump-coverage-mapping -emit-llvm-only %s | FileCheck %s
2+
3+
// CHECK: builtin_macro0:
4+
int builtin_macro0(int a) {
5+
// CHECK: Decision,File 0, [[@LINE+1]]:11 -> [[@LINE+2]]:15 = M:0, C:2
6+
return (__LINE__ // CHECK: Branch,File 0, [[@LINE]]:11 -> [[@LINE]]:11 = 0, 0 [1,2,0]
7+
&& a); // CHECK: Branch,File 0, [[@LINE]]:14 -> [[@LINE]]:15 = #2, (#1 - #2) [2,0,0]
8+
}
9+
10+
// CHECK: builtin_macro1:
11+
int builtin_macro1(int a) {
12+
// CHECK: Decision,File 0, [[@LINE+1]]:11 -> [[@LINE+2]]:22 = M:0, C:2
13+
return (a // CHECK: Branch,File 0, [[@LINE]]:11 -> [[@LINE]]:12 = (#0 - #1), #1 [1,0,2]
14+
|| __LINE__); // CHECK: Branch,File 0, [[@LINE]]:14 -> [[@LINE]]:14 = 0, 0 [2,0,0]
15+
}
16+
17+
#define PRE(x) pre_##x
18+
19+
// CHECK: pre0:
20+
int pre0(int pre_a, int b_post) {
21+
// CHECK: Decision,File 0, [[@LINE+2]]:11 -> [[@LINE+3]]:20 = M:0, C:2
22+
// CHECK: Expansion,File 0, [[@LINE+1]]:11 -> [[@LINE+1]]:14 = #0 (Expanded file = 1)
23+
return (PRE(a)
24+
&& b_post);
25+
// CHECK: Branch,File 0, [[@LINE-1]]:14 -> [[@LINE-1]]:20 = #2, (#1 - #2) [2,0,0]
26+
// CHECK: Branch,File 1, [[@LINE-9]]:16 -> [[@LINE-9]]:22 = #1, (#0 - #1) [1,2,0]
27+
}
28+
29+
#define POST(x) x##_post
30+
31+
// CHECK: post0:
32+
int post0(int pre_a, int b_post) {
33+
// CHECK: Decision,File 0, [[@LINE+2]]:11 -> [[@LINE+3]]:18 = M:0, C:2
34+
// CHECK: Branch,File 0, [[@LINE+1]]:11 -> [[@LINE+1]]:16 = (#0 - #1), #1 [1,0,2]
35+
return (pre_a
36+
|| POST(b));
37+
// CHECK: Expansion,File 0, [[@LINE-1]]:14 -> [[@LINE-1]]:18 = #1 (Expanded file = 1)
38+
// CHECK: Branch,File 1, [[@LINE-9]]:17 -> [[@LINE-9]]:20 = (#1 - #2), #2 [2,0,0]
39+
}

0 commit comments

Comments
 (0)