Tokenizer::simplifyEnum: Improved handling of shadow variables

This commit is contained in:
Daniel Marjamäki 2013-06-04 06:51:01 +02:00
parent 90a6472af9
commit fa1fd31667
2 changed files with 30 additions and 3 deletions

View File

@ -7433,6 +7433,7 @@ void Tokenizer::simplifyEnum()
if (!shadowId.empty()) if (!shadowId.empty())
shadowId.push(shadowId.top()); shadowId.push(shadowId.top());
// are there shadow arguments?
if (Token::simpleMatch(tok2->previous(), ") {") || Token::simpleMatch(tok2->tokAt(-2), ") const {")) { if (Token::simpleMatch(tok2->previous(), ") {") || Token::simpleMatch(tok2->tokAt(-2), ") const {")) {
std::set<std::string> shadowArg; std::set<std::string> shadowArg;
for (const Token* arg = tok2; arg && arg->str() != "("; arg = arg->previous()) { for (const Token* arg = tok2; arg && arg->str() != "("; arg = arg->previous()) {
@ -7448,7 +7449,24 @@ void Tokenizer::simplifyEnum()
} }
} }
// are there shadow variables in the scope?
std::set<std::string> shadowVars;
for (const Token *tok3 = tok2->next(); tok3 && tok3->str() != "}"; tok3 = tok3->next()) {
if (tok3->str() == "{")
tok3 = tok3->link(); // skip inner scopes
else if (tok3->isName() && enumValues.find(tok3->str()) != enumValues.end()) {
if (Token::Match(tok3->previous(), "*|%type%") || Token::Match(tok3->previous(), "& %type% =")) // variable declaration?
shadowVars.insert(tok3->str());
} }
}
if (!shadowVars.empty()) {
if (shadowId.empty())
shadowId.push(shadowVars);
else
shadowId.top().insert(shadowVars.begin(), shadowVars.end());
}
}
// Function head // Function head
} else if (Token::Match(tok2, "%var% (")) { } else if (Token::Match(tok2, "%var% (")) {
const Token *prev = tok2->previous(); const Token *prev = tok2->previous();

View File

@ -7334,9 +7334,18 @@ private:
ASSERT_EQUALS("struct X { X ( int ) { int y ; y = ( int ) 1 ; } } ;", checkSimplifyEnum(code)); ASSERT_EQUALS("struct X { X ( int ) { int y ; y = ( int ) 1 ; } } ;", checkSimplifyEnum(code));
} }
void enum37() { // #4280 void enum37() { // #4280 - shadow variables
const char code[] = "enum { a, b }; void f(int a) { return a + 1; }"; const char code1[] = "enum { a, b }; void f(int a) { return a + 1; }";
ASSERT_EQUALS("void f ( int a ) { return a + 1 ; }", checkSimplifyEnum(code)); ASSERT_EQUALS("void f ( int a ) { return a + 1 ; }", checkSimplifyEnum(code1));
const char code2[] = "enum { a, b }; void f() { int a; }";
ASSERT_EQUALS("void f ( ) { int a ; }", checkSimplifyEnum(code2));
const char code3[] = "enum { a, b }; void f() { int *a=do_something(); }";
ASSERT_EQUALS("void f ( ) { int * a ; a = do_something ( ) ; }", checkSimplifyEnum(code3));
const char code4[] = "enum { a, b }; void f() { int &a=x; }";
ASSERT_EQUALS("void f ( ) { int & a = x ; }", checkSimplifyEnum(code4));
} }
void enumscope1() { // #3949 - don't simplify enum from one function in another function void enumscope1() { // #3949 - don't simplify enum from one function in another function