diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index 2c00a0449..18f6a974f 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -7884,11 +7884,13 @@ void Tokenizer::simplifyEnum() } if (simplify) { - if (ev->value) + if (ev->value) { tok2->str(ev->value->str()); - else { + if (hasClass) + tok2->deleteNext(2); + } else { tok2 = tok2->previous(); - tok2->deleteNext(); + tok2->deleteNext(hasClass ? 3 : 1); bool hasOp = false; int indentlevel = 0; for (const Token *enumtok = ev->start; enumtok != ev->end; enumtok = enumtok->next()) { @@ -7907,13 +7909,10 @@ void Tokenizer::simplifyEnum() tok2 = copyTokens(startPar, ev->start, ev->end); tok2->insertToken(")"); Token::createMutualLinks(startPar, tok2->next()); + tok2 = tok2->next(); } } - if (hasClass) { - tok2->deleteNext(2); - } - simplify = false; } } diff --git a/test/testsimplifytokens.cpp b/test/testsimplifytokens.cpp index 144bc206d..4f1ec27aa 100644 --- a/test/testsimplifytokens.cpp +++ b/test/testsimplifytokens.cpp @@ -375,6 +375,7 @@ private: TEST_CASE(enum38); // ticket #4463 (when throwing enum id, don't warn about shadow variable) TEST_CASE(enum39); // ticket #5145 (fp variable hides enum) TEST_CASE(enum40); + TEST_CASE(enum41); // ticket #5212 (valgrind errors during enum simplification) TEST_CASE(enumscope1); // ticket #3949 TEST_CASE(duplicateDefinition); // ticket #3565 @@ -7500,6 +7501,18 @@ private: ASSERT_EQUALS("void f ( ) { x = y + ( 3 ) ; }", checkSimplifyEnum(code)); } + void enum41() { // ticket #5212 (valgrind errors during enum simplification) + const char code[] = "namespace Foo {\n" + " enum BarConfig {\n" + " eBitOne = (1 << 0),\n" + " eBitTwo = (1 << 1),\n" + " eAll = eBitOne|eBitTwo\n" + " };\n" + "}\n" + "int x = Foo::eAll;"; + ASSERT_EQUALS("int x ; x = ( 1 ) | 2 ;", 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; }";