Skip to content

Commit 8da9e50

Browse files
authored
CppCheck: no need to write locations in unused preprocessor output / PreprocessorHelper: preserve locations for includes in getcode() (danmar#7846)
1 parent 9433184 commit 8da9e50

File tree

4 files changed

+25
-19
lines changed

4 files changed

+25
-19
lines changed

lib/cppcheck.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1055,7 +1055,7 @@ unsigned int CppCheck::checkInternal(const FileWithDetails& file, const std::str
10551055

10561056
if (mSettings.checkConfiguration) {
10571057
for (const std::string &config : configurations)
1058-
(void)preprocessor.getcode(tokens1, config, files, true);
1058+
(void)preprocessor.getcode(tokens1, config, files, false);
10591059

10601060
if (analyzerInformation)
10611061
mLogger->setAnalyzerInfo(nullptr);

test/helpers.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727

2828
#include <cerrno>
2929
#include <cstdio>
30+
#include <cstring>
3031
#include <iostream>
3132
#include <fstream>
3233
#include <list>
@@ -151,8 +152,8 @@ std::map<std::string, std::string> PreprocessorHelper::getcode(const Settings& s
151152
cfgs = preprocessor.getConfigs(tokens);
152153
for (const std::string & config : cfgs) {
153154
try {
154-
// TODO: also preserve location information when #include exists - enabling that will fail since #line is treated like a regular token
155-
cfgcode[config] = preprocessor.getcode(tokens, config, files, std::string(code).find("#file") != std::string::npos);
155+
const bool writeLocations = (strstr(code, "#file") != nullptr) || (strstr(code, "#include") != nullptr);
156+
cfgcode[config] = preprocessor.getcode(tokens, config, files, writeLocations);
156157
} catch (const simplecpp::Output &) {
157158
cfgcode[config] = "";
158159
}

test/testpreprocessor.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,8 @@ class TestPreprocessor : public TestFixture {
298298
TEST_CASE(hashCalculation);
299299

300300
TEST_CASE(standard);
301+
302+
TEST_CASE(writeLocations);
301303
}
302304

303305
template<size_t size>
@@ -2655,6 +2657,25 @@ class TestPreprocessor : public TestFixture {
26552657
ASSERT(!tokenlist.front()); // nothing is tokenized when an unknown standard is provided
26562658
}
26572659
}
2660+
2661+
void writeLocations()
2662+
{
2663+
const char inc[] = "class A {\n"
2664+
"public:\n"
2665+
" void f() {}\n"
2666+
"};";
2667+
const char code[] = R"(#include "test.h")";
2668+
ScopedFile header("test.h", inc);
2669+
const std::string processed = PreprocessorHelper::getcodeforcfg(settingsDefault, *this, code, "", "test.cpp");
2670+
ASSERT_EQUALS(
2671+
"\n"
2672+
"#line 1 \"test.h\"\n"
2673+
"class A {\n"
2674+
"public :\n"
2675+
"void f ( ) { }\n"
2676+
"} ;",
2677+
processed);
2678+
}
26582679
};
26592680

26602681
REGISTER_TEST(TestPreprocessor)

test/testunusedfunctions.cpp

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,6 @@ class TestUnusedFunctions : public TestFixture {
7979
TEST_CASE(entrypointsWinU);
8080
TEST_CASE(entrypointsUnix);
8181

82-
TEST_CASE(includes);
8382
TEST_CASE(virtualFunc);
8483
TEST_CASE(parensInit);
8584
TEST_CASE(typeInCast);
@@ -732,21 +731,6 @@ class TestUnusedFunctions : public TestFixture {
732731
ASSERT_EQUALS("", errout_str());
733732
}
734733

735-
// TODO: fails because the location information is not be preserved by PreprocessorHelper::getcode()
736-
void includes()
737-
{
738-
// #11483
739-
const char inc[] = "class A {\n"
740-
"public:\n"
741-
" void f() {}\n"
742-
"};";
743-
const char code[] = R"(#include "test.h")";
744-
ScopedFile header("test.h", inc);
745-
const std::string processed = PreprocessorHelper::getcodeforcfg(settings, *this, code, "", "test.cpp");
746-
check(processed);
747-
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());
748-
}
749-
750734
void virtualFunc()
751735
{
752736
check("struct D : public B {\n" // #10660

0 commit comments

Comments
 (0)