diff --git a/man/manual.md b/man/manual.md index d8da119f388..1a4e4002f10 100644 --- a/man/manual.md +++ b/man/manual.md @@ -654,7 +654,21 @@ Or at the same line as the code: arr[10] = 0; // cppcheck-suppress arrayIndexOutOfBounds } -In this example there are 2 lines with code and 1 suppression comment. The suppression comment only applies to 1 line: `a = b + c;`. +The suppression comment and the line of code may be separated by additional comments or empty lines: + + void f() { + char arr[5]; + + // cppcheck-suppress arrayIndexOutOfBounds + + arr[10] = 0; + + // cppcheck-suppress arrayIndexOutOfBounds + // Set the tenth element of arr to zero + arr[10] = 0; + } + +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;`. void f() { a = b + c; // cppcheck-suppress abc diff --git a/test/testpreprocessor.cpp b/test/testpreprocessor.cpp index f0885308c73..f38b61b8a66 100644 --- a/test/testpreprocessor.cpp +++ b/test/testpreprocessor.cpp @@ -281,6 +281,7 @@ class TestPreprocessor : public TestFixture { // inline suppression, missingInclude/missingIncludeSystem TEST_CASE(inline_suppressions); + TEST_CASE(inline_suppressions_not_next_line); // remark comment TEST_CASE(remarkComment1); @@ -2030,6 +2031,38 @@ class TestPreprocessor : public TestFixture { ignore_errout(); // we are not interested in the output } + void inline_suppressions_not_next_line() { + const auto settings = dinit(Settings, + $.inlineSuppressions = true, + $.checks.enable (Checks::missingInclude)); + + const char code[] = "// cppcheck-suppress missingInclude\n" + "// some other comment\n" + "#include \"missing.h\"\n" + "// cppcheck-suppress missingIncludeSystem\n" + "\n" // Empty line + "#include \n"; + SuppressionList inlineSuppr; + (void)getcodeforcfg(settings, *this, code, "", "test.c", &inlineSuppr); + + auto suppressions = inlineSuppr.getSuppressions(); + ASSERT_EQUALS(2, suppressions.size()); + + auto suppr = suppressions.front(); + suppressions.pop_front(); + ASSERT_EQUALS("missingInclude", suppr.errorId); + ASSERT_EQUALS("test.c", suppr.fileName); + ASSERT_EQUALS(3, suppr.lineNumber); + + suppr = suppressions.front(); + suppressions.pop_front(); + ASSERT_EQUALS("missingIncludeSystem", suppr.errorId); + ASSERT_EQUALS("test.c", suppr.fileName); + ASSERT_EQUALS(6, suppr.lineNumber); + + ignore_errout(); + } + void remarkComment1() { const char code[] = "// REMARK: assignment with 1\n" "x=1;\n";