diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index 0aa08544e..7dde2535d 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -7891,7 +7891,25 @@ void Tokenizer::simplifyEnum() else { tok2 = tok2->previous(); tok2->deleteNext(); - tok2 = copyTokens(tok2, ev->start, ev->end); + bool hasOp = false; + int indentlevel = 0; + for (const Token *enumtok = ev->start; enumtok != ev->end; enumtok = enumtok->next()) { + if (enumtok->str() == "(") + ++indentlevel; + else if (enumtok->str() == ")") + --indentlevel; + if (indentlevel == 0) + hasOp |= enumtok->isOp(); + } + if (!hasOp) + tok2 = copyTokens(tok2, ev->start, ev->end); + else { + tok2->insertToken("("); + Token *startPar = tok2->next(); + tok2 = copyTokens(startPar, ev->start, ev->end); + tok2->insertToken(")"); + Token::createMutualLinks(startPar, tok2->next()); + } } if (hasClass) { diff --git a/test/testsimplifytokens.cpp b/test/testsimplifytokens.cpp index 0df4d2de2..34ccb74de 100644 --- a/test/testsimplifytokens.cpp +++ b/test/testsimplifytokens.cpp @@ -373,6 +373,7 @@ private: TEST_CASE(enum37); // ticket #4280 (shadow variable) 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(enumscope1); // ticket #3949 TEST_CASE(duplicateDefinition); // ticket #3565 @@ -7069,10 +7070,10 @@ private: "int sum = a + b + c + d + e + f + g;"; const char expected[] = "int sum ; sum = " "sizeof ( int ) + " - "1 + sizeof ( int ) + " - "1 + sizeof ( int ) + 101 + " // 101 = 100 + 1 - "sizeof ( int ) + 102 + " // 102 = 100 + 1 + 1 - "sizeof ( int ) + 283 " // 283 = 100+2+90+91 + "( 1 + sizeof ( int ) ) + " + "( 1 + sizeof ( int ) + 100 ) + " // 101 = 100 + 1 + "( 1 + sizeof ( int ) + 101 ) + " // 102 = 100 + 1 + 1 + "( 1 + sizeof ( int ) + 102 ) + 181 " // 283 = 100+2+90+91 ";"; ASSERT_EQUALS(expected, tok(code, false)); @@ -7417,7 +7418,7 @@ private: void enum32() { // #3998 - wrong enum simplification => access violation const char code[] = "enum { x=(32), y=x, z }; { a, z }"; - ASSERT_EQUALS("{ a , 33 }", checkSimplifyEnum(code)); + ASSERT_EQUALS("{ a , ( 33 ) }", checkSimplifyEnum(code)); } void enum33() { // #4015 - segmentation fault @@ -7470,6 +7471,11 @@ private: ASSERT_EQUALS("", errout.str()); } + void enum40() { + const char code[] = "enum { A=(1<<0)|(1<<1) }; void f() { x = y + A; }"; + ASSERT_EQUALS("void f ( ) { x = y + ( 3 ) ; }", 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; }";