[PATCH] D151523: [ASTStructuralEquivalence] Fix crash when ObjCCategoryDecl doesn't have corresponding ObjCInterfaceDecl.
Volodymyr Sapsai via Phabricator via cfe-commits
cfe-commits at lists.llvm.org
Thu May 25 18:16:44 PDT 2023
vsapsai created this revision.
vsapsai added reviewers: martong, shafik, ahatanak.
Herald added subscribers: ributzka, rnkovacs.
Herald added a project: All.
vsapsai requested review of this revision.
Herald added a project: clang.
Herald added a subscriber: cfe-commits.
When this happens, it is invalid code and there is diagnostic
error: cannot find interface declaration for '...'
But clang shouldn't crash even if code is invalid. Though subsequent
diagnostic can be imperfect because without ObjCInterfaceDecl we don't have
a type for error messages.
rdar://108818430
Repository:
rG LLVM Github Monorepo
https://reviews.llvm.org/D151523
Files:
clang/lib/AST/ASTStructuralEquivalence.cpp
clang/unittests/AST/StructuralEquivalenceTest.cpp
Index: clang/unittests/AST/StructuralEquivalenceTest.cpp
===================================================================
--- clang/unittests/AST/StructuralEquivalenceTest.cpp
+++ clang/unittests/AST/StructuralEquivalenceTest.cpp
@@ -1143,6 +1143,18 @@
EXPECT_FALSE(testStructuralMatch(t));
}
+TEST_F(StructuralEquivalenceObjCCategoryTest, CategoriesWithoutInterfaces) {
+ auto t = makeDecls<ObjCCategoryDecl>(" @interface A(X) @end",
+ "@interface A @end @interface A(X) @end",
+ Lang_OBJC, objcCategoryDecl());
+ EXPECT_FALSE(testStructuralMatch(t));
+
+ auto t2 = makeDecls<ObjCCategoryDecl>("@interface A(X) @end",
+ "@interface A(X) @end",
+ Lang_OBJC, objcCategoryDecl());
+ EXPECT_TRUE(testStructuralMatch(t2));
+}
+
TEST_F(StructuralEquivalenceObjCCategoryTest, CategoryAndExtension) {
auto t = makeDecls<ObjCCategoryDecl>("@interface A @end @interface A(X) @end",
"@interface A @end @interface A() @end",
Index: clang/lib/AST/ASTStructuralEquivalence.cpp
===================================================================
--- clang/lib/AST/ASTStructuralEquivalence.cpp
+++ clang/lib/AST/ASTStructuralEquivalence.cpp
@@ -2057,8 +2057,13 @@
if (!IsStructurallyEquivalent(D1->getIdentifier(), D2->getIdentifier()))
return false;
- if (!IsStructurallyEquivalent(D1->getClassInterface()->getIdentifier(),
- D2->getClassInterface()->getIdentifier()))
+ const ObjCInterfaceDecl *Intf1 = D1->getClassInterface(),
+ *Intf2 = D2->getClassInterface();
+ if ((Intf1 != nullptr) != (Intf2 != nullptr))
+ return false;
+
+ if (Intf1 &&
+ !IsStructurallyEquivalent(Intf1->getIdentifier(), Intf2->getIdentifier()))
return false;
// Compare protocols.
@@ -2077,7 +2082,8 @@
return false;
// Compare ivars.
- QualType D2Type = Context.ToCtx.getObjCInterfaceType(D2->getClassInterface());
+ QualType D2Type =
+ Intf2 ? Context.ToCtx.getObjCInterfaceType(Intf2) : QualType();
ObjCCategoryDecl::ivar_iterator Ivar2 = D2->ivar_begin(),
Ivar2End = D2->ivar_end();
for (ObjCCategoryDecl::ivar_iterator Ivar1 = D1->ivar_begin(),
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D151523.525898.patch
Type: text/x-patch
Size: 2388 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20230526/b275d3f3/attachment.bin>
More information about the cfe-commits
mailing list