From 461a67fa15370a9ec88f8f8a240bf7c123bb2029 Mon Sep 17 00:00:00 2001 From: Dimitry Andric Date: Mon, 18 Dec 2017 20:11:37 +0000 Subject: Vendor import of clang trunk r321017: https://llvm.org/svn/llvm-project/cfe/trunk@321017 --- lib/Tooling/CompilationDatabase.cpp | 53 ++++++++++++++++++++++++++++++------- 1 file changed, 43 insertions(+), 10 deletions(-) (limited to 'lib/Tooling/CompilationDatabase.cpp') diff --git a/lib/Tooling/CompilationDatabase.cpp b/lib/Tooling/CompilationDatabase.cpp index 0e835579e04ed..92b76b157dcb0 100644 --- a/lib/Tooling/CompilationDatabase.cpp +++ b/lib/Tooling/CompilationDatabase.cpp @@ -10,6 +10,9 @@ // This file contains implementations of the CompilationDatabase base class // and the FixedCompilationDatabase. // +// FIXME: Various functions that take a string &ErrorMessage should be upgraded +// to Expected. +// //===----------------------------------------------------------------------===// #include "clang/Tooling/CompilationDatabase.h" @@ -26,6 +29,7 @@ #include "llvm/ADT/SmallString.h" #include "llvm/Option/Arg.h" #include "llvm/Support/Host.h" +#include "llvm/Support/LineIterator.h" #include "llvm/Support/Path.h" #include "llvm/Support/raw_ostream.h" #include @@ -108,6 +112,15 @@ CompilationDatabase::autoDetectFromDirectory(StringRef SourceDir, return DB; } +std::vector CompilationDatabase::getAllCompileCommands() const { + std::vector Result; + for (const auto &File : getAllFiles()) { + auto C = getCompileCommands(File); + std::move(C.begin(), C.end(), std::back_inserter(Result)); + } + return Result; +} + CompilationDatabasePlugin::~CompilationDatabasePlugin() {} namespace { @@ -302,8 +315,22 @@ FixedCompilationDatabase::loadFromCommandLine(int &Argc, std::vector StrippedArgs; if (!stripPositionalArgs(CommandLine, StrippedArgs, ErrorMsg)) return nullptr; - return std::unique_ptr( - new FixedCompilationDatabase(Directory, StrippedArgs)); + return llvm::make_unique(Directory, StrippedArgs); +} + +std::unique_ptr +FixedCompilationDatabase::loadFromFile(StringRef Path, std::string &ErrorMsg) { + ErrorMsg.clear(); + llvm::ErrorOr> File = + llvm::MemoryBuffer::getFile(Path); + if (std::error_code Result = File.getError()) { + ErrorMsg = "Error while opening fixed database: " + Result.message(); + return nullptr; + } + std::vector Args{llvm::line_iterator(**File), + llvm::line_iterator()}; + return llvm::make_unique( + llvm::sys::path::parent_path(Path), std::move(Args)); } FixedCompilationDatabase:: @@ -324,15 +351,21 @@ FixedCompilationDatabase::getCompileCommands(StringRef FilePath) const { return Result; } -std::vector -FixedCompilationDatabase::getAllFiles() const { - return std::vector(); -} +namespace { -std::vector -FixedCompilationDatabase::getAllCompileCommands() const { - return std::vector(); -} +class FixedCompilationDatabasePlugin : public CompilationDatabasePlugin { + std::unique_ptr + loadFromDirectory(StringRef Directory, std::string &ErrorMessage) override { + SmallString<1024> DatabasePath(Directory); + llvm::sys::path::append(DatabasePath, "compile_flags.txt"); + return FixedCompilationDatabase::loadFromFile(DatabasePath, ErrorMessage); + } +}; + +static CompilationDatabasePluginRegistry::Add +X("fixed-compilation-database", "Reads plain-text flags file"); + +} // namespace namespace clang { namespace tooling { -- cgit v1.3