DependencyScanningTool.cpp 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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. #include "clang/Tooling/DependencyScanning/DependencyScanningTool.h"
  9. #include "clang/Frontend/Utils.h"
  10. namespace clang{
  11. namespace tooling{
  12. namespace dependencies{
  13. DependencyScanningTool::DependencyScanningTool(DependencyScanningService &Service,
  14. const tooling::CompilationDatabase &Compilations) : Worker(Service), Compilations(Compilations) {}
  15. llvm::Expected<std::string> DependencyScanningTool::getDependencyFile(const std::string &Input,
  16. StringRef CWD) {
  17. /// Prints out all of the gathered dependencies into a string.
  18. class DependencyPrinterConsumer : public DependencyConsumer {
  19. public:
  20. void handleFileDependency(const DependencyOutputOptions &Opts,
  21. StringRef File) override {
  22. if (!this->Opts)
  23. this->Opts = std::make_unique<DependencyOutputOptions>(Opts);
  24. Dependencies.push_back(File);
  25. }
  26. void printDependencies(std::string &S) {
  27. if (!Opts)
  28. return;
  29. class DependencyPrinter : public DependencyFileGenerator {
  30. public:
  31. DependencyPrinter(DependencyOutputOptions &Opts,
  32. ArrayRef<std::string> Dependencies)
  33. : DependencyFileGenerator(Opts) {
  34. for (const auto &Dep : Dependencies)
  35. addDependency(Dep);
  36. }
  37. void printDependencies(std::string &S) {
  38. llvm::raw_string_ostream OS(S);
  39. outputDependencyFile(OS);
  40. }
  41. };
  42. DependencyPrinter Generator(*Opts, Dependencies);
  43. Generator.printDependencies(S);
  44. }
  45. private:
  46. std::unique_ptr<DependencyOutputOptions> Opts;
  47. std::vector<std::string> Dependencies;
  48. };
  49. DependencyPrinterConsumer Consumer;
  50. auto Result =
  51. Worker.computeDependencies(Input, CWD, Compilations, Consumer);
  52. if (Result)
  53. return std::move(Result);
  54. std::string Output;
  55. Consumer.printDependencies(Output);
  56. return Output;
  57. }
  58. } // end namespace dependencies
  59. } // end namespace tooling
  60. } // end namespace clang