Skip to content

Commit

Permalink
Fix #12342 (Show premium autosar/misra/cert style issues even if --en…
Browse files Browse the repository at this point in the history
…able is not used)
  • Loading branch information
danmar committed Jan 12, 2024
1 parent 14e540a commit 0ba4b27
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 2 deletions.
21 changes: 19 additions & 2 deletions lib/cppcheck.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1472,8 +1472,12 @@ void CppCheck::executeAddons(const std::vector<std::string>& files, const std::s
continue;
errmsg.severity = Severity::internal;
}
else if (!mSettings.severity.isEnabled(errmsg.severity))
continue;
else if (!mSettings.severity.isEnabled(errmsg.severity)) {
// Do not filter out premium misra/cert/autosar messages that has been
// explicitly enabled with a --premium option
if (!isPremiumCodingStandardId(errmsg.id))
continue;
}
errmsg.file0 = file0;

reportErr(errmsg);
Expand Down Expand Up @@ -1866,3 +1870,16 @@ void CppCheck::printTimerResults(SHOWTIME_MODES mode)
{
s_timerResults.showResults(mode);
}

bool CppCheck::isPremiumCodingStandardId(const std::string& id) const {
std::vector<std::string> premiumCodingStandards;
if (mSettings.premiumArgs.find("--misra") != std::string::npos) {
if (startsWith(id, "misra-") || startsWith(id, "premium-misra-"))
return true;
}
if (mSettings.premiumArgs.find("--cert") != std::string::npos && startsWith(id, "premium-cert-"))
return true;
if (mSettings.premiumArgs.find("--autosar") != std::string::npos && startsWith(id, "premium-autosar-"))
return true;
return false;
}
2 changes: 2 additions & 0 deletions lib/cppcheck.h
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,8 @@ class CPPCHECKLIB CppCheck : ErrorLogger {
static void resetTimerResults();
static void printTimerResults(SHOWTIME_MODES mode);

bool isPremiumCodingStandardId(const std::string& id) const;

private:
#ifdef HAVE_RULES
/** Are there "simple" rules */
Expand Down
26 changes: 26 additions & 0 deletions test/testcppcheck.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ class TestCppcheck : public TestFixture {
TEST_CASE(checkWithFS);
TEST_CASE(suppress_error_library);
TEST_CASE(unique_errors);
TEST_CASE(isPremiumCodingStandardId);
}

void getErrorMessages() const {
Expand Down Expand Up @@ -180,6 +181,31 @@ class TestCppcheck : public TestFixture {
ASSERT_EQUALS("nullPointer", it->id);
}

void isPremiumCodingStandardId() {
ErrorLogger2 errorLogger;
CppCheck cppcheck(errorLogger, false, {});

cppcheck.settings().premiumArgs = "";
ASSERT_EQUALS(false, cppcheck.isPremiumCodingStandardId("misra-c2012-0.0"));
ASSERT_EQUALS(false, cppcheck.isPremiumCodingStandardId("misra-c2023-0.0"));
ASSERT_EQUALS(false, cppcheck.isPremiumCodingStandardId("premium-misra-c2012-0.0"));
ASSERT_EQUALS(false, cppcheck.isPremiumCodingStandardId("premium-misra-c2023-0.0"));
ASSERT_EQUALS(false, cppcheck.isPremiumCodingStandardId("premium-misra-c++2008-0.0.0"));
ASSERT_EQUALS(false, cppcheck.isPremiumCodingStandardId("premium-misra-c++2023-0.0.0"));
ASSERT_EQUALS(false, cppcheck.isPremiumCodingStandardId("premium-cert-int50-cpp"));
ASSERT_EQUALS(false, cppcheck.isPremiumCodingStandardId("premium-autosar-0-0-0"));

cppcheck.settings().premiumArgs = "--misra-c-2012 --cert-c++-2016 --autosar";
ASSERT_EQUALS(true, cppcheck.isPremiumCodingStandardId("misra-c2012-0.0"));
ASSERT_EQUALS(true, cppcheck.isPremiumCodingStandardId("misra-c2023-0.0"));
ASSERT_EQUALS(true, cppcheck.isPremiumCodingStandardId("premium-misra-c2012-0.0"));
ASSERT_EQUALS(true, cppcheck.isPremiumCodingStandardId("premium-misra-c2023-0.0"));
ASSERT_EQUALS(true, cppcheck.isPremiumCodingStandardId("premium-misra-c++2008-0.0.0"));
ASSERT_EQUALS(true, cppcheck.isPremiumCodingStandardId("premium-misra-c++2023-0.0.0"));
ASSERT_EQUALS(true, cppcheck.isPremiumCodingStandardId("premium-cert-int50-cpp"));
ASSERT_EQUALS(true, cppcheck.isPremiumCodingStandardId("premium-autosar-0-0-0"));
}

// TODO: test suppressions
// TODO: test all with FS
};
Expand Down

0 comments on commit 0ba4b27

Please sign in to comment.