diff --git a/CheckMemoryLeak.cpp b/CheckMemoryLeak.cpp index 081bbd5a2..0e8f474f9 100644 --- a/CheckMemoryLeak.cpp +++ b/CheckMemoryLeak.cpp @@ -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() diff --git a/CommonCheck.cpp b/CommonCheck.cpp index 0ad4c71ec..800539329 100644 --- a/CommonCheck.cpp +++ b/CommonCheck.cpp @@ -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; +} +//--------------------------------------------------------------------------- diff --git a/CommonCheck.h b/CommonCheck.h index a6ae635cf..c6e66d6d8 100644 --- a/CommonCheck.h +++ b/CommonCheck.h @@ -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 diff --git a/Statements.cpp b/Statements.cpp index e364bf4e1..6a25d4d7f 100644 --- a/Statements.cpp +++ b/Statements.cpp @@ -11,6 +11,7 @@ std::vector VariableNames; std::list 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);