diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index 116084c26..4b24c1d19 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -2399,6 +2399,10 @@ bool Tokenizer::tokenize(std::istream &code, // Use "<" comparison instead of ">" simplifyComparisonOrder(); + // Simplify '(p == 0)' to '(!p)' + simplifyIfNot(); + simplifyIfNotNull(); + /** * @todo simplify "for" * - move out start-statement "for (a;b;c);" => "{ a; for(;b;c); }" @@ -6163,16 +6167,18 @@ void Tokenizer::simplifyIfNot() 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 (Token::Match(tok->link()->tokAt(-2), "( %var%")) { - // if( foo(x) == 0 ) + Token::eraseTokens(tok, tok->tokAt(3)); tok->link()->previous()->insertToken(tok->link()->previous()->str().c_str()); tok->link()->previous()->previous()->str("!"); } - else + + // if( (x) == 0 ) + else if (Token::simpleMatch(tok->link()->previous(), "(")) { - // if( (x) == 0 ) + Token::eraseTokens(tok, tok->tokAt(3)); tok->link()->insertToken("("); tok->link()->str("!"); Token *temp = tok->link(); diff --git a/test/testtokenize.cpp b/test/testtokenize.cpp index 3e3c839eb..0978075b7 100644 --- a/test/testtokenize.cpp +++ b/test/testtokenize.cpp @@ -1498,7 +1498,7 @@ private: "{" " int i ;" " for ( i = 0 ; i < 10 ; ++ i ) {" - " if ( * str == 0 ) { goto label ; }" + " if ( ! * str ) { goto label ; }" " }" " return ;" " label : ;"