From cefe77623a4383cda8fc9024f78ce062125e2224 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Tue, 27 Aug 2024 13:18:39 +0200 Subject: [PATCH] Fix #13040 (Import project: compile_commands.json, fail to include header in project root folder) --- lib/importproject.cpp | 4 ++-- test/testimportproject.cpp | 18 ++++++++++++++++++ 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/lib/importproject.cpp b/lib/importproject.cpp index 336e25744c8..3106496e817 100644 --- a/lib/importproject.cpp +++ b/lib/importproject.cpp @@ -164,7 +164,7 @@ void ImportProject::fsSetIncludePaths(FileSettings& fs, const std::string &basep } if (endsWith(s,'/')) // this is a temporary hack, simplifyPath can crash if path ends with '/' - s.erase(s.size() - 1U); // TODO: Use std::string::pop_back() as soon as travis supports it + s.pop_back(); if (s.find("$(") == std::string::npos) { s = Path::simplifyPath(basepath + s); @@ -174,7 +174,7 @@ void ImportProject::fsSetIncludePaths(FileSettings& fs, const std::string &basep } if (s.empty()) continue; - fs.includePaths.push_back(s + '/'); + fs.includePaths.push_back(s.back() == '/' ? s : (s + '/')); } } diff --git a/test/testimportproject.cpp b/test/testimportproject.cpp index b96545a3770..2b4d8ce611e 100644 --- a/test/testimportproject.cpp +++ b/test/testimportproject.cpp @@ -62,6 +62,7 @@ class TestImportProject : public TestFixture { TEST_CASE(importCompileCommands9); TEST_CASE(importCompileCommands10); // #10887: include path with space TEST_CASE(importCompileCommands11); // include path order + TEST_CASE(importCompileCommands12); // #13040: "directory" is parent directory, relative include paths TEST_CASE(importCompileCommandsArgumentsSection); // Handle arguments section TEST_CASE(importCompileCommandsNoCommandSection); // gracefully handles malformed json TEST_CASE(importCppcheckGuiProject); @@ -319,6 +320,23 @@ class TestImportProject : public TestFixture { ASSERT_EQUALS("/x/abc/", fs.includePaths.back()); } + void importCompileCommands12() const { // #13040 + REDIRECT; + constexpr char json[] = + R"([{ + "file": "/x/src/1.c" , + "directory": "/x", + "command": "cc -c -I. src/1.c" + }])"; + std::istringstream istr(json); + TestImporter importer; + ASSERT_EQUALS(true, importer.importCompileCommands(istr)); + ASSERT_EQUALS(1, importer.fileSettings.size()); + const FileSettings &fs = importer.fileSettings.front(); + ASSERT_EQUALS(1, fs.includePaths.size()); + ASSERT_EQUALS("/x", fs.includePaths.front()); + } + void importCompileCommandsArgumentsSection() const { REDIRECT; constexpr char json[] = "[ { \"directory\": \"/tmp/\","