Introduce SourceManager::getDisplayNameForLoc

Add an abstraction to retrieve a name suitable for display for a given
source location for use with diagnostics, debug locations, and magic
file literals.
This commit is contained in:
Robert Widmann
2018-07-28 01:21:44 -04:00
parent 0e58b7fd14
commit 917e94d11c
5 changed files with 32 additions and 4 deletions

View File

@@ -39,6 +39,27 @@ SourceLoc SourceManager::getCodeCompletionLoc() const {
.getAdvancedLoc(CodeCompletionOffset);
}
StringRef SourceManager::getDisplayNameForLoc(SourceLoc Loc) const {
// Respect #line first
if (auto VFile = getVirtualFile(Loc))
return VFile->Name;
// Next, try the stat cache
auto Ident = getIdentifierForBuffer(findBufferContainingLoc(Loc));
auto found = StatusCache.find(Ident);
if (found != StatusCache.end()) {
return found->second.getName();
}
// Populate the cache with a (virtual) stat.
if (auto Status = FileSystem->status(Ident)) {
return (StatusCache[Ident] = Status.get()).getName();
}
// Finally, fall back to the buffer identifier.
return Ident;
}
unsigned
SourceManager::addNewSourceBuffer(std::unique_ptr<llvm::MemoryBuffer> Buffer) {
assert(Buffer);