Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/cppcheck.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
5 changes: 3 additions & 2 deletions test/helpers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@

#include <cerrno>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <fstream>
#include <list>
Expand Down Expand Up @@ -151,8 +152,8 @@ std::map<std::string, std::string> 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] = "";
}
Expand Down
21 changes: 21 additions & 0 deletions test/testpreprocessor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,8 @@ class TestPreprocessor : public TestFixture {
TEST_CASE(hashCalculation);

TEST_CASE(standard);

TEST_CASE(writeLocations);
}

template<size_t size>
Expand Down Expand Up @@ -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)
16 changes: 0 additions & 16 deletions test/testunusedfunctions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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
Expand Down