[PATCH] D53244: [Coverage] Fix PR39258: support coverage regions that start deeper than they end

Orivej Desh via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Sat Oct 13 06:52:10 PDT 2018


orivej created this revision.
orivej added reviewers: bogner, rsmith, vsk, aaron.ballman.
Herald added a subscriber: cfe-commits.

popRegions used to assume that the start location of a region can't be nested deeper than the end location, which is not always true.


Repository:
  rC Clang

https://reviews.llvm.org/D53244

Files:
  lib/CodeGen/CoverageMappingGen.cpp
  test/CoverageMapping/macros.c


Index: test/CoverageMapping/macros.c
===================================================================
--- test/CoverageMapping/macros.c
+++ test/CoverageMapping/macros.c
@@ -4,6 +4,7 @@
 #define MACRO_2 bar()
 #define MACRO_1 return; MACRO_2
 #define MACRO_3 MACRO_2
+#define GOTO goto
 
 void bar() {}
 
@@ -56,6 +57,15 @@
 // CHECK-NEXT: Expansion,File 1, 6:17 -> 6:24 = #1
 // CHECK-NEXT: File 2, 4:17 -> 4:22 = #1
 
+// CHECK-NEXT: func6
+void func6(unsigned count) { // CHECK-NEXT: File 0, [[@LINE]]:28 -> [[@LINE+4]]:2 = #0
+begin:                       // CHECK-NEXT: File 0, [[@LINE]]:1 -> [[@LINE+3]]:2 = #1
+    if (count--)             // CHECK-NEXT: File 0, [[@LINE]]:9 -> [[@LINE]]:16 = #1
+        GOTO begin;          // CHECK-NEXT: File 0, [[@LINE]]:9 -> [[@LINE]]:19 = #2
+}
+// CHECK-NEXT: Expansion,File 0, 64:9 -> 64:13 = #2
+// CHECK-NEXT: File 1, 7:14 -> 7:18 = #2
+
 int main(int argc, const char *argv[]) {
   func();
   func2();
Index: lib/CodeGen/CoverageMappingGen.cpp
===================================================================
--- lib/CodeGen/CoverageMappingGen.cpp
+++ lib/CodeGen/CoverageMappingGen.cpp
@@ -552,6 +552,15 @@
     completeDeferred(Count, DeferredEndLoc);
   }
 
+  size_t locactionDepth(SourceLocation Loc) {
+    size_t Depth = 0;
+    while (Loc.isValid()) {
+      Loc = getIncludeOrExpansionLoc(Loc);
+      Depth++;
+    }
+    return Depth;
+  }
+
   /// Pop regions from the stack into the function's list of regions.
   ///
   /// Adds all regions from \c ParentIndex to the top of the stack to the
@@ -566,19 +575,35 @@
         SourceLocation EndLoc = Region.hasEndLoc()
                                     ? Region.getEndLoc()
                                     : RegionStack[ParentIndex].getEndLoc();
+        bool UnnestEnd = locactionDepth(EndLoc) > locactionDepth(StartLoc);
         while (!SM.isWrittenInSameFile(StartLoc, EndLoc)) {
-          // The region ends in a nested file or macro expansion. Create a
-          // separate region for each expansion.
-          SourceLocation NestedLoc = getStartOfFileOrMacro(EndLoc);
-          assert(SM.isWrittenInSameFile(NestedLoc, EndLoc));
-
-          if (!isRegionAlreadyAdded(NestedLoc, EndLoc))
-            SourceRegions.emplace_back(Region.getCounter(), NestedLoc, EndLoc);
-
-          EndLoc = getPreciseTokenLocEnd(getIncludeOrExpansionLoc(EndLoc));
-          if (EndLoc.isInvalid())
-            llvm::report_fatal_error("File exit not handled before popRegions");
+          if (UnnestEnd) {
+            // The region ends in a nested file or macro expansion. Create a
+            // separate region for each expansion.
+            SourceLocation NestedLoc = getStartOfFileOrMacro(EndLoc);
+            assert(SM.isWrittenInSameFile(NestedLoc, EndLoc));
+
+            if (!isRegionAlreadyAdded(NestedLoc, EndLoc))
+              SourceRegions.emplace_back(Region.getCounter(), NestedLoc, EndLoc);
+
+            EndLoc = getPreciseTokenLocEnd(getIncludeOrExpansionLoc(EndLoc));
+            if (EndLoc.isInvalid())
+              llvm::report_fatal_error("File exit not handled before popRegions");
+          } else {
+            // The region begins in a nested file or macro expansion. Create a
+            // separate region for each expansion.
+            SourceLocation NestedLoc = getEndOfFileOrMacro(StartLoc);
+            assert(SM.isWrittenInSameFile(StartLoc, NestedLoc));
+
+            if (!isRegionAlreadyAdded(StartLoc, NestedLoc))
+              SourceRegions.emplace_back(Region.getCounter(), StartLoc, NestedLoc);
+
+            StartLoc = getIncludeOrExpansionLoc(StartLoc);
+            if (StartLoc.isInvalid())
+              llvm::report_fatal_error("File exit not handled before popRegions");
+          }
         }
+        Region.setStartLoc(StartLoc);
         Region.setEndLoc(EndLoc);
 
         MostRecentLocation = EndLoc;


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D53244.169561.patch
Type: text/x-patch
Size: 3903 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20181013/34ed29a0/attachment.bin>


More information about the cfe-commits mailing list