14using ast_matchers::callee;
18using ast_matchers::hasCanonicalType;
21using ast_matchers::ofClass;
22using ast_matchers::parameterCountIs;
25using ast_matchers::returns;
28 if (!RT.isNull() && RT->isPointerType()) {
29 return RT->getPointeeType()
30 ->getCanonicalTypeUnqualified()
31 .getUnqualifiedType();
37 if (!RT.isNull() && RT->isReferenceType()) {
38 return RT.getNonReferenceType()
39 ->getCanonicalTypeUnqualified()
40 .getUnqualifiedType();
45CanQualType pointerLikeReturnType(
const CXXRecordDecl &RD) {
49 for (
const auto *MD : RD.methods()) {
53 if (!MD->isConst() || MD->getNumParams() != 0)
55 switch (MD->getOverloadedOperator()) {
57 StarReturnType = valueLikeReturnType(MD->getReturnType());
60 ArrowReturnType = getLikeReturnType(MD->getReturnType());
66 if (!StarReturnType.isNull() && !ArrowReturnType.isNull() &&
67 StarReturnType == ArrowReturnType)
68 return StarReturnType;
73QualType findReturnType(
const CXXRecordDecl &RD, StringRef MethodName) {
74 for (
const auto *MD : RD.methods()) {
78 if (!MD->isConst() || MD->getNumParams() != 0 ||
79 MD->getOverloadedOperator() !=
OO_None)
81 clang::IdentifierInfo *II = MD->getIdentifier();
82 if (II && II->
isStr(MethodName))
83 return MD->getReturnType();
95using clang::dataflow::findReturnType;
96using clang::dataflow::getLikeReturnType;
97using clang::dataflow::pointerLikeReturnType;
98using clang::dataflow::valueLikeReturnType;
101 clang::StringRef, MethodName) {
102 auto RT = pointerLikeReturnType(Node);
105 return getLikeReturnType(findReturnType(Node, MethodName)) == RT;
109 clang::StringRef, MethodName) {
110 auto RT = pointerLikeReturnType(Node);
113 return valueLikeReturnType(findReturnType(Node, MethodName)) == RT;
117 auto RT = pointerLikeReturnType(Node);
120 return getLikeReturnType(findReturnType(Node,
"get")) == RT ||
121 valueLikeReturnType(findReturnType(Node,
"value")) == RT;
125 return !pointerLikeReturnType(Node).isNull();
137 ofClass(smartPointerClassWithGetOrValue()))));
145 ofClass(smartPointerClassWithGetOrValue()))));
153 ofClass(pointerClass()))));
161 ofClass(pointerClass()))));
167 parameterCountIs(0), returns(hasCanonicalType(
referenceType())),
169 ofClass(smartPointerClassWithValueLike(MethodName)))));
175 parameterCountIs(0), returns(hasCanonicalType(
pointerType())),
176 hasName(MethodName), ofClass(smartPointerClassWithGetLike(MethodName)))));
184 if (Callee ==
nullptr)
189 for (
const auto *MD : RD->
methods()) {
190 if (MD->getOverloadedOperator() == OO_Star && MD->isConst() &&
191 MD->getNumParams() == 0 && MD->getReturnType()->isReferenceType()) {
192 CanonicalCallee = MD;
196 return CanonicalCallee;
#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.
Represents a C++ struct/union/class.
method_range methods() const
CallExpr - Represents a function call (C99 6.5.2.2, C++ [expr.call]).
FunctionDecl * getDirectCallee()
If the callee is a FunctionDecl, return it. Otherwise return null.
Represents a function declaration or definition.
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.
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.