Skip to content

Commit

Permalink
fixed #13542 - added CLI option --emit-duplicates to allow showing …
Browse files Browse the repository at this point in the history
…duplicated findings (#7201)
  • Loading branch information
firewave authored Jan 11, 2025
1 parent a5cf0dd commit 7dc579a
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 2 deletions.
3 changes: 3 additions & 0 deletions cli/cmdlineparser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -666,6 +666,9 @@ CmdLineParser::Result CmdLineParser::parseFromArgs(int argc, const char* const a
else if (std::strcmp(argv[i], "--dump") == 0)
mSettings.dump = true;

else if (std::strcmp(argv[i], "--emit-duplicates") == 0)
mSettings.emitDuplicates = true;

else if (std::strncmp(argv[i], "--enable=", 9) == 0) {
const std::string enable_arg = argv[i] + 9;
const std::string errmsg = mSettings.addEnabled(enable_arg);
Expand Down
2 changes: 1 addition & 1 deletion cli/cppcheckexecutor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -623,7 +623,7 @@ void StdLogger::reportErr(const ErrorMessage &msg)
// TODO: we generate a different message here then we log below
// TODO: there should be no need for verbose and default messages here
// Alert only about unique errors
if (!mShownErrors.insert(msg.toString(mSettings.verbose)).second)
if (!mSettings.emitDuplicates && !mShownErrors.insert(msg.toString(mSettings.verbose)).second)
return;

if (mSettings.outputFormat == Settings::OutputFormat::sarif)
Expand Down
3 changes: 3 additions & 0 deletions cli/executor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ bool Executor::hasToLog(const ErrorMessage &msg)
if (errmsg.empty())
return false;

if (mSettings.emitDuplicates)
return true;

std::lock_guard<std::mutex> lg(mErrorListSync);
if (mErrorList.emplace(std::move(errmsg)).second) {
return true;
Expand Down
2 changes: 1 addition & 1 deletion lib/cppcheck.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ class CppCheck::CppCheckLogger : public ErrorLogger
// Alert only about unique errors.
// This makes sure the errors of a single check() call are unique.
// TODO: get rid of this? This is forwarded to another ErrorLogger which is also doing this
if (!mErrorList.emplace(std::move(errmsg)).second)
if (!mSettings.emitDuplicates && !mErrorList.emplace(std::move(errmsg)).second)
return;

if (mAnalyzerInformation)
Expand Down
3 changes: 3 additions & 0 deletions lib/settings.h
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,9 @@ class CPPCHECKLIB WARN_UNUSED Settings {
/** @brief Is --dump given? */
bool dump{};

/** @brief Do not filter duplicated errors. */
bool emitDuplicates{};

/** @brief Name of the language that is enforced. Empty per default. */
Standards::Language enforcedLang{};

Expand Down
8 changes: 8 additions & 0 deletions test/testcmdlineparser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -421,6 +421,7 @@ class TestCmdlineParser : public TestFixture {
TEST_CASE(debugLookupPlatform);
TEST_CASE(maxTemplateRecursion);
TEST_CASE(maxTemplateRecursionMissingCount);
TEST_CASE(emitDuplicates);

TEST_CASE(ignorepaths1);
TEST_CASE(ignorepaths2);
Expand Down Expand Up @@ -2899,6 +2900,13 @@ class TestCmdlineParser : public TestFixture {
ASSERT_EQUALS("cppcheck: error: argument to '--max-template-recursion=' is not valid - not an integer.\n", logger->str());
}

void emitDuplicates() {
REDIRECT;
const char * const argv[] = {"cppcheck", "--emit-duplicates", "file.cpp"};
ASSERT_EQUALS_ENUM(CmdLineParser::Result::Success, parser->parseFromArgs(3, argv));
ASSERT_EQUALS(true, settings->emitDuplicates);
}

void ignorepaths1() {
REDIRECT;
const char * const argv[] = {"cppcheck", "-isrc", "file.cpp"};
Expand Down

0 comments on commit 7dc579a

Please sign in to comment.