1 //===--- DeclRefExprUtils.cpp - clang-tidy---------------------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
10 #include "DeclRefExprUtils.h"
11 #include "clang/AST/ASTContext.h"
12 #include "clang/AST/DeclCXX.h"
13 #include "clang/ASTMatchers/ASTMatchFinder.h"
14 #include "llvm/ADT/SmallPtrSet.h"
15 
16 namespace clang {
17 namespace tidy {
18 namespace decl_ref_expr_utils {
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 // Finds all DeclRefExprs to VarDecl in Stmt.
41 SmallPtrSet<const DeclRefExpr *, 16>
42 declRefExprs(const VarDecl &VarDecl, const Stmt &Stmt, ASTContext &Context) {
43   auto Matches = match(
44       findAll(declRefExpr(to(varDecl(equalsNode(&VarDecl)))).bind("declRef")),
45       Stmt, Context);
46   SmallPtrSet<const DeclRefExpr *, 16> DeclRefs;
47   extractNodesByIdTo(Matches, "declRef", DeclRefs);
48   return DeclRefs;
49 }
50 
51 // Finds all DeclRefExprs where a const method is called on VarDecl or VarDecl
52 // is the a const reference or value argument to a CallExpr or CXXConstructExpr.
53 SmallPtrSet<const DeclRefExpr *, 16>
54 constReferenceDeclRefExprs(const VarDecl &VarDecl, const Stmt &Stmt,
55                            ASTContext &Context) {
56   auto DeclRefToVar =
57       declRefExpr(to(varDecl(equalsNode(&VarDecl)))).bind("declRef");
58   auto ConstMethodCallee = callee(cxxMethodDecl(isConst()));
59   // Match method call expressions where the variable is referenced as the this
60   // implicit object argument and opertor call expression for member operators
61   // where the variable is the 0-th argument.
62   auto Matches = match(
63       findAll(expr(anyOf(cxxMemberCallExpr(ConstMethodCallee, on(DeclRefToVar)),
64                          cxxOperatorCallExpr(ConstMethodCallee,
65                                              hasArgument(0, DeclRefToVar))))),
66       Stmt, Context);
67   SmallPtrSet<const DeclRefExpr *, 16> DeclRefs;
68   extractNodesByIdTo(Matches, "declRef", DeclRefs);
69   auto ConstReferenceOrValue =
70       qualType(anyOf(referenceType(pointee(qualType(isConstQualified()))),
71                      unless(anyOf(referenceType(), pointerType()))));
72   auto UsedAsConstRefOrValueArg = forEachArgumentWithParam(
73       DeclRefToVar, parmVarDecl(hasType(ConstReferenceOrValue)));
74   Matches = match(findAll(callExpr(UsedAsConstRefOrValueArg)), Stmt, Context);
75   extractNodesByIdTo(Matches, "declRef", DeclRefs);
76   Matches =
77       match(findAll(cxxConstructExpr(UsedAsConstRefOrValueArg)), Stmt, Context);
78   extractNodesByIdTo(Matches, "declRef", DeclRefs);
79   return DeclRefs;
80 }
81 
82 } // namespace
83 
84 bool isOnlyUsedAsConst(const VarDecl &Var, const Stmt &Stmt,
85                        ASTContext &Context) {
86   // Collect all DeclRefExprs to the loop variable and all CallExprs and
87   // CXXConstructExprs where the loop variable is used as argument to a const
88   // reference parameter.
89   // If the difference is empty it is safe for the loop variable to be a const
90   // reference.
91   auto AllDeclRefs = declRefExprs(Var, Stmt, Context);
92   auto ConstReferenceDeclRefs = constReferenceDeclRefExprs(Var, Stmt, Context);
93   return isSetDifferenceEmpty(AllDeclRefs, ConstReferenceDeclRefs);
94 }
95 
96 } // namespace decl_ref_expr_utils
97 } // namespace tidy
98 } // namespace clang
99