r204081 - Objective-C. Consider blocks for designated initializer

Fariborz Jahanian fjahanian at apple.com
Mon Mar 17 14:41:41 PDT 2014


Author: fjahanian
Date: Mon Mar 17 16:41:40 2014
New Revision: 204081

URL: http://llvm.org/viewvc/llvm-project?rev=204081&view=rev
Log:
Objective-C. Consider blocks for designated initializer
warnings (warning or lack there of) as well since
blocks are another pattern for envoking other
designated initializers. // rdar://16323233

Modified:
    cfe/trunk/include/clang/Sema/Sema.h
    cfe/trunk/lib/Sema/SemaExprObjC.cpp
    cfe/trunk/test/SemaObjC/attr-designated-init.m

Modified: cfe/trunk/include/clang/Sema/Sema.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Sema/Sema.h?rev=204081&r1=204080&r2=204081&view=diff
==============================================================================
--- cfe/trunk/include/clang/Sema/Sema.h (original)
+++ cfe/trunk/include/clang/Sema/Sema.h Mon Mar 17 16:41:40 2014
@@ -1013,6 +1013,18 @@ public:
     return FunctionScopes.back();
   }
   
+  sema::FunctionScopeInfo *getEnclosingFunction() const {
+    if (FunctionScopes.empty())
+      return 0;
+    
+    for (int e = FunctionScopes.size()-1; e >= 0; --e) {
+      if (isa<sema::BlockScopeInfo>(FunctionScopes[e]))
+        continue;
+      return FunctionScopes[e];
+    }
+    return 0;
+  }
+  
   template <typename ExprT>
   void recordUseOfEvaluatedWeak(const ExprT *E, bool IsRead=true) {
     if (!isUnevaluatedContext())

Modified: cfe/trunk/lib/Sema/SemaExprObjC.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaExprObjC.cpp?rev=204081&r1=204080&r2=204081&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/SemaExprObjC.cpp (original)
+++ cfe/trunk/lib/Sema/SemaExprObjC.cpp Mon Mar 17 16:41:40 2014
@@ -2499,8 +2499,12 @@ ExprResult Sema::BuildInstanceMessage(Ex
     }
   }
 
-  if (Method && Method->getMethodFamily() == OMF_init &&
-      getCurFunction()->ObjCIsDesignatedInit &&
+  FunctionScopeInfo *DIFunctionScopeInfo =
+    (Method && Method->getMethodFamily() == OMF_init)
+      ? getEnclosingFunction() : 0;
+  
+  if (DIFunctionScopeInfo &&
+      DIFunctionScopeInfo->ObjCIsDesignatedInit &&
       (SuperLoc.isValid() || isSelfExpr(Receiver))) {
     bool isDesignatedInitChain = false;
     if (SuperLoc.isValid()) {
@@ -2512,7 +2516,7 @@ ExprResult Sema::BuildInstanceMessage(Ex
           if (!ID->declaresOrInheritsDesignatedInitializers() ||
               ID->isDesignatedInitializer(Sel)) {
             isDesignatedInitChain = true;
-            getCurFunction()->ObjCWarnForNoDesignatedInitChain = false;
+            DIFunctionScopeInfo->ObjCWarnForNoDesignatedInitChain = false;
           }
         }
       }
@@ -2531,13 +2535,13 @@ ExprResult Sema::BuildInstanceMessage(Ex
     }
   }
 
-  if (Method && Method->getMethodFamily() == OMF_init &&
-      getCurFunction()->ObjCIsSecondaryInit &&
+  if (DIFunctionScopeInfo &&
+      DIFunctionScopeInfo->ObjCIsSecondaryInit &&
       (SuperLoc.isValid() || isSelfExpr(Receiver))) {
     if (SuperLoc.isValid()) {
       Diag(SelLoc, diag::warn_objc_secondary_init_super_init_call);
     } else {
-      getCurFunction()->ObjCWarnForNoInitDelegation = false;
+      DIFunctionScopeInfo->ObjCWarnForNoInitDelegation = false;
     }
   }
 

Modified: cfe/trunk/test/SemaObjC/attr-designated-init.m
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/SemaObjC/attr-designated-init.m?rev=204081&r1=204080&r2=204081&view=diff
==============================================================================
--- cfe/trunk/test/SemaObjC/attr-designated-init.m (original)
+++ cfe/trunk/test/SemaObjC/attr-designated-init.m Mon Mar 17 16:41:40 2014
@@ -36,7 +36,7 @@ __attribute__((objc_root_class))
 @interface B1
 -(id)initB1 NS_DESIGNATED_INITIALIZER; // expected-note 6 {{method marked as designated initializer of the class here}}
 -(id)initB2;
--(id)initB3 NS_DESIGNATED_INITIALIZER; // expected-note 3 {{method marked as designated initializer of the class here}}
+-(id)initB3 NS_DESIGNATED_INITIALIZER; // expected-note 4 {{method marked as designated initializer of the class here}}
 @end
 
 @implementation B1
@@ -131,7 +131,7 @@ __attribute__((objc_root_class))
   [s initB1];
   [self meth];
   void (^blk)(void) = ^{
-    [self initB1];
+    [self initB1]; // expected-warning {{designated initializer should only invoke a designated initializer on 'super'}}
   };
   return [super initB3];
 }
@@ -168,7 +168,7 @@ __attribute__((objc_root_class))
 -(id)initS5 {
   [super initB1]; // expected-warning {{secondary initializer should not invoke an initializer on 'super'}}
   void (^blk)(void) = ^{
-    [super initB1];
+    [super initB1]; // expected-warning {{secondary initializer should not invoke an initializer on 'super'}}
   };
   return [self initS1];
 }
@@ -262,3 +262,26 @@ __attribute__((objc_root_class))
    return ((void*)0);
 }
 @end
+
+// rdar://16323233
+__attribute__((objc_root_class))
+ at interface B4 
+-(id)initB4 NS_DESIGNATED_INITIALIZER; 
+ at end
+
+ at interface rdar16323233 : B4
+-(id)initS4 NS_DESIGNATED_INITIALIZER;
+ at end
+
+ at implementation rdar16323233
+-(id)initS4 {
+    static id sSharedObject = (void*)0;
+    (void)^(void) {
+        sSharedObject = [super initB4];
+    };
+    return 0;
+}
+-(id)initB4 {
+   return [self initS4];
+}
+ at end





More information about the cfe-commits mailing list