r233169 - InstrProf: Handle whitespace and comments at the ends of macros

Justin Bogner mail at justinbogner.com
Tue Mar 24 21:13:50 PDT 2015


Author: bogner
Date: Tue Mar 24 23:13:49 2015
New Revision: 233169

URL: http://llvm.org/viewvc/llvm-project?rev=233169&view=rev
Log:
InstrProf: Handle whitespace and comments at the ends of macros

When we try to find the end loc for a token, we have to re-lex the
token. This was running into a problem when we'd store the end loc of
a macro's coverage region, since we wouldn't actually be at the
beginning of a token when we tried to re-lex it, leading us to do
silly things (and eventually assert) when whitespace or comments
followed.

This pushes our use of getPreciseTokenLocEnd earlier, so that we won't
call it when it doesn't make sense to. It also removes an unnecessary
adjustment by 1 that was working around this problem in some cases.

Added:
    cfe/trunk/test/CoverageMapping/comment-in-macro.c
Modified:
    cfe/trunk/lib/CodeGen/CoverageMappingGen.cpp

Modified: cfe/trunk/lib/CodeGen/CoverageMappingGen.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/CodeGen/CoverageMappingGen.cpp?rev=233169&r1=233168&r2=233169&view=diff
==============================================================================
--- cfe/trunk/lib/CodeGen/CoverageMappingGen.cpp (original)
+++ cfe/trunk/lib/CodeGen/CoverageMappingGen.cpp Tue Mar 24 23:13:49 2015
@@ -124,7 +124,7 @@ public:
   SourceLocation getEndOfFileOrMacro(SourceLocation Loc) {
     if (Loc.isMacroID())
       return Loc.getLocWithOffset(SM.getFileIDSize(SM.getFileID(Loc)) -
-                                  SM.getFileOffset(Loc) - 1);
+                                  SM.getFileOffset(Loc));
     return SM.getLocForEndOfFile(SM.getFileID(Loc));
   }
 
@@ -147,7 +147,7 @@ public:
     SourceLocation Loc = S->getLocEnd();
     while (SM.isMacroArgExpansion(Loc))
       Loc = SM.getImmediateExpansionRange(Loc).first;
-    return Loc;
+    return getPreciseTokenLocEnd(Loc);
   }
 
   /// \brief Find the set of files we have regions for and assign IDs
@@ -257,7 +257,7 @@ public:
       if (!CovFileID)
         continue;
 
-      SourceLocation LocEnd = getPreciseTokenLocEnd(Region.getEndLoc());
+      SourceLocation LocEnd = Region.getEndLoc();
       assert(SM.isWrittenInSameFile(LocStart, LocEnd) &&
              "region spans multiple files");
 
@@ -407,7 +407,7 @@ struct CounterCoverageMappingBuilder
 
           SourceRegions.emplace_back(Region.getCounter(), NestedLoc, EndLoc);
 
-          EndLoc = getIncludeOrExpansionLoc(EndLoc);
+          EndLoc = getPreciseTokenLocEnd(getIncludeOrExpansionLoc(EndLoc));
           assert(!EndLoc.isInvalid() &&
                  "File exit was not handled before popRegions");
         }

Added: cfe/trunk/test/CoverageMapping/comment-in-macro.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/CoverageMapping/comment-in-macro.c?rev=233169&view=auto
==============================================================================
--- cfe/trunk/test/CoverageMapping/comment-in-macro.c (added)
+++ cfe/trunk/test/CoverageMapping/comment-in-macro.c Tue Mar 24 23:13:49 2015
@@ -0,0 +1,11 @@
+// RUN: %clang_cc1 -fprofile-instr-generate -fcoverage-mapping -dump-coverage-mapping -emit-llvm-only %s | FileCheck %s
+
+#define x1 "" // ...
+#define x2 return 0
+// CHECK: main
+int main() { // CHECK-NEXT: File 0, [[@LINE]]:12 -> [[@LINE+3]]:2 = #0
+  x1;        // CHECK-NEXT: Expansion,File 0, [[@LINE]]:3 -> [[@LINE]]:5 = #0
+  x2;        // CHECK-NEXT: Expansion,File 0, [[@LINE]]:3 -> [[@LINE]]:5 = #0
+}
+// CHECK-NEXT: File 1, 3:12 -> 3:14 = #0
+// CHECK-NEXT: File 2, 4:12 -> 4:20 = #0





More information about the cfe-commits mailing list