Yet more Objective-C++...
[gcc.git] / gcc / testsuite / objc.dg / try-catch-2.m
1 /* Test out '@catch(id foo) {...}', which should catch all uncaught
2 exceptions. */
3 /* Developed by Ziemowit Laski <zlaski@apple.com>. */
4
5 /* { dg-options "-fobjc-exceptions" } */
6 /* { dg-do run } */
7
8 #include <objc/Object.h>
9 #include <stdio.h>
10
11 /* The following is not required in actual user code; we include it
12 here to check that the compiler generates an internal definition of
13 _setjmp that is consistent with what <setjmp.h> provides. */
14 #include <setjmp.h>
15
16 extern void abort(void);
17 #define CHECK_IF(expr) if(!(expr)) abort()
18
19 @interface Frob: Object
20 @end
21
22 @implementation Frob: Object
23 @end
24
25 static Frob* _connection = nil;
26
27 //--------------------------------------------------------------------
28
29
30 void test (Object* sendPort)
31 {
32 int cleanupPorts = 1;
33 Frob* receivePort = nil;
34
35 @try {
36 printf ("receivePort = %p\n", receivePort);
37 printf ("sendPort = %p\n", sendPort);
38 printf ("cleanupPorts = %d\n", cleanupPorts);
39 printf ("---\n");
40
41 receivePort = (Frob *) -1;
42 _connection = (Frob *) -1;
43 printf ("receivePort = %p\n", receivePort);
44 printf ("sendPort = %p\n", sendPort);
45 printf ("cleanupPorts = %d\n", cleanupPorts);
46 printf ("---\n");
47
48 receivePort = nil;
49 sendPort = nil;
50 cleanupPorts = 0;
51
52 printf ("receivePort = %p\n", receivePort);
53 printf ("sendPort = %p\n", sendPort);
54 printf ("cleanupPorts = %d\n", cleanupPorts);
55 printf ("---\n");
56
57 @throw [Object new];
58 }
59 @catch(Frob *obj) {
60 printf ("Exception caught by incorrect handler!\n");
61 CHECK_IF(0);
62 }
63 @catch(id exc) {
64 printf ("Exception caught by correct handler.\n");
65 printf ("receivePort = %p (expected 0x0)\n", receivePort);
66 printf ("sendPort = %p (expected 0x0)\n", sendPort);
67 printf ("cleanupPorts = %d (expected 0)\n", cleanupPorts);
68 printf ("---");
69 CHECK_IF(!receivePort);
70 CHECK_IF(!sendPort);
71 CHECK_IF(!cleanupPorts);
72 }
73 }
74
75 int main (void) {
76
77 test((Object *)-1);
78 return 0;
79 }