Skip to content

Commit fdd09e9

Browse files
authoredDec 3, 2024··
[ASTMatchers] AST matcher support for ObjC pointers (#117021)
Add `ObjCInterfaceDecl` to the list of types supported by the `hasType` and `hasDeclaration` matchers, `ObjCObjectPointerType` to the list of types supported by `pointee`. These AST matcher improvements will help the new WebKit checker for unsafe casts ([https://github.com/llvm/llvm-project/pull/114606](https://github.com/llvm/llvm-project/pull/114606)) match on unsafe Objective-C pointer casts.
1 parent d6cd214 commit fdd09e9

File tree

5 files changed

+24
-4
lines changed

5 files changed

+24
-4
lines changed
 

‎clang/docs/ReleaseNotes.rst

+4
Original file line numberDiff line numberDiff line change
@@ -975,6 +975,10 @@ AST Matchers
975975

976976
- Add ``exportDecl`` matcher to match export declaration.
977977

978+
- Ensure ``hasType`` and ``hasDeclaration`` match Objective-C interface declarations.
979+
980+
- Ensure ``pointee`` matches Objective-C pointer types.
981+
978982
clang-format
979983
------------
980984

‎clang/include/clang/ASTMatchers/ASTMatchers.h

+3-2
Original file line numberDiff line numberDiff line change
@@ -4044,7 +4044,7 @@ AST_POLYMORPHIC_MATCHER_P_OVERLOAD(
40444044
AST_POLYMORPHIC_MATCHER_P_OVERLOAD(
40454045
hasType,
40464046
AST_POLYMORPHIC_SUPPORTED_TYPES(Expr, FriendDecl, ValueDecl,
4047-
CXXBaseSpecifier),
4047+
CXXBaseSpecifier, ObjCInterfaceDecl),
40484048
internal::Matcher<Decl>, InnerMatcher, 1) {
40494049
QualType QT = internal::getUnderlyingType(Node);
40504050
if (!QT.isNull())
@@ -7445,7 +7445,8 @@ extern const AstTypeMatcher<RValueReferenceType> rValueReferenceType;
74457445
AST_TYPELOC_TRAVERSE_MATCHER_DECL(
74467446
pointee, getPointee,
74477447
AST_POLYMORPHIC_SUPPORTED_TYPES(BlockPointerType, MemberPointerType,
7448-
PointerType, ReferenceType));
7448+
PointerType, ReferenceType,
7449+
ObjCObjectPointerType));
74497450

74507451
/// Matches typedef types.
74517452
///

‎clang/include/clang/ASTMatchers/ASTMatchersInternal.h

+9-1
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

+2-1
Original file line numberDiff line numberDiff line change
@@ -1114,7 +1114,8 @@ AST_TYPELOC_TRAVERSE_MATCHER_DEF(hasValueType,
11141114
AST_TYPELOC_TRAVERSE_MATCHER_DEF(
11151115
pointee,
11161116
AST_POLYMORPHIC_SUPPORTED_TYPES(BlockPointerType, MemberPointerType,
1117-
PointerType, ReferenceType));
1117+
PointerType, ReferenceType,
1118+
ObjCObjectPointerType));
11181119

11191120
const internal::VariadicDynCastAllOfMatcher<Stmt, OMPExecutableDirective>
11201121
ompExecutableDirective;

‎clang/unittests/ASTMatchers/ASTMatchersTraversalTest.cpp

+6
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)
Please sign in to comment.