]> granicus.if.org Git - clang/commitdiff
[tooling] Provide the compile commands of the JSON database in the order that they...
authorArgyrios Kyrtzidis <akyrtzi@gmail.com>
Tue, 22 Sep 2015 17:22:33 +0000 (17:22 +0000)
committerArgyrios Kyrtzidis <akyrtzi@gmail.com>
Tue, 22 Sep 2015 17:22:33 +0000 (17:22 +0000)
This is useful for debugging of issues and reduction of test cases.
For example, an issue may show up due to the order that some commands were processed.
It is convenient to be able to remove commands from the file and still preserve the order
that they are returned, instead of getting a completely different order when removing a few commands.

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@248292 91177308-0d34-0410-b5e6-96231b3b80d8

include/clang/Tooling/JSONCompilationDatabase.h
lib/Tooling/JSONCompilationDatabase.cpp
unittests/Tooling/CompilationDatabaseTest.cpp

index 0e6c893969da002fdae203ffa1e3cbbfb1ea0b5c..2a13fc155cead0e8382be23a9d2f3951d67fb476 100644 (file)
@@ -116,6 +116,10 @@ private:
   // Maps file paths to the compile command lines for that file.
   llvm::StringMap<std::vector<CompileCommandRef>> IndexByFile;
 
+  /// All the compile commands in the order that they were provided in the
+  /// JSON stream.
+  std::vector<CompileCommandRef> AllCommands;
+
   FileMatchTrie MatchTrie;
 
   std::unique_ptr<llvm::MemoryBuffer> Database;
index dd4d7a8f08364e18fbc844ac9bc0f52cfc58cbc3..299fbdc149bfb2fefe538ee64c9062b3913d282b 100644 (file)
@@ -206,11 +206,7 @@ JSONCompilationDatabase::getAllFiles() const {
 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);
-  }
+  getCommands(AllCommands, Commands);
   return Commands;
 }
 
@@ -337,8 +333,9 @@ bool JSONCompilationDatabase::parse(std::string &ErrorMessage) {
     } else {
       llvm::sys::path::native(FileName, NativeFilePath);
     }
-    IndexByFile[NativeFilePath].push_back(
-        CompileCommandRef(Directory, File, *Command));
+    auto Cmd = CompileCommandRef(Directory, File, *Command);
+    IndexByFile[NativeFilePath].push_back(Cmd);
+    AllCommands.push_back(Cmd);
     MatchTrie.insert(NativeFilePath);
   }
   return true;
index b7e7a72d23531c7dcd87d2f25a36b3143fde343a..380d86fc5660d34f6e431f25f81a40d813cdfe9c 100644 (file)
@@ -118,6 +118,25 @@ TEST(JSONCompilationDatabase, GetAllCompileCommands) {
   EXPECT_EQ(FileName2, Commands[1].Filename) << ErrorMessage;
   ASSERT_EQ(1u, Commands[1].CommandLine.size());
   EXPECT_EQ(Command2, Commands[1].CommandLine[0]) << ErrorMessage;
+
+  // Check that order is preserved.
+  Commands = getAllCompileCommands(
+      ("[{\"directory\":\"" + Directory2 + "\"," +
+             "\"command\":\"" + Command2 + "\","
+             "\"file\":\"" + FileName2 + "\"},"
+       " {\"directory\":\"" + Directory1 + "\"," +
+             "\"command\":\"" + Command1 + "\","
+             "\"file\":\"" + FileName1 + "\"}]").str(),
+      ErrorMessage);
+  EXPECT_EQ(2U, Commands.size()) << ErrorMessage;
+  EXPECT_EQ(Directory2, Commands[0].Directory) << ErrorMessage;
+  EXPECT_EQ(FileName2, Commands[0].Filename) << ErrorMessage;
+  ASSERT_EQ(1u, Commands[0].CommandLine.size());
+  EXPECT_EQ(Command2, Commands[0].CommandLine[0]) << ErrorMessage;
+  EXPECT_EQ(Directory1, Commands[1].Directory) << ErrorMessage;
+  EXPECT_EQ(FileName1, Commands[1].Filename) << ErrorMessage;
+  ASSERT_EQ(1u, Commands[1].CommandLine.size());
+  EXPECT_EQ(Command1, Commands[1].CommandLine[0]) << ErrorMessage;
 }
 
 static CompileCommand findCompileArgsInJsonDatabase(StringRef FileName,