2013-11-23 09:01:34 +08:00
|
|
|
// RUN: %clang_cc1 -triple x86_64-apple-darwin11 -fsyntax-only -verify %s -Wno-objc-root-class
|
|
|
|
|
2013-12-11 03:43:48 +08:00
|
|
|
// Mark this protocol as requiring all of its methods and properties
|
|
|
|
// to be explicitly implemented in the adopting class.
|
|
|
|
__attribute__((objc_protocol_requires_explicit_implementation))
|
2013-11-23 09:01:34 +08:00
|
|
|
@protocol Protocol
|
2014-02-27 09:28:58 +08:00
|
|
|
- (void) theBestOfTimes; // expected-note 2 {{method 'theBestOfTimes' declared here}}
|
2014-02-22 03:41:39 +08:00
|
|
|
@property (readonly) id theWorstOfTimes; // expected-note {{property declared here}}
|
2013-11-23 09:01:34 +08:00
|
|
|
@end
|
|
|
|
|
2013-12-11 03:43:48 +08:00
|
|
|
// In this example, ClassA adopts the protocol. We won't
|
|
|
|
// provide the implementation here, but this protocol will
|
|
|
|
// be adopted later by a subclass.
|
|
|
|
@interface ClassA <Protocol>
|
2013-11-23 09:01:34 +08:00
|
|
|
- (void) theBestOfTimes;
|
|
|
|
@property (readonly) id theWorstOfTimes;
|
|
|
|
@end
|
|
|
|
|
2013-12-11 03:43:48 +08:00
|
|
|
// This class subclasses ClassA (which adopts 'Protocol'),
|
|
|
|
// but does not provide the needed implementation.
|
2013-12-13 13:58:51 +08:00
|
|
|
@interface ClassB : ClassA <Protocol>
|
2013-11-23 09:01:34 +08:00
|
|
|
@end
|
|
|
|
|
2014-02-22 03:41:39 +08:00
|
|
|
@implementation ClassB // expected-warning {{method 'theBestOfTimes' in protocol 'Protocol' not implemented}} expected-warning {{property 'theWorstOfTimes' requires method 'theWorstOfTimes' to be defined - use @synthesize, @dynamic or provide a method implementation in this class implementation}}
|
|
|
|
@end
|
|
|
|
|
|
|
|
@interface ClassB_Good : ClassA <Protocol>
|
|
|
|
@end
|
|
|
|
|
|
|
|
@implementation ClassB_Good // no-warning
|
|
|
|
- (void) theBestOfTimes {}
|
|
|
|
@dynamic theWorstOfTimes;
|
2013-11-23 09:01:34 +08:00
|
|
|
@end
|
|
|
|
|
2014-02-22 08:02:03 +08:00
|
|
|
@interface ClassB_AlsoGood : ClassA <Protocol>
|
|
|
|
@property (readonly) id theWorstOfTimes;
|
|
|
|
@end
|
|
|
|
|
|
|
|
// Default synthesis acts as if @dynamic
|
|
|
|
// had been written for 'theWorstOfTimes' because
|
|
|
|
// it is declared in ClassA. This is okay, since
|
|
|
|
// the author of ClassB_AlsoGood needs explicitly
|
|
|
|
// write @property in the @interface.
|
|
|
|
@implementation ClassB_AlsoGood // no-warning
|
|
|
|
- (void) theBestOfTimes {}
|
|
|
|
@end
|
|
|
|
|
2013-12-12 14:20:42 +08:00
|
|
|
// Test that inherited protocols do not get the explicit conformance requirement.
|
|
|
|
@protocol Inherited
|
|
|
|
- (void) fairIsFoul;
|
|
|
|
@end
|
|
|
|
|
|
|
|
__attribute__((objc_protocol_requires_explicit_implementation))
|
|
|
|
@protocol Derived <Inherited>
|
|
|
|
- (void) foulIsFair; // expected-note {{method 'foulIsFair' declared here}}
|
|
|
|
@end
|
|
|
|
|
|
|
|
@interface ClassC <Inherited>
|
|
|
|
@end
|
|
|
|
|
2013-12-13 13:58:51 +08:00
|
|
|
@interface ClassD : ClassC <Derived>
|
2013-12-12 14:20:42 +08:00
|
|
|
@end
|
|
|
|
|
2013-12-13 13:58:51 +08:00
|
|
|
@implementation ClassD // expected-warning {{method 'foulIsFair' in protocol 'Derived' not implemented}}
|
2013-12-12 14:20:42 +08:00
|
|
|
@end
|
|
|
|
|
2013-12-11 03:43:48 +08:00
|
|
|
// Test that the attribute is used correctly.
|
|
|
|
__attribute__((objc_protocol_requires_explicit_implementation(1+2))) // expected-error {{attribute takes no arguments}}
|
|
|
|
@protocol AnotherProtocol @end
|
2013-11-23 09:01:34 +08:00
|
|
|
|
2013-12-11 03:43:48 +08:00
|
|
|
// Cannot put the attribute on classes or other non-protocol declarations.
|
|
|
|
__attribute__((objc_protocol_requires_explicit_implementation)) // expected-error {{attribute only applies to Objective-C protocols}}
|
|
|
|
@interface AnotherClass @end
|
2013-11-23 09:01:34 +08:00
|
|
|
|
2013-12-11 03:43:48 +08:00
|
|
|
__attribute__((objc_protocol_requires_explicit_implementation)) // expected-error {{attribute only applies to Objective-C protocols}}
|
|
|
|
int x;
|
2013-11-23 09:01:34 +08:00
|
|
|
|
2013-12-13 14:26:14 +08:00
|
|
|
// Test that inherited protocols with the attribute
|
|
|
|
// are treated properly.
|
|
|
|
__attribute__((objc_protocol_requires_explicit_implementation))
|
|
|
|
@protocol ProtocolA
|
|
|
|
@required
|
2014-01-17 16:34:19 +08:00
|
|
|
- (void)rlyeh; // expected-note 2 {{method 'rlyeh' declared here}}
|
|
|
|
- (void)innsmouth; // expected-note 2 {{method 'innsmouth' declared here}}
|
2013-12-13 14:26:14 +08:00
|
|
|
@end
|
|
|
|
|
|
|
|
@protocol ProtocolB <ProtocolA>
|
|
|
|
@required
|
|
|
|
- (void)dunwich;
|
2014-01-17 16:34:19 +08:00
|
|
|
- (void)innsmouth; // expected-note {{method 'innsmouth' declared here}}
|
|
|
|
@end
|
|
|
|
|
|
|
|
__attribute__((objc_protocol_requires_explicit_implementation))
|
|
|
|
@protocol ProtocolB_Explicit <ProtocolA>
|
|
|
|
@required
|
|
|
|
- (void)dunwich;
|
|
|
|
- (void)innsmouth; // expected-note 2 {{method 'innsmouth' declared here}}
|
2013-12-13 14:26:14 +08:00
|
|
|
@end
|
|
|
|
|
|
|
|
@protocol ProtocolC
|
|
|
|
@required
|
|
|
|
- (void)rlyeh;
|
|
|
|
- (void)innsmouth;
|
|
|
|
- (void)dunwich;
|
|
|
|
@end
|
|
|
|
|
2014-01-17 16:34:19 +08:00
|
|
|
@interface MyObject <ProtocolC> @end
|
2013-12-13 14:26:14 +08:00
|
|
|
|
2014-01-17 16:34:19 +08:00
|
|
|
// Provide two variants of a base class, one that adopts ProtocolA and
|
|
|
|
// one that does not.
|
|
|
|
@interface Lovecraft <ProtocolA> @end
|
|
|
|
@interface Lovecraft_2 @end
|
|
|
|
|
|
|
|
// Provide two variants of a subclass that conform to ProtocolB. One
|
|
|
|
// subclasses from a class that conforms to ProtocolA, the other that
|
|
|
|
// does not.
|
|
|
|
//
|
|
|
|
// From those, provide two variants that conformat to ProtocolB_Explicit
|
|
|
|
// instead.
|
|
|
|
@interface Shoggoth : Lovecraft <ProtocolB> @end
|
|
|
|
@interface Shoggoth_2 : Lovecraft_2 <ProtocolB> @end
|
|
|
|
@interface Shoggoth_Explicit : Lovecraft <ProtocolB_Explicit> @end
|
|
|
|
@interface Shoggoth_2_Explicit : Lovecraft_2 <ProtocolB_Explicit> @end
|
2013-12-13 14:26:14 +08:00
|
|
|
|
|
|
|
@implementation MyObject
|
|
|
|
- (void)innsmouth {}
|
|
|
|
- (void)rlyeh {}
|
|
|
|
- (void)dunwich {}
|
|
|
|
@end
|
|
|
|
|
2014-01-17 16:34:19 +08:00
|
|
|
@implementation Lovecraft
|
2013-12-13 14:26:14 +08:00
|
|
|
- (void)innsmouth {}
|
|
|
|
- (void)rlyeh {}
|
|
|
|
@end
|
|
|
|
|
2014-01-17 16:34:19 +08:00
|
|
|
@implementation Shoggoth
|
|
|
|
- (void)dunwich {}
|
|
|
|
@end
|
|
|
|
|
|
|
|
@implementation Shoggoth_2 // expected-warning {{method 'innsmouth' in protocol 'ProtocolB' not implemented}}\
|
|
|
|
// expected-warning {{method 'rlyeh' in protocol 'ProtocolA' not implemented}}\
|
|
|
|
// expected-warning {{'innsmouth' in protocol 'ProtocolA' not implemented}}
|
|
|
|
- (void)dunwich {}
|
|
|
|
@end
|
|
|
|
|
|
|
|
@implementation Shoggoth_Explicit // expected-warning {{method 'innsmouth' in protocol 'ProtocolB_Explicit' not implemented}}
|
|
|
|
- (void)dunwich {}
|
|
|
|
@end
|
|
|
|
|
|
|
|
@implementation Shoggoth_2_Explicit // expected-warning {{method 'innsmouth' in protocol 'ProtocolB_Explicit' not implemented}}\
|
|
|
|
// expected-warning {{method 'rlyeh' in protocol 'ProtocolA' not implemented}}\
|
|
|
|
// expected-warning {{method 'innsmouth' in protocol 'ProtocolA' not implemented}}
|
2013-12-13 14:26:14 +08:00
|
|
|
- (void)dunwich {}
|
|
|
|
@end
|
|
|
|
|
2014-02-27 09:28:58 +08:00
|
|
|
// Categories adopting a protocol with explicit conformance need to implement that protocol.
|
|
|
|
@interface Parent
|
|
|
|
- (void) theBestOfTimes;
|
|
|
|
@property (readonly) id theWorstOfTimes;
|
|
|
|
@end
|
|
|
|
|
|
|
|
@interface Derived : Parent
|
|
|
|
@end
|
|
|
|
|
|
|
|
@interface Derived (MyCat) <Protocol>
|
|
|
|
@end
|
|
|
|
|
|
|
|
@implementation Derived (MyCat) // expected-warning {{method 'theBestOfTimes' in protocol 'Protocol' not implemented}}
|
|
|
|
@end
|
|
|
|
|
2014-02-22 06:49:04 +08:00
|
|
|
__attribute__((objc_protocol_requires_explicit_implementation)) // expected-error{{attribute 'objc_protocol_requires_explicit_implementation' can only be applied to @protocol definitions, not forward declarations}}
|
|
|
|
@protocol NotDefined;
|
|
|
|
|
2014-02-27 09:28:58 +08:00
|
|
|
|