diff options
author | Duncan P. N. Exon Smith <dexonsmith@apple.com> | 2020-10-15 18:17:17 -0400 |
---|---|---|
committer | Duncan P. N. Exon Smith <dexonsmith@apple.com> | 2020-10-22 21:30:31 -0400 |
commit | b6c6daa95d3aa2206d5a42b46793226f181c3e44 (patch) | |
tree | 26ee736f7a23442e6a963619623692c8496f7e8f /clang/lib/Basic/SourceManager.cpp | |
parent | 3091ed099f2f6a3d16dbdae7d0406f54dfc3031f (diff) | |
download | llvm-b6c6daa95d3aa2206d5a42b46793226f181c3e44.zip llvm-b6c6daa95d3aa2206d5a42b46793226f181c3e44.tar.gz llvm-b6c6daa95d3aa2206d5a42b46793226f181c3e44.tar.bz2 |
SourceManager: Factor out helpers for common SLocEntry lookup pattern, NFC
Add helpers `getSLocEntryOrNull`, which handles the `Invalid` logic
around `getSLocEntry`, and `getSLocEntryForFile`, which also checks for
`SLocEntry::isFile`, and use them to reduce repeated code.
Differential Revision: https://reviews.llvm.org/D89503
Diffstat (limited to 'clang/lib/Basic/SourceManager.cpp')
-rw-r--r-- | clang/lib/Basic/SourceManager.cpp | 49 |
1 files changed, 18 insertions, 31 deletions
diff --git a/clang/lib/Basic/SourceManager.cpp b/clang/lib/Basic/SourceManager.cpp index db87a6e..a3e4e02 100644 --- a/clang/lib/Basic/SourceManager.cpp +++ b/clang/lib/Basic/SourceManager.cpp @@ -727,15 +727,11 @@ void SourceManager::setFileIsTransient(const FileEntry *File) { } Optional<FileEntryRef> SourceManager::getFileEntryRefForID(FileID FID) const { - bool Invalid = false; - const SrcMgr::SLocEntry &Entry = getSLocEntry(FID, &Invalid); - if (Invalid || !Entry.isFile()) - return None; - - const SrcMgr::ContentCache *Content = Entry.getFile().getContentCache(); - if (!Content || !Content->OrigEntry) - return None; - return FileEntryRef(Entry.getFile().getName(), *Content->OrigEntry); + if (const SrcMgr::SLocEntry *Entry = getSLocEntryForFile(FID)) + if (auto *Content = Entry->getFile().getContentCache()) + if (Content && Content->OrigEntry) + return FileEntryRef(Entry->getFile().getName(), *Content->OrigEntry); + return None; } StringRef SourceManager::getBufferData(FileID FID, bool *Invalid) const { @@ -747,23 +743,16 @@ StringRef SourceManager::getBufferData(FileID FID, bool *Invalid) const { llvm::Optional<StringRef> SourceManager::getBufferDataIfLoaded(FileID FID) const { - bool MyInvalid = false; - const SLocEntry &SLoc = getSLocEntry(FID, &MyInvalid); - if (!SLoc.isFile() || MyInvalid) - return None; - - return SLoc.getFile().getContentCache()->getBufferDataIfLoaded(); + if (const SrcMgr::SLocEntry *Entry = getSLocEntryForFile(FID)) + return Entry->getFile().getContentCache()->getBufferDataIfLoaded(); + return None; } llvm::Optional<StringRef> SourceManager::getBufferDataOrNone(FileID FID) const { - bool MyInvalid = false; - const SLocEntry &SLoc = getSLocEntry(FID, &MyInvalid); - if (!SLoc.isFile() || MyInvalid) - return None; - - if (auto B = SLoc.getFile().getContentCache()->getBufferOrNone( - Diag, getFileManager(), SourceLocation())) - return B->getBuffer(); + if (const SrcMgr::SLocEntry *Entry = getSLocEntryForFile(FID)) + if (auto B = Entry->getFile().getContentCache()->getBufferOrNone( + Diag, getFileManager(), SourceLocation())) + return B->getBuffer(); return None; } @@ -1443,12 +1432,11 @@ SrcMgr::CharacteristicKind SourceManager::getFileCharacteristic(SourceLocation Loc) const { assert(Loc.isValid() && "Can't get file characteristic of invalid loc!"); std::pair<FileID, unsigned> LocInfo = getDecomposedExpansionLoc(Loc); - bool Invalid = false; - const SLocEntry &SEntry = getSLocEntry(LocInfo.first, &Invalid); - if (Invalid || !SEntry.isFile()) + const SLocEntry *SEntry = getSLocEntryForFile(LocInfo.first); + if (!SEntry) return C_User; - const SrcMgr::FileInfo &FI = SEntry.getFile(); + const SrcMgr::FileInfo &FI = SEntry->getFile(); // If there are no #line directives in this file, just return the whole-file // state. @@ -1569,12 +1557,11 @@ bool SourceManager::isInMainFile(SourceLocation Loc) const { // Presumed locations are always for expansion points. std::pair<FileID, unsigned> LocInfo = getDecomposedExpansionLoc(Loc); - bool Invalid = false; - const SLocEntry &Entry = getSLocEntry(LocInfo.first, &Invalid); - if (Invalid || !Entry.isFile()) + const SLocEntry *Entry = getSLocEntryForFile(LocInfo.first); + if (!Entry) return false; - const SrcMgr::FileInfo &FI = Entry.getFile(); + const SrcMgr::FileInfo &FI = Entry->getFile(); // Check if there is a line directive for this location. if (FI.hasLineDirectives()) |