Import Project: Fix problem with define value with space

This commit is contained in:
Daniel Marjamäki 2022-04-15 14:16:17 +02:00
parent 5825ab75db
commit ba9bcf01df
2 changed files with 23 additions and 1 deletions

View File

@ -413,7 +413,7 @@ bool ImportProject::importCompileCommands(std::istream &istr)
for (const picojson::value& arg : obj["arguments"].get<picojson::array>()) { for (const picojson::value& arg : obj["arguments"].get<picojson::array>()) {
if (arg.is<std::string>()) { if (arg.is<std::string>()) {
std::string str = arg.get<std::string>(); std::string str = arg.get<std::string>();
if (str.find(" ") != std::string::npos) if (str.find(" ") != std::string::npos && str.find("=\"") > str.find(" "))
str = "\"" + str + "\""; str = "\"" + str + "\"";
command += str + " "; command += str + " ";
} }

View File

@ -60,6 +60,7 @@ private:
TEST_CASE(importCompileCommands9); TEST_CASE(importCompileCommands9);
TEST_CASE(importCompileCommands10); // #10887: include path with space TEST_CASE(importCompileCommands10); // #10887: include path with space
TEST_CASE(importCompileCommands11); // include path order TEST_CASE(importCompileCommands11); // include path order
TEST_CASE(importCompileCommands12); // defines
TEST_CASE(importCompileCommandsArgumentsSection); // Handle arguments section TEST_CASE(importCompileCommandsArgumentsSection); // Handle arguments section
TEST_CASE(importCompileCommandsNoCommandSection); // gracefully handles malformed json TEST_CASE(importCompileCommandsNoCommandSection); // gracefully handles malformed json
TEST_CASE(importCppcheckGuiProject); TEST_CASE(importCppcheckGuiProject);
@ -306,6 +307,27 @@ private:
ASSERT_EQUALS("/x/abc/", fs.includePaths.back()); 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 { void importCompileCommandsArgumentsSection() const {
const char json[] = "[ { \"directory\": \"/tmp/\"," const char json[] = "[ { \"directory\": \"/tmp/\","
"\"arguments\": [\"gcc\", \"-c\", \"src.c\"]," "\"arguments\": [\"gcc\", \"-c\", \"src.c\"],"