[cfe-commits] r158869 - in /cfe/trunk: include/clang/Sema/Sema.h lib/Sema/SemaObjCProperty.cpp test/SemaObjC/property-12.m test/SemaObjC/tentative-property-decl.m

Fariborz Jahanian fjahanian at apple.com
Wed Jun 20 15:57:42 PDT 2012


Author: fjahanian
Date: Wed Jun 20 17:57:42 2012
New Revision: 158869

URL: http://llvm.org/viewvc/llvm-project?rev=158869&view=rev
Log:
objective-c: Normally, a property cannot be both 'readonly' and having a 
"write" attribute (copy/retain/etc.). But, property declaration in 
primary class and protcols are tentative as they may be overridden 
into a 'readwrite' property in class extensions. Postpone diagnosing 
such warnings until the class implementation is seen. 
// rdar://11656982

Added:
    cfe/trunk/test/SemaObjC/tentative-property-decl.m
Modified:
    cfe/trunk/include/clang/Sema/Sema.h
    cfe/trunk/lib/Sema/SemaObjCProperty.cpp
    cfe/trunk/test/SemaObjC/property-12.m

Modified: cfe/trunk/include/clang/Sema/Sema.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Sema/Sema.h?rev=158869&r1=158868&r2=158869&view=diff
==============================================================================
--- cfe/trunk/include/clang/Sema/Sema.h (original)
+++ cfe/trunk/include/clang/Sema/Sema.h Wed Jun 20 17:57:42 2012
@@ -6006,7 +6006,8 @@
   /// be modified to be consistent with \arg PropertyTy.
   void CheckObjCPropertyAttributes(Decl *PropertyPtrTy,
                                    SourceLocation Loc,
-                                   unsigned &Attributes);
+                                   unsigned &Attributes,
+                                   bool propertyInPrimaryClass);
 
   /// Process the specified property declaration and create decls for the
   /// setters and getters as needed.

Modified: cfe/trunk/lib/Sema/SemaObjCProperty.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaObjCProperty.cpp?rev=158869&r1=158868&r2=158869&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/SemaObjCProperty.cpp (original)
+++ cfe/trunk/lib/Sema/SemaObjCProperty.cpp Wed Jun 20 17:57:42 2012
@@ -135,7 +135,6 @@
 
   // Proceed with constructing the ObjCPropertDecls.
   ObjCContainerDecl *ClassDecl = cast<ObjCContainerDecl>(CurContext);
-
   if (ObjCCategoryDecl *CDecl = dyn_cast<ObjCCategoryDecl>(ClassDecl))
     if (CDecl->IsClassExtension()) {
       Decl *Res = HandlePropertyInClassExtension(S, AtLoc, LParenLoc,
@@ -146,7 +145,7 @@
                                            isOverridingProperty, TSI,
                                            MethodImplKind);
       if (Res) {
-        CheckObjCPropertyAttributes(Res, AtLoc, Attributes);
+        CheckObjCPropertyAttributes(Res, AtLoc, Attributes, false);
         if (getLangOpts().ObjCAutoRefCount)
           checkARCPropertyDecl(*this, cast<ObjCPropertyDecl>(Res));
       }
@@ -163,7 +162,9 @@
     Res->setLexicalDeclContext(lexicalDC);
 
   // Validate the attributes on the @property.
-  CheckObjCPropertyAttributes(Res, AtLoc, Attributes);
+  CheckObjCPropertyAttributes(Res, AtLoc, Attributes, 
+                              (isa<ObjCInterfaceDecl>(ClassDecl) ||
+                               isa<ObjCProtocolDecl>(ClassDecl)));
 
   if (getLangOpts().ObjCAutoRefCount)
     checkARCPropertyDecl(*this, Res);
@@ -601,6 +602,67 @@
   return;
 }
 
+/// DiagnoseClassAndClassExtPropertyMismatch - diagnose inconsistant property
+/// attribute declared in primary class and attributes overridden in any of its
+/// class extensions.
+static void
+DiagnoseClassAndClassExtPropertyMismatch(Sema &S, ObjCInterfaceDecl *ClassDecl, 
+                                         ObjCPropertyDecl *property) {
+  unsigned Attributes = property->getPropertyAttributesAsWritten();
+  bool warn = (Attributes & ObjCDeclSpec::DQ_PR_readonly);
+  for (const ObjCCategoryDecl *CDecl = ClassDecl->getFirstClassExtension();
+       CDecl; CDecl = CDecl->getNextClassExtension()) {
+    warn = false;
+    ObjCPropertyDecl *ClassExtProperty = 0;
+    for (ObjCContainerDecl::prop_iterator P = CDecl->prop_begin(),
+         E = CDecl->prop_end(); P != E; ++P) {
+      if ((*P)->getIdentifier() == property->getIdentifier()) {
+        ClassExtProperty = *P;
+        break;
+      }
+    }
+    if (ClassExtProperty) {
+      unsigned classExtPropertyAttr = 
+        ClassExtProperty->getPropertyAttributesAsWritten();
+      // We are issuing the warning that we postponed because class extensions
+      // can override readonly->readwrite and 'setter' attributes originally
+      // placed on class's property declaration now make sense in the overridden
+      // property.
+      if (Attributes & ObjCDeclSpec::DQ_PR_readonly) {
+        if (!classExtPropertyAttr ||
+            (classExtPropertyAttr & ObjCDeclSpec::DQ_PR_readwrite))
+          continue;
+        warn = true;
+        break;
+      }
+    }
+  }
+  if (warn) {
+    unsigned setterAttrs = (ObjCDeclSpec::DQ_PR_assign |
+                            ObjCDeclSpec::DQ_PR_unsafe_unretained |
+                            ObjCDeclSpec::DQ_PR_copy |
+                            ObjCDeclSpec::DQ_PR_retain |
+                            ObjCDeclSpec::DQ_PR_strong);
+    if (Attributes & setterAttrs) {
+      const char * which =     
+      (Attributes & ObjCDeclSpec::DQ_PR_assign) ?
+      "assign" :
+      (Attributes & ObjCDeclSpec::DQ_PR_unsafe_unretained) ?
+      "unsafe_unretained" :
+      (Attributes & ObjCDeclSpec::DQ_PR_copy) ?
+      "copy" : 
+      (Attributes & ObjCDeclSpec::DQ_PR_retain) ?
+      "retain" : "strong";
+      
+      S.Diag(property->getLocation(), 
+             diag::warn_objc_property_attr_mutually_exclusive)
+      << "readonly" << which;
+    }
+  }
+  
+  
+}
+
 /// ActOnPropertyImplDecl - This routine performs semantic checks and
 /// builds the AST node for a property implementation declaration; declared
 /// as \@synthesize or \@dynamic.
@@ -681,6 +743,8 @@
         FixItHint::CreateReplacement(ReadonlySourceRange, "readwrite");
       }
     }
+    
+    DiagnoseClassAndClassExtPropertyMismatch(*this, IDecl, property);
         
   } else if ((CatImplClass = dyn_cast<ObjCCategoryImplDecl>(ClassImpDecl))) {
     if (Synthesize) {
@@ -1887,7 +1951,8 @@
 
 void Sema::CheckObjCPropertyAttributes(Decl *PDecl,
                                        SourceLocation Loc,
-                                       unsigned &Attributes) {
+                                       unsigned &Attributes,
+                                       bool propertyInPrimaryClass) {
   // FIXME: Improve the reported location.
   if (!PDecl || PDecl->isInvalidDecl())
     return;
@@ -1910,9 +1975,18 @@
       return;
   }
   
+  if (propertyInPrimaryClass) {
+    // we postpone most property diagnosis until class's implementation
+    // because, its readonly attribute may be overridden in its class 
+    // extensions making other attributes, which make no sense, to make sense.
+    if ((Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
+        (Attributes & ObjCDeclSpec::DQ_PR_readwrite))
+      Diag(Loc, diag::err_objc_property_attr_mutually_exclusive) 
+        << "readonly" << "readwrite";
+  }
   // readonly and readwrite/assign/retain/copy conflict.
-  if ((Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
-      (Attributes & (ObjCDeclSpec::DQ_PR_readwrite |
+  else if ((Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
+           (Attributes & (ObjCDeclSpec::DQ_PR_readwrite |
                      ObjCDeclSpec::DQ_PR_assign |
                      ObjCDeclSpec::DQ_PR_unsafe_unretained |
                      ObjCDeclSpec::DQ_PR_copy |

Modified: cfe/trunk/test/SemaObjC/property-12.m
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/SemaObjC/property-12.m?rev=158869&r1=158868&r2=158869&view=diff
==============================================================================
--- cfe/trunk/test/SemaObjC/property-12.m (original)
+++ cfe/trunk/test/SemaObjC/property-12.m Wed Jun 20 17:57:42 2012
@@ -29,4 +29,39 @@
 @end
 
 
+// rdar://11656982
+ at interface I0 <P0> @end
+ at implementation I0 
+ at synthesize X;
+ at end
+
+ at interface I1 <P1> @end
+ at implementation I1 
+ at synthesize X;
+ at end
+
+ at interface I2 <P2> @end
+ at implementation I2 
+ at synthesize X;
+ at end
+
+ at interface I3 <P3> @end
+ at implementation I3 
+ at synthesize X;
+ at end
+
+ at interface I4 <P4> @end
+ at implementation I4 
+ at synthesize X;
+ at end
+
+ at interface I5 <P5> @end
+ at implementation I5 
+ at synthesize X;
+ at end
+
+ at interface I6 <P6> @end
+ at implementation I6 
+ at synthesize X;
+ at end
 

Added: cfe/trunk/test/SemaObjC/tentative-property-decl.m
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/SemaObjC/tentative-property-decl.m?rev=158869&view=auto
==============================================================================
--- cfe/trunk/test/SemaObjC/tentative-property-decl.m (added)
+++ cfe/trunk/test/SemaObjC/tentative-property-decl.m Wed Jun 20 17:57:42 2012
@@ -0,0 +1,43 @@
+// RUN: %clang_cc1 -fsyntax-only -Weverything -verify %s
+// rdar://11656982
+/** Normally, a property cannot be both 'readonly' and having a "write" attribute
+    (copy/retain/etc.). But, property declaration in primary class and protcols
+    are tentative as they may be overridden into a 'readwrite' property in class 
+    extensions. Postpone diagnosing such warnings until the class implementation 
+    is seen.
+*/
+
+ at interface Super {
+}
+ at end
+
+ at class NSString;
+
+ at interface MyClass : Super
+ at property(nonatomic, copy, readonly) NSString *prop;
+ at end
+
+ at interface MyClass ()
+ at property(nonatomic, copy, readwrite) NSString *prop;
+ at end
+
+ at implementation MyClass
+ at synthesize prop;
+ at end
+
+
+ at protocol P
+ at property(nonatomic, copy, readonly) NSString *prop;
+ at end
+
+ at interface YourClass : Super <P>
+ at end
+
+ at interface YourClass ()
+ at property(nonatomic, copy, readwrite) NSString *prop;
+ at end
+
+ at implementation YourClass 
+ at synthesize prop;
+ at end
+





More information about the cfe-commits mailing list