[cfe-commits] r160167 - in /cfe/trunk: include/clang/Tooling/CompilationDatabase.h lib/Tooling/CompilationDatabase.cpp unittests/Tooling/CompilationDatabaseTest.cpp

Manuel Klimek klimek at google.com
Fri Jul 13 05:31:46 PDT 2012


Author: klimek
Date: Fri Jul 13 07:31:45 2012
New Revision: 160167

URL: http://llvm.org/viewvc/llvm-project?rev=160167&view=rev
Log:
Allows retrieving all files in a CompilationDatabase.

Patch by Tobias Koenig, some test changes by myself.


Modified:
    cfe/trunk/include/clang/Tooling/CompilationDatabase.h
    cfe/trunk/lib/Tooling/CompilationDatabase.cpp
    cfe/trunk/unittests/Tooling/CompilationDatabaseTest.cpp

Modified: cfe/trunk/include/clang/Tooling/CompilationDatabase.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Tooling/CompilationDatabase.h?rev=160167&r1=160166&r2=160167&view=diff
==============================================================================
--- cfe/trunk/include/clang/Tooling/CompilationDatabase.h (original)
+++ cfe/trunk/include/clang/Tooling/CompilationDatabase.h Fri Jul 13 07:31:45 2012
@@ -106,6 +106,9 @@
   /// lines for a.cc and b.cc and only the first command line for t.cc.
   virtual std::vector<CompileCommand> getCompileCommands(
     StringRef FilePath) const = 0;
+
+  /// \brief Returns the list of all files available in the compilation database.
+  virtual std::vector<std::string> getAllFiles() const = 0;
 };
 
 /// \brief A compilation database that returns a single compile command line.
@@ -155,6 +158,11 @@
   virtual std::vector<CompileCommand> 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<std::string> 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 @@
   virtual std::vector<CompileCommand> 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<std::string> getAllFiles() const;
+
 private:
   /// \brief Constructs a JSON compilation database on a memory buffer.
   JSONCompilationDatabase(llvm::MemoryBuffer *Database)

Modified: cfe/trunk/lib/Tooling/CompilationDatabase.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Tooling/CompilationDatabase.cpp?rev=160167&r1=160166&r2=160167&view=diff
==============================================================================
--- cfe/trunk/lib/Tooling/CompilationDatabase.cpp (original)
+++ cfe/trunk/lib/Tooling/CompilationDatabase.cpp Fri Jul 13 07:31:45 2012
@@ -199,6 +199,11 @@
   return Result;
 }
 
+std::vector<std::string>
+FixedCompilationDatabase::getAllFiles() const {
+  return std::vector<std::string>();
+}
+
 JSONCompilationDatabase *
 JSONCompilationDatabase::loadFromFile(StringRef FilePath,
                                       std::string &ErrorMessage) {
@@ -249,6 +254,21 @@
   return Commands;
 }
 
+std::vector<std::string>
+JSONCompilationDatabase::getAllFiles() const {
+  std::vector<std::string> Result;
+
+  llvm::StringMap< std::vector<CompileCommandRef> >::const_iterator
+    CommandsRefI = IndexByFile.begin();
+  const llvm::StringMap< std::vector<CompileCommandRef> >::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()) {

Modified: cfe/trunk/unittests/Tooling/CompilationDatabaseTest.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/unittests/Tooling/CompilationDatabaseTest.cpp?rev=160167&r1=160166&r2=160167&view=diff
==============================================================================
--- cfe/trunk/unittests/Tooling/CompilationDatabaseTest.cpp (original)
+++ cfe/trunk/unittests/Tooling/CompilationDatabaseTest.cpp Fri Jul 13 07:31:45 2012
@@ -38,6 +38,35 @@
   expectFailure("[{\"command\":\"\",\"file\":\"\"}]", "Missing directory");
 }
 
+static std::vector<std::string> getAllFiles(StringRef JSONDatabase,
+                                            std::string &ErrorMessage) {
+  llvm::OwningPtr<CompilationDatabase> Database(
+      JSONCompilationDatabase::loadFromBuffer(JSONDatabase, ErrorMessage));
+  if (!Database) {
+    ADD_FAILURE() << ErrorMessage;
+    return std::vector<std::string>();
+  }
+  return Database->getAllFiles();
+}
+
+TEST(JSONCompilationDatabase, GetAllFiles) {
+  std::string ErrorMessage;
+  EXPECT_EQ(std::vector<std::string>(),
+            getAllFiles("[]", ErrorMessage)) << ErrorMessage;
+
+  std::vector<std::string> 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 @@
   EXPECT_EQ(ExpectedCommandLine, Result[0].CommandLine);
 }
 
+TEST(FixedCompilationDatabase, GetAllFiles) {
+  std::vector<std::string> 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<FixedCompilationDatabase> Database(





More information about the cfe-commits mailing list