From 657b7c55d7b058a37a367239b8c35184bf90f3e9 Mon Sep 17 00:00:00 2001 From: firewave Date: Tue, 18 Nov 2025 00:21:27 +0100 Subject: [PATCH 1/2] Preprocessor: small `error()` refactoring --- lib/preprocessor.cpp | 8 +++++++- lib/preprocessor.h | 1 + 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/lib/preprocessor.cpp b/lib/preprocessor.cpp index e4bd51779bf..a2da8380303 100644 --- a/lib/preprocessor.cpp +++ b/lib/preprocessor.cpp @@ -914,6 +914,11 @@ static std::string simplecppErrToId(simplecpp::Output::Type type) } void Preprocessor::error(const std::string &filename, unsigned int linenr, unsigned int col, const std::string &msg, simplecpp::Output::Type type) +{ + error(filename, linenr, col, msg, simplecppErrToId(type)); +} + +void Preprocessor::error(const std::string &filename, unsigned int linenr, unsigned int col, const std::string &msg, const std::string& id) { std::list locationList; if (!filename.empty()) { @@ -927,7 +932,7 @@ void Preprocessor::error(const std::string &filename, unsigned int linenr, unsig mFile0, Severity::error, msg, - simplecppErrToId(type), + id, Certainty::normal)); } @@ -939,6 +944,7 @@ void Preprocessor::missingInclude(const std::string &filename, unsigned int line std::list locationList; if (!filename.empty()) { + // TODO: add relative path handling? locationList.emplace_back(filename, linenr, col); } ErrorMessage errmsg(std::move(locationList), mFile0, Severity::information, diff --git a/lib/preprocessor.h b/lib/preprocessor.h index f5d7497b4ff..cd0f8071d6b 100644 --- a/lib/preprocessor.h +++ b/lib/preprocessor.h @@ -159,6 +159,7 @@ class CPPCHECKLIB WARN_UNUSED Preprocessor { }; void missingInclude(const std::string &filename, unsigned int linenr, unsigned int col, const std::string &header, HeaderTypes headerType); + void error(const std::string &filename, unsigned int linenr, unsigned int col, const std::string &msg, const std::string& id); void addRemarkComments(const simplecpp::TokenList &tokens, std::vector &remarkComments) const; From 32b699feedbaabe06aea51cf6413a500b6e0d218 Mon Sep 17 00:00:00 2001 From: firewave Date: Tue, 18 Nov 2025 00:24:48 +0100 Subject: [PATCH 2/2] added separate (non-critical) error ID for invalid inline suppressions --- lib/preprocessor.cpp | 8 +++++++- lib/preprocessor.h | 1 + test/testsuppressions.cpp | 8 ++++---- 3 files changed, 12 insertions(+), 5 deletions(-) diff --git a/lib/preprocessor.cpp b/lib/preprocessor.cpp index a2da8380303..b6eb5c76cad 100644 --- a/lib/preprocessor.cpp +++ b/lib/preprocessor.cpp @@ -307,7 +307,7 @@ void Preprocessor::inlineSuppressions(SuppressionList &suppressions) ::addInlineSuppressions(filedata->tokens, mSettings, suppressions, err); } for (const BadInlineSuppression &bad : err) { - error(bad.file, bad.line, bad.col, bad.errmsg, simplecpp::Output::ERROR); // TODO: use individual (non-fatal) ID + invalidSuppression(bad.file, bad.line, bad.col, bad.errmsg); // TODO: column is always 0 } } @@ -956,6 +956,11 @@ void Preprocessor::missingInclude(const std::string &filename, unsigned int line mErrorLogger.reportErr(errmsg); } +void Preprocessor::invalidSuppression(const std::string &filename, unsigned int linenr, unsigned int col, const std::string &msg) +{ + error(filename, linenr, col, msg, "invalidSuppression"); +} + void Preprocessor::getErrorMessages(ErrorLogger &errorLogger, const Settings &settings) { std::vector files; @@ -968,6 +973,7 @@ void Preprocessor::getErrorMessages(ErrorLogger &errorLogger, const Settings &se preprocessor.error("", 1, 2, "message", simplecpp::Output::UNHANDLED_CHAR_ERROR); preprocessor.error("", 1, 2, "message", simplecpp::Output::INCLUDE_NESTED_TOO_DEEPLY); preprocessor.error("", 1, 2, "message", simplecpp::Output::FILE_NOT_FOUND); + preprocessor.invalidSuppression("", 1, 2, "message"); } void Preprocessor::dump(std::ostream &out) const diff --git a/lib/preprocessor.h b/lib/preprocessor.h index cd0f8071d6b..282084ad239 100644 --- a/lib/preprocessor.h +++ b/lib/preprocessor.h @@ -159,6 +159,7 @@ class CPPCHECKLIB WARN_UNUSED Preprocessor { }; void missingInclude(const std::string &filename, unsigned int linenr, unsigned int col, const std::string &header, HeaderTypes headerType); + void invalidSuppression(const std::string &filename, unsigned int linenr, unsigned int col, const std::string &msg); void error(const std::string &filename, unsigned int linenr, unsigned int col, const std::string &msg, const std::string& id); void addRemarkComments(const simplecpp::TokenList &tokens, std::vector &remarkComments) const; diff --git a/test/testsuppressions.cpp b/test/testsuppressions.cpp index 90bd5bac31f..6c6b06be8e7 100644 --- a/test/testsuppressions.cpp +++ b/test/testsuppressions.cpp @@ -426,7 +426,7 @@ class TestSuppressions : public TestFixture { " a++;\n" "}\n", "")); - ASSERT_EQUALS("[test.cpp:2:0]: (error) File suppression should be at the top of the file [preprocessorErrorDirective]\n" + ASSERT_EQUALS("[test.cpp:2:0]: (error) File suppression should be at the top of the file [invalidSuppression]\n" "[test.cpp:4:5]: (error) Uninitialized variable: a [uninitvar]\n", errout_str()); ASSERT_EQUALS(1, (this->*check)("void f() {\n" @@ -435,7 +435,7 @@ class TestSuppressions : public TestFixture { "}\n" "// cppcheck-suppress-file uninitvar\n", "")); - ASSERT_EQUALS("[test.cpp:5:0]: (error) File suppression should be at the top of the file [preprocessorErrorDirective]\n" + ASSERT_EQUALS("[test.cpp:5:0]: (error) File suppression should be at the top of the file [invalidSuppression]\n" "[test.cpp:3:5]: (error) Uninitialized variable: a [uninitvar]\n", errout_str()); ASSERT_EQUALS(0, (this->*check)("// cppcheck-suppress-file uninitvar\n" @@ -687,7 +687,7 @@ class TestSuppressions : public TestFixture { " b++;\n" "}\n", "")); - ASSERT_EQUALS("[test.cpp:2:0]: (error) Suppress Begin: No matching end [preprocessorErrorDirective]\n" + ASSERT_EQUALS("[test.cpp:2:0]: (error) Suppress Begin: No matching end [invalidSuppression]\n" "[test.cpp:4:5]: (error) Uninitialized variable: a [uninitvar]\n" "[test.cpp:6:5]: (error) Uninitialized variable: b [uninitvar]\n", errout_str()); @@ -699,7 +699,7 @@ class TestSuppressions : public TestFixture { " // cppcheck-suppress-end uninitvar\n" "}\n", "")); - ASSERT_EQUALS("[test.cpp:6:0]: (error) Suppress End: No matching begin [preprocessorErrorDirective]\n" + ASSERT_EQUALS("[test.cpp:6:0]: (error) Suppress End: No matching begin [invalidSuppression]\n" "[test.cpp:3:5]: (error) Uninitialized variable: a [uninitvar]\n" "[test.cpp:5:5]: (error) Uninitialized variable: b [uninitvar]\n", errout_str());