Skip to content

Fix #13831 (do not execute misra.py in cppcheck premium when --premium=misra-c-.. options are used) #7506

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
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
2 changes: 0 additions & 2 deletions cli/cmdlineparser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1167,8 +1167,6 @@ CmdLineParser::Result CmdLineParser::parseFromArgs(int argc, const char* const a
return Result::Fail;
}
mSettings.premiumArgs += "--" + p;
if (p == "misra-c-2012" || p == "misra-c-2023")
mSettings.addons.emplace("misra");
if (startsWith(p, "autosar") || startsWith(p, "cert") || startsWith(p, "misra")) {
// All checkers related to the coding standard should be enabled. The coding standards
// do not all undefined behavior or portability issues.
Expand Down
10 changes: 5 additions & 5 deletions gui/mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1010,10 +1010,7 @@ QString MainWindow::loadAddon(Settings &settings, const QString &filesDir, const
if (!misraFile.isEmpty()) {
QString arg;
picojson::array arr;
if (misraFile.endsWith(".pdf", Qt::CaseInsensitive))
arg = "--misra-pdf=" + misraFile;
else
arg = "--rule-texts=" + misraFile;
arg = "--rule-texts=" + misraFile;
arr.emplace_back(arg.toStdString());
obj["args"] = picojson::value(arr);
}
Expand All @@ -1039,6 +1036,7 @@ bool MainWindow::getCppcheckSettings(Settings& settings, Suppressions& supprs)
Settings::terminate(true);

settings.exename = QCoreApplication::applicationFilePath().toStdString();
settings.templateFormat = "{file}:{line}:{column}: {severity}:{inconclusive:inconclusive:} {message} [{id}]";

// default to --check-level=normal for GUI for now
settings.setCheckLevel(Settings::CheckLevel::normal);
Expand Down Expand Up @@ -1157,6 +1155,8 @@ bool MainWindow::getCppcheckSettings(Settings& settings, Suppressions& supprs)
settings.checkUnknownFunctionReturn.insert(s.toStdString());

for (const QString& addon : mProjectFile->getAddons()) {
if (isCppcheckPremium() && addon == "misra")
continue;
const QString addonError = loadAddon(settings, filesDir, pythonCmd, addon);
if (!addonError.isEmpty()) {
QMessageBox::critical(this, tr("Error"), tr("%1\n\nAnalysis is aborted.").arg(addonError));
Expand All @@ -1172,7 +1172,7 @@ bool MainWindow::getCppcheckSettings(Settings& settings, Suppressions& supprs)
premiumArgs += " --cert-c-int-precision=" + QString::number(mProjectFile->getCertIntPrecision());
for (const QString& c: mProjectFile->getCodingStandards())
premiumArgs += " --" + c;
if (!premiumArgs.contains("misra") && mProjectFile->getAddons().contains("misra"))
if (!premiumArgs.contains("--misra-c-") && mProjectFile->getAddons().contains("misra"))
premiumArgs += " --misra-c-2012";
settings.premiumArgs = premiumArgs.mid(1).toStdString();
settings.setMisraRuleTexts(CheckThread::executeCommand);
Expand Down
2 changes: 1 addition & 1 deletion lib/errorlogger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1016,7 +1016,7 @@ std::string getGuideline(const std::string &errId, ReportType reportType,
}
break;
case ReportType::misraC:
if (errId.rfind("misra-c20", 0) == 0)
if (errId.rfind("misra-c20", 0) == 0 || errId.rfind("premium-misra-c-20", 0) == 0)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this fix #13730?

guideline = errId.substr(errId.rfind('-') + 1);
break;
case ReportType::misraCpp2008:
Expand Down
16 changes: 16 additions & 0 deletions test/cli/premium_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,3 +114,19 @@ def _get_hash(s:str):
assert hash1 != hash2


def test_misra_py(tmpdir):
# 13831 - do not execute misra.py when --premium=misra-c-2012 is used
test_file = os.path.join(tmpdir, 'test.c')
with open(test_file, 'wt') as f:
f.write('void foo();\n')

exe = __copy_cppcheck_premium(tmpdir)

# ensure that misra.py is not available:
_, stdout, _ = cppcheck(['--enable=style', '--addon=misra', test_file], cppcheck_exe=exe)
assert 'Did not find addon misra.py' in stdout

# Execute misra
_, stdout, _ = cppcheck(['--enable=style', '--premium=misra-c-2012', test_file], cppcheck_exe=exe)
assert 'misra.py' not in stdout # Did not find misra.py
assert 'Checking' in stdout
Loading