[CrashReproducer] Cleanup and move functionality around in ModuleDependencyCollector. NFC
- Make ModuleDependencyCollector use the DependencyCollector interface - Move some methods from ModuleDependencyListener to ModuleDependencyCollector in order to share common functionality with other future possible callbacks. llvm-svn: 264808
This commit is contained in:
parent
b81f1e0db3
commit
b1631d9161
|
@ -73,12 +73,12 @@ void DoPrintPreprocessedInput(Preprocessor &PP, raw_ostream* OS,
|
||||||
/// An interface for collecting the dependencies of a compilation. Users should
|
/// An interface for collecting the dependencies of a compilation. Users should
|
||||||
/// use \c attachToPreprocessor and \c attachToASTReader to get all of the
|
/// use \c attachToPreprocessor and \c attachToASTReader to get all of the
|
||||||
/// dependencies.
|
/// dependencies.
|
||||||
// FIXME: Migrate DependencyFileGen, DependencyGraphGen, ModuleDepCollectory to
|
/// FIXME: Migrate DependencyFileGen and DependencyGraphGen to use this
|
||||||
// use this interface.
|
/// interface.
|
||||||
class DependencyCollector {
|
class DependencyCollector {
|
||||||
public:
|
public:
|
||||||
void attachToPreprocessor(Preprocessor &PP);
|
void attachToPreprocessor(Preprocessor &PP);
|
||||||
void attachToASTReader(ASTReader &R);
|
virtual void attachToASTReader(ASTReader &R);
|
||||||
llvm::ArrayRef<std::string> getDependencies() const { return Dependencies; }
|
llvm::ArrayRef<std::string> getDependencies() const { return Dependencies; }
|
||||||
|
|
||||||
/// Called when a new file is seen. Return true if \p Filename should be added
|
/// Called when a new file is seen. Return true if \p Filename should be added
|
||||||
|
@ -118,25 +118,29 @@ public:
|
||||||
|
|
||||||
/// Collects the dependencies for imported modules into a directory. Users
|
/// Collects the dependencies for imported modules into a directory. Users
|
||||||
/// should attach to the AST reader whenever a module is loaded.
|
/// should attach to the AST reader whenever a module is loaded.
|
||||||
class ModuleDependencyCollector {
|
class ModuleDependencyCollector : public DependencyCollector {
|
||||||
std::string DestDir;
|
std::string DestDir;
|
||||||
bool HasErrors;
|
bool HasErrors = false;
|
||||||
llvm::StringSet<> Seen;
|
llvm::StringSet<> Seen;
|
||||||
vfs::YAMLVFSWriter VFSWriter;
|
vfs::YAMLVFSWriter VFSWriter;
|
||||||
|
|
||||||
|
llvm::StringMap<std::string> SymLinkMap;
|
||||||
|
|
||||||
|
bool getRealPath(StringRef SrcPath, SmallVectorImpl<char> &Result);
|
||||||
|
std::error_code copyToRoot(StringRef Src);
|
||||||
public:
|
public:
|
||||||
StringRef getDest() { return DestDir; }
|
StringRef getDest() { return DestDir; }
|
||||||
bool insertSeen(StringRef Filename) { return Seen.insert(Filename).second; }
|
bool insertSeen(StringRef Filename) { return Seen.insert(Filename).second; }
|
||||||
void setHasErrors() { HasErrors = true; }
|
void addFile(StringRef Filename);
|
||||||
void addFileMapping(StringRef VPath, StringRef RPath) {
|
void addFileMapping(StringRef VPath, StringRef RPath) {
|
||||||
VFSWriter.addFileMapping(VPath, RPath);
|
VFSWriter.addFileMapping(VPath, RPath);
|
||||||
}
|
}
|
||||||
|
|
||||||
void attachToASTReader(ASTReader &R);
|
void attachToASTReader(ASTReader &R) override;
|
||||||
|
|
||||||
void writeFileMap();
|
void writeFileMap();
|
||||||
bool hasErrors() { return HasErrors; }
|
bool hasErrors() { return HasErrors; }
|
||||||
ModuleDependencyCollector(std::string DestDir)
|
ModuleDependencyCollector(std::string DestDir) : DestDir(DestDir) {}
|
||||||
: DestDir(DestDir), HasErrors(false) {}
|
|
||||||
~ModuleDependencyCollector() { writeFileMap(); }
|
~ModuleDependencyCollector() { writeFileMap(); }
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -349,14 +349,18 @@ void CompilerInstance::createPreprocessor(TranslationUnitKind TUKind) {
|
||||||
AttachDependencyGraphGen(*PP, DepOpts.DOTOutputFile,
|
AttachDependencyGraphGen(*PP, DepOpts.DOTOutputFile,
|
||||||
getHeaderSearchOpts().Sysroot);
|
getHeaderSearchOpts().Sysroot);
|
||||||
|
|
||||||
for (auto &Listener : DependencyCollectors)
|
|
||||||
Listener->attachToPreprocessor(*PP);
|
|
||||||
|
|
||||||
// If we don't have a collector, but we are collecting module dependencies,
|
// If we don't have a collector, but we are collecting module dependencies,
|
||||||
// then we're the top level compiler instance and need to create one.
|
// then we're the top level compiler instance and need to create one.
|
||||||
if (!ModuleDepCollector && !DepOpts.ModuleDependencyOutputDir.empty())
|
if (!ModuleDepCollector && !DepOpts.ModuleDependencyOutputDir.empty()) {
|
||||||
ModuleDepCollector = std::make_shared<ModuleDependencyCollector>(
|
ModuleDepCollector = std::make_shared<ModuleDependencyCollector>(
|
||||||
DepOpts.ModuleDependencyOutputDir);
|
DepOpts.ModuleDependencyOutputDir);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (ModuleDepCollector)
|
||||||
|
addDependencyCollector(ModuleDepCollector);
|
||||||
|
|
||||||
|
for (auto &Listener : DependencyCollectors)
|
||||||
|
Listener->attachToPreprocessor(*PP);
|
||||||
|
|
||||||
// Handle generating header include information, if requested.
|
// Handle generating header include information, if requested.
|
||||||
if (DepOpts.ShowHeaderIncludes)
|
if (DepOpts.ShowHeaderIncludes)
|
||||||
|
@ -1319,8 +1323,6 @@ void CompilerInstance::createModuleManager() {
|
||||||
|
|
||||||
if (TheDependencyFileGenerator)
|
if (TheDependencyFileGenerator)
|
||||||
TheDependencyFileGenerator->AttachToASTReader(*ModuleManager);
|
TheDependencyFileGenerator->AttachToASTReader(*ModuleManager);
|
||||||
if (ModuleDepCollector)
|
|
||||||
ModuleDepCollector->attachToASTReader(*ModuleManager);
|
|
||||||
for (auto &Listener : DependencyCollectors)
|
for (auto &Listener : DependencyCollectors)
|
||||||
Listener->attachToASTReader(*ModuleManager);
|
Listener->attachToASTReader(*ModuleManager);
|
||||||
}
|
}
|
||||||
|
|
|
@ -25,17 +25,16 @@ namespace {
|
||||||
/// Private implementation for ModuleDependencyCollector
|
/// Private implementation for ModuleDependencyCollector
|
||||||
class ModuleDependencyListener : public ASTReaderListener {
|
class ModuleDependencyListener : public ASTReaderListener {
|
||||||
ModuleDependencyCollector &Collector;
|
ModuleDependencyCollector &Collector;
|
||||||
llvm::StringMap<std::string> SymLinkMap;
|
|
||||||
|
|
||||||
bool getRealPath(StringRef SrcPath, SmallVectorImpl<char> &Result);
|
|
||||||
std::error_code copyToRoot(StringRef Src);
|
|
||||||
public:
|
public:
|
||||||
ModuleDependencyListener(ModuleDependencyCollector &Collector)
|
ModuleDependencyListener(ModuleDependencyCollector &Collector)
|
||||||
: Collector(Collector) {}
|
: Collector(Collector) {}
|
||||||
bool needsInputFileVisitation() override { return true; }
|
bool needsInputFileVisitation() override { return true; }
|
||||||
bool needsSystemInputFileVisitation() override { return true; }
|
bool needsSystemInputFileVisitation() override { return true; }
|
||||||
bool visitInputFile(StringRef Filename, bool IsSystem, bool IsOverridden,
|
bool visitInputFile(StringRef Filename, bool IsSystem, bool IsOverridden,
|
||||||
bool IsExplicitModule) override;
|
bool IsExplicitModule) override {
|
||||||
|
Collector.addFile(Filename);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -57,7 +56,7 @@ void ModuleDependencyCollector::writeFileMap() {
|
||||||
std::error_code EC;
|
std::error_code EC;
|
||||||
llvm::raw_fd_ostream OS(Dest, EC, llvm::sys::fs::F_Text);
|
llvm::raw_fd_ostream OS(Dest, EC, llvm::sys::fs::F_Text);
|
||||||
if (EC) {
|
if (EC) {
|
||||||
setHasErrors();
|
HasErrors = true;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
VFSWriter.write(OS);
|
VFSWriter.write(OS);
|
||||||
|
@ -81,8 +80,8 @@ static bool real_path(StringRef SrcPath, SmallVectorImpl<char> &RealPath) {
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
bool ModuleDependencyListener::getRealPath(StringRef SrcPath,
|
bool ModuleDependencyCollector::getRealPath(StringRef SrcPath,
|
||||||
SmallVectorImpl<char> &Result) {
|
SmallVectorImpl<char> &Result) {
|
||||||
using namespace llvm::sys;
|
using namespace llvm::sys;
|
||||||
SmallString<256> RealPath;
|
SmallString<256> RealPath;
|
||||||
StringRef FileName = path::filename(SrcPath);
|
StringRef FileName = path::filename(SrcPath);
|
||||||
|
@ -105,7 +104,7 @@ bool ModuleDependencyListener::getRealPath(StringRef SrcPath,
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::error_code ModuleDependencyListener::copyToRoot(StringRef Src) {
|
std::error_code ModuleDependencyCollector::copyToRoot(StringRef Src) {
|
||||||
using namespace llvm::sys;
|
using namespace llvm::sys;
|
||||||
|
|
||||||
// We need an absolute path to append to the root.
|
// We need an absolute path to append to the root.
|
||||||
|
@ -131,7 +130,7 @@ std::error_code ModuleDependencyListener::copyToRoot(StringRef Src) {
|
||||||
!StringRef(CanonicalPath).equals(RealPath);
|
!StringRef(CanonicalPath).equals(RealPath);
|
||||||
|
|
||||||
// Build the destination path.
|
// Build the destination path.
|
||||||
SmallString<256> Dest = Collector.getDest();
|
SmallString<256> Dest = getDest();
|
||||||
path::append(Dest, path::relative_path(HasRemovedSymlinkComponent ? RealPath
|
path::append(Dest, path::relative_path(HasRemovedSymlinkComponent ? RealPath
|
||||||
: CanonicalPath));
|
: CanonicalPath));
|
||||||
|
|
||||||
|
@ -145,18 +144,15 @@ std::error_code ModuleDependencyListener::copyToRoot(StringRef Src) {
|
||||||
|
|
||||||
// Use the canonical path under the root for the file mapping. Also create
|
// Use the canonical path under the root for the file mapping. Also create
|
||||||
// an additional entry for the real path.
|
// an additional entry for the real path.
|
||||||
Collector.addFileMapping(CanonicalPath, Dest);
|
addFileMapping(CanonicalPath, Dest);
|
||||||
if (HasRemovedSymlinkComponent)
|
if (HasRemovedSymlinkComponent)
|
||||||
Collector.addFileMapping(RealPath, Dest);
|
addFileMapping(RealPath, Dest);
|
||||||
|
|
||||||
return std::error_code();
|
return std::error_code();
|
||||||
}
|
}
|
||||||
|
|
||||||
bool ModuleDependencyListener::visitInputFile(StringRef Filename, bool IsSystem,
|
void ModuleDependencyCollector::addFile(StringRef Filename) {
|
||||||
bool IsOverridden,
|
if (insertSeen(Filename))
|
||||||
bool IsExplicitModule) {
|
|
||||||
if (Collector.insertSeen(Filename))
|
|
||||||
if (copyToRoot(Filename))
|
if (copyToRoot(Filename))
|
||||||
Collector.setHasErrors();
|
HasErrors = true;
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue