[clang] b6c6daa - SourceManager: Factor out helpers for common SLocEntry lookup pattern, NFC

Duncan P. N. Exon Smith via cfe-commits cfe-commits at lists.llvm.org
Thu Oct 22 18:31:52 PDT 2020


Author: Duncan P. N. Exon Smith
Date: 2020-10-22T21:30:31-04:00
New Revision: b6c6daa95d3aa2206d5a42b46793226f181c3e44

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

LOG: SourceManager: Factor out helpers for common SLocEntry lookup pattern, NFC

Add helpers `getSLocEntryOrNull`, which handles the `Invalid` logic
around `getSLocEntry`, and `getSLocEntryForFile`, which also checks for
`SLocEntry::isFile`, and use them to reduce repeated code.

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

Added: 
    

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

Removed: 
    


################################################################################
diff  --git a/clang/include/clang/Basic/SourceManager.h b/clang/include/clang/Basic/SourceManager.h
index 4b84ae132c3e..57f8e9d365ad 100644
--- a/clang/include/clang/Basic/SourceManager.h
+++ b/clang/include/clang/Basic/SourceManager.h
@@ -972,13 +972,10 @@ class SourceManager : public RefCountedBase<SourceManager> {
   /// If there is an error opening this buffer the first time, return None.
   llvm::Optional<llvm::MemoryBufferRef>
   getBufferOrNone(FileID FID, SourceLocation Loc = SourceLocation()) const {
-    bool MyInvalid = false;
-    const SrcMgr::SLocEntry &Entry = getSLocEntry(FID, &MyInvalid);
-    if (MyInvalid || !Entry.isFile())
-      return None;
-
-    return Entry.getFile().getContentCache()->getBufferOrNone(
-        Diag, getFileManager(), Loc);
+    if (auto *Entry = getSLocEntryForFile(FID))
+      return Entry->getFile().getContentCache()->getBufferOrNone(
+          Diag, getFileManager(), Loc);
+    return None;
   }
 
   /// Return the buffer for the specified FileID.
@@ -994,15 +991,10 @@ class SourceManager : public RefCountedBase<SourceManager> {
 
   /// Returns the FileEntry record for the provided FileID.
   const FileEntry *getFileEntryForID(FileID FID) const {
-    bool MyInvalid = false;
-    const SrcMgr::SLocEntry &Entry = getSLocEntry(FID, &MyInvalid);
-    if (MyInvalid || !Entry.isFile())
-      return nullptr;
-
-    const SrcMgr::ContentCache *Content = Entry.getFile().getContentCache();
-    if (!Content)
-      return nullptr;
-    return Content->OrigEntry;
+    if (auto *Entry = getSLocEntryForFile(FID))
+      if (auto *Content = Entry->getFile().getContentCache())
+        return Content->OrigEntry;
+    return nullptr;
   }
 
   /// Returns the FileEntryRef for the provided FileID.
@@ -1039,25 +1031,20 @@ class SourceManager : public RefCountedBase<SourceManager> {
   /// Get the number of FileIDs (files and macros) that were created
   /// during preprocessing of \p FID, including it.
   unsigned getNumCreatedFIDsForFileID(FileID FID) const {
-    bool Invalid = false;
-    const SrcMgr::SLocEntry &Entry = getSLocEntry(FID, &Invalid);
-    if (Invalid || !Entry.isFile())
-      return 0;
-
-    return Entry.getFile().NumCreatedFIDs;
+    if (auto *Entry = getSLocEntryForFile(FID))
+      return Entry->getFile().NumCreatedFIDs;
+    return 0;
   }
 
   /// Set the number of FileIDs (files and macros) that were created
   /// during preprocessing of \p FID, including it.
   void setNumCreatedFIDsForFileID(FileID FID, unsigned NumFIDs,
                                   bool Force = false) const {
-    bool Invalid = false;
-    const SrcMgr::SLocEntry &Entry = getSLocEntry(FID, &Invalid);
-    if (Invalid || !Entry.isFile())
+    auto *Entry = getSLocEntryForFile(FID);
+    if (!Entry)
       return;
-
-    assert((Force || Entry.getFile().NumCreatedFIDs == 0) && "Already set!");
-    const_cast<SrcMgr::FileInfo &>(Entry.getFile()).NumCreatedFIDs = NumFIDs;
+    assert((Force || Entry->getFile().NumCreatedFIDs == 0) && "Already set!");
+    const_cast<SrcMgr::FileInfo &>(Entry->getFile()).NumCreatedFIDs = NumFIDs;
   }
 
   //===--------------------------------------------------------------------===//
@@ -1086,36 +1073,26 @@ class SourceManager : public RefCountedBase<SourceManager> {
   /// Return the source location corresponding to the first byte of
   /// the specified file.
   SourceLocation getLocForStartOfFile(FileID FID) const {
-    bool Invalid = false;
-    const SrcMgr::SLocEntry &Entry = getSLocEntry(FID, &Invalid);
-    if (Invalid || !Entry.isFile())
-      return SourceLocation();
-
-    unsigned FileOffset = Entry.getOffset();
-    return SourceLocation::getFileLoc(FileOffset);
+    if (auto *Entry = getSLocEntryForFile(FID))
+      return SourceLocation::getFileLoc(Entry->getOffset());
+    return SourceLocation();
   }
 
   /// Return the source location corresponding to the last byte of the
   /// specified file.
   SourceLocation getLocForEndOfFile(FileID FID) const {
-    bool Invalid = false;
-    const SrcMgr::SLocEntry &Entry = getSLocEntry(FID, &Invalid);
-    if (Invalid || !Entry.isFile())
-      return SourceLocation();
-
-    unsigned FileOffset = Entry.getOffset();
-    return SourceLocation::getFileLoc(FileOffset + getFileIDSize(FID));
+    if (auto *Entry = getSLocEntryForFile(FID))
+      return SourceLocation::getFileLoc(Entry->getOffset() +
+                                        getFileIDSize(FID));
+    return SourceLocation();
   }
 
   /// Returns the include location if \p FID is a \#include'd file
   /// otherwise it returns an invalid location.
   SourceLocation getIncludeLoc(FileID FID) const {
-    bool Invalid = false;
-    const SrcMgr::SLocEntry &Entry = getSLocEntry(FID, &Invalid);
-    if (Invalid || !Entry.isFile())
-      return SourceLocation();
-
-    return Entry.getFile().getIncludeLoc();
+    if (auto *Entry = getSLocEntryForFile(FID))
+      return Entry->getFile().getIncludeLoc();
+    return SourceLocation();
   }
 
   // Returns the import location if the given source location is
@@ -1200,14 +1177,13 @@ class SourceManager : public RefCountedBase<SourceManager> {
 
   /// Form a SourceLocation from a FileID and Offset pair.
   SourceLocation getComposedLoc(FileID FID, unsigned Offset) const {
-    bool Invalid = false;
-    const SrcMgr::SLocEntry &Entry = getSLocEntry(FID, &Invalid);
-    if (Invalid)
+    auto *Entry = getSLocEntryOrNull(FID);
+    if (!Entry)
       return SourceLocation();
 
-    unsigned GlobalOffset = Entry.getOffset() + Offset;
-    return Entry.isFile() ? SourceLocation::getFileLoc(GlobalOffset)
-                          : SourceLocation::getMacroLoc(GlobalOffset);
+    unsigned GlobalOffset = Entry->getOffset() + Offset;
+    return Entry->isFile() ? SourceLocation::getFileLoc(GlobalOffset)
+                           : SourceLocation::getMacroLoc(GlobalOffset);
   }
 
   /// Decompose the specified location into a raw FileID + Offset pair.
@@ -1216,11 +1192,10 @@ class SourceManager : public RefCountedBase<SourceManager> {
   /// start of the buffer of the location.
   std::pair<FileID, unsigned> getDecomposedLoc(SourceLocation Loc) const {
     FileID FID = getFileID(Loc);
-    bool Invalid = false;
-    const SrcMgr::SLocEntry &E = getSLocEntry(FID, &Invalid);
-    if (Invalid)
+    auto *Entry = getSLocEntryOrNull(FID);
+    if (!Entry)
       return std::make_pair(FileID(), 0);
-    return std::make_pair(FID, Loc.getOffset()-E.getOffset());
+    return std::make_pair(FID, Loc.getOffset() - Entry->getOffset());
   }
 
   /// Decompose the specified location into a raw FileID + Offset pair.
@@ -1230,9 +1205,8 @@ class SourceManager : public RefCountedBase<SourceManager> {
   std::pair<FileID, unsigned>
   getDecomposedExpansionLoc(SourceLocation Loc) const {
     FileID FID = getFileID(Loc);
-    bool Invalid = false;
-    const SrcMgr::SLocEntry *E = &getSLocEntry(FID, &Invalid);
-    if (Invalid)
+    auto *E = getSLocEntryOrNull(FID);
+    if (!E)
       return std::make_pair(FileID(), 0);
 
     unsigned Offset = Loc.getOffset()-E->getOffset();
@@ -1249,9 +1223,8 @@ class SourceManager : public RefCountedBase<SourceManager> {
   std::pair<FileID, unsigned>
   getDecomposedSpellingLoc(SourceLocation Loc) const {
     FileID FID = getFileID(Loc);
-    bool Invalid = false;
-    const SrcMgr::SLocEntry *E = &getSLocEntry(FID, &Invalid);
-    if (Invalid)
+    auto *E = getSLocEntryOrNull(FID);
+    if (!E)
       return std::make_pair(FileID(), 0);
 
     unsigned Offset = Loc.getOffset()-E->getOffset();
@@ -1749,6 +1722,19 @@ class SourceManager : public RefCountedBase<SourceManager> {
 
   const SrcMgr::SLocEntry &loadSLocEntry(unsigned Index, bool *Invalid) const;
 
+  const SrcMgr::SLocEntry *getSLocEntryOrNull(FileID FID) const {
+    bool Invalid = false;
+    const SrcMgr::SLocEntry &Entry = getSLocEntry(FID, &Invalid);
+    return Invalid ? nullptr : &Entry;
+  }
+
+  const SrcMgr::SLocEntry *getSLocEntryForFile(FileID FID) const {
+    if (auto *Entry = getSLocEntryOrNull(FID))
+      if (Entry->isFile())
+        return Entry;
+    return nullptr;
+  }
+
   /// Get the entry with the given unwrapped FileID.
   /// Invalid will not be modified for Local IDs.
   const SrcMgr::SLocEntry &getSLocEntryByID(int ID,

diff  --git a/clang/lib/Basic/SourceManager.cpp b/clang/lib/Basic/SourceManager.cpp
index db87a6e57741..a3e4e02cbb5f 100644
--- a/clang/lib/Basic/SourceManager.cpp
+++ b/clang/lib/Basic/SourceManager.cpp
@@ -727,15 +727,11 @@ void SourceManager::setFileIsTransient(const FileEntry *File) {
 }
 
 Optional<FileEntryRef> SourceManager::getFileEntryRefForID(FileID FID) const {
-  bool Invalid = false;
-  const SrcMgr::SLocEntry &Entry = getSLocEntry(FID, &Invalid);
-  if (Invalid || !Entry.isFile())
-    return None;
-
-  const SrcMgr::ContentCache *Content = Entry.getFile().getContentCache();
-  if (!Content || !Content->OrigEntry)
-    return None;
-  return FileEntryRef(Entry.getFile().getName(), *Content->OrigEntry);
+  if (const SrcMgr::SLocEntry *Entry = getSLocEntryForFile(FID))
+    if (auto *Content = Entry->getFile().getContentCache())
+      if (Content && Content->OrigEntry)
+        return FileEntryRef(Entry->getFile().getName(), *Content->OrigEntry);
+  return None;
 }
 
 StringRef SourceManager::getBufferData(FileID FID, bool *Invalid) const {
@@ -747,23 +743,16 @@ StringRef SourceManager::getBufferData(FileID FID, bool *Invalid) const {
 
 llvm::Optional<StringRef>
 SourceManager::getBufferDataIfLoaded(FileID FID) const {
-  bool MyInvalid = false;
-  const SLocEntry &SLoc = getSLocEntry(FID, &MyInvalid);
-  if (!SLoc.isFile() || MyInvalid)
-    return None;
-
-  return SLoc.getFile().getContentCache()->getBufferDataIfLoaded();
+  if (const SrcMgr::SLocEntry *Entry = getSLocEntryForFile(FID))
+    return Entry->getFile().getContentCache()->getBufferDataIfLoaded();
+  return None;
 }
 
 llvm::Optional<StringRef> SourceManager::getBufferDataOrNone(FileID FID) const {
-  bool MyInvalid = false;
-  const SLocEntry &SLoc = getSLocEntry(FID, &MyInvalid);
-  if (!SLoc.isFile() || MyInvalid)
-    return None;
-
-  if (auto B = SLoc.getFile().getContentCache()->getBufferOrNone(
-          Diag, getFileManager(), SourceLocation()))
-    return B->getBuffer();
+  if (const SrcMgr::SLocEntry *Entry = getSLocEntryForFile(FID))
+    if (auto B = Entry->getFile().getContentCache()->getBufferOrNone(
+            Diag, getFileManager(), SourceLocation()))
+      return B->getBuffer();
   return None;
 }
 
@@ -1443,12 +1432,11 @@ SrcMgr::CharacteristicKind
 SourceManager::getFileCharacteristic(SourceLocation Loc) const {
   assert(Loc.isValid() && "Can't get file characteristic of invalid loc!");
   std::pair<FileID, unsigned> LocInfo = getDecomposedExpansionLoc(Loc);
-  bool Invalid = false;
-  const SLocEntry &SEntry = getSLocEntry(LocInfo.first, &Invalid);
-  if (Invalid || !SEntry.isFile())
+  const SLocEntry *SEntry = getSLocEntryForFile(LocInfo.first);
+  if (!SEntry)
     return C_User;
 
-  const SrcMgr::FileInfo &FI = SEntry.getFile();
+  const SrcMgr::FileInfo &FI = SEntry->getFile();
 
   // If there are no #line directives in this file, just return the whole-file
   // state.
@@ -1569,12 +1557,11 @@ bool SourceManager::isInMainFile(SourceLocation Loc) const {
   // Presumed locations are always for expansion points.
   std::pair<FileID, unsigned> LocInfo = getDecomposedExpansionLoc(Loc);
 
-  bool Invalid = false;
-  const SLocEntry &Entry = getSLocEntry(LocInfo.first, &Invalid);
-  if (Invalid || !Entry.isFile())
+  const SLocEntry *Entry = getSLocEntryForFile(LocInfo.first);
+  if (!Entry)
     return false;
 
-  const SrcMgr::FileInfo &FI = Entry.getFile();
+  const SrcMgr::FileInfo &FI = Entry->getFile();
 
   // Check if there is a line directive for this location.
   if (FI.hasLineDirectives())


        


More information about the cfe-commits mailing list