clang 22.0.0git
SmartPointerAccessorCaching.cpp
Go to the documentation of this file.
2
4#include "clang/AST/DeclCXX.h"
5#include "clang/AST/Type.h"
9
10namespace clang::dataflow {
11
12namespace {
13
14using ast_matchers::callee;
18using ast_matchers::hasCanonicalType;
21using ast_matchers::ofClass;
22using ast_matchers::parameterCountIs;
25using ast_matchers::returns;
26
27CanQualType getLikeReturnType(QualType RT) {
28 if (!RT.isNull() && RT->isPointerType()) {
29 return RT->getPointeeType()->getCanonicalTypeUnqualified();
30 }
31 return {};
32}
33
34CanQualType valueLikeReturnType(QualType RT) {
35 if (!RT.isNull() && RT->isReferenceType()) {
36 return RT.getNonReferenceType()->getCanonicalTypeUnqualified();
37 }
38 return {};
39}
40
41CanQualType pointerLikeReturnType(const CXXRecordDecl &RD) {
42 // We may want to cache this search, but in current profiles it hasn't shown
43 // up as a hot spot (possibly because there aren't many hits, relatively).
44 CanQualType StarReturnType, ArrowReturnType;
45 for (const auto *MD : RD.methods()) {
46 // We only consider methods that are const and have zero parameters.
47 // It may be that there is a non-const overload for the method, but
48 // there should at least be a const overload as well.
49 if (!MD->isConst() || MD->getNumParams() != 0)
50 continue;
51 switch (MD->getOverloadedOperator()) {
52 case OO_Star:
53 StarReturnType = valueLikeReturnType(MD->getReturnType());
54 break;
55 case OO_Arrow:
56 ArrowReturnType = getLikeReturnType(MD->getReturnType());
57 break;
58 default:
59 break;
60 }
61 }
62 if (!StarReturnType.isNull() && !ArrowReturnType.isNull() &&
63 StarReturnType == ArrowReturnType)
64 return StarReturnType;
65
66 return {};
67}
68
69QualType findReturnType(const CXXRecordDecl &RD, StringRef MethodName) {
70 for (const auto *MD : RD.methods()) {
71 // We only consider methods that are const and have zero parameters.
72 // It may be that there is a non-const overload for the method, but
73 // there should at least be a const overload as well.
74 if (!MD->isConst() || MD->getNumParams() != 0 ||
75 MD->getOverloadedOperator() != OO_None)
76 continue;
77 clang::IdentifierInfo *II = MD->getIdentifier();
78 if (II && II->isStr(MethodName))
79 return MD->getReturnType();
80 }
81 return {};
82}
83
84} // namespace
85} // namespace clang::dataflow
86
87// AST_MATCHER macros create an "internal" namespace, so we put it in
88// its own anonymous namespace instead of in clang::dataflow.
89namespace {
90
91using clang::dataflow::findReturnType;
92using clang::dataflow::getLikeReturnType;
93using clang::dataflow::pointerLikeReturnType;
94using clang::dataflow::valueLikeReturnType;
95
96AST_MATCHER_P(clang::CXXRecordDecl, smartPointerClassWithGetLike,
97 clang::StringRef, MethodName) {
98 auto RT = pointerLikeReturnType(Node);
99 if (RT.isNull())
100 return false;
101 return getLikeReturnType(findReturnType(Node, MethodName)) == RT;
102}
103
104AST_MATCHER_P(clang::CXXRecordDecl, smartPointerClassWithValueLike,
105 clang::StringRef, MethodName) {
106 auto RT = pointerLikeReturnType(Node);
107 if (RT.isNull())
108 return false;
109 return valueLikeReturnType(findReturnType(Node, MethodName)) == RT;
110}
111
112AST_MATCHER(clang::CXXRecordDecl, smartPointerClassWithGetOrValue) {
113 auto RT = pointerLikeReturnType(Node);
114 if (RT.isNull())
115 return false;
116 return getLikeReturnType(findReturnType(Node, "get")) == RT ||
117 valueLikeReturnType(findReturnType(Node, "value")) == RT;
118}
119
120AST_MATCHER(clang::CXXRecordDecl, pointerClass) {
121 return !pointerLikeReturnType(Node).isNull();
122}
123
124} // namespace
125
126namespace clang::dataflow {
127
129 return cxxOperatorCallExpr(
131 callee(cxxMethodDecl(parameterCountIs(0),
132 returns(hasCanonicalType(referenceType())),
133 ofClass(smartPointerClassWithGetOrValue()))));
134}
135
137 return cxxOperatorCallExpr(
139 callee(cxxMethodDecl(parameterCountIs(0),
140 returns(hasCanonicalType(pointerType())),
141 ofClass(smartPointerClassWithGetOrValue()))));
142}
143
145 return cxxOperatorCallExpr(
147 callee(cxxMethodDecl(parameterCountIs(0),
148 returns(hasCanonicalType(referenceType())),
149 ofClass(pointerClass()))));
150}
151
153 return cxxOperatorCallExpr(
155 callee(cxxMethodDecl(parameterCountIs(0),
156 returns(hasCanonicalType(pointerType())),
157 ofClass(pointerClass()))));
158}
159
161isSmartPointerLikeValueMethodCall(clang::StringRef MethodName) {
162 return cxxMemberCallExpr(callee(cxxMethodDecl(
163 parameterCountIs(0), returns(hasCanonicalType(referenceType())),
164 hasName(MethodName),
165 ofClass(smartPointerClassWithValueLike(MethodName)))));
166}
167
169isSmartPointerLikeGetMethodCall(clang::StringRef MethodName) {
170 return cxxMemberCallExpr(callee(cxxMethodDecl(
171 parameterCountIs(0), returns(hasCanonicalType(pointerType())),
172 hasName(MethodName), ofClass(smartPointerClassWithGetLike(MethodName)))));
173}
174
175const FunctionDecl *
177 const FunctionDecl *CanonicalCallee = nullptr;
178 const CXXMethodDecl *Callee =
179 cast_or_null<CXXMethodDecl>(CE->getDirectCallee());
180 if (Callee == nullptr)
181 return nullptr;
182 const CXXRecordDecl *RD = Callee->getParent();
183 if (RD == nullptr)
184 return nullptr;
185 for (const auto *MD : RD->methods()) {
186 if (MD->getOverloadedOperator() == OO_Star && MD->isConst() &&
187 MD->getNumParams() == 0 && MD->getReturnType()->isReferenceType()) {
188 CanonicalCallee = MD;
189 break;
190 }
191 }
192 return CanonicalCallee;
193}
194
195} // namespace clang::dataflow
#define AST_MATCHER(Type, DefineMatcher)
AST_MATCHER(Type, DefineMatcher) { ... } defines a zero parameter function named DefineMatcher() that...
#define AST_MATCHER_P(Type, DefineMatcher, ParamType, Param)
AST_MATCHER_P(Type, DefineMatcher, ParamType, Param) { ... } defines a single-parameter function name...
Defines the C++ Decl subclasses, other than those for templates (found in DeclTemplate....
Defines an enumeration for C++ overloaded operators.
C Language Family Type Representation.
Represents a static or instance method of a struct/union/class.
Definition DeclCXX.h:2129
Represents a C++ struct/union/class.
Definition DeclCXX.h:258
method_range methods() const
Definition DeclCXX.h:650
CallExpr - Represents a function call (C99 6.5.2.2, C++ [expr.call]).
Definition Expr.h:2943
FunctionDecl * getDirectCallee()
If the callee is a FunctionDecl, return it. Otherwise return null.
Definition Expr.h:3126
Represents a function declaration or definition.
Definition Decl.h:2000
bool isStr(const char(&Str)[StrLen]) const
Return true if this is the identifier for the specified string.
internal::Matcher< NamedDecl > hasName(StringRef Name)
Matches NamedDecl nodes that have the specified name.
const internal::VariadicDynCastAllOfMatcher< Stmt, CXXMemberCallExpr > cxxMemberCallExpr
Matches member call expressions.
internal::Matcher< Stmt > StatementMatcher
const internal::VariadicDynCastAllOfMatcher< Stmt, CXXOperatorCallExpr > cxxOperatorCallExpr
Matches overloaded operator calls.
const AstTypeMatcher< PointerType > pointerType
internal::PolymorphicMatcher< internal::HasOverloadedOperatorNameMatcher, AST_POLYMORPHIC_SUPPORTED_TYPES(CXXOperatorCallExpr, FunctionDecl), std::vector< std::string > > hasOverloadedOperatorName(StringRef Name)
Matches overloaded operator names.
const internal::VariadicDynCastAllOfMatcher< Decl, CXXMethodDecl > cxxMethodDecl
Matches method declarations.
const AstTypeMatcher< ReferenceType > referenceType
Dataflow Directional Tag Classes.
Definition AdornedCFG.h:29
ast_matchers::StatementMatcher isSmartPointerLikeValueMethodCall(clang::StringRef MethodName="value")
ast_matchers::StatementMatcher isPointerLikeOperatorStar()
Matchers: For now, these match on any class with an operator* or operator-> where the return types ha...
internal::Matcher< NamedDecl > hasName(StringRef Name)
Matches NamedDecl nodes that have the specified name.
const internal::VariadicDynCastAllOfMatcher< Stmt, CXXMemberCallExpr > cxxMemberCallExpr
Matches member call expressions.
ast_matchers::StatementMatcher isSmartPointerLikeOperatorArrow()
const internal::VariadicDynCastAllOfMatcher< Stmt, CXXOperatorCallExpr > cxxOperatorCallExpr
Matches overloaded operator calls.
const AstTypeMatcher< PointerType > pointerType
internal::PolymorphicMatcher< internal::HasOverloadedOperatorNameMatcher, AST_POLYMORPHIC_SUPPORTED_TYPES(CXXOperatorCallExpr, FunctionDecl), std::vector< std::string > > hasOverloadedOperatorName(StringRef Name)
Matches overloaded operator names.
const FunctionDecl * getCanonicalSmartPointerLikeOperatorCallee(const CallExpr *CE)
Returns the "canonical" callee for smart pointer operators (* and ->) as a key for caching.
ast_matchers::StatementMatcher isSmartPointerLikeOperatorStar()
const internal::VariadicDynCastAllOfMatcher< Decl, CXXMethodDecl > cxxMethodDecl
Matches method declarations.
ast_matchers::StatementMatcher isSmartPointerLikeGetMethodCall(clang::StringRef MethodName="get")
ast_matchers::StatementMatcher isPointerLikeOperatorArrow()
const AstTypeMatcher< ReferenceType > referenceType
CanQual< Type > CanQualType
Represents a canonical, potentially-qualified type.
@ OO_None
Not an overloaded operator.