Preprocessor: Improved handling of expressions in simplifyVarMap. Ticket: #4516
This commit is contained in:
parent
641e7cba28
commit
f69b1f426e
|
@ -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;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -2740,13 +2740,25 @@ private:
|
||||||
}
|
}
|
||||||
|
|
||||||
void define_if5() { // #4516 - #define B (A & 0x00f0)
|
void define_if5() { // #4516 - #define B (A & 0x00f0)
|
||||||
const char filedata[] = "#define A 0x0010\n"
|
{
|
||||||
"#define B (A & 0x00f0)\n"
|
const char filedata[] = "#define A 0x0010\n"
|
||||||
"#if B==0x0010\n"
|
"#define B (A & 0x00f0)\n"
|
||||||
"FOO\n"
|
"#if B==0x0010\n"
|
||||||
"#endif";
|
"FOO\n"
|
||||||
Preprocessor preprocessor(NULL, this);
|
"#endif";
|
||||||
ASSERT_EQUALS("\n\n\nFOO\n\n", preprocessor.getcode(filedata,"",""));
|
Preprocessor preprocessor(NULL, this);
|
||||||
|
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() {
|
||||||
|
|
Loading…
Reference in New Issue