diff --git a/lib/importproject.cpp b/lib/importproject.cpp index d2535e983..e187b4796 100644 --- a/lib/importproject.cpp +++ b/lib/importproject.cpp @@ -260,7 +260,14 @@ void ImportProject::importCompileCommands(std::istream &istr) for (const picojson::value &fileInfo : v.get()) { picojson::object obj = fileInfo.get(); - const std::string directory = Path::fromNativeSeparators(obj["directory"].get()); + std::string dirpath = obj["directory"].get(); + + /* CMAKE produces the directory without trailing / so add it if not + * there - it is needed by setIncludePaths() */ + if(!endsWith(dirpath, '/')) + dirpath += '/'; + + const std::string directory = Path::fromNativeSeparators(dirpath); const std::string command = obj["command"].get(); const std::string file = Path::fromNativeSeparators(obj["file"].get()); diff --git a/test/testimportproject.cpp b/test/testimportproject.cpp index 4dfdffb40..148c72854 100644 --- a/test/testimportproject.cpp +++ b/test/testimportproject.cpp @@ -45,6 +45,7 @@ private: TEST_CASE(setIncludePaths3); // macro names are case insensitive TEST_CASE(importCompileCommands1); TEST_CASE(importCompileCommands2); // #8563 + TEST_CASE(importCompileCommands3); // check with existing trailing / in directory } void setDefines() const { @@ -113,6 +114,17 @@ private: ASSERT_EQUALS(1, importer.fileSettings.size()); ASSERT_EQUALS("/tmp/src.c", importer.fileSettings.begin()->filename); } + + void importCompileCommands3() const { + const char json[] = "[ { \"directory\": \"/tmp/\"," + "\"command\": \"gcc -c src.c\"," + "\"file\": \"src.c\" } ]"; + std::istringstream istr(json); + TestImporter importer; + importer.importCompileCommands(istr); + ASSERT_EQUALS(1, importer.fileSettings.size()); + ASSERT_EQUALS("/tmp/src.c", importer.fileSettings.begin()->filename); + } }; REGISTER_TEST(TestImportProject)