[clang] [clang] SourceManager: Cache offsets for LastFileIDLookup to speed up getFileID (PR #146782)

Haojian Wu via cfe-commits cfe-commits at lists.llvm.org
Thu Jul 3 01:54:01 PDT 2025


================
@@ -849,7 +850,12 @@ FileID SourceManager::getFileIDLocal(SourceLocation::UIntTy SLocOffset) const {
       FileID Res = FileID::get(int(GreaterIndex));
       // Remember it.  We have good locality across FileID lookups.
       LastFileIDLookup = Res;
-      NumLinearScans += NumProbes+1;
+      LastLookupStartOffset = LocalSLocEntryTable[GreaterIndex].getOffset();
+      LastLookupEndOffset =
+          GreaterIndex + 1 >= LocalSLocEntryTable.size()
----------------
hokein wrote:

This case can happen — if the initial value of `GreaterIndex` is `LocalSLocEntryTable.size()`, then after `--GreaterIndex`, it points to the last valid element (size() - 1), which is fine. However, we will access `GreaterIndex + 1`, that would be out of bounds.

https://github.com/llvm/llvm-project/pull/146782


More information about the cfe-commits mailing list