r231700 - [PATCH Sema Objective-C]. Patch to warn on missing designated initializer

Fariborz Jahanian fjahanian at apple.com
Mon Mar 9 13:39:52 PDT 2015


Author: fjahanian
Date: Mon Mar  9 15:39:51 2015
New Revision: 231700

URL: http://llvm.org/viewvc/llvm-project?rev=231700&view=rev
Log:
[PATCH Sema Objective-C]. Patch to warn on missing designated initializer
override where at least a declaration of a designated initializer is in a super
class and not necessarily in the current class. rdar://19653785.

Modified:
    cfe/trunk/include/clang/AST/DeclObjC.h
    cfe/trunk/include/clang/Sema/Sema.h
    cfe/trunk/lib/AST/DeclObjC.cpp
    cfe/trunk/lib/Sema/SemaDeclObjC.cpp
    cfe/trunk/lib/Sema/SemaObjCProperty.cpp
    cfe/trunk/test/SemaObjC/attr-designated-init.m

Modified: cfe/trunk/include/clang/AST/DeclObjC.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/AST/DeclObjC.h?rev=231700&r1=231699&r2=231700&view=diff
==============================================================================
--- cfe/trunk/include/clang/AST/DeclObjC.h (original)
+++ cfe/trunk/include/clang/AST/DeclObjC.h Mon Mar  9 15:39:51 2015
@@ -803,6 +803,8 @@ public:
     return hasDesignatedInitializers() || inheritsDesignatedInitializers();
   }
 
+  bool hasDesignatedInitializersInSuperClass() const;
+
   const ObjCProtocolList &getReferencedProtocols() const {
     assert(hasDefinition() && "Caller did not check for forward reference!");
     if (data().ExternallyCompleted)

Modified: cfe/trunk/include/clang/Sema/Sema.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Sema/Sema.h?rev=231700&r1=231699&r2=231700&view=diff
==============================================================================
--- cfe/trunk/include/clang/Sema/Sema.h (original)
+++ cfe/trunk/include/clang/Sema/Sema.h Mon Mar  9 15:39:51 2015
@@ -2923,8 +2923,7 @@ public:
   void DiagnoseOwningPropertyGetterSynthesis(const ObjCImplementationDecl *D);
 
   void DiagnoseMissingDesignatedInitOverrides(
-                                          const ObjCImplementationDecl *ImplD,
-                                          const ObjCInterfaceDecl *IFD);
+                                          const ObjCImplementationDecl *ImplD);
 
   void DiagnoseDuplicateIvars(ObjCInterfaceDecl *ID, ObjCInterfaceDecl *SID);
 

Modified: cfe/trunk/lib/AST/DeclObjC.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/AST/DeclObjC.cpp?rev=231700&r1=231699&r2=231700&view=diff
==============================================================================
--- cfe/trunk/lib/AST/DeclObjC.cpp (original)
+++ cfe/trunk/lib/AST/DeclObjC.cpp Mon Mar  9 15:39:51 2015
@@ -1213,6 +1213,16 @@ bool ObjCInterfaceDecl::hasDesignatedIni
   return data().HasDesignatedInitializers;
 }
 
+bool ObjCInterfaceDecl::hasDesignatedInitializersInSuperClass() const {
+  ObjCInterfaceDecl *OSC = getSuperClass();
+  while (OSC) {
+    if (OSC->hasDesignatedInitializers())
+      return true;
+    OSC = OSC->getSuperClass();
+  }
+  return false;
+}
+
 StringRef
 ObjCInterfaceDecl::getObjCRuntimeNameAsString() const {
   if (ObjCRuntimeNameAttr *ObjCRTName = getAttr<ObjCRuntimeNameAttr>())

Modified: cfe/trunk/lib/Sema/SemaDeclObjC.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaDeclObjC.cpp?rev=231700&r1=231699&r2=231700&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/SemaDeclObjC.cpp (original)
+++ cfe/trunk/lib/Sema/SemaDeclObjC.cpp Mon Mar  9 15:39:51 2015
@@ -2715,8 +2715,9 @@ Decl *Sema::ActOnAtEnd(Scope *S, SourceR
       AtomicPropertySetterGetterRules(IC, IDecl);
       DiagnoseOwningPropertyGetterSynthesis(IC);
       DiagnoseUnusedBackingIvarInAccessor(S, IC);
-      if (IDecl->hasDesignatedInitializers())
-        DiagnoseMissingDesignatedInitOverrides(IC, IDecl);
+      if (IDecl->hasDesignatedInitializers() ||
+          IDecl->hasDesignatedInitializersInSuperClass())
+        DiagnoseMissingDesignatedInitOverrides(IC);
 
       bool HasRootClassAttr = IDecl->hasAttr<ObjCRootClassAttr>();
       if (IDecl->getSuperClass() == nullptr) {

Modified: cfe/trunk/lib/Sema/SemaObjCProperty.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaObjCProperty.cpp?rev=231700&r1=231699&r2=231700&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/SemaObjCProperty.cpp (original)
+++ cfe/trunk/lib/Sema/SemaObjCProperty.cpp Mon Mar  9 15:39:51 2015
@@ -1894,9 +1894,11 @@ void Sema::DiagnoseOwningPropertyGetterS
 }
 
 void Sema::DiagnoseMissingDesignatedInitOverrides(
-                                            const ObjCImplementationDecl *ImplD,
-                                            const ObjCInterfaceDecl *IFD) {
-  assert(IFD->hasDesignatedInitializers());
+                                            const ObjCImplementationDecl *ImplD) {
+  const ObjCInterfaceDecl *IFD = ImplD->getClassInterface();
+  assert(IFD);
+  assert(IFD->hasDesignatedInitializers() ||
+         IFD->hasDesignatedInitializersInSuperClass());
   const ObjCInterfaceDecl *SuperD = IFD->getSuperClass();
   if (!SuperD)
     return;

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=231700&r1=231699&r2=231700&view=diff
==============================================================================
--- cfe/trunk/test/SemaObjC/attr-designated-init.m (original)
+++ cfe/trunk/test/SemaObjC/attr-designated-init.m Mon Mar  9 15:39:51 2015
@@ -39,7 +39,7 @@ __attribute__((objc_root_class))
 @end
 
 @interface B1()
--(id)initB3 NS_DESIGNATED_INITIALIZER; // expected-note 4 {{method marked as designated initializer of the class here}}
+-(id)initB3 NS_DESIGNATED_INITIALIZER; // expected-note 6 {{method marked as designated initializer of the class here}}
 @end;
 
 @implementation B1
@@ -182,7 +182,7 @@ __attribute__((objc_root_class))
 -(id)initB1;
 @end
 
- at implementation SS4
+ at implementation SS4 // expected-warning {{method override for the designated initializer of the superclass '-initB3' not found}}
 -(id)initB1 { // expected-warning {{designated initializer missing a 'super' call to a designated initializer of the super class}}
   return 0;
 }
@@ -233,7 +233,7 @@ __attribute__((objc_root_class))
 -(id)initB1;
 @end
 
- at implementation SS9
+ at implementation SS9 // expected-warning {{method override for the designated initializer of the superclass '-initB3' not found}}
 -(id)initB1 { // expected-warning {{designated initializer missing a 'super' call to a designated initializer of the super class}}
   return 0;
 }
@@ -418,3 +418,41 @@ __attribute__((objc_root_class))
 @interface CategoryForMissingInterface(Cat) // expected-error{{cannot find interface declaration}}
 - (instancetype)init NS_DESIGNATED_INITIALIZER; // expected-error{{only applies to init methods of interface or class extension declarations}}
 @end
+
+// rdar://19653785
+ at class NSCoder;
+
+ at interface NSView 
+- (instancetype)initWithFrame:(int)frameRect NS_DESIGNATED_INITIALIZER; // expected-note {{method marked as designated initializer of the class here}}
+- (instancetype)initWithCoder:(NSCoder *)coder NS_DESIGNATED_INITIALIZER; // expected-note 2 {{method marked as designated initializer of the class here}}
+ at end
+
+ at interface MyHappyView : NSView
+- (instancetype)initWithFrame:(int)frameRect andOtherThing:(id)otherThing NS_DESIGNATED_INITIALIZER;
+ at end
+
+ at implementation MyHappyView // expected-warning {{method override for the designated initializer of the superclass '-initWithCoder:' not found}}
+- (instancetype)initWithFrame:(int)frameRect andOtherThing:(id)otherThing {
+ if (self = [super initWithFrame:frameRect]) {
+ }
+ return self;
+}
+
+- (instancetype)initWithFrame:(int)frameRect {
+ return [self initWithFrame:frameRect andOtherThing:((void *)0)];
+}
+ at end
+
+ at interface MySadView : NSView
+ at end
+
+ at implementation MySadView  // expected-warning {{method override for the designated initializer of the superclass '-initWithFrame:' not found}} \
+			   // expected-warning {{method override for the designated initializer of the superclass '-initWithCoder:' not found}}
+- (instancetype)initWithFrame:(int)frameRect andOtherThing:(id)otherThing {
+ if (self = [super initWithFrame:frameRect]) {
+ }
+
+ return self;
+}
+ at end
+





More information about the cfe-commits mailing list