[clang] 5431c37 - SourceManager: Make LastLineNoContentCache and ContentCache::SourceLineCache mutable, NFC

Duncan P. N. Exon Smith via cfe-commits cfe-commits at lists.llvm.org
Fri Oct 23 10:22:55 PDT 2020


Author: Duncan P. N. Exon Smith
Date: 2020-10-23T13:22:47-04:00
New Revision: 5431c37b55e2c2952b6b56c9690bd1ce05b23c7e

URL: https://github.com/llvm/llvm-project/commit/5431c37b55e2c2952b6b56c9690bd1ce05b23c7e
DIFF: https://github.com/llvm/llvm-project/commit/5431c37b55e2c2952b6b56c9690bd1ce05b23c7e.diff

LOG: SourceManager: Make LastLineNoContentCache and ContentCache::SourceLineCache mutable, NFC

Avoid some noisy `const_cast`s by making `ContentCache::SourceLineCache`
and `SourceManager::LastLineNoContentCache` both mutable.

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

Added: 
    

Modified: 
    clang/include/clang/Basic/SourceManager.h
    clang/lib/Basic/SourceManager.cpp
    clang/lib/Lex/ScratchBuffer.cpp

Removed: 
    


################################################################################
diff  --git a/clang/include/clang/Basic/SourceManager.h b/clang/include/clang/Basic/SourceManager.h
index 00a790038dbf..a458ef7e72a8 100644
--- a/clang/include/clang/Basic/SourceManager.h
+++ b/clang/include/clang/Basic/SourceManager.h
@@ -146,7 +146,7 @@ namespace SrcMgr {
     ///
     /// This is lazily computed.  The lines are owned by the SourceManager
     /// BumpPointerAllocator object.
-    LineOffsetMapping SourceLineCache;
+    mutable LineOffsetMapping SourceLineCache;
 
     /// Indicates whether the buffer itself was provided to override
     /// the actual file contents.
@@ -719,7 +719,7 @@ class SourceManager : public RefCountedBase<SourceManager> {
   /// These ivars serve as a cache used in the getLineNumber
   /// method which is used to speedup getLineNumber calls to nearby locations.
   mutable FileID LastLineNoFileIDQuery;
-  mutable SrcMgr::ContentCache *LastLineNoContentCache;
+  mutable const SrcMgr::ContentCache *LastLineNoContentCache;
   mutable unsigned LastLineNoFilePos;
   mutable unsigned LastLineNoResult;
 

diff  --git a/clang/lib/Basic/SourceManager.cpp b/clang/lib/Basic/SourceManager.cpp
index 778fd0dca329..f8607b0d4c24 100644
--- a/clang/lib/Basic/SourceManager.cpp
+++ b/clang/lib/Basic/SourceManager.cpp
@@ -1261,20 +1261,20 @@ unsigned SourceManager::getPresumedColumnNumber(SourceLocation Loc,
 #endif
 
 static LLVM_ATTRIBUTE_NOINLINE void
-ComputeLineNumbers(DiagnosticsEngine &Diag, ContentCache *FI,
+ComputeLineNumbers(DiagnosticsEngine &Diag, const ContentCache &FI,
                    llvm::BumpPtrAllocator &Alloc,
                    const SourceManager &SM, bool &Invalid);
-static void ComputeLineNumbers(DiagnosticsEngine &Diag, ContentCache *FI,
+static void ComputeLineNumbers(DiagnosticsEngine &Diag, const ContentCache &FI,
                                llvm::BumpPtrAllocator &Alloc,
                                const SourceManager &SM, bool &Invalid) {
   // Note that calling 'getBuffer()' may lazily page in the file.
   llvm::Optional<llvm::MemoryBufferRef> Buffer =
-      FI->getBufferOrNone(Diag, SM.getFileManager(), SourceLocation());
+      FI.getBufferOrNone(Diag, SM.getFileManager(), SourceLocation());
   Invalid = !Buffer;
   if (Invalid)
     return;
 
-  FI->SourceLineCache = LineOffsetMapping::get(*Buffer, Alloc);
+  FI.SourceLineCache = LineOffsetMapping::get(*Buffer, Alloc);
 }
 
 LineOffsetMapping LineOffsetMapping::get(llvm::MemoryBufferRef Buffer,
@@ -1324,7 +1324,7 @@ unsigned SourceManager::getLineNumber(FileID FID, unsigned FilePos,
     return 1;
   }
 
-  ContentCache *Content;
+  const ContentCache *Content;
   if (LastLineNoFileIDQuery == FID)
     Content = LastLineNoContentCache;
   else {
@@ -1336,14 +1336,14 @@ unsigned SourceManager::getLineNumber(FileID FID, unsigned FilePos,
       return 1;
     }
 
-    Content = const_cast<ContentCache *>(&Entry.getFile().getContentCache());
+    Content = &Entry.getFile().getContentCache();
   }
 
   // If this is the first use of line information for this buffer, compute the
   /// SourceLineCache for it on demand.
   if (!Content->SourceLineCache) {
     bool MyInvalid = false;
-    ComputeLineNumbers(Diag, Content, ContentCacheAlloc, *this, MyInvalid);
+    ComputeLineNumbers(Diag, *Content, ContentCacheAlloc, *this, MyInvalid);
     if (Invalid)
       *Invalid = MyInvalid;
     if (MyInvalid)
@@ -1685,14 +1685,13 @@ SourceLocation SourceManager::translateLineCol(FileID FID,
   if (Line == 1 && Col == 1)
     return FileLoc;
 
-  ContentCache *Content =
-      const_cast<ContentCache *>(&Entry.getFile().getContentCache());
+  const ContentCache *Content = &Entry.getFile().getContentCache();
 
   // If this is the first use of line information for this buffer, compute the
   // SourceLineCache for it on demand.
   if (!Content->SourceLineCache) {
     bool MyInvalid = false;
-    ComputeLineNumbers(Diag, Content, ContentCacheAlloc, *this, MyInvalid);
+    ComputeLineNumbers(Diag, *Content, ContentCacheAlloc, *this, MyInvalid);
     if (MyInvalid)
       return SourceLocation();
   }

diff  --git a/clang/lib/Lex/ScratchBuffer.cpp b/clang/lib/Lex/ScratchBuffer.cpp
index 56ea9b71fd02..51435225a676 100644
--- a/clang/lib/Lex/ScratchBuffer.cpp
+++ b/clang/lib/Lex/ScratchBuffer.cpp
@@ -37,11 +37,10 @@ SourceLocation ScratchBuffer::getToken(const char *Buf, unsigned Len,
   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 = SrcMgr::LineOffsetMapping();
+    SourceMgr.getSLocEntry(SourceMgr.getFileID(BufferStartLoc))
+        .getFile()
+        .getContentCache()
+        .SourceLineCache = SrcMgr::LineOffsetMapping();
   }
 
   // Prefix the token with a \n, so that it looks like it is the first thing on


        


More information about the cfe-commits mailing list