Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
170 changes: 74 additions & 96 deletions lib/addoninfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,14 @@

#include <fstream>
#include <iostream>
#include <map>
#include <optional>
#include <string>
#include <utility>
#include <variant>
#include <vector>

#include "json.h"
#include <rfl/json.hpp>

static std::string getFullPath(const std::string &fileName, const std::string &exename, bool debug = false) {
if (debug)
Expand Down Expand Up @@ -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<picojson::object>())
return "Loading " + fileName + " failed. JSON is not an object.";
namespace {
struct AddonInfoJson {
std::optional<std::vector<std::string>> args;
std::optional<bool> ctu;
std::optional<std::string> python;
std::optional<std::vector<std::map<std::string, std::string>>> checkers;
std::optional<std::string> executable;
std::optional<std::string> script;
};
}

// TODO: remove/complete default value handling for missing fields
const picojson::object& obj = json.get<picojson::object>();
{
const auto it = obj.find("args");
if (it != obj.cend()) {
const auto& val = it->second;
if (!val.is<picojson::array>())
return "Loading " + fileName + " failed. 'args' must be an array.";
for (const picojson::value &v : val.get<picojson::array>()) {
if (!v.is<std::string>())
return "Loading " + fileName + " failed. 'args' entry is not a string.";
addoninfo.args += " " + v.get<std::string>();
}
}
}
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<bool>())
return "Loading " + fileName + " failed. 'ctu' must be a boolean.";
addoninfo.ctu = val.get<bool>();
}
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<std::string>()) {
return "Loading " + fileName +" failed. 'python' must be a string.";
const auto* field = std::get_if<std::string>(&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<std::string>();
}
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<picojson::array>())
return "Loading " + fileName + " failed. 'checkers' must be an array.";
for (const picojson::value &v : val.get<picojson::array>()) {
if (!v.is<picojson::object>())
return "Loading " + fileName + " failed. 'checkers' entry is not an object.";

const picojson::object& checkerObj = v.get<picojson::object>();
if (checkerObj.size() == 1) {
const std::string c = checkerObj.begin()->first;
if (!checkerObj.begin()->second.is<std::string>())
return "Loading " + fileName + " failed. 'checkers' entry requirement is not a string.";
const std::string req = checkerObj.begin()->second.get<std::string>();
addoninfo.checkers.emplace(c, req);
}
}
}
}
static std::string parseAddonInfo(AddonInfo& addoninfo, const rfl::Result<AddonInfoJson>& 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<std::string>())
return "Loading " + fileName + " failed. 'executable' must be a string.";
std::string e = val.get<std::string>();
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<std::string>())
return "Loading " + fileName + " failed. 'script' must be a string.";

return addoninfo.getAddonInfo(val.get<std::string>(), 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<AddonInfoJson>(fileName), fileName, exename);
if (fileName.find('.') == std::string::npos)
return getAddonInfo(fileName + ".py", exename, debug);

Expand Down Expand Up @@ -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<AddonInfoJson>(fin), fileName, exename);
}
117 changes: 77 additions & 40 deletions lib/importproject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,15 @@
#include <iostream>
#include <iterator>
#include <map>
#include <optional>
#include <set>
#include <sstream>
#include <stdexcept>
#include <string>
#include <string_view>
#include <unordered_set>
#include <utility>
#include <variant>
#include <vector>

// Directory listing for wildcard <Import> support -- see listDirectoryFiles()
Expand All @@ -60,7 +63,7 @@

#include "xml.h"

#include "json.h"
#include <rfl/json/stream.hpp>


std::string ImportProject::collectArgs(const std::string &cmd, std::vector<std::string> &args)
Expand Down Expand Up @@ -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<std::vector<std::string>> arguments;
std::optional<std::string> command;
std::optional<std::string> file;
};
}

bool ImportProject::processCompileCommands(std::istream &istr)
{
picojson::value compileCommands;
istr >> compileCommands;
if (!compileCommands.is<picojson::array>()) {
errors.emplace_back("compilation database is not a JSON array");
return false;
}

rfl::json::StreamReader<CompileCommandEntry> reader(istr, rfl::json::StreamFormat::json_array);
std::map<std::string, std::size_t> 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<std::string>(&d.path.front()); key && *key == name)
return &d;
}
}
return nullptr;
};

for (const picojson::value &fileInfo : compileCommands.get<picojson::array>()) {
picojson::object obj = fileInfo.get<picojson::object>();

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<std::string>()) {
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>());
std::string dirpath = Path::fromNativeSeparators(entry.directory);

/* CMAKE produces the directory without trailing / so add it if not
* there - it is needed by setIncludePaths() */
Expand All @@ -1861,26 +1914,10 @@ bool ImportProject::processCompileCommands(std::istream &istr)
const std::string directory = std::move(dirpath);

std::vector<std::string> arguments;
if (obj.count("arguments")) {
if (obj["arguments"].is<picojson::array>()) {
for (const picojson::value& arg : obj["arguments"].get<picojson::array>()) {
if (arg.is<std::string>())
arguments.push_back(arg.get<std::string>());
}
} 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<std::string>()) {
command = obj["command"].get<std::string>();
} 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;
Expand All @@ -1890,12 +1927,12 @@ bool ImportProject::processCompileCommands(std::istream &istr)
return false;
}

if (!obj.count("file") || !obj["file"].is<std::string>()) {
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>());
std::string file = Path::fromNativeSeparators(*entry.file);

// Accept file?
if (!Path::acceptFile(file))
Expand Down