Debian Codegraph @

clang/test/SemaObjC/attr-swift_name.m

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