Fixed #1718 (Tokenizer add close braces to for loop at wrong place when if-else presents inside)

This commit is contained in:
Daniel Marjamäki 2010-06-13 08:00:46 +02:00
parent 18bb7488b9
commit 70bfa890a8
2 changed files with 58 additions and 30 deletions

View File

@ -3864,6 +3864,12 @@ void Tokenizer::simplifyIfAddBraces()
{
for (Token *tok = _tokens; tok; tok = tok ? tok->next() : NULL)
{
if (tok->str() == "(")
{
tok = tok->link();
continue;
}
if (tok->previous() && !Token::Match(tok->previous(), ";|{|}|else|)|:"))
continue;
@ -3920,42 +3926,30 @@ void Tokenizer::simplifyIfAddBraces()
// * if (cond) for (;;) break;
// * if (cond1) if (cond2) { }
// * if (cond1) if (cond2) ; else ;
int parlevel = 0;
int indentlevel = 0;
while ((tempToken = tempToken->next()) != NULL)
{
if (tempToken->str() == "{")
++indentlevel;
else if (tempToken->str() == "}")
{
--indentlevel;
if (indentlevel == 0 && parlevel == 0)
tempToken = tempToken->link();
if (tempToken->next()->isName() && tempToken->next()->str() != "else")
break;
else if (indentlevel < 0 && parlevel == 0)
{
// insert closing brace before this
tempToken = tempToken->previous();
break;
}
continue;
}
else if (tempToken->str() == "(")
++parlevel;
else if (tempToken->str() == ")")
if (tempToken->str() == "(")
{
if (parlevel == 0)
{
tok->deleteThis();
tempToken = 0;
break;
}
--parlevel;
tempToken = tempToken->link();
continue;
}
else if (indentlevel == 0 && parlevel == 0 && tempToken->str() == ";")
if (tempToken->str() == "}")
{
// insert closing brace before this token
tempToken = tempToken->previous();
break;
}
if (tempToken->str() == ";")
{
if (!innerIf)
break;

View File

@ -76,6 +76,8 @@ private:
TEST_CASE(whileAddBraces);
TEST_CASE(doWhileAddBraces);
TEST_CASE(forAddBraces);
TEST_CASE(pointers_condition);
TEST_CASE(simplifyKnownVariables1);
@ -656,10 +658,8 @@ private:
void whileAddBraces()
{
{
const char code[] = ";while(a);";
ASSERT_EQUALS("; while ( a ) { ; }", tokenizeAndStringify(code, true));
}
const char code[] = ";while(a);";
ASSERT_EQUALS("; while ( a ) { ; }", tokenizeAndStringify(code, true));
}
void doWhileAddBraces()
@ -722,6 +722,40 @@ private:
}
}
void forAddBraces()
{
{
const char code[] = "void f() {\n"
" for(;;)\n"
" if (a) { }\n"
" else { }\n"
"}";
const char expected[] = "void f ( ) {\n"
"for ( ; ; ) {\n"
"if ( a ) { }\n"
"else { } }\n"
"}";
ASSERT_EQUALS(expected, tokenizeAndStringify(code, true));
}
{
const char code[] = "void f() {\n"
" for(;;)\n"
" if (a) { }\n"
" else if (b) { }\n"
" else { }\n"
"}";
const char expected[] = "void f ( ) {\n"
"for ( ; ; ) {\n"
"if ( a ) { }\n"
"else { if ( b ) { }\n"
"else { } } }\n"
"}";
ASSERT_EQUALS(expected, tokenizeAndStringify(code, true));
}
}
std::string simplifyKnownVariables(const char code[])
{
Tokenizer tokenizer;