Skip to content

Commit d6f30e4

Browse files
authored
Fix 14306. Import project: include path in quotes is not read (danmar#8017)
1 parent 028baa2 commit d6f30e4

File tree

3 files changed

+33
-4
lines changed

3 files changed

+33
-4
lines changed

.github/workflows/selfcheck.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ jobs:
121121
122122
- name: Self check (unusedFunction / no test / no gui)
123123
run: |
124-
supprs="--suppress=unusedFunction:lib/errorlogger.h:193 --suppress=unusedFunction:lib/importproject.cpp:1508 --suppress=unusedFunction:lib/importproject.cpp:1532"
124+
supprs="--suppress=unusedFunction:lib/errorlogger.h:193 --suppress=unusedFunction:lib/importproject.cpp:1516 --suppress=unusedFunction:lib/importproject.cpp:1540"
125125
./cppcheck -q --template=selfcheck --error-exitcode=1 --library=cppcheck-lib -D__CPPCHECK__ -D__GNUC__ --enable=unusedFunction,information --exception-handling -rp=. --project=cmake.output.notest_nogui/compile_commands.json --suppressions-list=.selfcheck_unused_suppressions --inline-suppr $supprs
126126
env:
127127
DISABLE_VALUEFLOW: 1

lib/importproject.cpp

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -210,11 +210,10 @@ ImportProject::Type ImportProject::import(const std::string &filename, Settings
210210
return ImportProject::Type::FAILURE;
211211
}
212212

213-
static std::string readUntil(const std::string &command, std::string::size_type *pos, const char until[])
213+
static std::string readUntil(const std::string &command, std::string::size_type *pos, const char until[], bool str = false)
214214
{
215215
std::string ret;
216216
bool escapedString = false;
217-
bool str = false;
218217
bool escape = false;
219218
for (; *pos < command.size() && (str || !std::strchr(until, command[*pos])); (*pos)++) {
220219
if (escape)
@@ -266,6 +265,13 @@ void ImportProject::fsParseCommand(FileSettings& fs, const std::string& command,
266265
pos++;
267266
if (pos >= command.size())
268267
break;
268+
bool wholeArgQuoted = false;
269+
if (command[pos] == '"') {
270+
wholeArgQuoted = true;
271+
pos++;
272+
if (pos >= command.size())
273+
break;
274+
}
269275
if (command[pos] != '/' && command[pos] != '-')
270276
continue;
271277
pos++;
@@ -276,7 +282,9 @@ void ImportProject::fsParseCommand(FileSettings& fs, const std::string& command,
276282
while (pos < command.size() && command[pos] == ' ')
277283
++pos;
278284
}
279-
std::string fval = readUntil(command, &pos, " =");
285+
std::string fval = readUntil(command, &pos, " =", wholeArgQuoted);
286+
if (wholeArgQuoted && fval.back() == '\"')
287+
fval.resize(fval.size() - 1);
280288
if (F=='D') {
281289
std::string defval = readUntil(command, &pos, " ");
282290
defs += fval;

test/testimportproject.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ class TestImportProject : public TestFixture {
6666
TEST_CASE(importCompileCommands12); // #13040: "directory" is parent directory, relative include paths
6767
TEST_CASE(importCompileCommands13); // #13333: duplicate file entries
6868
TEST_CASE(importCompileCommands14); // #14156
69+
TEST_CASE(importCompileCommands15); // #14306
6970
TEST_CASE(importCompileCommandsArgumentsSection); // Handle arguments section
7071
TEST_CASE(importCompileCommandsNoCommandSection); // gracefully handles malformed json
7172
TEST_CASE(importCompileCommandsDirectoryMissing); // 'directory' field missing
@@ -389,6 +390,26 @@ class TestImportProject : public TestFixture {
389390
ASSERT_EQUALS("TFS_LINUX_MODULE_NAME=\"tfs_linux\"", fs.defines);
390391
}
391392

393+
void importCompileCommands15() const { // #14306
394+
REDIRECT;
395+
constexpr char json[] =
396+
R"([
397+
{
398+
"directory": "C:\\Users\\abcd\\efg\\hijk",
399+
"command": "gcc \"-Ipath\\123\" \"-c\" test.c",
400+
"file": "test.c",
401+
"output": "test.obj"
402+
}
403+
])";
404+
std::istringstream istr(json);
405+
TestImporter importer;
406+
ASSERT_EQUALS(true, importer.importCompileCommands(istr));
407+
ASSERT_EQUALS(1, importer.fileSettings.size());
408+
const FileSettings &fs = importer.fileSettings.front();
409+
ASSERT_EQUALS(1, fs.includePaths.size());
410+
ASSERT_EQUALS("C:/Users/abcd/efg/hijk/path/123/", fs.includePaths.front());
411+
}
412+
392413
void importCompileCommandsArgumentsSection() const {
393414
REDIRECT;
394415
constexpr char json[] = "[ { \"directory\": \"/tmp/\","

0 commit comments

Comments
 (0)