aboutsummaryrefslogtreecommitdiff
path: root/clang/lib/Basic/SourceManager.cpp
diff options
context:
space:
mode:
authorDuncan P. N. Exon Smith <dexonsmith@apple.com>2020-10-16 10:21:50 -0400
committerDuncan P. N. Exon Smith <dexonsmith@apple.com>2020-10-23 12:38:53 -0400
commit74a8783480219f5f0e5c4673a6d0e29b4ad99877 (patch)
treee52696482dcb4a647f6310c5e42cc42b2a7c117d /clang/lib/Basic/SourceManager.cpp
parent7a74bb899abe1772a428ec98597d544a637e5551 (diff)
downloadllvm-74a8783480219f5f0e5c4673a6d0e29b4ad99877.zip
llvm-74a8783480219f5f0e5c4673a6d0e29b4ad99877.tar.gz
llvm-74a8783480219f5f0e5c4673a6d0e29b4ad99877.tar.bz2
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
Diffstat (limited to 'clang/lib/Basic/SourceManager.cpp')
-rw-r--r--clang/lib/Basic/SourceManager.cpp75
1 files changed, 35 insertions, 40 deletions
diff --git a/clang/lib/Basic/SourceManager.cpp b/clang/lib/Basic/SourceManager.cpp
index e3b88f9..d2015b5 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();