[PATCH] D130847: [clang] SourceManager: fix isOffsetInFileID for the case of a fake SLocEntry

Ivan Murashko via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Sun Jul 31 08:48:36 PDT 2022


ivanmurashko created this revision.
ivanmurashko added reviewers: dexonsmith, aaron.ballman.
ivanmurashko added a project: clang.
Herald added subscribers: usaxena95, kadircet.
Herald added a project: All.
ivanmurashko requested review of this revision.
Herald added subscribers: cfe-commits, ilya-biryukov.

The `getSLocEntry` might return `SourceManager::FakeSLocEntryForRecovery` introduced at D89748 <https://reviews.llvm.org/D89748>. The result entry is not tested and as result the call

  return SLocOffset < getSLocEntryByID(FID.ID+1).getOffset();

might return `true` value. That would mark the tested offset as one that belongs to the fake `SLockEntry`.

It might cause a weird clangd crash when preamble contains some header files that were removed just after the preamble created. Unfortunately it's not so easy to reproduce the crash in the form of a LIT test thus I provided the fix only.

Test Plan:

  ninja check-clang


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D130847

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


Index: clang/include/clang/Basic/SourceManager.h
===================================================================
--- clang/include/clang/Basic/SourceManager.h
+++ clang/include/clang/Basic/SourceManager.h
@@ -1825,7 +1825,13 @@
   /// specified SourceLocation offset.  This is a very hot method.
   inline bool isOffsetInFileID(FileID FID,
                                SourceLocation::UIntTy SLocOffset) const {
-    const SrcMgr::SLocEntry &Entry = getSLocEntry(FID);
+    bool Invalid = false;
+    const SrcMgr::SLocEntry &Entry = getSLocEntry(FID, &Invalid);
+
+    // If the entry is invalid, it can't contain it.
+    if (Invalid)
+      return false;
+
     // If the entry is after the offset, it can't contain it.
     if (SLocOffset < Entry.getOffset()) return false;
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D130847.448866.patch
Type: text/x-patch
Size: 787 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20220731/dd6e5278/attachment.bin>


More information about the llvm-commits mailing list