10b57cec5SDimitry Andric //===- GuessTargetAndModeCompilationDatabase.cpp --------------------------===// 20b57cec5SDimitry Andric // 30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information. 50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 60b57cec5SDimitry Andric // 70b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 80b57cec5SDimitry Andric 90b57cec5SDimitry Andric #include "clang/Tooling/CompilationDatabase.h" 100b57cec5SDimitry Andric #include "clang/Tooling/Tooling.h" 110b57cec5SDimitry Andric #include <memory> 120b57cec5SDimitry Andric 130b57cec5SDimitry Andric namespace clang { 140b57cec5SDimitry Andric namespace tooling { 150b57cec5SDimitry Andric 160b57cec5SDimitry Andric namespace { 170b57cec5SDimitry Andric class TargetAndModeAdderDatabase : public CompilationDatabase { 180b57cec5SDimitry Andric public: TargetAndModeAdderDatabase(std::unique_ptr<CompilationDatabase> Base)190b57cec5SDimitry Andric TargetAndModeAdderDatabase(std::unique_ptr<CompilationDatabase> Base) 200b57cec5SDimitry Andric : Base(std::move(Base)) { 210b57cec5SDimitry Andric assert(this->Base != nullptr); 220b57cec5SDimitry Andric } 230b57cec5SDimitry Andric getAllFiles() const240b57cec5SDimitry Andric std::vector<std::string> getAllFiles() const override { 250b57cec5SDimitry Andric return Base->getAllFiles(); 260b57cec5SDimitry Andric } 270b57cec5SDimitry Andric getAllCompileCommands() const280b57cec5SDimitry Andric std::vector<CompileCommand> getAllCompileCommands() const override { 290b57cec5SDimitry Andric return addTargetAndMode(Base->getAllCompileCommands()); 300b57cec5SDimitry Andric } 310b57cec5SDimitry Andric 320b57cec5SDimitry Andric std::vector<CompileCommand> getCompileCommands(StringRef FilePath) const330b57cec5SDimitry Andric getCompileCommands(StringRef FilePath) const override { 340b57cec5SDimitry Andric return addTargetAndMode(Base->getCompileCommands(FilePath)); 350b57cec5SDimitry Andric } 360b57cec5SDimitry Andric 370b57cec5SDimitry Andric private: 380b57cec5SDimitry Andric std::vector<CompileCommand> addTargetAndMode(std::vector<CompileCommand> Cmds) const390b57cec5SDimitry Andric addTargetAndMode(std::vector<CompileCommand> Cmds) const { 400b57cec5SDimitry Andric for (auto &Cmd : Cmds) { 410b57cec5SDimitry Andric if (Cmd.CommandLine.empty()) 420b57cec5SDimitry Andric continue; 430b57cec5SDimitry Andric addTargetAndModeForProgramName(Cmd.CommandLine, Cmd.CommandLine.front()); 440b57cec5SDimitry Andric } 450b57cec5SDimitry Andric return Cmds; 460b57cec5SDimitry Andric } 470b57cec5SDimitry Andric std::unique_ptr<CompilationDatabase> Base; 480b57cec5SDimitry Andric }; 490b57cec5SDimitry Andric } // namespace 500b57cec5SDimitry Andric 510b57cec5SDimitry Andric std::unique_ptr<CompilationDatabase> inferTargetAndDriverMode(std::unique_ptr<CompilationDatabase> Base)520b57cec5SDimitry AndricinferTargetAndDriverMode(std::unique_ptr<CompilationDatabase> Base) { 53*a7dea167SDimitry Andric return std::make_unique<TargetAndModeAdderDatabase>(std::move(Base)); 540b57cec5SDimitry Andric } 550b57cec5SDimitry Andric 560b57cec5SDimitry Andric } // namespace tooling 570b57cec5SDimitry Andric } // namespace clang 58