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