diff --git a/lib/importproject.cpp b/lib/importproject.cpp index 810b6e4a9..1ef7c62aa 100644 --- a/lib/importproject.cpp +++ b/lib/importproject.cpp @@ -30,7 +30,7 @@ #include #include #include - +#include void ImportProject::ignorePaths(const std::vector &ipaths) { @@ -340,7 +340,30 @@ void ImportProject::importCompileCommands(std::istream &istr) dirpath += '/'; const std::string directory = dirpath; - const std::string command = obj["command"].get(); + + std::ostringstream comm; + if( obj.find( "arguments" ) != obj.end() ) { + if( obj[ "arguments" ].is< picojson::array >() ) { + for( const picojson::value& arg : obj[ "arguments" ].get< picojson::array >() ) { + if( arg.is< std::string >() ) { + comm << arg.get< std::string >() << " "; + } + } + } + else { + return; + } + } + else if( obj.find( "command" ) != obj.end() ) { + if( obj[ "command" ].is< std::string >() ) { + comm << obj[ "command" ].get< std::string >(); + } + } + else { + return; + } + + const std::string command = comm.str(); const std::string file = Path::fromNativeSeparators(obj["file"].get()); // Accept file? diff --git a/test/testimportproject.cpp b/test/testimportproject.cpp index af220d8d7..26df47eef 100644 --- a/test/testimportproject.cpp +++ b/test/testimportproject.cpp @@ -47,6 +47,8 @@ private: TEST_CASE(importCompileCommands2); // #8563 TEST_CASE(importCompileCommands3); // check with existing trailing / in directory TEST_CASE(importCompileCommands4); // only accept certain file types + TEST_CASE(importCompileCommandsArgumentsSection); // Handle arguments section + TEST_CASE(importCompileCommandsNoCommandSection); // gracefully handles malformed json TEST_CASE(importCppcheckGuiProject); } @@ -138,6 +140,26 @@ private: ASSERT_EQUALS(0, importer.fileSettings.size()); } + void importCompileCommandsArgumentsSection() const { + const char json[] = "[ { \"directory\": \"/tmp/\"," + "\"arguments\": [\"gcc\", \"-c\", \"src.c\"]," + "\"file\": \"src.c\" } ]"; + std::istringstream istr(json); + TestImporter importer; + importer.importCompileCommands(istr); + ASSERT_EQUALS(1, importer.fileSettings.size()); + ASSERT_EQUALS("/tmp/src.c", importer.fileSettings.begin()->filename); + } + + void importCompileCommandsNoCommandSection() const { + const char json[] = "[ { \"directory\": \"/tmp/\"," + "\"file\": \"src.mm\" } ]"; + std::istringstream istr(json); + TestImporter importer; + importer.importCompileCommands(istr); + ASSERT_EQUALS(0, importer.fileSettings.size()); + } + void importCppcheckGuiProject() const { const char xml[] = "\n" "\n"