Author: Duncan P. N. Exon Smith
Date: 2020-10-26T14:07:46-04:00
New Revision: 0387015d7549fc2c0cc8c45d3e78114cc4fd153f

URL: 
https://github.com/llvm/llvm-project/commit/0387015d7549fc2c0cc8c45d3e78114cc4fd153f
DIFF: 
https://github.com/llvm/llvm-project/commit/0387015d7549fc2c0cc8c45d3e78114cc4fd153f.diff

LOG: SourceManager: Return non-const references in getOrCreateContentCache and 
related, NFC

Update a few APIs to return non-const references instead of pointers,
and remove associated `const_cast`s and non-null assertions.

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

Added: 
    

Modified: 
    clang/include/clang/Basic/SourceManager.h
    clang/lib/Basic/SourceManager.cpp
    clang/lib/Serialization/ASTReader.cpp

Removed: 
    


################################################################################
diff  --git a/clang/include/clang/Basic/SourceManager.h 
b/clang/include/clang/Basic/SourceManager.h
index a458ef7e72a8..99428e6d7efa 100644
--- a/clang/include/clang/Basic/SourceManager.h
+++ b/clang/include/clang/Basic/SourceManager.h
@@ -297,7 +297,7 @@ namespace SrcMgr {
 
   public:
     /// Return a FileInfo object.
-    static FileInfo get(SourceLocation IL, const ContentCache &Con,
+    static FileInfo get(SourceLocation IL, ContentCache &Con,
                         CharacteristicKind FileCharacter, StringRef Filename) {
       FileInfo X;
       X.IncludeLoc = IL.getRawEncoding();
@@ -923,7 +923,7 @@ class SourceManager : public RefCountedBase<SourceManager> {
   llvm::MemoryBufferRef getMemoryBufferForFileOrFake(const FileEntry *File) {
     if (auto B = getMemoryBufferForFileOrNone(File))
       return *B;
-    return getFakeBufferForRecovery()->getMemBufferRef();
+    return getFakeBufferForRecovery();
   }
 
   /// Override the contents of the given source file by providing an
@@ -1008,7 +1008,7 @@ class SourceManager : public 
RefCountedBase<SourceManager> {
   getBufferOrFake(FileID FID, SourceLocation Loc = SourceLocation()) const {
     if (auto B = getBufferOrNone(FID, Loc))
       return *B;
-    return getFakeBufferForRecovery()->getMemBufferRef();
+    return getFakeBufferForRecovery();
   }
 
   /// Returns the FileEntry record for the provided FileID.
@@ -1738,8 +1738,8 @@ class SourceManager : public 
RefCountedBase<SourceManager> {
   friend class ASTReader;
   friend class ASTWriter;
 
-  llvm::MemoryBuffer *getFakeBufferForRecovery() const;
-  const SrcMgr::ContentCache *getFakeContentCacheForRecovery() const;
+  llvm::MemoryBufferRef getFakeBufferForRecovery() const;
+  SrcMgr::ContentCache &getFakeContentCacheForRecovery() const;
 
   const SrcMgr::SLocEntry &loadSLocEntry(unsigned Index, bool *Invalid) const;
 
@@ -1811,17 +1811,16 @@ class SourceManager : public 
RefCountedBase<SourceManager> {
   ///
   /// This works regardless of whether the ContentCache corresponds to a
   /// file or some other input source.
-  FileID createFileIDImpl(const SrcMgr::ContentCache &File, StringRef Filename,
+  FileID createFileIDImpl(SrcMgr::ContentCache &File, StringRef Filename,
                           SourceLocation IncludePos,
                           SrcMgr::CharacteristicKind DirCharacter, int 
LoadedID,
                           unsigned LoadedOffset);
 
-  const SrcMgr::ContentCache *
-    getOrCreateContentCache(const FileEntry *SourceFile,
-                            bool isSystemFile = false);
+  SrcMgr::ContentCache &getOrCreateContentCache(const FileEntry *SourceFile,
+                                                bool isSystemFile = false);
 
   /// Create a new ContentCache for the specified  memory buffer.
-  const SrcMgr::ContentCache *
+  SrcMgr::ContentCache &
   createMemBufferContentCache(std::unique_ptr<llvm::MemoryBuffer> Buf);
 
   FileID getFileIDSlow(unsigned SLocOffset) const;

diff  --git a/clang/lib/Basic/SourceManager.cpp 
b/clang/lib/Basic/SourceManager.cpp
index 1feb8c7b66dd..2ceb8046098f 100644
--- a/clang/lib/Basic/SourceManager.cpp
+++ b/clang/lib/Basic/SourceManager.cpp
@@ -380,16 +380,14 @@ void SourceManager::initializeForReplay(const 
SourceManager &Old) {
   }
 }
 
-/// getOrCreateContentCache - Create or return a cached ContentCache for the
-/// specified file.
-const ContentCache *
-SourceManager::getOrCreateContentCache(const FileEntry *FileEnt,
-                                       bool isSystemFile) {
+ContentCache &SourceManager::getOrCreateContentCache(const FileEntry *FileEnt,
+                                                     bool isSystemFile) {
   assert(FileEnt && "Didn't specify a file entry to use?");
 
   // Do we already have information about this file?
   ContentCache *&Entry = FileInfos[FileEnt];
-  if (Entry) return Entry;
+  if (Entry)
+    return *Entry;
 
   // Nope, create a new Cache entry.
   Entry = ContentCacheAlloc.Allocate<ContentCache>();
@@ -412,19 +410,19 @@ SourceManager::getOrCreateContentCache(const FileEntry 
*FileEnt,
   Entry->IsFileVolatile = UserFilesAreVolatile && !isSystemFile;
   Entry->IsTransient = FilesAreTransient;
 
-  return Entry;
+  return *Entry;
 }
 
 /// Create a new ContentCache for the specified memory buffer.
 /// This does no caching.
-const ContentCache *SourceManager::createMemBufferContentCache(
+ContentCache &SourceManager::createMemBufferContentCache(
     std::unique_ptr<llvm::MemoryBuffer> Buffer) {
   // Add a new ContentCache to the MemBufferInfos list and return it.
   ContentCache *Entry = ContentCacheAlloc.Allocate<ContentCache>();
   new (Entry) ContentCache();
   MemBufferInfos.push_back(Entry);
   Entry->setBuffer(std::move(Buffer));
-  return Entry;
+  return *Entry;
 }
 
 const SrcMgr::SLocEntry &SourceManager::loadSLocEntry(unsigned Index,
@@ -437,7 +435,7 @@ const SrcMgr::SLocEntry 
&SourceManager::loadSLocEntry(unsigned Index,
     if (!SLocEntryLoaded[Index]) {
       // Try to recover; create a SLocEntry so the rest of clang can handle it.
       LoadedSLocEntryTable[Index] = SLocEntry::get(
-          0, FileInfo::get(SourceLocation(), *getFakeContentCacheForRecovery(),
+          0, FileInfo::get(SourceLocation(), getFakeContentCacheForRecovery(),
                            SrcMgr::C_User, ""));
     }
   }
@@ -461,24 +459,22 @@ SourceManager::AllocateLoadedSLocEntries(unsigned 
NumSLocEntries,
 
 /// As part of recovering from missing or changed content, produce a
 /// fake, non-empty buffer.
-llvm::MemoryBuffer *SourceManager::getFakeBufferForRecovery() const {
+llvm::MemoryBufferRef SourceManager::getFakeBufferForRecovery() const {
   if (!FakeBufferForRecovery)
     FakeBufferForRecovery =
         llvm::MemoryBuffer::getMemBuffer("<<<INVALID BUFFER>>");
 
-  return FakeBufferForRecovery.get();
+  return *FakeBufferForRecovery;
 }
 
 /// As part of recovering from missing or changed content, produce a
 /// fake content cache.
-const SrcMgr::ContentCache *
-SourceManager::getFakeContentCacheForRecovery() const {
+SrcMgr::ContentCache &SourceManager::getFakeContentCacheForRecovery() const {
   if (!FakeContentCacheForRecovery) {
     FakeContentCacheForRecovery = std::make_unique<SrcMgr::ContentCache>();
-    FakeContentCacheForRecovery->setUnownedBuffer(
-        getFakeBufferForRecovery()->getMemBufferRef());
+    FakeContentCacheForRecovery->setUnownedBuffer(getFakeBufferForRecovery());
   }
-  return FakeContentCacheForRecovery.get();
+  return *FakeContentCacheForRecovery;
 }
 
 /// Returns the previous in-order FileID or an invalid FileID if there
@@ -531,21 +527,19 @@ FileID SourceManager::createFileID(const FileEntry 
*SourceFile,
                                    SrcMgr::CharacteristicKind FileCharacter,
                                    int LoadedID, unsigned LoadedOffset) {
   assert(SourceFile && "Null source file!");
-  const SrcMgr::ContentCache *IR =
+  SrcMgr::ContentCache &IR =
       getOrCreateContentCache(SourceFile, isSystem(FileCharacter));
-  assert(IR && "getOrCreateContentCache() cannot return NULL");
-  return createFileIDImpl(*IR, SourceFile->getName(), IncludePos, 
FileCharacter,
-                     LoadedID, LoadedOffset);
+  return createFileIDImpl(IR, SourceFile->getName(), IncludePos, FileCharacter,
+                          LoadedID, LoadedOffset);
 }
 
 FileID SourceManager::createFileID(FileEntryRef SourceFile,
                                    SourceLocation IncludePos,
                                    SrcMgr::CharacteristicKind FileCharacter,
                                    int LoadedID, unsigned LoadedOffset) {
-  const SrcMgr::ContentCache *IR = getOrCreateContentCache(
-      &SourceFile.getFileEntry(), isSystem(FileCharacter));
-  assert(IR && "getOrCreateContentCache() cannot return NULL");
-  return createFileIDImpl(*IR, SourceFile.getName(), IncludePos, FileCharacter,
+  SrcMgr::ContentCache &IR = 
getOrCreateContentCache(&SourceFile.getFileEntry(),
+                                                     isSystem(FileCharacter));
+  return createFileIDImpl(IR, SourceFile.getName(), IncludePos, FileCharacter,
                           LoadedID, LoadedOffset);
 }
 
@@ -558,7 +552,7 @@ FileID 
SourceManager::createFileID(std::unique_ptr<llvm::MemoryBuffer> Buffer,
                                    int LoadedID, unsigned LoadedOffset,
                                    SourceLocation IncludeLoc) {
   StringRef Name = Buffer->getBufferIdentifier();
-  return createFileIDImpl(*createMemBufferContentCache(std::move(Buffer)), 
Name,
+  return createFileIDImpl(createMemBufferContentCache(std::move(Buffer)), Name,
                           IncludeLoc, FileCharacter, LoadedID, LoadedOffset);
 }
 
@@ -587,8 +581,7 @@ SourceManager::getOrCreateFileID(const FileEntry 
*SourceFile,
 /// createFileID - Create a new FileID for the specified ContentCache and
 /// include position.  This works regardless of whether the ContentCache
 /// corresponds to a file or some other input source.
-FileID SourceManager::createFileIDImpl(const ContentCache &File,
-                                       StringRef Filename,
+FileID SourceManager::createFileIDImpl(ContentCache &File, StringRef Filename,
                                        SourceLocation IncludePos,
                                        SrcMgr::CharacteristicKind 
FileCharacter,
                                        int LoadedID, unsigned LoadedOffset) {
@@ -678,19 +671,16 @@ SourceManager::createExpansionLocImpl(const ExpansionInfo 
&Info,
 
 llvm::Optional<llvm::MemoryBufferRef>
 SourceManager::getMemoryBufferForFileOrNone(const FileEntry *File) {
-  const SrcMgr::ContentCache *IR = getOrCreateContentCache(File);
-  assert(IR && "getOrCreateContentCache() cannot return NULL");
-  return IR->getBufferOrNone(Diag, getFileManager(), SourceLocation());
+  SrcMgr::ContentCache &IR = getOrCreateContentCache(File);
+  return IR.getBufferOrNone(Diag, getFileManager(), SourceLocation());
 }
 
 void SourceManager::overrideFileContents(
     const FileEntry *SourceFile, std::unique_ptr<llvm::MemoryBuffer> Buffer) {
-  auto *IR =
-      const_cast<SrcMgr::ContentCache *>(getOrCreateContentCache(SourceFile));
-  assert(IR && "getOrCreateContentCache() cannot return NULL");
+  SrcMgr::ContentCache &IR = getOrCreateContentCache(SourceFile);
 
-  IR->setBuffer(std::move(Buffer));
-  IR->BufferOverridden = true;
+  IR.setBuffer(std::move(Buffer));
+  IR.BufferOverridden = true;
 
   getOverriddenFilesInfo().OverriddenFilesWithBuffer.insert(SourceFile);
 }
@@ -722,8 +712,7 @@ SourceManager::bypassFileContentsOverride(const FileEntry 
&File) {
 }
 
 void SourceManager::setFileIsTransient(const FileEntry *File) {
-  const SrcMgr::ContentCache *CC = getOrCreateContentCache(File);
-  const_cast<SrcMgr::ContentCache *>(CC)->IsTransient = true;
+  getOrCreateContentCache(File).IsTransient = true;
 }
 
 Optional<StringRef>

diff  --git a/clang/lib/Serialization/ASTReader.cpp 
b/clang/lib/Serialization/ASTReader.cpp
index bad4342d380d..808889b67333 100644
--- a/clang/lib/Serialization/ASTReader.cpp
+++ b/clang/lib/Serialization/ASTReader.cpp
@@ -1538,11 +1538,11 @@ bool ASTReader::ReadSLocEntry(int ID) {
                                                              NumFileDecls));
     }
 
-    const SrcMgr::ContentCache *ContentCache
-      = SourceMgr.getOrCreateContentCache(File, isSystem(FileCharacter));
-    if (OverriddenBuffer && !ContentCache->BufferOverridden &&
-        ContentCache->ContentsEntry == ContentCache->OrigEntry &&
-        !ContentCache->getBufferIfLoaded()) {
+    const SrcMgr::ContentCache &ContentCache =
+        SourceMgr.getOrCreateContentCache(File, isSystem(FileCharacter));
+    if (OverriddenBuffer && !ContentCache.BufferOverridden &&
+        ContentCache.ContentsEntry == ContentCache.OrigEntry &&
+        !ContentCache.getBufferIfLoaded()) {
       auto Buffer = ReadBuffer(SLocEntryCursor, File->getName());
       if (!Buffer)
         return true;


        
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to