diff options
Diffstat (limited to 'clang/lib/Frontend/DependencyFile.cpp')
-rw-r--r-- | clang/lib/Frontend/DependencyFile.cpp | 30 |
1 files changed, 18 insertions, 12 deletions
diff --git a/clang/lib/Frontend/DependencyFile.cpp b/clang/lib/Frontend/DependencyFile.cpp index 06ddb0f0db20..fe4218b6e672 100644 --- a/clang/lib/Frontend/DependencyFile.cpp +++ b/clang/lib/Frontend/DependencyFile.cpp @@ -21,10 +21,10 @@ #include "clang/Lex/Preprocessor.h" #include "clang/Serialization/ASTReader.h" #include "llvm/ADT/StringSet.h" -#include "llvm/ADT/StringSwitch.h" #include "llvm/Support/FileSystem.h" #include "llvm/Support/Path.h" #include "llvm/Support/raw_ostream.h" +#include <optional> using namespace clang; @@ -44,7 +44,7 @@ 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! - if (Optional<StringRef> Filename = + if (std::optional<StringRef> Filename = PP.getSourceManager().getNonBuiltinFilenameForID(FID)) DepCollector.maybeAddDependency( llvm::sys::path::remove_leading_dotslash(*Filename), @@ -65,7 +65,7 @@ struct DepCollectorPPCallbacks : public PPCallbacks { void InclusionDirective(SourceLocation HashLoc, const Token &IncludeTok, StringRef FileName, bool IsAngled, CharSourceRange FilenameRange, - Optional<FileEntryRef> File, StringRef SearchPath, + OptionalFileEntryRef File, StringRef SearchPath, StringRef RelativePath, const Module *Imported, SrcMgr::CharacteristicKind FileType) override { if (!File) @@ -76,7 +76,7 @@ struct DepCollectorPPCallbacks : public PPCallbacks { } void HasInclude(SourceLocation Loc, StringRef SpelledFilename, bool IsAngled, - Optional<FileEntryRef> File, + OptionalFileEntryRef File, SrcMgr::CharacteristicKind FileType) override { if (!File) return; @@ -109,7 +109,9 @@ struct DepCollectorMMCallbacks : public ModuleMapCallbacks { struct DepCollectorASTListener : public ASTReaderListener { DependencyCollector &DepCollector; - DepCollectorASTListener(DependencyCollector &L) : DepCollector(L) { } + FileManager &FileMgr; + DepCollectorASTListener(DependencyCollector &L, FileManager &FileMgr) + : DepCollector(L), FileMgr(FileMgr) {} bool needsInputFileVisitation() override { return true; } bool needsSystemInputFileVisitation() override { return DepCollector.needSystemDependencies(); @@ -125,6 +127,11 @@ struct DepCollectorASTListener : public ASTReaderListener { if (IsOverridden || IsExplicitModule) return true; + // Run this through the FileManager in order to respect 'use-external-name' + // in case we have a VFS overlay. + if (auto FE = FileMgr.getOptionalFileRef(Filename)) + Filename = FE->getName(); + DepCollector.maybeAddDependency(Filename, /*FromModule*/true, IsSystem, /*IsModuleFile*/false, /*IsMissing*/false); return true; @@ -160,10 +167,7 @@ bool DependencyCollector::addDependency(StringRef Filename) { } static bool isSpecialFilename(StringRef Filename) { - return llvm::StringSwitch<bool>(Filename) - .Case("<built-in>", true) - .Case("<stdin>", true) - .Default(false); + return Filename == "<built-in>"; } bool DependencyCollector::sawDependency(StringRef Filename, bool FromModule, @@ -180,7 +184,8 @@ void DependencyCollector::attachToPreprocessor(Preprocessor &PP) { std::make_unique<DepCollectorMMCallbacks>(*this)); } void DependencyCollector::attachToASTReader(ASTReader &R) { - R.addListener(std::make_unique<DepCollectorASTListener>(*this)); + R.addListener( + std::make_unique<DepCollectorASTListener>(*this, R.getFileManager())); } DependencyFileGenerator::DependencyFileGenerator( @@ -329,7 +334,7 @@ void DependencyFileGenerator::outputDependencyFile(DiagnosticsEngine &Diags) { void DependencyFileGenerator::outputDependencyFile(llvm::raw_ostream &OS) { // Write out the dependency targets, trying to avoid overly long // lines when possible. We try our best to emit exactly the same - // dependency file as GCC (4.2), assuming the included files are the + // dependency file as GCC>=10, assuming the included files are the // same. const unsigned MaxColumns = 75; unsigned Columns = 0; @@ -356,6 +361,8 @@ void DependencyFileGenerator::outputDependencyFile(llvm::raw_ostream &OS) { // duplicates. ArrayRef<std::string> Files = getDependencies(); for (StringRef File : Files) { + if (File == "<stdin>") + continue; // Start a new line if this would exceed the column limit. Make // sure to leave space for a trailing " \" in case we need to // break the line on the next iteration. @@ -376,7 +383,6 @@ void DependencyFileGenerator::outputDependencyFile(llvm::raw_ostream &OS) { for (auto I = Files.begin(), E = Files.end(); I != E; ++I) { if (Index++ == InputFileIndex) continue; - OS << '\n'; PrintFilename(OS, *I, OutputFormat); OS << ":\n"; } |