Skip to content

Commit ea3a9d6

Browse files
committed
Suppressions: Add special case for backwards compatibility. suppression comment after a {.
1 parent 7ff6d69 commit ea3a9d6

4 files changed

Lines changed: 27 additions & 3 deletions

File tree

lib/preprocessor.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,10 +166,20 @@ static void inlineSuppressions(const simplecpp::TokenList &tokens, Settings &mSe
166166
}
167167
relativeFilename = Path::simplifyPath(relativeFilename);
168168

169+
// special handling when suppressing { warnings for backwards compatibility
170+
const bool thisAndNextLine = tok->previous &&
171+
tok->previous->previous &&
172+
tok->next &&
173+
!sameline(tok->previous->previous, tok->previous) &&
174+
tok->location.line + 1 == tok->next->location.line &&
175+
tok->location.fileIndex == tok->next->location.fileIndex &&
176+
tok->previous->str() == "{";
177+
169178
// Add the suppressions.
170179
for (Suppressions::Suppression &suppr : inlineSuppressions) {
171180
suppr.fileName = relativeFilename;
172181
suppr.lineNumber = tok->location.line;
182+
suppr.thisAndNextLine = thisAndNextLine;
173183
mSettings.nomsg.addSuppression(suppr);
174184
}
175185
}

lib/suppressions.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -291,8 +291,10 @@ bool Suppressions::Suppression::isSuppressed(const Suppressions::ErrorMessage &e
291291
return false;
292292
if (!fileName.empty() && !matchglob(fileName, errmsg.getFileName()))
293293
return false;
294-
if (lineNumber != NO_LINE && lineNumber != errmsg.lineNumber)
295-
return false;
294+
if (lineNumber != NO_LINE && lineNumber != errmsg.lineNumber) {
295+
if (!thisAndNextLine || lineNumber + 1 != errmsg.lineNumber)
296+
return false;
297+
}
296298
if (!symbolName.empty()) {
297299
for (std::string::size_type pos = 0; pos < errmsg.symbolNames.size();) {
298300
const std::string::size_type pos2 = errmsg.symbolNames.find('\n',pos);

lib/suppressions.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,14 +53,15 @@ class CPPCHECKLIB Suppressions {
5353
Suppression(const Suppression &other) {
5454
*this = other;
5555
}
56-
Suppression(const std::string &id, const std::string &file, int line=NO_LINE) : errorId(id), fileName(file), lineNumber(line), hash(0), matched(false) {}
56+
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) {}
5757

5858
Suppression & operator=(const Suppression &other) {
5959
errorId = other.errorId;
6060
fileName = other.fileName;
6161
lineNumber = other.lineNumber;
6262
symbolName = other.symbolName;
6363
hash = other.hash;
64+
thisAndNextLine = other.thisAndNextLine;
6465
matched = other.matched;
6566
return *this;
6667
}
@@ -76,6 +77,8 @@ class CPPCHECKLIB Suppressions {
7677
return symbolName < other.symbolName;
7778
if (hash != other.hash)
7879
return hash < other.hash;
80+
if (thisAndNextLine != other.thisAndNextLine)
81+
return thisAndNextLine;
7982
return false;
8083
}
8184

@@ -101,6 +104,7 @@ class CPPCHECKLIB Suppressions {
101104
int lineNumber;
102105
std::string symbolName;
103106
std::size_t hash;
107+
bool thisAndNextLine; // Special case for backwards compatibility: { // cppcheck-suppress something
104108
bool matched;
105109

106110
enum { NO_LINE = -1 };

test/cli/test-inline-suppress.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,11 @@ def test_unmatched_suppression_path_with_extra_stuf():
2424
ret, stdout, stderr = cppcheck(['--inline-suppr', '--enable=information', '--error-exitcode=1', './proj-inline-suppress/2.c'])
2525
assert ret == 1
2626
assert 'Unmatched suppression: some_warning_id' in stderr
27+
28+
def test_backwards_compatibility():
29+
ret, stdout, stderr = cppcheck(['--enable=unusedFunction', 'proj-inline-suppress/3.cpp'])
30+
assert len(stderr) > 0
31+
32+
ret, stdout, stderr = cppcheck(['--inline-suppr', '--enable=unusedFunction', 'proj-inline-suppress/3.cpp'])
33+
assert ret == 0
34+
assert stderr == ''

0 commit comments

Comments
 (0)