Index: lib/Tooling/JSONCompilationDatabase.cpp =================================================================== --- lib/Tooling/JSONCompilationDatabase.cpp +++ lib/Tooling/JSONCompilationDatabase.cpp @@ -251,20 +251,44 @@ return Arguments; } +static std::vector +deduplicateCommandLine(const std::vector &Nodes) { + // This method filters out flags like: + // '-MD', '-MMD', '-MG', '-MP', '-MF', '-MT', '-MQ' + // Or might transform flags too like: + // '-Dkey=value' to ['-D', 'key=value'] + // '-I.' to ['-I', '.'] + return Nodes; +} + +struct CompareCompileCommand { + bool operator()(const CompileCommand &Lhs, const CompileCommand &Rhs) const { + return (Lhs.Directory == Rhs.Directory) && + (Lhs.Filename == Rhs.Filename) && + (Lhs.CommandLine == Rhs.CommandLine) && + (Lhs.Output == Rhs.Output); + } +}; + void JSONCompilationDatabase::getCommands( ArrayRef CommandsRef, std::vector &Commands) const { + // To filter out duplicate elements put the result first into a set. + std::set Results; for (int I = 0, E = CommandsRef.size(); I != E; ++I) { SmallString<8> DirectoryStorage; SmallString<32> FilenameStorage; SmallString<32> OutputStorage; auto Output = std::get<3>(CommandsRef[I]); - Commands.emplace_back( + Result.emplace( std::get<0>(CommandsRef[I])->getValue(DirectoryStorage), std::get<1>(CommandsRef[I])->getValue(FilenameStorage), - nodeToCommandLine(Syntax, std::get<2>(CommandsRef[I])), + deduplicateCommandLine( + nodeToCommandLine(Syntax, std::get<2>(CommandsRef[I]))), Output ? Output->getValue(OutputStorage) : ""); } + // Copy the unique elements into the real return value. + std::copy(Results.begin(), Results.end(), Commands.begin()); } bool JSONCompilationDatabase::parse(std::string &ErrorMessage) {