Fixed #1422 (false positive: uninitialized variable)

This commit is contained in:
Daniel Marjamäki 2010-02-20 11:43:53 +01:00
parent a6c9a6fc54
commit 084b21494a
2 changed files with 23 additions and 1 deletions

View File

@ -3194,11 +3194,14 @@ void Tokenizer::simplifyIfAddBraces()
tok = tok->next();
Token *tempToken = tok;
bool innerIf = Token::simpleMatch(tempToken->next(), "if");
// insert close brace..
// In most cases it would work to just search for the next ';' and insert a closing brace after it.
// But here are special cases..
// * if (cond) for (;;) break;
// * if (cond1) if (cond2) { }
// * if (cond1) if (cond2) ; else ;
int parlevel = 0;
int indentlevel = 0;
while ((tempToken = tempToken->next()) != NULL)
@ -3235,7 +3238,17 @@ void Tokenizer::simplifyIfAddBraces()
}
else if (indentlevel == 0 && parlevel == 0 && tempToken->str() == ";")
break;
{
if (!innerIf)
break;
if (Token::Match(tempToken, "; else if"))
;
else if (Token::Match(tempToken, "; else"))
innerIf = false;
else
break;
}
}
if (tempToken)

View File

@ -71,6 +71,7 @@ private:
TEST_CASE(ifAddBraces7);
TEST_CASE(ifAddBraces9);
TEST_CASE(ifAddBraces10);
TEST_CASE(ifAddBraces11);
TEST_CASE(whileAddBraces);
TEST_CASE(doWhileAddBraces);
@ -633,6 +634,14 @@ private:
ASSERT_EQUALS(expected, tokenizeAndStringify(code, true));
}
void ifAddBraces11()
{
// ticket #1361
const char code[] = "{ if (x) if (y) ; else ; }";
const char expected[] = "{ if ( x ) { if ( y ) { ; } else { ; } } }";
ASSERT_EQUALS(expected, tokenizeAndStringify(code, true));
}
void whileAddBraces()
{
{