Tokenizer: simplify enums better. add parentheses to enforce operator precedence.

This commit is contained in:
Daniel Marjamäki 2013-11-20 18:04:28 +01:00
parent e8eeb90adb
commit ef108c49ec
2 changed files with 30 additions and 6 deletions

View File

@ -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) {

View File

@ -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; }";