Fixed ticket #392 (false positive and wrong allocation and deallocation not detected)

http://sourceforge.net/apps/trac/cppcheck/ticket/392
This commit is contained in:
Slava Semushin 2009-06-12 21:14:01 +07:00
parent 56a0660972
commit cba0d9e130
3 changed files with 64 additions and 0 deletions

View File

@ -2579,6 +2579,24 @@ bool Tokenizer::simplifyRedundantParanthesis()
ret = true; ret = true;
} }
while (Token::Match(tok->previous(), "[;{] ( delete %var% ) ;"))
{
// We have "( delete var )", remove the outer
// paranthesis
tok->tokAt(3)->deleteThis();
tok->deleteThis();
ret = true;
}
while (Token::Match(tok->previous(), "[;{] ( delete [ ] %var% ) ;"))
{
// We have "( delete [] var )", remove the outer
// paranthesis
tok->tokAt(5)->deleteThis();
tok->deleteThis();
ret = true;
}
if (Token::Match(tok, "( ( %bool% )") || if (Token::Match(tok, "( ( %bool% )") ||
Token::Match(tok, "( ( %num% )")) Token::Match(tok, "( ( %num% )"))
{ {

View File

@ -212,6 +212,8 @@ private:
* Remove redundant paranthesis: * Remove redundant paranthesis:
* - "((x))" => "(x)" * - "((x))" => "(x)"
* - "(function())" => "function()" * - "(function())" => "function()"
* - "(delete x)" => "delete x"
* - "(delete [] x)" => "delete [] x"
* @return true if modifications to token-list are done. * @return true if modifications to token-list are done.
* false if no modifications are done. * false if no modifications are done.
*/ */

View File

@ -131,6 +131,7 @@ private:
TEST_CASE(removeParantheses2); TEST_CASE(removeParantheses2);
TEST_CASE(removeParantheses3); TEST_CASE(removeParantheses3);
TEST_CASE(removeParantheses4); // Ticket #390 TEST_CASE(removeParantheses4); // Ticket #390
TEST_CASE(removeParantheses5); // Ticket #392
TEST_CASE(simplify_numeric_condition); TEST_CASE(simplify_numeric_condition);
TEST_CASE(tokenize_double); TEST_CASE(tokenize_double);
@ -1748,6 +1749,49 @@ private:
ASSERT_EQUALS(" void foo ( ) { free ( p ) ; }", ostr.str()); ASSERT_EQUALS(" void foo ( ) { free ( p ) ; }", ostr.str());
} }
void removeParantheses5()
{
// Simplify "( delete x )" into "delete x"
{
const char code[] = "void foo()\n"
"{\n"
" (delete p);\n"
"}";
// tokenize..
Tokenizer tokenizer;
std::istringstream istr(code);
tokenizer.tokenize(istr, "test.cpp");
tokenizer.simplifyTokenList();
std::ostringstream ostr;
for (const Token *tok = tokenizer.tokens(); tok; tok = tok->next())
ostr << " " << tok->str();
ASSERT_EQUALS(" void foo ( ) { delete p ; }", ostr.str());
}
// Simplify "( delete [] x )" into "delete [] x"
{
const char code[] = "void foo()\n"
"{\n"
" (delete [] p);\n"
"}";
// tokenize..
Tokenizer tokenizer;
std::istringstream istr(code);
tokenizer.tokenize(istr, "test.cpp");
tokenizer.simplifyTokenList();
std::ostringstream ostr;
for (const Token *tok = tokenizer.tokens(); tok; tok = tok->next())
ostr << " " << tok->str();
ASSERT_EQUALS(" void foo ( ) { delete [ ] p ; }", ostr.str());
}
}
void simplify_numeric_condition() void simplify_numeric_condition()
{ {
{ {