Skip to content

Commit 1a88319

Browse files
committed
refs #14280 - report inline suppressions without an error ID
1 parent 2d0326d commit 1a88319

File tree

2 files changed

+46
-4
lines changed

2 files changed

+46
-4
lines changed

lib/preprocessor.cpp

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -88,18 +88,22 @@ static bool parseInlineSuppressionCommentToken(const simplecpp::Token *tok, std:
8888
const std::string::size_type pos1 = comment.find_first_not_of("/* \t");
8989
if (pos1 == std::string::npos)
9090
return false;
91-
if (pos1 + cppchecksuppress.size() >= comment.size())
92-
return false;
9391
if (comment.substr(pos1, cppchecksuppress.size()) != cppchecksuppress)
9492
return false;
93+
if (pos1 + cppchecksuppress.size() >= comment.size()) {
94+
bad.emplace_back(tok->location.file(), tok->location.line, 0, "suppression without error ID");
95+
return false;
96+
}
9597

9698
// check if it has a prefix
9799
const std::string::size_type posEndComment = comment.find_first_of(" [", pos1+cppchecksuppress.size());
98100

99101
// skip spaces after "cppcheck-suppress" and its possible prefix
100102
const std::string::size_type pos2 = comment.find_first_not_of(' ', posEndComment);
101-
if (pos2 == std::string::npos)
103+
if (pos2 == std::string::npos) {
104+
bad.emplace_back(tok->location.file(), tok->location.line, 0, "suppression without error ID");
102105
return false;
106+
}
103107

104108
SuppressionList::Type errorType = SuppressionList::Type::unique;
105109

@@ -142,9 +146,11 @@ static bool parseInlineSuppressionCommentToken(const simplecpp::Token *tok, std:
142146
s.lineNumber = tok->location.line;
143147
}
144148

149+
// TODO: return false?
145150
if (!errmsg.empty())
146151
bad.emplace_back(tok->location.file(), tok->location.line, tok->location.col, std::move(errmsg));
147152

153+
// TODO: report ones without ID - return false?
148154
std::copy_if(suppressions.cbegin(), suppressions.cend(), std::back_inserter(inlineSuppressions), [](const SuppressionList::Suppression& s) {
149155
return !s.errorId.empty();
150156
});
@@ -159,9 +165,12 @@ static bool parseInlineSuppressionCommentToken(const simplecpp::Token *tok, std:
159165
s.type = errorType;
160166
s.lineNumber = tok->location.line;
161167

168+
// TODO: report when no ID - unreachable?
162169
if (!s.errorId.empty())
163170
inlineSuppressions.push_back(std::move(s));
164171

172+
// TODO: unreachable?
173+
// TODO: return false?
165174
if (!errmsg.empty())
166175
bad.emplace_back(tok->location.file(), tok->location.line, tok->location.col, std::move(errmsg));
167176
}

test/testsuppressions.cpp

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -396,7 +396,6 @@ class TestSuppressions : public TestFixture {
396396
}
397397
#endif // HAS_THREADING_MODEL_FORK
398398

399-
// TODO: check all results
400399
void runChecks(unsigned int (TestSuppressions::*check)(const char[], const std::string &)) {
401400
// check to make sure the appropriate errors are present
402401
ASSERT_EQUALS(1, (this->*check)("void f() {\n"
@@ -720,6 +719,38 @@ class TestSuppressions : public TestFixture {
720719
"[test.cpp:6:0]: (error) unknown suppression type 'cppcheck-suppress-end-unknown' [invalidSuppression]\n"
721720
"[test.cpp:4:0]: (error) Suppress Begin: No matching end [invalidSuppression]\n", errout_str());
722721

722+
ASSERT_EQUALS(1, (this->*check)("// cppcheck-suppress-file\n"
723+
"// cppcheck-suppress\n"
724+
"// cppcheck-suppress \n"
725+
"// cppcheck-suppress\t\n"
726+
"// cppcheck-suppress []\n" // TODO
727+
"// cppcheck-suppress-begin\n"
728+
"// cppcheck-suppress-begin id0\n"
729+
"void f() {}\n"
730+
"// cppcheck-suppress-end\n",
731+
""));
732+
ASSERT_EQUALS("[test.cpp:1:0]: (error) suppression without error ID [invalidSuppression]\n"
733+
"[test.cpp:2:0]: (error) suppression without error ID [invalidSuppression]\n"
734+
"[test.cpp:3:0]: (error) suppression without error ID [invalidSuppression]\n"
735+
"[test.cpp:4:0]: (error) suppression without error ID [invalidSuppression]\n"
736+
"[test.cpp:6:0]: (error) suppression without error ID [invalidSuppression]\n"
737+
"[test.cpp:9:0]: (error) suppression without error ID [invalidSuppression]\n"
738+
"[test.cpp:7:0]: (error) Suppress Begin: No matching end [invalidSuppression]\n", errout_str());
739+
740+
ASSERT_EQUALS(1, (this->*check)("// cppcheck-suppress:\n"
741+
"// cppcheck-suppress-unknown\n"
742+
"// cppcheck-suppress-begin-unknown\n"
743+
"// cppcheck-suppress-begin\n"
744+
"void f() {}\n"
745+
"// cppcheck-suppress-end-unknown\n",
746+
""));
747+
// TODO: actually these are all invalid types
748+
ASSERT_EQUALS("[test.cpp:1:0]: (error) suppression without error ID [invalidSuppression]\n"
749+
"[test.cpp:2:0]: (error) suppression without error ID [invalidSuppression]\n"
750+
"[test.cpp:3:0]: (error) suppression without error ID [invalidSuppression]\n"
751+
"[test.cpp:4:0]: (error) suppression without error ID [invalidSuppression]\n"
752+
"[test.cpp:6:0]: (error) suppression without error ID [invalidSuppression]\n", errout_str());
753+
723754
ASSERT_EQUALS(1, (this->*check)("void f() {\n"
724755
" int a;\n"
725756
" // cppcheck-suppress-begin uninitvar\n"
@@ -915,13 +946,15 @@ class TestSuppressions : public TestFixture {
915946
"uninitvar"));
916947
ASSERT_EQUALS("", errout_str());
917948

949+
// TODO: check result
918950
// cppcheck-suppress-macro
919951
(this->*check)("// cppcheck-suppress-macro zerodiv\n"
920952
"#define DIV(A,B) A/B\n"
921953
"a = DIV(10,0);\n",
922954
"");
923955
ASSERT_EQUALS("", errout_str());
924956

957+
// TODO: check result
925958
(this->*check)("// cppcheck-suppress-macro abc\n"
926959
"#define DIV(A,B) A/B\n"
927960
"a = DIV(10,1);\n",

0 commit comments

Comments
 (0)