]> granicus.if.org Git - clang/commitdiff
Objective-C. Consider blocks for designated initializer
authorFariborz Jahanian <fjahanian@apple.com>
Mon, 17 Mar 2014 21:41:40 +0000 (21:41 +0000)
committerFariborz Jahanian <fjahanian@apple.com>
Mon, 17 Mar 2014 21:41:40 +0000 (21:41 +0000)
warnings (warning or lack there of) as well since
blocks are another pattern for envoking other
designated initializers. // rdar://16323233

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@204081 91177308-0d34-0410-b5e6-96231b3b80d8

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

index 5e72f769d7f8efe640af37e3397b31f51185f253..5e27001858f0825b3fec35650eafee96bf4548eb 100644 (file)
@@ -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())
index 7a3aa46ae6b85cc1bd91a3d25ed035b0f6576de5..d878179d781fb8b608942dcc0134a61d05c11191 100644 (file)
@@ -2499,8 +2499,12 @@ ExprResult Sema::BuildInstanceMessage(Expr *Receiver,
     }
   }
 
-  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(Expr *Receiver,
           if (!ID->declaresOrInheritsDesignatedInitializers() ||
               ID->isDesignatedInitializer(Sel)) {
             isDesignatedInitChain = true;
-            getCurFunction()->ObjCWarnForNoDesignatedInitChain = false;
+            DIFunctionScopeInfo->ObjCWarnForNoDesignatedInitChain = false;
           }
         }
       }
@@ -2531,13 +2535,13 @@ ExprResult Sema::BuildInstanceMessage(Expr *Receiver,
     }
   }
 
-  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;
     }
   }
 
index fccf21536cae473ce929c85611ae0d04aeaa7b26..c743afe82fb0c19877634c9e1b4bb7a2b43f822f 100644 (file)
@@ -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))
+@interface B4 
+-(id)initB4 NS_DESIGNATED_INITIALIZER; 
+@end
+
+@interface rdar16323233 : B4
+-(id)initS4 NS_DESIGNATED_INITIALIZER;
+@end
+
+@implementation rdar16323233
+-(id)initS4 {
+    static id sSharedObject = (void*)0;
+    (void)^(void) {
+        sSharedObject = [super initB4];
+    };
+    return 0;
+}
+-(id)initB4 {
+   return [self initS4];
+}
+@end