clang-tools  9.0.0
DeclRefExprUtils.cpp
Go to the documentation of this file.
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 
25 template <typename S> bool isSetDifferenceEmpty(const S &S1, const S &S2) {
26  for (const 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>
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>
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 
73 // Finds all DeclRefExprs where a const method is called on VarDecl or VarDecl
74 // is the a const reference or value argument to a CallExpr or CXXConstructExpr.
75 SmallPtrSet<const DeclRefExpr *, 16>
76 constReferenceDeclRefExprs(const VarDecl &VarDecl, const Decl &Decl,
77  ASTContext &Context) {
78  auto DeclRefToVar =
79  declRefExpr(to(varDecl(equalsNode(&VarDecl)))).bind("declRef");
80  auto ConstMethodCallee = callee(cxxMethodDecl(isConst()));
81  // Match method call expressions where the variable is referenced as the this
82  // implicit object argument and opertor call expression for member operators
83  // where the variable is the 0-th argument.
84  auto Matches =
85  match(decl(forEachDescendant(expr(
86  anyOf(cxxMemberCallExpr(ConstMethodCallee, on(DeclRefToVar)),
87  cxxOperatorCallExpr(ConstMethodCallee,
88  hasArgument(0, DeclRefToVar)))))),
89  Decl, Context);
90  SmallPtrSet<const DeclRefExpr *, 16> DeclRefs;
91  extractNodesByIdTo(Matches, "declRef", DeclRefs);
92  auto ConstReferenceOrValue =
93  qualType(anyOf(referenceType(pointee(qualType(isConstQualified()))),
94  unless(anyOf(referenceType(), pointerType()))));
95  auto UsedAsConstRefOrValueArg = forEachArgumentWithParam(
96  DeclRefToVar, parmVarDecl(hasType(ConstReferenceOrValue)));
97  Matches = match(decl(forEachDescendant(callExpr(UsedAsConstRefOrValueArg))),
98  Decl, Context);
99  extractNodesByIdTo(Matches, "declRef", DeclRefs);
100  Matches =
101  match(decl(forEachDescendant(cxxConstructExpr(UsedAsConstRefOrValueArg))),
102  Decl, Context);
103  extractNodesByIdTo(Matches, "declRef", DeclRefs);
104  return DeclRefs;
105 }
106 
107 bool isOnlyUsedAsConst(const VarDecl &Var, const Stmt &Stmt,
108  ASTContext &Context) {
109  // Collect all DeclRefExprs to the loop variable and all CallExprs and
110  // CXXConstructExprs where the loop variable is used as argument to a const
111  // reference parameter.
112  // If the difference is empty it is safe for the loop variable to be a const
113  // reference.
114  auto AllDeclRefs = allDeclRefExprs(Var, Stmt, Context);
115  auto ConstReferenceDeclRefs = constReferenceDeclRefExprs(Var, Stmt, Context);
116  return isSetDifferenceEmpty(AllDeclRefs, ConstReferenceDeclRefs);
117 }
118 
119 SmallPtrSet<const DeclRefExpr *, 16>
120 allDeclRefExprs(const VarDecl &VarDecl, const Stmt &Stmt, ASTContext &Context) {
121  auto Matches = match(
122  findAll(declRefExpr(to(varDecl(equalsNode(&VarDecl)))).bind("declRef")),
123  Stmt, Context);
124  SmallPtrSet<const DeclRefExpr *, 16> DeclRefs;
125  extractNodesByIdTo(Matches, "declRef", DeclRefs);
126  return DeclRefs;
127 }
128 
129 SmallPtrSet<const DeclRefExpr *, 16>
130 allDeclRefExprs(const VarDecl &VarDecl, const Decl &Decl, ASTContext &Context) {
131  auto Matches = match(
132  decl(forEachDescendant(
133  declRefExpr(to(varDecl(equalsNode(&VarDecl)))).bind("declRef"))),
134  Decl, Context);
135  SmallPtrSet<const DeclRefExpr *, 16> DeclRefs;
136  extractNodesByIdTo(Matches, "declRef", DeclRefs);
137  return DeclRefs;
138 }
139 
140 bool isCopyConstructorArgument(const DeclRefExpr &DeclRef, const Decl &Decl,
141  ASTContext &Context) {
142  auto UsedAsConstRefArg = forEachArgumentWithParam(
143  declRefExpr(equalsNode(&DeclRef)),
144  parmVarDecl(hasType(matchers::isReferenceToConst())));
145  auto Matches = match(
146  decl(hasDescendant(
147  cxxConstructExpr(UsedAsConstRefArg, hasDeclaration(cxxConstructorDecl(
148  isCopyConstructor())))
149  .bind("constructExpr"))),
150  Decl, Context);
151  return !Matches.empty();
152 }
153 
154 bool isCopyAssignmentArgument(const DeclRefExpr &DeclRef, const Decl &Decl,
155  ASTContext &Context) {
156  auto UsedAsConstRefArg = forEachArgumentWithParam(
157  declRefExpr(equalsNode(&DeclRef)),
158  parmVarDecl(hasType(matchers::isReferenceToConst())));
159  auto Matches = match(
160  decl(hasDescendant(
161  cxxOperatorCallExpr(UsedAsConstRefArg, hasOverloadedOperatorName("="),
162  callee(cxxMethodDecl(isCopyAssignmentOperator())))
163  .bind("operatorCallExpr"))),
164  Decl, Context);
165  return !Matches.empty();
166 }
167 
168 } // namespace decl_ref_expr
169 } // namespace utils
170 } // namespace tidy
171 } // namespace clang
SmallPtrSet< const DeclRefExpr *, 16 > allDeclRefExprs(const VarDecl &VarDecl, const Stmt &Stmt, ASTContext &Context)
Returns set of all DeclRefExprs to VarDecl within Stmt.
bool isCopyConstructorArgument(const DeclRefExpr &DeclRef, const Decl &Decl, ASTContext &Context)
Returns true if DeclRefExpr is the argument of a copy-constructor call expression within Decl...
bool isOnlyUsedAsConst(const VarDecl &Var, const Stmt &Stmt, ASTContext &Context)
Returns true if all DeclRefExpr to the variable within Stmt do not modify it.
std::vector< std::string > match(const SymbolIndex &I, const FuzzyFindRequest &Req, bool *Incomplete)
Definition: TestIndex.cpp:94
SmallPtrSet< const DeclRefExpr *, 16 > constReferenceDeclRefExprs(const VarDecl &VarDecl, const Stmt &Stmt, ASTContext &Context)
Returns set of all DeclRefExprs to VarDecl within Stmt where VarDecl is guaranteed to be accessed in ...
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
bool isCopyAssignmentArgument(const DeclRefExpr &DeclRef, const Decl &Decl, ASTContext &Context)
Returns true if DeclRefExpr is the argument of a copy-assignment operator CallExpr within Decl...
const DeclRefExpr * DeclRef