CheckClass::copyconstructors: Use library to determine if function is a allocation function

This commit is contained in:
Daniel Marjamäki 2018-04-27 23:20:04 +02:00
parent eb922a140a
commit 1a9e8c158d
2 changed files with 20 additions and 2 deletions

View File

@ -288,14 +288,16 @@ void CheckClass::copyconstructors()
continue; continue;
const Token* tok = func.token->linkAt(1); const Token* tok = func.token->linkAt(1);
for (const Token* const end = func.functionScope->bodyStart; tok != end; tok = tok->next()) { for (const Token* const end = func.functionScope->bodyStart; tok != end; tok = tok->next()) {
if (Token::Match(tok, "%var% ( new|malloc|g_malloc|g_try_malloc|realloc|g_realloc|g_try_realloc")) { if (Token::Match(tok, "%var% ( new") ||
(Token::Match(tok, "%var% ( %name% (") && _settings->library.alloc(tok->tokAt(2)))) {
const Variable* var = tok->variable(); const Variable* var = tok->variable();
if (var && var->isPointer() && var->scope() == scope) if (var && var->isPointer() && var->scope() == scope)
allocatedVars[tok->varId()] = tok; allocatedVars[tok->varId()] = tok;
} }
} }
for (const Token* const end = func.functionScope->bodyEnd; tok != end; tok = tok->next()) { for (const Token* const end = func.functionScope->bodyEnd; tok != end; tok = tok->next()) {
if (Token::Match(tok, "%var% = new|malloc|g_malloc|g_try_malloc|realloc|g_realloc|g_try_realloc")) { if (Token::Match(tok, "%var% = new") ||
(Token::Match(tok, "%var% = %name% (") && _settings->library.alloc(tok->tokAt(2)))) {
const Variable* var = tok->variable(); const Variable* var = tok->variable();
if (var && var->isPointer() && var->scope() == scope && !var->isStatic()) if (var && var->isPointer() && var->scope() == scope && !var->isStatic())
allocatedVars[tok->varId()] = tok; allocatedVars[tok->varId()] = tok;

View File

@ -38,6 +38,22 @@ private:
settings0.addEnabled("style"); settings0.addEnabled("style");
settings1.addEnabled("warning"); settings1.addEnabled("warning");
// Load std.cfg configuration
{
const char xmldata[] = "<?xml version=\"1.0\"?>\n"
"<def>\n"
" <memory>\n"
" <alloc init=\"false\">malloc</alloc>\n"
" <dealloc>free</dealloc>\n"
" </memory>\n"
"</def>";
tinyxml2::XMLDocument doc;
doc.Parse(xmldata, sizeof(xmldata));
settings0.library.load(doc);
settings1.library.load(doc);
}
TEST_CASE(virtualDestructor1); // Base class not found => no error TEST_CASE(virtualDestructor1); // Base class not found => no error
TEST_CASE(virtualDestructor2); // Base class doesn't have a destructor TEST_CASE(virtualDestructor2); // Base class doesn't have a destructor
TEST_CASE(virtualDestructor3); // Base class has a destructor, but it's not virtual TEST_CASE(virtualDestructor3); // Base class has a destructor, but it's not virtual