Skip to content

Commit 2ef8091

Browse files
committed
[ObjC] Recursively check superclasses to see if any inherit from NSObject
If an NSObject subclass has fixed offsets, then so must the subclasses of that subclass!
1 parent 077928a commit 2ef8091

File tree

2 files changed

+14
-7
lines changed

2 files changed

+14
-7
lines changed

clang/lib/CodeGen/CGObjCMac.cpp

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1593,12 +1593,19 @@ class CGObjCNonFragileABIMac : public CGObjCCommonMac {
15931593
}
15941594

15951595
bool isClassLayoutKnownStatically(const ObjCInterfaceDecl *ID) {
1596+
if (!ID)
1597+
return false;
1598+
1599+
if (ID->getName() == "NSObject")
1600+
return true;
1601+
15961602
// NSObject is a fixed size. If we can see the @implementation of a class
1597-
// which inherits from NSObject then we know that all it's offsets also must
1598-
// be fixed. FIXME: Can we do this if see a chain of super classes with
1599-
// implementations leading to NSObject?
1600-
return ID->getImplementation() && ID->getSuperClass() &&
1601-
ID->getSuperClass()->getName() == "NSObject";
1603+
// which inherits from NSObject, then we know that all its offsets must
1604+
// be fixed.
1605+
1606+
// Check recursively for all intermediate superclasses.
1607+
return ID->getImplementation() &&
1608+
isClassLayoutKnownStatically(ID->getSuperClass());
16021609
}
16031610

16041611
public:

clang/test/CodeGenObjC/constant-non-fragile-ivar-offset.m

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// RUN: %clang_cc1 -triple x86_64-apple-macosx10.14.0 -emit-llvm %s -o - | FileCheck %s
22

33
// CHECK: @"OBJC_IVAR_$_StaticLayout.static_layout_ivar" = hidden constant i64 20
4-
// CHECK: @"OBJC_IVAR_$_StaticLayoutSubClass.static_layout_ivar2" = hidden global i64 24
4+
// CHECK: @"OBJC_IVAR_$_StaticLayoutSubClass.static_layout_ivar2" = hidden constant i64 24
55
// CHECK: @"OBJC_IVAR_$_NotStaticLayout.not_static_layout_ivar" = hidden global i64 12
66

77
@interface NSObject {
@@ -29,7 +29,7 @@ @implementation StaticLayoutSubClass {
2929
}
3030
-(void)meth2 {
3131
static_layout_ivar2 = 0;
32-
// CHECK: load i64, ptr @"OBJC_IVAR_$_StaticLayoutSubClass
32+
// CHECK-NOT: load i64, ptr @"OBJC_IVAR_$_StaticLayoutSubClass
3333
}
3434
@end
3535

0 commit comments

Comments
 (0)