[cfe-commits] r162319 - in /cfe/trunk: lib/Sema/SemaObjCProperty.cpp test/SemaObjC/continuation-class-err.m test/SemaObjC/property-in-class-extension-1.m

Fariborz Jahanian fjahanian at apple.com
Tue Aug 21 14:45:59 PDT 2012


Author: fjahanian
Date: Tue Aug 21 16:45:58 2012
New Revision: 162319

URL: http://llvm.org/viewvc/llvm-project?rev=162319&view=rev
Log:
objective-C: Change rules for overriding properties in 
class extensions a little. clang now allows readonly property 
with no ownership rule (assign, unsafe_unretained, weak, retain, 
strong, or copy) with a readwrite property with an ownership rule.
// rdar://12103400 

Added:
    cfe/trunk/test/SemaObjC/property-in-class-extension-1.m
Modified:
    cfe/trunk/lib/Sema/SemaObjCProperty.cpp
    cfe/trunk/test/SemaObjC/continuation-class-err.m

Modified: cfe/trunk/lib/Sema/SemaObjCProperty.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaObjCProperty.cpp?rev=162319&r1=162318&r2=162319&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/SemaObjCProperty.cpp (original)
+++ cfe/trunk/lib/Sema/SemaObjCProperty.cpp Tue Aug 21 16:45:58 2012
@@ -102,6 +102,15 @@
     << propertyLifetime;
 }
 
+static unsigned deduceWeakPropertyFromType(Sema &S, QualType T) {
+  if ((S.getLangOpts().getGC() != LangOptions::NonGC && 
+       T.isObjCGCWeak()) ||
+      (S.getLangOpts().ObjCAutoRefCount &&
+       T.getObjCLifetime() == Qualifiers::OCL_Weak))
+    return ObjCDeclSpec::DQ_PR_weak;
+  return 0;
+}
+
 Decl *Sema::ActOnProperty(Scope *S, SourceLocation AtLoc,
                           SourceLocation LParenLoc,
                           FieldDeclarator &FD,
@@ -114,12 +123,8 @@
   unsigned Attributes = ODS.getPropertyAttributes();
   TypeSourceInfo *TSI = GetTypeForDeclarator(FD.D, S);
   QualType T = TSI->getType();
-  if ((getLangOpts().getGC() != LangOptions::NonGC && 
-       T.isObjCGCWeak()) ||
-      (getLangOpts().ObjCAutoRefCount &&
-       T.getObjCLifetime() == Qualifiers::OCL_Weak))
-    Attributes |= ObjCDeclSpec::DQ_PR_weak;
-
+  Attributes |= deduceWeakPropertyFromType(*this, T);
+  
   bool isReadWrite = ((Attributes & ObjCDeclSpec::DQ_PR_readwrite) ||
                       // default is readwrite!
                       !(Attributes & ObjCDeclSpec::DQ_PR_readonly));
@@ -236,6 +241,15 @@
   
 }
 
+static unsigned getMemoryModel(unsigned attr) {
+  return attr & (ObjCPropertyDecl::OBJC_PR_assign |
+                 ObjCPropertyDecl::OBJC_PR_retain |
+                 ObjCPropertyDecl::OBJC_PR_copy   |
+                 ObjCPropertyDecl::OBJC_PR_weak   |
+                 ObjCPropertyDecl::OBJC_PR_strong |
+                 ObjCPropertyDecl::OBJC_PR_unsafe_unretained);
+}
+
 Decl *
 Sema::HandlePropertyInClassExtension(Scope *S,
                                      SourceLocation AtLoc,
@@ -342,13 +356,11 @@
   // with continuation class's readwrite property attribute!
   unsigned PIkind = PIDecl->getPropertyAttributesAsWritten();
   if (isReadWrite && (PIkind & ObjCPropertyDecl::OBJC_PR_readonly)) {
-    unsigned retainCopyNonatomic =
-    (ObjCPropertyDecl::OBJC_PR_retain |
-     ObjCPropertyDecl::OBJC_PR_strong |
-     ObjCPropertyDecl::OBJC_PR_copy |
-     ObjCPropertyDecl::OBJC_PR_nonatomic);
-    if ((Attributes & retainCopyNonatomic) !=
-        (PIkind & retainCopyNonatomic)) {
+    PIkind |= deduceWeakPropertyFromType(*this, PIDecl->getType());
+    unsigned ClassExtensionMemoryModel = getMemoryModel(Attributes);
+    unsigned PrimaryClassMemoryModel = getMemoryModel(PIkind);
+    if (PrimaryClassMemoryModel && ClassExtensionMemoryModel &&
+        (PrimaryClassMemoryModel != ClassExtensionMemoryModel)) {
       Diag(AtLoc, diag::warn_property_attr_mismatch);
       Diag(PIDecl->getLocation(), diag::note_property_declare);
     }

Modified: cfe/trunk/test/SemaObjC/continuation-class-err.m
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/SemaObjC/continuation-class-err.m?rev=162319&r1=162318&r2=162319&view=diff
==============================================================================
--- cfe/trunk/test/SemaObjC/continuation-class-err.m (original)
+++ cfe/trunk/test/SemaObjC/continuation-class-err.m Tue Aug 21 16:45:58 2012
@@ -5,13 +5,13 @@
   id _object;
   id _object1;
 }
- at property(readonly) id object;	// expected-note {{property declared here}}
+ at property(readonly) id object;
 @property(readwrite, assign) id object1; // expected-note {{property declared here}}
 @property (readonly) int indentLevel;
 @end
 
 @interface ReadOnly ()
- at property(readwrite, copy) id object;	// expected-warning {{property attribute in class extension does not match the primary class}}
+ at property(readwrite, copy) id object; // Ok. declaring memory model in class extension - primary has none.
 @property(readonly) id object1; // expected-error {{illegal redeclaration of property in class extension 'ReadOnly' (attribute must be 'readwrite', while its primary must be 'readonly')}}
 @property (readwrite, assign) int indentLevel; // OK. assign the default in any case.
 @end

Added: cfe/trunk/test/SemaObjC/property-in-class-extension-1.m
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/SemaObjC/property-in-class-extension-1.m?rev=162319&view=auto
==============================================================================
--- cfe/trunk/test/SemaObjC/property-in-class-extension-1.m (added)
+++ cfe/trunk/test/SemaObjC/property-in-class-extension-1.m Tue Aug 21 16:45:58 2012
@@ -0,0 +1,49 @@
+// RUN: %clang_cc1  -fsyntax-only -verify -Weverything %s
+// RUN: %clang_cc1 -x objective-c++ -fsyntax-only -verify -Weverything %s
+// rdar://12103400
+
+ at class NSString;
+
+ at interface MyClass
+
+ at property (nonatomic, readonly) NSString* addingMemoryModel;
+
+ at property (nonatomic, copy, readonly) NSString* matchingMemoryModel;
+
+ at property (nonatomic, retain, readonly) NSString* addingNoNewMemoryModel;
+
+ at property (readonly) NSString* none;
+ at property (readonly) NSString* none1;
+
+ at property (assign, readonly) NSString* changeMemoryModel; // expected-note {{property declared here}}
+
+ at property (readonly) __weak id weak_prop;
+ at property (readonly) __weak id weak_prop1;
+
+ at property (assign, readonly) NSString* assignProperty;
+
+ at property (readonly) NSString* readonlyProp;
+
+
+
+ at end
+
+ at interface MyClass ()
+{
+  NSString* _name;
+}
+
+ at property (nonatomic, copy) NSString* addingMemoryModel;
+ at property (nonatomic, copy) NSString* matchingMemoryModel;
+ at property () NSString* addingNoNewMemoryModel;
+ at property () NSString* none;
+ at property (readwrite) NSString* none1;
+
+ at property (retain) NSString* changeMemoryModel; // expected-warning {{property attribute in class extension does not match the primary class}}
+ at property () __weak id weak_prop;
+ at property (readwrite) __weak id weak_prop1;
+
+ at property () NSString* assignProperty;
+ at property (assign) NSString* readonlyProp;
+ at end
+





More information about the cfe-commits mailing list