From cf593d224c9c3fc2cf93827cb66fd9e550a6a486 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Duncan=20P=2E=20N=2E=20Exon=C2=A0Smith?= Date: Thu, 15 Oct 2020 18:46:25 -0400 Subject: [PATCH] SourceManager: getFileEntryRefForID => getNonBuiltinFilenameForID, NFC `SourceManager::getFileEntryRefForID`'s remaining callers just want the filename component, which is coming from the `FileInfo`. Replace the API with `getNonBuiltinFilenameForID`, which also removes another use of `FileEntryRef::FileEntryRef` outside of `FileManager`. Both callers are collecting file dependencies, and one of them relied on this API to filter out built-ins (as exposed by clang/test/ClangScanDeps/modules-full.cpp). It seems nice to continue providing that service. Differential Revision: https://reviews.llvm.org/D89508 --- clang/include/clang/Basic/SourceManager.h | 7 +++++-- clang/lib/Basic/SourceManager.cpp | 5 +++-- clang/lib/Frontend/DependencyFile.cpp | 17 ++++++----------- .../DependencyScanning/ModuleDepCollector.cpp | 13 ++++--------- 4 files changed, 18 insertions(+), 24 deletions(-) diff --git a/clang/include/clang/Basic/SourceManager.h b/clang/include/clang/Basic/SourceManager.h index 93eee503a8e5..5035326297f7 100644 --- a/clang/include/clang/Basic/SourceManager.h +++ b/clang/include/clang/Basic/SourceManager.h @@ -997,8 +997,11 @@ public: return nullptr; } - /// Returns the FileEntryRef for the provided FileID. - Optional getFileEntryRefForID(FileID FID) const; + /// Returns the filename for the provided FileID, unless it's a built-in + /// buffer that's not represented by a filename. + /// + /// Returns None for non-files and built-in files. + Optional getNonBuiltinFilenameForID(FileID FID) const; /// Returns the FileEntry record for the provided SLocEntry. const FileEntry *getFileEntryForSLocEntry(const SrcMgr::SLocEntry &sloc) const diff --git a/clang/lib/Basic/SourceManager.cpp b/clang/lib/Basic/SourceManager.cpp index c91ef41979bc..79eeeb29d6b9 100644 --- a/clang/lib/Basic/SourceManager.cpp +++ b/clang/lib/Basic/SourceManager.cpp @@ -725,11 +725,12 @@ void SourceManager::setFileIsTransient(const FileEntry *File) { const_cast(CC)->IsTransient = true; } -Optional SourceManager::getFileEntryRefForID(FileID FID) const { +Optional +SourceManager::getNonBuiltinFilenameForID(FileID FID) const { 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 Entry->getFile().getName(); return None; } diff --git a/clang/lib/Frontend/DependencyFile.cpp b/clang/lib/Frontend/DependencyFile.cpp index c9240f4122a7..fe8ab7197400 100644 --- a/clang/lib/Frontend/DependencyFile.cpp +++ b/clang/lib/Frontend/DependencyFile.cpp @@ -46,17 +46,12 @@ struct DepCollectorPPCallbacks : public PPCallbacks { // Dependency generation really does want to go all the way to the // file entry for a source location to find out what is depended on. // We do not want #line markers to affect dependency generation! - Optional File = - SM.getFileEntryRefForID(SM.getFileID(SM.getExpansionLoc(Loc))); - if (!File) - return; - - StringRef Filename = - llvm::sys::path::remove_leading_dotslash(File->getName()); - - DepCollector.maybeAddDependency(Filename, /*FromModule*/false, - isSystem(FileType), - /*IsModuleFile*/false, /*IsMissing*/false); + if (Optional Filename = SM.getNonBuiltinFilenameForID( + SM.getFileID(SM.getExpansionLoc(Loc)))) + DepCollector.maybeAddDependency( + llvm::sys::path::remove_leading_dotslash(*Filename), + /*FromModule*/ false, isSystem(FileType), /*IsModuleFile*/ false, + /*IsMissing*/ false); } void FileSkipped(const FileEntryRef &SkippedFile, const Token &FilenameTok, diff --git a/clang/lib/Tooling/DependencyScanning/ModuleDepCollector.cpp b/clang/lib/Tooling/DependencyScanning/ModuleDepCollector.cpp index 4f6eff799f22..f74ce7304df5 100644 --- a/clang/lib/Tooling/DependencyScanning/ModuleDepCollector.cpp +++ b/clang/lib/Tooling/DependencyScanning/ModuleDepCollector.cpp @@ -77,15 +77,10 @@ void ModuleDepCollectorPP::FileChanged(SourceLocation Loc, // Dependency generation really does want to go all the way to the // file entry for a source location to find out what is depended on. // We do not want #line markers to affect dependency generation! - Optional File = - SM.getFileEntryRefForID(SM.getFileID(SM.getExpansionLoc(Loc))); - if (!File) - return; - - StringRef FileName = - llvm::sys::path::remove_leading_dotslash(File->getName()); - - MDC.MainDeps.push_back(std::string(FileName)); + if (Optional Filename = + SM.getNonBuiltinFilenameForID(SM.getFileID(SM.getExpansionLoc(Loc)))) + MDC.MainDeps.push_back( + std::string(llvm::sys::path::remove_leading_dotslash(*Filename))); } void ModuleDepCollectorPP::InclusionDirective(