Fix #1599 (false positive on if( (f = fopen("foo", "r")) == ((FILE*)NULL) ))
http://sourceforge.net/apps/trac/cppcheck/ticket/1599
This commit is contained in:
parent
4f0f980272
commit
4a33b226e6
|
@ -3190,16 +3190,6 @@ bool Tokenizer::simplifyTokenList()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Replace pointer casts of 0.. "(char *)0" => "0"
|
|
||||||
for (Token *tok = _tokens; tok; tok = tok->next())
|
|
||||||
{
|
|
||||||
if (Token::Match(tok->next(), "( %type% * ) 0") ||
|
|
||||||
Token::Match(tok->next(), "( %type% %type% * ) 0"))
|
|
||||||
{
|
|
||||||
Token::eraseTokens(tok, tok->next()->link()->next());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Change initialisation of variable to assignment
|
// Change initialisation of variable to assignment
|
||||||
simplifyInitVar();
|
simplifyInitVar();
|
||||||
|
|
||||||
|
@ -3947,6 +3937,19 @@ void Tokenizer::simplifyCasts()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Replace pointer casts of 0.. "(char *)0" => "0"
|
||||||
|
while (Token::Match(tok->next(), "( %type% * ) 0") ||
|
||||||
|
Token::Match(tok->next(), "( %type% %type% * ) 0"))
|
||||||
|
{
|
||||||
|
Token::eraseTokens(tok, tok->next()->link()->next());
|
||||||
|
if (tok->str() == ")" && tok->link()->previous())
|
||||||
|
{
|
||||||
|
// If there was another cast before this, go back
|
||||||
|
// there to check it also. e.g. "(char*)(char*)0"
|
||||||
|
tok = tok->link()->previous();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if (Token::Match(tok->next(), "dynamic_cast|reinterpret_cast|const_cast|static_cast <"))
|
if (Token::Match(tok->next(), "dynamic_cast|reinterpret_cast|const_cast|static_cast <"))
|
||||||
{
|
{
|
||||||
Token *tok2 = tok->next();
|
Token *tok2 = tok->next();
|
||||||
|
|
|
@ -105,6 +105,7 @@ private:
|
||||||
|
|
||||||
// Assignment in condition..
|
// Assignment in condition..
|
||||||
TEST_CASE(ifassign1);
|
TEST_CASE(ifassign1);
|
||||||
|
TEST_CASE(ifAssignWithCast);
|
||||||
TEST_CASE(whileAssign);
|
TEST_CASE(whileAssign);
|
||||||
|
|
||||||
// "if(0==x)" => "if(!x)"
|
// "if(0==x)" => "if(!x)"
|
||||||
|
@ -294,6 +295,7 @@ private:
|
||||||
ASSERT_EQUALS("if ( * a )", tok("if ((unsigned int)(unsigned char)*a)"));
|
ASSERT_EQUALS("if ( * a )", tok("if ((unsigned int)(unsigned char)*a)"));
|
||||||
ASSERT_EQUALS("class A { A operator * ( int ) ; } ;", tok("class A { A operator *(int); };"));
|
ASSERT_EQUALS("class A { A operator * ( int ) ; } ;", tok("class A { A operator *(int); };"));
|
||||||
ASSERT_EQUALS("class A { A operator * ( int ) const ; } ;", tok("class A { A operator *(int) const; };"));
|
ASSERT_EQUALS("class A { A operator * ( int ) const ; } ;", tok("class A { A operator *(int) const; };"));
|
||||||
|
ASSERT_EQUALS("if ( ! p )", tok("if (p == (char *)(char *)0)"));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -1861,6 +1863,27 @@ private:
|
||||||
TODO_ASSERT_EQUALS("void foo ( A a ) { a . c = b ( ) ; if ( 0 <= a . c ) { ; } }", tok("void foo(A a) {if((a.c=b())>=0);}"));
|
TODO_ASSERT_EQUALS("void foo ( A a ) { a . c = b ( ) ; if ( 0 <= a . c ) { ; } }", tok("void foo(A a) {if((a.c=b())>=0);}"));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void ifAssignWithCast()
|
||||||
|
{
|
||||||
|
const char *code = "void foo()\n"
|
||||||
|
"{\n"
|
||||||
|
"FILE *f;\n"
|
||||||
|
"if( (f = fopen(\"foo\", \"r\")) == ((FILE*)NULL) )\n"
|
||||||
|
"return(-1);\n"
|
||||||
|
"fclose(f);\n"
|
||||||
|
"}\n";
|
||||||
|
const char *exptected = "void foo ( ) "
|
||||||
|
"{ "
|
||||||
|
"FILE * f ; "
|
||||||
|
"f = fopen ( \"foo\" , \"r\" ) ; "
|
||||||
|
"if ( ! f ) "
|
||||||
|
"{ "
|
||||||
|
"return -1 ; "
|
||||||
|
"} "
|
||||||
|
"fclose ( f ) ; "
|
||||||
|
"}";
|
||||||
|
ASSERT_EQUALS(exptected, tok(code));
|
||||||
|
}
|
||||||
|
|
||||||
void whileAssign()
|
void whileAssign()
|
||||||
{
|
{
|
||||||
|
|
Loading…
Reference in New Issue