aboutsummaryrefslogtreecommitdiff
path: root/clang-tools-extra/clang-tidy/utils/DeclRefExprUtils.cpp
diff options
context:
space:
mode:
authorShivam Gupta <Shivam.Gupta2@amd.com>2023-07-23 23:41:39 +0530
committerShivam Gupta <Shivam.Gupta2@amd.com>2023-07-24 00:08:29 +0530
commit1e512688376c83d96f097e9b0ddb19132247a646 (patch)
tree8462e9df95d2375beb867d7b934b6c8f440e9ee0 /clang-tools-extra/clang-tidy/utils/DeclRefExprUtils.cpp
parent495bdfc7bb729ba22cf2b51d749f7789b0c2d9af (diff)
downloadllvm-1e512688376c83d96f097e9b0ddb19132247a646.zip
llvm-1e512688376c83d96f097e9b0ddb19132247a646.tar.gz
llvm-1e512688376c83d96f097e9b0ddb19132247a646.tar.bz2
[clang-tidy] performance-* checks: Also allow allow member expressions to be used in a const manner.
Until now when determining all the const uses of a VarDecl we only considered how the variable itself was used. This change extends checking for const usages of the type's members as well. This increases the number of true positives for various performance checks that share the same const usage analysis. Path by Felix Berger Reviewed By: njames93, PiotrZSL Differential Revision: https://reviews.llvm.org/D97567
Diffstat (limited to 'clang-tools-extra/clang-tidy/utils/DeclRefExprUtils.cpp')
-rw-r--r--clang-tools-extra/clang-tidy/utils/DeclRefExprUtils.cpp36
1 files changed, 21 insertions, 15 deletions
diff --git a/clang-tools-extra/clang-tidy/utils/DeclRefExprUtils.cpp b/clang-tools-extra/clang-tidy/utils/DeclRefExprUtils.cpp
index 8915299..2d73179 100644
--- a/clang-tools-extra/clang-tidy/utils/DeclRefExprUtils.cpp
+++ b/clang-tools-extra/clang-tidy/utils/DeclRefExprUtils.cpp
@@ -43,14 +43,19 @@ constReferenceDeclRefExprs(const VarDecl &VarDecl, const Stmt &Stmt,
ASTContext &Context) {
auto DeclRefToVar =
declRefExpr(to(varDecl(equalsNode(&VarDecl)))).bind("declRef");
+ auto MemberExprOfVar = memberExpr(hasObjectExpression(DeclRefToVar));
+ auto DeclRefToVarOrMemberExprOfVar =
+ stmt(anyOf(DeclRefToVar, MemberExprOfVar));
auto ConstMethodCallee = callee(cxxMethodDecl(isConst()));
// Match method call expressions where the variable is referenced as the this
// implicit object argument and operator call expression for member operators
// where the variable is the 0-th argument.
auto Matches = match(
- findAll(expr(anyOf(cxxMemberCallExpr(ConstMethodCallee, on(DeclRefToVar)),
- cxxOperatorCallExpr(ConstMethodCallee,
- hasArgument(0, DeclRefToVar))))),
+ findAll(expr(anyOf(
+ cxxMemberCallExpr(ConstMethodCallee,
+ on(DeclRefToVarOrMemberExprOfVar)),
+ cxxOperatorCallExpr(ConstMethodCallee,
+ hasArgument(0, DeclRefToVarOrMemberExprOfVar))))),
Stmt, Context);
SmallPtrSet<const DeclRefExpr *, 16> DeclRefs;
extractNodesByIdTo(Matches, "declRef", DeclRefs);
@@ -62,22 +67,23 @@ constReferenceDeclRefExprs(const VarDecl &VarDecl, const Stmt &Stmt,
ConstReferenceOrValue,
substTemplateTypeParmType(hasReplacementType(ConstReferenceOrValue))));
auto UsedAsConstRefOrValueArg = forEachArgumentWithParam(
- DeclRefToVar, parmVarDecl(hasType(ConstReferenceOrValueOrReplaced)));
+ DeclRefToVarOrMemberExprOfVar,
+ parmVarDecl(hasType(ConstReferenceOrValueOrReplaced)));
Matches = match(findAll(invocation(UsedAsConstRefOrValueArg)), Stmt, Context);
extractNodesByIdTo(Matches, "declRef", DeclRefs);
// References and pointers to const assignments.
- Matches =
- match(findAll(declStmt(
- has(varDecl(hasType(qualType(matchers::isReferenceToConst())),
- hasInitializer(ignoringImpCasts(DeclRefToVar)))))),
- Stmt, Context);
+ Matches = match(
+ findAll(declStmt(has(varDecl(
+ hasType(qualType(matchers::isReferenceToConst())),
+ hasInitializer(ignoringImpCasts(DeclRefToVarOrMemberExprOfVar)))))),
+ Stmt, Context);
extractNodesByIdTo(Matches, "declRef", DeclRefs);
- Matches =
- match(findAll(declStmt(has(varDecl(
- hasType(qualType(matchers::isPointerToConst())),
- hasInitializer(ignoringImpCasts(unaryOperator(
- hasOperatorName("&"), hasUnaryOperand(DeclRefToVar)))))))),
- Stmt, Context);
+ Matches = match(findAll(declStmt(has(varDecl(
+ hasType(qualType(matchers::isPointerToConst())),
+ hasInitializer(ignoringImpCasts(unaryOperator(
+ hasOperatorName("&"),
+ hasUnaryOperand(DeclRefToVarOrMemberExprOfVar)))))))),
+ Stmt, Context);
extractNodesByIdTo(Matches, "declRef", DeclRefs);
return DeclRefs;
}