summaryrefslogtreecommitdiffstats
path: root/clang
diff options
context:
space:
mode:
Diffstat (limited to 'clang')
-rw-r--r--clang/include/clang/Tooling/JSONCompilationDatabase.h4
-rw-r--r--clang/lib/Tooling/JSONCompilationDatabase.cpp11
-rw-r--r--clang/unittests/Tooling/CompilationDatabaseTest.cpp19
3 files changed, 27 insertions, 7 deletions
diff --git a/clang/include/clang/Tooling/JSONCompilationDatabase.h b/clang/include/clang/Tooling/JSONCompilationDatabase.h
index 0e6c893969d..2a13fc155ce 100644
--- a/clang/include/clang/Tooling/JSONCompilationDatabase.h
+++ b/clang/include/clang/Tooling/JSONCompilationDatabase.h
@@ -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;
diff --git a/clang/lib/Tooling/JSONCompilationDatabase.cpp b/clang/lib/Tooling/JSONCompilationDatabase.cpp
index dd4d7a8f083..299fbdc149b 100644
--- a/clang/lib/Tooling/JSONCompilationDatabase.cpp
+++ b/clang/lib/Tooling/JSONCompilationDatabase.cpp
@@ -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;
diff --git a/clang/unittests/Tooling/CompilationDatabaseTest.cpp b/clang/unittests/Tooling/CompilationDatabaseTest.cpp
index b7e7a72d235..380d86fc566 100644
--- a/clang/unittests/Tooling/CompilationDatabaseTest.cpp
+++ b/clang/unittests/Tooling/CompilationDatabaseTest.cpp
@@ -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,
OpenPOWER on IntegriCloud