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,15 @@ 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. + static std::unique_ptr + loadFromBuffer(StringRef Path, 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,9 +348,17 @@ ErrorMsg = "Error while opening fixed database: " + Result.message(); return nullptr; } + return loadFromBuffer(Path, (*File)->getBuffer(), ErrorMsg); +} + +std::unique_ptr +FixedCompilationDatabase::loadFromBuffer(StringRef Path, 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()) 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 @@ -541,6 +541,27 @@ EXPECT_EQ(0ul, Database.getAllCompileCommands().size()); } +TEST(FixedCompilationDatabase, FromBuffer) { + const char *Data = R"( + + -DFOO=BAR + +--baz + + )"; + std::string ErrorMsg; + auto CDB = FixedCompilationDatabase::loadFromBuffer("/path/compile_flags.txt", + Data, ErrorMsg); + + std::vector Result = CDB->getCompileCommands("/foo/bar.cc"); + ASSERT_EQ(1ul, Result.size()); + EXPECT_EQ("/path", 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;