Preprocessor: Improved handling of expressions in simplifyVarMap. Ticket: #4516

This commit is contained in:
Daniel Marjamki 2013-01-26 16:21:33 +01:00
parent 641e7cba28
commit f69b1f426e
2 changed files with 60 additions and 26 deletions

View File

@ -992,6 +992,40 @@ std::string Preprocessor::getdef(std::string line, bool def)
return line; return line;
} }
/** Simplify variable in variable map. */
static void simplifyVarMapExpandValue(Token *tok, const std::map<std::string, std::string> &variables, std::set<std::string> seenVariables)
{
// TODO: handle function-macros too.
// Prevent infinite recursion..
if (seenVariables.find(tok->str()) != seenVariables.end())
return;
seenVariables.insert(tok->str());
const std::map<std::string, std::string>::const_iterator it = variables.find(tok->str());
if (it != variables.end()) {
TokenList tokenList(NULL);
std::istringstream istr(it->second);
if (tokenList.createTokens(istr)) {
// expand token list
for (Token *tok2 = tokenList.front(); tok2; tok2 = tok2->next()) {
if (tok->isName()) {
simplifyVarMapExpandValue(tok2, variables, seenVariables);
}
}
// insert token list into "parent" token list
for (const Token *tok2 = tokenList.front(); tok2; tok2 = tok2->next()) {
if (tok2->previous()) {
tok->insertToken(tok2->str());
tok = tok->next();
} else
tok->str(tok2->str());
}
}
}
}
/** /**
* Simplifies the variable map. For example if the map contains A=>B, B=>1, then A=>B is simplified to A=>1. * 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. * @param [in,out] variables - a map of variable name to variable value. This map will be modified.
@ -999,32 +1033,20 @@ std::string Preprocessor::getdef(std::string line, bool def)
static void simplifyVarMap(std::map<std::string, std::string> &variables) 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) { for (std::map<std::string, std::string>::iterator i = variables.begin(); i != variables.end(); ++i) {
std::string& varValue = i->second;
// TODO: handle function-macros too.
// Bailout if variable A depends on variable B which depends on A..
std::set<std::string> seenVariables;
TokenList tokenList(NULL); TokenList tokenList(NULL);
std::istringstream istr(i->second); std::istringstream istr(i->second);
if (tokenList.createTokens(istr)) { if (tokenList.createTokens(istr)) {
for (Token *tok = tokenList.front(); tok; tok = tok->next()) { for (Token *tok = tokenList.front(); tok; tok = tok->next()) {
if (tok->isName()) { if (tok->isName()) {
std::map<std::string, std::string>::iterator it = variables.find(tok->str()); std::set<std::string> seenVariables;
while (it != variables.end() && it->first != it->second) { simplifyVarMapExpandValue(tok, variables, seenVariables);
if (seenVariables.find(it->first) != seenVariables.end()) {
// We have already seen this variable. there is a cycle of #define that we can't process at
// this time. Stop trying to simplify the current variable and leave it as is.
break;
} else {
seenVariables.insert(it->first);
varValue = it->second;
it = variables.find(varValue);
}
}
} }
} }
std::string str;
for (const Token *tok = tokenList.front(); tok; tok = tok->next())
str.append((tok->previous() ? " " : "") + tok->str());
i->second = str;
} }
} }
} }

View File

@ -2740,6 +2740,7 @@ private:
} }
void define_if5() { // #4516 - #define B (A & 0x00f0) void define_if5() { // #4516 - #define B (A & 0x00f0)
{
const char filedata[] = "#define A 0x0010\n" const char filedata[] = "#define A 0x0010\n"
"#define B (A & 0x00f0)\n" "#define B (A & 0x00f0)\n"
"#if B==0x0010\n" "#if B==0x0010\n"
@ -2748,6 +2749,17 @@ private:
Preprocessor preprocessor(NULL, this); Preprocessor preprocessor(NULL, this);
ASSERT_EQUALS("\n\n\nFOO\n\n", preprocessor.getcode(filedata,"","")); ASSERT_EQUALS("\n\n\nFOO\n\n", preprocessor.getcode(filedata,"",""));
} }
{
const char filedata[] = "#define A 0x00f0\n"
"#define B (16)\n"
"#define C (B & A)\n"
"#if C==0x0010\n"
"FOO\n"
"#endif";
Preprocessor preprocessor(NULL, this);
ASSERT_EQUALS("\n\n\n\nFOO\n\n", preprocessor.getcode(filedata,"",""));
}
}
void define_ifdef() { void define_ifdef() {
{ {