Skip to content

Commit 0f066b5

Browse files
authored
Fix #14067 (cmdlineparser: tweaks to --premium) (danmar#7734)
1 parent ceb87bf commit 0f066b5

File tree

3 files changed

+41
-2
lines changed

3 files changed

+41
-2
lines changed

cli/cmdlineparser.cpp

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1117,12 +1117,14 @@ CmdLineParser::Result CmdLineParser::parseFromArgs(int argc, const char* const a
11171117
mSettings.premiumArgs += " ";
11181118
const std::string p(argv[i] + 10);
11191119
const std::string p2(p.find('=') != std::string::npos ? p.substr(0, p.find('=')) : "");
1120-
if (!valid.count(p) && !valid2.count(p2)) {
1120+
const bool isCodingStandard = startsWith(p, "autosar") || startsWith(p,"cert-") || startsWith(p,"misra-");
1121+
const std::string p3(endsWith(p,":all") && isCodingStandard ? p.substr(0,p.rfind(':')) : p);
1122+
if (!valid.count(p3) && !valid2.count(p2)) {
11211123
mLogger.printError("invalid --premium option '" + (p2.empty() ? p : p2) + "'.");
11221124
return Result::Fail;
11231125
}
11241126
mSettings.premiumArgs += "--" + p;
1125-
if (startsWith(p, "autosar") || startsWith(p, "cert") || startsWith(p, "misra")) {
1127+
if (isCodingStandard) {
11261128
// All checkers related to the coding standard should be enabled. The coding standards
11271129
// do not all undefined behavior or portability issues.
11281130
mSettings.addEnabled("warning");
@@ -1888,9 +1890,13 @@ void CmdLineParser::printHelp() const
18881890
" * misra-c-2025 Misra C 2025\n"
18891891
" * misra-c++-2008 Misra C++ 2008\n"
18901892
" * misra-c++-2023 Misra C++ 2023\n"
1893+
" By default 'Misra/Cert C' only checks C files.\n"
1894+
" By default 'Autosar/Misra/Cert C++' only checks C++ files.\n"
1895+
" To check all files, append \":all\" i.e. --premium=misra-c++-2023:all.\n"
18911896
" Other:\n"
18921897
" * bughunting Soundy analysis\n"
18931898
" * cert-c-int-precision=BITS Integer precision to use in Cert C analysis.\n"
1899+
" * metrics Calculate metrics. Metrics are only reported in xmlv3 output.\n"
18941900
" * safety Turn on safety certified behavior (ON by default)\n"
18951901
" * safety-off Turn off safety certified behavior\n";
18961902
}

man/manual-premium.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1272,6 +1272,21 @@ Command to activate Misra C++ 2023 checkers:
12721272

12731273
cppcheck --premium=misra-c++-2023 ....
12741274

1275+
### Checking all C and C++ files
1276+
1277+
The `cert-c` and `misra-c-*` coding standards target C and therefore the checkers only check C files by default.
1278+
1279+
The `autosar`, `cert-c++` and `misra-c++-*` coding standards target C++ and therefore the checkers only check C++ files by default.
1280+
1281+
If you want to check all files you can append ":all" to the coding standard. Example:
1282+
1283+
# Misra C checkers are executed on C files, not on C++ files
1284+
cppcheck --premium=misra-c-2025 path
1285+
1286+
# Misra C checkers are executed on C and C++ files
1287+
cppcheck --premium=misra-c-2025:all path
1288+
1289+
12751290
## Compliance report
12761291

12771292
### Graphical user interface

test/testcmdlineparser.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,7 @@ class TestCmdlineParser : public TestFixture {
241241
TEST_CASE(premiumOptions3);
242242
TEST_CASE(premiumOptions4);
243243
TEST_CASE(premiumOptions5);
244+
TEST_CASE(premiumOptionsAll);
244245
TEST_CASE(premiumOptionsMetrics);
245246
TEST_CASE(premiumOptionsCertCIntPrecision);
246247
TEST_CASE(premiumOptionsLicenseFile);
@@ -1498,6 +1499,23 @@ class TestCmdlineParser : public TestFixture {
14981499
ASSERT_EQUALS(false, settings->severity.isEnabled(Severity::warning));
14991500
}
15001501

1502+
void premiumOptionsAll() {
1503+
REDIRECT;
1504+
asPremium();
1505+
const char * const argv[] = {
1506+
"cppcheck",
1507+
"--premium=autosar:all",
1508+
"--premium=cert-c:all",
1509+
"--premium=cert-c++:all",
1510+
"--premium=misra-c-2023:all",
1511+
"--premium=misra-c++-2023:all",
1512+
"file.c"
1513+
};
1514+
ASSERT_EQUALS_ENUM(CmdLineParser::Result::Success, parseFromArgs(argv));
1515+
ASSERT_EQUALS("--autosar:all --cert-c:all --cert-c++:all --misra-c-2023:all --misra-c++-2023:all",
1516+
settings->premiumArgs);
1517+
}
1518+
15011519
void premiumOptionsMetrics() {
15021520
REDIRECT;
15031521
asPremium();

0 commit comments

Comments
 (0)