enum: don't simplify enum constant that is assigned

This commit is contained in:
Daniel Marjamäki 2014-04-23 17:10:17 +02:00
parent 272fcc18d8
commit aea528c763
2 changed files with 9 additions and 2 deletions

View File

@ -7648,10 +7648,10 @@ void Tokenizer::simplifyEnum()
ev = &(enumValues.find(tok2->str())->second);
if (!duplicateDefinition(&tok2, ev->name)) {
if (tok2->strAt(-1) == "::" ||
Token::Match(tok2->next(), "::|[")) {
Token::Match(tok2->next(), "::|[|=")) {
// Don't replace this enum if:
// * it's preceded or followed by "::"
// * it's followed by "["
// * it's followed by "[" or "="
} else {
simplify = true;
ev = &(enumValues.find(tok2->str())->second);

View File

@ -373,6 +373,7 @@ private:
TEST_CASE(enum40);
TEST_CASE(enum41); // ticket #5212 (valgrind errors during enum simplification)
TEST_CASE(enum42); // ticket #5182 (template function call in enum value)
TEST_CASE(enum43); // lhs in assignment
TEST_CASE(enumscope1); // ticket #3949
TEST_CASE(duplicateDefinition); // ticket #3565
TEST_CASE(invalid_enum); // #5600
@ -7161,6 +7162,12 @@ private:
ASSERT_EQUALS("a = f < int , 2 > ( ) ;", checkSimplifyEnum(code));
}
void enum43() { // lhs in assignment
const char code[] = "enum { A, B };\n"
"A = 1;";
ASSERT_EQUALS("A = 1 ;", checkSimplifyEnum(code));
}
void enumscope1() { // #3949 - don't simplify enum from one function in another function
const char code[] = "void foo() { enum { A = 0, B = 1 }; }\n"
"void bar() { int a = A; }";