From 8d02df889768d4888fd1ac6e4a6fcb325a637def Mon Sep 17 00:00:00 2001 From: clockblocker Date: Fri, 27 Nov 2026 11:02:58 +0100 Subject: [PATCH] Migrate compile_commands.json and addoninfo.json parsing to reflect-cpp (StreamReader + ErrorKind) --- lib/addoninfo.cpp | 170 ++++++++++++++++++------------------------ lib/importproject.cpp | 117 +++++++++++++++++++---------- 2 files changed, 151 insertions(+), 136 deletions(-) diff --git a/lib/addoninfo.cpp b/lib/addoninfo.cpp index 388484916ea..05407db92ea 100644 --- a/lib/addoninfo.cpp +++ b/lib/addoninfo.cpp @@ -23,10 +23,14 @@ #include #include +#include +#include #include #include +#include +#include -#include "json.h" +#include static std::string getFullPath(const std::string &fileName, const std::string &exename, bool debug = false) { if (debug) @@ -73,113 +77,89 @@ static std::string getFullPath(const std::string &fileName, const std::string &e return ""; } -static std::string parseAddonInfo(AddonInfo& addoninfo, const picojson::value &json, const std::string &fileName, const std::string &exename) { - const std::string& json_error = picojson::get_last_error(); - if (!json_error.empty()) { - return "Loading " + fileName + " failed. " + json_error; - } - if (!json.is()) - return "Loading " + fileName + " failed. JSON is not an object."; +namespace { +struct AddonInfoJson { + std::optional> args; + std::optional ctu; + std::optional python; + std::optional>> checkers; + std::optional executable; + std::optional script; +}; +} - // TODO: remove/complete default value handling for missing fields - const picojson::object& obj = json.get(); - { - const auto it = obj.find("args"); - if (it != obj.cend()) { - const auto& val = it->second; - if (!val.is()) - return "Loading " + fileName + " failed. 'args' must be an array."; - for (const picojson::value &v : val.get()) { - if (!v.is()) - return "Loading " + fileName + " failed. 'args' entry is not a string."; - addoninfo.args += " " + v.get(); - } - } - } +static std::string formatAddonError(const rfl::Error& err, const std::string& fileName) { + const std::string prefix = "Loading " + fileName + " failed. "; + if (err.kind() == rfl::ErrorKind::syntax_error) + return prefix + err.what(); - { - const auto it = obj.find("ctu"); - if (it != obj.cend()) { - const auto& val = it->second; - // ctu is specified in the config file - if (!val.is()) - return "Loading " + fileName + " failed. 'ctu' must be a boolean."; - addoninfo.ctu = val.get(); - } - else { - addoninfo.ctu = false; + for (const auto& d : err.details()) { + if (d.path.empty()) { + if (d.kind == rfl::ErrorKind::invalid_type) + return prefix + "JSON is not an object."; + continue; } - } - - { - const auto it = obj.find("python"); - if (it != obj.cend()) { - const auto& val = it->second; - // Python was defined in the config file - if (!val.is()) { - return "Loading " + fileName +" failed. 'python' must be a string."; + const auto* field = std::get_if(&d.path.front()); + if (!field) + continue; + if (d.kind == rfl::ErrorKind::missing_field) + return prefix + "'" + *field + "' is missing."; + if (d.kind == rfl::ErrorKind::invalid_type) { + if (*field == "args") + return prefix + (d.path.size() == 1 ? "'args' must be an array." : "'args' entry is not a string."); + if (*field == "ctu") + return prefix + "'ctu' must be a boolean."; + if (*field == "python") + return prefix + "'python' must be a string."; + if (*field == "checkers") { + if (d.path.size() == 1) + return prefix + "'checkers' must be an array."; + if (d.path.size() == 2) + return prefix + "'checkers' entry is not an object."; + return prefix + "'checkers' entry requirement is not a string."; } - addoninfo.python = val.get(); - } - else { - addoninfo.python = ""; + if (*field == "executable") + return prefix + "'executable' must be a string."; + if (*field == "script") + return prefix + "'script' must be a string."; } } + return prefix + err.what(); +} - { - const auto it = obj.find("checkers"); - if (it != obj.cend()) { - const auto& val = it->second; - if (!val.is()) - return "Loading " + fileName + " failed. 'checkers' must be an array."; - for (const picojson::value &v : val.get()) { - if (!v.is()) - return "Loading " + fileName + " failed. 'checkers' entry is not an object."; - - const picojson::object& checkerObj = v.get(); - if (checkerObj.size() == 1) { - const std::string c = checkerObj.begin()->first; - if (!checkerObj.begin()->second.is()) - return "Loading " + fileName + " failed. 'checkers' entry requirement is not a string."; - const std::string req = checkerObj.begin()->second.get(); - addoninfo.checkers.emplace(c, req); - } - } - } - } +static std::string parseAddonInfo(AddonInfo& addoninfo, const rfl::Result& parsed, const std::string& fileName, const std::string& exename) { + if (!parsed) + return formatAddonError(parsed.error(), fileName); - { - const auto it = obj.find("executable"); - if (it != obj.cend()) { - const auto& val = it->second; - if (!val.is()) - return "Loading " + fileName + " failed. 'executable' must be a string."; - std::string e = val.get(); - addoninfo.executable = getFullPath(e, fileName); - if (addoninfo.executable.empty()) - addoninfo.executable = std::move(e); - return ""; // <- do not load both "executable" and "script". + const AddonInfoJson& cfg = *parsed; + if (cfg.args) { + for (const std::string& v : *cfg.args) + addoninfo.args += " " + v; + } + addoninfo.ctu = cfg.ctu.value_or(false); + addoninfo.python = cfg.python.value_or(""); + if (cfg.checkers) { + for (const auto& checkerObj : *cfg.checkers) { + if (checkerObj.size() == 1) + addoninfo.checkers.emplace(checkerObj.begin()->first, checkerObj.begin()->second); } } - - const auto it = obj.find("script"); - if (it == obj.cend()) + if (cfg.executable) { + std::string e = *cfg.executable; + addoninfo.executable = getFullPath(e, fileName); + if (addoninfo.executable.empty()) + addoninfo.executable = std::move(e); + return ""; // <- do not load both "executable" and "script". + } + if (!cfg.script) return "Loading " + fileName + " failed. 'script' is missing."; - const auto& val = it->second; - if (!val.is()) - return "Loading " + fileName + " failed. 'script' must be a string."; - - return addoninfo.getAddonInfo(val.get(), exename); + return addoninfo.getAddonInfo(*cfg.script, exename); } std::string AddonInfo::getAddonInfo(const std::string &fileName, const std::string &exename, bool debug) { - if (fileName[0] == '{') { - picojson::value json; - const std::string err = picojson::parse(json, fileName); - (void)err; // TODO: report - return parseAddonInfo(*this, json, fileName, exename); - } + if (!fileName.empty() && fileName[0] == '{') + return parseAddonInfo(*this, rfl::json::read(fileName), fileName, exename); if (fileName.find('.') == std::string::npos) return getAddonInfo(fileName + ".py", exename, debug); @@ -214,7 +194,5 @@ std::string AddonInfo::getAddonInfo(const std::string &fileName, const std::stri if (name.find('/') != std::string::npos) name = name.substr(name.rfind('/') + 1); } - picojson::value json; - fin >> json; - return parseAddonInfo(*this, json, fileName, exename); + return parseAddonInfo(*this, rfl::json::read(fin), fileName, exename); } diff --git a/lib/importproject.cpp b/lib/importproject.cpp index a53760c1a32..9b582b645fc 100644 --- a/lib/importproject.cpp +++ b/lib/importproject.cpp @@ -36,12 +36,15 @@ #include #include #include +#include #include #include #include #include +#include #include #include +#include #include // Directory listing for wildcard support -- see listDirectoryFiles() @@ -60,7 +63,7 @@ #include "xml.h" -#include "json.h" +#include std::string ImportProject::collectArgs(const std::string &cmd, std::vector &args) @@ -1827,31 +1830,81 @@ ImportProject::Type ImportProject::import(const std::string &filename, Settings return ImportProject::Type::FAILURE; } +namespace { +struct CompileCommandEntry { + std::string directory; + std::optional> arguments; + std::optional command; + std::optional file; +}; +} + bool ImportProject::processCompileCommands(std::istream &istr) { - picojson::value compileCommands; - istr >> compileCommands; - if (!compileCommands.is()) { - errors.emplace_back("compilation database is not a JSON array"); - return false; - } - + rfl::json::StreamReader reader(istr, rfl::json::StreamFormat::json_array); std::map fsFileIds; + std::size_t entryIndex = 0; + + while (auto next = reader.next()) { + if (!*next) { + const auto& err = next->error(); + const auto details = err.details(); + auto findField = [&](std::string_view name) -> const rfl::ErrorDetail* { + for (const auto& d : details) { + if (!d.path.empty()) { + if (const auto* key = std::get_if(&d.path.front()); key && *key == name) + return &d; + } + } + return nullptr; + }; - for (const picojson::value &fileInfo : compileCommands.get()) { - picojson::object obj = fileInfo.get(); - - if (obj.count("directory") == 0) { - errors.emplace_back("'directory' field in compilation database entry missing"); + if (const auto* d = findField("directory")) { + if (d->kind == rfl::ErrorKind::missing_field) + errors.emplace_back("'directory' field in compilation database entry missing"); + else if (d->kind == rfl::ErrorKind::invalid_type) + errors.emplace_back("'directory' field in compilation database entry is not a string"); + else + errors.emplace_back(d->message); + return false; + } + if (const auto* d = findField("arguments")) { + if (d->kind == rfl::ErrorKind::invalid_type && d->path.size() == 1) + errors.emplace_back("'arguments' field in compilation database entry is not a JSON array"); + else + errors.emplace_back("'arguments' entry in compilation database entry is not a string"); + return false; + } + if (const auto* d = findField("command")) { + if (d->kind == rfl::ErrorKind::invalid_type) + errors.emplace_back("'command' field in compilation database entry is not a string"); + else + errors.emplace_back(d->message); + return false; + } + if (const auto* d = findField("file")) { + if (d->kind == rfl::ErrorKind::invalid_type) { + errors.emplace_back("skip compilation database entry because it does not have a proper 'file' field"); + ++entryIndex; + continue; + } + } + if (err.kind() == rfl::ErrorKind::invalid_type) { + errors.emplace_back("compilation database entry is not a JSON object"); + return false; + } + if (entryIndex == 0 && startsWith(err.what(), "Expected '['")) { + errors.emplace_back("compilation database is not a JSON array"); + return false; + } + errors.emplace_back(err.what()); return false; } - if (!obj["directory"].is()) { - errors.emplace_back("'directory' field in compilation database entry is not a string"); - return false; - } + ++entryIndex; + CompileCommandEntry& entry = **next; - std::string dirpath = Path::fromNativeSeparators(obj["directory"].get()); + std::string dirpath = Path::fromNativeSeparators(entry.directory); /* CMAKE produces the directory without trailing / so add it if not * there - it is needed by setIncludePaths() */ @@ -1861,26 +1914,10 @@ bool ImportProject::processCompileCommands(std::istream &istr) const std::string directory = std::move(dirpath); std::vector arguments; - if (obj.count("arguments")) { - if (obj["arguments"].is()) { - for (const picojson::value& arg : obj["arguments"].get()) { - if (arg.is()) - arguments.push_back(arg.get()); - } - } else { - errors.emplace_back("'arguments' field in compilation database entry is not a JSON array"); - return false; - } - } else if (obj.count("command")) { - std::string command; - if (obj["command"].is()) { - command = obj["command"].get(); - } else { - errors.emplace_back("'command' field in compilation database entry is not a string"); - return false; - } - - std::string error = collectArgs(command, arguments); + if (entry.arguments) { + arguments = std::move(*entry.arguments); + } else if (entry.command) { + std::string error = collectArgs(*entry.command, arguments); if (!error.empty()) { errors.emplace_back(error); return false; @@ -1890,12 +1927,12 @@ bool ImportProject::processCompileCommands(std::istream &istr) return false; } - if (!obj.count("file") || !obj["file"].is()) { + if (!entry.file) { errors.emplace_back("skip compilation database entry because it does not have a proper 'file' field"); continue; } - std::string file = Path::fromNativeSeparators(obj["file"].get()); + std::string file = Path::fromNativeSeparators(*entry.file); // Accept file? if (!Path::acceptFile(file))