Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
danmar committed Jan 30, 2024
1 parent b6488c6 commit 06410ba
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 9 deletions.
12 changes: 7 additions & 5 deletions gui/mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -911,9 +911,13 @@ void MainWindow::loadAddon(Settings &settings, const QString &filesDir, const QS

if (addonFilePath.isEmpty()) {
// Addon not found => add data to addons and addonInfos so that fileNotFound error(s) are reported during analysis
settings.addons.emplace("file not found: " + addon.toStdString());
picojson::object obj;
obj["script"] = picojson::value(addonFilePath.toStdString());
settings.addons.emplace(picojson::value(obj).serialize());
AddonInfo addonInfo;
addonInfo.name = addon.toStdString();
addonInfo.errorText = "File not found: '" + addon.toStdString() + "'";
addonInfo.errorId = AddonInfo::ErrorId::fileNotFound;
settings.addonInfos.emplace_back(std::move(addonInfo));
return;
}
Expand All @@ -935,15 +939,13 @@ void MainWindow::loadAddon(Settings &settings, const QString &filesDir, const QS
}
}

picojson::value json;
json.set(std::move(obj));
std::string json_str = json.serialize();
const std::string& json_str = picojson::value(obj).serialize();

AddonInfo addonInfo;
addonInfo.getAddonInfo(json_str, settings.exename); // TODO: handle error
settings.addonInfos.emplace_back(std::move(addonInfo));

settings.addons.emplace(std::move(json_str));
settings.addons.emplace(json_str);
}

Settings MainWindow::getCppcheckSettings()
Expand Down
15 changes: 12 additions & 3 deletions lib/addoninfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ static std::string parseAddonInfo(AddonInfo& addoninfo, const picojson::value &j
if (!val.is<std::string>())
return "Loading " + fileName + " failed. 'executable' must be a string.";
addoninfo.executable = getFullPath(val.get<std::string>(), fileName);
return ""; // TODO: why bail out?
return ""; // <- do not set both "executable" and "script".
}
}

Expand All @@ -121,15 +121,15 @@ static std::string parseAddonInfo(AddonInfo& addoninfo, const picojson::value &j
return addoninfo.getAddonInfo(val.get<std::string>(), exename);
}

std::string AddonInfo::getAddonInfo(const std::string &fileName, const std::string &exename) {
std::string AddonInfo::getAddonInfoInternal(const std::string &fileName, const std::string &exename) {
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.find('.') == std::string::npos)
return getAddonInfo(fileName + ".py", exename);
return getAddonInfoInternal(fileName + ".py", exename);

if (endsWith(fileName, ".py")) {
scriptFile = Path::fromNativeSeparators(getFullPath(fileName, exename));
Expand Down Expand Up @@ -161,3 +161,12 @@ std::string AddonInfo::getAddonInfo(const std::string &fileName, const std::stri
fin >> json;
return parseAddonInfo(*this, json, fileName, exename);
}

std::string AddonInfo::getAddonInfo(const std::string &fileName, const std::string &exename) {
const std::string& errmsg = getAddonInfo(fileName, exename);
if (!errmsg.empty()) {
errorId = ErrorId::parseError;
errorText = errmsg;
}
return errorText;
}
6 changes: 6 additions & 0 deletions lib/addoninfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,13 @@ struct CPPCHECKLIB AddonInfo {
bool ctu = false;
std::string runScript;

enum class ErrorId {fileNotFound, parseError, none};
ErrorId errorId = ErrorId::none;
std::string errorText;

std::string getAddonInfo(const std::string &fileName, const std::string &exename);
private:
std::string getAddonInfoInternal(const std::string &fileName, const std::string &exename);
};

#endif // addonInfoH
5 changes: 4 additions & 1 deletion lib/cppcheck.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1455,9 +1455,12 @@ void CppCheck::executeAddons(const std::vector<std::string>& files, const std::s
if (addonInfo.name != "misra" && !addonInfo.ctu && endsWith(files.back(), ".ctu-info"))
continue;

if (addonInfo.scriptFile.empty() && addonInfo.executable.empty()) {
if (addonInfo.errorId == AddonInfo::ErrorId::fileNotFound) {
fileNotFoundError(file0, "Fail to execute addon " + addonInfo.name);
continue;
} else if (addonInfo.errorId != AddonInfo::ErrorId::none) {
// TODO addonError(file0, "Fail to execute addon " + addonInfo.name + ": " + addonInfo.errorText, "addonError");
continue;
}

const std::vector<picojson::value> results =
Expand Down

0 comments on commit 06410ba

Please sign in to comment.