Fixed #4516 (Preprocessor: wrong #if evaluation)

This commit is contained in:
Daniel Marjamki 2013-01-26 12:37:46 +01:00
parent 8738223e82
commit 641e7cba28
2 changed files with 32 additions and 12 deletions

View File

@ -1001,20 +1001,29 @@ 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.
// TODO: handle function-macros too.
// Bailout if variable A depends on variable B which depends on A..
std::set<std::string> seenVariables;
std::map<std::string, std::string>::iterator it = variables.find(varValue);
while (it != variables.end() && it->first != it->second) {
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);
TokenList tokenList(NULL);
std::istringstream istr(i->second);
if (tokenList.createTokens(istr)) {
for (Token *tok = tokenList.front(); tok; tok = tok->next()) {
if (tok->isName()) {
std::map<std::string, std::string>::iterator it = variables.find(tok->str());
while (it != variables.end() && it->first != it->second) {
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);
}
}
}
}
}
}

View File

@ -226,6 +226,7 @@ private:
TEST_CASE(define_if2);
TEST_CASE(define_if3);
TEST_CASE(define_if4); // #4079 - #define X +123
TEST_CASE(define_if5); // #4516 - #define B (A & 0x00f0)
TEST_CASE(define_ifdef);
TEST_CASE(define_ifndef1);
TEST_CASE(define_ifndef2);
@ -2738,6 +2739,16 @@ private:
ASSERT_EQUALS("\n\nFOO\n\n", preprocessor.getcode(filedata,"",""));
}
void define_if5() { // #4516 - #define B (A & 0x00f0)
const char filedata[] = "#define A 0x0010\n"
"#define B (A & 0x00f0)\n"
"#if B==0x0010\n"
"FOO\n"
"#endif";
Preprocessor preprocessor(NULL, this);
ASSERT_EQUALS("\n\n\nFOO\n\n", preprocessor.getcode(filedata,"",""));
}
void define_ifdef() {
{
const char filedata[] = "#define ABC\n"