Refactorization: Removed std::string overload of CheckMemoryLeak::getDeallocationType(), improved varid overload to handle member variables

This commit is contained in:
PKEuS 2015-11-18 21:17:50 +01:00
parent e990cfb76b
commit 4957e48d7c
2 changed files with 33 additions and 88 deletions

View File

@ -226,94 +226,55 @@ CheckMemoryLeak::AllocType CheckMemoryLeak::getReallocationType(const Token *tok
if (tok2->str() == "realloc") if (tok2->str() == "realloc")
return Malloc; return Malloc;
// GTK memory reallocation..
//if (Token::Match(tok2, "g_realloc|g_try_realloc|g_renew|g_try_renew"))
return No; return No;
} }
CheckMemoryLeak::AllocType CheckMemoryLeak::getDeallocationType(const Token *tok, unsigned int varid) const CheckMemoryLeak::AllocType CheckMemoryLeak::getDeallocationType(const Token *tok, unsigned int varid) const
{ {
if (tokenizer->isCPP()) { if (tokenizer->isCPP() && tok->str() == "delete") {
if (Token::Match(tok, "delete %varid% ;", varid)) const Token* vartok = tok->astOperand1() ? tok->astOperand1() : tok->next();
if (Token::Match(vartok, ".|::"))
vartok = vartok->astOperand2();
if (vartok && vartok->varId() == varid) {
if (tok->strAt(1) == "[")
return NewArray;
return New; return New;
}
if (Token::Match(tok, "delete [ ] %varid% ;", varid))
return NewArray;
if (Token::Match(tok, "delete ( %varid% ) ;", varid))
return New;
if (Token::Match(tok, "delete [ ] ( %varid% ) ;", varid))
return NewArray;
} }
if (tok && tok->str() == "::") if (tok && tok->str() == "::")
tok = tok->next(); tok = tok->next();
if (Token::Match(tok, "free|kfree ( %varid% ) [;:]", varid) || if (Token::Match(tok, "%name% (")) {
Token::Match(tok, "free|kfree ( %varid% -|,", varid) || if (Token::simpleMatch(tok, "fcloseall ( )"))
Token::Match(tok, "realloc ( %varid% , 0 ) ;", varid)) return File;
return Malloc;
if (Token::Match(tok, "fclose ( %varid% )", varid) || const Token* vartok = tok->tokAt(2);
Token::simpleMatch(tok, "fcloseall ( )")) while (Token::Match(vartok, "%name% .|::"))
return File; vartok = vartok->tokAt(2);
if (settings1->standards.posix) { if (Token::Match(vartok, "%varid% )|,|-", varid)) {
if (Token::Match(tok, "close ( %varid% )", varid)) if (Token::Match(tok, "free|kfree") ||
return Fd; (tok->str() == "realloc" && Token::Match(vartok->next(), ", 0 )")))
return Malloc;
if (Token::Match(tok, "pclose ( %varid% )", varid)) if (tok->str() == "fclose")
return Pipe; return File;
}
// Does tok2 point on "g_free", etc .. if (settings1->standards.posix) {
if (Token::Match(tok, "%type% ( %varid% )", varid)) { if (tok->str() == "close")
const int dealloctype = settings1->library.dealloc(tok); return Fd;
if (dealloctype > 0) if (tok->str() == "pclose")
return Library::ismemory(dealloctype) ? OtherMem : OtherRes; return Pipe;
} }
return No; // Does tok2 point on "g_free", etc ..
} const int dealloctype = settings1->library.dealloc(tok);
if (dealloctype > 0)
CheckMemoryLeak::AllocType CheckMemoryLeak::getDeallocationType(const Token *tok, const std::string &varname) const return Library::ismemory(dealloctype) ? OtherMem : OtherRes;
{ }
if (tokenizer->isCPP()) {
if (Token::Match(tok, std::string("delete " + varname + " [,;]").c_str()))
return New;
if (Token::Match(tok, std::string("delete [ ] " + varname + " [,;]").c_str()))
return NewArray;
if (Token::Match(tok, std::string("delete ( " + varname + " ) [,;]").c_str()))
return New;
if (Token::Match(tok, std::string("delete [ ] ( " + varname + " ) [,;]").c_str()))
return NewArray;
}
if (Token::simpleMatch(tok, std::string("free ( " + varname + " ) ;").c_str()) ||
Token::simpleMatch(tok, std::string("kfree ( " + varname + " ) ;").c_str()) ||
Token::simpleMatch(tok, std::string("realloc ( " + varname + " , 0 ) ;").c_str()))
return Malloc;
if (Token::simpleMatch(tok, std::string("fclose ( " + varname + " )").c_str()) ||
Token::simpleMatch(tok, "fcloseall ( )"))
return File;
if (Token::simpleMatch(tok, std::string("close ( " + varname + " )").c_str()))
return Fd;
if (Token::simpleMatch(tok, std::string("pclose ( " + varname + " )").c_str()))
return Pipe;
if (Token::Match(tok, ("%type% ( " + varname + " )").c_str())) {
int type = settings1->library.dealloc(tok);
if (type > 0)
return Library::ismemory(type) ? OtherMem : OtherRes;
} }
return No; return No;
@ -2384,15 +2345,7 @@ void CheckMemoryLeakInClass::variable(const Scope *scope, const Token *tokVarnam
continue; continue;
// Deallocate.. // Deallocate..
AllocType dealloc = getDeallocationType(tok, varname); AllocType dealloc = getDeallocationType(tok, varid);
if (dealloc == No) {
std::string temp = scope->className + " :: " + varname;
dealloc = getDeallocationType(tok, temp);
}
if (dealloc == No) {
std::string temp = "this . " + varname;
dealloc = getDeallocationType(tok, temp);
}
// some usage in the destructor => assume it's related // some usage in the destructor => assume it's related
// to deallocation // to deallocation
if (destructor && tok->str() == varname) if (destructor && tok->str() == varname)

View File

@ -97,14 +97,6 @@ public:
void memoryLeak(const Token *tok, const std::string &varname, AllocType alloctype); void memoryLeak(const Token *tok, const std::string &varname, AllocType alloctype);
/**
* @brief Get type of deallocation at given position
* @param tok position
* @param varname variable name
* @return type of deallocation
*/
AllocType getDeallocationType(const Token *tok, const std::string &varname) const;
/** /**
* @brief Get type of deallocation at given position * @brief Get type of deallocation at given position
* @param tok position * @param tok position