From 22c8e4d7ac2c1ea5605917e591030a22ff82736c Mon Sep 17 00:00:00 2001 From: firewave Date: Fri, 5 Sep 2025 09:09:33 +0200 Subject: [PATCH 1/3] moved a test from `TestUnusedFunctions` to `TestPreprocessor` --- test/testpreprocessor.cpp | 26 ++++++++++++++++++++++++++ test/testunusedfunctions.cpp | 16 ---------------- 2 files changed, 26 insertions(+), 16 deletions(-) diff --git a/test/testpreprocessor.cpp b/test/testpreprocessor.cpp index 171f9aa7105..bfb2911b2cd 100644 --- a/test/testpreprocessor.cpp +++ b/test/testpreprocessor.cpp @@ -298,6 +298,8 @@ class TestPreprocessor : public TestFixture { TEST_CASE(hashCalculation); TEST_CASE(standard); + + TEST_CASE(writeLocations); } template @@ -2655,6 +2657,30 @@ class TestPreprocessor : public TestFixture { ASSERT(!tokenlist.front()); // nothing is tokenized when an unknown standard is provided } } + + // TODO: fails because the location information is not be preserved by PreprocessorHelper::getcode() + void writeLocations() + { + const char inc[] = "class A {\n" + "public:\n" + " void f() {}\n" + "};"; + const char code[] = R"(#include "test.h")"; + ScopedFile header("test.h", inc); + const std::string processed = PreprocessorHelper::getcodeforcfg(settingsDefault, *this, code, "", "test.cpp"); + TODO_ASSERT_EQUALS( + "\n" + "#line 1 \"test.h\"\n" + "class A {\n" + "public :\n" + "void f ( ) { }\n" + "} ;", + "class A {\n" + "public :\n" + "void f ( ) { }\n" + "} ;", + processed); + } }; REGISTER_TEST(TestPreprocessor) diff --git a/test/testunusedfunctions.cpp b/test/testunusedfunctions.cpp index 34e91c1d977..8db3015f9cc 100644 --- a/test/testunusedfunctions.cpp +++ b/test/testunusedfunctions.cpp @@ -79,7 +79,6 @@ class TestUnusedFunctions : public TestFixture { TEST_CASE(entrypointsWinU); TEST_CASE(entrypointsUnix); - TEST_CASE(includes); TEST_CASE(virtualFunc); TEST_CASE(parensInit); TEST_CASE(typeInCast); @@ -732,21 +731,6 @@ class TestUnusedFunctions : public TestFixture { ASSERT_EQUALS("", errout_str()); } - // TODO: fails because the location information is not be preserved by PreprocessorHelper::getcode() - void includes() - { - // #11483 - const char inc[] = "class A {\n" - "public:\n" - " void f() {}\n" - "};"; - const char code[] = R"(#include "test.h")"; - ScopedFile header("test.h", inc); - const std::string processed = PreprocessorHelper::getcodeforcfg(settings, *this, code, "", "test.cpp"); - check(processed); - TODO_ASSERT_EQUALS("[test.h:3:6]: (style) The function 'f' is never used. [unusedFunction]\n", "[test.cpp:3:6]: (style) The function 'f' is never used. [unusedFunction]\n", errout_str()); - } - void virtualFunc() { check("struct D : public B {\n" // #10660 From 98d5d6cc85e06eec166d1250ba10791c64ad36d6 Mon Sep 17 00:00:00 2001 From: firewave Date: Fri, 5 Sep 2025 09:15:12 +0200 Subject: [PATCH 2/3] PreprocessorHelper: write locations in `getcode()` when `#include` is encountered --- test/helpers.cpp | 5 +++-- test/testpreprocessor.cpp | 7 +------ 2 files changed, 4 insertions(+), 8 deletions(-) diff --git a/test/helpers.cpp b/test/helpers.cpp index 2d6a60f9f45..c9a463e76f4 100644 --- a/test/helpers.cpp +++ b/test/helpers.cpp @@ -27,6 +27,7 @@ #include #include +#include #include #include #include @@ -151,8 +152,8 @@ std::map PreprocessorHelper::getcode(const Settings& s cfgs = preprocessor.getConfigs(tokens); for (const std::string & config : cfgs) { try { - // TODO: also preserve location information when #include exists - enabling that will fail since #line is treated like a regular token - cfgcode[config] = preprocessor.getcode(tokens, config, files, std::string(code).find("#file") != std::string::npos); + const bool writeLocations = (strstr(code, "#file") != nullptr) || (strstr(code, "#include") != nullptr); + cfgcode[config] = preprocessor.getcode(tokens, config, files, writeLocations); } catch (const simplecpp::Output &) { cfgcode[config] = ""; } diff --git a/test/testpreprocessor.cpp b/test/testpreprocessor.cpp index bfb2911b2cd..64b41c6891e 100644 --- a/test/testpreprocessor.cpp +++ b/test/testpreprocessor.cpp @@ -2658,7 +2658,6 @@ class TestPreprocessor : public TestFixture { } } - // TODO: fails because the location information is not be preserved by PreprocessorHelper::getcode() void writeLocations() { const char inc[] = "class A {\n" @@ -2668,17 +2667,13 @@ class TestPreprocessor : public TestFixture { const char code[] = R"(#include "test.h")"; ScopedFile header("test.h", inc); const std::string processed = PreprocessorHelper::getcodeforcfg(settingsDefault, *this, code, "", "test.cpp"); - TODO_ASSERT_EQUALS( + ASSERT_EQUALS( "\n" "#line 1 \"test.h\"\n" "class A {\n" "public :\n" "void f ( ) { }\n" "} ;", - "class A {\n" - "public :\n" - "void f ( ) { }\n" - "} ;", processed); } }; From 9167d914bac3325ec2db6038ebf73d53074b2973 Mon Sep 17 00:00:00 2001 From: firewave Date: Fri, 5 Sep 2025 09:16:40 +0200 Subject: [PATCH 3/3] CppCheck: no need to write locations in unused preprocessor output --- lib/cppcheck.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/cppcheck.cpp b/lib/cppcheck.cpp index 008bbe0eee9..2ff826d0436 100644 --- a/lib/cppcheck.cpp +++ b/lib/cppcheck.cpp @@ -1055,7 +1055,7 @@ unsigned int CppCheck::checkInternal(const FileWithDetails& file, const std::str if (mSettings.checkConfiguration) { for (const std::string &config : configurations) - (void)preprocessor.getcode(tokens1, config, files, true); + (void)preprocessor.getcode(tokens1, config, files, false); if (analyzerInformation) mLogger->setAnalyzerInfo(nullptr);