From 60b801619742af8bb5a9c949d6a979e8f48be43b Mon Sep 17 00:00:00 2001 From: Manuel Klimek Date: Fri, 13 Jul 2012 12:31:45 +0000 Subject: [PATCH] Allows retrieving all files in a CompilationDatabase. Patch by Tobias Koenig, some test changes by myself. llvm-svn: 160167 --- .../clang/Tooling/CompilationDatabase.h | 13 +++++++ clang/lib/Tooling/CompilationDatabase.cpp | 20 ++++++++++ .../Tooling/CompilationDatabaseTest.cpp | 38 +++++++++++++++++++ 3 files changed, 71 insertions(+) diff --git a/clang/include/clang/Tooling/CompilationDatabase.h b/clang/include/clang/Tooling/CompilationDatabase.h index 143c65e313a4..f78ffaed284c 100644 --- a/clang/include/clang/Tooling/CompilationDatabase.h +++ b/clang/include/clang/Tooling/CompilationDatabase.h @@ -106,6 +106,9 @@ public: /// lines for a.cc and b.cc and only the first command line for t.cc. virtual std::vector getCompileCommands( StringRef FilePath) const = 0; + + /// \brief Returns the list of all files available in the compilation database. + virtual std::vector getAllFiles() const = 0; }; /// \brief A compilation database that returns a single compile command line. @@ -155,6 +158,11 @@ public: virtual std::vector getCompileCommands( StringRef FilePath) const; + /// \brief Returns the list of all files available in the compilation database. + /// + /// Note: This is always an empty list for the fixed compilation database. + virtual std::vector getAllFiles() const; + private: /// This is built up to contain a single entry vector to be returned from /// getCompileCommands after adding the positional argument. @@ -201,6 +209,11 @@ public: virtual std::vector getCompileCommands( StringRef FilePath) const; + /// \brief Returns the list of all files available in the compilation database. + /// + /// These are the 'file' entries of the JSON objects. + virtual std::vector getAllFiles() const; + private: /// \brief Constructs a JSON compilation database on a memory buffer. JSONCompilationDatabase(llvm::MemoryBuffer *Database) diff --git a/clang/lib/Tooling/CompilationDatabase.cpp b/clang/lib/Tooling/CompilationDatabase.cpp index d54a5e79ebdc..802a4c3ea45a 100644 --- a/clang/lib/Tooling/CompilationDatabase.cpp +++ b/clang/lib/Tooling/CompilationDatabase.cpp @@ -199,6 +199,11 @@ FixedCompilationDatabase::getCompileCommands(StringRef FilePath) const { return Result; } +std::vector +FixedCompilationDatabase::getAllFiles() const { + return std::vector(); +} + JSONCompilationDatabase * JSONCompilationDatabase::loadFromFile(StringRef FilePath, std::string &ErrorMessage) { @@ -249,6 +254,21 @@ JSONCompilationDatabase::getCompileCommands(StringRef FilePath) const { return Commands; } +std::vector +JSONCompilationDatabase::getAllFiles() const { + std::vector Result; + + llvm::StringMap< std::vector >::const_iterator + CommandsRefI = IndexByFile.begin(); + const llvm::StringMap< std::vector >::const_iterator + CommandsRefEnd = IndexByFile.end(); + for (; CommandsRefI != CommandsRefEnd; ++CommandsRefI) { + Result.push_back(CommandsRefI->first().str()); + } + + return Result; +} + bool JSONCompilationDatabase::parse(std::string &ErrorMessage) { llvm::yaml::document_iterator I = YAMLStream.begin(); if (I == YAMLStream.end()) { diff --git a/clang/unittests/Tooling/CompilationDatabaseTest.cpp b/clang/unittests/Tooling/CompilationDatabaseTest.cpp index 7753c154100f..591d48dbbd61 100644 --- a/clang/unittests/Tooling/CompilationDatabaseTest.cpp +++ b/clang/unittests/Tooling/CompilationDatabaseTest.cpp @@ -38,6 +38,35 @@ TEST(JSONCompilationDatabase, ErrsOnInvalidFormat) { expectFailure("[{\"command\":\"\",\"file\":\"\"}]", "Missing directory"); } +static std::vector getAllFiles(StringRef JSONDatabase, + std::string &ErrorMessage) { + llvm::OwningPtr Database( + JSONCompilationDatabase::loadFromBuffer(JSONDatabase, ErrorMessage)); + if (!Database) { + ADD_FAILURE() << ErrorMessage; + return std::vector(); + } + return Database->getAllFiles(); +} + +TEST(JSONCompilationDatabase, GetAllFiles) { + std::string ErrorMessage; + EXPECT_EQ(std::vector(), + getAllFiles("[]", ErrorMessage)) << ErrorMessage; + + std::vector expected_files; + expected_files.push_back("file1"); + expected_files.push_back("file2"); + EXPECT_EQ(expected_files, getAllFiles( + "[{\"directory\":\"dir\"," + "\"command\":\"command\"," + "\"file\":\"file1\"}," + " {\"directory\":\"dir\"," + "\"command\":\"command\"," + "\"file\":\"file2\"}]", + ErrorMessage)) << ErrorMessage; +} + static CompileCommand findCompileArgsInJsonDatabase(StringRef FileName, StringRef JSONDatabase, std::string &ErrorMessage) { @@ -255,6 +284,15 @@ TEST(FixedCompilationDatabase, ReturnsFixedCommandLine) { EXPECT_EQ(ExpectedCommandLine, Result[0].CommandLine); } +TEST(FixedCompilationDatabase, GetAllFiles) { + std::vector CommandLine; + CommandLine.push_back("one"); + CommandLine.push_back("two"); + FixedCompilationDatabase Database(".", CommandLine); + + EXPECT_EQ(0ul, Database.getAllFiles().size()); +} + TEST(ParseFixedCompilationDatabase, ReturnsNullOnEmptyArgumentList) { int Argc = 0; llvm::OwningPtr Database(