11 #include "clang/AST/ASTContext.h"
12 #include "clang/AST/DeclCXX.h"
13 #include "clang/ASTMatchers/ASTMatchFinder.h"
18 namespace decl_ref_expr {
21 using llvm::SmallPtrSet;
25 template <
typename S>
bool isSetDifferenceEmpty(
const S &S1,
const S &S2) {
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));
44 SmallPtrSet<const DeclRefExpr *, 16>
46 ASTContext &Context) {
48 declRefExpr(to(varDecl(equalsNode(&VarDecl)))).bind(
"declRef");
49 auto ConstMethodCallee = callee(cxxMethodDecl(isConst()));
54 findAll(expr(anyOf(cxxMemberCallExpr(ConstMethodCallee, on(DeclRefToVar)),
55 cxxOperatorCallExpr(ConstMethodCallee,
56 hasArgument(0, DeclRefToVar))))),
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);
68 match(findAll(cxxConstructExpr(UsedAsConstRefOrValueArg)), Stmt, Context);
69 extractNodesByIdTo(Matches,
"declRef", DeclRefs);
74 ASTContext &Context) {
82 return isSetDifferenceEmpty(AllDeclRefs, ConstReferenceDeclRefs);
85 SmallPtrSet<const DeclRefExpr *, 16>
88 findAll(declRefExpr(to(varDecl(equalsNode(&VarDecl)))).bind(
"declRef")),
90 SmallPtrSet<const DeclRefExpr *, 16> DeclRefs;
91 extractNodesByIdTo(Matches,
"declRef", DeclRefs);
95 SmallPtrSet<const DeclRefExpr *, 16>
98 decl(forEachDescendant(
99 declRefExpr(to(varDecl(equalsNode(&VarDecl)))).bind(
"declRef"))),
101 SmallPtrSet<const DeclRefExpr *, 16> DeclRefs;
102 extractNodesByIdTo(Matches,
"declRef", DeclRefs);
107 ASTContext &Context) {
108 auto UsedAsConstRefArg = forEachArgumentWithParam(
109 declRefExpr(equalsNode(&
DeclRef)),
110 parmVarDecl(hasType(matchers::isReferenceToConst())));
111 auto Matches =
match(
113 cxxConstructExpr(UsedAsConstRefArg, hasDeclaration(cxxConstructorDecl(
114 isCopyConstructor())))
115 .bind(
"constructExpr"))),
117 return !Matches.empty();
121 ASTContext &Context) {
122 auto UsedAsConstRefArg = forEachArgumentWithParam(
123 declRefExpr(equalsNode(&
DeclRef)),
124 parmVarDecl(hasType(matchers::isReferenceToConst())));
125 auto Matches =
match(
127 cxxOperatorCallExpr(UsedAsConstRefArg, hasOverloadedOperatorName(
"="),
128 callee(cxxMethodDecl(isCopyAssignmentOperator())))
129 .bind(
"operatorCallExpr"))),
131 return !Matches.empty();