From aab50af8c18ab2eb2149bb516c8a0993ffc5abb7 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Duncan=20P=2E=20N=2E=20Exon=C2=A0Smith?= Date: Mon, 19 Oct 2020 18:30:49 -0400 Subject: [PATCH] SourceManager: Use the same fake SLocEntry whenever it fails to load Instead of putting a fake `SLocEntry` at `LoadedSLocEntryTable[Index]` when it fails to load in `SourceManager::loadSLocEntry`, allocate a fake one. Unless someone is sniffing the address of the returned `SLocEntry` (doubtful), this won't be a functionality change. Note that `SLocEntryLoaded[Index]` wasn't being set to `true` either before or after this change so no accessor is every going to look at `LoadedSLocEntryTable[Index]`. As a side effect, drop the `mutable` from `LoadedSLocEntryTable`. Differential Revision: https://reviews.llvm.org/D89748 --- clang/include/clang/Basic/SourceManager.h | 4 +++- clang/lib/Basic/SourceManager.cpp | 8 +++++--- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/clang/include/clang/Basic/SourceManager.h b/clang/include/clang/Basic/SourceManager.h index ec9b2b7d0cfd..765cec465f9d 100644 --- a/clang/include/clang/Basic/SourceManager.h +++ b/clang/include/clang/Basic/SourceManager.h @@ -693,7 +693,7 @@ class SourceManager : public RefCountedBase { /// /// Negative FileIDs are indexes into this table. To get from ID to an index, /// use (-ID - 2). - mutable SmallVector LoadedSLocEntryTable; + SmallVector LoadedSLocEntryTable; /// The starting offset of the next local SLocEntry. /// @@ -775,6 +775,8 @@ class SourceManager : public RefCountedBase { mutable std::unique_ptr FakeContentCacheForRecovery; + mutable std::unique_ptr FakeSLocEntryForRecovery; + /// Lazily computed map of macro argument chunks to their expanded /// source location. using MacroArgsMap = std::map; diff --git a/clang/lib/Basic/SourceManager.cpp b/clang/lib/Basic/SourceManager.cpp index 6eae0f06a122..88f95d18ddf1 100644 --- a/clang/lib/Basic/SourceManager.cpp +++ b/clang/lib/Basic/SourceManager.cpp @@ -434,9 +434,11 @@ const SrcMgr::SLocEntry &SourceManager::loadSLocEntry(unsigned Index, // If the file of the SLocEntry changed we could still have loaded it. if (!SLocEntryLoaded[Index]) { // Try to recover; create a SLocEntry so the rest of clang can handle it. - LoadedSLocEntryTable[Index] = SLocEntry::get( - 0, FileInfo::get(SourceLocation(), getFakeContentCacheForRecovery(), - SrcMgr::C_User, "")); + if (!FakeSLocEntryForRecovery) + FakeSLocEntryForRecovery = std::make_unique(SLocEntry::get( + 0, FileInfo::get(SourceLocation(), getFakeContentCacheForRecovery(), + SrcMgr::C_User, ""))); + return *FakeSLocEntryForRecovery; } } -- 2.34.1