diff --git a/clang/include/clang/Tooling/CompilationDatabase.h b/clang/include/clang/Tooling/CompilationDatabase.h --- a/clang/include/clang/Tooling/CompilationDatabase.h +++ b/clang/include/clang/Tooling/CompilationDatabase.h @@ -184,11 +184,16 @@ int &Argc, const char *const *Argv, std::string &ErrorMsg, Twine Directory = "."); - /// Reads flags from the given file, one-per line. + /// Reads flags from the given file, one-per-line. /// Returns nullptr and sets ErrorMessage if we can't read the file. static std::unique_ptr loadFromFile(StringRef Path, std::string &ErrorMsg); + /// Reads flags from the given buffer, one-per-line. + /// Directory is the command CWD, typically the parent of compile_flags.txt. + static std::unique_ptr + loadFromBuffer(StringRef Directory, StringRef Data, std::string &ErrorMsg); + /// Constructs a compilation data base from a specified directory /// and command line. FixedCompilationDatabase(Twine Directory, ArrayRef CommandLine); diff --git a/clang/lib/Tooling/CompilationDatabase.cpp b/clang/lib/Tooling/CompilationDatabase.cpp --- a/clang/lib/Tooling/CompilationDatabase.cpp +++ b/clang/lib/Tooling/CompilationDatabase.cpp @@ -348,16 +348,24 @@ ErrorMsg = "Error while opening fixed database: " + Result.message(); return nullptr; } + return loadFromBuffer(llvm::sys::path::parent_path(Path), + (*File)->getBuffer(), ErrorMsg); +} + +std::unique_ptr +FixedCompilationDatabase::loadFromBuffer(StringRef Directory, StringRef Data, + std::string &ErrorMsg) { + ErrorMsg.clear(); std::vector Args; - for (llvm::StringRef Line : - llvm::make_range(llvm::line_iterator(**File), llvm::line_iterator())) { + StringRef Line; + while (!Data.empty()) { + std::tie(Line, Data) = Data.split('\n'); // Stray whitespace is almost certainly unintended. Line = Line.trim(); if (!Line.empty()) Args.push_back(Line.str()); } - return std::make_unique( - llvm::sys::path::parent_path(Path), std::move(Args)); + return std::make_unique(Directory, std::move(Args)); } FixedCompilationDatabase:: diff --git a/clang/unittests/Tooling/CompilationDatabaseTest.cpp b/clang/unittests/Tooling/CompilationDatabaseTest.cpp --- a/clang/unittests/Tooling/CompilationDatabaseTest.cpp +++ b/clang/unittests/Tooling/CompilationDatabaseTest.cpp @@ -543,6 +543,27 @@ EXPECT_EQ(0ul, Database.getAllCompileCommands().size()); } +TEST(FixedCompilationDatabase, FromBuffer) { + const char *Data = R"( + + -DFOO=BAR + +--baz + + )"; + std::string ErrorMsg; + auto CDB = + FixedCompilationDatabase::loadFromBuffer("/cdb/dir", Data, ErrorMsg); + + std::vector Result = CDB->getCompileCommands("/foo/bar.cc"); + ASSERT_EQ(1ul, Result.size()); + EXPECT_EQ("/cdb/dir", Result.front().Directory); + EXPECT_EQ("/foo/bar.cc", Result.front().Filename); + EXPECT_THAT( + Result.front().CommandLine, + ElementsAre(EndsWith("clang-tool"), "-DFOO=BAR", "--baz", "/foo/bar.cc")); +} + TEST(ParseFixedCompilationDatabase, ReturnsNullOnEmptyArgumentList) { int Argc = 0; std::string ErrorMsg;