[cfe-commits] r155169 - in /cfe/trunk: include/clang/Sema/Sema.h lib/Sema/SemaExprObjC.cpp lib/Sema/SemaPseudoObject.cpp test/SemaObjC/weak-receiver-warn.m

Fariborz Jahanian fjahanian at apple.com
Thu Apr 19 16:49:39 PDT 2012


Author: fjahanian
Date: Thu Apr 19 18:49:39 2012
New Revision: 155169

URL: http://llvm.org/viewvc/llvm-project?rev=155169&view=rev
Log:
objective-arc: Retune my previous patch so warning
is issued on weak property as receiver and not on
any other use of a weak property. // rdar://10225276

Modified:
    cfe/trunk/include/clang/Sema/Sema.h
    cfe/trunk/lib/Sema/SemaExprObjC.cpp
    cfe/trunk/lib/Sema/SemaPseudoObject.cpp
    cfe/trunk/test/SemaObjC/weak-receiver-warn.m

Modified: cfe/trunk/include/clang/Sema/Sema.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Sema/Sema.h?rev=155169&r1=155168&r2=155169&view=diff
==============================================================================
--- cfe/trunk/include/clang/Sema/Sema.h (original)
+++ cfe/trunk/include/clang/Sema/Sema.h Thu Apr 19 18:49:39 2012
@@ -5901,9 +5901,6 @@
                                            bool IsInstance);
 
   bool inferObjCARCLifetime(ValueDecl *decl);
-  
-  void DiagnoseARCUseOfWeakReceiver(NamedDecl *PDecl,
-                                    QualType T, SourceLocation Loc);
 
   ExprResult
   HandleExprPropertyRefExpr(const ObjCObjectPointerType *OPT,

Modified: cfe/trunk/lib/Sema/SemaExprObjC.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaExprObjC.cpp?rev=155169&r1=155168&r2=155169&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/SemaExprObjC.cpp (original)
+++ cfe/trunk/lib/Sema/SemaExprObjC.cpp Thu Apr 19 18:49:39 2012
@@ -1323,30 +1323,48 @@
   return 0;
 }
 
-void 
-Sema::DiagnoseARCUseOfWeakReceiver(NamedDecl *PDecl,
-                                   QualType T, SourceLocation Loc) {
-  if (!getLangOpts().ObjCAutoRefCount)
+static void DiagnoseARCUseOfWeakReceiver(Sema &S, Expr *Receiver) {
+  if (!Receiver)
     return;
   
-  if (T.getObjCLifetime() == Qualifiers::OCL_Weak) {
-    Diag(Loc, diag::warn_receiver_is_weak) 
-      << (!PDecl ? 0 : (isa<ObjCPropertyDecl>(PDecl) ? 1 : 2));
-    if (PDecl) {
-      if (isa<ObjCPropertyDecl>(PDecl))
-        Diag(PDecl->getLocation(), diag::note_property_declare);
-      else
-        Diag(PDecl->getLocation(), diag::note_method_declared_at) << PDecl;
+  Expr *RExpr = Receiver->IgnoreParenImpCasts();
+  SourceLocation Loc = RExpr->getLocStart();
+  QualType T = RExpr->getType();
+  ObjCPropertyDecl *PDecl = 0;
+  ObjCMethodDecl *GDecl = 0;
+  if (PseudoObjectExpr *POE = dyn_cast<PseudoObjectExpr>(RExpr)) {
+    RExpr = POE->getSyntacticForm();
+    if (ObjCPropertyRefExpr *PRE = dyn_cast<ObjCPropertyRefExpr>(RExpr)) {
+      if (PRE->isImplicitProperty()) {
+        GDecl = PRE->getImplicitPropertyGetter();
+        if (GDecl) {
+          T = GDecl->getResultType();
+        }
+      }
+      else {
+        PDecl = PRE->getExplicitProperty();
+        if (PDecl) {
+          T = PDecl->getType();
+        }
+      }
     }
+  }
+  
+  if (T.getObjCLifetime() == Qualifiers::OCL_Weak) {
+    S.Diag(Loc, diag::warn_receiver_is_weak) 
+      << ((!PDecl && !GDecl) ? 0 : (PDecl ? 1 : 2));
+    if (PDecl)
+      S.Diag(PDecl->getLocation(), diag::note_property_declare);
+    else if (GDecl)
+      S.Diag(GDecl->getLocation(), diag::note_method_declared_at) << GDecl;
     return;
   }
   
-  if (PDecl)
-    if (ObjCPropertyDecl *Prop = dyn_cast<ObjCPropertyDecl>(PDecl))
-      if (Prop->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_weak) {
-        Diag(Loc, diag::warn_receiver_is_weak) << 1;
-        Diag(Prop->getLocation(), diag::note_property_declare);
-      }
+  if (PDecl && 
+      (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_weak)) {
+    S.Diag(Loc, diag::warn_receiver_is_weak) << 1;
+    S.Diag(PDecl->getLocation(), diag::note_property_declare);
+  }
 }
 
 /// HandleExprPropertyRefExpr - Handle foo.bar where foo is a pointer to an
@@ -2404,10 +2422,7 @@
   }
 
   if (getLangOpts().ObjCAutoRefCount) {
-    if (Receiver)
-      DiagnoseARCUseOfWeakReceiver(0 /* PDecl */,
-                                   Receiver->IgnoreParenImpCasts()->getType(),
-                                   Receiver->getLocStart());
+    DiagnoseARCUseOfWeakReceiver(*this, Receiver);
     
     // In ARC, annotate delegate init calls.
     if (Result->getMethodFamily() == OMF_init &&

Modified: cfe/trunk/lib/Sema/SemaPseudoObject.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaPseudoObject.cpp?rev=155169&r1=155168&r2=155169&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/SemaPseudoObject.cpp (original)
+++ cfe/trunk/lib/Sema/SemaPseudoObject.cpp Thu Apr 19 18:49:39 2012
@@ -478,8 +478,6 @@
   if (RefExpr->isImplicitProperty()) {
     if ((Getter = RefExpr->getImplicitPropertyGetter())) {
       GetterSelector = Getter->getSelector();
-      S.DiagnoseARCUseOfWeakReceiver(Getter, Getter->getResultType(),
-                                     RefExpr->getLocation());
       return true;
     }
     else {
@@ -499,8 +497,6 @@
   }
 
   ObjCPropertyDecl *prop = RefExpr->getExplicitProperty();
-  S.DiagnoseARCUseOfWeakReceiver(prop, prop->getType(),
-                                 RefExpr->getLocation());
   Getter = LookupMethodInReceiverType(S, prop->getGetterName(), RefExpr);
   return (Getter != 0);
 }

Modified: cfe/trunk/test/SemaObjC/weak-receiver-warn.m
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/SemaObjC/weak-receiver-warn.m?rev=155169&r1=155168&r2=155169&view=diff
==============================================================================
--- cfe/trunk/test/SemaObjC/weak-receiver-warn.m (original)
+++ cfe/trunk/test/SemaObjC/weak-receiver-warn.m Thu Apr 19 18:49:39 2012
@@ -23,21 +23,21 @@
   __weak Test* weak_prop;
 }
 - (void) Meth;
- at property  __weak Test* weak_prop; // expected-note 2 {{property declared here}}
- at property (weak, atomic) id weak_atomic_prop; // expected-note 2 {{property declared here}}
-- (__weak id) P; // expected-note 2 {{method 'P' declared here}}
+ at property  __weak Test* weak_prop; // expected-note {{property declared here}}
+ at property (weak, atomic) id weak_atomic_prop; // expected-note {{property declared here}}
+- (__weak id) P; // expected-note {{method 'P' declared here}}
 @end
 
 @implementation Test
 - (void) Meth {
-    if (self.weak_prop) { // expected-warning {{weak property may be unpredictably null in ARC mode}}
+    if (self.weak_prop) {
       self.weak_prop = 0;
     }
-    if (self.weak_atomic_prop) { // expected-warning {{weak property may be unpredictably null in ARC mode}}
+    if (self.weak_atomic_prop) {
       self.weak_atomic_prop = 0;
     }
     [self.weak_prop Meth]; // expected-warning {{weak property may be unpredictably null in ARC mode}}
-    id pi = self.P; // expected-warning {{weak implicit property may be unpredictably null in ARC mode}}
+    id pi = self.P;
 
     [self.weak_atomic_prop Meth];  // expected-warning {{weak property may be unpredictably null in ARC mode}}
 





More information about the cfe-commits mailing list