diff --git a/lib/importproject.cpp b/lib/importproject.cpp index e8269b30f..93c300bf7 100644 --- a/lib/importproject.cpp +++ b/lib/importproject.cpp @@ -253,24 +253,28 @@ void ImportProject::FileSettings::parseCommand(const std::string &command) void ImportProject::importCompileCommands(std::istream &istr) { - picojson::value v; - istr >> v; - if (!v.is()) + picojson::value compileCommands; + istr >> compileCommands; + if (!compileCommands.is()) return; - for (const picojson::value &fileInfo : v.get()) { + for (const picojson::value &fileInfo : compileCommands.get()) { picojson::object obj = fileInfo.get(); - std::string dirpath = obj["directory"].get(); + std::string dirpath = Path::fromNativeSeparators(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 directory = dirpath; const std::string command = obj["command"].get(); const std::string file = Path::fromNativeSeparators(obj["file"].get()); + // Accept file? + if (!Path::acceptFile(file)) + continue; + struct FileSettings fs; if (Path::isAbsolute(file) || Path::fileExists(file)) fs.filename = file; diff --git a/test/testimportproject.cpp b/test/testimportproject.cpp index bbd0eadc8..60a026c6b 100644 --- a/test/testimportproject.cpp +++ b/test/testimportproject.cpp @@ -46,6 +46,7 @@ private: TEST_CASE(importCompileCommands1); TEST_CASE(importCompileCommands2); // #8563 TEST_CASE(importCompileCommands3); // check with existing trailing / in directory + TEST_CASE(importCompileCommands4); // only accept certain file types } void setDefines() const { @@ -125,6 +126,16 @@ private: ASSERT_EQUALS(1, importer.fileSettings.size()); ASSERT_EQUALS("/tmp/src.c", importer.fileSettings.begin()->filename); } + + void importCompileCommands4() const { + const char json[] = "[ { \"directory\": \"/tmp/\"," + "\"command\": \"gcc -c src.mm\"," + "\"file\": \"src.mm\" } ]"; + std::istringstream istr(json); + TestImporter importer; + importer.importCompileCommands(istr); + ASSERT_EQUALS(0, importer.fileSettings.size()); + } }; REGISTER_TEST(TestImportProject)