[cfe-commits] r147956 - in /cfe/trunk: lib/Sema/SemaObjCProperty.cpp test/SemaObjC/arc-readonly-property-ivar-1.m test/SemaObjC/arc-readonly-property-ivar.m

Fariborz Jahanian fjahanian at apple.com
Wed Jan 11 10:26:06 PST 2012


Author: fjahanian
Date: Wed Jan 11 12:26:06 2012
New Revision: 147956

URL: http://llvm.org/viewvc/llvm-project?rev=147956&view=rev
Log:
objc-arc: evaluate 'readonly' property with no known
life-time to that of its backing 'ivar's lifetime.
// rdar://10558871

Added:
    cfe/trunk/test/SemaObjC/arc-readonly-property-ivar-1.m
    cfe/trunk/test/SemaObjC/arc-readonly-property-ivar.m
Modified:
    cfe/trunk/lib/Sema/SemaObjCProperty.cpp

Modified: cfe/trunk/lib/Sema/SemaObjCProperty.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaObjCProperty.cpp?rev=147956&r1=147955&r2=147956&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/SemaObjCProperty.cpp (original)
+++ cfe/trunk/lib/Sema/SemaObjCProperty.cpp Wed Jan 11 12:26:06 2012
@@ -518,6 +518,33 @@
   S.Diag(property->getLocation(), diag::note_property_declare);
 }
 
+/// setImpliedPropertyAttributeForReadOnlyProperty -
+/// This routine evaludates life-time attributes for a 'readonly'
+/// property with no known lifetime of its own, using backing
+/// 'ivar's attribute, if any. If no backing 'ivar', property's
+/// life-time is assumed 'strong'.
+static void setImpliedPropertyAttributeForReadOnlyProperty(
+              ObjCPropertyDecl *property, ObjCIvarDecl *ivar) {
+  Qualifiers::ObjCLifetime propertyLifetime = 
+    getImpliedARCOwnership(property->getPropertyAttributes(),
+                           property->getType());
+  if (propertyLifetime != Qualifiers::OCL_None)
+    return;
+  
+  if (!ivar) {
+    // if no backing ivar, make property 'strong'.
+    property->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_strong);
+    return;
+  }
+  // property assumes owenership of backing ivar.
+  QualType ivarType = ivar->getType();
+  Qualifiers::ObjCLifetime ivarLifetime = ivarType.getObjCLifetime();
+  if (ivarLifetime == Qualifiers::OCL_Strong)
+    property->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_strong);
+  else if (ivarLifetime == Qualifiers::OCL_Weak)
+    property->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_weak);
+  return;
+}
 
 /// ActOnPropertyImplDecl - This routine performs semantic checks and
 /// builds the AST node for a property implementation declaration; declared
@@ -608,11 +635,21 @@
     // @synthesize
     if (!PropertyIvar)
       PropertyIvar = PropertyId;
-    ObjCPropertyDecl::PropertyAttributeKind kind 
-      = property->getPropertyAttributes();
+    // Check that this is a previously declared 'ivar' in 'IDecl' interface
+    ObjCInterfaceDecl *ClassDeclared;
+    Ivar = IDecl->lookupInstanceVariable(PropertyIvar, ClassDeclared);
     QualType PropType = property->getType();
-
     QualType PropertyIvarType = PropType.getNonReferenceType();
+    
+    if (getLangOptions().ObjCAutoRefCount &&
+        PropertyIvarType->isObjCRetainableType() &&
+        (property->getPropertyAttributesAsWritten() &
+         ObjCPropertyDecl::OBJC_PR_readonly)) {
+      setImpliedPropertyAttributeForReadOnlyProperty(property, Ivar);    
+    }
+    
+    ObjCPropertyDecl::PropertyAttributeKind kind 
+      = property->getPropertyAttributes();
 
     // Add GC __weak to the ivar type if the property is weak.
     if ((kind & ObjCPropertyDecl::OBJC_PR_weak) && 
@@ -627,9 +664,6 @@
       }
     }
 
-    // Check that this is a previously declared 'ivar' in 'IDecl' interface
-    ObjCInterfaceDecl *ClassDeclared;
-    Ivar = IDecl->lookupInstanceVariable(PropertyIvar, ClassDeclared);
     if (!Ivar) {
       // In ARC, give the ivar a lifetime qualifier based on the
       // property attributes.
@@ -1681,6 +1715,21 @@
   ObjCPropertyDecl *PropertyDecl = cast<ObjCPropertyDecl>(PDecl);
   QualType PropertyTy = PropertyDecl->getType(); 
 
+  if (getLangOptions().ObjCAutoRefCount &&
+      (Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
+      PropertyTy->isObjCRetainableType()) {
+    // 'readonly' property with no obvious lifetime.
+    // its life time will be determined by its backing ivar.
+    unsigned rel = (ObjCDeclSpec::DQ_PR_unsafe_unretained |
+                    ObjCDeclSpec::DQ_PR_copy |
+                    ObjCDeclSpec::DQ_PR_retain |
+                    ObjCDeclSpec::DQ_PR_strong |
+                    ObjCDeclSpec::DQ_PR_weak |
+                    ObjCDeclSpec::DQ_PR_assign);
+    if ((Attributes & rel) == 0)
+      return;
+  }
+  
   // readonly and readwrite/assign/retain/copy conflict.
   if ((Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
       (Attributes & (ObjCDeclSpec::DQ_PR_readwrite |

Added: cfe/trunk/test/SemaObjC/arc-readonly-property-ivar-1.m
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/SemaObjC/arc-readonly-property-ivar-1.m?rev=147956&view=auto
==============================================================================
--- cfe/trunk/test/SemaObjC/arc-readonly-property-ivar-1.m (added)
+++ cfe/trunk/test/SemaObjC/arc-readonly-property-ivar-1.m Wed Jan 11 12:26:06 2012
@@ -0,0 +1,29 @@
+// RUN: %clang_cc1  -fobjc-default-synthesize-properties  -triple x86_64-apple-darwin11  -fobjc-runtime-has-weak -fobjc-arc -fsyntax-only -verify %s
+// RUN: %clang_cc1  -x objective-c++ -fobjc-default-synthesize-properties  -triple x86_64-apple-darwin11  -fobjc-runtime-has-weak -fobjc-arc -fsyntax-only -verify %s
+// rdar:// 10558871
+
+ at interface PP
+ at property (readonly) id ReadOnlyPropertyNoBackingIvar;
+ at property (readonly) id ReadOnlyProperty;
+ at property (readonly) id ReadOnlyPropertyX;
+ at end
+
+ at implementation PP {
+__weak id _ReadOnlyProperty;
+}
+ at synthesize ReadOnlyPropertyNoBackingIvar;
+ at synthesize ReadOnlyProperty = _ReadOnlyProperty;
+ at synthesize ReadOnlyPropertyX = _ReadOnlyPropertyX;
+ at end
+
+ at interface DD
+ at property (readonly) id ReadOnlyProperty;
+ at property (readonly) id ReadOnlyPropertyStrong;
+ at property (readonly) id ReadOnlyPropertyNoBackingIvar;
+ at end
+
+ at implementation DD {
+__weak id _ReadOnlyProperty;
+__strong id _ReadOnlyPropertyStrong;
+}
+ at end

Added: cfe/trunk/test/SemaObjC/arc-readonly-property-ivar.m
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/SemaObjC/arc-readonly-property-ivar.m?rev=147956&view=auto
==============================================================================
--- cfe/trunk/test/SemaObjC/arc-readonly-property-ivar.m (added)
+++ cfe/trunk/test/SemaObjC/arc-readonly-property-ivar.m Wed Jan 11 12:26:06 2012
@@ -0,0 +1,16 @@
+// RUN: %clang_cc1  -triple x86_64-apple-darwin11  -fobjc-runtime-has-weak -fobjc-arc -fsyntax-only -verify %s
+// rdar:// 10558871
+
+ at interface PP
+ at property (readonly) id ReadOnlyPropertyNoBackingIvar;
+ at property (readonly) id ReadOnlyProperty;
+ at property (readonly) id ReadOnlyPropertyX;
+ at end
+
+ at implementation PP {
+__weak id _ReadOnlyProperty;
+}
+ at synthesize ReadOnlyPropertyNoBackingIvar;
+ at synthesize ReadOnlyProperty = _ReadOnlyProperty;
+ at synthesize ReadOnlyPropertyX = _ReadOnlyPropertyX;
+ at end





More information about the cfe-commits mailing list