Path: blob/main/contrib/llvm-project/clang/lib/Tooling/LocateToolCompilationDatabase.cpp
35234 views
//===- GuessTargetAndModeCompilationDatabase.cpp --------------------------===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//78#include "clang/Tooling/CompilationDatabase.h"9#include "clang/Tooling/Tooling.h"10#include "llvm/Support/Path.h"11#include "llvm/Support/Program.h"12#include <memory>1314namespace clang {15namespace tooling {1617namespace {18class LocationAdderDatabase : public CompilationDatabase {19public:20LocationAdderDatabase(std::unique_ptr<CompilationDatabase> Base)21: Base(std::move(Base)) {22assert(this->Base != nullptr);23}2425std::vector<std::string> getAllFiles() const override {26return Base->getAllFiles();27}2829std::vector<CompileCommand> getAllCompileCommands() const override {30return addLocation(Base->getAllCompileCommands());31}3233std::vector<CompileCommand>34getCompileCommands(StringRef FilePath) const override {35return addLocation(Base->getCompileCommands(FilePath));36}3738private:39std::vector<CompileCommand>40addLocation(std::vector<CompileCommand> Cmds) const {41for (auto &Cmd : Cmds) {42if (Cmd.CommandLine.empty())43continue;44std::string &Driver = Cmd.CommandLine.front();45// If the driver name already is absolute, we don't need to do anything.46if (llvm::sys::path::is_absolute(Driver))47continue;48// If the name is a relative path, like bin/clang, we assume it's49// possible to resolve it and don't do anything about it either.50if (llvm::any_of(Driver,51[](char C) { return llvm::sys::path::is_separator(C); }))52continue;53auto Absolute = llvm::sys::findProgramByName(Driver);54// If we found it in path, update the entry in Cmd.CommandLine55if (Absolute && llvm::sys::path::is_absolute(*Absolute))56Driver = std::move(*Absolute);57}58return Cmds;59}60std::unique_ptr<CompilationDatabase> Base;61};62} // namespace6364std::unique_ptr<CompilationDatabase>65inferToolLocation(std::unique_ptr<CompilationDatabase> Base) {66return std::make_unique<LocationAdderDatabase>(std::move(Base));67}6869} // namespace tooling70} // namespace clang717273