Memory leak: The "do" must be handled differently. Made a first fix for it

This commit is contained in:
Daniel Marjamäki 2008-11-30 19:00:07 +00:00
parent 5326a35951
commit a7ece61734
2 changed files with 12 additions and 54 deletions

View File

@ -546,58 +546,6 @@ void CheckMemoryLeakClass::erase(TOKEN *begin, const TOKEN *end)
*/
void CheckMemoryLeakClass::simplifycode(TOKEN *tok)
{
// Remove "do"...
// do { x } while (y);
// =>
// { x } while(y) { x }"
for ( TOKEN *tok2 = tok; tok2; tok2 = tok2->next )
{
if ( ! TOKEN::Match(tok2->next, "do") )
continue;
// Remove the next token "do"
erase( tok2, tok2->tokAt(2) );
tok2 = tok2->next;
// Find the end of the "do" block..
TOKEN *tok2_;
int indentlevel = 0;
for ( tok2_ = tok2; tok2_ && indentlevel>=0; tok2_ = tok2_->next )
{
if ( tok2_->str() == "{" )
++indentlevel;
else if ( tok2_->str() == "}" )
--indentlevel;
else if ( indentlevel == 0 && (tok2_->next->str() == ";") )
break;
}
// End not found?
if ( ! tok2_ )
continue;
// Copy code..
indentlevel = 0;
do
{
if ( tok2->str() == "{" )
++indentlevel;
else if ( tok2->str() == "}" )
--indentlevel;
// Copy token..
instoken( tok2_, tok2->aaaa() );
// Next token..
tok2 = tok2->next;
tok2_ = tok2_->next;
}
while ( tok2 && indentlevel > 0 );
}
// reduce the code..
bool done = false;
while ( ! done )
@ -705,6 +653,14 @@ void CheckMemoryLeakClass::simplifycode(TOKEN *tok)
done = false;
}
// Reduce "do { alloc ; } " => "alloc ;"
if ( TOKEN::Match(tok2->next, "do { alloc ; }") )
{
erase(tok2, tok2->tokAt(3));
erase(tok2->next, tok2->tokAt(3));
done = false;
}
// Replace "loop ;" with ";"
if ( TOKEN::Match(tok2->next, "loop ;") )
{

View File

@ -608,7 +608,9 @@ private:
" while (!str);\n"
" return str;\n"
"}\n" );
ASSERT_EQUALS( std::string("[test.cpp:5]: Memory leak: str\n"), errout.str() );
std::string err( errout.str() );
std::cout << err;
ASSERT_EQUALS( std::string("[test.cpp:5]: Memory leak: str\n"), err );
}