diff --git a/lib/importproject.cpp b/lib/importproject.cpp index 1261bd60d..0f9dfe00e 100644 --- a/lib/importproject.cpp +++ b/lib/importproject.cpp @@ -413,7 +413,7 @@ bool ImportProject::importCompileCommands(std::istream &istr) for (const picojson::value& arg : obj["arguments"].get()) { if (arg.is()) { std::string str = arg.get(); - if (str.find(" ") != std::string::npos) + if (str.find(" ") != std::string::npos && str.find("=\"") > str.find(" ")) str = "\"" + str + "\""; command += str + " "; } diff --git a/test/testimportproject.cpp b/test/testimportproject.cpp index 6430ab0bc..d370606e0 100644 --- a/test/testimportproject.cpp +++ b/test/testimportproject.cpp @@ -61,6 +61,7 @@ private: TEST_CASE(importCompileCommands9); TEST_CASE(importCompileCommands10); // #10887: include path with space TEST_CASE(importCompileCommands11); // include path order + TEST_CASE(importCompileCommands12); // defines TEST_CASE(importCompileCommandsArgumentsSection); // Handle arguments section TEST_CASE(importCompileCommandsNoCommandSection); // gracefully handles malformed json TEST_CASE(importCppcheckGuiProject); @@ -307,6 +308,27 @@ private: ASSERT_EQUALS("/x/abc/", fs.includePaths.back()); } + void importCompileCommands12() const { // define + const char json[] = + R"([{ + "file": "1.c" , + "directory": "/x", + "arguments": [ + "cc", + "-D", + "X=1", + "-D", + "__VERSION__=\"IAR C/C++ Compiler V6.40.2.748 for Atmel AVR\"" + ] + }])"; + std::istringstream istr(json); + TestImporter importer; + ASSERT_EQUALS(true, importer.importCompileCommands(istr)); + ASSERT_EQUALS(1, importer.fileSettings.size()); + const ImportProject::FileSettings &fs = importer.fileSettings.front(); + ASSERT_EQUALS("X=1;__VERSION__=IAR C/C++ Compiler V6.40.2.748 for Atmel AVR", fs.defines); + } + void importCompileCommandsArgumentsSection() const { const char json[] = "[ { \"directory\": \"/tmp/\"," "\"arguments\": [\"gcc\", \"-c\", \"src.c\"],"