Skip to content

Commit 348b78a

Browse files
committed
[Coverage] Handle non-local exits out of DoStmt's properly
First, assign counter expressions to the DoStmt and its body. Next, fix the count associated to the DoStmt when handling a non-local exit. This fixes SR-1850.
1 parent 3c9c520 commit 348b78a

File tree

2 files changed

+31
-1
lines changed

2 files changed

+31
-1
lines changed

lib/SILGen/SILGenProfiling.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -349,7 +349,8 @@ struct CoverageMapping : public ASTWalker {
349349
CounterExpr *JumpsToLabel = nullptr;
350350
Stmt *ParentStmt = Parent.getAsStmt();
351351
if (ParentStmt) {
352-
if (isa<DoCatchStmt>(ParentStmt) || isa<CatchStmt>(ParentStmt))
352+
if (isa<DoStmt>(ParentStmt) || isa<DoCatchStmt>(ParentStmt) ||
353+
isa<CatchStmt>(ParentStmt))
353354
return;
354355
if (auto *LS = dyn_cast<LabeledStmt>(ParentStmt))
355356
JumpsToLabel = &getCounter(LS);
@@ -530,6 +531,10 @@ struct CoverageMapping : public ASTWalker {
530531
} else if (isa<CaseStmt>(S)) {
531532
pushRegion(S);
532533

534+
} else if (auto *DS = dyn_cast<DoStmt>(S)) {
535+
assignCounter(DS->getBody(), CounterExpr::Ref(getCurrentCounter()));
536+
assignCounter(DS);
537+
533538
} else if (auto *DCS = dyn_cast<DoCatchStmt>(S)) {
534539
assignCounter(DCS->getBody(), CounterExpr::Ref(getCurrentCounter()));
535540
assignCounter(DCS);

test/SILGen/coverage_label.swift

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// RUN: %target-swift-frontend -Xllvm -sil-full-demangle -suppress-warnings -profile-generate -profile-coverage-mapping -emit-sorted-sil -emit-sil -module-name coverage_label %s | FileCheck %s
2+
3+
// CHECK-LABEL: sil_coverage_map {{.*}}// coverage_label.foo
4+
func foo() { // CHECK-DAG: [[@LINE]]:12 -> [[@LINE+19]]:2 : 0
5+
var x : Int32 = 0
6+
7+
label1: do { // CHECK-DAG: [[@LINE]]:14 -> [[@LINE+4]]:4 : 0
8+
x += 1
9+
break label1
10+
x += 2 // CHECK-DAG: [[@LINE]]:5 -> [[@LINE+1]]:4 : zero
11+
}
12+
13+
label2: do { // CHECK-DAG: [[@LINE]]:14 -> [[@LINE+7]]:4 : 0
14+
x += 3 // CHECK-DAG: [[@LINE+1]]:11 -> [[@LINE+1]]:17 : 0
15+
while (true) { // CHECK-DAG: [[@LINE]]:18 -> [[@LINE+3]]:6 : 1
16+
x += 4
17+
break label2 // Note: This exit affects the condition counter expr @ L15.
18+
} // CHECK-DAG: [[@LINE]]:6 -> [[@LINE+2]]:4 : (0 - 1)
19+
x += 5
20+
}
21+
22+
x += 6
23+
}
24+
25+
foo()

0 commit comments

Comments
 (0)