r196317 - [objc] Emit warnings when the implementation of a designated initializer calls on
Argyrios Kyrtzidis
akyrtzi at gmail.com
Tue Dec 3 13:11:43 PST 2013
Author: akirtzidis
Date: Tue Dec 3 15:11:43 2013
New Revision: 196317
URL: http://llvm.org/viewvc/llvm-project?rev=196317&view=rev
Log:
[objc] Emit warnings when the implementation of a designated initializer calls on
super an initializer that is not a designated one or any initializer on self.
Modified:
cfe/trunk/include/clang/AST/DeclObjC.h
cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td
cfe/trunk/lib/AST/DeclObjC.cpp
cfe/trunk/lib/Sema/SemaDecl.cpp
cfe/trunk/lib/Sema/SemaExprObjC.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=196317&r1=196316&r2=196317&view=diff
==============================================================================
--- cfe/trunk/include/clang/AST/DeclObjC.h (original)
+++ cfe/trunk/include/clang/AST/DeclObjC.h Tue Dec 3 15:11:43 2013
@@ -457,7 +457,12 @@ public:
/// Returns true if the method selector resolves to a designated initializer
/// in the class's interface.
- bool isDesignatedInitializerForTheInterface() const;
+ ///
+ /// \param InitMethod if non-null and the function returns true, it receives
+ /// the method declaration that was marked with the designated initializer
+ /// attribute.
+ bool isDesignatedInitializerForTheInterface(
+ const ObjCMethodDecl **InitMethod = 0) const;
/// \brief Determine whether this method has a body.
virtual bool hasBody() const { return Body.isValid(); }
Modified: cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td?rev=196317&r1=196316&r2=196317&view=diff
==============================================================================
--- cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td (original)
+++ cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td Tue Dec 3 15:11:43 2013
@@ -2437,6 +2437,12 @@ def warn_objc_designated_init_missing_su
InGroup<ObjCDesignatedInit>;
def note_objc_designated_init_marked_here : Note<
"method marked as designated initializer of the class here">;
+def warn_objc_designated_init_non_super_designated_init_call : Warning<
+ "designated initializer should only invoke a designated initializer on 'super'">,
+ InGroup<ObjCDesignatedInit>;
+def warn_objc_designated_init_non_designated_init_call : Warning<
+ "designated initializer invoked a non-designated initializer">,
+ InGroup<ObjCDesignatedInit>;
def err_ns_bridged_not_interface : Error<
"parameter of 'ns_bridged' attribute does not name an Objective-C class">;
Modified: cfe/trunk/lib/AST/DeclObjC.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/AST/DeclObjC.cpp?rev=196317&r1=196316&r2=196317&view=diff
==============================================================================
--- cfe/trunk/lib/AST/DeclObjC.cpp (original)
+++ cfe/trunk/lib/AST/DeclObjC.cpp Tue Dec 3 15:11:43 2013
@@ -655,12 +655,15 @@ bool ObjCMethodDecl::isThisDeclarationAD
hasAttr<ObjCDesignatedInitializerAttr>();
}
-bool ObjCMethodDecl::isDesignatedInitializerForTheInterface() const {
+bool ObjCMethodDecl::isDesignatedInitializerForTheInterface(
+ const ObjCMethodDecl **InitMethod) const {
+ if (getMethodFamily() != OMF_init)
+ return false;
const DeclContext *DC = getDeclContext();
if (isa<ObjCProtocolDecl>(DC))
return false;
if (const ObjCInterfaceDecl *ID = getClassInterface())
- return ID->isDesignatedInitializer(getSelector());
+ return ID->isDesignatedInitializer(getSelector(), InitMethod);
return false;
}
Modified: cfe/trunk/lib/Sema/SemaDecl.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaDecl.cpp?rev=196317&r1=196316&r2=196317&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/SemaDecl.cpp (original)
+++ cfe/trunk/lib/Sema/SemaDecl.cpp Tue Dec 3 15:11:43 2013
@@ -9816,8 +9816,8 @@ Decl *Sema::ActOnFinishFunctionBody(Decl
}
if (getCurFunction()->ObjCWarnForNoDesignatedInitChain) {
const ObjCMethodDecl *InitMethod = 0;
- bool isDesignated = MD->getClassInterface()
- ->isDesignatedInitializer(MD->getSelector(), &InitMethod);
+ bool isDesignated =
+ MD->isDesignatedInitializerForTheInterface(&InitMethod);
assert(isDesignated && InitMethod);
(void)isDesignated;
Diag(MD->getLocation(),
Modified: cfe/trunk/lib/Sema/SemaExprObjC.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaExprObjC.cpp?rev=196317&r1=196316&r2=196317&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/SemaExprObjC.cpp (original)
+++ cfe/trunk/lib/Sema/SemaExprObjC.cpp Tue Dec 3 15:11:43 2013
@@ -2447,14 +2447,33 @@ ExprResult Sema::BuildInstanceMessage(Ex
}
}
- if (SuperLoc.isValid() && getCurFunction()->ObjCIsDesignatedInit) {
- if (const ObjCObjectPointerType *
- OCIType = ReceiverType->getAsObjCInterfacePointerType()) {
- if (const ObjCInterfaceDecl *ID = OCIType->getInterfaceDecl()) {
- if (ID->isDesignatedInitializer(Sel))
- getCurFunction()->ObjCWarnForNoDesignatedInitChain = false;
+ if (Method && Method->getMethodFamily() == OMF_init &&
+ getCurFunction()->ObjCIsDesignatedInit &&
+ (SuperLoc.isValid() || isSelfExpr(Receiver))) {
+ bool isDesignatedInitChain = false;
+ if (SuperLoc.isValid()) {
+ if (const ObjCObjectPointerType *
+ OCIType = ReceiverType->getAsObjCInterfacePointerType()) {
+ if (const ObjCInterfaceDecl *ID = OCIType->getInterfaceDecl()) {
+ if (ID->isDesignatedInitializer(Sel)) {
+ isDesignatedInitChain = true;
+ getCurFunction()->ObjCWarnForNoDesignatedInitChain = false;
+ }
+ }
}
}
+ if (!isDesignatedInitChain) {
+ const ObjCMethodDecl *InitMethod = 0;
+ bool isDesignated =
+ getCurMethodDecl()->isDesignatedInitializerForTheInterface(&InitMethod);
+ assert(isDesignated && InitMethod);
+ (void)isDesignated;
+ Diag(SelLoc, SuperLoc.isValid() ?
+ diag::warn_objc_designated_init_non_designated_init_call :
+ diag::warn_objc_designated_init_non_super_designated_init_call);
+ Diag(InitMethod->getLocation(),
+ diag::note_objc_designated_init_marked_here);
+ }
}
// Check the message arguments.
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=196317&r1=196316&r2=196317&view=diff
==============================================================================
--- cfe/trunk/test/SemaObjC/attr-designated-init.m (original)
+++ cfe/trunk/test/SemaObjC/attr-designated-init.m Tue Dec 3 15:11:43 2013
@@ -1,4 +1,4 @@
-// RUN: %clang_cc1 -fsyntax-only -verify %s
+// RUN: %clang_cc1 -fsyntax-only -verify -fblocks %s
#define NS_DESIGNATED_INITIALIZER __attribute__((objc_designated_initializer))
@@ -35,7 +35,7 @@ __attribute__((objc_root_class))
@interface B1
-(id)initB1 NS_DESIGNATED_INITIALIZER; // expected-note 2 {{method marked as designated initializer of the class here}}
-(id)initB2;
--(id)initB3 NS_DESIGNATED_INITIALIZER;
+-(id)initB3 NS_DESIGNATED_INITIALIZER; // expected-note {{method marked as designated initializer of the class here}}
@end
@implementation B1
@@ -47,8 +47,8 @@ __attribute__((objc_root_class))
@interface S1 : B1
-(id)initS1 NS_DESIGNATED_INITIALIZER; // expected-note {{method marked as designated initializer of the class here}}
-(id)initS2 NS_DESIGNATED_INITIALIZER;
--(id)initS3 NS_DESIGNATED_INITIALIZER; // expected-note {{method marked as designated initializer of the class here}}
--(id)initS4 NS_DESIGNATED_INITIALIZER; // expected-note {{method marked as designated initializer of the class here}}
+-(id)initS3 NS_DESIGNATED_INITIALIZER; // expected-note 2 {{method marked as designated initializer of the class here}}
+-(id)initS4 NS_DESIGNATED_INITIALIZER; // expected-note 2 {{method marked as designated initializer of the class here}}
-(id)initB1;
@end
@@ -60,10 +60,10 @@ __attribute__((objc_root_class))
return [super initB1];
}
-(id)initS3 { // expected-warning {{designated initializer missing a 'super' call to a designated initializer of the super class}}
- return [super initB2];
+ return [super initB2]; // expected-warning {{designated initializer invoked a non-designated initializer}}
}
-(id)initS4 { // expected-warning {{designated initializer missing a 'super' call to a designated initializer of the super class}}
- return [self initB1];
+ return [self initB1]; // expected-warning {{designated initializer should only invoke a designated initializer on 'super'}}
}
-(id)initB1 {
return [self initS1];
@@ -92,12 +92,12 @@ __attribute__((objc_root_class))
@end
@interface SS3 : S3
--(id)initSS1 NS_DESIGNATED_INITIALIZER; // expected-note {{method marked as designated initializer of the class here}}
+-(id)initSS1 NS_DESIGNATED_INITIALIZER; // expected-note 2 {{method marked as designated initializer of the class here}}
@end
@implementation SS3
-(id)initSS1 { // expected-warning {{designated initializer missing a 'super' call to a designated initializer of the super class}}
- return [super initB1];
+ return [super initB1]; // expected-warning {{designated initializer invoked a non-designated initializer}}
}
@end
@@ -116,6 +116,7 @@ __attribute__((objc_root_class))
@end
@interface S5 : B1
+-(void)meth;
@end
@implementation S5
@@ -123,6 +124,14 @@ __attribute__((objc_root_class))
return 0;
}
-(id)initB3 {
+ [self initB1]; // expected-warning {{designated initializer should only invoke a designated initializer on 'super'}}
+ S5 *s;
+ [s initB1];
+ [self meth];
+ void (^blk)(void) = ^{
+ [self initB1];
+ };
return [super initB3];
}
+-(void)meth {}
@end
More information about the cfe-commits
mailing list