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); 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 171f9aa7105..64b41c6891e 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,25 @@ class TestPreprocessor : public TestFixture { ASSERT(!tokenlist.front()); // nothing is tokenized when an unknown standard is provided } } + + 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"); + ASSERT_EQUALS( + "\n" + "#line 1 \"test.h\"\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