From b30ed60710849a4a631b0347bd83c9552577dbdc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ludvig=20Gunne=20Lindstr=C3=B6m?= Date: Mon, 17 Nov 2025 13:56:36 +0100 Subject: [PATCH 1/3] fix #14265 --- lib/preprocessor.cpp | 10 +++++----- lib/preprocessor.h | 2 +- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/lib/preprocessor.cpp b/lib/preprocessor.cpp index f326b7efc80..75c20292365 100644 --- a/lib/preprocessor.cpp +++ b/lib/preprocessor.cpp @@ -870,7 +870,7 @@ bool Preprocessor::reportOutput(const simplecpp::OutputList &outputList, bool sh const std::string::size_type pos1 = out.msg.find_first_of("<\""); const std::string::size_type pos2 = out.msg.find_first_of(">\"", pos1 + 1U); if (pos1 < pos2 && pos2 != std::string::npos) - missingInclude(out.location.file(), out.location.line, out.msg.substr(pos1+1, pos2-pos1-1), out.msg[pos1] == '\"' ? UserHeader : SystemHeader); + missingInclude(out.location.file(), out.location.line, out.location.col, out.msg.substr(pos1+1, pos2-pos1-1), out.msg[pos1] == '\"' ? UserHeader : SystemHeader); } break; case simplecpp::Output::INCLUDE_NESTED_TOO_DEEPLY: @@ -910,14 +910,14 @@ void Preprocessor::error(const std::string &filename, unsigned int linenr, const } // Report that include is missing -void Preprocessor::missingInclude(const std::string &filename, unsigned int linenr, const std::string &header, HeaderTypes headerType) +void Preprocessor::missingInclude(const std::string &filename, unsigned int linenr, unsigned int col, const std::string &header, HeaderTypes headerType) { if (!mSettings.checks.isEnabled(Checks::missingInclude)) return; std::list locationList; if (!filename.empty()) { - locationList.emplace_back(filename, linenr, 0); + locationList.emplace_back(filename, linenr, col); } ErrorMessage errmsg(std::move(locationList), mFile0, Severity::information, (headerType==SystemHeader) ? @@ -933,8 +933,8 @@ void Preprocessor::getErrorMessages(ErrorLogger &errorLogger, const Settings &se std::vector files; simplecpp::TokenList tokens(files); Preprocessor preprocessor(tokens, settings, errorLogger, Standards::Language::CPP); - preprocessor.missingInclude("", 1, "", UserHeader); - preprocessor.missingInclude("", 1, "", SystemHeader); + preprocessor.missingInclude("", 1, 2, "", UserHeader); + preprocessor.missingInclude("", 1, 2, "", SystemHeader); preprocessor.error("", 1, "#error message"); // #error .. } diff --git a/lib/preprocessor.h b/lib/preprocessor.h index d8078879ffc..2b5fef5158e 100644 --- a/lib/preprocessor.h +++ b/lib/preprocessor.h @@ -158,7 +158,7 @@ class CPPCHECKLIB WARN_UNUSED Preprocessor { SystemHeader }; - void missingInclude(const std::string &filename, unsigned int linenr, const std::string &header, HeaderTypes headerType); + 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, const std::string &msg); void addRemarkComments(const simplecpp::TokenList &tokens, std::vector &remarkComments) const; From 37264bf1307f72d68819ce537f336f18a224d526 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ludvig=20Gunne=20Lindstr=C3=B6m?= Date: Mon, 17 Nov 2025 14:03:35 +0100 Subject: [PATCH 2/3] update tests --- test/testpreprocessor.cpp | 30 +++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/test/testpreprocessor.cpp b/test/testpreprocessor.cpp index 15001f3e0fc..ea49ddbd7d1 100644 --- a/test/testpreprocessor.cpp +++ b/test/testpreprocessor.cpp @@ -2424,7 +2424,7 @@ class TestPreprocessor : public TestFixture { const char code[] = "#include \"header.h\""; (void)getcodeforcfg(settings, *this, code, "", "test.c"); - ASSERT_EQUALS("test.c:1:0: information: Include file: \"header.h\" not found. [missingInclude]\n", errout_str()); + ASSERT_EQUALS("test.c:1:2: information: Include file: \"header.h\" not found. [missingInclude]\n", errout_str()); } // test for missing local include - no include path given @@ -2440,7 +2440,7 @@ class TestPreprocessor : public TestFixture { const char code[] = "#include \"header.h\""; (void)getcodeforcfg(settings, *this, code, "", "test.c"); - ASSERT_EQUALS("test.c:1:0: information: Include file: \"header.h\" not found. [missingInclude]\n", errout_str()); + ASSERT_EQUALS("test.c:1:2: information: Include file: \"header.h\" not found. [missingInclude]\n", errout_str()); } // test for existing local include - include path provided @@ -2490,7 +2490,7 @@ class TestPreprocessor : public TestFixture { std::string code("#include \"" + header + "\""); (void)getcodeforcfg(settings, *this, code.data(), code.size(), "", "test.c"); - ASSERT_EQUALS("test.c:1:0: information: Include file: \"" + header + "\" not found. [missingInclude]\n", errout_str()); + ASSERT_EQUALS("test.c:1:2: information: Include file: \"" + header + "\" not found. [missingInclude]\n", errout_str()); } // test for missing system include - system includes are not searched for in relative path @@ -2506,7 +2506,7 @@ class TestPreprocessor : public TestFixture { const char code[] = "#include "; (void)getcodeforcfg(settings, *this, code, "", "test.c"); - ASSERT_EQUALS("test.c:1:0: information: Include file: not found. Please note: Cppcheck does not need standard library headers to get proper results. [missingIncludeSystem]\n", errout_str()); + ASSERT_EQUALS("test.c:1:2: information: Include file: not found. Please note: Cppcheck does not need standard library headers to get proper results. [missingIncludeSystem]\n", errout_str()); } // test for missing system include @@ -2520,7 +2520,7 @@ class TestPreprocessor : public TestFixture { const char code[] = "#include "; (void)getcodeforcfg(settings, *this, code, "", "test.c"); - ASSERT_EQUALS("test.c:1:0: information: Include file: not found. Please note: Cppcheck does not need standard library headers to get proper results. [missingIncludeSystem]\n", errout_str()); + ASSERT_EQUALS("test.c:1:2: information: Include file: not found. Please note: Cppcheck does not need standard library headers to get proper results. [missingIncludeSystem]\n", errout_str()); } // test for existing system include in system include path @@ -2570,7 +2570,7 @@ class TestPreprocessor : public TestFixture { std::string code("#include <" + header + ">"); (void)getcodeforcfg(settings, *this, code.data(), code.size(), "", "test.c"); - ASSERT_EQUALS("test.c:1:0: information: Include file: <" + header + "> not found. Please note: Cppcheck does not need standard library headers to get proper results. [missingIncludeSystem]\n", errout_str()); + ASSERT_EQUALS("test.c:1:2: information: Include file: <" + header + "> not found. Please note: Cppcheck does not need standard library headers to get proper results. [missingIncludeSystem]\n", errout_str()); } // test for missing local and system include @@ -2590,9 +2590,9 @@ class TestPreprocessor : public TestFixture { "#include \"header2.h\""; (void)getcodeforcfg(settings, *this, code, "", "test.c"); - ASSERT_EQUALS("test.c:1:0: information: Include file: \"missing.h\" not found. [missingInclude]\n" - "test.c:2:0: information: Include file: not found. Please note: Cppcheck does not need standard library headers to get proper results. [missingIncludeSystem]\n" - "test.c:3:0: information: Include file: not found. Please note: Cppcheck does not need standard library headers to get proper results. [missingIncludeSystem]\n", errout_str()); + ASSERT_EQUALS("test.c:1:2: information: Include file: \"missing.h\" not found. [missingInclude]\n" + "test.c:2:2: information: Include file: not found. Please note: Cppcheck does not need standard library headers to get proper results. [missingIncludeSystem]\n" + "test.c:3:2: information: Include file: not found. Please note: Cppcheck does not need standard library headers to get proper results. [missingIncludeSystem]\n", errout_str()); } void testMissingIncludeCheckConfig() { @@ -2626,12 +2626,12 @@ class TestPreprocessor : public TestFixture { "#include <" + missing4 + ">\n"); (void)getcodeforcfg(settings, *this, code.data(), code.size(), "", "test.c"); - ASSERT_EQUALS("test.c:1:0: information: Include file: \"missing.h\" not found. [missingInclude]\n" - "test.c:2:0: information: Include file: not found. Please note: Cppcheck does not need standard library headers to get proper results. [missingIncludeSystem]\n" - "test.c:3:0: information: Include file: not found. Please note: Cppcheck does not need standard library headers to get proper results. [missingIncludeSystem]\n" - "test.c:6:0: information: Include file: \"header4.h\" not found. [missingInclude]\n" - "test.c:9:0: information: Include file: \"" + missing3 + "\" not found. [missingInclude]\n" - "test.c:11:0: information: Include file: <" + missing4 + "> not found. Please note: Cppcheck does not need standard library headers to get proper results. [missingIncludeSystem]\n", errout_str()); + ASSERT_EQUALS("test.c:1:2: information: Include file: \"missing.h\" not found. [missingInclude]\n" + "test.c:2:2: information: Include file: not found. Please note: Cppcheck does not need standard library headers to get proper results. [missingIncludeSystem]\n" + "test.c:3:2: information: Include file: not found. Please note: Cppcheck does not need standard library headers to get proper results. [missingIncludeSystem]\n" + "test.c:6:2: information: Include file: \"header4.h\" not found. [missingInclude]\n" + "test.c:9:2: information: Include file: \"" + missing3 + "\" not found. [missingInclude]\n" + "test.c:11:2: information: Include file: <" + missing4 + "> not found. Please note: Cppcheck does not need standard library headers to get proper results. [missingIncludeSystem]\n", errout_str()); } void hasInclude() { From d12f7ea7ccbc248e858421a2a20d85ed2334e30d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ludvig=20Gunne=20Lindstr=C3=B6m?= Date: Mon, 17 Nov 2025 14:21:28 +0100 Subject: [PATCH 3/3] update cli tests --- test/cli/helloworld_test.py | 2 +- test/cli/other_test.py | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/test/cli/helloworld_test.py b/test/cli/helloworld_test.py index 3b1e21fe19f..3faa64a8120 100644 --- a/test/cli/helloworld_test.py +++ b/test/cli/helloworld_test.py @@ -357,7 +357,7 @@ def test_missing_include_system(): # #11283 ] _, _, stderr = cppcheck(args, cwd=__script_dir) - assert stderr.replace('\\', '/') == 'helloworld/main.c:1:0: information: Include file: not found. Please note: Cppcheck does not need standard library headers to get proper results. [missingIncludeSystem]\n' + assert stderr.replace('\\', '/') == 'helloworld/main.c:1:2: information: Include file: not found. Please note: Cppcheck does not need standard library headers to get proper results. [missingIncludeSystem]\n' def test_sarif(): diff --git a/test/cli/other_test.py b/test/cli/other_test.py index 0031affe3ce..d071a080548 100644 --- a/test/cli/other_test.py +++ b/test/cli/other_test.py @@ -25,13 +25,13 @@ def test_missing_include(tmpdir): # #11283 test_file = os.path.join(tmpdir, 'test.c') with open(test_file, 'wt') as f: f.write(""" - #include "test.h" - """) +#include "test.h" +""") args = ['--enable=missingInclude', '--template=simple', test_file] _, _, stderr = cppcheck(args) - assert stderr == '{}:2:0: information: Include file: "test.h" not found. [missingInclude]\n'.format(test_file) + assert stderr == '{}:2:2: information: Include file: "test.h" not found. [missingInclude]\n'.format(test_file) def __test_missing_include_check_config(tmpdir, use_j):