Fixed #1809 (false positive: uninitialized variable with nested if)

This commit is contained in:
Daniel Marjamäki 2010-06-26 17:15:44 +02:00
parent 539804369a
commit f82895c1f2
2 changed files with 22 additions and 0 deletions

View File

@ -3934,6 +3934,14 @@ void Tokenizer::simplifyIfAddBraces()
{ {
if (tempToken->str() == "{") if (tempToken->str() == "{")
{ {
if (Token::simpleMatch(tempToken->previous(),"else {"))
{
if (innerIf)
tempToken = tempToken->link();
else
tempToken = tempToken->tokAt(-2);
break;
}
tempToken = tempToken->link(); tempToken = tempToken->link();
if (tempToken->next()->isName() && tempToken->next()->str() != "else") if (tempToken->next()->isName() && tempToken->next()->str() != "else")
break; break;

View File

@ -75,6 +75,7 @@ private:
TEST_CASE(ifAddBraces10); TEST_CASE(ifAddBraces10);
TEST_CASE(ifAddBraces11); TEST_CASE(ifAddBraces11);
TEST_CASE(ifAddBraces12); TEST_CASE(ifAddBraces12);
TEST_CASE(ifAddBraces13);
TEST_CASE(whileAddBraces); TEST_CASE(whileAddBraces);
TEST_CASE(doWhileAddBraces); TEST_CASE(doWhileAddBraces);
@ -671,6 +672,19 @@ private:
ASSERT_EQUALS(expected, tokenizeAndStringify(code, true)); ASSERT_EQUALS(expected, tokenizeAndStringify(code, true));
} }
void ifAddBraces13()
{
// ticket #1809
const char code[] = "{ if (x) if (y) { } else { } else { } }";
const char expected[] = "{ if ( x ) { if ( y ) { } else { } } else { } }";
ASSERT_EQUALS(expected, tokenizeAndStringify(code, true));
// ticket #1809
const char code2[] = "{ if (x) while (y) { } else { } }";
const char expected2[] = "{ if ( x ) { while ( y ) { } } else { } }";
ASSERT_EQUALS(expected2, tokenizeAndStringify(code2, true));
}
void whileAddBraces() void whileAddBraces()
{ {
const char code[] = ";while(a);"; const char code[] = ";while(a);";