Skip to content

Commit

Permalink
Force file suppression to happen at the top of files
Browse files Browse the repository at this point in the history
  • Loading branch information
JohanBertrand committed Sep 4, 2023
1 parent 1ed3d98 commit 27cb87b
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 7 deletions.
15 changes: 11 additions & 4 deletions lib/preprocessor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ Preprocessor::~Preprocessor()

namespace {
struct BadInlineSuppression {
BadInlineSuppression(const std::string &file, const int line, std::string msg) : file(file), line(line), errmsg(std::move(msg)) {}
BadInlineSuppression(std::string file, const int line, std::string msg) : file(std::move(file)), line(line), errmsg(std::move(msg)) {}
std::string file;
int line;
std::string errmsg;
Expand Down Expand Up @@ -169,9 +169,13 @@ static void addInlineSuppressions(const simplecpp::TokenList &tokens, const Sett
{
std::list<Suppressions::Suppression> inlineSuppressionsBlockBegin;

bool onlyComments = true;

for (const simplecpp::Token *tok = tokens.cfront(); tok; tok = tok->next) {
if (!tok->comment)
if (!tok->comment) {
onlyComments = false;
continue;
}

std::list<Suppressions::Suppression> inlineSuppressions;
if (!parseInlineSuppressionCommentToken(tok, inlineSuppressions, bad))
Expand Down Expand Up @@ -260,8 +264,11 @@ static void addInlineSuppressions(const simplecpp::TokenList &tokens, const Sett
suppr.thisAndNextLine = thisAndNextLine;
suppr.lineNumber = tok->location.line;
suppressions.addSuppression(std::move(suppr));
} else {
suppressions.addSuppression(std::move(suppr));
} else if (Suppressions::Type::file == suppr.type) {
if (onlyComments)
suppressions.addSuppression(std::move(suppr));
else
bad.emplace_back(suppr.fileName, suppr.lineNumber, "File suppression should be at the top of the file");
}
}
}
Expand Down
31 changes: 28 additions & 3 deletions test/testsuppressions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -304,15 +304,40 @@ class TestSuppressions : public TestFixture {
"uninitvar"));
ASSERT_EQUALS("", errout.str());

ASSERT_EQUALS(0, (this->*check)("void f() {\n"
" // cppcheck-suppress-file uninitvar\n"
(this->*check)("void f() {\n"
" // cppcheck-suppress-file uninitvar\n"
" int a;\n"
" a++;\n"
"}\n",
"");
ASSERT_EQUALS("[test.cpp:2]: (error) File suppression should be at the top of the file\n"
"[test.cpp:4]: (error) Uninitialized variable: a\n", errout.str());

(this->*check)("void f() {\n"
" int a;\n"
" a++;\n"
"}\n"
"// cppcheck-suppress-file uninitvar\n",
"");
ASSERT_EQUALS("[test.cpp:5]: (error) File suppression should be at the top of the file\n"
"[test.cpp:3]: (error) Uninitialized variable: a\n", errout.str());

ASSERT_EQUALS(0, (this->*check)("// cppcheck-suppress-file uninitvar\n"
"void f() {\n"
" int a;\n"
" a++;\n"
" int b;\n"
" b++;\n"
"}\n",
""));
ASSERT_EQUALS("", errout.str());

ASSERT_EQUALS(0, (this->*check)("// cppcheck-suppress-file uninitvar\n"
ASSERT_EQUALS(0, (this->*check)("/* Fake file description\n"
" * End\n"
" */\n"
"\n"
"// cppcheck-suppress-file uninitvar\n"
"\n"
"void f() {\n"
" int a;\n"
" a++;\n"
Expand Down

0 comments on commit 27cb87b

Please sign in to comment.