Fixed #3123 ([False Positive] Shadowed enum)

This commit is contained in:
Johan Samuelson 2011-10-22 13:43:53 +02:00 committed by Daniel Marjamäki
parent 84cd0bd585
commit 3d8fa2f76e
2 changed files with 19 additions and 8 deletions

View File

@ -7912,14 +7912,16 @@ void Tokenizer::simplifyEnum()
simplify = true;
hasClass = true;
} else if (inScope && !exitThisScope && tok2->str() == enumName->str()) {
if (Token::simpleMatch(tok2->previous(), "::") ||
Token::Match(tok2->next(), "::|[")) {
// Don't replace this enum if:
// * it's preceded or followed by "::"
// * it's followed by "["
} else if (!duplicateDefinition(&tok2, enumName)) {
simplify = true;
hasClass = false;
if (!duplicateDefinition(&tok2, enumName)) {
if (Token::simpleMatch(tok2->previous(), "::") ||
Token::Match(tok2->next(), "::|[")) {
// Don't replace this enum if:
// * it's preceded or followed by "::"
// * it's followed by "["
} else {
simplify = true;
hasClass = false;
}
} else {
// something with the same name.
exitScope = level;

View File

@ -339,6 +339,7 @@ private:
TEST_CASE(enum25); // ticket #2966
TEST_CASE(enum26); // ticket #2975 (segmentation fault)
TEST_CASE(enum27); // ticket #3005 (segmentation fault)
TEST_CASE(enum28);
// remove "std::" on some standard functions
TEST_CASE(removestd);
@ -6863,6 +6864,14 @@ private:
ASSERT_EQUALS("[test.cpp:1]: (error) syntax error\n", errout.str());
}
void enum28() {
const char code[] = "enum { x=0 };\n"
"void f() { char x[4]; memset(x, 0, 4); \n"
"{ x } };\n"
"void g() { x; }";
ASSERT_EQUALS("; void f ( ) { char x [ 4 ] ; memset ( x , 0 , 4 ) ; { x } } ; void g ( ) { 0 ; }", tok(code, true));
}
void removestd() {
ASSERT_EQUALS("; strcpy ( a , b ) ;", tok("; std::strcpy(a,b);"));
ASSERT_EQUALS("; strcat ( a , b ) ;", tok("; std::strcat(a,b);"));