Skip to content

Commit 9c1d989

Browse files
dmcppcheckclaude
andcommitted
isAliasOf: filter the alias values once per reference
The values of the reference token were filtered again for every node in the expression. Filter them once and skip the AST search when there is nothing that can match. No changed warnings in a 50 package debian corpus, ValueFlow ~10% faster. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 7e562f0 commit 9c1d989

2 files changed

Lines changed: 31 additions & 14 deletions

File tree

‎lib/astutils.cpp‎

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1088,29 +1088,32 @@ bool isAliasOf(const Token* tok, const Token* expr, nonneg int* indirect)
10881088
if (!tok)
10891089
return false;
10901090
const Token* r = nullptr;
1091+
std::vector<const Token*> aliasValues;
10911092
for (const ReferenceToken& ref : tok->refs()) {
10921093
const bool pointer = astIsPointer(ref.token);
1094+
const bool deref = expr && ref.token != tok && ref.token->isUnaryOp("*") && expr->exprId() == ref.token->astOperand1()->exprId();
1095+
aliasValues.clear();
1096+
for (const ValueFlow::Value& val : ref.token->values()) {
1097+
if (val.isImpossible())
1098+
continue;
1099+
if (val.isLocalLifetimeValue() || (pointer && val.isSymbolicValue() && val.intvalue == 0))
1100+
aliasValues.push_back(val.tokvalue);
1101+
}
1102+
if (!deref && aliasValues.empty())
1103+
continue;
10931104
r = findAstNode(expr, [&](const Token* childTok) {
10941105
if (childTok->exprId() == 0)
10951106
return false;
1096-
if (ref.token != tok && expr->exprId() == childTok->exprId() && ref.token->isUnaryOp("*") && expr->exprId() == ref.token->astOperand1()->exprId()) {
1107+
if (deref && expr->exprId() == childTok->exprId()) {
10971108
if (indirect)
10981109
*indirect = 0;
10991110
return true;
11001111
}
1101-
for (const ValueFlow::Value& val : ref.token->values()) {
1102-
if (val.isImpossible())
1103-
continue;
1104-
if (val.isLocalLifetimeValue() || (pointer && val.isSymbolicValue() && val.intvalue == 0)) {
1105-
if (findAstNode(val.tokvalue,
1106-
[&](const Token* aliasTok) {
1107-
return aliasTok != childTok && aliasTok->exprId() == childTok->exprId();
1108-
})) {
1109-
return true;
1110-
}
1111-
}
1112-
}
1113-
return false;
1112+
return std::any_of(aliasValues.cbegin(), aliasValues.cend(), [&](const Token* aliasValue) {
1113+
return findAstNode(aliasValue, [&](const Token* aliasTok) {
1114+
return aliasTok != childTok && aliasTok->exprId() == childTok->exprId();
1115+
}) != nullptr;
1116+
});
11141117
});
11151118
if (r)
11161119
break;

‎test/testvalueflow.cpp‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8188,6 +8188,20 @@ class TestValueFlow : public TestFixture {
81888188
void valueFlowCrash() {
81898189
const char* code;
81908190

8191+
code = "struct N {\n" // isAliasOf() was called with expr=nullptr
8192+
" N* r;\n"
8193+
" N root() const;\n"
8194+
" N f(const char* path, char d) const;\n"
8195+
"};\n"
8196+
"N N::f(const char* path, char d) const {\n"
8197+
" N c = path[0] == d ? root() : *this;\n"
8198+
" if (!c.r) return N();\n"
8199+
" const char* p = path;\n"
8200+
" while (*p == d) ++p;\n"
8201+
" return c;\n"
8202+
"}\n";
8203+
(void)valueOfTok(code, "p");
8204+
81918205
code = "void f(int x) {\n"
81928206
" if (0 * (x > 2)) {}\n"
81938207
"}\n";

0 commit comments

Comments
 (0)