diff --git a/lib/suppressions.cpp b/lib/suppressions.cpp index 5ee25511d..1d94155b6 100644 --- a/lib/suppressions.cpp +++ b/lib/suppressions.cpp @@ -169,8 +169,20 @@ std::vector Suppressions::parseMultiSuppressComment(c std::string Suppressions::addSuppressionLine(const std::string &line) { - std::istringstream lineStream(line); + std::istringstream lineStream; Suppressions::Suppression suppression; + + // Strip any end of line comments + std::string::size_type endpos = std::min(line.find("#"), line.find("//")); + if (endpos != std::string::npos) { + while (endpos > 0 && std::isspace(line[endpos-1])) { + endpos--; + } + lineStream.str(line.substr(0, endpos)); + } else { + lineStream.str(line); + } + if (std::getline(lineStream, suppression.errorId, ':')) { if (std::getline(lineStream, suppression.fileName)) { // If there is not a dot after the last colon in "file" then diff --git a/man/manual.md b/man/manual.md index 08ac44b26..c1b0793cd 100644 --- a/man/manual.md +++ b/man/manual.md @@ -391,10 +391,10 @@ You can create a suppressions file. Example: memleak:src/file1.cpp exceptNew:src/file1.cpp - // suppress all uninitvar errors in all files - uninitvar + uninitvar // suppress all uninitvar errors in all files Note that you may add empty lines and comments in the suppressions file. +Comments must start with `#` or `//` and be at the start of the line, or after the suppression line. You can use the suppressions file like this: diff --git a/test/testsuppressions.cpp b/test/testsuppressions.cpp index 18b46933a..6842d2be4 100644 --- a/test/testsuppressions.cpp +++ b/test/testsuppressions.cpp @@ -493,6 +493,26 @@ private: Suppressions suppressions2; suppressions2.parseFile(file2); ASSERT_EQUALS(true, suppressions2.isSuppressed(errorMessage("abc", "test.cpp", 123))); + + std::istringstream file3("abc // comment"); + Suppressions suppressions3; + suppressions3.parseFile(file3); + ASSERT_EQUALS(true, suppressions3.isSuppressed(errorMessage("abc", "test.cpp", 123))); + + std::istringstream file4("abc\t\t # comment"); + Suppressions suppressions4; + suppressions4.parseFile(file4); + ASSERT_EQUALS(true, suppressions4.isSuppressed(errorMessage("abc", "test.cpp", 123))); + + std::istringstream file5("abc:test.cpp\t\t # comment"); + Suppressions suppressions5; + suppressions5.parseFile(file5); + ASSERT_EQUALS(true, suppressions5.isSuppressed(errorMessage("abc", "test.cpp", 123))); + + std::istringstream file6("abc:test.cpp:123\t\t # comment with . inside"); + Suppressions suppressions6; + suppressions6.parseFile(file6); + ASSERT_EQUALS(true, suppressions6.isSuppressed(errorMessage("abc", "test.cpp", 123))); } void inlinesuppress() {