From 60d74e45888e2bab27bb003193e22acbc754c67a Mon Sep 17 00:00:00 2001 From: Sam McCall Date: Thu, 9 Nov 2017 10:37:39 +0000 Subject: [Tooling] Use FixedCompilationDatabase when `compile_flags.txt` is found. Summary: This is an alternative to JSONCompilationDatabase for simple projects that don't use a build system such as CMake. (You can also drop one in ~, to make your tools use e.g. C++11 by default) There's no facility for varying flags per-source-file or per-machine. Possibly this could be accommodated backwards-compatibly using cpp, but even if not the simplicity seems worthwhile for the cases that are addressed. Tested with clangd, works great! (requires clangd restart) Reviewers: klimek Subscribers: ilya-biryukov, cfe-commits Differential Revision: https://reviews.llvm.org/D39799 llvm-svn: 317777 --- clang/lib/Tooling/CompilationDatabase.cpp | 38 +++++++++++++++++++++++++++++-- 1 file changed, 36 insertions(+), 2 deletions(-) (limited to 'clang/lib/Tooling/CompilationDatabase.cpp') diff --git a/clang/lib/Tooling/CompilationDatabase.cpp b/clang/lib/Tooling/CompilationDatabase.cpp index 0e83557..f252ef0 100644 --- a/clang/lib/Tooling/CompilationDatabase.cpp +++ b/clang/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 @@ -302,8 +306,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:: @@ -334,6 +352,22 @@ FixedCompilationDatabase::getAllCompileCommands() const { return std::vector(); } +namespace { + +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.1