OSDN Git Service

PR testsuite/23611, PR testsuite/23615
[pf3gnuchains/gcc-fork.git] / gcc / testsuite / obj-c++.dg / try-catch-2.mm
1 /* Test out '@catch(id foo) {...}', which should catch
2    all uncaught 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 #include <stdlib.h>
11
12 /* The following is not required in actual user code; we include it
13    here to check that the compiler generates an internal definition of
14    _setjmp that is consistent with what <setjmp.h> provides.  */
15 #include <setjmp.h>
16
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   test((Object *)-1);
77   return 0;
78 }