blob: e5fe1036bfb3b6e2bb65386588044c4dda2c5198 [file] [log] [blame]
Ted Kremenek28eace62013-11-23 01:01:34 +00001// RUN: %clang_cc1 -triple x86_64-apple-darwin11 -fsyntax-only -verify %s -Wno-objc-root-class
2
Ted Kremenekf41cf7f12013-12-10 19:43:48 +00003// Mark this protocol as requiring all of its methods and properties
4// to be explicitly implemented in the adopting class.
5__attribute__((objc_protocol_requires_explicit_implementation))
Ted Kremenek28eace62013-11-23 01:01:34 +00006@protocol Protocol
7- (void) theBestOfTimes; // expected-note {{method 'theBestOfTimes' declared here}}
Ted Kremenekf41cf7f12013-12-10 19:43:48 +00008@property (readonly) id theWorstOfTimes;
Ted Kremenek28eace62013-11-23 01:01:34 +00009@end
10
Ted Kremenekf41cf7f12013-12-10 19:43:48 +000011// In this example, ClassA adopts the protocol. We won't
12// provide the implementation here, but this protocol will
13// be adopted later by a subclass.
Ted Kremenek28eace62013-11-23 01:01:34 +000014@interface ClassA <Protocol>
15- (void) theBestOfTimes;
Ted Kremenek28eace62013-11-23 01:01:34 +000016@property (readonly) id theWorstOfTimes;
17@end
18
Ted Kremenekf41cf7f12013-12-10 19:43:48 +000019// This class subclasses ClassA (which adopts 'Protocol'),
20// but does not provide the needed implementation.
Ted Kremenek2ccf19e2013-12-13 05:58:51 +000021@interface ClassB : ClassA <Protocol>
Ted Kremenek28eace62013-11-23 01:01:34 +000022@end
23
Ted Kremenek2ccf19e2013-12-13 05:58:51 +000024@implementation ClassB // expected-warning {{method 'theBestOfTimes' in protocol 'Protocol' not implemented}}
Ted Kremenek28eace62013-11-23 01:01:34 +000025@end
26
Ted Kremenekc152c522013-12-12 06:20:42 +000027// Test that inherited protocols do not get the explicit conformance requirement.
28@protocol Inherited
29- (void) fairIsFoul;
30@end
31
32__attribute__((objc_protocol_requires_explicit_implementation))
33@protocol Derived <Inherited>
34- (void) foulIsFair; // expected-note {{method 'foulIsFair' declared here}}
35@end
36
37@interface ClassC <Inherited>
38@end
39
Ted Kremenek2ccf19e2013-12-13 05:58:51 +000040@interface ClassD : ClassC <Derived>
Ted Kremenekc152c522013-12-12 06:20:42 +000041@end
42
Ted Kremenek2ccf19e2013-12-13 05:58:51 +000043@implementation ClassD // expected-warning {{method 'foulIsFair' in protocol 'Derived' not implemented}}
Ted Kremenekc152c522013-12-12 06:20:42 +000044@end
45
Ted Kremenekf41cf7f12013-12-10 19:43:48 +000046// Test that the attribute is used correctly.
47__attribute__((objc_protocol_requires_explicit_implementation(1+2))) // expected-error {{attribute takes no arguments}}
48@protocol AnotherProtocol @end
Ted Kremenek28eace62013-11-23 01:01:34 +000049
Ted Kremenekf41cf7f12013-12-10 19:43:48 +000050// Cannot put the attribute on classes or other non-protocol declarations.
51__attribute__((objc_protocol_requires_explicit_implementation)) // expected-error {{attribute only applies to Objective-C protocols}}
52@interface AnotherClass @end
Ted Kremenek28eace62013-11-23 01:01:34 +000053
Ted Kremenekf41cf7f12013-12-10 19:43:48 +000054__attribute__((objc_protocol_requires_explicit_implementation)) // expected-error {{attribute only applies to Objective-C protocols}}
55int x;
Ted Kremenek28eace62013-11-23 01:01:34 +000056
Ted Kremenek33e430f2013-12-13 06:26:14 +000057// Test that inherited protocols with the attribute
58// are treated properly.
59__attribute__((objc_protocol_requires_explicit_implementation))
60@protocol ProtocolA
61@required
62- (void)rlyeh;
63- (void)innsmouth;
64@end
65
66@protocol ProtocolB <ProtocolA>
67@required
68- (void)dunwich;
69- (id)innsmouth;
70@end
71
72@protocol ProtocolC
73@required
74- (void)rlyeh;
75- (void)innsmouth;
76- (void)dunwich;
77@end
78
79@interface MyObject <ProtocolC>
80@end
81
82@interface MyLovecraft <ProtocolA>
83@end
84
85@interface MyShoggoth : MyLovecraft <ProtocolB>
86@end
87
88@implementation MyObject
89- (void)innsmouth {}
90- (void)rlyeh {}
91- (void)dunwich {}
92@end
93
94@implementation MyLovecraft
95- (void)innsmouth {}
96- (void)rlyeh {}
97@end
98
99@implementation MyShoggoth
100- (void)dunwich {}
101@end
102