From 45def06d594696eebe76a1151b54f13acf64753c Mon Sep 17 00:00:00 2001 From: juremenart Date: Fri, 11 Jan 2019 16:36:55 +0100 Subject: [PATCH] fix trailing / from CMAKE JSON file and update unittests to cover both (w and w/o cases) (#1592) * fix for CMake compile_commands.json input - director does not include trailing / which makes include directories wrong - so add it if it doesnt exist * fix the bugfix for trailing / in the directory name of CMAKE JSON file, add also new test case to see if it works in both cases (with and without trailing /) * revert adding accidental new line --- lib/importproject.cpp | 9 ++++++++- test/testimportproject.cpp | 12 ++++++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) 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)