summaryrefslogtreecommitdiff
path: root/tools
diff options
context:
space:
mode:
authorAlex Lorenz <arphaman@gmail.com>2019-08-30 01:25:57 +0000
committerAlex Lorenz <arphaman@gmail.com>2019-08-30 01:25:57 +0000
commit145272e6be4c7ba738c9795fd19b5ca68702c097 (patch)
tree21d43ebd284e8c5b2ab9f8320d1e162e26836c67 /tools
parentd6beacf5d7933083d3a3102102d3d8b379288aa5 (diff)
downloadclang-145272e6be4c7ba738c9795fd19b5ca68702c097.tar.gz
[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
Diffstat (limited to 'tools')
-rw-r--r--tools/clang-scan-deps/ClangScanDeps.cpp58
1 files changed, 57 insertions, 1 deletions
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<std::string> 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<DependencyOutputOptions>(Opts);
+ Dependencies.push_back(File);
+ }
+
+ void printDependencies(std::string &S) {
+ if (!Opts)
+ return;
+
+ class DependencyPrinter : public DependencyFileGenerator {
+ public:
+ DependencyPrinter(DependencyOutputOptions &Opts,
+ ArrayRef<std::string> 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<DependencyOutputOptions> Opts;
+ std::vector<std::string> 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) {