| Ted Kremenek | 28eace6 | 2013-11-23 01:01:34 +0000 | [diff] [blame] | 1 | // RUN: %clang_cc1 -triple x86_64-apple-darwin11 -fsyntax-only -verify %s -Wno-objc-root-class |
| 2 | |
| Ted Kremenek | f41cf7f1 | 2013-12-10 19:43:48 +0000 | [diff] [blame] | 3 | // 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 Kremenek | 28eace6 | 2013-11-23 01:01:34 +0000 | [diff] [blame] | 6 | @protocol Protocol |
| Ted Kremenek | 294c082 | 2014-02-27 01:28:58 +0000 | [diff] [blame^] | 7 | - (void) theBestOfTimes; // expected-note 2 {{method 'theBestOfTimes' declared here}} |
| Ted Kremenek | 3888202 | 2014-02-21 19:41:39 +0000 | [diff] [blame] | 8 | @property (readonly) id theWorstOfTimes; // expected-note {{property declared here}} |
| Ted Kremenek | 28eace6 | 2013-11-23 01:01:34 +0000 | [diff] [blame] | 9 | @end |
| 10 | |
| Ted Kremenek | f41cf7f1 | 2013-12-10 19:43:48 +0000 | [diff] [blame] | 11 | // 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 Kremenek | 28eace6 | 2013-11-23 01:01:34 +0000 | [diff] [blame] | 14 | @interface ClassA <Protocol> |
| 15 | - (void) theBestOfTimes; |
| Ted Kremenek | 28eace6 | 2013-11-23 01:01:34 +0000 | [diff] [blame] | 16 | @property (readonly) id theWorstOfTimes; |
| 17 | @end |
| 18 | |
| Ted Kremenek | f41cf7f1 | 2013-12-10 19:43:48 +0000 | [diff] [blame] | 19 | // This class subclasses ClassA (which adopts 'Protocol'), |
| 20 | // but does not provide the needed implementation. |
| Ted Kremenek | 2ccf19e | 2013-12-13 05:58:51 +0000 | [diff] [blame] | 21 | @interface ClassB : ClassA <Protocol> |
| Ted Kremenek | 28eace6 | 2013-11-23 01:01:34 +0000 | [diff] [blame] | 22 | @end |
| 23 | |
| Ted Kremenek | 3888202 | 2014-02-21 19:41:39 +0000 | [diff] [blame] | 24 | @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}} |
| 25 | @end |
| 26 | |
| 27 | @interface ClassB_Good : ClassA <Protocol> |
| 28 | @end |
| 29 | |
| 30 | @implementation ClassB_Good // no-warning |
| 31 | - (void) theBestOfTimes {} |
| 32 | @dynamic theWorstOfTimes; |
| Ted Kremenek | 28eace6 | 2013-11-23 01:01:34 +0000 | [diff] [blame] | 33 | @end |
| 34 | |
| Ted Kremenek | 204c3c5 | 2014-02-22 00:02:03 +0000 | [diff] [blame] | 35 | @interface ClassB_AlsoGood : ClassA <Protocol> |
| 36 | @property (readonly) id theWorstOfTimes; |
| 37 | @end |
| 38 | |
| 39 | // Default synthesis acts as if @dynamic |
| 40 | // had been written for 'theWorstOfTimes' because |
| 41 | // it is declared in ClassA. This is okay, since |
| 42 | // the author of ClassB_AlsoGood needs explicitly |
| 43 | // write @property in the @interface. |
| 44 | @implementation ClassB_AlsoGood // no-warning |
| 45 | - (void) theBestOfTimes {} |
| 46 | @end |
| 47 | |
| Ted Kremenek | c152c52 | 2013-12-12 06:20:42 +0000 | [diff] [blame] | 48 | // Test that inherited protocols do not get the explicit conformance requirement. |
| 49 | @protocol Inherited |
| 50 | - (void) fairIsFoul; |
| 51 | @end |
| 52 | |
| 53 | __attribute__((objc_protocol_requires_explicit_implementation)) |
| 54 | @protocol Derived <Inherited> |
| 55 | - (void) foulIsFair; // expected-note {{method 'foulIsFair' declared here}} |
| 56 | @end |
| 57 | |
| 58 | @interface ClassC <Inherited> |
| 59 | @end |
| 60 | |
| Ted Kremenek | 2ccf19e | 2013-12-13 05:58:51 +0000 | [diff] [blame] | 61 | @interface ClassD : ClassC <Derived> |
| Ted Kremenek | c152c52 | 2013-12-12 06:20:42 +0000 | [diff] [blame] | 62 | @end |
| 63 | |
| Ted Kremenek | 2ccf19e | 2013-12-13 05:58:51 +0000 | [diff] [blame] | 64 | @implementation ClassD // expected-warning {{method 'foulIsFair' in protocol 'Derived' not implemented}} |
| Ted Kremenek | c152c52 | 2013-12-12 06:20:42 +0000 | [diff] [blame] | 65 | @end |
| 66 | |
| Ted Kremenek | f41cf7f1 | 2013-12-10 19:43:48 +0000 | [diff] [blame] | 67 | // Test that the attribute is used correctly. |
| 68 | __attribute__((objc_protocol_requires_explicit_implementation(1+2))) // expected-error {{attribute takes no arguments}} |
| 69 | @protocol AnotherProtocol @end |
| Ted Kremenek | 28eace6 | 2013-11-23 01:01:34 +0000 | [diff] [blame] | 70 | |
| Ted Kremenek | f41cf7f1 | 2013-12-10 19:43:48 +0000 | [diff] [blame] | 71 | // Cannot put the attribute on classes or other non-protocol declarations. |
| 72 | __attribute__((objc_protocol_requires_explicit_implementation)) // expected-error {{attribute only applies to Objective-C protocols}} |
| 73 | @interface AnotherClass @end |
| Ted Kremenek | 28eace6 | 2013-11-23 01:01:34 +0000 | [diff] [blame] | 74 | |
| Ted Kremenek | f41cf7f1 | 2013-12-10 19:43:48 +0000 | [diff] [blame] | 75 | __attribute__((objc_protocol_requires_explicit_implementation)) // expected-error {{attribute only applies to Objective-C protocols}} |
| 76 | int x; |
| Ted Kremenek | 28eace6 | 2013-11-23 01:01:34 +0000 | [diff] [blame] | 77 | |
| Ted Kremenek | 33e430f | 2013-12-13 06:26:14 +0000 | [diff] [blame] | 78 | // Test that inherited protocols with the attribute |
| 79 | // are treated properly. |
| 80 | __attribute__((objc_protocol_requires_explicit_implementation)) |
| 81 | @protocol ProtocolA |
| 82 | @required |
| Ted Kremenek | 15478b3 | 2014-01-17 08:34:19 +0000 | [diff] [blame] | 83 | - (void)rlyeh; // expected-note 2 {{method 'rlyeh' declared here}} |
| 84 | - (void)innsmouth; // expected-note 2 {{method 'innsmouth' declared here}} |
| Ted Kremenek | 33e430f | 2013-12-13 06:26:14 +0000 | [diff] [blame] | 85 | @end |
| 86 | |
| 87 | @protocol ProtocolB <ProtocolA> |
| 88 | @required |
| 89 | - (void)dunwich; |
| Ted Kremenek | 15478b3 | 2014-01-17 08:34:19 +0000 | [diff] [blame] | 90 | - (void)innsmouth; // expected-note {{method 'innsmouth' declared here}} |
| 91 | @end |
| 92 | |
| 93 | __attribute__((objc_protocol_requires_explicit_implementation)) |
| 94 | @protocol ProtocolB_Explicit <ProtocolA> |
| 95 | @required |
| 96 | - (void)dunwich; |
| 97 | - (void)innsmouth; // expected-note 2 {{method 'innsmouth' declared here}} |
| Ted Kremenek | 33e430f | 2013-12-13 06:26:14 +0000 | [diff] [blame] | 98 | @end |
| 99 | |
| 100 | @protocol ProtocolC |
| 101 | @required |
| 102 | - (void)rlyeh; |
| 103 | - (void)innsmouth; |
| 104 | - (void)dunwich; |
| 105 | @end |
| 106 | |
| Ted Kremenek | 15478b3 | 2014-01-17 08:34:19 +0000 | [diff] [blame] | 107 | @interface MyObject <ProtocolC> @end |
| Ted Kremenek | 33e430f | 2013-12-13 06:26:14 +0000 | [diff] [blame] | 108 | |
| Ted Kremenek | 15478b3 | 2014-01-17 08:34:19 +0000 | [diff] [blame] | 109 | // Provide two variants of a base class, one that adopts ProtocolA and |
| 110 | // one that does not. |
| 111 | @interface Lovecraft <ProtocolA> @end |
| 112 | @interface Lovecraft_2 @end |
| Ted Kremenek | 33e430f | 2013-12-13 06:26:14 +0000 | [diff] [blame] | 113 | |
| Ted Kremenek | 15478b3 | 2014-01-17 08:34:19 +0000 | [diff] [blame] | 114 | // Provide two variants of a subclass that conform to ProtocolB. One |
| 115 | // subclasses from a class that conforms to ProtocolA, the other that |
| 116 | // does not. |
| 117 | // |
| 118 | // From those, provide two variants that conformat to ProtocolB_Explicit |
| 119 | // instead. |
| 120 | @interface Shoggoth : Lovecraft <ProtocolB> @end |
| 121 | @interface Shoggoth_2 : Lovecraft_2 <ProtocolB> @end |
| 122 | @interface Shoggoth_Explicit : Lovecraft <ProtocolB_Explicit> @end |
| 123 | @interface Shoggoth_2_Explicit : Lovecraft_2 <ProtocolB_Explicit> @end |
| 124 | |
| Ted Kremenek | 33e430f | 2013-12-13 06:26:14 +0000 | [diff] [blame] | 125 | @implementation MyObject |
| 126 | - (void)innsmouth {} |
| 127 | - (void)rlyeh {} |
| 128 | - (void)dunwich {} |
| 129 | @end |
| 130 | |
| Ted Kremenek | 15478b3 | 2014-01-17 08:34:19 +0000 | [diff] [blame] | 131 | @implementation Lovecraft |
| Ted Kremenek | 33e430f | 2013-12-13 06:26:14 +0000 | [diff] [blame] | 132 | - (void)innsmouth {} |
| 133 | - (void)rlyeh {} |
| 134 | @end |
| 135 | |
| Ted Kremenek | 15478b3 | 2014-01-17 08:34:19 +0000 | [diff] [blame] | 136 | @implementation Shoggoth |
| 137 | - (void)dunwich {} |
| 138 | @end |
| 139 | |
| 140 | @implementation Shoggoth_2 // expected-warning {{method 'innsmouth' in protocol 'ProtocolB' not implemented}}\ |
| 141 | // expected-warning {{method 'rlyeh' in protocol 'ProtocolA' not implemented}}\ |
| 142 | // expected-warning {{'innsmouth' in protocol 'ProtocolA' not implemented}} |
| 143 | - (void)dunwich {} |
| 144 | @end |
| 145 | |
| 146 | @implementation Shoggoth_Explicit // expected-warning {{method 'innsmouth' in protocol 'ProtocolB_Explicit' not implemented}} |
| 147 | - (void)dunwich {} |
| 148 | @end |
| 149 | |
| 150 | @implementation Shoggoth_2_Explicit // expected-warning {{method 'innsmouth' in protocol 'ProtocolB_Explicit' not implemented}}\ |
| 151 | // expected-warning {{method 'rlyeh' in protocol 'ProtocolA' not implemented}}\ |
| 152 | // expected-warning {{method 'innsmouth' in protocol 'ProtocolA' not implemented}} |
| Ted Kremenek | 33e430f | 2013-12-13 06:26:14 +0000 | [diff] [blame] | 153 | - (void)dunwich {} |
| 154 | @end |
| 155 | |
| Ted Kremenek | 294c082 | 2014-02-27 01:28:58 +0000 | [diff] [blame^] | 156 | // Categories adopting a protocol with explicit conformance need to implement that protocol. |
| 157 | @interface Parent |
| 158 | - (void) theBestOfTimes; |
| 159 | @property (readonly) id theWorstOfTimes; |
| 160 | @end |
| 161 | |
| 162 | @interface Derived : Parent |
| 163 | @end |
| 164 | |
| 165 | @interface Derived (MyCat) <Protocol> |
| 166 | @end |
| 167 | |
| 168 | @implementation Derived (MyCat) // expected-warning {{method 'theBestOfTimes' in protocol 'Protocol' not implemented}} |
| 169 | @end |
| 170 | |
| Ted Kremenek | 27cfe10 | 2014-02-21 22:49:04 +0000 | [diff] [blame] | 171 | __attribute__((objc_protocol_requires_explicit_implementation)) // expected-error{{attribute 'objc_protocol_requires_explicit_implementation' can only be applied to @protocol definitions, not forward declarations}} |
| 172 | @protocol NotDefined; |
| 173 | |
| Ted Kremenek | 294c082 | 2014-02-27 01:28:58 +0000 | [diff] [blame^] | 174 | |