Debian Codegraph @

gcc/testsuite/objc.dg/gnu-api-2-resolve-method.m

raw
1 /* Test the Modern GNU Objective-C Runtime API.
2
3 This is test 'resolve-method', covering +resolveClassMethod: and
4 +resolveInstanceMethod:. */
5
6 /* { dg-do run } */
7 /* { dg-skip-if "" { *-*-* } { "-fnext-runtime" } { "" } } */
8 /* { dg-additional-options "-Wno-objc-root-class" } */
9
10 /* To get the modern GNU Objective-C Runtime API, you include
11 objc/runtime.h. */
12 #include <objc/runtime.h>
13
14 /* For __objc_msg_forward2. */
15 #include <objc/message.h>
16
17 #include <stdlib.h>
18 #include <stdio.h>
19 #include <string.h>
20
21 @interface MyRootClass
22 { Class isa; }
23 + alloc;
24 - init;
25 @end
26
27 @implementation MyRootClass
28 + alloc { return class_createInstance (self, 0); }
29 - init { return self; }
30 @end
31
32
33 /* A number of tests will try invoking methods that don't exist. We
34 want to record the fact, but not abort the program, so we supply
35 our own fowarding implementation which will invoke the following
36 function for any method that is not found. */
37
38 /* Keep track of how many times a non-existing method was executed. */
39 static int nonExistingMethodCount = 0;
40
41 /* Inspired by nil_method in libobjc. */
42 id nonExisting_method (id receiver __attribute__ ((__unused__)),
43 SEL sel __attribute__ ((__unused__)))
44 {
45 nonExistingMethodCount++;
46 return nil;
47 }
48
49 /* Keep track of how many times the forwarding lookup was invoked. */
50 static int forwardingCount = 0;
51
52 /* We install this forwarding hook to cause all failed method lookups
53 to call our 'nonExisting_method' function. */
54 IMP forward_everything_to_non_existing_method (id receiver __attribute__ ((__unused__)),
55 SEL sel __attribute__ ((__unused__)))
56 {
57 forwardingCount++;
58 return (IMP)nonExisting_method;
59 }
60
61
62 /* 'CountClass' is used to test that +resolveClassMethod: and
63 +resolveInstanceMethod: are called when expected. They do nothing
64 other than recording that they are called. */
65 @interface CountClass : MyRootClass
66 + (BOOL) resolveClassMethod: (SEL)selector;
67 + (BOOL) resolveInstanceMethod: (SEL)selector;
68 + (void) existingClassMethod;
69 - (void) existingInstanceMethod;
70 @end
71
72 /* Count how many times the methods are called for class
73 'CountClass'. */
74 static int resolveClassMethodCount = 0;
75 static int resolveInstanceMethodCount = 0;
76
77 @implementation CountClass : MyRootClass
78 + (BOOL) resolveClassMethod: (SEL)selector
79 {
80 resolveClassMethodCount++;
81 return NO;
82 }
83 + (BOOL) resolveInstanceMethod: (SEL)selector
84 {
85 resolveInstanceMethodCount++;
86 return NO;
87 }
88 + (void) existingClassMethod
89 {
90 return;
91 }
92 - (void) existingInstanceMethod
93 {
94 return;
95 }
96 @end
97
98 @protocol NonExistingStuff
99 + (void) nonExistingClassMethod;
100 - (void) nonExistingInstanceMethod;
101 @end
102
103 /* Declare a category with some non existing methods, but don't
104 actually implement them. */
105 @interface CountClass (NonExistingStuff) <NonExistingStuff>
106 @end
107
108
109 /* 'SelfExtendingClass' is used to test that +resolveClassMethod: and
110 +resolveInstanceMethod: can extend the class. Any time they are
111 called, they install the requested method, mapping it to the same
112 implementation as 'countHits'. */
113 @interface SelfExtendingClass : MyRootClass
114 + (int) countHits;
115 + (BOOL) resolveClassMethod: (SEL)selector;
116 + (BOOL) resolveInstanceMethod: (SEL)selector;
117 @end
118
119 /* How many times the countHits method (or a clone) was called. */
120 static int hitCount = 0;
121
122 @implementation SelfExtendingClass : MyRootClass
123 + (int) countHits
124 {
125 hitCount++;
126 return hitCount;
127 }
128 + (BOOL) resolveClassMethod: (SEL)selector
129 {
130 /* Duplicate the 'countHits' method into the new method. */
131 Method method = class_getClassMethod (self, @selector (countHits));
132 class_addMethod (object_getClass (self), selector,
133 method_getImplementation (method),
134 method_getTypeEncoding (method));
135 resolveClassMethodCount++;
136 return YES;
137 }
138 + (BOOL) resolveInstanceMethod: (SEL)selector
139 {
140 /* Duplicate the 'countHits' method into the new method. */
141 Method method = class_getClassMethod (self, @selector (countHits));
142 class_addMethod (self, selector,
143 method_getImplementation (method),
144 method_getTypeEncoding (method));
145 resolveInstanceMethodCount++;
146 return YES;
147
148 }
149 @end
150
151 @protocol NonExistingStuff2
152 + (int) nonExistingCountHitsMethod;
153 - (int) nonExistingCountHitsMethod;
154
155 + (int) nonExistingCountHitsMethod2;
156 - (int) nonExistingCountHitsMethod2;
157
158 + (int) nonExistingCountHitsMethod3;
159 - (int) nonExistingCountHitsMethod3;
160 @end
161
162 /* Declare a category with some non existing methods, but don't
163 actually implement them. */
164 @interface SelfExtendingClass (NonExistingStuff) <NonExistingStuff2>
165 @end
166
167
168 int main (int argc, void **args)
169 {
170 /* Functions are tested in alphabetical order. */
171
172 /* Install our test forwarding hook. */
173 __objc_msg_forward2 = forward_everything_to_non_existing_method;
174
175 printf ("Testing [+resolveClassMethod:]...\n");
176 {
177 Method m;
178 IMP i;
179
180 /** CountClass tests. **/
181
182 /* Call an existing method. No +resolveClassMethod and no
183 forwarding should be triggered. */
184 [CountClass existingClassMethod];
185
186 if (resolveClassMethodCount != 0)
187 abort ();
188
189 if (forwardingCount != 0)
190 abort ();
191
192 if (nonExistingMethodCount != 0)
193 abort ();
194
195 /* Call a non-existing method. Both +resolveClassMethod and the
196 forwarding should be triggered. */
197 [CountClass nonExistingClassMethod];
198
199 if (resolveClassMethodCount != 1)
200 abort ();
201
202 if (forwardingCount != 1)
203 abort ();
204
205 if (nonExistingMethodCount != 1)
206 abort ();
207
208 /* Now try the same tests with class_getClassMethod(), which
209 should trigger the resolve methods too, but not the
210 forwarding. */
211 m = class_getClassMethod (objc_getClass ("CountClass"),
212 @selector (existingClassMethod));
213 if (resolveClassMethodCount != 1)
214 abort ();
215
216 if (forwardingCount != 1)
217 abort ();
218
219 if (nonExistingMethodCount != 1)
220 abort ();
221
222 m = class_getClassMethod (objc_getClass ("CountClass"),
223 @selector (nonExistingClassMethod));
224 if (resolveClassMethodCount != 2)
225 abort ();
226
227 if (forwardingCount != 1)
228 abort ();
229
230 if (nonExistingMethodCount != 1)
231 abort ();
232
233 /* Now try the same tests with class_getMethodImplementation(),
234 which should trigger the resolve methods and the forwarding
235 (but not execute the forwarding, obviously). */
236 i = class_getMethodImplementation (object_getClass (objc_getClass ("CountClass")),
237 @selector (existingClassMethod));
238 if (resolveClassMethodCount != 2)
239 abort ();
240
241 if (forwardingCount != 1)
242 abort ();
243
244 if (nonExistingMethodCount != 1)
245 abort ();
246
247 i = class_getMethodImplementation (object_getClass (objc_getClass ("CountClass")),
248 @selector (nonExistingClassMethod));
249 if (resolveClassMethodCount != 3)
250 abort ();
251
252 if (forwardingCount != 2)
253 abort ();
254
255 if (nonExistingMethodCount != 1)
256 abort ();
257
258
259 /* Reset the counters for the next test. */
260 resolveClassMethodCount = 0;
261 forwardingCount = 0;
262 nonExistingMethodCount = 0;
263
264
265 /** SelfExtendingClass tests. **/
266
267 /* Try the direct countHits method first. No resolving or
268 forwarding should be triggered. */
269 if ([SelfExtendingClass countHits] != 1)
270 abort ();
271
272 if (resolveClassMethodCount != 0)
273 abort ();
274
275 if (forwardingCount != 0)
276 abort ();
277
278 if (nonExistingMethodCount != 0)
279 abort ();
280
281 /* Now, try calling a non-existing count method; it should be
282 installed and invoked. */
283 if ([SelfExtendingClass nonExistingCountHitsMethod] != 2)
284 abort ();
285
286 if (resolveClassMethodCount != 1)
287 abort ();
288
289 if (forwardingCount != 0)
290 abort ();
291
292 if (nonExistingMethodCount != 0)
293 abort ();
294
295 /* Try it again. The method has now been installed, so it should
296 be used and work, but with no additional resolving
297 involved. */
298 if ([SelfExtendingClass nonExistingCountHitsMethod] != 3)
299 abort ();
300
301 if (resolveClassMethodCount != 1)
302 abort ();
303
304 if (forwardingCount != 0)
305 abort ();
306
307 if (nonExistingMethodCount != 0)
308 abort ();
309
310
311 /* Now try the same tests with class_getClassMethod(). */
312 m = class_getClassMethod (objc_getClass ("SelfExtendingClass"),
313 @selector (nonExistingCountHitsMethod2));
314 if (resolveClassMethodCount != 2)
315 abort ();
316
317 if (forwardingCount != 0)
318 abort ();
319
320 if (nonExistingMethodCount != 0)
321 abort ();
322
323 /* Try it again. The method has now been installed, so it should
324 be used and work, but with no additional resolving
325 involved. */
326 if ([SelfExtendingClass nonExistingCountHitsMethod2] != 4)
327 abort ();
328
329 if (resolveClassMethodCount != 2)
330 abort ();
331
332 if (forwardingCount != 0)
333 abort ();
334
335 if (nonExistingMethodCount != 0)
336 abort ();
337
338
339 /* Now try the same tests with class_getMethodImplementation(). */
340 i = class_getMethodImplementation (object_getClass (objc_getClass ("SelfExtendingClass")),
341 @selector (nonExistingCountHitsMethod3));
342 if (resolveClassMethodCount != 3)
343 abort ();
344
345 if (forwardingCount != 0)
346 abort ();
347
348 if (nonExistingMethodCount != 0)
349 abort ();
350
351 /* Try it again. The method has now been installed, so it should
352 be used and work, but with no additional resolving
353 involved. */
354 if ([SelfExtendingClass nonExistingCountHitsMethod3] != 5)
355 abort ();
356
357 if (resolveClassMethodCount != 3)
358 abort ();
359
360 if (forwardingCount != 0)
361 abort ();
362
363 if (nonExistingMethodCount != 0)
364 abort ();
365 }
366
367 /* Reset the counters for the next test. */
368 nonExistingMethodCount = 0;
369 forwardingCount = 0;
370 hitCount = 0;
371
372 printf ("Testing [+resolveInstanceMethod:]...\n");
373 {
374 Method m;
375 IMP i;
376 CountClass *object = [[CountClass alloc] init];
377 SelfExtendingClass *object2 = [[SelfExtendingClass alloc] init];
378
379 /** CountClass tests. **/
380
381 /* Call an existing method. No +resolveInstanceMethod and no
382 forwarding should be triggered. */
383 [object existingInstanceMethod];
384
385 if (resolveInstanceMethodCount != 0)
386 abort ();
387
388 if (forwardingCount != 0)
389 abort ();
390
391 if (nonExistingMethodCount != 0)
392 abort ();
393
394 /* Call a non-existing method. Both +resolveInstanceMethod and the
395 forwarding should be triggered. */
396 [object nonExistingInstanceMethod];
397
398 if (resolveInstanceMethodCount != 1)
399 abort ();
400
401 if (forwardingCount != 1)
402 abort ();
403
404 if (nonExistingMethodCount != 1)
405 abort ();
406
407 /* Now try the same tests with class_getInstanceMethod(), which
408 should trigger the resolve methods too, but not the
409 forwarding. */
410 m = class_getInstanceMethod (objc_getClass ("CountClass"),
411 @selector (existingInstanceMethod));
412
413 if (resolveInstanceMethodCount != 1)
414 abort ();
415
416 if (forwardingCount != 1)
417 abort ();
418
419 if (nonExistingMethodCount != 1)
420 abort ();
421
422 m = class_getInstanceMethod (objc_getClass ("CountClass"),
423 @selector (nonExistingInstanceMethod));
424
425 if (resolveInstanceMethodCount != 2)
426 abort ();
427
428 if (forwardingCount != 1)
429 abort ();
430
431 if (nonExistingMethodCount != 1)
432 abort ();
433
434 /* Now try the same tests with class_getMethodImplementation(),
435 which should trigger the resolve methods and the
436 forwarding. */
437 i = class_getMethodImplementation (objc_getClass ("CountClass"),
438 @selector (existingInstanceMethod));
439 if (resolveInstanceMethodCount != 2)
440 abort ();
441
442 if (forwardingCount != 1)
443 abort ();
444
445 if (nonExistingMethodCount != 1)
446 abort ();
447
448 i = class_getMethodImplementation (objc_getClass ("CountClass"),
449 @selector (nonExistingInstanceMethod));
450 if (resolveInstanceMethodCount != 3)
451 abort ();
452
453 if (forwardingCount != 2)
454 abort ();
455
456 if (nonExistingMethodCount != 1)
457 abort ();
458
459 /* Reset the counters for the next test. */
460 resolveInstanceMethodCount = 0;
461 forwardingCount = 0;
462 nonExistingMethodCount = 0;
463
464
465 /** SelfExtendingClass tests. **/
466
467 /* Try the direct countHits method first. No resolving or
468 forwarding should be triggered. */
469 if ([SelfExtendingClass countHits] != 1)
470 abort ();
471
472 if (resolveInstanceMethodCount != 0)
473 abort ();
474
475 if (forwardingCount != 0)
476 abort ();
477
478 if (nonExistingMethodCount != 0)
479 abort ();
480
481 /* Now, try calling a non-existing count method; it should be
482 installed and invoked. */
483 if ([object2 nonExistingCountHitsMethod] != 2)
484 abort ();
485
486 if (resolveInstanceMethodCount != 1)
487 abort ();
488
489 if (forwardingCount != 0)
490 abort ();
491
492 if (nonExistingMethodCount != 0)
493 abort ();
494
495 /* Try it again. The method has now been installed, so it should
496 be used and work, but with no additional resolving
497 involved. */
498 if ([object2 nonExistingCountHitsMethod] != 3)
499 abort ();
500
501 if (resolveInstanceMethodCount != 1)
502 abort ();
503
504 if (forwardingCount != 0)
505 abort ();
506
507 if (nonExistingMethodCount != 0)
508 abort ();
509
510 /* Now try the same tests with class_getInstanceMethod(). */
511 m = class_getInstanceMethod (objc_getClass ("SelfExtendingClass"),
512 @selector (nonExistingCountHitsMethod2));
513 if (resolveInstanceMethodCount != 2)
514 abort ();
515
516 if (forwardingCount != 0)
517 abort ();
518
519 if (nonExistingMethodCount != 0)
520 abort ();
521
522 /* Try it again. The method has now been installed, so it should
523 be used and work, but with no additional resolving
524 involved. */
525 if ([object2 nonExistingCountHitsMethod2] != 4)
526 abort ();
527
528 if (resolveInstanceMethodCount != 2)
529 abort ();
530
531 if (forwardingCount != 0)
532 abort ();
533
534 if (nonExistingMethodCount != 0)
535 abort ();
536
537
538 /* Now try the same tests with class_getMethodImplementation(). */
539 i = class_getMethodImplementation (objc_getClass ("SelfExtendingClass"),
540 @selector (nonExistingCountHitsMethod3));
541 if (resolveInstanceMethodCount != 3)
542 abort ();
543
544 if (forwardingCount != 0)
545 abort ();
546
547 if (nonExistingMethodCount != 0)
548 abort ();
549
550 /* Try it again. The method has now been installed, so it should
551 be used and work, but with no additional resolving
552 involved. */
553 if ([object2 nonExistingCountHitsMethod3] != 5)
554 abort ();
555
556 if (resolveInstanceMethodCount != 3)
557 abort ();
558
559 if (forwardingCount != 0)
560 abort ();
561
562 if (nonExistingMethodCount != 0)
563 abort ();
564 }
565
566
567 return 0;
568 }