Skip to content

Commit c643f77

Browse files
t-rasmudrniwa
authored andcommitted
[ASTMatchers] AST matcher support for ObjC pointers (llvm#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 572ebb1 commit c643f77

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
@@ -1128,6 +1128,10 @@ AST Matchers
11281128

11291129
- Add ``arrayInitIndexExpr`` and ``arrayInitLoopExpr`` matchers.
11301130

1131+
- Ensure ``hasType`` and ``hasDeclaration`` match Objective-C interface declarations.
1132+
1133+
- Ensure ``pointee`` matches Objective-C pointer types.
1134+
11311135
clang-format
11321136
------------
11331137

clang/include/clang/ASTMatchers/ASTMatchers.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3997,7 +3997,7 @@ AST_POLYMORPHIC_MATCHER_P_OVERLOAD(
39973997
AST_POLYMORPHIC_MATCHER_P_OVERLOAD(
39983998
hasType,
39993999
AST_POLYMORPHIC_SUPPORTED_TYPES(Expr, FriendDecl, ValueDecl,
4000-
CXXBaseSpecifier),
4000+
CXXBaseSpecifier, ObjCInterfaceDecl),
40014001
internal::Matcher<Decl>, InnerMatcher, 1) {
40024002
QualType QT = internal::getUnderlyingType(Node);
40034003
if (!QT.isNull())
@@ -7227,7 +7227,8 @@ extern const AstTypeMatcher<RValueReferenceType> rValueReferenceType;
72277227
AST_TYPELOC_TRAVERSE_MATCHER_DECL(
72287228
pointee, getPointee,
72297229
AST_POLYMORPHIC_SUPPORTED_TYPES(BlockPointerType, MemberPointerType,
7230-
PointerType, ReferenceType));
7230+
PointerType, ReferenceType,
7231+
ObjCObjectPointerType));
72317232

72327233
/// Matches typedef types.
72337234
///

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,
@@ -1117,6 +1120,11 @@ class HasDeclarationMatcher : public MatcherInterface<T> {
11171120
return matchesDecl(Node.getDecl(), Finder, Builder);
11181121
}
11191122

1123+
bool matchesSpecialized(const ObjCInterfaceDecl &Node, ASTMatchFinder *Finder,
1124+
BoundNodesTreeBuilder *Builder) const {
1125+
return matchesDecl(Node.getCanonicalDecl(), Finder, Builder);
1126+
}
1127+
11201128
/// Extracts the operator new of the new call and returns whether the
11211129
/// inner matcher matches on it.
11221130
bool matchesSpecialized(const CXXNewExpr &Node,
@@ -1217,7 +1225,7 @@ using HasDeclarationSupportedTypes =
12171225
ElaboratedType, InjectedClassNameType, LabelStmt, AddrLabelExpr,
12181226
MemberExpr, QualType, RecordType, TagType,
12191227
TemplateSpecializationType, TemplateTypeParmType, TypedefType,
1220-
UnresolvedUsingType, ObjCIvarRefExpr>;
1228+
UnresolvedUsingType, ObjCIvarRefExpr, ObjCInterfaceDecl>;
12211229

12221230
/// A Matcher that allows binding the node it matches to an id.
12231231
///

clang/lib/ASTMatchers/ASTMatchersInternal.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1081,7 +1081,8 @@ AST_TYPELOC_TRAVERSE_MATCHER_DEF(hasValueType,
10811081
AST_TYPELOC_TRAVERSE_MATCHER_DEF(
10821082
pointee,
10831083
AST_POLYMORPHIC_SUPPORTED_TYPES(BlockPointerType, MemberPointerType,
1084-
PointerType, ReferenceType));
1084+
PointerType, ReferenceType,
1085+
ObjCObjectPointerType));
10851086

10861087
const internal::VariadicDynCastAllOfMatcher<Stmt, OMPExecutableDirective>
10871088
ompExecutableDirective;

clang/unittests/ASTMatchers/ASTMatchersTraversalTest.cpp

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

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

0 commit comments

Comments
 (0)