Skip to content

Commit

Permalink
[ObjC] Expand isClassLayoutKnownStatically to base classes as long as…
Browse files Browse the repository at this point in the history
… the implementation of it is known

Only NSObject we can trust the layout of won't change even though we cannot directly see its @implementation
  • Loading branch information
AreaZR committed Mar 16, 2024
1 parent 84b5178 commit d778404
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 2 deletions.
9 changes: 8 additions & 1 deletion clang/lib/CodeGen/CGObjCMac.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1595,6 +1595,11 @@ class CGObjCNonFragileABIMac : public CGObjCCommonMac {
bool isClassLayoutKnownStatically(const ObjCInterfaceDecl *ID) {
// Test a class by checking its superclasses up to
// its base class if it has one.

// Cannot check a null class
if (!ID)
return false;

for (; ID; ID = ID->getSuperClass()) {
// The layout of base class NSObject
// is guaranteed to be statically known
Expand All @@ -1606,7 +1611,9 @@ class CGObjCNonFragileABIMac : public CGObjCCommonMac {
if (!ID->getImplementation())
return false;
}
return false;

// We know the layout of all the intermediate classes and superclasses.
return true;
}

public:
Expand Down
30 changes: 29 additions & 1 deletion clang/test/CodeGenObjC/constant-non-fragile-ivar-offset.m
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@
// CHECK: @"OBJC_IVAR_$_IntermediateClass._intermediateProperty" = hidden constant i64 48
// CHECK: @"OBJC_IVAR_$_SubClass.subClassIvar" = constant i64 56
// CHECK: @"OBJC_IVAR_$_SubClass._subClassProperty" = hidden constant i64 64

// CHECK: @"OBJC_IVAR_$_RootClass.these" = constant i64 0
// CHECK: @"OBJC_IVAR_$_RootClass.dont" = constant i64 4
// CHECK: @"OBJC_IVAR_$_RootClass.change" = constant i64 4
// CHECK: @"OBJC_IVAR_$_StillStaticLayout.static_layout_ivar" = hidden global i32 12

// CHECK: @"OBJC_IVAR_$_NotStaticLayout.not_static_layout_ivar" = hidden global i64 12

@interface NSObject {
Expand Down Expand Up @@ -120,7 +126,29 @@ -(void)intermediateSubclassVar {
// CHECK: getelementptr inbounds i8, ptr %1, i64 64
@end

@interface NotNSObject {
__attribute((objc_root_class)) @interface RootClass {
int these, dont, change;
}
@end

@implementation RootClass
@end

@interface StillStaticLayout : RootClass
@end

@implementation StillStaticLayout {
int static_layout_ivar;
}

// CHECK-LABEL: define internal void @"\01-[StillStaticLayout meth]"
-(void)meth {
static_layout_ivar = 0;
// CHECK-NOT: load i64, ptr @"OBJC_IVAR_$StillStaticLayout.static_layout_ivar
}
@end

__attribute((objc_root_class)) @interface NotNSObject {
int these, might, change;
}
@end
Expand Down

0 comments on commit d778404

Please sign in to comment.