Refactor alias check in isExpressionChangeAt into isAlias function (#4223)

This commit is contained in:
Paul Fultz II 2022-06-28 15:40:59 -05:00 committed by GitHub
parent 2223cd24b9
commit a2f2699088
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 32 additions and 17 deletions

View File

@ -865,6 +865,34 @@ bool isAliasOf(const Token *tok, nonneg int varid, bool* inconclusive)
return false; return false;
} }
bool isAliasOf(const Token* tok, const Token* expr, bool* inconclusive)
{
const bool pointer = astIsPointer(tok);
const ValueFlow::Value* value = nullptr;
const Token* r = findAstNode(expr, [&](const Token* childTok) {
for (const ValueFlow::Value& val : tok->values()) {
if (val.isImpossible())
continue;
if (val.isLocalLifetimeValue() || (pointer && val.isSymbolicValue() && val.intvalue == 0)) {
if (findAstNode(val.tokvalue,
[&](const Token* aliasTok) {
return aliasTok->exprId() == childTok->exprId();
})) {
if (val.isInconclusive() && inconclusive) { // NOLINT
value = &val;
} else {
return true;
}
}
}
}
return false;
});
if (!r && value && inconclusive)
*inconclusive = true;
return r || value;
}
static bool isAliased(const Token *startTok, const Token *endTok, nonneg int varid) static bool isAliased(const Token *startTok, const Token *endTok, nonneg int varid)
{ {
if (!precedes(startTok, endTok)) if (!precedes(startTok, endTok))
@ -2452,27 +2480,12 @@ static bool isExpressionChangedAt(const F& getExprTok,
if (globalvar && !tok->isKeyword() && Token::Match(tok, "%name% (") && !(tok->function() && tok->function()->isAttributePure())) if (globalvar && !tok->isKeyword() && Token::Match(tok, "%name% (") && !(tok->function() && tok->function()->isAttributePure()))
// TODO: Is global variable really changed by function call? // TODO: Is global variable really changed by function call?
return true; return true;
const bool pointer = astIsPointer(tok);
bool aliased = false; bool aliased = false;
// If we can't find the expression then assume it is an alias // If we can't find the expression then assume it is an alias
if (!getExprTok()) if (!getExprTok())
aliased = true; aliased = true;
if (!aliased) { if (!aliased)
aliased = findAstNode(getExprTok(), [&](const Token* childTok) { aliased = isAliasOf(tok, getExprTok());
for (const ValueFlow::Value& val : tok->values()) {
if (val.isImpossible())
continue;
if (val.isLocalLifetimeValue() || (pointer && val.isSymbolicValue() && val.intvalue == 0)) {
if (findAstNode(val.tokvalue,
[&](const Token* aliasTok) {
return aliasTok->exprId() == childTok->exprId();
}))
return true;
}
}
return false;
});
}
if (!aliased) if (!aliased)
return false; return false;
if (isVariableChanged(tok, 1, settings, cpp, depth)) if (isVariableChanged(tok, 1, settings, cpp, depth))

View File

@ -335,6 +335,8 @@ bool isExpressionChangedAt(const Token* expr,
/// If token is an alias if another variable /// If token is an alias if another variable
bool isAliasOf(const Token *tok, nonneg int varid, bool* inconclusive = nullptr); bool isAliasOf(const Token *tok, nonneg int varid, bool* inconclusive = nullptr);
bool isAliasOf(const Token* tok, const Token* expr, bool* inconclusive = nullptr);
bool isAliased(const Variable *var); bool isAliased(const Variable *var);
const Token* getArgumentStart(const Token* ftok); const Token* getArgumentStart(const Token* ftok);