1// RUN: %clang_cc1 -verify -fsyntax-only -fobjc-arc -fblocks %s
2
3#define SWIFT_NAME(name) __attribute__((__swift_name__(name)))
4#define SWIFT_ASYNC_NAME(name) __attribute__((__swift_async_name__(name)))
5
6typedef struct {
7  float x, y, z;
8} Point3D;
9
10__attribute__((__swift_name__("PType")))
11@protocol P
12@end
13
14__attribute__((__swift_name__("IClass")))
15@interface I<P>
16- (instancetype)init SWIFT_NAME("init()");
17- (instancetype)initWithValue:(int)value SWIFT_NAME("iWithValue(_:)");
18
19+ (void)refresh SWIFT_NAME("refresh()");
20
21- (instancetype)i SWIFT_NAME("i()");
22
23- (I *)iWithValue:(int)value SWIFT_NAME("i(value:)");
24- (I *)iWithValue:(int)value value:(int)value2 SWIFT_NAME("i(value:extra:)");
25- (I *)iWithValueConvertingValue:(int)value value:(int)value2 SWIFT_NAME("i(_:extra:)");
26
27+ (I *)iWithOtheValue:(int)value SWIFT_NAME("init");
28// expected-warning@-1 {{'__swift_name__' attribute argument must be a string literal specifying a Swift function name}}
29
30+ (I *)iWithAnotherValue:(int)value SWIFT_NAME("i()");
31// expected-warning@-1 {{too few parameters in the signature specified by the '__swift_name__' attribute (expected 1; got 0)}}
32
33+ (I *)iWithYetAnotherValue:(int)value SWIFT_NAME("i(value:extra:)");
34// expected-warning@-1 {{too many parameters in the signature specified by the '__swift_name__' attribute (expected 1; got 2}}
35
36+ (I *)iAndReturnErrorCode:(int *)errorCode SWIFT_NAME("i()"); // no-warning
37+ (I *)iWithValue:(int)value andReturnErrorCode:(int *)errorCode SWIFT_NAME("i(value:)"); // no-warning
38
39+ (I *)iFromErrorCode:(const int *)errorCode SWIFT_NAME("i()");
40// expected-warning@-1 {{too few parameters in the signature specified by the '__swift_name__' attribute (expected 1; got 0)}}
41
42+ (I *)iWithPointerA:(int *)value andReturnErrorCode:(int *)errorCode SWIFT_NAME("i()"); // no-warning
43+ (I *)iWithPointerB:(int *)value andReturnErrorCode:(int *)errorCode SWIFT_NAME("i(pointer:)"); // no-warning
44+ (I *)iWithPointerC:(int *)value andReturnErrorCode:(int *)errorCode SWIFT_NAME("i(pointer:errorCode:)"); // no-warning
45
46+ (I *)iWithOtherI:(I *)other SWIFT_NAME("i()");
47// expected-warning@-1 {{too few parameters in the signature specified by the '__swift_name__' attribute (expected 1; got 0)}}
48
49+ (instancetype)specialI SWIFT_NAME("init(options:)");
50+ (instancetype)specialJ SWIFT_NAME("init(options:extra:)");
51// expected-warning@-1 {{too many parameters in the signature specified by the '__swift_name__' attribute (expected 0; got 2)}}
52+ (instancetype)specialK SWIFT_NAME("init(_:)");
53// expected-warning@-1 {{too many parameters in the signature specified by the '__swift_name__' attribute (expected 0; got 1)}}
54+ (instancetype)specialL SWIFT_NAME("i(options:)");
55// expected-warning@-1 {{too many parameters in the signature specified by the '__swift_name__' attribute (expected 0; got 1)}}
56
57+ (instancetype)trailingParen SWIFT_NAME("foo(");
58// expected-warning@-1 {{'__swift_name__' attribute argument must be a string literal specifying a Swift function name}}
59+ (instancetype)trailingColon SWIFT_NAME("foo:");
60// expected-warning@-1 {{'__swift_name__' attribute argument must be a string literal specifying a Swift function name}}
61+ (instancetype)initialIgnore:(int)value SWIFT_NAME("_(value:)");
62// expected-warning@-1 {{'__swift_name__' attribute has invalid identifier for the base name}}
63+ (instancetype)middleOmitted:(int)value SWIFT_NAME("i(:)");
64// expected-warning@-1 {{'__swift_name__' attribute has invalid identifier for the parameter name}}
65
66@property(strong) id someProp SWIFT_NAME("prop");
67@end
68
69enum SWIFT_NAME("E") E {
70  value1,
71  value2,
72  value3 SWIFT_NAME("three"),
73  value4 SWIFT_NAME("four()"), // expected-warning {{'__swift_name__' attribute has invalid identifier for the base name}}
74};
75
76struct SWIFT_NAME("TStruct") SStruct {
77  int i, j, k SWIFT_NAME("kay");
78};
79
80int i SWIFT_NAME("g_i");
81
82void f0(int i) SWIFT_NAME("f_0");
83// expected-warning@-1 {{'__swift_name__' attribute argument must be a string literal specifying a Swift function name}}
84
85void f1(int i) SWIFT_NAME("f_1()");
86// expected-warning@-1 {{too few parameters in the signature specified by the '__swift_name__' attribute (expected 1; got 0)}}
87
88void f2(int i) SWIFT_NAME("f_2(a:b:)");
89// expected-warning@-1 {{too many parameters in the signature specified by the '__swift_name__' attribute (expected 1; got 2)}}
90
91void f3(int x, int y) SWIFT_NAME("fWithX(_:y:)");
92void f4(int x, int *error) SWIFT_NAME("fWithX(_:)");
93
94typedef int int_t SWIFT_NAME("IntType");
95
96struct Point3D createPoint3D(float x, float y, float z) SWIFT_NAME("Point3D.init(x:y:z:)");
97struct Point3D rotatePoint3D(Point3D point, float radians) SWIFT_NAME("Point3D.rotate(self:radians:)");
98struct Point3D badRotatePoint3D(Point3D point, float radians) SWIFT_NAME("Point3D.rotate(radians:)");
99// expected-warning@-1 {{too few parameters in the signature specified by the '__swift_name__' attribute (expected 2; got 1)}}
100
101extern struct Point3D identityPoint SWIFT_NAME("Point3D.identity");
102
103float Point3DGetMagnitude(Point3D point) SWIFT_NAME("getter:Point3D.magnitude(self:)");
104float Point3DGetMagnitudeAndSomethingElse(Point3D point, float f) SWIFT_NAME("getter:Point3D.magnitude(self:f:)");
105// expected-warning@-1 {{'__swift_name__' attribute for getter must not have any parameters besides 'self:'}}
106
107float Point3DGetRadius(Point3D point) SWIFT_NAME("getter:Point3D.radius(self:)");
108void Point3DSetRadius(Point3D point, float radius) SWIFT_NAME("setter:Point3D.radius(self:newValue:)");
109
110float Point3DPreGetRadius(Point3D point) SWIFT_NAME("getter:Point3D.preRadius(self:)");
111void Point3DPreSetRadius(float radius, Point3D point) SWIFT_NAME("setter:Point3D.preRadius(newValue:self:)");
112
113void Point3DSetRadiusAndSomethingElse(Point3D point, float radius, float f) SWIFT_NAME("setter:Point3D.radius(self:newValue:f:)");
114// expected-warning@-1 {{'__swift_name__' attribute for setter must have one parameter for new value}}
115
116float Point3DGetComponent(Point3D point, unsigned index) SWIFT_NAME("getter:Point3D.subscript(self:_:)");
117float Point3DSetComponent(Point3D point, unsigned index, float value) SWIFT_NAME("setter:Point3D.subscript(self:_:newValue:)");
118
119float Point3DGetMatrixComponent(Point3D point, unsigned x, unsigned y) SWIFT_NAME("getter:Point3D.subscript(self:x:y:)");
120void Point3DSetMatrixComponent(Point3D point, unsigned x, float value, unsigned y) SWIFT_NAME("setter:Point3D.subscript(self:x:newValue:y:)");
121
122float Point3DSetWithoutNewValue(Point3D point, unsigned x, unsigned y) SWIFT_NAME("setter:Point3D.subscript(self:x:y:)");
123// expected-warning@-1 {{'__swift_name__' attribute for 'subscript' setter must have a 'newValue:' parameter}}
124
125float Point3DSubscriptButNotGetterSetter(Point3D point, unsigned x) SWIFT_NAME("Point3D.subscript(self:_:)");
126// expected-warning@-1 {{'__swift_name__' attribute for 'subscript' must be a getter or setter}}
127
128void Point3DSubscriptSetterTwoNewValues(Point3D point, unsigned x, float a, float b) SWIFT_NAME("setter:Point3D.subscript(self:_:newValue:newValue:)");
129// expected-warning@-1 {{'__swift_name__' attribute for 'subscript' setter cannot have multiple 'newValue:' parameters}}
130
131float Point3DSubscriptGetterNewValue(Point3D point, unsigned x, float a, float b) SWIFT_NAME("getter:Point3D.subscript(self:_:newValue:newValue:)");
132// expected-warning@-1 {{'__swift_name__' attribute for 'subscript' getter cannot have a 'newValue:' parameter}}
133
134void Point3DMethodWithNewValue(Point3D point, float newValue) SWIFT_NAME("Point3D.method(self:newValue:)");
135void Point3DMethodWithNewValues(Point3D point, float newValue, float newValueB) SWIFT_NAME("Point3D.method(self:newValue:newValue:)");
136
137float Point3DStaticSubscript(unsigned x) SWIFT_NAME("getter:Point3D.subscript(_:)");
138// expected-warning@-1 {{'__swift_name__' attribute for 'subscript' must have a 'self:' parameter}}
139
140float Point3DStaticSubscriptNoArgs(void) SWIFT_NAME("getter:Point3D.subscript()");
141// expected-warning@-1 {{'__swift_name__' attribute for 'subscript' must have at least one parameter}}
142
143float Point3DPreGetComponent(Point3D point, unsigned index) SWIFT_NAME("getter:Point3D.subscript(self:_:)");
144
145Point3D getCurrentPoint3D(void) SWIFT_NAME("getter:currentPoint3D()");
146
147void setCurrentPoint3D(Point3D point) SWIFT_NAME("setter:currentPoint3D(newValue:)");
148
149Point3D getLastPoint3D(void) SWIFT_NAME("getter:lastPoint3D()");
150
151void setLastPoint3D(Point3D point) SWIFT_NAME("setter:lastPoint3D(newValue:)");
152
153Point3D getZeroPoint(void) SWIFT_NAME("getter:Point3D.zero()");
154void setZeroPoint(Point3D point) SWIFT_NAME("setter:Point3D.zero(newValue:)");
155Point3D getZeroPointNoPrototype() SWIFT_NAME("getter:Point3D.zeroNoPrototype()");
156// expected-warning@-1 {{'__swift_name__' attribute only applies to non-K&R-style functions}}
157
158Point3D badGetter1(int x) SWIFT_NAME("getter:bad1(_:)");
159// expected-warning@-1 {{'__swift_name__' attribute for getter must not have any parameters besides 'self:'}}
160
161void badSetter1(void) SWIFT_NAME("getter:bad1())");
162// expected-warning@-1 {{'__swift_name__' attribute argument must be a string literal specifying a Swift function name}}
163
164Point3D badGetter2(Point3D point) SWIFT_NAME("getter:bad2(_:))");
165// expected-warning@-1 {{'__swift_name__' attribute argument must be a string literal specifying a Swift function name}}
166
167void badSetter2(Point3D point) SWIFT_NAME("setter:bad2(self:))");
168// expected-warning@-1 {{'__swift_name__' attribute argument must be a string literal specifying a Swift function name}}
169
170void g(int i) SWIFT_NAME("function(int:)");
171// expected-note@-1 {{conflicting attribute is here}}
172
173// expected-error@+1 {{'swift_name' and 'swift_name' attributes are not compatible}}
174void g(int i) SWIFT_NAME("function(_:)") {
175}
176
177typedef int (^CallbackTy)(void);
178
179@interface AsyncI<P>
180
181- (void)doSomethingWithCallback:(CallbackTy)callback SWIFT_ASYNC_NAME("doSomething()");
182- (void)doSomethingX:(int)x withCallback:(CallbackTy)callback SWIFT_ASYNC_NAME("doSomething(x:)");
183
184// expected-warning@+1 {{too many parameters in the signature specified by the '__swift_async_name__' attribute (expected 1; got 2)}}
185- (void)doSomethingY:(int)x withCallback:(CallbackTy)callback SWIFT_ASYNC_NAME("doSomething(x:y:)");
186
187// expected-warning@+1 {{too few parameters in the signature specified by the '__swift_async_name__' attribute (expected 1; got 0)}}
188- (void)doSomethingZ:(int)x withCallback:(CallbackTy)callback SWIFT_ASYNC_NAME("doSomething()");
189
190// expected-warning@+1 {{'__swift_async_name__' attribute cannot be applied to a method with no parameters}}
191- (void)doSomethingNone SWIFT_ASYNC_NAME("doSomething()");
192
193// expected-error@+1 {{'__swift_async_name__' attribute takes one argument}}
194- (void)brokenAttr __attribute__((__swift_async_name__("brokenAttr", 2)));
195
196@end
197
198void asyncFunc(CallbackTy callback) SWIFT_ASYNC_NAME("asyncFunc()");
199
200// expected-warning@+1 {{'__swift_async_name__' attribute cannot be applied to a function with no parameters}}
201void asyncNoParams(void) SWIFT_ASYNC_NAME("asyncNoParams()");
202
203// expected-error@+1 {{'__swift_async_name__' attribute only applies to Objective-C methods and functions}}
204SWIFT_ASYNC_NAME("NoAsync")
205@protocol NoAsync @end
206