From 2ccc01f5ed347454e7eb766116469959e56ae521 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Sat, 18 Jul 2009 10:18:46 +0200 Subject: [PATCH] tokenizer: improved the Tokenizer::simplifyIfNot --- src/tokenize.cpp | 43 ++++++++++++++++++++++++------------- test/testsimplifytokens.cpp | 2 ++ 2 files changed, 30 insertions(+), 15 deletions(-) diff --git a/src/tokenize.cpp b/src/tokenize.cpp index 7ba1652cd..17174e2e9 100644 --- a/src/tokenize.cpp +++ b/src/tokenize.cpp @@ -2434,24 +2434,37 @@ bool Tokenizer::simplifyIfNot() bool ret = false; for (Token *tok = _tokens; tok; tok = tok->next()) { - - if (Token::simpleMatch(tok, "0 == (") || - Token::Match(tok, "0 == %var%")) + if (tok->str() == "(" || tok->str() == "||" || tok->str() == "&&") { - tok->deleteNext(); - tok->str("!"); - ret = true; + tok = tok->next(); + if (Token::simpleMatch(tok, "0 == (") || + Token::Match(tok, "0 == %var%")) + { + tok->deleteNext(); + tok->str("!"); + ret = true; + } + + else if (Token::Match(tok, "%var% == 0")) + { + tok->deleteNext(); + tok->next()->str(tok->str()); + tok->str("!"); + ret = true; + } + + else if (Token::Match(tok, "%var% . %var% == 0")) + { + tok = tok->previous(); + tok->insertToken("!"); + tok = tok->tokAt(4); + tok->deleteNext(); + tok->deleteNext(); + ret = true; + } } - if (Token::Match(tok, "%var% == 0")) - { - tok->deleteNext(); - tok->next()->str(tok->str()); - tok->str("!"); - ret = true; - } - - if (tok->link() && Token::simpleMatch(tok, ") == 0")) + else if (tok->link() && Token::simpleMatch(tok, ") == 0")) { tok->deleteNext(); tok->deleteNext(); diff --git a/test/testsimplifytokens.cpp b/test/testsimplifytokens.cpp index aaa7015df..f836a2aa1 100644 --- a/test/testsimplifytokens.cpp +++ b/test/testsimplifytokens.cpp @@ -971,11 +971,13 @@ private: void ifnot() { ASSERT_EQUALS("if ( ! x )", simplifyIfNot("if(0==x)")); + ASSERT_EQUALS("if ( ! x )", simplifyIfNot("if(x==0)")); ASSERT_EQUALS("if ( ! ( a = b ) )", simplifyIfNot("if(0==(a=b))")); ASSERT_EQUALS("if ( ! x )", simplifyIfNot("if(x==0)")); ASSERT_EQUALS("if ( ! a && b ( ) )", simplifyIfNot("if( 0 == a && b() )")); ASSERT_EQUALS("if ( b ( ) && ! a )", simplifyIfNot("if( b() && 0 == a )")); ASSERT_EQUALS("if ( ! ( a = b ) )", simplifyIfNot("if((a=b)==0)")); + ASSERT_EQUALS("if ( ! x . y )", simplifyIfNot("if(x.y==0)")); }