Changeset e4e9173 for doc/working/exception/impl/test-main.c
- Timestamp:
- Jun 13, 2017, 4:24:17 PM (7 years ago)
- Branches:
- ADT, aaron-thesis, arm-eh, ast-experimental, cleanup-dtors, deferred_resn, demangler, enum, forall-pointer-decay, jacob/cs343-translation, jenkins-sandbox, master, new-ast, new-ast-unique-expr, new-env, no_list, persistent-indexer, pthread-emulation, qualifiedEnum, resolv-new, with_gc
- Children:
- 6a48cc9
- Parents:
- 35dd0f42
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
doc/working/exception/impl/test-main.c
r35dd0f42 re4e9173 1 1 #include "exception.h" 2 2 3 // Use: gcc -fexceptions -Wall -Werror -g exception.c test-main.c 4 3 5 #include <stdio.h> 6 #include <stdbool.h> 4 7 5 8 // Translation Helpers: … … 7 10 struct __cleanup_hook __hidden_hook __attribute__((cleanup(function))) 8 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 } 9 22 10 23 // Local Print On Exit: … … 19 32 #define raii_t __attribute__((cleanup(raii_dtor))) struct raii_base_type 20 33 34 // =========================================================================== 21 35 // Runtime code (post-translation). 22 36 void terminate(int except_value) { … … 34 48 // Termination Test: Two handlers: no catch, catch 35 49 void bar() { 36 void bar_try1() { 37 terminate(4); 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); 38 72 } 39 void bar_catch1(int index, exception except) {40 switch(except) {41 case 1:42 printf("bar caught exception.\n");43 break;44 default:45 printf("INVALID INDEX in bar: %d\n", except);46 }47 }48 int bar_match1(exception except) {49 if (3 == except) {50 return 1;51 } else {52 return 0;53 }54 }55 __try_terminate(bar_try1, bar_catch1, bar_match1);56 73 } 57 74 58 75 void foo() { 59 void foo_try1() { 60 bar(); 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); 61 103 } 62 void foo_catch1(int index, exception except) { 63 switch(except) { 64 case 1: 65 printf("foo caught exception 4.\n"); 66 break; 67 case 2: 68 printf("foo caught exception 2.\n"); 69 break; 70 default: 71 printf("INVALID INDEX in foo: %d\n", except); 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); 72 123 } 73 124 } 74 int foo_match1(exception except) { 75 if (4 == except) { 76 return 1; 77 } else if (2 == except) { 78 return 2; 79 } else { 80 return 0; 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(); 81 145 } 82 146 } 83 __try_terminate(foo_try1, foo_catch1, foo_match1);84 147 } 85 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 86 190 int main(int argc, char * argv[]) { 87 191 raii_t a = {"main function"}; 88 192 89 foo(); 193 foo(); printf("\n"); 194 alpha(); printf("\n"); 195 farewell(); printf("\n"); 196 fallback(); printf("\n"); 197 // Uncaught termination test. 198 terminate(7); 90 199 }
Note: See TracChangeset
for help on using the changeset viewer.