1 //===--- DeclRefExprUtils.cpp - clang-tidy---------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8
9 #include "DeclRefExprUtils.h"
10 #include "Matchers.h"
11 #include "clang/AST/ASTContext.h"
12 #include "clang/AST/DeclCXX.h"
13 #include "clang/ASTMatchers/ASTMatchFinder.h"
14
15 namespace clang {
16 namespace tidy {
17 namespace utils {
18 namespace decl_ref_expr {
19
20 using namespace ::clang::ast_matchers;
21 using llvm::SmallPtrSet;
22
23 namespace {
24
isSetDifferenceEmpty(const S & S1,const S & S2)25 template <typename S> bool isSetDifferenceEmpty(const S &S1, const S &S2) {
26 for (auto E : S1)
27 if (S2.count(E) == 0)
28 return false;
29 return true;
30 }
31
32 // Extracts all Nodes keyed by ID from Matches and inserts them into Nodes.
33 template <typename Node>
extractNodesByIdTo(ArrayRef<BoundNodes> Matches,StringRef ID,SmallPtrSet<const Node *,16> & Nodes)34 void extractNodesByIdTo(ArrayRef<BoundNodes> Matches, StringRef ID,
35 SmallPtrSet<const Node *, 16> &Nodes) {
36 for (const auto &Match : Matches)
37 Nodes.insert(Match.getNodeAs<Node>(ID));
38 }
39
40 } // namespace
41
42 // Finds all DeclRefExprs where a const method is called on VarDecl or VarDecl
43 // is the a const reference or value argument to a CallExpr or CXXConstructExpr.
44 SmallPtrSet<const DeclRefExpr *, 16>
constReferenceDeclRefExprs(const VarDecl & VarDecl,const Stmt & Stmt,ASTContext & Context)45 constReferenceDeclRefExprs(const VarDecl &VarDecl, const Stmt &Stmt,
46 ASTContext &Context) {
47 auto DeclRefToVar =
48 declRefExpr(to(varDecl(equalsNode(&VarDecl)))).bind("declRef");
49 auto ConstMethodCallee = callee(cxxMethodDecl(isConst()));
50 // Match method call expressions where the variable is referenced as the this
51 // implicit object argument and opertor call expression for member operators
52 // where the variable is the 0-th argument.
53 auto Matches = match(
54 findAll(expr(anyOf(cxxMemberCallExpr(ConstMethodCallee, on(DeclRefToVar)),
55 cxxOperatorCallExpr(ConstMethodCallee,
56 hasArgument(0, DeclRefToVar))))),
57 Stmt, Context);
58 SmallPtrSet<const DeclRefExpr *, 16> DeclRefs;
59 extractNodesByIdTo(Matches, "declRef", DeclRefs);
60 auto ConstReferenceOrValue =
61 qualType(anyOf(referenceType(pointee(qualType(isConstQualified()))),
62 unless(anyOf(referenceType(), pointerType()))));
63 auto UsedAsConstRefOrValueArg = forEachArgumentWithParam(
64 DeclRefToVar, parmVarDecl(hasType(ConstReferenceOrValue)));
65 Matches = match(findAll(callExpr(UsedAsConstRefOrValueArg)), Stmt, Context);
66 extractNodesByIdTo(Matches, "declRef", DeclRefs);
67 Matches =
68 match(findAll(cxxConstructExpr(UsedAsConstRefOrValueArg)), Stmt, Context);
69 extractNodesByIdTo(Matches, "declRef", DeclRefs);
70 return DeclRefs;
71 }
72
isOnlyUsedAsConst(const VarDecl & Var,const Stmt & Stmt,ASTContext & Context)73 bool isOnlyUsedAsConst(const VarDecl &Var, const Stmt &Stmt,
74 ASTContext &Context) {
75 // Collect all DeclRefExprs to the loop variable and all CallExprs and
76 // CXXConstructExprs where the loop variable is used as argument to a const
77 // reference parameter.
78 // If the difference is empty it is safe for the loop variable to be a const
79 // reference.
80 auto AllDeclRefs = allDeclRefExprs(Var, Stmt, Context);
81 auto ConstReferenceDeclRefs = constReferenceDeclRefExprs(Var, Stmt, Context);
82 return isSetDifferenceEmpty(AllDeclRefs, ConstReferenceDeclRefs);
83 }
84
85 SmallPtrSet<const DeclRefExpr *, 16>
allDeclRefExprs(const VarDecl & VarDecl,const Stmt & Stmt,ASTContext & Context)86 allDeclRefExprs(const VarDecl &VarDecl, const Stmt &Stmt, ASTContext &Context) {
87 auto Matches = match(
88 findAll(declRefExpr(to(varDecl(equalsNode(&VarDecl)))).bind("declRef")),
89 Stmt, Context);
90 SmallPtrSet<const DeclRefExpr *, 16> DeclRefs;
91 extractNodesByIdTo(Matches, "declRef", DeclRefs);
92 return DeclRefs;
93 }
94
95 SmallPtrSet<const DeclRefExpr *, 16>
allDeclRefExprs(const VarDecl & VarDecl,const Decl & Decl,ASTContext & Context)96 allDeclRefExprs(const VarDecl &VarDecl, const Decl &Decl, ASTContext &Context) {
97 auto Matches = match(
98 decl(forEachDescendant(
99 declRefExpr(to(varDecl(equalsNode(&VarDecl)))).bind("declRef"))),
100 Decl, Context);
101 SmallPtrSet<const DeclRefExpr *, 16> DeclRefs;
102 extractNodesByIdTo(Matches, "declRef", DeclRefs);
103 return DeclRefs;
104 }
105
isCopyConstructorArgument(const DeclRefExpr & DeclRef,const Decl & Decl,ASTContext & Context)106 bool isCopyConstructorArgument(const DeclRefExpr &DeclRef, const Decl &Decl,
107 ASTContext &Context) {
108 auto UsedAsConstRefArg = forEachArgumentWithParam(
109 declRefExpr(equalsNode(&DeclRef)),
110 parmVarDecl(hasType(matchers::isReferenceToConst())));
111 auto Matches = match(
112 decl(hasDescendant(
113 cxxConstructExpr(UsedAsConstRefArg, hasDeclaration(cxxConstructorDecl(
114 isCopyConstructor())))
115 .bind("constructExpr"))),
116 Decl, Context);
117 return !Matches.empty();
118 }
119
isCopyAssignmentArgument(const DeclRefExpr & DeclRef,const Decl & Decl,ASTContext & Context)120 bool isCopyAssignmentArgument(const DeclRefExpr &DeclRef, const Decl &Decl,
121 ASTContext &Context) {
122 auto UsedAsConstRefArg = forEachArgumentWithParam(
123 declRefExpr(equalsNode(&DeclRef)),
124 parmVarDecl(hasType(matchers::isReferenceToConst())));
125 auto Matches = match(
126 decl(hasDescendant(
127 cxxOperatorCallExpr(UsedAsConstRefArg, hasOverloadedOperatorName("="),
128 callee(cxxMethodDecl(isCopyAssignmentOperator())))
129 .bind("operatorCallExpr"))),
130 Decl, Context);
131 return !Matches.empty();
132 }
133
134 } // namespace decl_ref_expr
135 } // namespace utils
136 } // namespace tidy
137 } // namespace clang
138