diff options
| author | Alex Lorenz <arphaman@gmail.com> | 2019-06-21 18:24:55 +0000 |
|---|---|---|
| committer | Alex Lorenz <arphaman@gmail.com> | 2019-06-21 18:24:55 +0000 |
| commit | 459f733ef00bf0a4b8d7d5b975bcefb8dc2971f3 (patch) | |
| tree | f2bb754fea8a93b1adee9361e76837f2554d241c /clang/tools | |
| parent | b250a62a51bbbec0e1f2dd1de8b8c52bead2230e (diff) | |
| download | bcm5719-llvm-459f733ef00bf0a4b8d7d5b975bcefb8dc2971f3.tar.gz bcm5719-llvm-459f733ef00bf0a4b8d7d5b975bcefb8dc2971f3.zip | |
[clang-scan-deps] print the dependencies to stdout
and remove the need to use -MD options in the CDB
Differential Revision: https://reviews.llvm.org/D63579
llvm-svn: 364088
Diffstat (limited to 'clang/tools')
| -rw-r--r-- | clang/tools/clang-scan-deps/ClangScanDeps.cpp | 65 |
1 files changed, 58 insertions, 7 deletions
diff --git a/clang/tools/clang-scan-deps/ClangScanDeps.cpp b/clang/tools/clang-scan-deps/ClangScanDeps.cpp index e1d7d94c760..64733e947e5 100644 --- a/clang/tools/clang-scan-deps/ClangScanDeps.cpp +++ b/clang/tools/clang-scan-deps/ClangScanDeps.cpp @@ -29,12 +29,43 @@ using namespace clang; namespace { +class SharedStream { +public: + SharedStream(raw_ostream &OS) : OS(OS) {} + void applyLocked(llvm::function_ref<void(raw_ostream &OS)> Fn) { + std::unique_lock<std::mutex> LockGuard(Lock); + Fn(OS); + OS.flush(); + } + +private: + std::mutex Lock; + raw_ostream &OS; +}; + +/// Prints out all of the gathered dependencies into one output stream instead +/// of using the output dependency file. +class DependencyPrinter : public DependencyFileGenerator { +public: + DependencyPrinter(std::unique_ptr<DependencyOutputOptions> Opts, + SharedStream &OS) + : DependencyFileGenerator(*Opts), Opts(std::move(Opts)), OS(OS) {} + + void finishedMainFile(DiagnosticsEngine &Diags) override { + OS.applyLocked([this](raw_ostream &OS) { outputDependencyFile(OS); }); + } + +private: + std::unique_ptr<DependencyOutputOptions> Opts; + SharedStream &OS; +}; + /// A clang tool that runs the preprocessor only for the given compiler /// invocation. class PreprocessorOnlyTool : public tooling::ToolAction { public: - PreprocessorOnlyTool(StringRef WorkingDirectory) - : WorkingDirectory(WorkingDirectory) {} + PreprocessorOnlyTool(StringRef WorkingDirectory, SharedStream &OS) + : WorkingDirectory(WorkingDirectory), OS(OS) {} bool runInvocation(std::shared_ptr<CompilerInvocation> Invocation, FileManager *FileMgr, @@ -53,6 +84,21 @@ public: Compiler.createSourceManager(*FileMgr); + // Create the dependency collector that will collect the produced + // dependencies. + // + // This also moves the existing dependency output options from the + // invocation to the collector. The options in the invocation are reset, + // which ensures that the compiler won't create new dependency collectors, + // and thus won't write out the extra '.d' files to disk. + auto Opts = llvm::make_unique<DependencyOutputOptions>( + std::move(Compiler.getInvocation().getDependencyOutputOpts())); + // We need at least one -MT equivalent for the generator to work. + if (Opts->Targets.empty()) + Opts->Targets = {"clang-scan-deps dependency"}; + Compiler.addDependencyCollector( + std::make_shared<DependencyPrinter>(std::move(Opts), OS)); + auto Action = llvm::make_unique<PreprocessOnlyAction>(); const bool Result = Compiler.ExecuteAction(*Action); FileMgr->clearStatCache(); @@ -61,6 +107,7 @@ public: private: StringRef WorkingDirectory; + SharedStream &OS; }; /// A proxy file system that doesn't call `chdir` when changing the working @@ -93,8 +140,9 @@ public: /// /// \param Compilations The reference to the compilation database that's /// used by the clang tool. - DependencyScanningTool(const tooling::CompilationDatabase &Compilations) - : Compilations(Compilations) { + DependencyScanningTool(const tooling::CompilationDatabase &Compilations, + SharedStream &OS) + : Compilations(Compilations), OS(OS) { PCHContainerOps = std::make_shared<PCHContainerOperations>(); BaseFS = new ProxyFileSystemWithoutChdir(llvm::vfs::getRealFileSystem()); } @@ -107,12 +155,13 @@ public: tooling::ClangTool Tool(Compilations, Input, PCHContainerOps, BaseFS); Tool.clearArgumentsAdjusters(); Tool.setRestoreWorkingDir(false); - PreprocessorOnlyTool Action(CWD); + PreprocessorOnlyTool Action(CWD, OS); return Tool.run(&Action); } private: const tooling::CompilationDatabase &Compilations; + SharedStream &OS; std::shared_ptr<PCHContainerOperations> PCHContainerOps; /// The real filesystem used as a base for all the operations performed by the /// tool. @@ -176,12 +225,14 @@ int main(int argc, const char **argv) { return AdjustedArgs; }); + // Print out the dependency results to STDOUT by default. + SharedStream DependencyOS(llvm::outs()); unsigned NumWorkers = NumThreads == 0 ? llvm::hardware_concurrency() : NumThreads; std::vector<std::unique_ptr<DependencyScanningTool>> WorkerTools; for (unsigned I = 0; I < NumWorkers; ++I) - WorkerTools.push_back( - llvm::make_unique<DependencyScanningTool>(*AdjustingCompilations)); + WorkerTools.push_back(llvm::make_unique<DependencyScanningTool>( + *AdjustingCompilations, DependencyOS)); std::vector<std::thread> WorkerThreads; std::atomic<bool> HadErrors(false); |

