There is a fix for the search procedure at SourceManager::getFileIDLoaded. It might return an invalid (not loaded) entry. That might cause clang/clangd crashes. At my case the scenario was the following:
- SourceManager::getFileIDLoaded returned an invalid file id for a non loaded entry and incorrectly set SourceManager::LastFileIDLookup to the value
- getSLocEntry returned SourceManager::FakeSLocEntryForRecovery introduced at D89748.
- The result entry is not tested at SourceManager::isOffsetInFileIDand as result the call return SLocOffset < getSLocEntryByID(FID.ID+1).getOffset(); returned true value because FID.ID+1 pointed to a non-fake entry
- The tested offset was marked as one that belonged to the fake SLockEntry
Such behaviour 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
This looks incorrect to me -- I don't think an offset of 0 is invalid. I think a better way to do this is: