Skip to content

Commit c1b6cca

Browse files
authored
[clang][CoverageMapping] do not emit a gap region when either end doesn't have valid source locations (#89564)
Fixes #86998
1 parent a6c0282 commit c1b6cca

File tree

2 files changed

+44
-3
lines changed

2 files changed

+44
-3
lines changed

clang/lib/CodeGen/CoverageMappingGen.cpp

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1208,6 +1208,12 @@ struct CounterCoverageMappingBuilder
12081208
/// Find a valid gap range between \p AfterLoc and \p BeforeLoc.
12091209
std::optional<SourceRange> findGapAreaBetween(SourceLocation AfterLoc,
12101210
SourceLocation BeforeLoc) {
1211+
// Some statements (like AttributedStmt and ImplicitValueInitExpr) don't
1212+
// have valid source locations. Do not emit a gap region if this is the case
1213+
// in either AfterLoc end or BeforeLoc end.
1214+
if (AfterLoc.isInvalid() || BeforeLoc.isInvalid())
1215+
return std::nullopt;
1216+
12111217
// If AfterLoc is in function-like macro, use the right parenthesis
12121218
// location.
12131219
if (AfterLoc.isMacroID()) {
@@ -1368,9 +1374,8 @@ struct CounterCoverageMappingBuilder
13681374
for (const Stmt *Child : S->children())
13691375
if (Child) {
13701376
// If last statement contains terminate statements, add a gap area
1371-
// between the two statements. Skipping attributed statements, because
1372-
// they don't have valid start location.
1373-
if (LastStmt && HasTerminateStmt && !isa<AttributedStmt>(Child)) {
1377+
// between the two statements.
1378+
if (LastStmt && HasTerminateStmt) {
13741379
auto Gap = findGapAreaBetween(getEnd(LastStmt), getStart(Child));
13751380
if (Gap)
13761381
fillGapAreaWithCount(Gap->getBegin(), Gap->getEnd(),
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// RUN: %clang_cc1 -mllvm -emptyline-comment-coverage=false -fprofile-instrument=clang -fcoverage-mapping -dump-coverage-mapping -emit-llvm-only -main-file-name statement-expression.c %s
2+
3+
// No crash for the following examples, where GNU Statement Expression extension
4+
// could introduce region terminators (break, goto etc) before implicit
5+
// initializers in a struct or an array.
6+
// See https://github.com/llvm/llvm-project/pull/89564
7+
8+
struct Foo {
9+
int field1;
10+
int field2;
11+
};
12+
13+
void f1(void) {
14+
struct Foo foo = {
15+
.field1 = ({
16+
switch (0) {
17+
case 0:
18+
break; // A region terminator
19+
}
20+
0;
21+
}),
22+
// ImplicitValueInitExpr introduced here for .field2
23+
};
24+
}
25+
26+
void f2(void) {
27+
int arr[3] = {
28+
[0] = ({
29+
goto L0; // A region terminator
30+
L0:
31+
0;
32+
}),
33+
// ImplicitValueInitExpr introduced here for subscript [1]
34+
[2] = 0,
35+
};
36+
}

0 commit comments

Comments
 (0)