Skip to content

Commit 1233774

Browse files
committed
Fix #14067 (cmdlineparser: tweaks to --premium) (#7734)
1 parent d6a6f69 commit 1233774

3 files changed

Lines changed: 41 additions & 2 deletions

File tree

cli/cmdlineparser.cpp

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1103,12 +1103,14 @@ CmdLineParser::Result CmdLineParser::parseFromArgs(int argc, const char* const a
11031103
mSettings.premiumArgs += " ";
11041104
const std::string p(argv[i] + 10);
11051105
const std::string p2(p.find('=') != std::string::npos ? p.substr(0, p.find('=')) : "");
1106-
if (!valid.count(p) && !valid2.count(p2)) {
1106+
const bool isCodingStandard = startsWith(p, "autosar") || startsWith(p,"cert-") || startsWith(p,"misra-");
1107+
const std::string p3(endsWith(p,":all") && isCodingStandard ? p.substr(0,p.rfind(':')) : p);
1108+
if (!valid.count(p3) && !valid2.count(p2)) {
11071109
mLogger.printError("invalid --premium option '" + (p2.empty() ? p : p2) + "'.");
11081110
return Result::Fail;
11091111
}
11101112
mSettings.premiumArgs += "--" + p;
1111-
if (startsWith(p, "autosar") || startsWith(p, "cert") || startsWith(p, "misra")) {
1113+
if (isCodingStandard) {
11121114
// All checkers related to the coding standard should be enabled. The coding standards
11131115
// do not all undefined behavior or portability issues.
11141116
mSettings.addEnabled("warning");
@@ -1862,9 +1864,13 @@ void CmdLineParser::printHelp() const
18621864
" * misra-c-2025 Misra C 2025\n"
18631865
" * misra-c++-2008 Misra C++ 2008\n"
18641866
" * misra-c++-2023 Misra C++ 2023\n"
1867+
" By default 'Misra/Cert C' only checks C files.\n"
1868+
" By default 'Autosar/Misra/Cert C++' only checks C++ files.\n"
1869+
" To check all files, append \":all\" i.e. --premium=misra-c++-2023:all.\n"
18651870
" Other:\n"
18661871
" * bughunting Soundy analysis\n"
18671872
" * cert-c-int-precision=BITS Integer precision to use in Cert C analysis.\n"
1873+
" * metrics Calculate metrics. Metrics are only reported in xmlv3 output.\n"
18681874
" * safety Turn on safety certified behavior (ON by default)\n"
18691875
" * safety-off Turn off safety certified behavior\n";
18701876
}

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
@@ -236,6 +236,7 @@ class TestCmdlineParser : public TestFixture {
236236
TEST_CASE(premiumOptions3);
237237
TEST_CASE(premiumOptions4);
238238
TEST_CASE(premiumOptions5);
239+
TEST_CASE(premiumOptionsAll);
239240
TEST_CASE(premiumOptionsMetrics);
240241
TEST_CASE(premiumOptionsCertCIntPrecision);
241242
TEST_CASE(premiumOptionsLicenseFile);
@@ -1456,6 +1457,23 @@ class TestCmdlineParser : public TestFixture {
14561457
ASSERT_EQUALS(false, settings->severity.isEnabled(Severity::warning));
14571458
}
14581459

1460+
void premiumOptionsAll() {
1461+
REDIRECT;
1462+
asPremium();
1463+
const char * const argv[] = {
1464+
"cppcheck",
1465+
"--premium=autosar:all",
1466+
"--premium=cert-c:all",
1467+
"--premium=cert-c++:all",
1468+
"--premium=misra-c-2023:all",
1469+
"--premium=misra-c++-2023:all",
1470+
"file.c"
1471+
};
1472+
ASSERT_EQUALS_ENUM(CmdLineParser::Result::Success, parseFromArgs(argv));
1473+
ASSERT_EQUALS("--autosar:all --cert-c:all --cert-c++:all --misra-c-2023:all --misra-c++-2023:all",
1474+
settings->premiumArgs);
1475+
}
1476+
14591477
void premiumOptionsMetrics() {
14601478
REDIRECT;
14611479
asPremium();

0 commit comments

Comments
 (0)