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);
75 SmallPtrSet<const DeclRefExpr *, 16>
77 ASTContext &Context) {
79 declRefExpr(to(varDecl(equalsNode(&VarDecl)))).bind(
"declRef");
80 auto ConstMethodCallee = callee(cxxMethodDecl(isConst()));
85 match(decl(forEachDescendant(expr(
86 anyOf(cxxMemberCallExpr(ConstMethodCallee, on(DeclRefToVar)),
87 cxxOperatorCallExpr(ConstMethodCallee,
88 hasArgument(0, DeclRefToVar)))))),
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))),
99 extractNodesByIdTo(Matches,
"declRef", DeclRefs);
101 match(decl(forEachDescendant(cxxConstructExpr(UsedAsConstRefOrValueArg))),
103 extractNodesByIdTo(Matches,
"declRef", DeclRefs);
108 ASTContext &Context) {
116 return isSetDifferenceEmpty(AllDeclRefs, ConstReferenceDeclRefs);
119 SmallPtrSet<const DeclRefExpr *, 16>
121 auto Matches =
match(
122 findAll(declRefExpr(to(varDecl(equalsNode(&VarDecl)))).bind(
"declRef")),
124 SmallPtrSet<const DeclRefExpr *, 16> DeclRefs;
125 extractNodesByIdTo(Matches,
"declRef", DeclRefs);
129 SmallPtrSet<const DeclRefExpr *, 16>
131 auto Matches =
match(
132 decl(forEachDescendant(
133 declRefExpr(to(varDecl(equalsNode(&VarDecl)))).bind(
"declRef"))),
135 SmallPtrSet<const DeclRefExpr *, 16> DeclRefs;
136 extractNodesByIdTo(Matches,
"declRef", DeclRefs);
141 ASTContext &Context) {
142 auto UsedAsConstRefArg = forEachArgumentWithParam(
143 declRefExpr(equalsNode(&DeclRef)),
144 parmVarDecl(hasType(matchers::isReferenceToConst())));
145 auto Matches =
match(
147 cxxConstructExpr(UsedAsConstRefArg, hasDeclaration(cxxConstructorDecl(
148 isCopyConstructor())))
149 .bind(
"constructExpr"))),
151 return !Matches.empty();
155 ASTContext &Context) {
156 auto UsedAsConstRefArg = forEachArgumentWithParam(
157 declRefExpr(equalsNode(&DeclRef)),
158 parmVarDecl(hasType(matchers::isReferenceToConst())));
159 auto Matches =
match(
161 cxxOperatorCallExpr(UsedAsConstRefArg, hasOverloadedOperatorName(
"="),
162 callee(cxxMethodDecl(isCopyAssignmentOperator())))
163 .bind(
"operatorCallExpr"))),
165 return !Matches.empty();
const FunctionDecl * Decl
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)
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