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 substTemplateTypeParmType()))));
64 auto ConstReferenceOrValueOrReplaced = qualType(anyOf(
65 ConstReferenceOrValue,
66 substTemplateTypeParmType(hasReplacementType(ConstReferenceOrValue))));
67 auto UsedAsConstRefOrValueArg = forEachArgumentWithParam(
68 DeclRefToVar, parmVarDecl(hasType(ConstReferenceOrValueOrReplaced)));
69 Matches = match(findAll(callExpr(UsedAsConstRefOrValueArg)), Stmt, Context);
70 extractNodesByIdTo(Matches, "declRef", DeclRefs);
71 Matches =
72 match(findAll(cxxConstructExpr(UsedAsConstRefOrValueArg)), Stmt, Context);
73 extractNodesByIdTo(Matches, "declRef", DeclRefs);
74 return DeclRefs;
75 }
76
isOnlyUsedAsConst(const VarDecl & Var,const Stmt & Stmt,ASTContext & Context)77 bool isOnlyUsedAsConst(const VarDecl &Var, const Stmt &Stmt,
78 ASTContext &Context) {
79 // Collect all DeclRefExprs to the loop variable and all CallExprs and
80 // CXXConstructExprs where the loop variable is used as argument to a const
81 // reference parameter.
82 // If the difference is empty it is safe for the loop variable to be a const
83 // reference.
84 auto AllDeclRefs = allDeclRefExprs(Var, Stmt, Context);
85 auto ConstReferenceDeclRefs = constReferenceDeclRefExprs(Var, Stmt, Context);
86 return isSetDifferenceEmpty(AllDeclRefs, ConstReferenceDeclRefs);
87 }
88
89 SmallPtrSet<const DeclRefExpr *, 16>
allDeclRefExprs(const VarDecl & VarDecl,const Stmt & Stmt,ASTContext & Context)90 allDeclRefExprs(const VarDecl &VarDecl, const Stmt &Stmt, ASTContext &Context) {
91 auto Matches = match(
92 findAll(declRefExpr(to(varDecl(equalsNode(&VarDecl)))).bind("declRef")),
93 Stmt, Context);
94 SmallPtrSet<const DeclRefExpr *, 16> DeclRefs;
95 extractNodesByIdTo(Matches, "declRef", DeclRefs);
96 return DeclRefs;
97 }
98
99 SmallPtrSet<const DeclRefExpr *, 16>
allDeclRefExprs(const VarDecl & VarDecl,const Decl & Decl,ASTContext & Context)100 allDeclRefExprs(const VarDecl &VarDecl, const Decl &Decl, ASTContext &Context) {
101 auto Matches = match(
102 decl(forEachDescendant(
103 declRefExpr(to(varDecl(equalsNode(&VarDecl)))).bind("declRef"))),
104 Decl, Context);
105 SmallPtrSet<const DeclRefExpr *, 16> DeclRefs;
106 extractNodesByIdTo(Matches, "declRef", DeclRefs);
107 return DeclRefs;
108 }
109
isCopyConstructorArgument(const DeclRefExpr & DeclRef,const Decl & Decl,ASTContext & Context)110 bool isCopyConstructorArgument(const DeclRefExpr &DeclRef, const Decl &Decl,
111 ASTContext &Context) {
112 auto UsedAsConstRefArg = forEachArgumentWithParam(
113 declRefExpr(equalsNode(&DeclRef)),
114 parmVarDecl(hasType(matchers::isReferenceToConst())));
115 auto Matches = match(
116 decl(hasDescendant(
117 cxxConstructExpr(UsedAsConstRefArg, hasDeclaration(cxxConstructorDecl(
118 isCopyConstructor())))
119 .bind("constructExpr"))),
120 Decl, Context);
121 return !Matches.empty();
122 }
123
isCopyAssignmentArgument(const DeclRefExpr & DeclRef,const Decl & Decl,ASTContext & Context)124 bool isCopyAssignmentArgument(const DeclRefExpr &DeclRef, const Decl &Decl,
125 ASTContext &Context) {
126 auto UsedAsConstRefArg = forEachArgumentWithParam(
127 declRefExpr(equalsNode(&DeclRef)),
128 parmVarDecl(hasType(matchers::isReferenceToConst())));
129 auto Matches = match(
130 decl(hasDescendant(
131 cxxOperatorCallExpr(UsedAsConstRefArg, hasOverloadedOperatorName("="),
132 callee(cxxMethodDecl(isCopyAssignmentOperator())))
133 .bind("operatorCallExpr"))),
134 Decl, Context);
135 return !Matches.empty();
136 }
137
138 } // namespace decl_ref_expr
139 } // namespace utils
140 } // namespace tidy
141 } // namespace clang
142