More improvement to removeReduntantConditions() and few tests for it also

This commit is contained in:
Reijo Tomperi 2008-12-21 21:04:58 +00:00
parent 496b09a377
commit 85e6ab5226
3 changed files with 126 additions and 31 deletions

View File

@ -74,27 +74,59 @@ private:
{
{
const char code1[] = " void f() { int a; bool use = false; if( use ) { a=0; } else {a=1;} } ";
const char code2[] = " void f() { int a; bool use = false; if( false ) { a=0; } else {a=1;} } ";
ASSERT_EQUALS( tok(code1), tok(code2) );
const char code2[] = " void f() { int a; bool use = false; {a=1;} } ";
ASSERT_EQUALS( tok(code2), tok(code1) );
}
{
const char code1[] = " void f() { int a; bool use = true; if( use ) { a=0; } else {a=1;} } ";
const char code2[] = " void f() { int a; bool use = true; if( true ) { a=0; } else {a=1;} } ";
ASSERT_EQUALS( tok(code1), tok(code2) );
const char code2[] = " void f() { int a; bool use = true; { a=0; } } ";
ASSERT_EQUALS( tok(code2), tok(code1) );
}
{
const char code1[] = " void f() { int a; int use = 5; if( use ) { a=0; } else {a=1;} } ";
const char code2[] = " void f() { int a; int use = 5; if( true ) { a=0; } else {a=1;} } ";
ASSERT_EQUALS( tok(code1), tok(code2) );
const char code2[] = " void f() { int a; int use = 5; { a=0; } } ";
ASSERT_EQUALS( tok(code2), tok(code1) );
}
{
const char code1[] = " void f() { int a; int use = 0; if( use ) { a=0; } else {a=1;} } ";
const char code2[] = " void f() { int a; int use = 0; if( false ) { a=0; } else {a=1;} } ";
ASSERT_EQUALS( tok(code1), tok(code2) );
const char code2[] = " void f() { int a; int use = 0; {a=1;} } ";
ASSERT_EQUALS( tok(code2), tok(code1) );
}
{
const char code1[] = " void f() { int a; bool use = false; if( use ) a=0; else a=1; int c=1; } ";
const char code2[] = " void f() { int a; bool use = false; a=1; int c=1; } ";
ASSERT_EQUALS( tok(code2), tok(code1) );
}
{
const char code1[] = " void f() { int a; bool use = true; if( use ) a=0; else a=1; int c=1; } ";
const char code2[] = " void f() { int a; bool use = true; a=0; int c=1; } ";
ASSERT_EQUALS( tok(code2), tok(code1) );
}
{
const char code1[] = " void f() { int a; bool use = false; if( use ) a=0; else if( bb ) a=1; int c=1; } ";
const char code2[] = " void f() { int a; bool use = false; if( bb ) a=1; int c=1; } ";
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 code2[] = " void f() { int a; bool use = true; if( use ) a=0; int c=1; } ";
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 code2[] = " void f() { int a; bool use = true; if( use ) a=0; int c=1; } ";
ASSERT_EQUALS( tok(code2), tok(code1) );
}*/
}
};

View File

@ -1038,6 +1038,34 @@ void Tokenizer::simplifyTokenList()
}
//---------------------------------------------------------------------------
const TOKEN *Tokenizer::findClosing( const TOKEN *tok )
{
if( !tok )
return 0;
// Find the closing "}"
int indentLevel = 0;
for ( const TOKEN *closing = tok->next(); closing; closing = closing->next() )
{
if( TOKEN::Match( closing, "{" ) )
{
indentLevel++;
continue;
}
if( TOKEN::Match( closing, "}" ) )
indentLevel--;
if( indentLevel >= 0 )
continue;
// Closing } is found.
return closing;
}
return 0;
}
bool Tokenizer::removeReduntantConditions()
{
bool ret = false;
@ -1052,25 +1080,9 @@ bool Tokenizer::removeReduntantConditions()
if( TOKEN::Match( tok->tokAt( 4 ), "{" ) )
{
// Find the closing "}"
int indentLevel = 0;
for ( const TOKEN *closing = tok->tokAt( 5 ); closing; closing = closing->next() )
{
if( TOKEN::Match( closing, "{" ) )
{
indentLevel++;
continue;
}
if( TOKEN::Match( closing, "}" ) )
indentLevel--;
if( indentLevel >= 0 )
continue;
// Closing } is found.
elseTag = closing->next();
break;
}
elseTag = Tokenizer::findClosing( tok->tokAt( 4 ) );
if( elseTag )
elseTag = elseTag->next();
}
else
{
@ -1089,6 +1101,7 @@ bool Tokenizer::removeReduntantConditions()
if( tok->tokAt( 2 )->str() == "true" )
boolValue = true;
// Handle if with else
if( elseTag && TOKEN::Match( elseTag, "else" ) )
{
if( TOKEN::Match( elseTag->next(), "if" ) )
@ -1097,7 +1110,7 @@ bool Tokenizer::removeReduntantConditions()
if( boolValue == false )
{
// Convert "if( false ) {aaa;} else if() {bbb;}" => "if() {bbb;}"
TOKEN::eraseTokens( tok, elseTag->tokAt( 1 ) );
TOKEN::eraseTokens( tok, elseTag->tokAt( 2 ) );
ret = true;
}
else
@ -1123,15 +1136,58 @@ bool Tokenizer::removeReduntantConditions()
}
else
{
// Convert "if( true ) {aaa;} else {bbb;}" => "{aaa;}"
if( TOKEN::Match( elseTag->tokAt( 1 ), "{" ) )
{
// Convert "if( true ) {aaa;} else {bbb;}" => "{aaa;}"
const TOKEN *end = Tokenizer::findClosing( elseTag->tokAt( 1 ) );
if( !end )
{
// Possibly syntax error in code
return false;
}
// TODO, implement
// Remove the "else { aaa; }"
TOKEN::eraseTokens( elseTag->previous(), end->tokAt( 1 ) );
}
else
{
// Convert "if( true ) {aaa;} else bbb;" => "{aaa;}"
// Find the closing ";"
const TOKEN *end = 0;
for ( const TOKEN *closing = elseTag->tokAt( 1 ); closing; closing = closing->next() )
{
if( TOKEN::Match( closing, ";" ) )
{
end = closing;
break;
}
}
if( !end )
{
// Possibly syntax error
return false;
}
TOKEN::eraseTokens( elseTag->previous(), end->next() );
}
// Remove "if( true )"
if( tok->previous() )
tok = tok->previous();
else
tok->setstr( ";" );
TOKEN::eraseTokens( tok, tok->tokAt(5) );
ret = true;
}
}
}
// Handle if without else
else
{
// Handle if without else
if( boolValue == false )
{
// Remove if and its content

View File

@ -91,6 +91,13 @@ private:
struct DefineSymbol *next;
};
/**
* Finds matching "}" for "{".
* @param tok The "{"
* @return The "}" that matches given parameter or 0 if not found.
*/
static const TOKEN *findClosing( const TOKEN *tok );
void Define(const char Name[], const char Value[]);
void addtoken(const char str[], const unsigned int lineno, const unsigned int fileno);