Tokenizer: execute the simplifyIfNot and simplifyIfNotNull in the normal token list. ticket #2642

This commit is contained in:
Daniel Marjamäki 2011-04-02 21:21:05 +02:00
parent 9182b8be19
commit ed8cdddcf5
2 changed files with 12 additions and 6 deletions

View File

@ -2399,6 +2399,10 @@ bool Tokenizer::tokenize(std::istream &code,
// Use "<" comparison instead of ">" // Use "<" comparison instead of ">"
simplifyComparisonOrder(); simplifyComparisonOrder();
// Simplify '(p == 0)' to '(!p)'
simplifyIfNot();
simplifyIfNotNull();
/** /**
* @todo simplify "for" * @todo simplify "for"
* - move out start-statement "for (a;b;c);" => "{ a; for(;b;c); }" * - move out start-statement "for (a;b;c);" => "{ a; for(;b;c); }"
@ -6162,17 +6166,19 @@ void Tokenizer::simplifyIfNot()
} }
else if (tok->link() && Token::Match(tok, ") == 0|false")) else if (tok->link() && Token::Match(tok, ") == 0|false"))
{
Token::eraseTokens(tok, tok->tokAt(3));
if (Token::Match(tok->link()->previous(), "%var%"))
{ {
// if( foo(x) == 0 ) // if( foo(x) == 0 )
if (Token::Match(tok->link()->tokAt(-2), "( %var%"))
{
Token::eraseTokens(tok, tok->tokAt(3));
tok->link()->previous()->insertToken(tok->link()->previous()->str().c_str()); tok->link()->previous()->insertToken(tok->link()->previous()->str().c_str());
tok->link()->previous()->previous()->str("!"); tok->link()->previous()->previous()->str("!");
} }
else
{
// if( (x) == 0 ) // if( (x) == 0 )
else if (Token::simpleMatch(tok->link()->previous(), "("))
{
Token::eraseTokens(tok, tok->tokAt(3));
tok->link()->insertToken("("); tok->link()->insertToken("(");
tok->link()->str("!"); tok->link()->str("!");
Token *temp = tok->link(); Token *temp = tok->link();

View File

@ -1498,7 +1498,7 @@ private:
"{" "{"
" int i ;" " int i ;"
" for ( i = 0 ; i < 10 ; ++ i ) {" " for ( i = 0 ; i < 10 ; ++ i ) {"
" if ( * str == 0 ) { goto label ; }" " if ( ! * str ) { goto label ; }"
" }" " }"
" return ;" " return ;"
" label : ;" " label : ;"