From 145272e6be4c7ba738c9795fd19b5ca68702c097 Mon Sep 17 00:00:00 2001 From: Alex Lorenz Date: Fri, 30 Aug 2019 01:25:57 +0000 Subject: [clang-scan-deps] NFC, refactor the DependencyScanningWorker to use a consumer to report the dependencies to the client This will allow the scanner to report modular dependencies to the consumer. This will also allow the scanner to accept regular cc1 clang invocations, e.g. in an implementation of a libclang C API for clang-scan-deps, that I will add follow-up patches for in the future. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@370425 91177308-0d34-0410-b5e6-96231b3b80d8 --- tools/clang-scan-deps/ClangScanDeps.cpp | 58 ++++++++++++++++++++++++++++++++- 1 file changed, 57 insertions(+), 1 deletion(-) (limited to 'tools') diff --git a/tools/clang-scan-deps/ClangScanDeps.cpp b/tools/clang-scan-deps/ClangScanDeps.cpp index 5e567fef9d..bee4b479b2 100644 --- a/tools/clang-scan-deps/ClangScanDeps.cpp +++ b/tools/clang-scan-deps/ClangScanDeps.cpp @@ -51,11 +51,67 @@ public: SharedStream &OS, SharedStream &Errs) : Worker(Service), Compilations(Compilations), OS(OS), Errs(Errs) {} + /// Print out the dependency information into a string using the dependency + /// file format that is specified in the options (-MD is the default) and + /// return it. + /// + /// \returns A \c StringError with the diagnostic output if clang errors + /// occurred, dependency file contents otherwise. + llvm::Expected getDependencyFile(const std::string &Input, + StringRef CWD) { + /// Prints out all of the gathered dependencies into a string. + class DependencyPrinterConsumer : public DependencyConsumer { + public: + void handleFileDependency(const DependencyOutputOptions &Opts, + StringRef File) override { + if (!this->Opts) + this->Opts = std::make_unique(Opts); + Dependencies.push_back(File); + } + + void printDependencies(std::string &S) { + if (!Opts) + return; + + class DependencyPrinter : public DependencyFileGenerator { + public: + DependencyPrinter(DependencyOutputOptions &Opts, + ArrayRef Dependencies) + : DependencyFileGenerator(Opts) { + for (const auto &Dep : Dependencies) + addDependency(Dep); + } + + void printDependencies(std::string &S) { + llvm::raw_string_ostream OS(S); + outputDependencyFile(OS); + } + }; + + DependencyPrinter Generator(*Opts, Dependencies); + Generator.printDependencies(S); + } + + private: + std::unique_ptr Opts; + std::vector Dependencies; + }; + + DependencyPrinterConsumer Consumer; + auto Result = + Worker.computeDependencies(Input, CWD, Compilations, Consumer); + if (Result) + return std::move(Result); + std::string Output; + Consumer.printDependencies(Output); + return Output; + } + /// Computes the dependencies for the given file and prints them out. /// /// \returns True on error. bool runOnFile(const std::string &Input, StringRef CWD) { - auto MaybeFile = Worker.getDependencyFile(Input, CWD, Compilations); + auto MaybeFile = getDependencyFile(Input, CWD); if (!MaybeFile) { llvm::handleAllErrors( MaybeFile.takeError(), [this, &Input](llvm::StringError &Err) { -- cgit v1.2.1