diff --git a/lib/importproject.cpp b/lib/importproject.cpp index fec2deb734d..4910e259e96 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..3857b751599 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/\","