Skip to content

Commit 9fcdbb5

Browse files
committed
refs #10765 - Token: introduced cache for isAliasOf() calls
1 parent fdd7b1d commit 9fcdbb5

3 files changed

Lines changed: 22 additions & 1 deletion

File tree

lib/astutils.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2947,7 +2947,7 @@ static bool isExpressionChangedAt(const F& getExprTok,
29472947
Token::Match(tok->astOperand2() ? tok->astOperand2() : tok->astOperand1(), "%varid%", expr->varId()))
29482948
aliased = true;
29492949
if (!aliased)
2950-
aliased = isAliasOf(tok, expr, &i);
2950+
aliased = tok->isAliasOf(expr, &i);
29512951
if (!aliased)
29522952
return false;
29532953
i += indirect;

lib/token.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2753,3 +2753,19 @@ const SmallVector<ReferenceToken>& Token::refs(bool temporary) const
27532753
mImpl->mRefs.reset(new SmallVector<ReferenceToken>(followAllReferences(this, false)));
27542754
return *mImpl->mRefs;
27552755
}
2756+
2757+
bool Token::isAliasOf(const Token* expr, int* indirect) const
2758+
{
2759+
const auto it = utils::as_const(mImpl->mAliases).find(expr);
2760+
if (it != mImpl->mAliases.cend()) {
2761+
if (indirect)
2762+
*indirect = it->second.second;
2763+
return it->second.first;
2764+
}
2765+
int i = 0;
2766+
const bool b = ::isAliasOf(this, expr, &i);
2767+
mImpl->mAliases[expr] = std::make_pair(b, i);
2768+
if (indirect)
2769+
*indirect = i;
2770+
return b;
2771+
}

lib/token.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
#include <set>
4242
#include <string>
4343
#include <type_traits>
44+
#include <unordered_map>
4445
#include <utility>
4546
#include <vector>
4647

@@ -173,6 +174,8 @@ class CPPCHECKLIB Token {
173174
std::unique_ptr<SmallVector<ReferenceToken>> mRefs;
174175
std::unique_ptr<SmallVector<ReferenceToken>> mRefsTemp;
175176

177+
std::unordered_map<const Token*, std::pair<bool, int>> mAliases;
178+
176179
void setCppcheckAttribute(CppcheckAttributesType type, MathLib::bigint value);
177180
bool getCppcheckAttribute(CppcheckAttributesType type, MathLib::bigint &value) const;
178181

@@ -1364,6 +1367,8 @@ class CPPCHECKLIB Token {
13641367
// provides and caches result of a followAllReferences() call
13651368
const SmallVector<ReferenceToken>& refs(bool temporary = true) const;
13661369

1370+
bool isAliasOf(const Token* expr, int* indirect) const;
1371+
13671372
/**
13681373
* Sets the original name.
13691374
*/

0 commit comments

Comments
 (0)