xref: /openbsd-src/gnu/llvm/clang/lib/Tooling/GuessTargetAndModeCompilationDatabase.cpp (revision e5dd70708596ae51455a0ffa086a00c5b29f8583)
1*e5dd7070Spatrick //===- GuessTargetAndModeCompilationDatabase.cpp --------------------------===//
2*e5dd7070Spatrick //
3*e5dd7070Spatrick // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4*e5dd7070Spatrick // See https://llvm.org/LICENSE.txt for license information.
5*e5dd7070Spatrick // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6*e5dd7070Spatrick //
7*e5dd7070Spatrick //===----------------------------------------------------------------------===//
8*e5dd7070Spatrick 
9*e5dd7070Spatrick #include "clang/Tooling/CompilationDatabase.h"
10*e5dd7070Spatrick #include "clang/Tooling/Tooling.h"
11*e5dd7070Spatrick #include <memory>
12*e5dd7070Spatrick 
13*e5dd7070Spatrick namespace clang {
14*e5dd7070Spatrick namespace tooling {
15*e5dd7070Spatrick 
16*e5dd7070Spatrick namespace {
17*e5dd7070Spatrick class TargetAndModeAdderDatabase : public CompilationDatabase {
18*e5dd7070Spatrick public:
TargetAndModeAdderDatabase(std::unique_ptr<CompilationDatabase> Base)19*e5dd7070Spatrick   TargetAndModeAdderDatabase(std::unique_ptr<CompilationDatabase> Base)
20*e5dd7070Spatrick       : Base(std::move(Base)) {
21*e5dd7070Spatrick     assert(this->Base != nullptr);
22*e5dd7070Spatrick   }
23*e5dd7070Spatrick 
getAllFiles() const24*e5dd7070Spatrick   std::vector<std::string> getAllFiles() const override {
25*e5dd7070Spatrick     return Base->getAllFiles();
26*e5dd7070Spatrick   }
27*e5dd7070Spatrick 
getAllCompileCommands() const28*e5dd7070Spatrick   std::vector<CompileCommand> getAllCompileCommands() const override {
29*e5dd7070Spatrick     return addTargetAndMode(Base->getAllCompileCommands());
30*e5dd7070Spatrick   }
31*e5dd7070Spatrick 
32*e5dd7070Spatrick   std::vector<CompileCommand>
getCompileCommands(StringRef FilePath) const33*e5dd7070Spatrick   getCompileCommands(StringRef FilePath) const override {
34*e5dd7070Spatrick     return addTargetAndMode(Base->getCompileCommands(FilePath));
35*e5dd7070Spatrick   }
36*e5dd7070Spatrick 
37*e5dd7070Spatrick private:
38*e5dd7070Spatrick   std::vector<CompileCommand>
addTargetAndMode(std::vector<CompileCommand> Cmds) const39*e5dd7070Spatrick   addTargetAndMode(std::vector<CompileCommand> Cmds) const {
40*e5dd7070Spatrick     for (auto &Cmd : Cmds) {
41*e5dd7070Spatrick       if (Cmd.CommandLine.empty())
42*e5dd7070Spatrick         continue;
43*e5dd7070Spatrick       addTargetAndModeForProgramName(Cmd.CommandLine, Cmd.CommandLine.front());
44*e5dd7070Spatrick     }
45*e5dd7070Spatrick     return Cmds;
46*e5dd7070Spatrick   }
47*e5dd7070Spatrick   std::unique_ptr<CompilationDatabase> Base;
48*e5dd7070Spatrick };
49*e5dd7070Spatrick } // namespace
50*e5dd7070Spatrick 
51*e5dd7070Spatrick std::unique_ptr<CompilationDatabase>
inferTargetAndDriverMode(std::unique_ptr<CompilationDatabase> Base)52*e5dd7070Spatrick inferTargetAndDriverMode(std::unique_ptr<CompilationDatabase> Base) {
53*e5dd7070Spatrick   return std::make_unique<TargetAndModeAdderDatabase>(std::move(Base));
54*e5dd7070Spatrick }
55*e5dd7070Spatrick 
56*e5dd7070Spatrick } // namespace tooling
57*e5dd7070Spatrick } // namespace clang
58