diff --git a/lib/astutils.cpp b/lib/astutils.cpp index 1629d6239..dc1f6ac7a 100644 --- a/lib/astutils.cpp +++ b/lib/astutils.cpp @@ -865,6 +865,34 @@ bool isAliasOf(const Token *tok, nonneg int varid, bool* inconclusive) 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) { 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())) // TODO: Is global variable really changed by function call? return true; - const bool pointer = astIsPointer(tok); bool aliased = false; // If we can't find the expression then assume it is an alias if (!getExprTok()) aliased = true; - if (!aliased) { - aliased = findAstNode(getExprTok(), [&](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(); - })) - return true; - } - } - return false; - }); - } + if (!aliased) + aliased = isAliasOf(tok, getExprTok()); if (!aliased) return false; if (isVariableChanged(tok, 1, settings, cpp, depth)) diff --git a/lib/astutils.h b/lib/astutils.h index b82c922b9..c30242f3b 100644 --- a/lib/astutils.h +++ b/lib/astutils.h @@ -335,6 +335,8 @@ bool isExpressionChangedAt(const Token* expr, /// If token is an alias if another variable 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); const Token* getArgumentStart(const Token* ftok);