[clang] 9ae6e6a - [clang] NFC: Extract lower-level SourceManager functions

Jan Svoboda via cfe-commits cfe-commits at lists.llvm.org
Tue Nov 1 19:18:49 PDT 2022


Author: Jan Svoboda
Date: 2022-11-01T19:11:24-07:00
New Revision: 9ae6e6a50273abd683f61b017a4d4c34a964c703

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

LOG: [clang] NFC: Extract lower-level SourceManager functions

This is a prep-patch for D136624 which allows querying `SourceManager` with raw offsets instead of `SourceLocation`s.

Reviewed By: dexonsmith

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

Added: 
    

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

Removed: 
    


################################################################################
diff  --git a/clang/include/clang/Basic/SourceManager.h b/clang/include/clang/Basic/SourceManager.h
index 1e0743ecd8b6c..d5d5af461fc31 100644
--- a/clang/include/clang/Basic/SourceManager.h
+++ b/clang/include/clang/Basic/SourceManager.h
@@ -1114,13 +1114,7 @@ class SourceManager : public RefCountedBase<SourceManager> {
   /// the entry in SLocEntryTable which contains the specified location.
   ///
   FileID getFileID(SourceLocation SpellingLoc) const {
-    SourceLocation::UIntTy SLocOffset = SpellingLoc.getOffset();
-
-    // If our one-entry cache covers this offset, just return it.
-    if (isOffsetInFileID(LastFileIDLookup, SLocOffset))
-      return LastFileIDLookup;
-
-    return getFileIDSlow(SLocOffset);
+    return getFileID(SpellingLoc.getOffset());
   }
 
   /// Return the filename of the file containing a SourceLocation.
@@ -1747,12 +1741,12 @@ class SourceManager : public RefCountedBase<SourceManager> {
 
   /// Returns true if \p Loc came from a PCH/Module.
   bool isLoadedSourceLocation(SourceLocation Loc) const {
-    return Loc.getOffset() >= CurrentLoadedOffset;
+    return isLoadedOffset(Loc.getOffset());
   }
 
   /// Returns true if \p Loc did not come from a PCH/Module.
   bool isLocalSourceLocation(SourceLocation Loc) const {
-    return Loc.getOffset() < NextLocalOffset;
+    return isLocalOffset(Loc.getOffset());
   }
 
   /// Returns true if \p FID came from a PCH/Module.
@@ -1822,6 +1816,22 @@ class SourceManager : public RefCountedBase<SourceManager> {
     return getLoadedSLocEntry(static_cast<unsigned>(-ID - 2), Invalid);
   }
 
+  FileID getFileID(SourceLocation::UIntTy SLocOffset) const {
+    // If our one-entry cache covers this offset, just return it.
+    if (isOffsetInFileID(LastFileIDLookup, SLocOffset))
+      return LastFileIDLookup;
+
+    return getFileIDSlow(SLocOffset);
+  }
+
+  bool isLocalOffset(SourceLocation::UIntTy SLocOffset) const {
+    return SLocOffset < CurrentLoadedOffset;
+  }
+
+  bool isLoadedOffset(SourceLocation::UIntTy SLocOffset) const {
+    return SLocOffset >= CurrentLoadedOffset;
+  }
+
   /// Implements the common elements of storing an expansion info struct into
   /// the SLocEntry table and producing a source location that refers to it.
   SourceLocation


        


More information about the cfe-commits mailing list