Skip to content

Commit d549770

Browse files
committed
updated extracttests.py. fix syntax errors in test cases.
1 parent 7ba9e37 commit d549770

9 files changed

Lines changed: 329 additions & 186 deletions

lib/astutils.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2324,6 +2324,21 @@ const Variable *getLHSVariable(const Token *tok)
23242324
return getLHSVariableRecursive(tok->astOperand1());
23252325
}
23262326

2327+
const Token* findAllocFuncCallToken(const Token *expr, const Library &library)
2328+
{
2329+
if (!expr)
2330+
return nullptr;
2331+
if (Token::Match(expr, "[+-]")) {
2332+
const Token *tok1 = findAllocFuncCallToken(expr->astOperand1(), library);
2333+
return tok1 ? tok1 : findAllocFuncCallToken(expr->astOperand2(), library);
2334+
}
2335+
if (expr->isCast())
2336+
return findAllocFuncCallToken(expr->astOperand2() ? expr->astOperand2() : expr->astOperand1(), library);
2337+
if (Token::Match(expr->previous(), "%name% (") && library.getAllocFuncInfo(expr->astOperand1()))
2338+
return expr->astOperand1();
2339+
return (Token::simpleMatch(expr, "new") && expr->astOperand1()) ? expr : nullptr;
2340+
}
2341+
23272342
static bool nonLocal(const Variable* var, bool deref)
23282343
{
23292344
return !var || (!var->isLocal() && !var->isArgument()) || (deref && var->isArgument() && var->isPointer()) || var->isStatic() || var->isReference() || var->isExtern();

lib/astutils.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,9 @@ const Variable *getLHSVariable(const Token *tok);
281281

282282
std::vector<const Variable*> getLHSVariables(const Token* tok);
283283

284+
/** Find a allocation function call in expression, so result of expression is allocated memory/resource. */
285+
const Token* findAllocFuncCallToken(const Token *expr, const Library &library);
286+
284287
bool isScopeBracket(const Token* tok);
285288

286289
bool isNullOperand(const Token *expr);

lib/checkuninitvar.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -171,9 +171,12 @@ void CheckUninitVar::checkScope(const Scope* scope, const std::set<std::string>
171171
if (arg.declarationId() && Token::Match(arg.typeStartToken(), "%type% * %name% [,)]")) {
172172
// Treat the pointer as initialized until it is assigned by malloc
173173
for (const Token *tok = scope->bodyStart; tok != scope->bodyEnd; tok = tok->next()) {
174-
if (!Token::Match(tok, "[;{}] %varid% = %name% (", arg.declarationId()))
174+
if (!Token::Match(tok, "[;{}] %varid% =", arg.declarationId()))
175175
continue;
176-
const Library::AllocFunc *allocFunc = mSettings->library.getAllocFuncInfo(tok->tokAt(3));
176+
const Token *allocFuncCallToken = findAllocFuncCallToken(tok->tokAt(2)->astOperand2(), mSettings->library);
177+
if (!allocFuncCallToken)
178+
continue;
179+
const Library::AllocFunc *allocFunc = mSettings->library.getAllocFuncInfo(allocFuncCallToken);
177180
if (!allocFunc || allocFunc->initData)
178181
continue;
179182

lib/checkunusedvar.cpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -938,11 +938,14 @@ void CheckUnusedVar::checkFunctionVariableUsage_iterateScopes(const Scope* const
938938
}
939939
// Consider allocating memory separately because allocating/freeing alone does not constitute using the variable
940940
else if (var && var->mType == Variables::pointer &&
941-
Token::Match(start, "%name% = new|malloc|calloc|kmalloc|kzalloc|kcalloc|strdup|strndup|vmalloc|g_new0|g_try_new|g_new|g_malloc|g_malloc0|g_try_malloc|g_try_malloc0|g_strdup|g_strndup|g_strdup_printf")) {
941+
Token::Match(start, "%name% =") &&
942+
findAllocFuncCallToken(start->next()->astOperand2(), mSettings->library)) {
942943
bool allocate = true;
943944

944-
if (start->strAt(2) == "new") {
945-
const Token *type = start->tokAt(3);
945+
const Token *allocFuncCallToken = findAllocFuncCallToken(start->next()->astOperand2(), mSettings->library);
946+
947+
if (allocFuncCallToken->str() == "new") {
948+
const Token *type = allocFuncCallToken->next();
946949

947950
// skip nothrow
948951
if (mTokenizer->isCPP() && (Token::simpleMatch(type, "( nothrow )") ||

0 commit comments

Comments
 (0)