Skip to content

Commit a2f2699

Browse files
authored
Refactor alias check in isExpressionChangeAt into isAlias function (#4223)
1 parent 2223cd2 commit a2f2699

2 files changed

Lines changed: 32 additions & 17 deletions

File tree

lib/astutils.cpp

Lines changed: 30 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -865,6 +865,34 @@ bool isAliasOf(const Token *tok, nonneg int varid, bool* inconclusive)
865865
return false;
866866
}
867867

868+
bool isAliasOf(const Token* tok, const Token* expr, bool* inconclusive)
869+
{
870+
const bool pointer = astIsPointer(tok);
871+
const ValueFlow::Value* value = nullptr;
872+
const Token* r = findAstNode(expr, [&](const Token* childTok) {
873+
for (const ValueFlow::Value& val : tok->values()) {
874+
if (val.isImpossible())
875+
continue;
876+
if (val.isLocalLifetimeValue() || (pointer && val.isSymbolicValue() && val.intvalue == 0)) {
877+
if (findAstNode(val.tokvalue,
878+
[&](const Token* aliasTok) {
879+
return aliasTok->exprId() == childTok->exprId();
880+
})) {
881+
if (val.isInconclusive() && inconclusive) { // NOLINT
882+
value = &val;
883+
} else {
884+
return true;
885+
}
886+
}
887+
}
888+
}
889+
return false;
890+
});
891+
if (!r && value && inconclusive)
892+
*inconclusive = true;
893+
return r || value;
894+
}
895+
868896
static bool isAliased(const Token *startTok, const Token *endTok, nonneg int varid)
869897
{
870898
if (!precedes(startTok, endTok))
@@ -2452,27 +2480,12 @@ static bool isExpressionChangedAt(const F& getExprTok,
24522480
if (globalvar && !tok->isKeyword() && Token::Match(tok, "%name% (") && !(tok->function() && tok->function()->isAttributePure()))
24532481
// TODO: Is global variable really changed by function call?
24542482
return true;
2455-
const bool pointer = astIsPointer(tok);
24562483
bool aliased = false;
24572484
// If we can't find the expression then assume it is an alias
24582485
if (!getExprTok())
24592486
aliased = true;
2460-
if (!aliased) {
2461-
aliased = findAstNode(getExprTok(), [&](const Token* childTok) {
2462-
for (const ValueFlow::Value& val : tok->values()) {
2463-
if (val.isImpossible())
2464-
continue;
2465-
if (val.isLocalLifetimeValue() || (pointer && val.isSymbolicValue() && val.intvalue == 0)) {
2466-
if (findAstNode(val.tokvalue,
2467-
[&](const Token* aliasTok) {
2468-
return aliasTok->exprId() == childTok->exprId();
2469-
}))
2470-
return true;
2471-
}
2472-
}
2473-
return false;
2474-
});
2475-
}
2487+
if (!aliased)
2488+
aliased = isAliasOf(tok, getExprTok());
24762489
if (!aliased)
24772490
return false;
24782491
if (isVariableChanged(tok, 1, settings, cpp, depth))

lib/astutils.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -335,6 +335,8 @@ bool isExpressionChangedAt(const Token* expr,
335335
/// If token is an alias if another variable
336336
bool isAliasOf(const Token *tok, nonneg int varid, bool* inconclusive = nullptr);
337337

338+
bool isAliasOf(const Token* tok, const Token* expr, bool* inconclusive = nullptr);
339+
338340
bool isAliased(const Variable *var);
339341

340342
const Token* getArgumentStart(const Token* ftok);

0 commit comments

Comments
 (0)