]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - lib/Tooling/DependencyScanning/DependencyScanningTool.cpp
Vendor import of stripped clang trunk r375505, the last commit before
[FreeBSD/FreeBSD.git] / lib / Tooling / DependencyScanning / DependencyScanningTool.cpp
1 //===- DependencyScanningTool.cpp - clang-scan-deps service ------------===//
2 //
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
6 //
7 //===----------------------------------------------------------------------===//
8
9 #include "clang/Tooling/DependencyScanning/DependencyScanningTool.h"
10 #include "clang/Frontend/Utils.h"
11
12 namespace clang{
13 namespace tooling{
14 namespace dependencies{
15
16 DependencyScanningTool::DependencyScanningTool(DependencyScanningService &Service,
17 const tooling::CompilationDatabase &Compilations) : Worker(Service), Compilations(Compilations) {}
18
19 llvm::Expected<std::string> DependencyScanningTool::getDependencyFile(const std::string &Input,
20                                               StringRef CWD) {
21   /// Prints out all of the gathered dependencies into a string.
22   class DependencyPrinterConsumer : public DependencyConsumer {
23   public:
24     void handleFileDependency(const DependencyOutputOptions &Opts,
25                               StringRef File) override {
26       if (!this->Opts)
27         this->Opts = std::make_unique<DependencyOutputOptions>(Opts);
28       Dependencies.push_back(File);
29     }
30
31     void printDependencies(std::string &S) {
32       if (!Opts)
33         return;
34
35       class DependencyPrinter : public DependencyFileGenerator {
36       public:
37         DependencyPrinter(DependencyOutputOptions &Opts,
38                           ArrayRef<std::string> Dependencies)
39             : DependencyFileGenerator(Opts) {
40           for (const auto &Dep : Dependencies)
41             addDependency(Dep);
42         }
43
44         void printDependencies(std::string &S) {
45           llvm::raw_string_ostream OS(S);
46           outputDependencyFile(OS);
47         }
48       };
49
50       DependencyPrinter Generator(*Opts, Dependencies);
51       Generator.printDependencies(S);
52     }
53
54   private:
55     std::unique_ptr<DependencyOutputOptions> Opts;
56     std::vector<std::string> Dependencies;
57   };
58
59   DependencyPrinterConsumer Consumer;
60   auto Result =
61       Worker.computeDependencies(Input, CWD, Compilations, Consumer);
62   if (Result)
63     return std::move(Result);
64   std::string Output;
65   Consumer.printDependencies(Output);
66   return Output;
67 }
68
69 } // end namespace dependencies
70 } // end namespace tooling
71 } // end namespace clang