OSDN Git Service

* gcc.dg/cleanup-10.c, gcc.dg/cleanup-11.c, gcc.dg/cleanup-8.c,
[pf3gnuchains/gcc-fork.git] / gcc / testsuite / gcc.dg / cleanup-8.c
1 /* { dg-do run { target *-*-linux* powerpc*-*-darwin* } } */
2 /* { dg-options "-fexceptions -fnon-call-exceptions -O2" } */
3 /* Verify that cleanups work with exception handling through signal
4    frames.  */
5
6 #include <unwind.h>
7 #include <stdlib.h>
8 #include <signal.h>
9
10 static _Unwind_Reason_Code
11 force_unwind_stop (int version, _Unwind_Action actions,
12                    _Unwind_Exception_Class exc_class,
13                    struct _Unwind_Exception *exc_obj,
14                    struct _Unwind_Context *context,
15                    void *stop_parameter)
16 {
17   if (actions & _UA_END_OF_STACK)
18     abort ();
19   return _URC_NO_REASON;
20 }
21
22 static void force_unwind ()
23 {
24   struct _Unwind_Exception *exc = malloc (sizeof (*exc));
25   exc->exception_class = 0;
26   exc->exception_cleanup = 0;
27                    
28 #ifndef __USING_SJLJ_EXCEPTIONS__
29   _Unwind_ForcedUnwind (exc, force_unwind_stop, 0);
30 #else
31   _Unwind_SjLj_ForcedUnwind (exc, force_unwind_stop, 0);
32 #endif
33                    
34   abort ();
35 }
36
37 int count;
38 char *null;
39
40 static void counter (void *p __attribute__((unused)))
41 {
42   ++count;
43 }
44
45 static void handler (void *p __attribute__((unused)))
46 {
47   if (count != 2)
48     abort ();
49   exit (0);
50 }
51
52 static int __attribute__((noinline)) fn5 ()
53 {
54   char dummy __attribute__((cleanup (counter)));
55   force_unwind ();
56   return 0;
57 }
58
59 static void fn4 (int sig)
60 {
61   char dummy __attribute__((cleanup (counter)));
62   fn5 ();
63   null = NULL;
64 }
65
66 static void fn3 ()
67 {
68   abort ();
69 }
70
71 static int __attribute__((noinline)) fn2 ()
72 {
73   *null = 0;
74   fn3 ();
75   return 0;
76 }
77
78 static int __attribute__((noinline)) fn1 ()
79 {
80   signal (SIGSEGV, fn4);
81   signal (SIGBUS, fn4);
82   fn2 ();
83   return 0;
84 }
85
86 static int __attribute__((noinline)) fn0 ()
87 {
88   char dummy __attribute__((cleanup (handler)));
89   fn1 ();
90   null = 0;
91   return 0;
92 }
93
94 int main()
95
96   fn0 ();
97   abort ();
98 }