[cfe-commits] r135002 - in /cfe/trunk: lib/ARCMigrate/TransBlockObjCVariable.cpp lib/ARCMigrate/TransProperties.cpp lib/ARCMigrate/Transforms.cpp lib/ARCMigrate/Transforms.h test/ARCMT/assign-prop-with-arc-runtime.m test/ARCMT/assign-prop-with-arc-runtime.m.result

Argyrios Kyrtzidis akyrtzi at gmail.com
Tue Jul 12 15:05:17 PDT 2011


Author: akirtzidis
Date: Tue Jul 12 17:05:17 2011
New Revision: 135002

URL: http://llvm.org/viewvc/llvm-project?rev=135002&view=rev
Log:
[arcmt] Before applying '__weak' check whether the objc class is annotated with objc_arc_weak_reference_unavailable
or is in a list of classes not supporting 'weak'.

rdar://9489367.

Modified:
    cfe/trunk/lib/ARCMigrate/TransBlockObjCVariable.cpp
    cfe/trunk/lib/ARCMigrate/TransProperties.cpp
    cfe/trunk/lib/ARCMigrate/Transforms.cpp
    cfe/trunk/lib/ARCMigrate/Transforms.h
    cfe/trunk/test/ARCMT/assign-prop-with-arc-runtime.m
    cfe/trunk/test/ARCMT/assign-prop-with-arc-runtime.m.result

Modified: cfe/trunk/lib/ARCMigrate/TransBlockObjCVariable.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/ARCMigrate/TransBlockObjCVariable.cpp?rev=135002&r1=135001&r2=135002&view=diff
==============================================================================
--- cfe/trunk/lib/ARCMigrate/TransBlockObjCVariable.cpp (original)
+++ cfe/trunk/lib/ARCMigrate/TransBlockObjCVariable.cpp Tue Jul 12 17:05:17 2011
@@ -98,12 +98,12 @@
         BlocksAttr *attr = var->getAttr<BlocksAttr>();
         if(!attr)
           continue;
-        bool hasWeak = Pass.Ctx.getLangOptions().ObjCRuntimeHasWeak;
+        bool useWeak = canApplyWeak(Pass.Ctx, var->getType());
         SourceManager &SM = Pass.Ctx.getSourceManager();
         Transaction Trans(Pass.TA);
         Pass.TA.replaceText(SM.getInstantiationLoc(attr->getLocation()),
                             "__block",
-                            hasWeak ? "__weak" : "__unsafe_unretained");
+                            useWeak ? "__weak" : "__unsafe_unretained");
       }
 
     }

Modified: cfe/trunk/lib/ARCMigrate/TransProperties.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/ARCMigrate/TransProperties.cpp?rev=135002&r1=135001&r2=135002&view=diff
==============================================================================
--- cfe/trunk/lib/ARCMigrate/TransProperties.cpp (original)
+++ cfe/trunk/lib/ARCMigrate/TransProperties.cpp Tue Jul 12 17:05:17 2011
@@ -112,7 +112,7 @@
   }
 
   void applyWeak(PropData &prop) {
-    assert(Pass.Ctx.getLangOptions().ObjCRuntimeHasWeak);
+    assert(canApplyWeak(Pass.Ctx, prop.IvarD->getType()));
 
     Transaction Trans(Pass.TA);
     Pass.TA.insert(prop.IvarD->getLocation(), "__weak "); 
@@ -157,7 +157,7 @@
     // There is a "error: existing ivar for assign property must be
     // __unsafe_unretained"; fix it.
 
-    if (!Pass.Ctx.getLangOptions().ObjCRuntimeHasWeak) {
+    if (!canApplyWeak(Pass.Ctx, ivarD->getType())) {
       // We will just add __unsafe_unretained to the ivar.
       Transaction Trans(Pass.TA);
       Pass.TA.insert(ivarD->getLocation(), "__unsafe_unretained ");

Modified: cfe/trunk/lib/ARCMigrate/Transforms.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/ARCMigrate/Transforms.cpp?rev=135002&r1=135001&r2=135002&view=diff
==============================================================================
--- cfe/trunk/lib/ARCMigrate/Transforms.cpp (original)
+++ cfe/trunk/lib/ARCMigrate/Transforms.cpp Tue Jul 12 17:05:17 2011
@@ -29,6 +29,61 @@
 // Helpers.
 //===----------------------------------------------------------------------===//
 
+/// \brief True if the class is one that does not support weak.
+static bool isClassInWeakBlacklist(ObjCInterfaceDecl *cls) {
+  if (!cls)
+    return false;
+
+  bool inList = llvm::StringSwitch<bool>(cls->getName())
+                 .Case("NSColorSpace", true)
+                 .Case("NSFont", true)
+                 .Case("NSFontPanel", true)
+                 .Case("NSImage", true)
+                 .Case("NSLazyBrowserCell", true)
+                 .Case("NSWindow", true)
+                 .Case("NSWindowController", true)
+                 .Case("NSMenuView", true)
+                 .Case("NSPersistentUIWindowInfo", true)
+                 .Case("NSTableCellView", true)
+                 .Case("NSATSTypeSetter", true)
+                 .Case("NSATSGlyphStorage", true)
+                 .Case("NSLineFragmentRenderingContext", true)
+                 .Case("NSAttributeDictionary", true)
+                 .Case("NSParagraphStyle", true)
+                 .Case("NSTextTab", true)
+                 .Case("NSSimpleHorizontalTypesetter", true)
+                 .Case("_NSCachedAttributedString", true)
+                 .Case("NSStringDrawingTextStorage", true)
+                 .Case("NSTextView", true)
+                 .Case("NSSubTextStorage", true)
+                 .Default(false);
+
+  if (inList)
+    return true;
+
+  return isClassInWeakBlacklist(cls->getSuperClass());
+}
+
+bool trans::canApplyWeak(ASTContext &Ctx, QualType type) {
+  if (!Ctx.getLangOptions().ObjCRuntimeHasWeak)
+    return false;
+
+  QualType T = type;
+  while (const PointerType *ptr = T->getAs<PointerType>())
+    T = ptr->getPointeeType();
+  if (const ObjCObjectPointerType *ObjT = T->getAs<ObjCObjectPointerType>()) {
+    ObjCInterfaceDecl *Class = ObjT->getInterfaceDecl();
+    if (!Class || Class->getName() == "NSObject")
+      return false; // id/NSObject is not safe for weak.
+    if (Class->isArcWeakrefUnavailable())
+      return false;
+    if (isClassInWeakBlacklist(Class))
+      return false;
+  }
+
+  return true;
+}
+
 /// \brief 'Loc' is the end of a statement range. This returns the location
 /// immediately after the semicolon following the statement.
 /// If no semicolon is found or the location is inside a macro, the returned

Modified: cfe/trunk/lib/ARCMigrate/Transforms.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/ARCMigrate/Transforms.h?rev=135002&r1=135001&r2=135002&view=diff
==============================================================================
--- cfe/trunk/lib/ARCMigrate/Transforms.h (original)
+++ cfe/trunk/lib/ARCMigrate/Transforms.h Tue Jul 12 17:05:17 2011
@@ -44,6 +44,9 @@
 // Helpers.
 //===----------------------------------------------------------------------===//
 
+/// \brief Determine whether we can add weak to the given type.
+bool canApplyWeak(ASTContext &Ctx, QualType type);
+
 /// \brief 'Loc' is the end of a statement range. This returns the location
 /// immediately after the semicolon following the statement.
 /// If no semicolon is found or the location is inside a macro, the returned

Modified: cfe/trunk/test/ARCMT/assign-prop-with-arc-runtime.m
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/ARCMT/assign-prop-with-arc-runtime.m?rev=135002&r1=135001&r2=135002&view=diff
==============================================================================
--- cfe/trunk/test/ARCMT/assign-prop-with-arc-runtime.m (original)
+++ cfe/trunk/test/ARCMT/assign-prop-with-arc-runtime.m Tue Jul 12 17:05:17 2011
@@ -4,16 +4,32 @@
 
 #include "Common.h"
 
+__attribute__((objc_arc_weak_reference_unavailable))
+ at interface WeakOptOut
+ at end
+
+ at class _NSCachedAttributedString;
+
+typedef _NSCachedAttributedString *BadClassForWeak;
+
 @interface Foo : NSObject {
-  NSObject *x, *w, *q1, *q2;
-  NSObject *z1, *__unsafe_unretained z2;
+  Foo *x, *w, *q1, *q2;
+  Foo *z1, *__unsafe_unretained z2;
+  WeakOptOut *oo;
+  BadClassForWeak bcw;
+  id not_safe1;
+  NSObject *not_safe2;
 }
- at property (readonly,assign) id x;
- at property (assign) id w;
- at property (assign) id q1, q2;
- at property (assign) id z1, z2;
+ at property (readonly,assign) Foo *x;
+ at property (assign) Foo *w;
+ at property (assign) Foo *q1, *q2;
+ at property (assign) Foo *z1, *z2;
+ at property (assign) WeakOptOut *oo;
+ at property (assign) BadClassForWeak bcw;
+ at property (assign) id not_safe1;
+ at property (assign) NSObject *not_safe2;
 @end
 
 @implementation Foo
- at synthesize x,w,q1,q2,z1,z2;
+ at synthesize x,w,q1,q2,z1,z2,oo,bcw,not_safe1,not_safe2;
 @end

Modified: cfe/trunk/test/ARCMT/assign-prop-with-arc-runtime.m.result
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/ARCMT/assign-prop-with-arc-runtime.m.result?rev=135002&r1=135001&r2=135002&view=diff
==============================================================================
--- cfe/trunk/test/ARCMT/assign-prop-with-arc-runtime.m.result (original)
+++ cfe/trunk/test/ARCMT/assign-prop-with-arc-runtime.m.result Tue Jul 12 17:05:17 2011
@@ -4,16 +4,32 @@
 
 #include "Common.h"
 
+__attribute__((objc_arc_weak_reference_unavailable))
+ at interface WeakOptOut
+ at end
+
+ at class _NSCachedAttributedString;
+
+typedef _NSCachedAttributedString *BadClassForWeak;
+
 @interface Foo : NSObject {
-  NSObject *__weak x, *__weak w, *__weak q1, *__weak q2;
-  NSObject *__unsafe_unretained z1, *__unsafe_unretained z2;
+  Foo *__weak x, *__weak w, *__weak q1, *__weak q2;
+  Foo *__unsafe_unretained z1, *__unsafe_unretained z2;
+  WeakOptOut *__unsafe_unretained oo;
+  BadClassForWeak __unsafe_unretained bcw;
+  id __unsafe_unretained not_safe1;
+  NSObject *__unsafe_unretained not_safe2;
 }
- at property (readonly,weak) id x;
- at property (weak) id w;
- at property (weak) id q1, q2;
- at property (assign) id z1, z2;
+ at property (readonly,weak) Foo *x;
+ at property (weak) Foo *w;
+ at property (weak) Foo *q1, *q2;
+ at property (assign) Foo *z1, *z2;
+ at property (assign) WeakOptOut *oo;
+ at property (assign) BadClassForWeak bcw;
+ at property (assign) id not_safe1;
+ at property (assign) NSObject *not_safe2;
 @end
 
 @implementation Foo
- at synthesize x,w,q1,q2,z1,z2;
+ at synthesize x,w,q1,q2,z1,z2,oo,bcw,not_safe1,not_safe2;
 @end





More information about the cfe-commits mailing list