Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 12 additions & 3 deletions lib/preprocessor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,15 @@ static std::string getRelativeFilename(const simplecpp::Token* tok, const Settin
return Path::simplifyPath(std::move(relativeFilename));
}

static void addInlineSuppression(SuppressionList::Suppression suppr, SuppressionList &suppressions, std::list<BadInlineSuppression> &bad)
{
const std::string file = suppr.fileName;
const int line = suppr.lineNumber;
const std::string errmsg = suppressions.addSuppression(std::move(suppr));
if (!errmsg.empty())
bad.emplace_back(file, line, errmsg);
}

static void addInlineSuppressions(const simplecpp::TokenList &tokens, const Settings &settings, SuppressionList &suppressions, std::list<BadInlineSuppression> &bad)
{
std::list<SuppressionList::Suppression> inlineSuppressionsBlockBegin;
Expand Down Expand Up @@ -262,7 +271,7 @@ static void addInlineSuppressions(const simplecpp::TokenList &tokens, const Sett
suppr.lineNumber = supprBegin->lineNumber;
suppr.type = SuppressionList::Type::block;
inlineSuppressionsBlockBegin.erase(supprBegin);
suppressions.addSuppression(std::move(suppr)); // TODO: check result
addInlineSuppression(std::move(suppr), suppressions, bad);
throwError = false;
break;
}
Expand All @@ -287,10 +296,10 @@ static void addInlineSuppressions(const simplecpp::TokenList &tokens, const Sett
suppr.thisAndNextLine = thisAndNextLine;
suppr.lineNumber = tok->location.line;
suppr.macroName = macroName;
suppressions.addSuppression(std::move(suppr)); // TODO: check result
addInlineSuppression(std::move(suppr), suppressions, bad);
} else if (SuppressionList::Type::file == suppr.type) {
if (onlyComments)
suppressions.addSuppression(std::move(suppr)); // TODO: check result
addInlineSuppression(std::move(suppr), suppressions, bad);
else
bad.emplace_back(suppr.fileName, suppr.lineNumber, "File suppression should be at the top of the file");
}
Expand Down
13 changes: 13 additions & 0 deletions test/testsuppressions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -703,6 +703,19 @@ class TestSuppressions : public TestFixture {
"[test.cpp:3:5]: (error) Uninitialized variable: a [uninitvar]\n"
"[test.cpp:5:5]: (error) Uninitialized variable: b [uninitvar]\n", errout_str());

ASSERT_EQUALS(1, (this->*check)("// cppcheck-suppress-file :id0\n"
"// cppcheck-suppress :id\n"
"// cppcheck-suppress [:id1,id2]\n"
"// cppcheck-suppress-begin :id3\n"
"void f() {}\n"
"// cppcheck-suppress-end :id3\n",
""));
ASSERT_EQUALS("[test.cpp:1:0]: (error) Failed to add suppression. Invalid id \":id0\" [preprocessorErrorDirective]\n"
"[test.cpp:5:0]: (error) Failed to add suppression. Invalid id \":id\" [preprocessorErrorDirective]\n" // TODO: should we report the location of the suppression instead?
"[test.cpp:5:0]: (error) Failed to add suppression. Invalid id \":id1\" [preprocessorErrorDirective]\n" // TODO: should we report the location of the suppression instead?
"[test.cpp:4:0]: (error) Failed to add suppression. Invalid id \":id3\" [preprocessorErrorDirective]\n"
"[test.cpp:5:0]: (information) Unmatched suppression: id2 [unmatchedSuppression]\n", errout_str()); // TODO: should we report the location of the suppression instead?

ASSERT_EQUALS(1, (this->*check)("void f() {\n"
" int a;\n"
" // cppcheck-suppress-begin uninitvar\n"
Expand Down