Fix ticket #204 (false positive::memory leak with --all when free is guarded by simple if)

http://apps.sourceforge.net/trac/cppcheck/ticket/204
This commit is contained in:
Reijo Tomperi 2009-05-01 21:31:07 +03:00
parent bc4fb21325
commit 26c193f9bc
2 changed files with 62 additions and 1 deletions

View File

@ -1979,6 +1979,7 @@ bool Tokenizer::simplifyIfNot()
bool Tokenizer::simplifyKnownVariables() bool Tokenizer::simplifyKnownVariables()
{ {
createLinks();
bool ret = false; bool ret = false;
for (Token *tok = _tokens; tok; tok = tok->next()) for (Token *tok = _tokens; tok; tok = tok->next())
{ {
@ -2013,7 +2014,9 @@ bool Tokenizer::simplifyKnownVariables()
for (Token *tok3 = tok2->next(); tok3; tok3 = tok3->next()) for (Token *tok3 = tok2->next(); tok3; tok3 = tok3->next())
{ {
// Perhaps it's a loop => bail out // Perhaps it's a loop => bail out
if (Token::Match(tok3, "[{}]")) if (tok3->str() == "{" && Token::Match(tok3->previous(), ")"))
break;
else if (tok3->str() == "}" && Token::Match(tok3->link()->previous(), ")"))
break; break;
// Variable is used somehow in a non-defined pattern => bail out // Variable is used somehow in a non-defined pattern => bail out

View File

@ -90,6 +90,7 @@ private:
TEST_CASE(simplifyKnownVariables7); TEST_CASE(simplifyKnownVariables7);
TEST_CASE(simplifyKnownVariables8); TEST_CASE(simplifyKnownVariables8);
TEST_CASE(simplifyKnownVariables9); TEST_CASE(simplifyKnownVariables9);
TEST_CASE(simplifyKnownVariables10);
TEST_CASE(multiCompare); TEST_CASE(multiCompare);
@ -634,7 +635,64 @@ private:
ASSERT_EQUALS(std::string(" void foo ( ) { int a ; a = 1 ; int b ; b = 2 ; if ( 1 < 2 ) ; }"), ostr.str()); ASSERT_EQUALS(std::string(" void foo ( ) { int a ; a = 1 ; int b ; b = 2 ; if ( 1 < 2 ) ; }"), ostr.str());
} }
void simplifyKnownVariables10()
{
{
const char code[] = "void f()\n"
"{\n"
" bool b=false;\n"
"\n"
" {\n"
" b = true;\n"
" }\n"
"\n"
" if( b )\n"
" {\n"
" a();\n"
" }\n"
"}\n";
// tokenize..
OurTokenizer tokenizer;
std::istringstream istr(code);
tokenizer.tokenize(istr, "test.cpp");
tokenizer.setVarId();
tokenizer.simplifyKnownVariables();
std::ostringstream ostr;
for (const Token *tok = tokenizer.tokens(); tok; tok = tok->next())
ostr << " " << tok->str();
ASSERT_EQUALS(std::string(" void f ( ) { bool b ; b = false ; { b = true ; } if ( true ) { a ( ) ; } }"), ostr.str());
}
{
const char code[] = "void f()\n"
"{\n"
" bool b=false;\n"
" { b = false; }\n"
" {\n"
" b = true;\n"
" }\n"
"\n"
" if( b )\n"
" {\n"
" a();\n"
" }\n"
"}\n";
// tokenize..
OurTokenizer tokenizer;
std::istringstream istr(code);
tokenizer.tokenize(istr, "test.cpp");
tokenizer.setVarId();
tokenizer.simplifyKnownVariables();
std::ostringstream ostr;
for (const Token *tok = tokenizer.tokens(); tok; tok = tok->next())
ostr << " " << tok->str();
ASSERT_EQUALS(std::string(" void f ( ) { bool b ; b = false ; { b = false ; } { b = true ; } if ( true ) { a ( ) ; } }"), ostr.str());
}
}
void multiCompare() void multiCompare()
{ {