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

View File

@ -76,6 +76,8 @@ private:
TEST_CASE(whileAddBraces); TEST_CASE(whileAddBraces);
TEST_CASE(doWhileAddBraces); TEST_CASE(doWhileAddBraces);
TEST_CASE(forAddBraces);
TEST_CASE(pointers_condition); TEST_CASE(pointers_condition);
TEST_CASE(simplifyKnownVariables1); TEST_CASE(simplifyKnownVariables1);
@ -655,12 +657,10 @@ private:
} }
void whileAddBraces() void whileAddBraces()
{
{ {
const char code[] = ";while(a);"; const char code[] = ";while(a);";
ASSERT_EQUALS("; while ( a ) { ; }", tokenizeAndStringify(code, true)); ASSERT_EQUALS("; while ( a ) { ; }", tokenizeAndStringify(code, true));
} }
}
void doWhileAddBraces() 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[]) std::string simplifyKnownVariables(const char code[])
{ {
Tokenizer tokenizer; Tokenizer tokenizer;