Index: cfe/trunk/lib/CodeGen/CoverageMappingGen.cpp =================================================================== --- cfe/trunk/lib/CodeGen/CoverageMappingGen.cpp +++ cfe/trunk/lib/CodeGen/CoverageMappingGen.cpp @@ -834,22 +834,6 @@ handleFileExit(getEnd(S)); } - /// Determine whether the final deferred region emitted in \p Body should be - /// discarded. - static bool discardFinalDeferredRegionInDecl(Stmt *Body) { - if (auto *CS = dyn_cast(Body)) { - Stmt *LastStmt = CS->body_back(); - if (auto *IfElse = dyn_cast(LastStmt)) { - if (auto *Else = dyn_cast_or_null(IfElse->getElse())) - LastStmt = Else->body_back(); - else - LastStmt = IfElse->getElse(); - } - return dyn_cast_or_null(LastStmt); - } - return false; - } - void VisitDecl(const Decl *D) { assert(!DeferredRegion && "Deferred region never completed"); @@ -859,17 +843,13 @@ if (Body && SM.isInSystemHeader(SM.getSpellingLoc(getStart(Body)))) return; - Counter ExitCount = propagateCounts(getRegionCounter(Body), Body); + propagateCounts(getRegionCounter(Body), Body); assert(RegionStack.empty() && "Regions entered but never exited"); - if (DeferredRegion) { - // Complete (or discard) any deferred regions introduced by the last - // statement. - if (discardFinalDeferredRegionInDecl(Body)) - DeferredRegion = None; - else - popRegions(completeDeferred(ExitCount, getEnd(Body))); - } + // Discard the last uncompleted deferred region in a decl, if one exists. + // This prevents lines at the end of a function containing only whitespace + // or closing braces from being marked as uncovered. + DeferredRegion = None; } void VisitReturnStmt(const ReturnStmt *S) { Index: cfe/trunk/test/CoverageMapping/deferred-region.cpp =================================================================== --- cfe/trunk/test/CoverageMapping/deferred-region.cpp +++ cfe/trunk/test/CoverageMapping/deferred-region.cpp @@ -7,11 +7,12 @@ void foo(int x) { if (x == 0) { return; - } // CHECK: Gap,File 0, [[@LINE]]:4 -> [[@LINE+2]]:2 = (#0 - #1) - + } // CHECK-NOT: Gap,File 0, [[@LINE]]:4 + //< Don't complete the last deferred region in a decl, even though it may + //< leave some whitespace marked with the same counter as the final return. } -// CHECK-NEXT: _Z4foooi: +// CHECK-LABEL: _Z4foooi: void fooo(int x) { if (x == 0) { return; @@ -19,7 +20,7 @@ if (x == 1) { return; - } // CHECK: Gap,File 0, [[@LINE]]:4 -> [[@LINE+2]]:2 = ((#0 - #1) - #2) + } // CHECK-NOT: Gap,File 0, [[@LINE]]:4 } @@ -108,7 +109,7 @@ } if (false) - return; // CHECK: Gap,File 0, [[@LINE]]:11 -> [[@LINE+1]]:2 + return; // CHECK-NOT: Gap,File 0, [[@LINE]]:11 } // CHECK-LABEL: _Z8for_loopv: @@ -167,7 +168,18 @@ return; // CHECK: [[@LINE]]:3 -> [[@LINE+4]]:2 = (#0 - #1) out: - return; // CHECK: Gap,File 0, [[@LINE]]:8 -> [[@LINE+1]]:2 = 0 + return; // CHECK-NOT: Gap,File 0, [[@LINE]]:8 +} + +// CHECK-LABEL: _Z8switchesv: +void switches() { + int x; + switch (x) { + case 0: + return; + default: + return; // CHECK-NOT: Gap,File 0, [[@LINE]] + } } #include "deferred-region-helper.h" Index: cfe/trunk/test/CoverageMapping/label.cpp =================================================================== --- cfe/trunk/test/CoverageMapping/label.cpp +++ cfe/trunk/test/CoverageMapping/label.cpp @@ -16,11 +16,10 @@ goto x; // CHECK: File 0, [[@LINE]]:7 -> [[@LINE]]:13 = (#1 - #2) int k = 3; // CHECK-NEXT: File 0, [[@LINE-1]]:13 -> [[@LINE]]:5 = #3 } // CHECK-NEXT: File 0, [[@LINE-1]]:5 -> [[@LINE]]:4 = #3 - static int j = 0; // CHECK-NEXT: File 0, [[@LINE]]:3 -> [[@LINE+5]]:2 = ((#0 + #3) - #1) + static int j = 0; // CHECK-NEXT: File 0, [[@LINE]]:3 -> [[@LINE+4]]:2 = ((#0 + #3) - #1) ++j; if(j == 1) // CHECK-NEXT: File 0, [[@LINE]]:6 -> [[@LINE]]:12 = ((#0 + #3) - #1) goto x; // CHECK: File 0, [[@LINE]]:5 -> [[@LINE]]:11 = #4 - // CHECK-NEXT: File 0, [[@LINE-1]]:11 -> [[@LINE+1]]:2 = (((#0 + #3) - #1) - #4) } // CHECK-NEXT: test1 @@ -28,7 +27,7 @@ if(x == 0) // CHECK-NEXT: File 0, [[@LINE]]:6 -> [[@LINE]]:12 = #0 goto a; // CHECK: File 0, [[@LINE]]:5 -> [[@LINE]]:11 = #1 // CHECK-NEXT: File 0, [[@LINE-1]]:11 -> [[@LINE+1]]:3 = (#0 - #1) - goto b; // CHECK: Gap,File 0, [[@LINE]]:3 -> [[@LINE+5]]:2 = #3 + goto b; // CHECK: File 0, [[@LINE]]:3 -> [[@LINE+5]]:2 = (#0 - #1) // CHECK-NEXT: Gap,File 0, [[@LINE-1]]:9 -> [[@LINE+1]]:1 = #2 a: // CHECK-NEXT: File 0, [[@LINE]]:1 -> [[@LINE+3]]:2 = #2 b: // CHECK-NEXT: File 0, [[@LINE]]:1 -> [[@LINE+2]]:2 = #3 Index: cfe/trunk/test/CoverageMapping/moremacros.c =================================================================== --- cfe/trunk/test/CoverageMapping/moremacros.c +++ cfe/trunk/test/CoverageMapping/moremacros.c @@ -31,7 +31,7 @@ if (!argc) { return 0; // CHECK-NEXT: Expansion,File 0, [[@LINE+1]]:3 -> [[@LINE+1]]:8 = #4 - RBRAC // CHECK-NEXT: [[@LINE]]:8 -> [[@LINE+1]]:2 = (((#0 - #2) - #3) - #4) + RBRAC } // CHECK-NEXT: File 1, 3:15 -> 3:16 = #2 Index: cfe/trunk/test/CoverageMapping/trycatch.cpp =================================================================== --- cfe/trunk/test/CoverageMapping/trycatch.cpp +++ cfe/trunk/test/CoverageMapping/trycatch.cpp @@ -18,7 +18,7 @@ // CHECK: File 0, [[@LINE+1]]:10 -> [[@LINE+2]]:27 = (#0 - #1) } else if(i == 8) // CHECK-NEXT: File 0, [[@LINE]]:13 -> [[@LINE]]:19 = (#0 - #1) throw ImportantError(); // CHECK: File 0, [[@LINE]]:5 -> [[@LINE]]:27 = #2 -} // CHECK-NEXT: File 0, [[@LINE-1]]:27 -> [[@LINE]]:2 = ((#0 - #1) - #2) +} // CHECK-NEXT: main int main() { // CHECK-NEXT: File 0, [[@LINE]]:12 -> [[@LINE+13]]:2 = #0