https://github.com/Endilll created https://github.com/llvm/llvm-project/pull/92436
This patch adds several const-qualified variants of existing member functions to `SourceManager`. I started with removing const qualification from `setNumCreatedFIDsForFileID`, and removing `const_cast` in the body of this function, as I think it doesn't make sense to const-qualify setters. >From d794398b304e1f20dcd61e45ae034195563494ee Mon Sep 17 00:00:00 2001 From: Vlad Serebrennikov <serebrennikov.vladis...@gmail.com> Date: Thu, 16 May 2024 21:04:39 +0300 Subject: [PATCH] [clang][NFC] Improve const-correctness in `SourceManager` --- clang/include/clang/Basic/SourceManager.h | 44 +++++++++++++++++++++-- clang/lib/Basic/SourceManager.cpp | 4 +++ 2 files changed, 45 insertions(+), 3 deletions(-) diff --git a/clang/include/clang/Basic/SourceManager.h b/clang/include/clang/Basic/SourceManager.h index 5258bab584f49..ce33423551039 100644 --- a/clang/include/clang/Basic/SourceManager.h +++ b/clang/include/clang/Basic/SourceManager.h @@ -497,6 +497,10 @@ class SLocEntry { bool isFile() const { return !isExpansion(); } const FileInfo &getFile() const { + return const_cast<SLocEntry *>(this)->getFile(); + } + + FileInfo &getFile() { assert(isFile() && "Not a file SLocEntry!"); return File; } @@ -1120,12 +1124,12 @@ class SourceManager : public RefCountedBase<SourceManager> { /// Set the number of FileIDs (files and macros) that were created /// during preprocessing of \p FID, including it. void setNumCreatedFIDsForFileID(FileID FID, unsigned NumFIDs, - bool Force = false) const { + bool Force = false) { auto *Entry = getSLocEntryForFile(FID); if (!Entry) return; assert((Force || Entry->getFile().NumCreatedFIDs == 0) && "Already set!"); - const_cast<SrcMgr::FileInfo &>(Entry->getFile()).NumCreatedFIDs = NumFIDs; + Entry->getFile().NumCreatedFIDs = NumFIDs; } //===--------------------------------------------------------------------===// @@ -1730,6 +1734,11 @@ class SourceManager : public RefCountedBase<SourceManager> { /// Get a local SLocEntry. This is exposed for indexing. const SrcMgr::SLocEntry &getLocalSLocEntry(unsigned Index) const { + return const_cast<SourceManager *>(this)->getLocalSLocEntry(Index); + } + + /// Get a local SLocEntry. This is exposed for indexing. + SrcMgr::SLocEntry &getLocalSLocEntry(unsigned Index) { assert(Index < LocalSLocEntryTable.size() && "Invalid index"); return LocalSLocEntryTable[Index]; } @@ -1740,6 +1749,13 @@ class SourceManager : public RefCountedBase<SourceManager> { /// Get a loaded SLocEntry. This is exposed for indexing. const SrcMgr::SLocEntry &getLoadedSLocEntry(unsigned Index, bool *Invalid = nullptr) const { + return const_cast<SourceManager *>(this)->getLoadedSLocEntry(Index, + Invalid); + } + + /// Get a loaded SLocEntry. This is exposed for indexing. + SrcMgr::SLocEntry &getLoadedSLocEntry(unsigned Index, + bool *Invalid = nullptr) { assert(Index < LoadedSLocEntryTable.size() && "Invalid index"); if (SLocEntryLoaded[Index]) return LoadedSLocEntryTable[Index]; @@ -1748,6 +1764,10 @@ class SourceManager : public RefCountedBase<SourceManager> { const SrcMgr::SLocEntry &getSLocEntry(FileID FID, bool *Invalid = nullptr) const { + return const_cast<SourceManager *>(this)->getSLocEntry(FID, Invalid); + } + + SrcMgr::SLocEntry &getSLocEntry(FileID FID, bool *Invalid = nullptr) { if (FID.ID == 0 || FID.ID == -1) { if (Invalid) *Invalid = true; return LocalSLocEntryTable[0]; @@ -1821,14 +1841,23 @@ class SourceManager : public RefCountedBase<SourceManager> { SrcMgr::ContentCache &getFakeContentCacheForRecovery() const; const SrcMgr::SLocEntry &loadSLocEntry(unsigned Index, bool *Invalid) const; + SrcMgr::SLocEntry &loadSLocEntry(unsigned Index, bool *Invalid); const SrcMgr::SLocEntry *getSLocEntryOrNull(FileID FID) const { + return const_cast<SourceManager *>(this)->getSLocEntryOrNull(FID); + } + + SrcMgr::SLocEntry *getSLocEntryOrNull(FileID FID) { bool Invalid = false; - const SrcMgr::SLocEntry &Entry = getSLocEntry(FID, &Invalid); + SrcMgr::SLocEntry &Entry = getSLocEntry(FID, &Invalid); return Invalid ? nullptr : &Entry; } const SrcMgr::SLocEntry *getSLocEntryForFile(FileID FID) const { + return const_cast<SourceManager *>(this)->getSLocEntryForFile(FID); + } + + SrcMgr::SLocEntry *getSLocEntryForFile(FileID FID) { if (auto *Entry = getSLocEntryOrNull(FID)) if (Entry->isFile()) return Entry; @@ -1839,6 +1868,10 @@ class SourceManager : public RefCountedBase<SourceManager> { /// Invalid will not be modified for Local IDs. const SrcMgr::SLocEntry &getSLocEntryByID(int ID, bool *Invalid = nullptr) const { + return const_cast<SourceManager *>(this)->getSLocEntryByID(ID, Invalid); + } + + SrcMgr::SLocEntry &getSLocEntryByID(int ID, bool *Invalid = nullptr) { assert(ID != -1 && "Using FileID sentinel value"); if (ID < 0) return getLoadedSLocEntryByID(ID, Invalid); @@ -1847,6 +1880,11 @@ class SourceManager : public RefCountedBase<SourceManager> { const SrcMgr::SLocEntry & getLoadedSLocEntryByID(int ID, bool *Invalid = nullptr) const { + return const_cast<SourceManager *>(this)->getLoadedSLocEntryByID(ID, + Invalid); + } + + SrcMgr::SLocEntry &getLoadedSLocEntryByID(int ID, bool *Invalid = nullptr) { return getLoadedSLocEntry(static_cast<unsigned>(-ID - 2), Invalid); } diff --git a/clang/lib/Basic/SourceManager.cpp b/clang/lib/Basic/SourceManager.cpp index 37734d3b10e78..afdb5b0e92d64 100644 --- a/clang/lib/Basic/SourceManager.cpp +++ b/clang/lib/Basic/SourceManager.cpp @@ -431,6 +431,10 @@ ContentCache &SourceManager::createMemBufferContentCache( const SrcMgr::SLocEntry &SourceManager::loadSLocEntry(unsigned Index, bool *Invalid) const { + return const_cast<SourceManager *>(this)->loadSLocEntry(Index, Invalid); +} + +SrcMgr::SLocEntry &SourceManager::loadSLocEntry(unsigned Index, bool *Invalid) { assert(!SLocEntryLoaded[Index]); if (ExternalSLocEntries->ReadSLocEntry(-(static_cast<int>(Index) + 2))) { if (Invalid) _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits