From 3bcbba598d7de0736ec6c7e14293a74c68812b3b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Wed, 23 Mar 2022 21:13:51 +0100 Subject: [PATCH] Fixed #10887 (compile database: include path with space is not handled) --- lib/importproject.cpp | 5 ++++- test/testimportproject.cpp | 23 ++++++++++++++++++++++- 2 files changed, 26 insertions(+), 2 deletions(-) diff --git a/lib/importproject.cpp b/lib/importproject.cpp index 02e13c24c..334a0eabd 100644 --- a/lib/importproject.cpp +++ b/lib/importproject.cpp @@ -414,7 +414,10 @@ bool ImportProject::importCompileCommands(std::istream &istr) if (obj["arguments"].is()) { for (const picojson::value& arg : obj["arguments"].get()) { if (arg.is()) { - command += arg.get() + " "; + std::string str = arg.get(); + if (str.find(" ") != std::string::npos) + str = "\"" + str + "\""; + command += str + " "; } } } else { diff --git a/test/testimportproject.cpp b/test/testimportproject.cpp index a0cc3decc..20e560528 100644 --- a/test/testimportproject.cpp +++ b/test/testimportproject.cpp @@ -58,6 +58,7 @@ private: TEST_CASE(importCompileCommands7); // linux: "/home/danielm/cppcheck 2" TEST_CASE(importCompileCommands8); // Windows: "C:\Users\danielm\cppcheck" TEST_CASE(importCompileCommands9); + TEST_CASE(importCompileCommands10); // #10887: include path with space TEST_CASE(importCompileCommandsArgumentsSection); // Handle arguments section TEST_CASE(importCompileCommandsNoCommandSection); // gracefully handles malformed json TEST_CASE(importCppcheckGuiProject); @@ -254,13 +255,33 @@ private: "powershell.exe -WindowStyle Hidden -NoProfile -ExecutionPolicy Bypass -File d:\\Projekte\\xyz\\firmware\\app\\xyz-lib\\build.ps1 -IAR -COMPILER_PATH \"c:\\Program Files (x86)\\IAR Systems\\Embedded Workbench 9.0\" -CONTROLLER CC1310F128 -LIB LIB_PERMANENT -COMPILER_DEFINES \"CC1310_HFXO_FREQ=24000000 DEBUG\"" ], "directory" : "d:\\Projekte\\xyz\\firmware\\app", - "type" : "PRE" + "type" : "PRE", + "file": "1.c" }])"; std::istringstream istr(json); TestImporter importer; ASSERT_EQUALS(true, importer.importCompileCommands(istr)); } + void importCompileCommands10() const { // #10887 + const char json[] = + R"([{ + "file": "/home/danielm/cppcheck/1/test folder/1.c" , + "directory": "", + "arguments": [ + "iccavr.exe", + "-I", + "/home/danielm/cppcheck/test folder" + ] + }])"; + 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("/home/danielm/cppcheck/test folder/", fs.includePaths.front()); + } + void importCompileCommandsArgumentsSection() const { const char json[] = "[ { \"directory\": \"/tmp/\"," "\"arguments\": [\"gcc\", \"-c\", \"src.c\"],"