Skip to content

Commit fac75a1

Browse files
authored
Fix #14461: Manual: comment directly after inline suppression (danmar#8181)
1 parent d766098 commit fac75a1

File tree

2 files changed

+48
-1
lines changed

2 files changed

+48
-1
lines changed

man/manual.md

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -654,7 +654,21 @@ Or at the same line as the code:
654654
arr[10] = 0; // cppcheck-suppress arrayIndexOutOfBounds
655655
}
656656

657-
In this example there are 2 lines with code and 1 suppression comment. The suppression comment only applies to 1 line: `a = b + c;`.
657+
The suppression comment and the line of code may be separated by additional comments or empty lines:
658+
659+
void f() {
660+
char arr[5];
661+
662+
// cppcheck-suppress arrayIndexOutOfBounds
663+
664+
arr[10] = 0;
665+
666+
// cppcheck-suppress arrayIndexOutOfBounds
667+
// Set the tenth element of arr to zero
668+
arr[10] = 0;
669+
}
670+
671+
In the example below there are 2 lines with code and 1 suppression comment. The suppression comment only applies to 1 line: `a = b + c;`.
658672

659673
void f() {
660674
a = b + c; // cppcheck-suppress abc

test/testpreprocessor.cpp

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,7 @@ class TestPreprocessor : public TestFixture {
281281

282282
// inline suppression, missingInclude/missingIncludeSystem
283283
TEST_CASE(inline_suppressions);
284+
TEST_CASE(inline_suppressions_not_next_line);
284285

285286
// remark comment
286287
TEST_CASE(remarkComment1);
@@ -2030,6 +2031,38 @@ class TestPreprocessor : public TestFixture {
20302031
ignore_errout(); // we are not interested in the output
20312032
}
20322033

2034+
void inline_suppressions_not_next_line() {
2035+
const auto settings = dinit(Settings,
2036+
$.inlineSuppressions = true,
2037+
$.checks.enable (Checks::missingInclude));
2038+
2039+
const char code[] = "// cppcheck-suppress missingInclude\n"
2040+
"// some other comment\n"
2041+
"#include \"missing.h\"\n"
2042+
"// cppcheck-suppress missingIncludeSystem\n"
2043+
"\n" // Empty line
2044+
"#include <missing2.h>\n";
2045+
SuppressionList inlineSuppr;
2046+
(void)getcodeforcfg(settings, *this, code, "", "test.c", &inlineSuppr);
2047+
2048+
auto suppressions = inlineSuppr.getSuppressions();
2049+
ASSERT_EQUALS(2, suppressions.size());
2050+
2051+
auto suppr = suppressions.front();
2052+
suppressions.pop_front();
2053+
ASSERT_EQUALS("missingInclude", suppr.errorId);
2054+
ASSERT_EQUALS("test.c", suppr.fileName);
2055+
ASSERT_EQUALS(3, suppr.lineNumber);
2056+
2057+
suppr = suppressions.front();
2058+
suppressions.pop_front();
2059+
ASSERT_EQUALS("missingIncludeSystem", suppr.errorId);
2060+
ASSERT_EQUALS("test.c", suppr.fileName);
2061+
ASSERT_EQUALS(6, suppr.lineNumber);
2062+
2063+
ignore_errout();
2064+
}
2065+
20332066
void remarkComment1() {
20342067
const char code[] = "// REMARK: assignment with 1\n"
20352068
"x=1;\n";

0 commit comments

Comments
 (0)