Skip to content

Commit

Permalink
fixed #12227 - properly load addons from cppcheck.cfg in GUI (danma…
Browse files Browse the repository at this point in the history
…r#5716)

The GUI was asserting when addons were specified in the `cppcheck.cfg`
since we did not generate the info for them.
  • Loading branch information
firewave authored Dec 6, 2023
1 parent 6aa3478 commit 3fc62ce
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 34 deletions.
82 changes: 48 additions & 34 deletions gui/mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -905,7 +905,43 @@ bool MainWindow::tryLoadLibrary(Library *library, const QString& filename)
return true;
}

Settings MainWindow::getCppcheckSettings() {
void MainWindow::loadAddon(Settings &settings, const QString &filesDir, const QString &pythonCmd, const QString& addon)
{
QString addonFilePath = ProjectFile::getAddonFilePath(filesDir, addon);
if (addonFilePath.isEmpty())
return; // TODO: report an error

addonFilePath.replace(QChar('\\'), QChar('/'));

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());
}
}
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
settings.addonInfos.emplace_back(std::move(addonInfo));

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

Settings MainWindow::getCppcheckSettings()
{
saveSettings(); // Save settings

Settings result;
Expand All @@ -916,10 +952,20 @@ Settings MainWindow::getCppcheckSettings() {
if (!std)
QMessageBox::critical(this, tr("Error"), tr("Failed to load %1. Your Cppcheck installation is broken. You can use --data-dir=<directory> at the command line to specify where this file is located. Please note that --data-dir is supposed to be used by installation scripts and therefore the GUI does not start when it is used, all that happens is that the setting is configured.").arg("std.cfg"));

const QString filesDir(getDataDir());
const QString pythonCmd = fromNativePath(mSettings->value(SETTINGS_PYTHON_PATH).toString());

{
const QString cfgErr = QString::fromStdString(result.loadCppcheckCfg());
if (!cfgErr.isEmpty())
QMessageBox::critical(this, tr("Error"), tr("Failed to load %1 - %2").arg("cppcheck.cfg").arg(cfgErr));

const auto cfgAddons = result.addons;
result.addons.clear();
for (const std::string& addon : cfgAddons) {
// TODO: support addons which are a script and not a file
loadAddon(result, filesDir, pythonCmd, QString::fromStdString(addon));
}
}

// If project file loaded, read settings from it
Expand Down Expand Up @@ -998,40 +1044,8 @@ Settings MainWindow::getCppcheckSettings() {
for (const QString& s : mProjectFile->getCheckUnknownFunctionReturn())
result.checkUnknownFunctionReturn.insert(s.toStdString());

QString filesDir(getDataDir());
const QString pythonCmd = fromNativePath(mSettings->value(SETTINGS_PYTHON_PATH).toString());
for (const QString& addon : mProjectFile->getAddons()) {
QString addonFilePath = ProjectFile::getAddonFilePath(filesDir, addon);
if (addonFilePath.isEmpty())
continue;

addonFilePath.replace(QChar('\\'), QChar('/'));

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());
}
}
picojson::value json;
json.set(std::move(obj));
std::string json_str = json.serialize();

AddonInfo addonInfo;
addonInfo.getAddonInfo(json_str, result.exename);
result.addonInfos.emplace_back(std::move(addonInfo));

result.addons.emplace(std::move(json_str));
loadAddon(result, filesDir, pythonCmd, addon);
}

if (isCppcheckPremium()) {
Expand Down
2 changes: 2 additions & 0 deletions gui/mainwindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -401,6 +401,8 @@ private slots:
*/
bool tryLoadLibrary(Library *library, const QString& filename);

void loadAddon(Settings &settings, const QString &filesDir, const QString &pythonCmd, const QString& addon);

/**
* @brief Update project MRU items in File-menu.
*/
Expand Down
1 change: 1 addition & 0 deletions releasenotes.txt
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,4 @@ Other:
- fixed CMake build with UBSAN and GCC
- Added command-line options "--fsigned-char" and "--funsigned-char" to control the signess of the "char" type. This overrides previously specified "--platform" options and is overrides by following ones.
- An error is now reported when the "cppcheck.cfg" is invalid. The CLI version will also exit with a failure in that case.
- Fixed loading of addons from "cppcheck.cfg" in the GUI application.

0 comments on commit 3fc62ce

Please sign in to comment.