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
43 changes: 0 additions & 43 deletions lib/preprocessor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -725,10 +725,6 @@ simplecpp::TokenList Preprocessor::preprocess(const simplecpp::TokenList &tokens

tokens2.removeComments();

// ensure that guessed define macros without value are not used in the code
if (!validateCfg(cfg, macroUsage))
return simplecpp::TokenList(files);

return tokens2;
}

Expand Down Expand Up @@ -878,52 +874,13 @@ void Preprocessor::missingInclude(const std::string &filename, unsigned int line
}
}

bool Preprocessor::validateCfg(const std::string &cfg, const std::list<simplecpp::MacroUsage> &macroUsageList)
{
bool ret = true;
std::list<std::string> defines;
splitcfg(cfg, defines, emptyString);
for (const std::string &define : defines) {
if (define.find('=') != std::string::npos)
continue;
const std::string macroName(define.substr(0, define.find('(')));
for (const simplecpp::MacroUsage &mu : macroUsageList) {
if (mu.macroValueKnown)
continue;
if (mu.macroName != macroName)
continue;
const bool directiveLocation = std::any_of(mDirectives.cbegin(), mDirectives.cend(),
[=](const Directive &dir) {
return mu.useLocation.file() == dir.file && mu.useLocation.line == dir.linenr;
});

if (!directiveLocation) {
if (mSettings.severity.isEnabled(Severity::information))
validateCfgError(mu.useLocation.file(), mu.useLocation.line, cfg, macroName);
ret = false;
}
}
}

return ret;
}

void Preprocessor::validateCfgError(const std::string &file, const unsigned int line, const std::string &cfg, const std::string &macro)
{
const std::string id = "ConfigurationNotChecked";
ErrorMessage::FileLocation loc(file, line, 0);
const ErrorMessage errmsg({std::move(loc)}, mFile0, Severity::information, "Skipping configuration '" + cfg + "' since the value of '" + macro + "' is unknown. Use -D if you want to check it. You can use -U to skip it explicitly.", id, Certainty::normal);
mErrorLogger->reportInfo(errmsg);
}

void Preprocessor::getErrorMessages(ErrorLogger *errorLogger, const Settings *settings)
{
Settings settings2(*settings);
Preprocessor preprocessor(settings2, errorLogger);
settings2.checkConfiguration = true;
preprocessor.missingInclude(emptyString, 1, emptyString, UserHeader);
preprocessor.missingInclude(emptyString, 1, emptyString, SystemHeader);
preprocessor.validateCfgError(emptyString, 1, "X", "X");
preprocessor.error(emptyString, 1, "#error message"); // #error ..
}

Expand Down
9 changes: 0 additions & 9 deletions lib/preprocessor.h
Original file line number Diff line number Diff line change
Expand Up @@ -156,15 +156,6 @@ class CPPCHECKLIB Preprocessor {
*/
std::string getcode(const std::string &filedata, const std::string &cfg, const std::string &filename);

/**
* make sure empty configuration macros are not used in code. the given code must be a single configuration
* @param cfg configuration
* @param macroUsageList macro usage list
* @return true => configuration is valid
*/
bool validateCfg(const std::string &cfg, const std::list<simplecpp::MacroUsage> &macroUsageList);
void validateCfgError(const std::string &file, const unsigned int line, const std::string &cfg, const std::string &macro);

/**
* Calculate HASH. Using toolinfo, tokens1, filedata.
*
Expand Down
35 changes: 1 addition & 34 deletions test/testpreprocessor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -244,9 +244,6 @@ class TestPreprocessor : public TestFixture {
TEST_CASE(getConfigsU6);
TEST_CASE(getConfigsU7);

TEST_CASE(validateCfg1);
TEST_CASE(validateCfg2);

TEST_CASE(if_sizeof);

TEST_CASE(invalid_ifs); // #5909
Expand Down Expand Up @@ -274,6 +271,7 @@ class TestPreprocessor : public TestFixture {
TEST_CASE(testMissingIncludeCheckConfig);
}

// TODO: we should be calling the actual Preprocessor::preprocess() implementation
void preprocess(const char* code, std::map<std::string, std::string>& actual, const char filename[] = "file.c") {
errout.str("");
std::istringstream istr(code);
Expand Down Expand Up @@ -2281,37 +2279,6 @@ class TestPreprocessor : public TestFixture {
ASSERT_EQUALS("\nY\n", getConfigsStr(code, "-DX"));
}


void validateCfg1() {
Preprocessor preprocessor(settings0, this);

std::vector<std::string> files(1, "test.c");
simplecpp::MacroUsage macroUsage(files, false);
macroUsage.useLocation.fileIndex = 0;
macroUsage.useLocation.line = 1;
macroUsage.macroName = "X";
std::list<simplecpp::MacroUsage> macroUsageList(1, macroUsage);

ASSERT_EQUALS(true, preprocessor.validateCfg("", macroUsageList));
ASSERT_EQUALS(false, preprocessor.validateCfg("X",macroUsageList));
ASSERT_EQUALS(false, preprocessor.validateCfg("A=42;X", macroUsageList));
ASSERT_EQUALS(true, preprocessor.validateCfg("X=1", macroUsageList));
ASSERT_EQUALS(true, preprocessor.validateCfg("Y", macroUsageList));

macroUsageList.front().macroValueKnown = true; // #8404
ASSERT_EQUALS(true, preprocessor.validateCfg("X", macroUsageList));
}

void validateCfg2() {
const char filedata[] = "#ifdef ABC\n"
"#endif\n"
"int i = ABC;";

std::map<std::string, std::string> actual;
preprocess(filedata, actual, "file.cpp");
ASSERT_EQUALS("[file.cpp:3]: (information) Skipping configuration 'ABC' since the value of 'ABC' is unknown. Use -D if you want to check it. You can use -U to skip it explicitly.\n", errout.str());
}

void if_sizeof() { // #4071
static const char* code = "#if sizeof(unsigned short) == 2\n"
"Fred & Wilma\n"
Expand Down