Skip to content
Closed
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
22 changes: 21 additions & 1 deletion lib/preprocessor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -876,7 +876,25 @@ std::string Preprocessor::getdef(std::string line, bool def)
return line;
}

/**
* Simplifies the variable map. For example if the map contains A=>B, B=>1, then A=>B is simplified to A=>1.
* @param [in,out] variables - a map of variable name to variable value. This map will be modified.
*/
static void simplifyVarMap(std::map<std::string, std::string> &variables)
{
for (std::map<std::string, std::string>::iterator i = variables.begin(); i != variables.end(); ++i) {
std::string& varValue = i->second;

// TODO: 1. tokenize the value, replace each token like this.
// TODO: 2. handle function-macros too.

std::map<std::string, std::string>::iterator it = variables.find(varValue);
while (it != variables.end() && it != i) {
varValue = it->second;
it = variables.find(varValue);
}
}
}

std::list<std::string> Preprocessor::getcfgs(const std::string &filedata, const std::string &filename)
{
Expand Down Expand Up @@ -998,6 +1016,7 @@ std::list<std::string> Preprocessor::getcfgs(const std::string &filedata, const
varmap[varname] = value;
}

simplifyVarMap(varmap);
simplifyCondition(varmap, def, false);
}

Expand Down Expand Up @@ -1350,7 +1369,7 @@ void Preprocessor::simplifyCondition(const std::map<std::string, std::string> &c
condition = "0";
}

bool Preprocessor::match_cfg_def(const std::map<std::string, std::string> &cfg, std::string def)
bool Preprocessor::match_cfg_def(std::map<std::string, std::string> cfg, std::string def)
{
/*
std::cout << "cfg: \"";
Expand All @@ -1365,6 +1384,7 @@ bool Preprocessor::match_cfg_def(const std::map<std::string, std::string> &cfg,
std::cout << "def: \"" << def << "\"\n";
*/

simplifyVarMap(cfg);
simplifyCondition(cfg, def, true);

if (cfg.find(def) != cfg.end())
Expand Down
2 changes: 1 addition & 1 deletion lib/preprocessor.h
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ class Preprocessor {
* @param def condition
* @return result when evaluating the condition
*/
bool match_cfg_def(const std::map<std::string, std::string> &cfg, std::string def);
bool match_cfg_def(std::map<std::string, std::string> cfg, std::string def);

static void getErrorMessages(ErrorLogger *errorLogger, const Settings *settings);

Expand Down
19 changes: 19 additions & 0 deletions test/testpreprocessor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,8 @@ class TestPreprocessor : public TestFixture {
TEST_CASE(if_or_1);
TEST_CASE(if_or_2);

TEST_CASE(if_macro_eq_macro); // #3536

TEST_CASE(multiline1);
TEST_CASE(multiline2);
TEST_CASE(multiline3);
Expand Down Expand Up @@ -1478,6 +1480,23 @@ class TestPreprocessor : public TestFixture {
ASSERT_EQUALS("\na1;\n\n", preprocessor.getcode(code, "Y", "test.c"));
}

void if_macro_eq_macro() {
const std::string code("#define A B\n"
"#define B 1\n"
"#define C 1\n"
"#if A == C\n"
"Wilma\n"
"#else\n"
"Betty\n"
"#endif\n");
Settings settings;
Preprocessor preprocessor(&settings, this);
std::istringstream istr(code);
std::map<std::string, std::string> actual;
preprocessor.preprocess(istr, actual, "file.c");

ASSERT_EQUALS("\n\n\n\nWilma\n\n\n\n", actual[""]);
}

void multiline1() {
const char filedata[] = "#define str \"abc\" \\\n"
Expand Down