1 //===- DependencyScanningTool.cpp - clang-scan-deps service ------------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 #include "clang/Tooling/DependencyScanning/DependencyScanningTool.h"
10 #include "clang/Frontend/Utils.h"
14 namespace dependencies{
16 DependencyScanningTool::DependencyScanningTool(DependencyScanningService &Service,
17 const tooling::CompilationDatabase &Compilations) : Worker(Service), Compilations(Compilations) {}
19 llvm::Expected<std::string> DependencyScanningTool::getDependencyFile(const std::string &Input,
21 /// Prints out all of the gathered dependencies into a string.
22 class DependencyPrinterConsumer : public DependencyConsumer {
24 void handleFileDependency(const DependencyOutputOptions &Opts,
25 StringRef File) override {
27 this->Opts = std::make_unique<DependencyOutputOptions>(Opts);
28 Dependencies.push_back(File);
31 void printDependencies(std::string &S) {
35 class DependencyPrinter : public DependencyFileGenerator {
37 DependencyPrinter(DependencyOutputOptions &Opts,
38 ArrayRef<std::string> Dependencies)
39 : DependencyFileGenerator(Opts) {
40 for (const auto &Dep : Dependencies)
44 void printDependencies(std::string &S) {
45 llvm::raw_string_ostream OS(S);
46 outputDependencyFile(OS);
50 DependencyPrinter Generator(*Opts, Dependencies);
51 Generator.printDependencies(S);
55 std::unique_ptr<DependencyOutputOptions> Opts;
56 std::vector<std::string> Dependencies;
59 DependencyPrinterConsumer Consumer;
61 Worker.computeDependencies(Input, CWD, Compilations, Consumer);
63 return std::move(Result);
65 Consumer.printDependencies(Output);
69 } // end namespace dependencies
70 } // end namespace tooling
71 } // end namespace clang