OSDN Git Service

* obj-c++.dg/defs.mm (abort): Make it extern "C".
[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-do run } */
6 /* { dg-xfail-if "PR23616" { "*-*-*" } { "*" } { "" } } */
7 /* { dg-options "-fobjc-exceptions" } */
8
9 #include <objc/Object.h>
10 #include <stdio.h>
11 #include <stdlib.h>
12
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>
17
18 #define CHECK_IF(expr) if(!(expr)) abort()
19
20 @interface Frob: Object
21 @end
22
23 @implementation Frob: Object
24 @end
25
26 static Frob* _connection = nil;
27
28 //--------------------------------------------------------------------
29
30
31 void test (Object* sendPort)
32 {
33   int cleanupPorts = 1;
34   Frob* receivePort = nil;
35         
36   @try {
37     printf ("receivePort = %p\n", receivePort);
38     printf ("sendPort = %p\n", sendPort);
39     printf ("cleanupPorts = %d\n", cleanupPorts);
40     printf ("---\n");
41                 
42     receivePort = (Frob *) -1;
43     _connection = (Frob *) -1;
44     printf ("receivePort = %p\n", receivePort);
45     printf ("sendPort = %p\n", sendPort);
46     printf ("cleanupPorts = %d\n", cleanupPorts);
47     printf ("---\n");
48                 
49     receivePort = nil;
50     sendPort = nil;
51     cleanupPorts = 0;
52                 
53     printf ("receivePort = %p\n", receivePort);
54     printf ("sendPort = %p\n", sendPort);
55     printf ("cleanupPorts = %d\n", cleanupPorts);
56     printf ("---\n");           
57                 
58     @throw [Object new];
59   }
60   @catch(Frob *obj) {
61     printf ("Exception caught by incorrect handler!\n");
62     CHECK_IF(0);
63   }
64   @catch(id exc) {
65     printf ("Exception caught by correct handler.\n");
66     printf ("receivePort = %p (expected 0x0)\n", receivePort);
67     printf ("sendPort = %p (expected 0x0)\n", sendPort);
68     printf ("cleanupPorts = %d (expected 0)\n", cleanupPorts);
69     printf ("---");
70     CHECK_IF(!receivePort);
71     CHECK_IF(!sendPort);
72     CHECK_IF(!cleanupPorts);
73   }
74 }
75
76 int main (void) {
77   test((Object *)-1);
78   return 0;
79 }