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
{
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)
{
// Deallocation..
@ -386,12 +396,16 @@ static void _ClassMembers_CheckVar(const char *classname, const char *varname)
if ( match(tok, "var = new type ;") ||
match(tok, "var = new type (") )
{
if ( ! ShowWarnings && ! IsStandardType(getstr(tok,3)) )
continue;
err |= ( Alloc != No && Alloc != New );
Alloc = New;
}
else if ( match(tok, "var = new type [") )
{
if ( ! ShowWarnings && ! IsStandardType(getstr(tok,3)) )
continue;
err |= ( Alloc != No && Alloc != NewA );
Alloc = NewA;
}
@ -400,6 +414,11 @@ static void _ClassMembers_CheckVar(const char *classname, const char *varname)
else if ( match(tok, "var = strdup (") ||
match(tok, "var = ( type * ) malloc ("))
{
if ( ! ShowWarnings &&
tok->next->next->str[0] == '(' &&
! IsStandardType(getstr(tok,3)) )
continue;
err |= ( Alloc != No && 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 << "'";
ReportErr(ostr.str());
}
std::ostringstream ostr;
ostr << "Memory leak for '" << classname << "::" << varname << "'";
ReportErr(ostr.str());
}
}
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 IsNumber(const char str[]);
bool IsStandardType(const char str[]);
//---------------------------------------------------------------------------
#endif

View File

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