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(matchers::isReferenceToConst(),
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(invocation(UsedAsConstRefOrValueArg)), Stmt, Context);
70 extractNodesByIdTo(Matches,
"declRef", DeclRefs);
73 match(findAll(declStmt(
74 has(varDecl(hasType(qualType(matchers::isReferenceToConst())),
75 hasInitializer(ignoringImpCasts(DeclRefToVar)))))),
77 extractNodesByIdTo(Matches,
"declRef", DeclRefs);
79 match(findAll(declStmt(has(varDecl(
80 hasType(qualType(matchers::isPointerToConst())),
81 hasInitializer(ignoringImpCasts(unaryOperator(
82 hasOperatorName(
"&"), hasUnaryOperand(DeclRefToVar)))))))),
84 extractNodesByIdTo(Matches,
"declRef", DeclRefs);
89 ASTContext &Context) {
97 return isSetDifferenceEmpty(AllDeclRefs, ConstReferenceDeclRefs);
100 SmallPtrSet<const DeclRefExpr *, 16>
102 auto Matches =
match(
103 findAll(declRefExpr(to(varDecl(equalsNode(&VarDecl)))).bind(
"declRef")),
105 SmallPtrSet<const DeclRefExpr *, 16> DeclRefs;
106 extractNodesByIdTo(Matches,
"declRef", DeclRefs);
110 SmallPtrSet<const DeclRefExpr *, 16>
112 auto Matches =
match(
113 decl(forEachDescendant(
114 declRefExpr(to(varDecl(equalsNode(&VarDecl)))).bind(
"declRef"))),
116 SmallPtrSet<const DeclRefExpr *, 16> DeclRefs;
117 extractNodesByIdTo(Matches,
"declRef", DeclRefs);
122 ASTContext &Context) {
123 auto UsedAsConstRefArg = forEachArgumentWithParam(
124 declRefExpr(equalsNode(&
DeclRef)),
125 parmVarDecl(hasType(matchers::isReferenceToConst())));
126 auto Matches =
match(
128 cxxConstructExpr(UsedAsConstRefArg, hasDeclaration(cxxConstructorDecl(
129 isCopyConstructor())))
130 .bind(
"constructExpr"))),
132 return !Matches.empty();
136 ASTContext &Context) {
137 auto UsedAsConstRefArg = forEachArgumentWithParam(
138 declRefExpr(equalsNode(&
DeclRef)),
139 parmVarDecl(hasType(matchers::isReferenceToConst())));
140 auto Matches =
match(
142 cxxOperatorCallExpr(UsedAsConstRefArg, hasOverloadedOperatorName(
"="),
143 callee(cxxMethodDecl(isCopyAssignmentOperator())))
144 .bind(
"operatorCallExpr"))),
146 return !Matches.empty();