[cfe-commits] r169226 - in /cfe/trunk: include/clang-c/CXCompilationDatabase.h include/clang-c/Index.h include/clang/Tooling/CompilationDatabase.h include/clang/Tooling/JSONCompilationDatabase.h lib/Tooling/CompilationDatabase.cpp lib/Tooling/JSONCompilationDatabase.cpp tools/libclang/CXCompilationDatabase.cpp tools/libclang/libclang.exports unittests/Tooling/CompilationDatabaseTest.cpp

Argyrios Kyrtzidis akyrtzi at gmail.com
Mon Dec 3 23:26:44 PST 2012


Author: akirtzidis
Date: Tue Dec  4 01:26:44 2012
New Revision: 169226

URL: http://llvm.org/viewvc/llvm-project?rev=169226&view=rev
Log:
Introduce CompilationDatabase::getAllCompileCommands() that returns all
compile commands of the database and expose it via the libclang API.

Modified:
    cfe/trunk/include/clang-c/CXCompilationDatabase.h
    cfe/trunk/include/clang-c/Index.h
    cfe/trunk/include/clang/Tooling/CompilationDatabase.h
    cfe/trunk/include/clang/Tooling/JSONCompilationDatabase.h
    cfe/trunk/lib/Tooling/CompilationDatabase.cpp
    cfe/trunk/lib/Tooling/JSONCompilationDatabase.cpp
    cfe/trunk/tools/libclang/CXCompilationDatabase.cpp
    cfe/trunk/tools/libclang/libclang.exports
    cfe/trunk/unittests/Tooling/CompilationDatabaseTest.cpp

Modified: cfe/trunk/include/clang-c/CXCompilationDatabase.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang-c/CXCompilationDatabase.h?rev=169226&r1=169225&r2=169226&view=diff
==============================================================================
--- cfe/trunk/include/clang-c/CXCompilationDatabase.h (original)
+++ cfe/trunk/include/clang-c/CXCompilationDatabase.h Tue Dec  4 01:26:44 2012
@@ -95,6 +95,12 @@
                                              const char *CompleteFileName);
 
 /**
+ * \brief Get all the compile commands in the given compilation database.
+ */
+CINDEX_LINKAGE CXCompileCommands
+clang_CompilationDatabase_getAllCompileCommands(CXCompilationDatabase);
+
+/**
  * \brief Free the given CompileCommands
  */
 CINDEX_LINKAGE void clang_CompileCommands_dispose(CXCompileCommands);

Modified: cfe/trunk/include/clang-c/Index.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang-c/Index.h?rev=169226&r1=169225&r2=169226&view=diff
==============================================================================
--- cfe/trunk/include/clang-c/Index.h (original)
+++ cfe/trunk/include/clang-c/Index.h Tue Dec  4 01:26:44 2012
@@ -32,7 +32,7 @@
  * compatible, thus CINDEX_VERSION_MAJOR is expected to remain stable.
  */
 #define CINDEX_VERSION_MAJOR 0
-#define CINDEX_VERSION_MINOR 6
+#define CINDEX_VERSION_MINOR 7
 
 #define CINDEX_VERSION_ENCODE(major, minor) ( \
       ((major) * 10000)                       \

Modified: cfe/trunk/include/clang/Tooling/CompilationDatabase.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Tooling/CompilationDatabase.h?rev=169226&r1=169225&r2=169226&view=diff
==============================================================================
--- cfe/trunk/include/clang/Tooling/CompilationDatabase.h (original)
+++ cfe/trunk/include/clang/Tooling/CompilationDatabase.h Tue Dec  4 01:26:44 2012
@@ -106,6 +106,10 @@
 
   /// \brief Returns the list of all files available in the compilation database.
   virtual std::vector<std::string> getAllFiles() const = 0;
+
+  /// \brief Returns all compile commands for all the files in the compilation
+  /// database.
+  virtual std::vector<CompileCommand> getAllCompileCommands() const = 0;
 };
 
 /// \brief Interface for compilation database plugins.
@@ -181,6 +185,12 @@
   /// Note: This is always an empty list for the fixed compilation database.
   virtual std::vector<std::string> getAllFiles() const;
 
+  /// \brief Returns all compile commands for all the files in the compilation
+  /// database.
+  ///
+  /// Note: This is always an empty list for the fixed compilation database.
+  virtual std::vector<CompileCommand> getAllCompileCommands() const;
+
 private:
   /// This is built up to contain a single entry vector to be returned from
   /// getCompileCommands after adding the positional argument.

Modified: cfe/trunk/include/clang/Tooling/JSONCompilationDatabase.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Tooling/JSONCompilationDatabase.h?rev=169226&r1=169225&r2=169226&view=diff
==============================================================================
--- cfe/trunk/include/clang/Tooling/JSONCompilationDatabase.h (original)
+++ cfe/trunk/include/clang/Tooling/JSONCompilationDatabase.h Tue Dec  4 01:26:44 2012
@@ -75,6 +75,10 @@
   /// These are the 'file' entries of the JSON objects.
   virtual std::vector<std::string> getAllFiles() const;
 
+  /// \brief Returns all compile commands for all the files in the compilation
+  /// database.
+  virtual std::vector<CompileCommand> getAllCompileCommands() const;
+
 private:
   /// \brief Constructs a JSON compilation database on a memory buffer.
   JSONCompilationDatabase(llvm::MemoryBuffer *Database)
@@ -91,6 +95,10 @@
   typedef std::pair<llvm::yaml::ScalarNode*,
                     llvm::yaml::ScalarNode*> CompileCommandRef;
 
+  /// \brief Converts the given array of CompileCommandRefs to CompileCommands.
+  void getCommands(ArrayRef<CompileCommandRef> CommandsRef,
+                   std::vector<CompileCommand> &Commands) const;
+
   // Maps file paths to the compile command lines for that file.
   llvm::StringMap< std::vector<CompileCommandRef> > IndexByFile;
 

Modified: cfe/trunk/lib/Tooling/CompilationDatabase.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Tooling/CompilationDatabase.cpp?rev=169226&r1=169225&r2=169226&view=diff
==============================================================================
--- cfe/trunk/lib/Tooling/CompilationDatabase.cpp (original)
+++ cfe/trunk/lib/Tooling/CompilationDatabase.cpp Tue Dec  4 01:26:44 2012
@@ -132,6 +132,11 @@
   return std::vector<std::string>();
 }
 
+std::vector<CompileCommand>
+FixedCompilationDatabase::getAllCompileCommands() const {
+  return std::vector<CompileCommand>();
+}
+
 // This anchor is used to force the linker to link in the generated object file
 // and thus register the JSONCompilationDatabasePlugin.
 extern volatile int JSONAnchorSource;

Modified: cfe/trunk/lib/Tooling/JSONCompilationDatabase.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Tooling/JSONCompilationDatabase.cpp?rev=169226&r1=169225&r2=169226&view=diff
==============================================================================
--- cfe/trunk/lib/Tooling/JSONCompilationDatabase.cpp (original)
+++ cfe/trunk/lib/Tooling/JSONCompilationDatabase.cpp Tue Dec  4 01:26:44 2012
@@ -178,16 +178,8 @@
     CommandsRefI = IndexByFile.find(Match);
   if (CommandsRefI == IndexByFile.end())
     return std::vector<CompileCommand>();
-  const std::vector<CompileCommandRef> &CommandsRef = CommandsRefI->getValue();
   std::vector<CompileCommand> Commands;
-  for (int I = 0, E = CommandsRef.size(); I != E; ++I) {
-    llvm::SmallString<8> DirectoryStorage;
-    llvm::SmallString<1024> CommandStorage;
-    Commands.push_back(CompileCommand(
-      // FIXME: Escape correctly:
-      CommandsRef[I].first->getValue(DirectoryStorage),
-      unescapeCommandLine(CommandsRef[I].second->getValue(CommandStorage))));
-  }
+  getCommands(CommandsRefI->getValue(), Commands);
   return Commands;
 }
 
@@ -206,6 +198,30 @@
   return Result;
 }
 
+std::vector<CompileCommand>
+JSONCompilationDatabase::getAllCompileCommands() const {
+  std::vector<CompileCommand> Commands;
+  for (llvm::StringMap< std::vector<CompileCommandRef> >::const_iterator
+        CommandsRefI = IndexByFile.begin(), CommandsRefEnd = IndexByFile.end();
+      CommandsRefI != CommandsRefEnd; ++CommandsRefI) {
+    getCommands(CommandsRefI->getValue(), Commands);
+  }
+  return Commands;
+}
+
+void JSONCompilationDatabase::getCommands(
+                                  ArrayRef<CompileCommandRef> CommandsRef,
+                                  std::vector<CompileCommand> &Commands) const {
+  for (int I = 0, E = CommandsRef.size(); I != E; ++I) {
+    llvm::SmallString<8> DirectoryStorage;
+    llvm::SmallString<1024> CommandStorage;
+    Commands.push_back(CompileCommand(
+      // FIXME: Escape correctly:
+      CommandsRef[I].first->getValue(DirectoryStorage),
+      unescapeCommandLine(CommandsRef[I].second->getValue(CommandStorage))));
+  }
+}
+
 bool JSONCompilationDatabase::parse(std::string &ErrorMessage) {
   llvm::yaml::document_iterator I = YAMLStream.begin();
   if (I == YAMLStream.end()) {

Modified: cfe/trunk/tools/libclang/CXCompilationDatabase.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/tools/libclang/CXCompilationDatabase.cpp?rev=169226&r1=169225&r2=169226&view=diff
==============================================================================
--- cfe/trunk/tools/libclang/CXCompilationDatabase.cpp (original)
+++ cfe/trunk/tools/libclang/CXCompilationDatabase.cpp Tue Dec  4 01:26:44 2012
@@ -59,6 +59,17 @@
   return 0;
 }
 
+CXCompileCommands
+clang_CompilationDatabase_getAllCompileCommands(CXCompilationDatabase CDb) {
+  if (CompilationDatabase *db = static_cast<CompilationDatabase *>(CDb)) {
+    const std::vector<CompileCommand> CCmd(db->getAllCompileCommands());
+    if (!CCmd.empty())
+      return new AllocatedCXCompileCommands( CCmd );
+  }
+
+  return 0;
+}
+
 void
 clang_CompileCommands_dispose(CXCompileCommands Cmds)
 {

Modified: cfe/trunk/tools/libclang/libclang.exports
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/tools/libclang/libclang.exports?rev=169226&r1=169225&r2=169226&view=diff
==============================================================================
--- cfe/trunk/tools/libclang/libclang.exports (original)
+++ cfe/trunk/tools/libclang/libclang.exports Tue Dec  4 01:26:44 2012
@@ -250,6 +250,7 @@
 clang_CompilationDatabase_fromDirectory
 clang_CompilationDatabase_dispose
 clang_CompilationDatabase_getCompileCommands
+clang_CompilationDatabase_getAllCompileCommands
 clang_CompileCommands_dispose
 clang_CompileCommands_getSize
 clang_CompileCommands_getCommand

Modified: cfe/trunk/unittests/Tooling/CompilationDatabaseTest.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/unittests/Tooling/CompilationDatabaseTest.cpp?rev=169226&r1=169225&r2=169226&view=diff
==============================================================================
--- cfe/trunk/unittests/Tooling/CompilationDatabaseTest.cpp (original)
+++ cfe/trunk/unittests/Tooling/CompilationDatabaseTest.cpp Tue Dec  4 01:26:44 2012
@@ -51,6 +51,17 @@
   return Database->getAllFiles();
 }
 
+static std::vector<CompileCommand> getAllCompileCommands(StringRef JSONDatabase,
+                                                    std::string &ErrorMessage) {
+  llvm::OwningPtr<CompilationDatabase> Database(
+      JSONCompilationDatabase::loadFromBuffer(JSONDatabase, ErrorMessage));
+  if (!Database) {
+    ADD_FAILURE() << ErrorMessage;
+    return std::vector<CompileCommand>();
+  }
+  return Database->getAllCompileCommands();
+}
+
 TEST(JSONCompilationDatabase, GetAllFiles) {
   std::string ErrorMessage;
   EXPECT_EQ(std::vector<std::string>(),
@@ -72,6 +83,35 @@
     ErrorMessage)) << ErrorMessage;
 }
 
+TEST(JSONCompilationDatabase, GetAllCompileCommands) {
+  std::string ErrorMessage;
+  EXPECT_EQ(0u,
+            getAllCompileCommands("[]", ErrorMessage).size()) << ErrorMessage;
+
+  StringRef Directory1("//net/dir1");
+  StringRef FileName1("file1");
+  StringRef Command1("command1");
+  StringRef Directory2("//net/dir2");
+  StringRef FileName2("file1");
+  StringRef Command2("command1");
+
+  std::vector<CompileCommand> Commands = getAllCompileCommands(
+      ("[{\"directory\":\"" + Directory1 + "\"," +
+             "\"command\":\"" + Command1 + "\","
+             "\"file\":\"" + FileName1 + "\"},"
+       " {\"directory\":\"" + Directory2 + "\"," +
+             "\"command\":\"" + Command2 + "\","
+             "\"file\":\"" + FileName2 + "\"}]").str(),
+      ErrorMessage);
+  EXPECT_EQ(2U, Commands.size()) << ErrorMessage;
+  EXPECT_EQ(Directory1, Commands[0].Directory) << ErrorMessage;
+  ASSERT_EQ(1u, Commands[0].CommandLine.size());
+  EXPECT_EQ(Command1, Commands[0].CommandLine[0]) << ErrorMessage;
+  EXPECT_EQ(Directory2, Commands[1].Directory) << ErrorMessage;
+  ASSERT_EQ(1u, Commands[1].CommandLine.size());
+  EXPECT_EQ(Command2, Commands[1].CommandLine[0]) << ErrorMessage;
+}
+
 static CompileCommand findCompileArgsInJsonDatabase(StringRef FileName,
                                                     StringRef JSONDatabase,
                                                     std::string &ErrorMessage) {
@@ -376,6 +416,15 @@
   EXPECT_EQ(0ul, Database.getAllFiles().size());
 }
 
+TEST(FixedCompilationDatabase, GetAllCompileCommands) {
+  std::vector<std::string> CommandLine;
+  CommandLine.push_back("one");
+  CommandLine.push_back("two");
+  FixedCompilationDatabase Database(".", CommandLine);
+
+  EXPECT_EQ(0ul, Database.getAllCompileCommands().size());
+}
+
 TEST(ParseFixedCompilationDatabase, ReturnsNullOnEmptyArgumentList) {
   int Argc = 0;
   llvm::OwningPtr<FixedCompilationDatabase> Database(





More information about the cfe-commits mailing list