Make it possible to disable memory leak checking for a variable. Usable to avoid false positives
This commit is contained in:
parent
214266c08c
commit
1d258a55a4
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
10
tests.cpp
10
tests.cpp
|
@ -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, "" );
|
||||
|
||||
|
||||
|
||||
}
|
||||
//---------------------------------------------------------------------------
|
||||
|
||||
|
|
33
tokenize.cpp
33
tokenize.cpp
|
@ -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;
|
||||
}
|
||||
|
||||
|
|
|
@ -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
|
||||
|
||||
|
|
Loading…
Reference in New Issue