Fix #778 (Tokenizer: impove simplification inside if())

http://sourceforge.net/apps/trac/cppcheck/ticket/778
This commit is contained in:
Reijo Tomperi 2009-10-05 14:10:09 +03:00
parent 1df8f38423
commit fc684d7b5a
2 changed files with 16 additions and 1 deletions

View File

@ -1862,6 +1862,7 @@ void Tokenizer::simplifyTokenList()
simplifyFunctionParameters();
elseif();
simplifyRedundantParanthesis();
simplifyIfNot();
simplifyIfNotNull();
simplifyIfAssign();
@ -3066,6 +3067,14 @@ void Tokenizer::simplifyIfNot()
tok = tok->tokAt(4);
Token::eraseTokens(tok, tok->tokAt(3));
}
else if (Token::Match(tok, "* %var% == 0"))
{
tok = tok->previous();
tok->insertToken("!");
tok = tok->tokAt(3);
Token::eraseTokens(tok, tok->tokAt(3));
}
}
else if (tok->link() && Token::simpleMatch(tok, ") == 0"))
@ -3082,7 +3091,9 @@ void Tokenizer::simplifyIfNot()
// if( (x) == 0 )
tok->link()->insertToken("(");
tok->link()->str("!");
Token *temp = tok->link();
Token::createMutualLinks(tok->link()->next(), tok);
temp->link(0);
}
}
}
@ -3397,7 +3408,7 @@ bool Tokenizer::simplifyRedundantParanthesis()
ret = true;
}
if (Token::Match(tok->previous(), "( ( %var% )") && tok->next()->varId() != 0)
if (Token::Match(tok->previous(), "[(!*] ( %var% )") && tok->next()->varId() != 0)
{
// We have "( var )", remove the paranthesis
tok = tok->previous();

View File

@ -415,11 +415,15 @@ private:
ASSERT_EQUALS("void f ( ) { int p ; if ( -1 == p ) { } }", tok("void f(){int p; if(-1==(p)){}}"));
ASSERT_EQUALS("void f ( ) { int p ; if ( p ) { } }", tok("void f(){int p; if((p)){}}"));
ASSERT_EQUALS("return p ;", tok("return (p);"));
ASSERT_EQUALS("void f ( ) { int * p ; if ( ! * p ) { } }", tok("void f(){int *p; if (*(p) == 0) {}}"));
ASSERT_EQUALS("void f ( ) { int * p ; if ( ! * p ) { } }", tok("void f(){int *p; if (*p == 0) {}}"));
// keep parantheses..
ASSERT_EQUALS("= a ;", tok("= (char)a;"));
ASSERT_EQUALS("cast < char * > ( p )", tok("cast<char *>(p)"));
ASSERT_EQUALS("return ( a + b ) * c ;", tok("return (a+b)*c;"));
ASSERT_EQUALS("void f ( ) { int p ; if ( 2 * p == 0 ) { } }", tok("void f(){int p; if (2*p == 0) {}}"));
ASSERT_EQUALS("void f ( ) { DIR * f ; f = opendir ( dirname ) ; if ( closedir ( f ) ) { } }", tok("void f(){DIR * f = opendir(dirname);if (closedir(f)){}}"));
}
void declareVar()