| 1 | #include "exception.h"
|
|---|
| 2 |
|
|---|
| 3 | // Use: gcc -fexceptions -Wall -Werror -g exception.c test-main.c
|
|---|
| 4 |
|
|---|
| 5 | #include <stdio.h>
|
|---|
| 6 | #include <stdbool.h>
|
|---|
| 7 |
|
|---|
| 8 | // Translation Helpers:
|
|---|
| 9 | #define CLEANUP(function) \
|
|---|
| 10 | struct __cleanup_hook __hidden_hook __attribute__((cleanup(function)))
|
|---|
| 11 |
|
|---|
| 12 | void __try_resume_node_new(struct __try_resume_node * node,
|
|---|
| 13 | _Bool (*handler)(exception except)) {
|
|---|
| 14 | node->next = shared_stack.top_resume;
|
|---|
| 15 | shared_stack.top_resume = node;
|
|---|
| 16 | node->try_to_handle = handler;
|
|---|
| 17 | }
|
|---|
| 18 |
|
|---|
| 19 | void __try_resume_node_delete(struct __try_resume_node * node) {
|
|---|
| 20 | shared_stack.top_resume = node->next;
|
|---|
| 21 | }
|
|---|
| 22 |
|
|---|
| 23 | // Local Print On Exit:
|
|---|
| 24 | struct raii_base_type {
|
|---|
| 25 | const char * area;
|
|---|
| 26 | };
|
|---|
| 27 |
|
|---|
| 28 | void raii_dtor(struct raii_base_type * this) {
|
|---|
| 29 | printf("Exiting: %s\n", this->area);
|
|---|
| 30 | }
|
|---|
| 31 |
|
|---|
| 32 | #define raii_t __attribute__((cleanup(raii_dtor))) struct raii_base_type
|
|---|
| 33 |
|
|---|
| 34 | // ===========================================================================
|
|---|
| 35 | // Runtime code (post-translation).
|
|---|
| 36 | void terminate(int except_value) {
|
|---|
| 37 | raii_t a = {"terminate function"};
|
|---|
| 38 | __throw_terminate(except_value);
|
|---|
| 39 | printf("terminate returned\n");
|
|---|
| 40 | }
|
|---|
| 41 |
|
|---|
| 42 | void resume(int except_value) {
|
|---|
| 43 | raii_t a = {"resume function"};
|
|---|
| 44 | __throw_resume(except_value);
|
|---|
| 45 | printf("resume returned\n");
|
|---|
| 46 | }
|
|---|
| 47 |
|
|---|
| 48 | // Termination Test: Two handlers: no catch, catch
|
|---|
| 49 | void bar() {
|
|---|
| 50 | raii_t a = {"bar function"};
|
|---|
| 51 | {
|
|---|
| 52 | void bar_try1() {
|
|---|
| 53 | terminate(4);
|
|---|
| 54 | }
|
|---|
| 55 | void bar_catch1(int index, exception except) {
|
|---|
| 56 | switch(except) {
|
|---|
| 57 | case 1:
|
|---|
| 58 | printf("bar caught exception 3.\n");
|
|---|
| 59 | break;
|
|---|
| 60 | default:
|
|---|
| 61 | printf("INVALID INDEX in bar: %d (%d)\n", index, except);
|
|---|
| 62 | }
|
|---|
| 63 | }
|
|---|
| 64 | int bar_match1(exception except) {
|
|---|
| 65 | if (3 == except) {
|
|---|
| 66 | return 1;
|
|---|
| 67 | } else {
|
|---|
| 68 | return 0;
|
|---|
| 69 | }
|
|---|
| 70 | }
|
|---|
| 71 | __try_terminate(bar_try1, bar_catch1, bar_match1);
|
|---|
| 72 | }
|
|---|
| 73 | }
|
|---|
| 74 |
|
|---|
| 75 | void foo() {
|
|---|
| 76 | raii_t a = {"foo function"};
|
|---|
| 77 | {
|
|---|
| 78 | void foo_try1() {
|
|---|
| 79 | bar();
|
|---|
| 80 | }
|
|---|
| 81 | void foo_catch1(int index, exception except) {
|
|---|
| 82 | switch(index) {
|
|---|
| 83 | case 1:
|
|---|
| 84 | printf("foo caught exception 4.\n");
|
|---|
| 85 | break;
|
|---|
| 86 | case 2:
|
|---|
| 87 | printf("foo caught exception 2.\n");
|
|---|
| 88 | break;
|
|---|
| 89 | default:
|
|---|
| 90 | printf("INVALID INDEX in foo: %d (%d)\n", index, except);
|
|---|
| 91 | }
|
|---|
| 92 | }
|
|---|
| 93 | int foo_match1(exception except) {
|
|---|
| 94 | if (4 == except) {
|
|---|
| 95 | return 1;
|
|---|
| 96 | } else if (2 == except) {
|
|---|
| 97 | return 2;
|
|---|
| 98 | } else {
|
|---|
| 99 | return 0;
|
|---|
| 100 | }
|
|---|
| 101 | }
|
|---|
| 102 | __try_terminate(foo_try1, foo_catch1, foo_match1);
|
|---|
| 103 | }
|
|---|
| 104 | }
|
|---|
| 105 |
|
|---|
| 106 | // Resumption Two Handler Test: no catch, catch.
|
|---|
| 107 | void beta() {
|
|---|
| 108 | raii_t a = {"beta function"};
|
|---|
| 109 | {
|
|---|
| 110 | bool beta_handle1(exception except) {
|
|---|
| 111 | if (3 == except) {
|
|---|
| 112 | printf("beta caught exception 3\n");
|
|---|
| 113 | return true;
|
|---|
| 114 | } else {
|
|---|
| 115 | return false;
|
|---|
| 116 | }
|
|---|
| 117 | }
|
|---|
| 118 | struct __try_resume_node node
|
|---|
| 119 | __attribute__((cleanup(__try_resume_node_delete)));
|
|---|
| 120 | __try_resume_node_new(&node, beta_handle1);
|
|---|
| 121 | {
|
|---|
| 122 | resume(4);
|
|---|
| 123 | }
|
|---|
| 124 | }
|
|---|
| 125 | }
|
|---|
| 126 | void alpha() {
|
|---|
| 127 | raii_t a = {"alpha function"};
|
|---|
| 128 | {
|
|---|
| 129 | bool alpha_handle1(exception except) {
|
|---|
| 130 | if (2 == except) {
|
|---|
| 131 | printf("alpha caught exception 2\n");
|
|---|
| 132 | return true;
|
|---|
| 133 | } else if (4 == except) {
|
|---|
| 134 | printf("alpha caught exception 4\n");
|
|---|
| 135 | return true;
|
|---|
| 136 | } else {
|
|---|
| 137 | return false;
|
|---|
| 138 | }
|
|---|
| 139 | }
|
|---|
| 140 | struct __try_resume_node node
|
|---|
| 141 | __attribute__((cleanup(__try_resume_node_delete)));
|
|---|
| 142 | __try_resume_node_new(&node, alpha_handle1);
|
|---|
| 143 | {
|
|---|
| 144 | beta();
|
|---|
| 145 | }
|
|---|
| 146 | }
|
|---|
| 147 | }
|
|---|
| 148 |
|
|---|
| 149 | // Finally Test:
|
|---|
| 150 | void farewell() {
|
|---|
| 151 | {
|
|---|
| 152 | void farewell_finally1() {
|
|---|
| 153 | printf("See you next time\n");
|
|---|
| 154 | }
|
|---|
| 155 | CLEANUP(farewell_finally1);
|
|---|
| 156 | {
|
|---|
| 157 | printf("jump out of farewell\n");
|
|---|
| 158 | }
|
|---|
| 159 | }
|
|---|
| 160 | }
|
|---|
| 161 |
|
|---|
| 162 | // Resume-to-Terminate Test:
|
|---|
| 163 | void fallback() {
|
|---|
| 164 | //raii_t a = {"fallback function\n"};
|
|---|
| 165 | {
|
|---|
| 166 | void fallback_try1() {
|
|---|
| 167 | resume(1);
|
|---|
| 168 | }
|
|---|
| 169 | void fallback_catch1(int index, exception except) {
|
|---|
| 170 | switch (index) {
|
|---|
| 171 | case 1:
|
|---|
| 172 | printf("fallback caught termination 1\n");
|
|---|
| 173 | break;
|
|---|
| 174 | default:
|
|---|
| 175 | printf("INVALID INDEX in fallback: %d (%d)\n", index, except);
|
|---|
| 176 | }
|
|---|
| 177 | }
|
|---|
| 178 | int fallback_match1(exception except) {
|
|---|
| 179 | if (1 == except) {
|
|---|
| 180 | return 1;
|
|---|
| 181 | } else {
|
|---|
| 182 | return 0;
|
|---|
| 183 | }
|
|---|
| 184 | }
|
|---|
| 185 | __try_terminate(fallback_try1, fallback_catch1, fallback_match1);
|
|---|
| 186 | }
|
|---|
| 187 | }
|
|---|
| 188 |
|
|---|
| 189 | // main: choose which tests to run
|
|---|
| 190 | int main(int argc, char * argv[]) {
|
|---|
| 191 | raii_t a = {"main function"};
|
|---|
| 192 |
|
|---|
| 193 | foo(); printf("\n");
|
|---|
| 194 | alpha(); printf("\n");
|
|---|
| 195 | farewell(); printf("\n");
|
|---|
| 196 | fallback(); printf("\n");
|
|---|
| 197 | // Uncaught termination test.
|
|---|
| 198 | terminate(7);
|
|---|
| 199 | }
|
|---|