Make it possible to disable memory leak checking for a variable. Usable to avoid false positives

This commit is contained in:
Daniel Marjamäki 2008-04-11 18:37:15 +00:00
parent 214266c08c
commit 1d258a55a4
4 changed files with 51 additions and 1 deletions

View File

@ -155,6 +155,10 @@ static void CheckMemoryLeak_CheckScope( const TOKEN *Tok1, const char varname[]
{
Alloc = alloc;
alloc_indentlevel = indentlevel;
// Is there a comment such as "var is deleted automaticly"
if ( Alloc == New && isdeleted(tok->str) > 0 )
return;
}
}

View File

@ -585,6 +585,16 @@ static void memleak_in_function()
const char test13[] = "static char *f()\n"
"{\n"
" Fred *fred = new Fred;\n"
" // fred is deleted automaticly\n"
"}\n";
check( CheckMemoryLeak, __LINE__, test13, "" );
}
//---------------------------------------------------------------------------

View File

@ -287,8 +287,18 @@ void Tokenize(const char FileName[])
// Tokenize - tokenizes input stream
//---------------------------------------------------------------------------
struct DeleteComment
{
unsigned int LineNr;
std::string Str;
};
std::list< DeleteComment > DeleteComments;
void TokenizeCode(std::istream &code, const unsigned int FileIndex)
{
DeleteComments.clear();
// Tokenize the file.
unsigned int lineno = 1;
char CurrentToken[1000] = {0};
@ -391,6 +401,8 @@ void TokenizeCode(std::istream &code, const unsigned int FileIndex)
// Comments..
if (ch == '/' && !code.eof())
{
bool newstatement = bool( strchr(";{}", CurrentToken[0]) != NULL );
// Add current token..
addtoken(CurrentToken, lineno, FileIndex);
memset(CurrentToken, 0, sizeof(CurrentToken));
@ -402,7 +414,15 @@ void TokenizeCode(std::istream &code, const unsigned int FileIndex)
// If '//'..
if (ch == '/')
{
while (!code.eof() && (char)code.get()!='\n');
std::string comment;
getline( code, comment );
if ( newstatement && comment.find(" delete")!=std::string::npos )
{
DeleteComment dc;
dc.LineNr = lineno;
dc.Str = comment;
DeleteComments.push_back( dc );
}
lineno++;
continue;
}
@ -1093,4 +1113,15 @@ void DeallocateTokens()
int isdeleted( const char varname[] )
{
std::list<DeleteComment>::const_iterator it;
for ( it = DeleteComments.begin(); it != DeleteComments.end(); it++ )
{
const DeleteComment &dc = *it;
if ( dc.Str.find( varname ) != std::string::npos )
return dc.LineNr;
}
return -1;
}

View File

@ -39,6 +39,11 @@ const TOKEN *findtoken(const TOKEN *tok1, const char *tokenstr[]);
const TOKEN *gettok(const TOKEN *tok, int index);
const char *getstr(const TOKEN *tok, int index);
// Delete comments such as "fred is deleted automaticly"
int isdeleted( const char varname[] );
//---------------------------------------------------------------------------
#endif