Skip to content

Commit f624a90

Browse files
committed
[ASTMatchers] AST matcher support for ObjC pointers
Add `ObjCInterfaceDecl` to the list of types supported by the `hasType` and `hasDeclaration` matchers, `ObjCObjectPointerType` to the list of types supported by `pointee`.
1 parent def22f4 commit f624a90

File tree

5 files changed

+24
-4
lines changed

5 files changed

+24
-4
lines changed

clang/docs/ReleaseNotes.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -885,6 +885,10 @@ AST Matchers
885885
- Ensure ``hasName`` matches template specializations across inline namespaces,
886886
making `matchesNodeFullSlow` and `matchesNodeFullFast` consistent.
887887

888+
- Ensure ``hasType`` and ``hasDeclaration`` match Objective-C interface declarations.
889+
890+
- Ensure ``pointee`` matches Objective-C pointer types.
891+
888892
clang-format
889893
------------
890894

clang/include/clang/ASTMatchers/ASTMatchers.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4033,7 +4033,7 @@ AST_POLYMORPHIC_MATCHER_P_OVERLOAD(
40334033
AST_POLYMORPHIC_MATCHER_P_OVERLOAD(
40344034
hasType,
40354035
AST_POLYMORPHIC_SUPPORTED_TYPES(Expr, FriendDecl, ValueDecl,
4036-
CXXBaseSpecifier),
4036+
CXXBaseSpecifier, ObjCInterfaceDecl),
40374037
internal::Matcher<Decl>, InnerMatcher, 1) {
40384038
QualType QT = internal::getUnderlyingType(Node);
40394039
if (!QT.isNull())
@@ -7434,7 +7434,8 @@ extern const AstTypeMatcher<RValueReferenceType> rValueReferenceType;
74347434
AST_TYPELOC_TRAVERSE_MATCHER_DECL(
74357435
pointee, getPointee,
74367436
AST_POLYMORPHIC_SUPPORTED_TYPES(BlockPointerType, MemberPointerType,
7437-
PointerType, ReferenceType));
7437+
PointerType, ReferenceType,
7438+
ObjCObjectPointerType));
74387439

74397440
/// Matches typedef types.
74407441
///

clang/include/clang/ASTMatchers/ASTMatchersInternal.h

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,9 @@ inline QualType getUnderlyingType(const FriendDecl &Node) {
161161
inline QualType getUnderlyingType(const CXXBaseSpecifier &Node) {
162162
return Node.getType();
163163
}
164+
inline QualType getUnderlyingType(const ObjCInterfaceDecl &Node) {
165+
return Node.getTypeForDecl()->getPointeeType();
166+
}
164167

165168
/// Unifies obtaining a `TypeSourceInfo` from different node types.
166169
template <typename T,
@@ -1113,6 +1116,11 @@ class HasDeclarationMatcher : public MatcherInterface<T> {
11131116
return matchesDecl(Node.getDecl(), Finder, Builder);
11141117
}
11151118

1119+
bool matchesSpecialized(const ObjCInterfaceDecl &Node, ASTMatchFinder *Finder,
1120+
BoundNodesTreeBuilder *Builder) const {
1121+
return matchesDecl(Node.getCanonicalDecl(), Finder, Builder);
1122+
}
1123+
11161124
/// Extracts the operator new of the new call and returns whether the
11171125
/// inner matcher matches on it.
11181126
bool matchesSpecialized(const CXXNewExpr &Node,
@@ -1213,7 +1221,7 @@ using HasDeclarationSupportedTypes =
12131221
ElaboratedType, InjectedClassNameType, LabelStmt, AddrLabelExpr,
12141222
MemberExpr, QualType, RecordType, TagType,
12151223
TemplateSpecializationType, TemplateTypeParmType, TypedefType,
1216-
UnresolvedUsingType, ObjCIvarRefExpr>;
1224+
UnresolvedUsingType, ObjCIvarRefExpr, ObjCInterfaceDecl>;
12171225

12181226
/// A Matcher that allows binding the node it matches to an id.
12191227
///

clang/lib/ASTMatchers/ASTMatchersInternal.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1097,7 +1097,8 @@ AST_TYPELOC_TRAVERSE_MATCHER_DEF(hasValueType,
10971097
AST_TYPELOC_TRAVERSE_MATCHER_DEF(
10981098
pointee,
10991099
AST_POLYMORPHIC_SUPPORTED_TYPES(BlockPointerType, MemberPointerType,
1100-
PointerType, ReferenceType));
1100+
PointerType, ReferenceType,
1101+
ObjCObjectPointerType));
11011102

11021103
const internal::VariadicDynCastAllOfMatcher<Stmt, OMPExecutableDirective>
11031104
ompExecutableDirective;

clang/unittests/ASTMatchers/ASTMatchersTraversalTest.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,12 @@ TEST(HasDeclaration, HasDeclarationOfTypeAlias) {
283283
hasDeclaration(typeAliasTemplateDecl()))))))));
284284
}
285285

286+
TEST(HasDeclaration, HasDeclarationOfObjCInterface) {
287+
EXPECT_TRUE(matchesObjC("@interface BaseClass @end void f() {BaseClass* b;}",
288+
varDecl(hasType(objcObjectPointerType(
289+
pointee(hasDeclaration(objcInterfaceDecl())))))));
290+
}
291+
286292
TEST(HasUnqualifiedDesugaredType, DesugarsUsing) {
287293
EXPECT_TRUE(
288294
matches("struct A {}; using B = A; B b;",

0 commit comments

Comments
 (0)