CheckMemoryLeak: Improved the checking (handling loops)

This commit is contained in:
Daniel Marjamäki 2008-08-09 08:34:05 +00:00
parent 9607f3f4ce
commit ff0f8a7434
1 changed files with 11 additions and 3 deletions

View File

@ -124,8 +124,9 @@ static void CheckMemoryLeak_CheckScope( const TOKEN *Tok1, const char varname[]
AllocType Alloc = No; AllocType Alloc = No;
int alloc_indentlevel = 0; int alloc_indentlevel = -1;
int dealloc_indentlevel = 0; int dealloc_indentlevel = -1;
std::list<int> loop_indentlevel;
bool isif = false; bool isif = false;
@ -145,6 +146,9 @@ static void CheckMemoryLeak_CheckScope( const TOKEN *Tok1, const char varname[]
return; return;
} }
if ( !loop_indentlevel.empty() && indentlevel <= loop_indentlevel.back() )
loop_indentlevel.pop_back();
if ( indentlevel < alloc_indentlevel ) if ( indentlevel < alloc_indentlevel )
alloc_indentlevel = -1; alloc_indentlevel = -1;
@ -152,6 +156,10 @@ static void CheckMemoryLeak_CheckScope( const TOKEN *Tok1, const char varname[]
dealloc_indentlevel = -1; dealloc_indentlevel = -1;
} }
// for, while set loop level..
if ( alloc_indentlevel >= 0 && (Match(tok,"while") || Match(tok,"for")) )
loop_indentlevel.push_back( indentlevel );
// Skip stuff like: if (!var) ... // Skip stuff like: if (!var) ...
if ( Match(tok, "if ( ! %var1% )", varnames) || if ( Match(tok, "if ( ! %var1% )", varnames) ||
Match(tok, "if ( unlikely ( ! %var1% ) )", varnames) || Match(tok, "if ( unlikely ( ! %var1% ) )", varnames) ||
@ -269,7 +277,7 @@ static void CheckMemoryLeak_CheckScope( const TOKEN *Tok1, const char varname[]
// continue/break loop.. // continue/break loop..
if (Alloc != No && if (Alloc != No &&
alloc_indentlevel == indentlevel && loop_indentlevel.empty() &&
(Match(tok,"continue") || Match(tok,"break"))) (Match(tok,"continue") || Match(tok,"break")))
{ {
MemoryLeak( tok, varname ); MemoryLeak( tok, varname );