Skip to content

Commit 2010fb3

Browse files
committed
[Coverage][Expansion] handle nested macros in scratch space
1 parent 968ef43 commit 2010fb3

File tree

2 files changed

+43
-1
lines changed

2 files changed

+43
-1
lines changed

clang/lib/CodeGen/CoverageMappingGen.cpp

+17-1
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,22 @@ class CoverageMappingBuilder {
298298
: SM.getIncludeLoc(SM.getFileID(Loc));
299299
}
300300

301+
/// Find out where the current file is included or macro is expanded. If the
302+
/// found expansion is a <scratch space>, keep looking.
303+
SourceLocation getIncludeOrNonScratchExpansionLoc(SourceLocation Loc) {
304+
if (Loc.isMacroID()) {
305+
Loc = SM.getImmediateExpansionRange(Loc).getBegin();
306+
while (Loc.isMacroID() &&
307+
SM.isWrittenInScratchSpace(SM.getSpellingLoc(Loc))) {
308+
auto ExpansionRange = SM.getImmediateExpansionRange(Loc);
309+
Loc = ExpansionRange.getBegin();
310+
}
311+
} else {
312+
Loc = SM.getIncludeLoc(SM.getFileID(Loc));
313+
}
314+
return Loc;
315+
}
316+
301317
/// Return true if \c Loc is a location in a built-in macro.
302318
bool isInBuiltin(SourceLocation Loc) {
303319
return SM.getBufferName(SM.getSpellingLoc(Loc)) == "<built-in>";
@@ -527,7 +543,7 @@ class CoverageMappingBuilder {
527543
SourceRegionFilter Filter;
528544
for (const auto &FM : FileIDMapping) {
529545
SourceLocation ExpandedLoc = FM.second.second;
530-
SourceLocation ParentLoc = getIncludeOrExpansionLoc(ExpandedLoc);
546+
SourceLocation ParentLoc = getIncludeOrNonScratchExpansionLoc(ExpandedLoc);
531547
if (ParentLoc.isInvalid())
532548
continue;
533549

clang/test/CoverageMapping/mcdc-scratch-space.c

+26
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,19 @@ int pre0(int pre_a, int b_post) {
2626
// CHECK: Branch,File 1, [[@LINE-9]]:16 -> [[@LINE-9]]:22 = #1, (#0 - #1) [1,2,0]
2727
}
2828

29+
#define pre_foo pre_a
30+
31+
// CHECK: pre1:
32+
int pre1(int pre_a, int b_post) {
33+
// CHECK: Decision,File 0, [[@LINE+3]]:11 -> [[@LINE+4]]:20 = M:0, C:2
34+
// CHECK: Expansion,File 0, [[@LINE+2]]:11 -> [[@LINE+2]]:14 = #0 (Expanded file = 1)
35+
// CHECK: Branch,File 0, [[@LINE+2]]:14 -> [[@LINE+2]]:20 = #2, (#1 - #2) [2,0,0]
36+
return (PRE(foo)
37+
&& b_post);
38+
// CHECK: Expansion,File 1, 17:16 -> 17:20 = #0 (Expanded file = 2)
39+
// CHECK: Branch,File 2, 29:17 -> 29:22 = #1, (#0 - #1) [1,2,0]
40+
}
41+
2942
#define POST(x) x##_post
3043

3144
// CHECK: post0:
@@ -37,3 +50,16 @@ int post0(int pre_a, int b_post) {
3750
// CHECK: Expansion,File 0, [[@LINE-1]]:14 -> [[@LINE-1]]:18 = #1 (Expanded file = 1)
3851
// CHECK: Branch,File 1, [[@LINE-9]]:17 -> [[@LINE-9]]:20 = (#1 - #2), #2 [2,0,0]
3952
}
53+
54+
#define bar_post b_post
55+
56+
// CHECK: post1:
57+
int post1(int pre_a, int b_post) {
58+
// CHECK: Decision,File 0, [[@LINE+3]]:11 -> [[@LINE+4]]:18 = M:0, C:2
59+
// CHECK: Branch,File 0, [[@LINE+2]]:11 -> [[@LINE+2]]:16 = (#0 - #1), #1 [1,0,2]
60+
// CHECK: Expansion,File 0, [[@LINE+2]]:14 -> [[@LINE+2]]:18 = 0 (Expanded file = 1)
61+
return (pre_a
62+
|| POST(bar));
63+
// CHECK: Expansion,File 1, 42:17 -> 42:18 = #1 (Expanded file = 2)
64+
// CHECK: Branch,File 2, 54:18 -> 54:24 = (#1 - #2), #2 [2,0,0]
65+
}

0 commit comments

Comments
 (0)