Library: Refactoring alloc/dealloc functions. Normally the token should be passed so extra validation can be made

This commit is contained in:
Daniel Marjamäki 2014-04-20 14:59:16 +02:00
parent 5fc43f85b1
commit 01d14d0388
3 changed files with 22 additions and 11 deletions

View File

@ -232,7 +232,7 @@ void CheckLeakAutoVar::checkScope(const Token * const startToken,
// allocation? // allocation?
if (Token::Match(tok->tokAt(2), "%type% (")) { if (Token::Match(tok->tokAt(2), "%type% (")) {
int i = _settings->library.alloc(tok->strAt(2)); int i = _settings->library.alloc(tok->tokAt(2));
if (i > 0) { if (i > 0) {
alloctype[tok->varId()] = i; alloctype[tok->varId()] = i;
} }
@ -255,7 +255,7 @@ void CheckLeakAutoVar::checkScope(const Token * const startToken,
if (innerTok->str() == ")") if (innerTok->str() == ")")
break; break;
if (innerTok->str() == "(" && innerTok->previous()->isName()) { if (innerTok->str() == "(" && innerTok->previous()->isName()) {
const int deallocId = _settings->library.dealloc(tok->str()); const int deallocId = _settings->library.dealloc(tok);
functionCall(innerTok->previous(), varInfo, deallocId); functionCall(innerTok->previous(), varInfo, deallocId);
innerTok = innerTok->link(); innerTok = innerTok->link();
} }
@ -343,7 +343,7 @@ void CheckLeakAutoVar::checkScope(const Token * const startToken,
// Function call.. // Function call..
else if (Token::Match(tok, "%type% (") && tok->str() != "return") { else if (Token::Match(tok, "%type% (") && tok->str() != "return") {
const int dealloc = _settings->library.dealloc(tok->str()); const int dealloc = _settings->library.dealloc(tok);
functionCall(tok, varInfo, dealloc); functionCall(tok, varInfo, dealloc);

View File

@ -180,7 +180,7 @@ CheckMemoryLeak::AllocType CheckMemoryLeak::getAllocationType(const Token *tok2,
} }
// Does tok2 point on "g_malloc", "g_strdup", .. // Does tok2 point on "g_malloc", "g_strdup", ..
const int alloctype = settings1->library.alloc(tok2->str()); const int alloctype = settings1->library.alloc(tok2);
if (alloctype > 0) { if (alloctype > 0) {
if (alloctype == settings1->library.dealloc("free")) if (alloctype == settings1->library.dealloc("free"))
return Malloc; return Malloc;
@ -271,7 +271,7 @@ CheckMemoryLeak::AllocType CheckMemoryLeak::getDeallocationType(const Token *tok
// Does tok2 point on "g_free", etc .. // Does tok2 point on "g_free", etc ..
if (Token::Match(tok, "%type% ( %varid% )", varid)) { if (Token::Match(tok, "%type% ( %varid% )", varid)) {
const int dealloctype = settings1->library.dealloc(tok->str()); const int dealloctype = settings1->library.dealloc(tok);
if (dealloctype > 0) if (dealloctype > 0)
return Library::ismemory(dealloctype) ? OtherMem : OtherRes; return Library::ismemory(dealloctype) ? OtherMem : OtherRes;
} }
@ -309,7 +309,7 @@ CheckMemoryLeak::AllocType CheckMemoryLeak::getDeallocationType(const Token *tok
return Pipe; return Pipe;
if (Token::Match(tok, ("%type% ( " + varname + " )").c_str())) { if (Token::Match(tok, ("%type% ( " + varname + " )").c_str())) {
int type = settings1->library.dealloc(tok->str()); int type = settings1->library.dealloc(tok);
if (type > 0) if (type > 0)
return Library::ismemory(type) ? OtherMem : OtherRes; return Library::ismemory(type) ? OtherMem : OtherRes;
} }
@ -2764,7 +2764,7 @@ void CheckMemoryLeakNoVar::checkForUnusedReturnValue(const Scope *scope)
for (const Token *tok = scope->classStart; tok != scope->classEnd; tok = tok->next()) { for (const Token *tok = scope->classStart; tok != scope->classEnd; tok = tok->next()) {
if (Token::Match(tok, "{|}|; %var% (")) { if (Token::Match(tok, "{|}|; %var% (")) {
tok = tok->next(); tok = tok->next();
const int allocationId = _settings->library.alloc(tok->str()); const int allocationId = _settings->library.alloc(tok);
if (allocationId > 0) if (allocationId > 0)
returnValueNotUsedError(tok, tok->str()); returnValueNotUsedError(tok, tok->str());
} }

View File

@ -24,6 +24,7 @@
#include "config.h" #include "config.h"
#include "path.h" #include "path.h"
#include "mathlib.h" #include "mathlib.h"
#include "token.h"
#include <map> #include <map>
#include <set> #include <set>
@ -50,13 +51,23 @@ public:
bool loadxmldata(const char xmldata[], std::size_t len); bool loadxmldata(const char xmldata[], std::size_t len);
bool load(const tinyxml2::XMLDocument &doc); bool load(const tinyxml2::XMLDocument &doc);
/** get allocation id for function (by name) */ /** get allocation id for function by name */
int alloc(const std::string &name) const { int alloc(const char name[]) const {
return getid(_alloc, name); return getid(_alloc, name);
} }
/** get deallocation id for function (by name) */ /** get allocation id for function */
int dealloc(const std::string &name) const { int alloc(const Token *tok) const {
return tok->function() ? 0 : getid(_alloc, tok->str());
}
/** get deallocation id for function */
int dealloc(const Token *tok) const {
return tok->function() ? 0 : getid(_dealloc, tok->str());
}
/** get deallocation id for function by name */
int dealloc(const char name[]) const {
return getid(_dealloc, name); return getid(_dealloc, name);
} }