Skip to content

Commit 3fc62ce

Browse files
authored
fixed #12227 - properly load addons from cppcheck.cfg in GUI (#5716)
The GUI was asserting when addons were specified in the `cppcheck.cfg` since we did not generate the info for them.
1 parent 6aa3478 commit 3fc62ce

3 files changed

Lines changed: 51 additions & 34 deletions

File tree

gui/mainwindow.cpp

Lines changed: 48 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -905,7 +905,43 @@ bool MainWindow::tryLoadLibrary(Library *library, const QString& filename)
905905
return true;
906906
}
907907

908-
Settings MainWindow::getCppcheckSettings() {
908+
void MainWindow::loadAddon(Settings &settings, const QString &filesDir, const QString &pythonCmd, const QString& addon)
909+
{
910+
QString addonFilePath = ProjectFile::getAddonFilePath(filesDir, addon);
911+
if (addonFilePath.isEmpty())
912+
return; // TODO: report an error
913+
914+
addonFilePath.replace(QChar('\\'), QChar('/'));
915+
916+
picojson::object obj;
917+
obj["script"] = picojson::value(addonFilePath.toStdString());
918+
if (!pythonCmd.isEmpty())
919+
obj["python"] = picojson::value(pythonCmd.toStdString());
920+
921+
if (!isCppcheckPremium() && addon == "misra") {
922+
const QString misraFile = fromNativePath(mSettings->value(SETTINGS_MISRA_FILE).toString());
923+
if (!misraFile.isEmpty()) {
924+
QString arg;
925+
if (misraFile.endsWith(".pdf", Qt::CaseInsensitive))
926+
arg = "--misra-pdf=" + misraFile;
927+
else
928+
arg = "--rule-texts=" + misraFile;
929+
obj["args"] = picojson::value(arg.toStdString());
930+
}
931+
}
932+
picojson::value json;
933+
json.set(std::move(obj));
934+
std::string json_str = json.serialize();
935+
936+
AddonInfo addonInfo;
937+
addonInfo.getAddonInfo(json_str, settings.exename); // TODO: handle error
938+
settings.addonInfos.emplace_back(std::move(addonInfo));
939+
940+
settings.addons.emplace(std::move(json_str));
941+
}
942+
943+
Settings MainWindow::getCppcheckSettings()
944+
{
909945
saveSettings(); // Save settings
910946

911947
Settings result;
@@ -916,10 +952,20 @@ Settings MainWindow::getCppcheckSettings() {
916952
if (!std)
917953
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"));
918954

955+
const QString filesDir(getDataDir());
956+
const QString pythonCmd = fromNativePath(mSettings->value(SETTINGS_PYTHON_PATH).toString());
957+
919958
{
920959
const QString cfgErr = QString::fromStdString(result.loadCppcheckCfg());
921960
if (!cfgErr.isEmpty())
922961
QMessageBox::critical(this, tr("Error"), tr("Failed to load %1 - %2").arg("cppcheck.cfg").arg(cfgErr));
962+
963+
const auto cfgAddons = result.addons;
964+
result.addons.clear();
965+
for (const std::string& addon : cfgAddons) {
966+
// TODO: support addons which are a script and not a file
967+
loadAddon(result, filesDir, pythonCmd, QString::fromStdString(addon));
968+
}
923969
}
924970

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

1001-
QString filesDir(getDataDir());
1002-
const QString pythonCmd = fromNativePath(mSettings->value(SETTINGS_PYTHON_PATH).toString());
10031047
for (const QString& addon : mProjectFile->getAddons()) {
1004-
QString addonFilePath = ProjectFile::getAddonFilePath(filesDir, addon);
1005-
if (addonFilePath.isEmpty())
1006-
continue;
1007-
1008-
addonFilePath.replace(QChar('\\'), QChar('/'));
1009-
1010-
picojson::object obj;
1011-
obj["script"] = picojson::value(addonFilePath.toStdString());
1012-
if (!pythonCmd.isEmpty())
1013-
obj["python"] = picojson::value(pythonCmd.toStdString());
1014-
1015-
if (!isCppcheckPremium() && addon == "misra") {
1016-
const QString misraFile = fromNativePath(mSettings->value(SETTINGS_MISRA_FILE).toString());
1017-
if (!misraFile.isEmpty()) {
1018-
QString arg;
1019-
if (misraFile.endsWith(".pdf", Qt::CaseInsensitive))
1020-
arg = "--misra-pdf=" + misraFile;
1021-
else
1022-
arg = "--rule-texts=" + misraFile;
1023-
obj["args"] = picojson::value(arg.toStdString());
1024-
}
1025-
}
1026-
picojson::value json;
1027-
json.set(std::move(obj));
1028-
std::string json_str = json.serialize();
1029-
1030-
AddonInfo addonInfo;
1031-
addonInfo.getAddonInfo(json_str, result.exename);
1032-
result.addonInfos.emplace_back(std::move(addonInfo));
1033-
1034-
result.addons.emplace(std::move(json_str));
1048+
loadAddon(result, filesDir, pythonCmd, addon);
10351049
}
10361050

10371051
if (isCppcheckPremium()) {

gui/mainwindow.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -401,6 +401,8 @@ private slots:
401401
*/
402402
bool tryLoadLibrary(Library *library, const QString& filename);
403403

404+
void loadAddon(Settings &settings, const QString &filesDir, const QString &pythonCmd, const QString& addon);
405+
404406
/**
405407
* @brief Update project MRU items in File-menu.
406408
*/

releasenotes.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,4 @@ Other:
3333
- fixed CMake build with UBSAN and GCC
3434
- 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.
3535
- An error is now reported when the "cppcheck.cfg" is invalid. The CLI version will also exit with a failure in that case.
36+
- Fixed loading of addons from "cppcheck.cfg" in the GUI application.

0 commit comments

Comments
 (0)