[clang] SourceManager: fix at SourceManager::getFileIDLoaded for the case of invalid...
authorIvan Murashko <ivanmurashko@fb.com>
Wed, 10 Aug 2022 15:36:19 +0000 (16:36 +0100)
committerIvan Murashko <ivan.murashko@gmail.com>
Wed, 10 Aug 2022 15:37:42 +0000 (16:37 +0100)
commite78c80e3d622d455934102dabd765a60ac917739
tree7ac7514a37321481e048a910d02efba71c2f19f6
parentb1009bbd9e3d5acd85e92cb7f9a1109d2e72967c
[clang] SourceManager: fix at SourceManager::getFileIDLoaded for the case of invalid SLockEntry

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](https://reviews.llvm.org/D89748).
- The result entry is not tested at `SourceManager::isOffsetInFileID`and 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
```

Reviewed By: aaron.ballman

Differential Revision: https://reviews.llvm.org/D130847
clang/lib/Basic/SourceManager.cpp