simplifyIfAddBraces : Fixed minor bug that caused the closing brace to be put on the wrong place

This commit is contained in:
Daniel Marjamäki 2008-12-22 18:32:04 +00:00
parent 3a4e113f64
commit 78f46c5848
2 changed files with 27 additions and 0 deletions

View File

@ -49,6 +49,7 @@ private:
TEST_CASE( ifAddBraces1 );
TEST_CASE( ifAddBraces2 );
TEST_CASE( ifAddBraces3 );
TEST_CASE( ifAddBraces4 );
TEST_CASE( numeric_true_condition );
@ -288,6 +289,31 @@ private:
ASSERT_EQUALS( std::string(" void f ( ) { if ( a ) { for ( ; ; ) { } } }"), ostr.str() );
}
void ifAddBraces4()
{
const char code[] = "char * foo ()\n"
"{\n"
" char *str = malloc(10);\n"
" if (somecondition)\n"
" for ( ; ; )\n"
" { }\n"
" return str;\n"
"}\n";
// tokenize..
Tokenizer tokenizer;
std::istringstream istr(code);
tokenizer.tokenize(istr, "test.cpp");
ASSERT_EQUALS( true, tokenizer.simplifyIfAddBraces() );
std::ostringstream ostr;
for (const TOKEN *tok = tokenizer.tokens(); tok; tok = tok->next())
ostr << " " << tok->str();
ASSERT_EQUALS( std::string(" char * foo ( ) { char * str = malloc ( 10 ) ; if ( somecondition ) { for ( ; ; ) { } } return str ; }"), ostr.str() );
}
void simplifyKnownVariables1()
{
const char code[] = "void f()\n"

View File

@ -1242,6 +1242,7 @@ bool Tokenizer::simplifyIfAddBraces()
// insert open brace..
tok->insertToken("{");
tok = tok->next();
// insert close brace..
// In most cases it would work to just search for the next ';' and insert a closing brace after it.