1 // RUN: %clang_cc1 -std=c++03 -fblocks -triple x86_64-windows-msvc -fms-extensions -fsyntax-only -fexceptions -fcxx-exceptions -verify %s
2 // RUN: %clang_cc1 -std=c++11 -fblocks -triple x86_64-windows-msvc -fms-extensions -fsyntax-only -fexceptions -fcxx-exceptions -verify %s
3 
4 // Basic usage should work.
safe_div(int n,int d)5 int safe_div(int n, int d) {
6   int r;
7   __try {
8     r = n / d;
9   } __except(_exception_code() == 0xC0000094) {
10     r = 0;
11   }
12   return r;
13 }
14 
15 void might_crash();
16 
17 // Diagnose obvious builtin mis-usage.
bad_builtin_scope()18 void bad_builtin_scope() {
19   __try {
20     might_crash();
21   } __except(1) {
22   }
23   _exception_code(); // expected-error {{'_exception_code' only allowed in __except block or filter expression}}
24   _exception_info(); // expected-error {{'_exception_info' only allowed in __except filter expression}}
25 }
26 
27 // Diagnose obvious builtin misusage in a template.
28 template <void FN()>
bad_builtin_scope_template()29 void bad_builtin_scope_template() {
30   __try {
31     FN();
32   } __except(1) {
33   }
34   _exception_code(); // expected-error {{'_exception_code' only allowed in __except block or filter expression}}
35   _exception_info(); // expected-error {{'_exception_info' only allowed in __except filter expression}}
36 }
instantiate_bad_scope_tmpl()37 void instantiate_bad_scope_tmpl() {
38   bad_builtin_scope_template<might_crash>();
39 }
40 
41 #if __cplusplus < 201103L
42 template <typename T, T FN()>
43 T func_template() {
44   return FN(); // expected-error 2{{builtin functions must be directly called}}
45 }
inject_builtins()46 void inject_builtins() {
47   func_template<void *, __exception_info>(); // expected-note {{instantiation of}}
48   func_template<unsigned long, __exception_code>(); // expected-note {{instantiation of}}
49 }
50 #endif
51 
use_seh_after_cxx()52 void use_seh_after_cxx() {
53   try { // expected-note {{conflicting 'try' here}}
54     might_crash();
55   } catch (int) {
56   }
57   __try { // expected-error {{cannot use C++ 'try' in the same function as SEH '__try'}}
58     might_crash();
59   } __except(1) {
60   }
61 }
62 
use_cxx_after_seh()63 void use_cxx_after_seh() {
64   __try { // expected-note {{conflicting '__try' here}}
65     might_crash();
66   } __except(1) {
67   }
68   try { // expected-error {{cannot use C++ 'try' in the same function as SEH '__try'}}
69     might_crash();
70   } catch (int) {
71   }
72 }
73 
74 #if __cplusplus >= 201103L
use_seh_in_lambda()75 void use_seh_in_lambda() {
76   ([]() {
77     __try {
78       might_crash();
79     } __except(1) {
80     }
81   })();
82   try {
83     might_crash();
84   } catch (int) {
85   }
86 }
87 #endif
88 
use_seh_in_block()89 void use_seh_in_block() {
90   void (^b)() = ^{
91     __try { // expected-error {{cannot use SEH '__try' in blocks, captured regions, or Obj-C method decls}}
92       might_crash();
93     } __except(1) {
94     }
95   };
96   try {
97     b();
98   } catch (int) {
99   }
100 }
101 
102 void (^use_seh_in_global_block)() = ^{
103   __try { // expected-error {{cannot use SEH '__try' in blocks, captured regions, or Obj-C method decls}}
104     might_crash();
105   } __except(1) {
106   }
107 };
108 
109 void (^use_cxx_in_global_block)() = ^{
110   try {
111     might_crash();
112   } catch(int) {
113   }
114 };
115 
dependent_filter()116 template <class T> void dependent_filter() {
117   __try {
118     might_crash();
119   } __except (T()) { // expected-error {{filter expression has non-integral type 'NotInteger'}}
120   }
121 }
122 
123 struct NotInteger { int x; };
124 
instantiate_dependent_filter()125 void instantiate_dependent_filter() {
126   dependent_filter<int>();
127   dependent_filter<NotInteger>(); // expected-note {{requested here}}
128 }
129