summaryrefslogtreecommitdiff
path: root/test/SemaObjCXX/arc-0x.mm
blob: 052c99ac13c8a18b5083e56cf2d0dd76d11a0c0e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
// RUN: %clang_cc1 -std=c++11 -fsyntax-only -fobjc-arc -fobjc-runtime-has-weak -fobjc-weak -verify -fblocks -fobjc-exceptions %s

// "Move" semantics, trivial version.
void move_it(__strong id &&from) {
  id to = static_cast<__strong id&&>(from);
}

// Deduction with 'auto'.
@interface A
+ alloc;
- init;
@end

// <rdar://problem/12031870>: don't warn about this
extern "C" A* MakeA();

// Ensure that deduction works with lifetime qualifiers.
void deduction(id obj) {
  auto a = [[A alloc] init];
  __strong A** aPtr = &a;

  auto a2([[A alloc] init]);
  __strong A** aPtr2 = &a2;

  __strong id *idp = new auto(obj);

  __strong id array[17];
  for (auto x : array) { // expected-warning{{'auto' deduced as 'id' in declaration of 'x'}}
    __strong id *xPtr = &x;
  }

  @try {
  } @catch (auto e) { // expected-error {{'auto' not allowed in exception declaration}}
  }
}

// rdar://problem/11068137
void test1a() {
  __autoreleasing id p; // expected-note 2 {{'p' declared here}}
  (void) [&p] {};
  (void) [p] {}; // expected-error {{cannot capture __autoreleasing variable in a lambda by copy}}
  (void) [=] { (void) p; }; // expected-error {{cannot capture __autoreleasing variable in a lambda by copy}}
}
void test1b() {
  __autoreleasing id v;
  __autoreleasing id &p = v; // expected-note 2 {{'p' declared here}}
  (void) [&p] {};
  (void) [p] {}; // expected-error {{cannot capture __autoreleasing variable in a lambda by copy}}
  (void) [=] { (void) p; }; // expected-error {{cannot capture __autoreleasing variable in a lambda by copy}}
}
void test1c() {
  __autoreleasing id v; // expected-note {{'v' declared here}}
  __autoreleasing id &p = v;
  (void) ^{ (void) p; };
  (void) ^{ (void) v; }; // expected-error {{cannot capture __autoreleasing variable in a block}}
}


// <rdar://problem/11319689>
// warn when initializing an 'auto' variable with an 'id' initializer expression

void testAutoId(id obj) {
  auto x = obj; // expected-warning{{'auto' deduced as 'id' in declaration of 'x'}}
}

@interface Array
+ (instancetype)new;
- (id)objectAtIndex:(int)index;
@end

// ...but don't warn if it's coming from a template parameter.
template<typename T, int N>
void autoTemplateFunction(T param, id obj, Array *arr) {
  auto x = param; // no-warning
  auto y = obj; // expected-warning{{'auto' deduced as 'id' in declaration of 'y'}}
  auto z = [arr objectAtIndex:N]; // expected-warning{{'auto' deduced as 'id' in declaration of 'z'}}
}

void testAutoIdTemplate(id obj) {
  autoTemplateFunction<id, 2>(obj, obj, [Array new]); // no-warning
}

// rdar://12229679
@interface NSObject @end
typedef __builtin_va_list va_list;
@interface MyClass : NSObject
@end

@implementation MyClass
+ (void)fooMethod:(id)firstArg, ... {
    va_list args;

    __builtin_va_arg(args, id);
}
@end

namespace rdar12078752 {
  void f() {
    NSObject* o =0;
    __autoreleasing decltype(o) o2 = o;
    __autoreleasing auto o3 = o;
  }
}

namespace test_err_arc_array_param_no_ownership {
  template <class T>
  void func(T a) {}

  void test() {
    func([](A *a[]){}); // expected-error{{must explicitly describe intended ownership of an object array parameter}}
    func(^(A *a[]){}); // expected-error{{must explicitly describe intended ownership of an object array parameter}}
  }
}

namespace test_union {
  // Implicitly-declared special functions of a union are deleted by default if
  // ARC is enabled and the union has an ObjC pointer field.
  union U0 {
    id f0; // expected-note 6 {{'U0' is implicitly deleted because variant field 'f0' is an ObjC pointer}}
  };

  union U1 {
    __weak id f0; // expected-note 12 {{'U1' is implicitly deleted because variant field 'f0' is an ObjC pointer}}
    U1() = default; // expected-warning {{explicitly defaulted default constructor is implicitly deleted}} expected-note {{explicitly defaulted function was implicitly deleted here}}
    ~U1() = default; // expected-warning {{explicitly defaulted destructor is implicitly deleted}} expected-note {{explicitly defaulted function was implicitly deleted here}}
    U1(const U1 &) = default; // expected-warning {{explicitly defaulted copy constructor is implicitly deleted}} expected-note 2 {{explicitly defaulted function was implicitly deleted here}}
    U1(U1 &&) = default; // expected-warning {{explicitly defaulted move constructor is implicitly deleted}}
    U1 & operator=(const U1 &) = default; // expected-warning {{explicitly defaulted copy assignment operator is implicitly deleted}} expected-note 2 {{explicitly defaulted function was implicitly deleted here}}
    U1 & operator=(U1 &&) = default; // expected-warning {{explicitly defaulted move assignment operator is implicitly deleted}}
  };

  id getStrong();

  // If the ObjC pointer field of a union has a default member initializer, the
  // implicitly-declared default constructor of the union is not deleted by
  // default.
  union U2 {
    id f0 = getStrong(); // expected-note 4 {{'U2' is implicitly deleted because variant field 'f0' is an ObjC pointer}}
    ~U2();
  };

  // It's fine if the user has explicitly defined the special functions.
  union U3 {
    id f0;
    U3();
    ~U3();
    U3(const U3 &);
    U3(U3 &&);
    U3 & operator=(const U3 &);
    U3 & operator=(U3 &&);
  };

  // ObjC pointer fields in anonymous union fields delete the defaulted special
  // functions of the containing class.
  struct S0 {
    union {
      id f0; // expected-note 6 {{'' is implicitly deleted because variant field 'f0' is an ObjC pointer}}
      char f1;
    };
  };

  struct S1 {
    union {
      union { // expected-note 2 {{'S1' is implicitly deleted because variant field '' has a non-trivial}} expected-note 4 {{'S1' is implicitly deleted because field '' has a deleted}}
        id f0; // expected-note 2 {{'' is implicitly deleted because variant field 'f0' is an ObjC pointer}}
        char f1;
      };
      int f2;
    };
  };

  struct S2 {
    union {
      // FIXME: the note should say 'f0' is causing the special functions to be deleted.
      struct { // expected-note 6 {{'S2' is implicitly deleted because variant field '' has a non-trivial}}
        id f0;
        int f1;
      };
      int f2;
    };
    int f3;
  };

  U0 *x0;
  U1 *x1;
  U2 *x2;
  U3 *x3;
  S0 *x4;
  S1 *x5;
  S2 *x6;

  static union { // expected-error {{call to implicitly-deleted default constructor of}}
    id g0; // expected-note {{default constructor of '' is implicitly deleted because variant field 'g0' is an ObjC pointer}}
  };

  static union { // expected-error {{call to implicitly-deleted default constructor of}}
    union { // expected-note {{default constructor of '' is implicitly deleted because field '' has a deleted default constructor}}
      union { // expected-note {{default constructor of '' is implicitly deleted because field '' has a deleted default constructor}}
        __weak id g1; // expected-note {{default constructor of '' is implicitly deleted because variant field 'g1' is an ObjC pointer}}
        int g2;
      };
      int g3;
    };
    int g4;
  };

  void testDefaultConstructor() {
    U0 t0; // expected-error {{call to implicitly-deleted default constructor}}
    U1 t1; // expected-error {{call to implicitly-deleted default constructor}}
    U2 t2;
    U3 t3;
    S0 t4; // expected-error {{call to implicitly-deleted default constructor}}
    S1 t5; // expected-error {{call to implicitly-deleted default constructor}}
    S2 t6; // expected-error {{call to implicitly-deleted default constructor}}
  }

  void testDestructor(U0 *u0, U1 *u1, U2 *u2, U3 *u3, S0 *s0, S1 *s1, S2 *s2) {
    delete u0; // expected-error {{attempt to use a deleted function}}
    delete u1; // expected-error {{attempt to use a deleted function}}
    delete u2;
    delete u3;
    delete s0; // expected-error {{attempt to use a deleted function}}
    delete s1; // expected-error {{attempt to use a deleted function}}
    delete s2; // expected-error {{attempt to use a deleted function}}
  }

  void testCopyConstructor(U0 *u0, U1 *u1, U2 *u2, U3 *u3, S0 *s0, S1 *s1, S2 *s2) {
    U0 t0(*u0); // expected-error {{call to implicitly-deleted copy constructor}}
    U1 t1(*u1); // expected-error {{call to implicitly-deleted copy constructor}}
    U2 t2(*u2); // expected-error {{call to implicitly-deleted copy constructor}}
    U3 t3(*u3);
    S0 t4(*s0); // expected-error {{call to implicitly-deleted copy constructor}}
    S1 t5(*s1); // expected-error {{call to implicitly-deleted copy constructor}}
    S2 t6(*s2); // expected-error {{call to implicitly-deleted copy constructor}}
  }

  void testCopyAssignment(U0 *u0, U1 *u1, U2 *u2, U3 *u3, S0 *s0, S1 *s1, S2 *s2) {
    *x0 = *u0; // expected-error {{cannot be assigned because its copy assignment operator is implicitly deleted}}
    *x1 = *u1; // expected-error {{cannot be assigned because its copy assignment operator is implicitly deleted}}
    *x2 = *u2; // expected-error {{cannot be assigned because its copy assignment operator is implicitly deleted}}
    *x3 = *u3;
    *x4 = *s0; // expected-error {{cannot be assigned because its copy assignment operator is implicitly deleted}}
    *x5 = *s1; // expected-error {{cannot be assigned because its copy assignment operator is implicitly deleted}}
    *x6 = *s2; // expected-error {{cannot be assigned because its copy assignment operator is implicitly deleted}}
  }

  // The diagnostics below refer to the deleted copy constructors and assignment
  // operators since defaulted move constructors and assignment operators that are
  // defined as deleted are ignored by overload resolution.

  void testMoveConstructor(U0 *u0, U1 *u1, U2 *u2, U3 *u3, S0 *s0, S1 *s1, S2 *s2) {
    U0 t0(static_cast<U0 &&>(*u0)); // expected-error {{call to implicitly-deleted copy constructor}}
    U1 t1(static_cast<U1 &&>(*u1)); // expected-error {{call to implicitly-deleted copy constructor}}
    U2 t2(static_cast<U2 &&>(*u2)); // expected-error {{call to implicitly-deleted copy constructor}}
    U3 t3(static_cast<U3 &&>(*u3));
    S0 t4(static_cast<S0 &&>(*s0)); // expected-error {{call to implicitly-deleted copy constructor}}
    S1 t5(static_cast<S1 &&>(*s1)); // expected-error {{call to implicitly-deleted copy constructor}}
    S2 t6(static_cast<S2 &&>(*s2)); // expected-error {{call to implicitly-deleted copy constructor}}
  }

  void testMoveAssignment(U0 *u0, U1 *u1, U2 *u2, U3 *u3, S0 *s0, S1 *s1, S2 *s2) {
    *x0 = static_cast<U0 &&>(*u0); // expected-error {{cannot be assigned because its copy assignment operator is implicitly deleted}}
    *x1 = static_cast<U1 &&>(*u1); // expected-error {{cannot be assigned because its copy assignment operator is implicitly deleted}}
    *x2 = static_cast<U2 &&>(*u2); // expected-error {{cannot be assigned because its copy assignment operator is implicitly deleted}}
    *x3 = static_cast<U3 &&>(*u3);
    *x4 = static_cast<S0 &&>(*s0); // expected-error {{cannot be assigned because its copy assignment operator is implicitly deleted}}
    *x5 = static_cast<S1 &&>(*s1); // expected-error {{cannot be assigned because its copy assignment operator is implicitly deleted}}
    *x6 = static_cast<S2 &&>(*s2); // expected-error {{cannot be assigned because its copy assignment operator is implicitly deleted}}
  }
}