removeReduntantConditions() can now handle if( true ) else if () conditions also.

This commit is contained in:
Reijo Tomperi 2008-12-26 12:55:53 +00:00
parent 12a898c2fa
commit b9494c8081
3 changed files with 36 additions and 17 deletions

View File

@ -114,19 +114,17 @@ private:
ASSERT_EQUALS( tok(code2), tok(code1) ); ASSERT_EQUALS( tok(code2), tok(code1) );
} }
// TODO, uncomment below and fix
/*
{ {
const char code1[] = " void f() { int a; bool use = true; if( use ) a=0; else if( bb ) a=1; int c=1; } "; const char code1[] = " void f() { int a; bool use = true; if( use ) a=0; else if( bb ) a=1; int c=1; } ";
const char code2[] = " void f() { int a; bool use = true; if( use ) a=0; int c=1; } "; const char code2[] = " void f() { int a; bool use = true; { a=0;} int c=1; } ";
ASSERT_EQUALS( tok(code2), tok(code1) ); ASSERT_EQUALS( tok(code2), tok(code1) );
} }
{ {
const char code1[] = " void f() { int a; bool use = true; if( use ) a=0; else if( bb ) a=1; else if( cc ) a=33; else { gg = 0; } int c=1; } "; const char code1[] = " void f() { int a; bool use = true; if( use ) a=0; else if( bb ) a=1; else if( cc ) a=33; else { gg = 0; } int c=1; } ";
const char code2[] = " void f() { int a; bool use = true; if( use ) a=0; int c=1; } "; const char code2[] = " void f() { int a; bool use = true; { a=0; }int c=1; } ";
ASSERT_EQUALS( tok(code2), tok(code1) ); ASSERT_EQUALS( tok(code2), tok(code1) );
}*/ }
} }
}; };

View File

@ -1044,7 +1044,7 @@ void Tokenizer::simplifyTokenList()
} }
//--------------------------------------------------------------------------- //---------------------------------------------------------------------------
const TOKEN *Tokenizer::findClosing( const TOKEN *tok ) const TOKEN *Tokenizer::findClosing( const TOKEN *tok, const char *start, const char *end )
{ {
if( !tok ) if( !tok )
return 0; return 0;
@ -1053,13 +1053,13 @@ const TOKEN *Tokenizer::findClosing( const TOKEN *tok )
int indentLevel = 0; int indentLevel = 0;
for ( const TOKEN *closing = tok->next(); closing; closing = closing->next() ) for ( const TOKEN *closing = tok->next(); closing; closing = closing->next() )
{ {
if( closing->str() == "{" ) if( closing->str() == start )
{ {
indentLevel++; indentLevel++;
continue; continue;
} }
if( closing->str() == "}" ) if( closing->str() == end )
indentLevel--; indentLevel--;
if( indentLevel >= 0 ) if( indentLevel >= 0 )
@ -1088,7 +1088,7 @@ bool Tokenizer::removeReduntantConditions()
const TOKEN *elseTag = 0; const TOKEN *elseTag = 0;
// Find the closing "}" // Find the closing "}"
elseTag = Tokenizer::findClosing( tok->tokAt( 4 ) ); elseTag = Tokenizer::findClosing( tok->tokAt( 4 ), "{", "}" );
if( elseTag ) if( elseTag )
elseTag = elseTag->next(); elseTag = elseTag->next();
@ -1111,8 +1111,27 @@ bool Tokenizer::removeReduntantConditions()
else else
{ {
// Keep first if, remove every else if and else after it // Keep first if, remove every else if and else after it
const TOKEN *lastTagInIf = elseTag->tokAt( 2 );
while( lastTagInIf )
{
if( lastTagInIf->str() == "(" )
{
lastTagInIf = Tokenizer::findClosing( lastTagInIf, "(", ")" );
lastTagInIf = lastTagInIf->next();
}
// TODO, implement lastTagInIf = Tokenizer::findClosing( lastTagInIf, "{", "}" );
lastTagInIf = lastTagInIf->next();
if( !TOKEN::simpleMatch( lastTagInIf, "else" ) )
break;
lastTagInIf = lastTagInIf->next();
if( TOKEN::simpleMatch( lastTagInIf, "if" ) )
lastTagInIf = lastTagInIf->next();
}
TOKEN::eraseTokens( elseTag->previous(), lastTagInIf );
ret = true;
} }
} }
else else
@ -1127,14 +1146,13 @@ bool Tokenizer::removeReduntantConditions()
tok->setstr( ";" ); tok->setstr( ";" );
TOKEN::eraseTokens( tok, elseTag->tokAt( 1 ) ); TOKEN::eraseTokens( tok, elseTag->tokAt( 1 ) );
ret = true;
} }
else else
{ {
if( TOKEN::simpleMatch( elseTag->tokAt( 1 ), "{" ) ) if( TOKEN::simpleMatch( elseTag->tokAt( 1 ), "{" ) )
{ {
// Convert "if( true ) {aaa;} else {bbb;}" => "{aaa;}" // Convert "if( true ) {aaa;} else {bbb;}" => "{aaa;}"
const TOKEN *end = Tokenizer::findClosing( elseTag->tokAt( 1 ) ); const TOKEN *end = Tokenizer::findClosing( elseTag->tokAt( 1 ), "{", "}" );
if( !end ) if( !end )
{ {
// Possibly syntax error in code // Possibly syntax error in code
@ -1152,8 +1170,9 @@ bool Tokenizer::removeReduntantConditions()
tok->setstr( ";" ); tok->setstr( ";" );
TOKEN::eraseTokens( tok, tok->tokAt(5) ); TOKEN::eraseTokens( tok, tok->tokAt(5) );
ret = true;
} }
ret = true;
} }
} }

View File

@ -92,11 +92,13 @@ private:
}; };
/** /**
* Finds matching "}" for "{". * Finds matching "end" for "start".
* @param tok The "{" * @param tok The start tag
* @return The "}" that matches given parameter or 0 if not found. * @param start e.g. "{"
* @param end e.g. "}"
* @return The end tag that matches given parameter or 0 if not found.
*/ */
static const TOKEN *findClosing( const TOKEN *tok ); static const TOKEN *findClosing( const TOKEN *tok, const char *start, const char *end );
void Define(const char Name[], const char Value[]); void Define(const char Name[], const char Value[]);