Tokenizer: Simplify 'false ==' to '!'

This commit is contained in:
Daniel Marjamäki 2010-03-24 19:55:02 +01:00
parent e95bc41e59
commit 80edb4d8f4
3 changed files with 39 additions and 75 deletions

View File

@ -4527,21 +4527,21 @@ void Tokenizer::simplifyIfNot()
if (!tok) if (!tok)
break; break;
if (Token::simpleMatch(tok, "0 == (") || if (Token::Match(tok, "0|false == (") ||
Token::Match(tok, "0 == %var%")) Token::Match(tok, "0|false == %var%"))
{ {
tok->deleteNext(); tok->deleteNext();
tok->str("!"); tok->str("!");
} }
else if (Token::Match(tok, "%var% == 0")) else if (Token::Match(tok, "%var% == 0|false"))
{ {
tok->deleteNext(); tok->deleteNext();
tok->next()->str(tok->str()); tok->next()->str(tok->str());
tok->str("!"); tok->str("!");
} }
else if (Token::Match(tok, "%var% .|:: %var% == 0")) else if (Token::Match(tok, "%var% .|:: %var% == 0|false"))
{ {
tok = tok->previous(); tok = tok->previous();
tok->insertToken("!"); tok->insertToken("!");
@ -4549,7 +4549,7 @@ void Tokenizer::simplifyIfNot()
Token::eraseTokens(tok, tok->tokAt(3)); Token::eraseTokens(tok, tok->tokAt(3));
} }
else if (Token::Match(tok, "* %var% == 0")) else if (Token::Match(tok, "* %var% == 0|false"))
{ {
tok = tok->previous(); tok = tok->previous();
tok->insertToken("!"); tok->insertToken("!");
@ -4558,7 +4558,7 @@ void Tokenizer::simplifyIfNot()
} }
} }
else if (tok->link() && Token::simpleMatch(tok, ") == 0")) else if (tok->link() && Token::Match(tok, ") == 0|false"))
{ {
Token::eraseTokens(tok, tok->tokAt(3)); Token::eraseTokens(tok, tok->tokAt(3));
if (Token::Match(tok->link()->previous(), "%var%")) if (Token::Match(tok->link()->previous(), "%var%"))

View File

@ -448,77 +448,41 @@ private:
void pointers_condition() void pointers_condition()
{ {
const char code[] = "void f()\n" ASSERT_EQUALS("( p )", tokenizeAndStringify("( p != NULL )", true));
"{\n" ASSERT_EQUALS("( p )", tokenizeAndStringify("( NULL != p )", true));
" if (p != NULL);\n" ASSERT_EQUALS("( this . p )", tokenizeAndStringify("( this->p != NULL )", true));
" if (NULL != p);\n" ASSERT_EQUALS("( this . p )", tokenizeAndStringify("( NULL != this->p )", true));
" if (this->p != NULL);\n" ASSERT_EQUALS("( Foo :: p )", tokenizeAndStringify("( Foo::p != NULL )", true));
" if (NULL != this->p);\n" ASSERT_EQUALS("( Foo :: p )", tokenizeAndStringify("( NULL != Foo::p )", true));
" if (Foo::p != NULL);\n"
" if (NULL != Foo::p);\n"
" while (p != NULL);\n"
" while (NULL != p);\n"
" while (this->p != NULL);\n"
" while (NULL != this->p);\n"
" while (Foo::p != NULL);\n"
" while (NULL != Foo::p);\n"
" if (p == NULL);\n"
" if (NULL == p);\n"
" if (this->p == NULL);\n"
" if (NULL == this->p);\n"
" if (Foo::p == NULL);\n"
" if (NULL == Foo::p);\n"
" while (p == NULL);\n"
" while (NULL == p);\n"
" while (this->p == NULL);\n"
" while (NULL == this->p);\n"
" while (Foo::p == NULL);\n"
" while (NULL == Foo::p);\n"
" if (p1 != NULL || p2 == NULL) { ; }\n"
" if (p1 != NULL && p2 == NULL) { ; }\n"
" if (p == '\\0');\n"
" if (p == 0L);\n"
" if (p == 0UL);\n"
" if (p == 0ul);\n"
" if (p == 0l);\n"
"}\n";
ASSERT_EQUALS("void f ( )\n" ASSERT_EQUALS("( ! p )", tokenizeAndStringify("( p == NULL )", true));
"{\n" ASSERT_EQUALS("( ! p )", tokenizeAndStringify("( NULL == p )", true));
"if ( p ) { ; }\n" ASSERT_EQUALS("( ! this . p )", tokenizeAndStringify("( this->p == NULL )", true));
"if ( p ) { ; }\n" ASSERT_EQUALS("( ! this . p )", tokenizeAndStringify("( NULL == this->p )", true));
"if ( this . p ) { ; }\n" ASSERT_EQUALS("( ! Foo :: p )", tokenizeAndStringify("( Foo::p == NULL )", true));
"if ( this . p ) { ; }\n" ASSERT_EQUALS("( ! Foo :: p )", tokenizeAndStringify("( NULL == Foo::p )", true));
"if ( Foo :: p ) { ; }\n"
"if ( Foo :: p ) { ; }\n" ASSERT_EQUALS("( p1 || ! p2 )", tokenizeAndStringify("( p1 != NULL || p2 == NULL )", true));
"while ( p ) { ; }\n" ASSERT_EQUALS("( p1 && ! p2 )", tokenizeAndStringify("( p1 != NULL && p2 == NULL )", true));
"while ( p ) { ; }\n"
"while ( this . p ) { ; }\n" ASSERT_EQUALS("( ! p )", tokenizeAndStringify("( p == false )", true));
"while ( this . p ) { ; }\n" ASSERT_EQUALS("( ! p )", tokenizeAndStringify("( p == 0 )", true));
"while ( Foo :: p ) { ; }\n" ASSERT_EQUALS("( ! p )", tokenizeAndStringify("( p == '\\0' )", true));
"while ( Foo :: p ) { ; }\n" ASSERT_EQUALS("( ! p )", tokenizeAndStringify("( p == 0L )", true));
"if ( ! p ) { ; }\n" ASSERT_EQUALS("( ! p )", tokenizeAndStringify("( p == 0UL )", true));
"if ( ! p ) { ; }\n" ASSERT_EQUALS("( ! p )", tokenizeAndStringify("( p == 0ul )", true));
"if ( ! this . p ) { ; }\n" ASSERT_EQUALS("( ! p )", tokenizeAndStringify("( p == 0l )", true));
"if ( ! this . p ) { ; }\n"
"if ( ! Foo :: p ) { ; }\n" ASSERT_EQUALS("( ! p )", tokenizeAndStringify("( false == p )", true));
"if ( ! Foo :: p ) { ; }\n" ASSERT_EQUALS("( ! p )", tokenizeAndStringify("( 0 == p )", true));
"while ( ! p ) { ; }\n" ASSERT_EQUALS("( ! p )", tokenizeAndStringify("( '\\0' == p )", true));
"while ( ! p ) { ; }\n" ASSERT_EQUALS("( ! p )", tokenizeAndStringify("( 0L == p )", true));
"while ( ! this . p ) { ; }\n" ASSERT_EQUALS("( ! p )", tokenizeAndStringify("( 0UL == p )", true));
"while ( ! this . p ) { ; }\n" ASSERT_EQUALS("( ! p )", tokenizeAndStringify("( 0ul == p )", true));
"while ( ! Foo :: p ) { ; }\n" ASSERT_EQUALS("( ! p )", tokenizeAndStringify("( 0l == p )", true));
"while ( ! Foo :: p ) { ; }\n"
"if ( p1 || ! p2 ) { ; }\n"
"if ( p1 && ! p2 ) { ; }\n"
"if ( ! p ) { ; }\n"
"if ( ! p ) { ; }\n"
"if ( ! p ) { ; }\n"
"if ( ! p ) { ; }\n"
"if ( ! p ) { ; }\n"
"}", tokenizeAndStringify(code, true));
} }
void ifAddBraces1() void ifAddBraces1()
{ {
const char code[] = "void f()\n" const char code[] = "void f()\n"