__try.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. // RUN: %clang_cc1 -triple x86_64-windows -fborland-extensions -DBORLAND -fsyntax-only -verify -fblocks %s
  2. // RUN: %clang_cc1 -triple x86_64-windows -fms-extensions -fsyntax-only -verify -fblocks %s
  3. #define JOIN2(x,y) x ## y
  4. #define JOIN(x,y) JOIN2(x,y)
  5. #define TEST2(name) JOIN(name,__LINE__)
  6. #define TEST TEST2(test)
  7. typedef int DWORD;
  8. #pragma sysheader begin
  9. struct EXCEPTION_INFO{};
  10. unsigned long __exception_code();
  11. #ifdef BORLAND
  12. struct EXCEPTION_INFO* __exception_info();
  13. #endif
  14. int __abnormal_termination();
  15. #define GetExceptionCode __exception_code
  16. #define GetExceptionInformation __exception_info
  17. #define AbnormalTermination __abnormal_termination
  18. #pragma sysheader end
  19. DWORD FilterExpression(int); // expected-note{{declared here}}
  20. DWORD FilterExceptionInformation(struct EXCEPTION_INFO*);
  21. const char * NotFilterExpression();
  22. void TEST() {
  23. __try {
  24. __try {
  25. __try {
  26. }
  27. __finally{
  28. }
  29. }
  30. __finally{
  31. }
  32. }
  33. __finally{
  34. }
  35. }
  36. void TEST() {
  37. __try {
  38. }
  39. } // expected-error{{expected '__except' or '__finally' block}}
  40. void TEST() {
  41. __except ( FilterExpression() ) { // expected-warning{{implicit declaration of function '__except' is invalid in C99}} \
  42. // expected-error{{too few arguments to function call, expected 1, have 0}}
  43. }
  44. }
  45. void TEST() {
  46. __finally { } // expected-error{{}}
  47. }
  48. void TEST() {
  49. __try{
  50. int try_scope = 0;
  51. } // TODO: expected expression is an extra error
  52. __except( try_scope ? 1 : -1 ) // expected-error{{undeclared identifier 'try_scope'}} expected-error{{expected expression}}
  53. {}
  54. }
  55. void TEST() {
  56. __try {
  57. }
  58. // TODO: Why are there two errors?
  59. __except( ) { // expected-error{{expected expression}} expected-error{{expected expression}}
  60. }
  61. }
  62. void TEST() {
  63. __try {
  64. }
  65. __except ( FilterExpression(GetExceptionCode()) ) {
  66. }
  67. __try {
  68. }
  69. __except( FilterExpression(__exception_code()) ) {
  70. }
  71. __try {
  72. }
  73. __except( FilterExceptionInformation(__exception_info()) ) {
  74. }
  75. __try {
  76. }
  77. __except(FilterExceptionInformation( GetExceptionInformation() ) ) {
  78. }
  79. }
  80. void TEST() {
  81. __try {
  82. }
  83. __except ( NotFilterExpression() ) { // expected-error{{filter expression type should be an integral value not 'const char *'}}
  84. }
  85. }
  86. void TEST() {
  87. int function_scope = 0;
  88. __try {
  89. int try_scope = 0;
  90. }
  91. __except ( FilterExpression(GetExceptionCode()) ) {
  92. (void)function_scope;
  93. (void)try_scope; // expected-error{{undeclared identifier}}
  94. }
  95. }
  96. void TEST() {
  97. int function_scope = 0;
  98. __try {
  99. int try_scope = 0;
  100. }
  101. __finally {
  102. (void)function_scope;
  103. (void)try_scope; // expected-error{{undeclared identifier}}
  104. }
  105. }
  106. void TEST() {
  107. int function_scope = 0;
  108. __try {
  109. }
  110. __except( function_scope ? 1 : -1 ) {}
  111. }
  112. #ifdef BORLAND
  113. void TEST() {
  114. (void)__abnormal_termination(); // expected-error{{only allowed in __finally block}}
  115. (void)AbnormalTermination(); // expected-error{{only allowed in __finally block}}
  116. __try {
  117. (void)AbnormalTermination; // expected-error{{only allowed in __finally block}}
  118. (void)__abnormal_termination; // expected-error{{only allowed in __finally block}}
  119. }
  120. __except( 1 ) {
  121. (void)AbnormalTermination; // expected-error{{only allowed in __finally block}}
  122. (void)__abnormal_termination; // expected-error{{only allowed in __finally block}}
  123. }
  124. __try {
  125. }
  126. __finally {
  127. AbnormalTermination();
  128. __abnormal_termination();
  129. }
  130. }
  131. #endif
  132. void TEST() {
  133. (void)__exception_info(); // expected-error{{only allowed in __except filter expression}}
  134. (void)GetExceptionInformation(); // expected-error{{only allowed in __except filter expression}}
  135. }
  136. void TEST() {
  137. #ifndef BORLAND
  138. (void)__exception_code; // expected-error{{builtin functions must be directly called}}
  139. #endif
  140. (void)__exception_code(); // expected-error{{only allowed in __except block or filter expression}}
  141. (void)GetExceptionCode(); // expected-error{{only allowed in __except block or filter expression}}
  142. }
  143. void TEST() {
  144. __try {
  145. } __except(1) {
  146. GetExceptionCode(); // valid
  147. GetExceptionInformation(); // expected-error{{only allowed in __except filter expression}}
  148. }
  149. }
  150. void test_seh_leave_stmt() {
  151. __leave; // expected-error{{'__leave' statement not in __try block}}
  152. __try {
  153. __leave;
  154. __leave 4; // expected-error{{expected ';' after __leave statement}}
  155. } __except(1) {
  156. __leave; // expected-error{{'__leave' statement not in __try block}}
  157. }
  158. __try {
  159. __leave;
  160. } __finally {
  161. __leave; // expected-error{{'__leave' statement not in __try block}}
  162. }
  163. __leave; // expected-error{{'__leave' statement not in __try block}}
  164. }
  165. void test_jump_out_of___finally() {
  166. while(1) {
  167. __try {
  168. } __finally {
  169. continue; // expected-warning{{jump out of __finally block has undefined behavior}}
  170. }
  171. }
  172. __try {
  173. } __finally {
  174. while (1) {
  175. continue;
  176. }
  177. }
  178. // Check that a deep __finally containing a block with a shallow continue
  179. // doesn't trigger the warning.
  180. while(1) {{{{
  181. __try {
  182. } __finally {
  183. ^{
  184. while(1)
  185. continue;
  186. }();
  187. }
  188. }}}}
  189. while(1) {
  190. __try {
  191. } __finally {
  192. break; // expected-warning{{jump out of __finally block has undefined behavior}}
  193. }
  194. }
  195. switch(1) {
  196. case 1:
  197. __try {
  198. } __finally {
  199. break; // expected-warning{{jump out of __finally block has undefined behavior}}
  200. }
  201. }
  202. __try {
  203. } __finally {
  204. while (1) {
  205. break;
  206. }
  207. }
  208. __try {
  209. __try {
  210. } __finally {
  211. __leave; // expected-warning{{jump out of __finally block has undefined behavior}}
  212. }
  213. } __finally {
  214. }
  215. __try {
  216. } __finally {
  217. __try {
  218. __leave;
  219. } __finally {
  220. }
  221. }
  222. __try {
  223. } __finally {
  224. return; // expected-warning{{jump out of __finally block has undefined behavior}}
  225. }
  226. __try {
  227. } __finally {
  228. ^{
  229. return;
  230. }();
  231. }
  232. }
  233. void test_typo_in_except() {
  234. __try {
  235. } __except(undeclared_identifier) { // expected-error {{use of undeclared identifier 'undeclared_identifier'}} expected-error {{expected expression}}
  236. }
  237. }