r175100 - objective-C: Make order of ivars which are synthesized

Fariborz Jahanian fjahanian at apple.com
Wed Feb 13 14:50:37 PST 2013


Author: fjahanian
Date: Wed Feb 13 16:50:36 2013
New Revision: 175100

URL: http://llvm.org/viewvc/llvm-project?rev=175100&view=rev
Log:
objective-C: Make order of ivars which are synthesized
in the course of property synthesis deterministic (ordered
by their type size), instead of having hashtable order
(as it is currently). // rdar://13192366

Added:
    cfe/trunk/test/CodeGenObjC/reorder-synthesized-ivars.m
Modified:
    cfe/trunk/lib/AST/DeclObjC.cpp

Modified: cfe/trunk/lib/AST/DeclObjC.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/AST/DeclObjC.cpp?rev=175100&r1=175099&r2=175100&view=diff
==============================================================================
--- cfe/trunk/lib/AST/DeclObjC.cpp (original)
+++ cfe/trunk/lib/AST/DeclObjC.cpp Wed Feb 13 16:50:36 2013
@@ -1074,6 +1074,20 @@ void ObjCInterfaceDecl::setImplementatio
   getASTContext().setObjCImplementation(getDefinition(), ImplD);
 }
 
+namespace {
+  struct SynthesizeIvarChunk {
+    uint64_t Size;
+    ObjCIvarDecl *Ivar;
+    SynthesizeIvarChunk(uint64_t size, ObjCIvarDecl *ivar)
+      : Size(size), Ivar(ivar) {}
+  };
+
+  bool operator<(const SynthesizeIvarChunk & LHS,
+                 const SynthesizeIvarChunk &RHS) {
+      return LHS.Size < RHS.Size;
+  }
+}
+
 /// all_declared_ivar_begin - return first ivar declared in this class,
 /// its extensions and its implementation. Lazily build the list on first
 /// access.
@@ -1110,14 +1124,33 @@ ObjCIvarDecl *ObjCInterfaceDecl::all_dec
   
   if (ObjCImplementationDecl *ImplDecl = getImplementation()) {
     if (!ImplDecl->ivar_empty()) {
-      ObjCImplementationDecl::ivar_iterator I = ImplDecl->ivar_begin(),
-                                            E = ImplDecl->ivar_end();
-      if (!data().IvarList) {
-        data().IvarList = *I; ++I;
-        curIvar = data().IvarList;
+      SmallVector<SynthesizeIvarChunk, 16> layout;
+      for (ObjCImplementationDecl::ivar_iterator I = ImplDecl->ivar_begin(),
+           E = ImplDecl->ivar_end(); I != E; ++I) {
+        ObjCIvarDecl *IV = *I;
+        if (IV->getSynthesize() && !IV->isInvalidDecl()) {
+          layout.push_back(SynthesizeIvarChunk(
+                             IV->getASTContext().getTypeSize(IV->getType()), IV));
+          continue;
+        }
+        if (!data().IvarList)
+          data().IvarList = *I;
+        else
+          curIvar->setNextIvar(*I);
+        curIvar = *I;
+      }
+      
+      if (!layout.empty()) {
+        // Order synthesized ivars by their size.
+        std::stable_sort(layout.begin(), layout.end());
+        unsigned Ix = 0, EIx = layout.size();
+        if (!data().IvarList) {
+          data().IvarList = layout[0].Ivar; Ix++;
+          curIvar = data().IvarList;
+        }
+        for ( ; Ix != EIx; curIvar = layout[Ix].Ivar, Ix++)
+          curIvar->setNextIvar(layout[Ix].Ivar);
       }
-      for ( ;I != E; curIvar = *I, ++I)
-        curIvar->setNextIvar(*I);
     }
   }
   return data().IvarList;

Added: cfe/trunk/test/CodeGenObjC/reorder-synthesized-ivars.m
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/CodeGenObjC/reorder-synthesized-ivars.m?rev=175100&view=auto
==============================================================================
--- cfe/trunk/test/CodeGenObjC/reorder-synthesized-ivars.m (added)
+++ cfe/trunk/test/CodeGenObjC/reorder-synthesized-ivars.m Wed Feb 13 16:50:36 2013
@@ -0,0 +1,58 @@
+// RUN: %clang_cc1 -triple x86_64-apple-darwin10 -fobjc-default-synthesize-properties -emit-llvm -x objective-c %s -o - | FileCheck %s
+// rdar://13192366
+typedef signed char BOOL;
+ at interface NSObject 
+{
+  id isa;
+}
+ at end
+
+ at interface MyClass : NSObject
+
+ at property (readwrite) BOOL boolean1;
+ at property (readwrite, copy) id object1;
+ at property (readwrite) BOOL boolean2;
+ at property (readwrite, copy) id object2;
+ at property (readwrite) BOOL boolean3;
+ at property (readwrite, copy) id object3;
+ at property (readwrite) BOOL boolean4;
+ at property (readwrite, copy) id object4;
+ at property (readwrite) BOOL boolean5;
+ at property (readwrite, copy) id object5;
+ at property (readwrite) BOOL boolean6;
+ at property (readwrite, copy) id object6;
+ at property (readwrite) BOOL boolean7;
+ at property (readwrite) BOOL MyBool;
+ at property (readwrite, copy) id object7;
+ at property (readwrite) BOOL boolean8;
+ at property (readwrite, copy) id object8;
+ at property (readwrite) BOOL boolean9;
+ at property (readwrite, copy) id object9;
+ at end
+
+ at implementation MyClass
+{
+  id MyIvar;
+  BOOL _MyBool;
+  char * pc;
+}
+ at end
+
+// CHECK: @"{{.*}}" = internal global [10 x i8] c"_boolean
+// CHECK-NEXT: @"{{.*}}" = internal global [10 x i8] c"_boolean
+// CHECK-NEXT: @"{{.*}}" = internal global [10 x i8] c"_boolean
+// CHECK-NEXT: @"{{.*}}" = internal global [10 x i8] c"_boolean
+// CHECK-NEXT: @"{{.*}}" = internal global [10 x i8] c"_boolean
+// CHECK-NEXT: @"{{.*}}" = internal global [10 x i8] c"_boolean
+// CHECK-NEXT: @"{{.*}}" = internal global [10 x i8] c"_boolean
+// CHECK-NEXT: @"{{.*}}" = internal global [10 x i8] c"_boolean
+// CHECK-NEXT: @"{{.*}}" = internal global [10 x i8] c"_boolean
+// CHECK-NEXT: @"{{.*}}" = internal global [9 x i8] c"_object
+// CHECK-NEXT: @"{{.*}}" = internal global [9 x i8] c"_object
+// CHECK-NEXT: @"{{.*}}" = internal global [9 x i8] c"_object
+// CHECK-NEXT: @"{{.*}}" = internal global [9 x i8] c"_object
+// CHECK-NEXT: @"{{.*}}" = internal global [9 x i8] c"_object
+// CHECK-NEXT: @"{{.*}}" = internal global [9 x i8] c"_object
+// CHECK-NEXT: @"{{.*}}" = internal global [9 x i8] c"_object
+// CHECK-NEXT: @"{{.*}}" = internal global [9 x i8] c"_object
+// CHECK-NEXT: @"{{.*}}" = internal global [9 x i8] c"_object





More information about the cfe-commits mailing list