[clang] [ObjC] Recursively check superclasses to see if any inherit from NSObject (PR #81335)

via cfe-commits cfe-commits at lists.llvm.org
Fri Feb 9 15:05:49 PST 2024


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-clang

Author: AtariDreams (AtariDreams)

<details>
<summary>Changes</summary>

If an NSObject subclass has fixed offsets, then so must the subclasses of that subclass!

---
Full diff: https://github.com/llvm/llvm-project/pull/81335.diff


2 Files Affected:

- (modified) clang/lib/CodeGen/CGObjCMac.cpp (+12-5) 
- (modified) clang/test/CodeGenObjC/constant-non-fragile-ivar-offset.m (+13) 


``````````diff
diff --git a/clang/lib/CodeGen/CGObjCMac.cpp b/clang/lib/CodeGen/CGObjCMac.cpp
index 27d77e9a8a5511..ed7b6533752664 100644
--- a/clang/lib/CodeGen/CGObjCMac.cpp
+++ b/clang/lib/CodeGen/CGObjCMac.cpp
@@ -1593,12 +1593,19 @@ class CGObjCNonFragileABIMac : public CGObjCCommonMac {
   }
 
   bool isClassLayoutKnownStatically(const ObjCInterfaceDecl *ID) {
+    if (!ID)
+      return false;
+
     // NSObject is a fixed size. If we can see the @implementation of a class
-    // which inherits from NSObject then we know that all it's offsets also must
-    // be fixed. FIXME: Can we do this if see a chain of super classes with
-    // implementations leading to NSObject?
-    return ID->getImplementation() && ID->getSuperClass() &&
-           ID->getSuperClass()->getName() == "NSObject";
+    // which inherits from NSObject, then we know that all its offsets must
+    // be fixed.
+    if (ID->getImplementation() && ID->getSuperClass() &&
+        ID->getSuperClass()->getName() == "NSObject") {
+      // Check recursively for all intermediate superclasses.
+      return isClassLayoutKnownStatically(ID->getSuperClass());
+    }
+
+    return false;
   }
 
 public:
diff --git a/clang/test/CodeGenObjC/constant-non-fragile-ivar-offset.m b/clang/test/CodeGenObjC/constant-non-fragile-ivar-offset.m
index 788b3220af3067..4701f1c5c9b93f 100644
--- a/clang/test/CodeGenObjC/constant-non-fragile-ivar-offset.m
+++ b/clang/test/CodeGenObjC/constant-non-fragile-ivar-offset.m
@@ -2,6 +2,7 @@
 
 // CHECK: @"OBJC_IVAR_$_StaticLayout.static_layout_ivar" = hidden constant i64 20
 // CHECK: @"OBJC_IVAR_$_NotStaticLayout.not_static_layout_ivar" = hidden global i64 12
+// CHECK: @"OBJC_IVAR_$_StaticLayoutSubClass.static_layout_ivar2" = hidden global i64 24
 
 @interface NSObject {
   int these, will, never, change, ever;
@@ -20,6 +21,18 @@ -(void)meth {
 }
 @end
 
+ at interface StaticLayoutSubClass : StaticLayout
+ at end
+
+ at implementation StaticLayoutSubClass {
+  int static_layout_ivar2;
+}
+-(void)meth2 {
+  static_layout_ivar2 = 0;
+  // CHECK-NOT: load i64, ptr @"OBJC_IVAR_$_StaticLayoutSubClass.static_layout_ivar2
+}
+ at end
+
 @interface NotNSObject {
   int these, might, change;
 }

``````````

</details>


https://github.com/llvm/llvm-project/pull/81335


More information about the cfe-commits mailing list