Fixed ticket #500 (Tokenizer: simplify the "(p != NULL)" conditions)

Also teach simplifyIfNot() to handle variables like Foo::var.

https://sourceforge.net/apps/trac/cppcheck/ticket/500
This commit is contained in:
Slava Semushin 2009-07-25 18:11:29 +07:00
parent 88d7c598e3
commit 8643936e0c
5 changed files with 122 additions and 20 deletions

View File

@ -728,9 +728,7 @@ Token *CheckMemoryLeakInFunction::getcode(const Token *tok, std::list<const Toke
// if else switch
if (tok->str() == "if")
{
if (Token::simpleMatch(tok, std::string("if ( " + varnameStr + " )").c_str()) ||
Token::simpleMatch(tok, std::string("if ( " + varnameStr + " != 0 )").c_str()) ||
Token::simpleMatch(tok, std::string("if ( 0 != " + varnameStr + " )").c_str()))
if (Token::simpleMatch(tok, std::string("if ( " + varnameStr + " )").c_str()))
{
addtoken("if(var)");

View File

@ -86,23 +86,12 @@ void CheckOther::warningRedundantCode()
/*
* Possible if-constructions:
*
* if (0 != var)
* if (0 != this->var)
* if (0 != Foo::var)
* if (var)
* if (this->var)
* if (Foo::var)
* if (var != 0)
* if (this->var != 0)
* if (Foo::var != 0)
*
**/
if (Token::simpleMatch(tok2, "0 !="))
{
tok2 = tok2->tokAt(2);
}
if (Token::simpleMatch(tok2, "this .") ||
Token::Match(tok2, "%var% ::"))
{
@ -115,11 +104,6 @@ void CheckOther::warningRedundantCode()
tok2 = tok2->next();
}
if (Token::simpleMatch(tok2, "!= 0"))
{
tok2 = tok2->tokAt(2);
}
if (tok2->str() == ")")
{
tok2 = tok2->next();

View File

@ -1478,6 +1478,7 @@ void Tokenizer::simplifyTokenList()
elseif();
simplifyIfNot();
simplifyIfNotNull();
simplifyNot();
simplifyIfAssign();
@ -2514,7 +2515,7 @@ bool Tokenizer::simplifyIfNot()
ret = true;
}
else if (Token::Match(tok, "%var% . %var% == 0"))
else if (Token::Match(tok, "%var% .|:: %var% == 0"))
{
tok = tok->previous();
tok->insertToken("!");
@ -2538,6 +2539,53 @@ bool Tokenizer::simplifyIfNot()
}
bool Tokenizer::simplifyIfNotNull()
{
// Make sure we have working links
createLinks();
bool ret = false;
for (Token *tok = _tokens; tok; tok = tok->next())
{
Token *deleteFrom = NULL;
if (tok->str() == "(" || tok->str() == "||" || tok->str() == "&&")
{
tok = tok->next();
if (Token::simpleMatch(tok, "0 != (") ||
Token::Match(tok, "0 != %var%"))
{
deleteFrom = tok->previous();
}
else if (Token::Match(tok, "%var% != 0"))
{
deleteFrom = tok;
}
else if (Token::Match(tok, "%var% .|:: %var% != 0"))
{
tok = tok->tokAt(2);
deleteFrom = tok;
}
}
else if (tok->link() && Token::simpleMatch(tok, ") != 0"))
{
deleteFrom = tok;
}
if (deleteFrom) {
deleteFrom->deleteNext();
deleteFrom->deleteNext();
ret = true;
}
}
return ret;
}
bool Tokenizer::simplifyNot()
{
// "if (not p)" => "if (!p)"

View File

@ -148,6 +148,14 @@ public:
*/
bool simplifyIfNot();
/**
* simplify if-not NULL..
* Example: "if(0!=x);" => "if(x);"
* @return true if something is modified
* false if nothing is done.
*/
bool simplifyIfNotNull();
/**
* simplify the "not" keyword to "!"
* Example: "if (not p)" => "if (!p)"

View File

@ -85,6 +85,7 @@ private:
TEST_CASE(whileAddBraces);
TEST_CASE(numeric_true_condition);
TEST_CASE(pointers_condition);
TEST_CASE(simplifyKnownVariables1);
TEST_CASE(simplifyKnownVariables2);
@ -357,6 +358,69 @@ private:
"}", tokenizeAndStringify(code, true));
}
void pointers_condition()
{
const char code[] = "void f()\n"
"{\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 (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"
"}\n";
ASSERT_EQUALS("void f ( )\n"
"{\n"
"if ( p ) { ; }\n"
"if ( p ) { ; }\n"
"if ( this . p ) { ; }\n"
"if ( this . p ) { ; }\n"
"if ( Foo :: p ) { ; }\n"
"if ( Foo :: p ) { ; }\n"
"while ( p ) { ; }\n"
"while ( p ) { ; }\n"
"while ( this . p ) { ; }\n"
"while ( this . p ) { ; }\n"
"while ( Foo :: p ) { ; }\n"
"while ( Foo :: p ) { ; }\n"
"if ( ! p ) { ; }\n"
"if ( ! p ) { ; }\n"
"if ( ! this . p ) { ; }\n"
"if ( ! this . p ) { ; }\n"
"if ( ! Foo :: p ) { ; }\n"
"if ( ! Foo :: p ) { ; }\n"
"while ( ! p ) { ; }\n"
"while ( ! p ) { ; }\n"
"while ( ! this . p ) { ; }\n"
"while ( ! this . p ) { ; }\n"
"while ( ! Foo :: p ) { ; }\n"
"while ( ! Foo :: p ) { ; }\n"
"if ( p1 || ! p2 ) { ; }\n"
"if ( p1 && ! p2 ) { ; }\n"
"}", tokenizeAndStringify(code, true));
}
void ifAddBraces1()
{
const char code[] = "void f()\n"