Skip to content

Commit

Permalink
cli: add --premium option for license file (#6858)
Browse files Browse the repository at this point in the history
  • Loading branch information
danmar authored Oct 4, 2024
1 parent 3466bdc commit addfb01
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 3 deletions.
13 changes: 10 additions & 3 deletions cli/cmdlineparser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -969,7 +969,8 @@ CmdLineParser::Result CmdLineParser::parseFromArgs(int argc, const char* const a
}

// Special Cppcheck Premium options
else if (std::strncmp(argv[i], "--premium=", 10) == 0 && isCppcheckPremium()) {
else if ((std::strncmp(argv[i], "--premium=", 10) == 0 || std::strncmp(argv[i], "--premium-", 10) == 0) && isCppcheckPremium()) {
// valid options --premium=..
const std::set<std::string> valid{
"autosar",
"cert-c-2016",
Expand All @@ -983,6 +984,11 @@ CmdLineParser::Result CmdLineParser::parseFromArgs(int argc, const char* const a
"misra-cpp-2023",
"bughunting",
"safety"};
// valid options --premium-..=
const std::set<std::string> valid2{
"cert-c-int-precision",
"license-file"
};

if (std::strcmp(argv[i], "--premium=safety-off") == 0) {
mSettings.safety = false;
Expand All @@ -993,8 +999,9 @@ CmdLineParser::Result CmdLineParser::parseFromArgs(int argc, const char* const a
if (!mSettings.premiumArgs.empty())
mSettings.premiumArgs += " ";
const std::string p(argv[i] + 10);
if (!valid.count(p) && !startsWith(p, "cert-c-int-precision=")) {
mLogger.printError("invalid --premium option '" + p + "'.");
const std::string p2(p.find('=') != std::string::npos ? p.substr(0, p.find('=')) : "");
if (!valid.count(p) && !valid2.count(p2)) {
mLogger.printError("invalid --premium option '" + (p2.empty() ? p : p2) + "'.");
return Result::Fail;
}
mSettings.premiumArgs += "--" + p;
Expand Down
18 changes: 18 additions & 0 deletions test/testcmdlineparser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,8 @@ class TestCmdlineParser : public TestFixture {
TEST_CASE(premiumOptions3);
TEST_CASE(premiumOptions4);
TEST_CASE(premiumOptions5);
TEST_CASE(premiumOptionsCertCIntPrecision);
TEST_CASE(premiumOptionsLicenseFile);
TEST_CASE(premiumOptionsInvalid1);
TEST_CASE(premiumOptionsInvalid2);
TEST_CASE(premiumSafety);
Expand Down Expand Up @@ -1290,6 +1292,22 @@ class TestCmdlineParser : public TestFixture {
ASSERT_EQUALS(false, settings->severity.isEnabled(Severity::warning));
}

void premiumOptionsCertCIntPrecision() {
REDIRECT;
asPremium();
const char * const argv[] = {"cppcheck", "--premium-cert-c-int-precision=12", "file.c"};
ASSERT_EQUALS_ENUM(CmdLineParser::Result::Success, parser->parseFromArgs(3, argv));
ASSERT_EQUALS("--cert-c-int-precision=12", settings->premiumArgs);
}

void premiumOptionsLicenseFile() {
REDIRECT;
asPremium();
const char * const argv[] = {"cppcheck", "--premium-license-file=file.lic", "file.c"};
ASSERT_EQUALS_ENUM(CmdLineParser::Result::Success, parser->parseFromArgs(3, argv));
ASSERT_EQUALS("--license-file=file.lic", settings->premiumArgs);
}

void premiumOptionsInvalid1() {
REDIRECT;
asPremium();
Expand Down

0 comments on commit addfb01

Please sign in to comment.