Refactoring, simplifyConditions(), simplifyCasts() and simplifyFunctionReturn() now return the opposite of their boolean return value.

This commit is contained in:
Reijo Tomperi 2008-12-13 18:54:48 +00:00
parent 1e9863d671
commit 02d6e367e7
2 changed files with 32 additions and 16 deletions

View File

@ -1016,9 +1016,14 @@ void Tokenizer::simplifyTokenList()
while ( ! done ) while ( ! done )
{ {
done = true; done = true;
done &= simplifyConditions(); if( simplifyConditions() )
done &= simplifyCasts(); done = false;
done &= simplifyFunctionReturn();
if( simplifyCasts() )
done = false;
if( simplifyFunctionReturn() )
done = false;
} }
} }
//--------------------------------------------------------------------------- //---------------------------------------------------------------------------
@ -1026,7 +1031,7 @@ void Tokenizer::simplifyTokenList()
bool Tokenizer::simplifyConditions() bool Tokenizer::simplifyConditions()
{ {
bool ret = true; bool ret = false;
for ( TOKEN *tok = _tokens; tok; tok = tok->next() ) for ( TOKEN *tok = _tokens; tok; tok = tok->next() )
{ {
@ -1034,14 +1039,14 @@ bool Tokenizer::simplifyConditions()
{ {
tok->deleteNext(); tok->deleteNext();
tok->deleteNext(); tok->deleteNext();
ret = false; ret = true;
} }
else if (TOKEN::Match(tok, "( false ||") || TOKEN::Match(tok, "|| false ||") || TOKEN::Match(tok->next(), "|| false )")) else if (TOKEN::Match(tok, "( false ||") || TOKEN::Match(tok, "|| false ||") || TOKEN::Match(tok->next(), "|| false )"))
{ {
tok->deleteNext(); tok->deleteNext();
tok->deleteNext(); tok->deleteNext();
ret = false; ret = true;
} }
// Change numeric constant in condition to "true" or "false" // Change numeric constant in condition to "true" or "false"
@ -1051,7 +1056,7 @@ bool Tokenizer::simplifyConditions()
(TOKEN::Match(tok2, ")") || TOKEN::Match(tok2, "&&") || TOKEN::Match(tok2, "||")) ) (TOKEN::Match(tok2, ")") || TOKEN::Match(tok2, "&&") || TOKEN::Match(tok2, "||")) )
{ {
tok->next()->setstr((tok->next()->str() != "0") ? "true" : "false"); tok->next()->setstr((tok->next()->str() != "0") ? "true" : "false");
ret = false; ret = true;
} }
// Reduce "(%num% == %num%)" => "(true)"/"(false)" // Reduce "(%num% == %num%)" => "(true)"/"(false)"
@ -1086,7 +1091,7 @@ bool Tokenizer::simplifyConditions()
tok->deleteNext(); tok->deleteNext();
tok->setstr( result ? "true" : "false" ); tok->setstr( result ? "true" : "false" );
ret = false; ret = true;
} }
} }
} }
@ -1097,7 +1102,7 @@ bool Tokenizer::simplifyConditions()
bool Tokenizer::simplifyCasts() bool Tokenizer::simplifyCasts()
{ {
bool ret = true; bool ret = false;
for ( TOKEN *tok = _tokens; tok; tok = tok->next() ) for ( TOKEN *tok = _tokens; tok; tok = tok->next() )
{ {
if ( TOKEN::Match(tok->next(), "( %type% * )") ) if ( TOKEN::Match(tok->next(), "( %type% * )") )
@ -1106,7 +1111,7 @@ bool Tokenizer::simplifyCasts()
tok->deleteNext(); tok->deleteNext();
tok->deleteNext(); tok->deleteNext();
tok->deleteNext(); tok->deleteNext();
ret = false; ret = true;
} }
else if ( TOKEN::Match(tok->next(), "dynamic_cast|reinterpret_cast|const_cast <" ) ) else if ( TOKEN::Match(tok->next(), "dynamic_cast|reinterpret_cast|const_cast <" ) )
@ -1127,6 +1132,8 @@ bool Tokenizer::simplifyCasts()
} }
if (tok2->next()) if (tok2->next())
tok2->deleteNext(); tok2->deleteNext();
ret = true;
} }
} }
@ -1137,7 +1144,7 @@ bool Tokenizer::simplifyCasts()
bool Tokenizer::simplifyFunctionReturn() bool Tokenizer::simplifyFunctionReturn()
{ {
bool ret = true; bool ret = false;
int indentlevel = 0; int indentlevel = 0;
for ( const TOKEN *tok = tokens(); tok; tok = tok->next() ) for ( const TOKEN *tok = tokens(); tok; tok = tok->next() )
{ {
@ -1159,7 +1166,7 @@ bool Tokenizer::simplifyFunctionReturn()
tok2->setstr( tok->strAt(5) ); tok2->setstr( tok->strAt(5) );
tok2->deleteNext(); tok2->deleteNext();
tok2->deleteNext(); tok2->deleteNext();
ret = false; ret = true;
} }
} }
} }

View File

@ -95,13 +95,22 @@ private:
void addtoken(const char str[], const unsigned int lineno, const unsigned int fileno); void addtoken(const char str[], const unsigned int lineno, const unsigned int fileno);
/** Simplify conditions */ /** Simplify conditions
* @return true if something is modified
* false if nothing is done.
*/
bool simplifyConditions(); bool simplifyConditions();
/** Simplify casts */ /** Simplify casts
* @return true if something is modified
* false if nothing is done.
*/
bool simplifyCasts(); bool simplifyCasts();
/** Simplify function calls - constant return value */ /** Simplify function calls - constant return value
* @return true if something is modified
* false if nothing is done.
*/
bool simplifyFunctionReturn(); bool simplifyFunctionReturn();
TOKEN *_gettok(TOKEN *tok, int index); TOKEN *_gettok(TOKEN *tok, int index);