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