GuessTargetAndModeCompilationDatabase.cpp 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. //===- GuessTargetAndModeCompilationDatabase.cpp --------------------------===//
  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/CompilationDatabase.h"
  9. #include "clang/Tooling/Tooling.h"
  10. #include <memory>
  11. namespace clang {
  12. namespace tooling {
  13. namespace {
  14. class TargetAndModeAdderDatabase : public CompilationDatabase {
  15. public:
  16. TargetAndModeAdderDatabase(std::unique_ptr<CompilationDatabase> Base)
  17. : Base(std::move(Base)) {
  18. assert(this->Base != nullptr);
  19. }
  20. std::vector<std::string> getAllFiles() const override {
  21. return Base->getAllFiles();
  22. }
  23. std::vector<CompileCommand> getAllCompileCommands() const override {
  24. return addTargetAndMode(Base->getAllCompileCommands());
  25. }
  26. std::vector<CompileCommand>
  27. getCompileCommands(StringRef FilePath) const override {
  28. return addTargetAndMode(Base->getCompileCommands(FilePath));
  29. }
  30. private:
  31. std::vector<CompileCommand>
  32. addTargetAndMode(std::vector<CompileCommand> Cmds) const {
  33. for (auto &Cmd : Cmds) {
  34. if (Cmd.CommandLine.empty())
  35. continue;
  36. addTargetAndModeForProgramName(Cmd.CommandLine, Cmd.CommandLine.front());
  37. }
  38. return Cmds;
  39. }
  40. std::unique_ptr<CompilationDatabase> Base;
  41. };
  42. } // namespace
  43. std::unique_ptr<CompilationDatabase>
  44. inferTargetAndDriverMode(std::unique_ptr<CompilationDatabase> Base) {
  45. return std::make_unique<TargetAndModeAdderDatabase>(std::move(Base));
  46. }
  47. } // namespace tooling
  48. } // namespace clang