r309503 - PR33902: Invalidate line number cache when adding more text to existing buffer.

Richard Smith via cfe-commits cfe-commits at lists.llvm.org
Sat Jul 29 23:31:29 PDT 2017


Author: rsmith
Date: Sat Jul 29 23:31:29 2017
New Revision: 309503

URL: http://llvm.org/viewvc/llvm-project?rev=309503&view=rev
Log:
PR33902: Invalidate line number cache when adding more text to existing buffer.

This led to crashes as the line number cache would report a bogus line number
for a line of code, and we'd try to find a nonexistent column within the line
when printing diagnostics.

Modified:
    cfe/trunk/lib/Lex/ScratchBuffer.cpp
    cfe/trunk/test/Misc/caret-diags-multiline.cpp

Modified: cfe/trunk/lib/Lex/ScratchBuffer.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Lex/ScratchBuffer.cpp?rev=309503&r1=309502&r2=309503&view=diff
==============================================================================
--- cfe/trunk/lib/Lex/ScratchBuffer.cpp (original)
+++ cfe/trunk/lib/Lex/ScratchBuffer.cpp Sat Jul 29 23:31:29 2017
@@ -35,6 +35,14 @@ SourceLocation ScratchBuffer::getToken(c
                                        const char *&DestPtr) {
   if (BytesUsed+Len+2 > ScratchBufSize)
     AllocScratchBuffer(Len+2);
+  else {
+    // Clear out the source line cache if it's already been computed.
+    // FIXME: Allow this to be incrementally extended.
+    auto *ContentCache = const_cast<SrcMgr::ContentCache *>(
+        SourceMgr.getSLocEntry(SourceMgr.getFileID(BufferStartLoc))
+                 .getFile().getContentCache());
+    ContentCache->SourceLineCache = nullptr;
+  }
 
   // Prefix the token with a \n, so that it looks like it is the first thing on
   // its own virtual line in caret diagnostics.

Modified: cfe/trunk/test/Misc/caret-diags-multiline.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Misc/caret-diags-multiline.cpp?rev=309503&r1=309502&r2=309503&view=diff
==============================================================================
--- cfe/trunk/test/Misc/caret-diags-multiline.cpp (original)
+++ cfe/trunk/test/Misc/caret-diags-multiline.cpp Sat Jul 29 23:31:29 2017
@@ -232,3 +232,8 @@ void multiple_ranges(int a, int b) {
   b
   );
 }
+
+#define pr33902_a(b) #b
+#define pr33902_c(d) _Pragma(pr33902_a(d))
+#define pr33902_e(f) pr33902_c(GCC warning #f)
+pr33902_e() pr33902_e()




More information about the cfe-commits mailing list