Fixed #4516 (Preprocessor: wrong #if evaluation)
This commit is contained in:
parent
8738223e82
commit
641e7cba28
|
@ -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) {
|
for (std::map<std::string, std::string>::iterator i = variables.begin(); i != variables.end(); ++i) {
|
||||||
std::string& varValue = i->second;
|
std::string& varValue = i->second;
|
||||||
|
|
||||||
// TODO: 1. tokenize the value, replace each token like this.
|
// TODO: handle function-macros too.
|
||||||
// TODO: 2. handle function-macros too.
|
|
||||||
|
|
||||||
|
// Bailout if variable A depends on variable B which depends on A..
|
||||||
std::set<std::string> seenVariables;
|
std::set<std::string> seenVariables;
|
||||||
std::map<std::string, std::string>::iterator it = variables.find(varValue);
|
|
||||||
while (it != variables.end() && it->first != it->second) {
|
TokenList tokenList(NULL);
|
||||||
if (seenVariables.find(it->first) != seenVariables.end()) {
|
std::istringstream istr(i->second);
|
||||||
// We have already seen this variable. there is a cycle of #define that we can't process at
|
if (tokenList.createTokens(istr)) {
|
||||||
// this time. Stop trying to simplify the current variable and leave it as is.
|
for (Token *tok = tokenList.front(); tok; tok = tok->next()) {
|
||||||
break;
|
if (tok->isName()) {
|
||||||
} else {
|
std::map<std::string, std::string>::iterator it = variables.find(tok->str());
|
||||||
seenVariables.insert(it->first);
|
while (it != variables.end() && it->first != it->second) {
|
||||||
varValue = it->second;
|
if (seenVariables.find(it->first) != seenVariables.end()) {
|
||||||
it = variables.find(varValue);
|
// 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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -226,6 +226,7 @@ private:
|
||||||
TEST_CASE(define_if2);
|
TEST_CASE(define_if2);
|
||||||
TEST_CASE(define_if3);
|
TEST_CASE(define_if3);
|
||||||
TEST_CASE(define_if4); // #4079 - #define X +123
|
TEST_CASE(define_if4); // #4079 - #define X +123
|
||||||
|
TEST_CASE(define_if5); // #4516 - #define B (A & 0x00f0)
|
||||||
TEST_CASE(define_ifdef);
|
TEST_CASE(define_ifdef);
|
||||||
TEST_CASE(define_ifndef1);
|
TEST_CASE(define_ifndef1);
|
||||||
TEST_CASE(define_ifndef2);
|
TEST_CASE(define_ifndef2);
|
||||||
|
@ -2738,6 +2739,16 @@ private:
|
||||||
ASSERT_EQUALS("\n\nFOO\n\n", preprocessor.getcode(filedata,"",""));
|
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() {
|
void define_ifdef() {
|
||||||
{
|
{
|
||||||
const char filedata[] = "#define ABC\n"
|
const char filedata[] = "#define ABC\n"
|
||||||
|
|
Loading…
Reference in New Issue