Suppressions: Add special case for backwards compatibility. suppression comment after a {.

This commit is contained in:
Daniel Marjamäki 2020-10-02 18:56:26 +02:00
parent 7ff6d691e2
commit ea3a9d658a
4 changed files with 27 additions and 3 deletions

View File

@ -166,10 +166,20 @@ static void inlineSuppressions(const simplecpp::TokenList &tokens, Settings &mSe
} }
relativeFilename = Path::simplifyPath(relativeFilename); relativeFilename = Path::simplifyPath(relativeFilename);
// special handling when suppressing { warnings for backwards compatibility
const bool thisAndNextLine = tok->previous &&
tok->previous->previous &&
tok->next &&
!sameline(tok->previous->previous, tok->previous) &&
tok->location.line + 1 == tok->next->location.line &&
tok->location.fileIndex == tok->next->location.fileIndex &&
tok->previous->str() == "{";
// Add the suppressions. // Add the suppressions.
for (Suppressions::Suppression &suppr : inlineSuppressions) { for (Suppressions::Suppression &suppr : inlineSuppressions) {
suppr.fileName = relativeFilename; suppr.fileName = relativeFilename;
suppr.lineNumber = tok->location.line; suppr.lineNumber = tok->location.line;
suppr.thisAndNextLine = thisAndNextLine;
mSettings.nomsg.addSuppression(suppr); mSettings.nomsg.addSuppression(suppr);
} }
} }

View File

@ -291,8 +291,10 @@ bool Suppressions::Suppression::isSuppressed(const Suppressions::ErrorMessage &e
return false; return false;
if (!fileName.empty() && !matchglob(fileName, errmsg.getFileName())) if (!fileName.empty() && !matchglob(fileName, errmsg.getFileName()))
return false; return false;
if (lineNumber != NO_LINE && lineNumber != errmsg.lineNumber) if (lineNumber != NO_LINE && lineNumber != errmsg.lineNumber) {
if (!thisAndNextLine || lineNumber + 1 != errmsg.lineNumber)
return false; return false;
}
if (!symbolName.empty()) { if (!symbolName.empty()) {
for (std::string::size_type pos = 0; pos < errmsg.symbolNames.size();) { for (std::string::size_type pos = 0; pos < errmsg.symbolNames.size();) {
const std::string::size_type pos2 = errmsg.symbolNames.find('\n',pos); const std::string::size_type pos2 = errmsg.symbolNames.find('\n',pos);

View File

@ -53,7 +53,7 @@ public:
Suppression(const Suppression &other) { Suppression(const Suppression &other) {
*this = other; *this = other;
} }
Suppression(const std::string &id, const std::string &file, int line=NO_LINE) : errorId(id), fileName(file), lineNumber(line), hash(0), matched(false) {} Suppression(const std::string &id, const std::string &file, int line=NO_LINE) : errorId(id), fileName(file), lineNumber(line), hash(0), thisAndNextLine(false), matched(false) {}
Suppression & operator=(const Suppression &other) { Suppression & operator=(const Suppression &other) {
errorId = other.errorId; errorId = other.errorId;
@ -61,6 +61,7 @@ public:
lineNumber = other.lineNumber; lineNumber = other.lineNumber;
symbolName = other.symbolName; symbolName = other.symbolName;
hash = other.hash; hash = other.hash;
thisAndNextLine = other.thisAndNextLine;
matched = other.matched; matched = other.matched;
return *this; return *this;
} }
@ -76,6 +77,8 @@ public:
return symbolName < other.symbolName; return symbolName < other.symbolName;
if (hash != other.hash) if (hash != other.hash)
return hash < other.hash; return hash < other.hash;
if (thisAndNextLine != other.thisAndNextLine)
return thisAndNextLine;
return false; return false;
} }
@ -101,6 +104,7 @@ public:
int lineNumber; int lineNumber;
std::string symbolName; std::string symbolName;
std::size_t hash; std::size_t hash;
bool thisAndNextLine; // Special case for backwards compatibility: { // cppcheck-suppress something
bool matched; bool matched;
enum { NO_LINE = -1 }; enum { NO_LINE = -1 };

View File

@ -24,3 +24,11 @@ def test_unmatched_suppression_path_with_extra_stuf():
ret, stdout, stderr = cppcheck(['--inline-suppr', '--enable=information', '--error-exitcode=1', './proj-inline-suppress/2.c']) ret, stdout, stderr = cppcheck(['--inline-suppr', '--enable=information', '--error-exitcode=1', './proj-inline-suppress/2.c'])
assert ret == 1 assert ret == 1
assert 'Unmatched suppression: some_warning_id' in stderr assert 'Unmatched suppression: some_warning_id' in stderr
def test_backwards_compatibility():
ret, stdout, stderr = cppcheck(['--enable=unusedFunction', 'proj-inline-suppress/3.cpp'])
assert len(stderr) > 0
ret, stdout, stderr = cppcheck(['--inline-suppr', '--enable=unusedFunction', 'proj-inline-suppress/3.cpp'])
assert ret == 0
assert stderr == ''