[Coverage] Remove deferred region for trailing return, fixes PR34611

As a special case, throw away deferred regions for trailing returns.
This allows the closing curly brace to have a count, and is less
distracting.

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@313603 91177308-0d34-0410-b5e6-96231b3b80d8
(cherry picked from commit b3bac44cb354b63214b16fab24b5c0ffd392ec02)
diff --git a/lib/CodeGen/CoverageMappingGen.cpp b/lib/CodeGen/CoverageMappingGen.cpp
index 943c7e0..8f6f3ed 100644
--- a/lib/CodeGen/CoverageMappingGen.cpp
+++ b/lib/CodeGen/CoverageMappingGen.cpp
@@ -770,7 +770,13 @@
     Counter ExitCount = propagateCounts(getRegionCounter(Body), Body);
     assert(RegionStack.empty() && "Regions entered but never exited");
 
-    // Complete any deferred regions introduced by the last statement in a decl.
+    // Special case: if the last statement is a return, throw away the
+    // deferred region. This allows the closing brace to have a count.
+    if (auto *CS = dyn_cast_or_null<CompoundStmt>(Body))
+      if (dyn_cast_or_null<ReturnStmt>(CS->body_back()))
+        DeferredRegion = None;
+
+    // Complete any deferred regions introduced by the last statement.
     popRegions(completeDeferred(ExitCount, getEnd(Body)));
   }
 
diff --git a/test/CoverageMapping/deferred-region.cpp b/test/CoverageMapping/deferred-region.cpp
index 41b5e6b..743b635 100644
--- a/test/CoverageMapping/deferred-region.cpp
+++ b/test/CoverageMapping/deferred-region.cpp
@@ -28,6 +28,14 @@
   return;    // CHECK-NOT: File
 }
 
+// CHECK-LABEL: _Z3mazv:
+void maz() {
+  if (true)
+    return; // CHECK: Gap,File 0, [[@LINE]]:11 -> 36:3 = (#0 - #1)
+
+  return; // CHECK-NOT: Gap
+}
+
 // CHECK-LABEL: _Z3bari:
 void bar(int x) {
   IF (x)