Skip to content

Commit

Permalink
error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
danmar committed Jan 30, 2024
1 parent e411d6e commit a0f9972
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 16 deletions.
39 changes: 23 additions & 16 deletions gui/mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -908,31 +908,38 @@ bool MainWindow::tryLoadLibrary(Library *library, const QString& filename)
void MainWindow::loadAddon(Settings &settings, const QString &filesDir, const QString &pythonCmd, const QString& addon)
{
const QString addonFilePath = fromNativePath(ProjectFile::getAddonFilePath(filesDir, addon));
if (addonFilePath.isEmpty())
return; // TODO: report an error

picojson::object obj;
obj["script"] = picojson::value(addonFilePath.toStdString());
if (!pythonCmd.isEmpty())
obj["python"] = picojson::value(pythonCmd.toStdString());

if (!isCppcheckPremium() && addon == "misra") {
const QString misraFile = fromNativePath(mSettings->value(SETTINGS_MISRA_FILE).toString());
if (!misraFile.isEmpty()) {
QString arg;
if (misraFile.endsWith(".pdf", Qt::CaseInsensitive))
arg = "--misra-pdf=" + misraFile;
else
arg = "--rule-texts=" + misraFile;
obj["args"] = picojson::value(arg.toStdString());
if (addonFilePath.isEmpty()) // Ensure there will be "fileNotFound" errors
obj["script"] = picojson::value(addon.toStdString());
else {
obj["script"] = picojson::value(addonFilePath.toStdString());
if (!pythonCmd.isEmpty())
obj["python"] = picojson::value(pythonCmd.toStdString());

if (!isCppcheckPremium() && addon == "misra") {
const QString misraFile = fromNativePath(mSettings->value(SETTINGS_MISRA_FILE).toString());
if (!misraFile.isEmpty()) {
QString arg;
if (misraFile.endsWith(".pdf", Qt::CaseInsensitive))
arg = "--misra-pdf=" + misraFile;
else
arg = "--rule-texts=" + misraFile;
obj["args"] = picojson::value(arg.toStdString());
}
}
}

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

AddonInfo addonInfo;
addonInfo.getAddonInfo(json_str, settings.exename); // TODO: handle error
if (addonFilePath.isEmpty()) {
addonInfo.name = addon.toStdString();
} else {
addonInfo.getAddonInfo(json_str, settings.exename); // TODO: handle error
}
settings.addonInfos.emplace_back(std::move(addonInfo));

settings.addons.emplace(std::move(json_str));
Expand Down
20 changes: 20 additions & 0 deletions lib/cppcheck.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1033,6 +1033,21 @@ unsigned int CppCheck::checkFile(const std::string& filename, const std::string
return mExitCode;
}

void CppCheck::fileNotFoundError(const std::string &filename, const std::string& msg)
{
const ErrorMessage::FileLocation loc1(filename, 0, 0);
std::list<ErrorMessage::FileLocation> callstack(1, loc1);

ErrorMessage errmsg(callstack,
emptyString,
Severity::error,
msg,
"fileNotFound",
Certainty::normal);

mErrorLogger.reportErr(errmsg);
}

// TODO: replace with ErrorMessage::fromInternalError()
void CppCheck::internalError(const std::string &filename, const std::string &msg)
{
Expand Down Expand Up @@ -1440,6 +1455,11 @@ 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()) {
fileNotFoundError(file0, "Fail to execute addon " + addonInfo.name);
continue;
}

const std::vector<picojson::value> results =
executeAddon(addonInfo, mSettings.addonPython, fileList.empty() ? files[0] : fileList, mSettings.premiumArgs, mExecuteCommand);

Expand Down
3 changes: 3 additions & 0 deletions lib/cppcheck.h
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,9 @@ class CPPCHECKLIB CppCheck : ErrorLogger {
bool hasRule(const std::string &tokenlist) const;
#endif

/** @brief A file is not found => report error message */
void fileNotFoundError(const std::string &filename, const std::string& msg);

/** @brief There has been an internal error => Report information message */
void internalError(const std::string &filename, const std::string &msg);

Expand Down

0 comments on commit a0f9972

Please sign in to comment.