For the highest accuracy, don't warn for all memory leaks unless the "-w" is given

This commit is contained in:
Daniel Marjamäki 2007-07-20 06:20:31 +00:00
parent 8af24ee783
commit 5b6ab28e0b
4 changed files with 58 additions and 15 deletions

View File

@ -17,6 +17,10 @@
//--------------------------------------------------------------------------- //---------------------------------------------------------------------------
extern bool ShowWarnings;
//---------------------------------------------------------------------------
struct _variable struct _variable
{ {
int indentlevel; int indentlevel;
@ -334,6 +338,12 @@ static void _ClassMembers_CheckVar(const char *classname, const char *varname)
} }
} }
else if (indentlevel==0 && tok->str[0] == ';')
{
func = tok;
break;
}
else if (indentlevel > 0) else if (indentlevel > 0)
{ {
// Deallocation.. // Deallocation..
@ -386,12 +396,16 @@ static void _ClassMembers_CheckVar(const char *classname, const char *varname)
if ( match(tok, "var = new type ;") || if ( match(tok, "var = new type ;") ||
match(tok, "var = new type (") ) match(tok, "var = new type (") )
{ {
if ( ! ShowWarnings && ! IsStandardType(getstr(tok,3)) )
continue;
err |= ( Alloc != No && Alloc != New ); err |= ( Alloc != No && Alloc != New );
Alloc = New; Alloc = New;
} }
else if ( match(tok, "var = new type [") ) else if ( match(tok, "var = new type [") )
{ {
if ( ! ShowWarnings && ! IsStandardType(getstr(tok,3)) )
continue;
err |= ( Alloc != No && Alloc != NewA ); err |= ( Alloc != No && Alloc != NewA );
Alloc = NewA; Alloc = NewA;
} }
@ -400,6 +414,11 @@ static void _ClassMembers_CheckVar(const char *classname, const char *varname)
else if ( match(tok, "var = strdup (") || else if ( match(tok, "var = strdup (") ||
match(tok, "var = ( type * ) malloc (")) match(tok, "var = ( type * ) malloc ("))
{ {
if ( ! ShowWarnings &&
tok->next->next->str[0] == '(' &&
! IsStandardType(getstr(tok,3)) )
continue;
err |= ( Alloc != No && Alloc != Malloc ); err |= ( Alloc != No && Alloc != Malloc );
Alloc = Malloc; Alloc = Malloc;
} }
@ -415,16 +434,12 @@ static void _ClassMembers_CheckVar(const char *classname, const char *varname)
} }
} }
if ( Alloc != Dealloc ) if ( Alloc != Dealloc && Dealloc == No )
{ {
if ( Dealloc == No ) std::ostringstream ostr;
{ ostr << "Memory leak for '" << classname << "::" << varname << "'";
std::ostringstream ostr; ReportErr(ostr.str());
ostr << "Memory leak for '" << classname << "::" << varname << "'";
ReportErr(ostr.str());
}
} }
} }
static void _ClassMembers() static void _ClassMembers()

View File

@ -34,4 +34,15 @@ bool IsNumber(const char str[])
} }
//--------------------------------------------------------------------------- //---------------------------------------------------------------------------
bool IsStandardType(const char str[])
{
if (!str)
return false;
bool Ret = false;
const char *type[] = {"bool","char","short","int","long","float","double",0};
for (int i = 0; type[i]; i++)
Ret |= (strcmp(str,type[i])==0);
return Ret;
}
//---------------------------------------------------------------------------

View File

@ -15,5 +15,7 @@ void ReportErr(const std::string errmsg);
bool IsName(const char str[]); bool IsName(const char str[]);
bool IsNumber(const char str[]); bool IsNumber(const char str[]);
bool IsStandardType(const char str[]);
//--------------------------------------------------------------------------- //---------------------------------------------------------------------------
#endif #endif

View File

@ -11,6 +11,7 @@ std::vector<std::string> VariableNames;
std::list<STATEMENT> Statements; std::list<STATEMENT> Statements;
extern bool Debug; extern bool Debug;
extern bool ShowWarnings;
@ -19,7 +20,7 @@ extern bool Debug;
// Create statement list // Create statement list
//--------------------------------------------------------------------------- //---------------------------------------------------------------------------
void AppendStatement(STATEMENT::etype Type, TOKEN *tok, std::string Var="") static void AppendStatement(STATEMENT::etype Type, TOKEN *tok, std::string Var="", const char PointerType[]=0)
{ {
STATEMENT NewStatement; STATEMENT NewStatement;
NewStatement.Type = Type; NewStatement.Type = Type;
@ -47,6 +48,13 @@ void AppendStatement(STATEMENT::etype Type, TOKEN *tok, std::string Var="")
VariableNames.push_back(Var); VariableNames.push_back(Var);
} }
} }
if ( PointerType )
{
if ( ! ShowWarnings && ! IsStandardType(PointerType) )
return;
}
Statements.push_back(NewStatement); Statements.push_back(NewStatement);
} }
@ -210,27 +218,34 @@ void CreateStatementList()
{ {
TOKEN *rs = eq->next; TOKEN *rs = eq->next;
const char *pointertype = 0;
bool ismalloc = false; bool ismalloc = false;
ismalloc |= match(rs, "strdup ("); if (match(rs, "strdup ("))
if (rs->str[0]=='(' && IsName(getstr(rs,1))) {
pointertype = "char";
ismalloc = true;
}
else if (rs->str[0]=='(' && IsName(getstr(rs,1)))
{ {
ismalloc |= match(rs, "( type * ) malloc ("); ismalloc |= match(rs, "( type * ) malloc (");
ismalloc |= match(rs, "( type * * ) malloc ("); ismalloc |= match(rs, "( type * * ) malloc (");
ismalloc |= match(rs, "( type type * ) malloc ("); ismalloc |= match(rs, "( type type * ) malloc (");
ismalloc |= match(rs, "( type type * * ) malloc ("); ismalloc |= match(rs, "( type type * * ) malloc (");
if (ismalloc)
pointertype = getstr(rs, rs->next->next->str[0]=='*' ? 1 : 2 );
} }
if ( ismalloc ) if ( ismalloc )
AppendStatement(STATEMENT::MALLOC, tok2, varname); AppendStatement(STATEMENT::MALLOC, tok2, varname, pointertype);
else if ( match(rs,"new type ;") ) else if ( match(rs,"new type ;") )
AppendStatement(STATEMENT::NEW, tok2, varname); AppendStatement(STATEMENT::NEW, tok2, varname, getstr(rs,1));
else if ( match(rs, "new type (") ) else if ( match(rs, "new type (") )
AppendStatement(STATEMENT::NEW, tok2, varname); AppendStatement(STATEMENT::NEW, tok2, varname, getstr(rs,1));
else if ( match(rs, "new type [") ) else if ( match(rs, "new type [") )
AppendStatement(STATEMENT::NEWARRAY, tok2, varname); AppendStatement(STATEMENT::NEWARRAY, tok2, varname, getstr(rs,1));
else else
AppendStatement(STATEMENT::ASSIGN, tok2, varname); AppendStatement(STATEMENT::ASSIGN, tok2, varname);