r279962 - [Coverage] Prevent creating a redundant counter if a nested body ends with a macro.

Igor Kudrin via cfe-commits cfe-commits at lists.llvm.org
Mon Aug 29 04:48:52 PDT 2016


Author: ikudrin
Date: Mon Aug 29 06:48:50 2016
New Revision: 279962

URL: http://llvm.org/viewvc/llvm-project?rev=279962&view=rev
Log:
[Coverage] Prevent creating a redundant counter if a nested body ends with a macro.

If there were several nested statements arranged in a way that all of them
end up with the same macro, then the expansion of this macro was assigned
with all the corresponding counters of these statements.
As a result, the wrong counter value was shown for the macro in llvm-cov.

This patch fixes the issue by preventing adding a counter for an expanded
source range if it already has an assigned counter, which is expected
to come from the most specific statement.

Differential Revision: https://reviews.llvm.org/D23160

Modified:
    cfe/trunk/lib/CodeGen/CoverageMappingGen.cpp
    cfe/trunk/test/CoverageMapping/macros.c

Modified: cfe/trunk/lib/CodeGen/CoverageMappingGen.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/CodeGen/CoverageMappingGen.cpp?rev=279962&r1=279961&r2=279962&view=diff
==============================================================================
--- cfe/trunk/lib/CodeGen/CoverageMappingGen.cpp (original)
+++ cfe/trunk/lib/CodeGen/CoverageMappingGen.cpp Mon Aug 29 06:48:50 2016
@@ -432,7 +432,8 @@ struct CounterCoverageMappingBuilder
           SourceLocation NestedLoc = getStartOfFileOrMacro(EndLoc);
           assert(SM.isWrittenInSameFile(NestedLoc, EndLoc));
 
-          SourceRegions.emplace_back(Region.getCounter(), NestedLoc, EndLoc);
+          if (!isRegionAlreadyAdded(NestedLoc, EndLoc))
+            SourceRegions.emplace_back(Region.getCounter(), NestedLoc, EndLoc);
 
           EndLoc = getPreciseTokenLocEnd(getIncludeOrExpansionLoc(EndLoc));
           if (EndLoc.isInvalid())

Modified: cfe/trunk/test/CoverageMapping/macros.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/CoverageMapping/macros.c?rev=279962&r1=279961&r2=279962&view=diff
==============================================================================
--- cfe/trunk/test/CoverageMapping/macros.c (original)
+++ cfe/trunk/test/CoverageMapping/macros.c Mon Aug 29 06:48:50 2016
@@ -36,8 +36,20 @@ void func3() { // CHECK-NEXT: File 0, [[
 // CHECK-NEXT: File 1, 4:17 -> 4:22 = #0
 // CHECK-NEXT: File 2, 4:17 -> 4:22 = #0
 
+// CHECK-NEXT: func4
+void func4() { // CHECK-NEXT: File 0, [[@LINE]]:14 -> [[@LINE+6]]:2 = #0
+  int i = 0;
+  while (i++ < 10) // CHECK-NEXT: File 0, [[@LINE]]:10 -> [[@LINE]]:18 = (#0 + #1)
+    if (i < 5) // CHECK-NEXT: File 0, [[@LINE]]:5 -> [[@LINE+2]]:14 = #1
+               // CHECK-NEXT: File 0, [[@LINE-1]]:9 -> [[@LINE-1]]:14 = #1
+      MACRO_2; // CHECK-NEXT: Expansion,File 0, [[@LINE]]:7 -> [[@LINE]]:14 = #2
+}
+// CHECK-NEXT: File 1, 4:17 -> 4:22 = #2
+// CHECK-NOT: File 1
+
 int main(int argc, const char *argv[]) {
   func();
   func2();
   func3();
+  func4();
 }




More information about the cfe-commits mailing list