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