Skip to content

Commit

Permalink
split off handling of inline suppressions
Browse files Browse the repository at this point in the history
  • Loading branch information
firewave committed May 5, 2024
1 parent 637b27c commit dd9c938
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 7 deletions.
24 changes: 18 additions & 6 deletions lib/cppcheck.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -586,6 +586,10 @@ unsigned int CppCheck::check(const FileSettings &fs)
const unsigned int returnValue = temp.checkFile(Path::simplifyPath(fs.filename()), fs.cfg);
for (const auto& suppr : temp.mSettings.supprs.nomsg.getSuppressions())
{
// skip inline suppressions - are handled in checkFile()
if (suppr.isInline)
continue;

const bool res = mSettings.supprs.nomsg.updateSuppressionState(suppr);
if (!res)
throw InternalError(nullptr, "could not update suppression '" + suppr.errorId + "'"); // TODO: remove
Expand Down Expand Up @@ -903,8 +907,10 @@ unsigned int CppCheck::checkFile(const std::string& filename, const std::string
fdump << "</dump>" << std::endl;
}

// Need to call this even if the hash will skip this configuration
mSettings.supprs.nomsg.markUnmatchedInlineSuppressionsAsChecked(tokenizer);
if (mSettings.inlineSuppressions) {
// Need to call this even if the hash will skip this configuration
mSettings.supprs.nomsg.markUnmatchedInlineSuppressionsAsChecked(tokenizer);
}

// Skip if we already met the same simplified token list
if (mSettings.force || mSettings.maxConfigs > 1) {
Expand Down Expand Up @@ -994,10 +1000,16 @@ unsigned int CppCheck::checkFile(const std::string& filename, const std::string
mAnalyzerInformation.close();
}

// In jointSuppressionReport mode, unmatched suppressions are
// collected after all files are processed
if (!mSettings.useSingleJob() && (mSettings.severity.isEnabled(Severity::information) || mSettings.checkConfiguration)) {
SuppressionList::reportUnmatchedSuppressions(mSettings.supprs.nomsg.getUnmatchedLocalSuppressions(filename, (bool)mUnusedFunctionsCheck), *this);
if (mSettings.severity.isEnabled(Severity::information) || mSettings.checkConfiguration) {
// TODO: check result?
SuppressionList::reportUnmatchedSuppressions(mSettings.supprs.nomsg.getUnmatchedInlineSuppressions(false), *this);

// In jointSuppressionReport mode, unmatched suppressions are
// collected after all files are processed
if (!mSettings.useSingleJob()) {
// TODO: check result?
SuppressionList::reportUnmatchedSuppressions(mSettings.supprs.nomsg.getUnmatchedLocalSuppressions(filename, (bool)mUnusedFunctionsCheck), *this);
}
}

mErrorList.clear();
Expand Down
1 change: 1 addition & 0 deletions lib/preprocessor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,7 @@ static void addInlineSuppressions(const simplecpp::TokenList &tokens, const Sett
// Add the suppressions.
for (SuppressionList::Suppression &suppr : inlineSuppressions) {
suppr.fileName = relativeFilename;
suppr.isInline = true; // TODO: set earlier

if (SuppressionList::Type::blockBegin == suppr.type)
{
Expand Down
23 changes: 23 additions & 0 deletions lib/suppressions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -492,6 +492,8 @@ std::list<SuppressionList::Suppression> SuppressionList::getUnmatchedLocalSuppre
std::string tmpFile = Path::simplifyPath(file);
std::list<Suppression> result;
for (const Suppression &s : mSuppressions) {
if (s.isInline)
continue;
if (s.matched || ((s.lineNumber != Suppression::NO_LINE) && !s.checked))
continue;
if (s.type == SuppressionList::Type::macro)
Expand All @@ -513,6 +515,8 @@ std::list<SuppressionList::Suppression> SuppressionList::getUnmatchedGlobalSuppr
{
std::list<Suppression> result;
for (const Suppression &s : mSuppressions) {
if (s.isInline)
continue;
if (s.matched || ((s.lineNumber != Suppression::NO_LINE) && !s.checked))
continue;
if (s.hash > 0)
Expand All @@ -528,6 +532,25 @@ std::list<SuppressionList::Suppression> SuppressionList::getUnmatchedGlobalSuppr
return result;
}

std::list<SuppressionList::Suppression> SuppressionList::getUnmatchedInlineSuppressions(const bool unusedFunctionChecking) const
{
std::list<SuppressionList::Suppression> result;
for (const SuppressionList::Suppression &s : SuppressionList::mSuppressions) {
if (!s.isInline)
continue;
if (!s.checked)
continue;
if (s.matched)
continue;
if (s.hash > 0)
continue;
if (!unusedFunctionChecking && s.errorId == ID_UNUSEDFUNCTION)
continue;
result.push_back(s);
}
return result;
}

const std::list<SuppressionList::Suppression> &SuppressionList::getSuppressions() const
{
return mSuppressions;
Expand Down
7 changes: 7 additions & 0 deletions lib/suppressions.h
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ class CPPCHECKLIB SuppressionList {
bool thisAndNextLine{}; // Special case for backwards compatibility: { // cppcheck-suppress something
bool matched{};
bool checked{}; // for inline suppressions, checked or not
bool isInline{};

enum { NO_LINE = -1 };
};
Expand Down Expand Up @@ -244,6 +245,12 @@ class CPPCHECKLIB SuppressionList {
*/
std::list<Suppression> getUnmatchedGlobalSuppressions(const bool unusedFunctionChecking) const;

/**
* @brief Returns list of unmatched inline suppressions.
* @return list of unmatched suppressions
*/
std::list<Suppression> getUnmatchedInlineSuppressions(const bool unusedFunctionChecking) const;

/**
* @brief Returns list of all suppressions.
* @return list of suppressions
Expand Down
2 changes: 1 addition & 1 deletion test/testsuppressions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -874,7 +874,7 @@ class TestSuppressions : public TestFixture {
"#define DIV(A,B) A/B\n"
"a = DIV(10,1);\n",
"");
ASSERT_EQUALS("", errout_str()); // <- no unmatched suppression reported for macro suppression
ASSERT_EQUALS("[test.cpp:2]: (information) Unmatched suppression: abc\n", errout_str());
}

void suppressionsSettingsFiles() {
Expand Down

0 comments on commit dd9c938

Please sign in to comment.