[clang] 74a8783 - SourceManager: Clarify that FileInfo always has a ContentCache, NFC
Duncan P. N. Exon Smith via cfe-commits
cfe-commits at lists.llvm.org
Fri Oct 23 09:39:02 PDT 2020
Author: Duncan P. N. Exon Smith
Date: 2020-10-23T12:38:53-04:00
New Revision: 74a8783480219f5f0e5c4673a6d0e29b4ad99877
URL: https://github.com/llvm/llvm-project/commit/74a8783480219f5f0e5c4673a6d0e29b4ad99877
DIFF: https://github.com/llvm/llvm-project/commit/74a8783480219f5f0e5c4673a6d0e29b4ad99877.diff
LOG: SourceManager: Clarify that FileInfo always has a ContentCache, NFC
It turns out that `FileInfo` *always* has a ContentCache. Clarify that
in the code:
- Update the private version of `SourceManager::createFileID` to take a
`ContentCache&` instead of `ContentCache*`, and rename it to
`createFileIDImpl` for clarity.
- Change `FileInfo::getContentCache` to return a reference.
Differential Revision: https://reviews.llvm.org/D89554
Added:
Modified:
clang/include/clang/Basic/SourceManager.h
clang/lib/AST/ASTImporter.cpp
clang/lib/Basic/SourceManager.cpp
clang/lib/Lex/ScratchBuffer.cpp
clang/lib/Rewrite/Rewriter.cpp
clang/lib/Serialization/ASTWriter.cpp
clang/tools/libclang/CIndexInclusionStack.cpp
Removed:
################################################################################
diff --git a/clang/include/clang/Basic/SourceManager.h b/clang/include/clang/Basic/SourceManager.h
index 5035326297f7..06bec09cda98 100644
--- a/clang/include/clang/Basic/SourceManager.h
+++ b/clang/include/clang/Basic/SourceManager.h
@@ -275,13 +275,13 @@ namespace SrcMgr {
public:
/// Return a FileInfo object.
- static FileInfo get(SourceLocation IL, const ContentCache *Con,
+ static FileInfo get(SourceLocation IL, const ContentCache &Con,
CharacteristicKind FileCharacter, StringRef Filename) {
FileInfo X;
X.IncludeLoc = IL.getRawEncoding();
X.NumCreatedFIDs = 0;
X.HasLineDirectives = false;
- X.ContentAndKind.setPointer(Con);
+ X.ContentAndKind.setPointer(&Con);
X.ContentAndKind.setInt(FileCharacter);
X.Filename = Filename;
return X;
@@ -291,8 +291,8 @@ namespace SrcMgr {
return SourceLocation::getFromRawEncoding(IncludeLoc);
}
- const ContentCache *getContentCache() const {
- return ContentAndKind.getPointer();
+ const ContentCache &getContentCache() const {
+ return *ContentAndKind.getPointer();
}
/// Return whether this is a system header or not.
@@ -973,7 +973,7 @@ class SourceManager : public RefCountedBase<SourceManager> {
llvm::Optional<llvm::MemoryBufferRef>
getBufferOrNone(FileID FID, SourceLocation Loc = SourceLocation()) const {
if (auto *Entry = getSLocEntryForFile(FID))
- return Entry->getFile().getContentCache()->getBufferOrNone(
+ return Entry->getFile().getContentCache().getBufferOrNone(
Diag, getFileManager(), Loc);
return None;
}
@@ -992,8 +992,7 @@ class SourceManager : public RefCountedBase<SourceManager> {
/// Returns the FileEntry record for the provided FileID.
const FileEntry *getFileEntryForID(FileID FID) const {
if (auto *Entry = getSLocEntryForFile(FID))
- if (auto *Content = Entry->getFile().getContentCache())
- return Content->OrigEntry;
+ return Entry->getFile().getContentCache().OrigEntry;
return nullptr;
}
@@ -1006,10 +1005,7 @@ class SourceManager : public RefCountedBase<SourceManager> {
/// Returns the FileEntry record for the provided SLocEntry.
const FileEntry *getFileEntryForSLocEntry(const SrcMgr::SLocEntry &sloc) const
{
- const SrcMgr::ContentCache *Content = sloc.getFile().getContentCache();
- if (!Content)
- return nullptr;
- return Content->OrigEntry;
+ return sloc.getFile().getContentCache().OrigEntry;
}
/// Return a StringRef to the source buffer data for the
@@ -1793,10 +1789,10 @@ class SourceManager : public RefCountedBase<SourceManager> {
///
/// This works regardless of whether the ContentCache corresponds to a
/// file or some other input source.
- FileID createFileID(const SrcMgr::ContentCache *File, StringRef Filename,
- SourceLocation IncludePos,
- SrcMgr::CharacteristicKind DirCharacter, int LoadedID,
- unsigned LoadedOffset);
+ FileID createFileIDImpl(const SrcMgr::ContentCache &File, StringRef Filename,
+ SourceLocation IncludePos,
+ SrcMgr::CharacteristicKind DirCharacter, int LoadedID,
+ unsigned LoadedOffset);
const SrcMgr::ContentCache *
getOrCreateContentCache(const FileEntry *SourceFile,
diff --git a/clang/lib/AST/ASTImporter.cpp b/clang/lib/AST/ASTImporter.cpp
index ce79b99d7043..f15be09f0e5d 100644
--- a/clang/lib/AST/ASTImporter.cpp
+++ b/clang/lib/AST/ASTImporter.cpp
@@ -8654,7 +8654,7 @@ Expected<FileID> ASTImporter::Import(FileID FromID, bool IsBuiltin) {
}
ToID = ToSM.getFileID(MLoc);
} else {
- const SrcMgr::ContentCache *Cache = FromSLoc.getFile().getContentCache();
+ const SrcMgr::ContentCache *Cache = &FromSLoc.getFile().getContentCache();
if (!IsBuiltin && !Cache->BufferOverridden) {
// Include location of this file.
diff --git a/clang/lib/Basic/SourceManager.cpp b/clang/lib/Basic/SourceManager.cpp
index e3b88f9127d5..d2015b55c03f 100644
--- a/clang/lib/Basic/SourceManager.cpp
+++ b/clang/lib/Basic/SourceManager.cpp
@@ -437,7 +437,7 @@ const SrcMgr::SLocEntry &SourceManager::loadSLocEntry(unsigned Index,
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(),
+ 0, FileInfo::get(SourceLocation(), *getFakeContentCacheForRecovery(),
SrcMgr::C_User, ""));
}
}
@@ -534,7 +534,7 @@ FileID SourceManager::createFileID(const FileEntry *SourceFile,
const SrcMgr::ContentCache *IR =
getOrCreateContentCache(SourceFile, isSystem(FileCharacter));
assert(IR && "getOrCreateContentCache() cannot return NULL");
- return createFileID(IR, SourceFile->getName(), IncludePos, FileCharacter,
+ return createFileIDImpl(*IR, SourceFile->getName(), IncludePos, FileCharacter,
LoadedID, LoadedOffset);
}
@@ -545,8 +545,8 @@ FileID SourceManager::createFileID(FileEntryRef SourceFile,
const SrcMgr::ContentCache *IR = getOrCreateContentCache(
&SourceFile.getFileEntry(), isSystem(FileCharacter));
assert(IR && "getOrCreateContentCache() cannot return NULL");
- return createFileID(IR, SourceFile.getName(), IncludePos, FileCharacter,
- LoadedID, LoadedOffset);
+ return createFileIDImpl(*IR, SourceFile.getName(), IncludePos, FileCharacter,
+ LoadedID, LoadedOffset);
}
/// Create a new FileID that represents the specified memory buffer.
@@ -558,8 +558,8 @@ FileID SourceManager::createFileID(std::unique_ptr<llvm::MemoryBuffer> Buffer,
int LoadedID, unsigned LoadedOffset,
SourceLocation IncludeLoc) {
StringRef Name = Buffer->getBufferIdentifier();
- return createFileID(createMemBufferContentCache(std::move(Buffer)), Name,
- IncludeLoc, FileCharacter, LoadedID, LoadedOffset);
+ return createFileIDImpl(*createMemBufferContentCache(std::move(Buffer)), Name,
+ IncludeLoc, FileCharacter, LoadedID, LoadedOffset);
}
/// Create a new FileID that represents the specified memory buffer.
@@ -587,10 +587,11 @@ SourceManager::getOrCreateFileID(const FileEntry *SourceFile,
/// createFileID - Create a new FileID for the specified ContentCache and
/// include position. This works regardless of whether the ContentCache
/// corresponds to a file or some other input source.
-FileID SourceManager::createFileID(const ContentCache *File, StringRef Filename,
- SourceLocation IncludePos,
- SrcMgr::CharacteristicKind FileCharacter,
- int LoadedID, unsigned LoadedOffset) {
+FileID SourceManager::createFileIDImpl(const ContentCache &File,
+ StringRef Filename,
+ SourceLocation IncludePos,
+ SrcMgr::CharacteristicKind FileCharacter,
+ int LoadedID, unsigned LoadedOffset) {
if (LoadedID < 0) {
assert(LoadedID != -1 && "Loading sentinel FileID");
unsigned Index = unsigned(-LoadedID) - 2;
@@ -601,7 +602,7 @@ FileID SourceManager::createFileID(const ContentCache *File, StringRef Filename,
SLocEntryLoaded[Index] = true;
return FileID::get(LoadedID);
}
- unsigned FileSize = File->getSize();
+ unsigned FileSize = File.getSize();
if (!(NextLocalOffset + FileSize + 1 > NextLocalOffset &&
NextLocalOffset + FileSize + 1 <= CurrentLoadedOffset)) {
Diag.Report(IncludePos, diag::err_include_too_large);
@@ -728,9 +729,8 @@ void SourceManager::setFileIsTransient(const FileEntry *File) {
Optional<StringRef>
SourceManager::getNonBuiltinFilenameForID(FileID FID) const {
if (const SrcMgr::SLocEntry *Entry = getSLocEntryForFile(FID))
- if (auto *Content = Entry->getFile().getContentCache())
- if (Content->OrigEntry)
- return Entry->getFile().getName();
+ if (Entry->getFile().getContentCache().OrigEntry)
+ return Entry->getFile().getName();
return None;
}
@@ -744,13 +744,13 @@ StringRef SourceManager::getBufferData(FileID FID, bool *Invalid) const {
llvm::Optional<StringRef>
SourceManager::getBufferDataIfLoaded(FileID FID) const {
if (const SrcMgr::SLocEntry *Entry = getSLocEntryForFile(FID))
- return Entry->getFile().getContentCache()->getBufferDataIfLoaded();
+ return Entry->getFile().getContentCache().getBufferDataIfLoaded();
return None;
}
llvm::Optional<StringRef> SourceManager::getBufferDataOrNone(FileID FID) const {
if (const SrcMgr::SLocEntry *Entry = getSLocEntryForFile(FID))
- if (auto B = Entry->getFile().getContentCache()->getBufferOrNone(
+ if (auto B = Entry->getFile().getContentCache().getBufferOrNone(
Diag, getFileManager(), SourceLocation()))
return B->getBuffer();
return None;
@@ -1171,8 +1171,8 @@ const char *SourceManager::getCharacterData(SourceLocation SL,
return "<<<<INVALID BUFFER>>>>";
}
llvm::Optional<llvm::MemoryBufferRef> Buffer =
- Entry.getFile().getContentCache()->getBufferOrNone(Diag, getFileManager(),
- SourceLocation());
+ Entry.getFile().getContentCache().getBufferOrNone(Diag, getFileManager(),
+ SourceLocation());
if (Invalid)
*Invalid = !Buffer;
return Buffer ? Buffer->getBufferStart() + LocInfo.second
@@ -1327,7 +1327,7 @@ unsigned SourceManager::getLineNumber(FileID FID, unsigned FilePos,
return 1;
}
- Content = const_cast<ContentCache*>(Entry.getFile().getContentCache());
+ Content = const_cast<ContentCache *>(&Entry.getFile().getContentCache());
}
// If this is the first use of line information for this buffer, compute the
@@ -1488,7 +1488,7 @@ PresumedLoc SourceManager::getPresumedLoc(SourceLocation Loc,
return PresumedLoc();
const SrcMgr::FileInfo &FI = Entry.getFile();
- const SrcMgr::ContentCache *C = FI.getContentCache();
+ const SrcMgr::ContentCache *C = &FI.getContentCache();
// To get the source name, first consult the FileEntry (if one exists)
// before the MemBuffer as this will avoid unnecessarily paging in the
@@ -1626,9 +1626,7 @@ FileID SourceManager::translateFile(const FileEntry *SourceFile) const {
return FileID();
if (MainSLoc.isFile()) {
- const ContentCache *MainContentCache =
- MainSLoc.getFile().getContentCache();
- if (MainContentCache && MainContentCache->OrigEntry == SourceFile)
+ if (MainSLoc.getFile().getContentCache().OrigEntry == SourceFile)
return MainFileID;
}
}
@@ -1637,16 +1635,16 @@ FileID SourceManager::translateFile(const FileEntry *SourceFile) const {
// through all of the local source locations.
for (unsigned I = 0, N = local_sloc_entry_size(); I != N; ++I) {
const SLocEntry &SLoc = getLocalSLocEntry(I);
- if (SLoc.isFile() && SLoc.getFile().getContentCache() &&
- SLoc.getFile().getContentCache()->OrigEntry == SourceFile)
+ if (SLoc.isFile() &&
+ SLoc.getFile().getContentCache().OrigEntry == SourceFile)
return FileID::get(I);
}
// If that still didn't help, try the modules.
for (unsigned I = 0, N = loaded_sloc_entry_size(); I != N; ++I) {
const SLocEntry &SLoc = getLoadedSLocEntry(I);
- if (SLoc.isFile() && SLoc.getFile().getContentCache() &&
- SLoc.getFile().getContentCache()->OrigEntry == SourceFile)
+ if (SLoc.isFile() &&
+ SLoc.getFile().getContentCache().OrigEntry == SourceFile)
return FileID::get(-int(I) - 2);
}
@@ -1678,10 +1676,8 @@ SourceLocation SourceManager::translateLineCol(FileID FID,
if (Line == 1 && Col == 1)
return FileLoc;
- ContentCache *Content
- = const_cast<ContentCache *>(Entry.getFile().getContentCache());
- if (!Content)
- return SourceLocation();
+ ContentCache *Content =
+ const_cast<ContentCache *>(&Entry.getFile().getContentCache());
// If this is the first use of line information for this buffer, compute the
// SourceLineCache for it on demand.
@@ -2139,16 +2135,15 @@ LLVM_DUMP_METHOD void SourceManager::dump() const {
<< ">\n";
if (FI.getIncludeLoc().isValid())
out << " included from " << FI.getIncludeLoc().getOffset() << "\n";
- if (auto *CC = FI.getContentCache()) {
- out << " for " << (CC->OrigEntry ? CC->OrigEntry->getName() : "<none>")
+ auto &CC = FI.getContentCache();
+ out << " for " << (CC.OrigEntry ? CC.OrigEntry->getName() : "<none>")
+ << "\n";
+ if (CC.BufferOverridden)
+ out << " contents overridden\n";
+ if (CC.ContentsEntry != CC.OrigEntry) {
+ out << " contents from "
+ << (CC.ContentsEntry ? CC.ContentsEntry->getName() : "<none>")
<< "\n";
- if (CC->BufferOverridden)
- out << " contents overridden\n";
- if (CC->ContentsEntry != CC->OrigEntry) {
- out << " contents from "
- << (CC->ContentsEntry ? CC->ContentsEntry->getName() : "<none>")
- << "\n";
- }
}
} else {
auto &EI = Entry.getExpansion();
diff --git a/clang/lib/Lex/ScratchBuffer.cpp b/clang/lib/Lex/ScratchBuffer.cpp
index 19ab93ec54b4..174b4b362ada 100644
--- a/clang/lib/Lex/ScratchBuffer.cpp
+++ b/clang/lib/Lex/ScratchBuffer.cpp
@@ -38,8 +38,9 @@ SourceLocation ScratchBuffer::getToken(const char *Buf, unsigned Len,
// Clear out the source line cache if it's already been computed.
// FIXME: Allow this to be incrementally extended.
auto *ContentCache = const_cast<SrcMgr::ContentCache *>(
- SourceMgr.getSLocEntry(SourceMgr.getFileID(BufferStartLoc))
- .getFile().getContentCache());
+ &SourceMgr.getSLocEntry(SourceMgr.getFileID(BufferStartLoc))
+ .getFile()
+ .getContentCache());
ContentCache->SourceLineCache = nullptr;
}
diff --git a/clang/lib/Rewrite/Rewriter.cpp b/clang/lib/Rewrite/Rewriter.cpp
index 33718b7721ce..040e1c284253 100644
--- a/clang/lib/Rewrite/Rewriter.cpp
+++ b/clang/lib/Rewrite/Rewriter.cpp
@@ -263,8 +263,8 @@ bool Rewriter::InsertText(SourceLocation Loc, StringRef Str,
StringRef MB = SourceMgr->getBufferData(FID);
unsigned lineNo = SourceMgr->getLineNumber(FID, StartOffs) - 1;
- const SrcMgr::ContentCache *
- Content = SourceMgr->getSLocEntry(FID).getFile().getContentCache();
+ const SrcMgr::ContentCache *Content =
+ &SourceMgr->getSLocEntry(FID).getFile().getContentCache();
unsigned lineOffs = Content->SourceLineCache[lineNo];
// Find the whitespace at the start of the line.
@@ -367,8 +367,8 @@ bool Rewriter::IncreaseIndentation(CharSourceRange range,
unsigned startLineNo = SourceMgr->getLineNumber(FID, StartOff) - 1;
unsigned endLineNo = SourceMgr->getLineNumber(FID, EndOff) - 1;
- const SrcMgr::ContentCache *
- Content = SourceMgr->getSLocEntry(FID).getFile().getContentCache();
+ const SrcMgr::ContentCache *Content =
+ &SourceMgr->getSLocEntry(FID).getFile().getContentCache();
// Find where the lines start.
unsigned parentLineOffs = Content->SourceLineCache[parentLineNo];
diff --git a/clang/lib/Serialization/ASTWriter.cpp b/clang/lib/Serialization/ASTWriter.cpp
index 760278c20288..f0ce843bd3e4 100644
--- a/clang/lib/Serialization/ASTWriter.cpp
+++ b/clang/lib/Serialization/ASTWriter.cpp
@@ -1452,7 +1452,7 @@ void ASTWriter::WriteInputFiles(SourceManager &SourceMgr,
if (!SLoc->isFile())
continue;
const SrcMgr::FileInfo &File = SLoc->getFile();
- const SrcMgr::ContentCache *Cache = File.getContentCache();
+ const SrcMgr::ContentCache *Cache = &File.getContentCache();
if (!Cache->OrigEntry)
continue;
@@ -1952,7 +1952,7 @@ void ASTWriter::WriteSourceManagerBlock(SourceManager &SourceMgr,
// Figure out which record code to use.
unsigned Code;
if (SLoc->isFile()) {
- const SrcMgr::ContentCache *Cache = SLoc->getFile().getContentCache();
+ const SrcMgr::ContentCache *Cache = &SLoc->getFile().getContentCache();
if (Cache->OrigEntry) {
Code = SM_SLOC_FILE_ENTRY;
} else
@@ -1970,7 +1970,7 @@ void ASTWriter::WriteSourceManagerBlock(SourceManager &SourceMgr,
Record.push_back(File.getFileCharacteristic()); // FIXME: stable encoding
Record.push_back(File.hasLineDirectives());
- const SrcMgr::ContentCache *Content = File.getContentCache();
+ const SrcMgr::ContentCache *Content = &File.getContentCache();
bool EmitBlob = false;
if (Content->OrigEntry) {
assert(Content->OrigEntry == Content->ContentsEntry &&
diff --git a/clang/tools/libclang/CIndexInclusionStack.cpp b/clang/tools/libclang/CIndexInclusionStack.cpp
index 7572512ae576..3e05cff12c48 100644
--- a/clang/tools/libclang/CIndexInclusionStack.cpp
+++ b/clang/tools/libclang/CIndexInclusionStack.cpp
@@ -35,7 +35,7 @@ void getInclusions(bool IsLocal, unsigned n, CXTranslationUnit TU,
continue;
const SrcMgr::FileInfo &FI = SL.getFile();
- if (!FI.getContentCache()->OrigEntry)
+ if (!FI.getContentCache().OrigEntry)
continue;
// If this is the main file, and there is a preamble, skip this SLoc. The
@@ -60,7 +60,7 @@ void getInclusions(bool IsLocal, unsigned n, CXTranslationUnit TU,
// Callback to the client.
// FIXME: We should have a function to construct CXFiles.
CB(static_cast<CXFile>(
- const_cast<FileEntry *>(FI.getContentCache()->OrigEntry)),
+ const_cast<FileEntry *>(FI.getContentCache().OrigEntry)),
InclusionStack.data(), InclusionStack.size(), clientData);
}
}
More information about the cfe-commits
mailing list