| 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 |
| 7 | - (void) theBestOfTimes; // expected-note {{method 'theBestOfTimes' declared here}} |
| Ted Kremenek | f41cf7f1 | 2013-12-10 19:43:48 +0000 | [diff] [blame] | 8 | @property (readonly) id theWorstOfTimes; |
| 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 | 2ccf19e | 2013-12-13 05:58:51 +0000 | [diff] [blame] | 24 | @implementation ClassB // expected-warning {{method 'theBestOfTimes' in protocol 'Protocol' not implemented}} |
| Ted Kremenek | 28eace6 | 2013-11-23 01:01:34 +0000 | [diff] [blame] | 25 | @end |
| 26 | |
| Ted Kremenek | c152c52 | 2013-12-12 06:20:42 +0000 | [diff] [blame] | 27 | // 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 Kremenek | 2ccf19e | 2013-12-13 05:58:51 +0000 | [diff] [blame] | 40 | @interface ClassD : ClassC <Derived> |
| Ted Kremenek | c152c52 | 2013-12-12 06:20:42 +0000 | [diff] [blame] | 41 | @end |
| 42 | |
| Ted Kremenek | 2ccf19e | 2013-12-13 05:58:51 +0000 | [diff] [blame] | 43 | @implementation ClassD // expected-warning {{method 'foulIsFair' in protocol 'Derived' not implemented}} |
| Ted Kremenek | c152c52 | 2013-12-12 06:20:42 +0000 | [diff] [blame] | 44 | @end |
| 45 | |
| Ted Kremenek | f41cf7f1 | 2013-12-10 19:43:48 +0000 | [diff] [blame] | 46 | // 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 Kremenek | 28eace6 | 2013-11-23 01:01:34 +0000 | [diff] [blame] | 49 | |
| Ted Kremenek | f41cf7f1 | 2013-12-10 19:43:48 +0000 | [diff] [blame] | 50 | // 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 Kremenek | 28eace6 | 2013-11-23 01:01:34 +0000 | [diff] [blame] | 53 | |
| Ted Kremenek | f41cf7f1 | 2013-12-10 19:43:48 +0000 | [diff] [blame] | 54 | __attribute__((objc_protocol_requires_explicit_implementation)) // expected-error {{attribute only applies to Objective-C protocols}} |
| 55 | int x; |
| Ted Kremenek | 28eace6 | 2013-11-23 01:01:34 +0000 | [diff] [blame] | 56 | |
| Ted Kremenek | 33e430f | 2013-12-13 06:26:14 +0000 | [diff] [blame] | 57 | // Test that inherited protocols with the attribute |
| 58 | // are treated properly. |
| 59 | __attribute__((objc_protocol_requires_explicit_implementation)) |
| 60 | @protocol ProtocolA |
| 61 | @required |
| Ted Kremenek | 15478b3 | 2014-01-17 08:34:19 +0000 | [diff] [blame^] | 62 | - (void)rlyeh; // expected-note 2 {{method 'rlyeh' declared here}} |
| 63 | - (void)innsmouth; // expected-note 2 {{method 'innsmouth' declared here}} |
| Ted Kremenek | 33e430f | 2013-12-13 06:26:14 +0000 | [diff] [blame] | 64 | @end |
| 65 | |
| 66 | @protocol ProtocolB <ProtocolA> |
| 67 | @required |
| 68 | - (void)dunwich; |
| Ted Kremenek | 15478b3 | 2014-01-17 08:34:19 +0000 | [diff] [blame^] | 69 | - (void)innsmouth; // expected-note {{method 'innsmouth' declared here}} |
| 70 | @end |
| 71 | |
| 72 | __attribute__((objc_protocol_requires_explicit_implementation)) |
| 73 | @protocol ProtocolB_Explicit <ProtocolA> |
| 74 | @required |
| 75 | - (void)dunwich; |
| 76 | - (void)innsmouth; // expected-note 2 {{method 'innsmouth' declared here}} |
| Ted Kremenek | 33e430f | 2013-12-13 06:26:14 +0000 | [diff] [blame] | 77 | @end |
| 78 | |
| 79 | @protocol ProtocolC |
| 80 | @required |
| 81 | - (void)rlyeh; |
| 82 | - (void)innsmouth; |
| 83 | - (void)dunwich; |
| 84 | @end |
| 85 | |
| Ted Kremenek | 15478b3 | 2014-01-17 08:34:19 +0000 | [diff] [blame^] | 86 | @interface MyObject <ProtocolC> @end |
| Ted Kremenek | 33e430f | 2013-12-13 06:26:14 +0000 | [diff] [blame] | 87 | |
| Ted Kremenek | 15478b3 | 2014-01-17 08:34:19 +0000 | [diff] [blame^] | 88 | // Provide two variants of a base class, one that adopts ProtocolA and |
| 89 | // one that does not. |
| 90 | @interface Lovecraft <ProtocolA> @end |
| 91 | @interface Lovecraft_2 @end |
| Ted Kremenek | 33e430f | 2013-12-13 06:26:14 +0000 | [diff] [blame] | 92 | |
| Ted Kremenek | 15478b3 | 2014-01-17 08:34:19 +0000 | [diff] [blame^] | 93 | // Provide two variants of a subclass that conform to ProtocolB. One |
| 94 | // subclasses from a class that conforms to ProtocolA, the other that |
| 95 | // does not. |
| 96 | // |
| 97 | // From those, provide two variants that conformat to ProtocolB_Explicit |
| 98 | // instead. |
| 99 | @interface Shoggoth : Lovecraft <ProtocolB> @end |
| 100 | @interface Shoggoth_2 : Lovecraft_2 <ProtocolB> @end |
| 101 | @interface Shoggoth_Explicit : Lovecraft <ProtocolB_Explicit> @end |
| 102 | @interface Shoggoth_2_Explicit : Lovecraft_2 <ProtocolB_Explicit> @end |
| 103 | |
| Ted Kremenek | 33e430f | 2013-12-13 06:26:14 +0000 | [diff] [blame] | 104 | |
| 105 | @implementation MyObject |
| 106 | - (void)innsmouth {} |
| 107 | - (void)rlyeh {} |
| 108 | - (void)dunwich {} |
| 109 | @end |
| 110 | |
| Ted Kremenek | 15478b3 | 2014-01-17 08:34:19 +0000 | [diff] [blame^] | 111 | @implementation Lovecraft |
| Ted Kremenek | 33e430f | 2013-12-13 06:26:14 +0000 | [diff] [blame] | 112 | - (void)innsmouth {} |
| 113 | - (void)rlyeh {} |
| 114 | @end |
| 115 | |
| Ted Kremenek | 15478b3 | 2014-01-17 08:34:19 +0000 | [diff] [blame^] | 116 | @implementation Shoggoth |
| 117 | - (void)dunwich {} |
| 118 | @end |
| 119 | |
| 120 | @implementation Shoggoth_2 // expected-warning {{method 'innsmouth' in protocol 'ProtocolB' not implemented}}\ |
| 121 | // expected-warning {{method 'rlyeh' in protocol 'ProtocolA' not implemented}}\ |
| 122 | // expected-warning {{'innsmouth' in protocol 'ProtocolA' not implemented}} |
| 123 | - (void)dunwich {} |
| 124 | @end |
| 125 | |
| 126 | @implementation Shoggoth_Explicit // expected-warning {{method 'innsmouth' in protocol 'ProtocolB_Explicit' not implemented}} |
| 127 | - (void)dunwich {} |
| 128 | @end |
| 129 | |
| 130 | @implementation Shoggoth_2_Explicit // expected-warning {{method 'innsmouth' in protocol 'ProtocolB_Explicit' not implemented}}\ |
| 131 | // expected-warning {{method 'rlyeh' in protocol 'ProtocolA' not implemented}}\ |
| 132 | // expected-warning {{method 'innsmouth' in protocol 'ProtocolA' not implemented}} |
| Ted Kremenek | 33e430f | 2013-12-13 06:26:14 +0000 | [diff] [blame] | 133 | - (void)dunwich {} |
| 134 | @end |
| 135 | |