llvm-project/clang/test/SemaCXX/exceptions-seh.cpp

116 lines
2.7 KiB
C++
Raw Normal View History

// RUN: %clang_cc1 -std=c++03 -fblocks -triple x86_64-windows-msvc -fms-extensions -fsyntax-only -fexceptions -fcxx-exceptions -verify %s
// RUN: %clang_cc1 -std=c++11 -fblocks -triple x86_64-windows-msvc -fms-extensions -fsyntax-only -fexceptions -fcxx-exceptions -verify %s
Initial support for Win64 SEH IR emission The lowering looks a lot like normal EH lowering, with the exception that the exceptions are caught by executing filter expression code instead of matching typeinfo globals. The filter expressions are outlined into functions which are used in landingpad clauses where typeinfo would normally go. Major aspects that still need work: - Non-call exceptions in __try bodies won't work yet. The plan is to outline the __try block in the frontend to keep things simple. - Filter expressions cannot use local variables until capturing is implemented. - __finally blocks will not run after exceptions. Fixing this requires work in the LLVM SEH preparation pass. The IR lowering looks like this: // C code: bool safe_div(int n, int d, int *r) { __try { *r = normal_div(n, d); } __except(_exception_code() == EXCEPTION_INT_DIVIDE_BY_ZERO) { return false; } return true; } ; LLVM IR: define i32 @filter(i8* %e, i8* %fp) { %ehptrs = bitcast i8* %e to i32** %ehrec = load i32** %ehptrs %code = load i32* %ehrec %matches = icmp eq i32 %code, i32 u0xC0000094 %matches.i32 = zext i1 %matches to i32 ret i32 %matches.i32 } define i1 zeroext @safe_div(i32 %n, i32 %d, i32* %r) { %rr = invoke i32 @normal_div(i32 %n, i32 %d) to label %normal unwind to label %lpad normal: store i32 %rr, i32* %r ret i1 1 lpad: %ehvals = landingpad {i8*, i32} personality i32 (...)* @__C_specific_handler catch i8* bitcast (i32 (i8*, i8*)* @filter to i8*) %ehptr = extractvalue {i8*, i32} %ehvals, i32 0 %sel = extractvalue {i8*, i32} %ehvals, i32 1 %filter_sel = call i32 @llvm.eh.seh.typeid.for(i8* bitcast (i32 (i8*, i8*)* @filter to i8*)) %matches = icmp eq i32 %sel, %filter_sel br i1 %matches, label %eh.except, label %eh.resume eh.except: ret i1 false eh.resume: resume } Reviewers: rjmccall, rsmith, majnemer Differential Revision: http://reviews.llvm.org/D5607 llvm-svn: 226760
2015-01-22 09:36:17 +08:00
// Basic usage should work.
int safe_div(int n, int d) {
int r;
__try {
r = n / d;
} __except(_exception_code() == 0xC0000094) {
r = 0;
}
return r;
}
void might_crash();
// Diagnose obvious builtin mis-usage.
void bad_builtin_scope() {
__try {
might_crash();
} __except(1) {
}
_exception_code(); // expected-error {{'_exception_code' only allowed in __except block or filter expression}}
_exception_info(); // expected-error {{'_exception_info' only allowed in __except filter expression}}
}
// Diagnose obvious builtin misusage in a template.
template <void FN()>
void bad_builtin_scope_template() {
__try {
FN();
} __except(1) {
}
_exception_code(); // expected-error {{'_exception_code' only allowed in __except block or filter expression}}
_exception_info(); // expected-error {{'_exception_info' only allowed in __except filter expression}}
}
void instantiate_bad_scope_tmpl() {
bad_builtin_scope_template<might_crash>();
}
#if __cplusplus < 201103L
Initial support for Win64 SEH IR emission The lowering looks a lot like normal EH lowering, with the exception that the exceptions are caught by executing filter expression code instead of matching typeinfo globals. The filter expressions are outlined into functions which are used in landingpad clauses where typeinfo would normally go. Major aspects that still need work: - Non-call exceptions in __try bodies won't work yet. The plan is to outline the __try block in the frontend to keep things simple. - Filter expressions cannot use local variables until capturing is implemented. - __finally blocks will not run after exceptions. Fixing this requires work in the LLVM SEH preparation pass. The IR lowering looks like this: // C code: bool safe_div(int n, int d, int *r) { __try { *r = normal_div(n, d); } __except(_exception_code() == EXCEPTION_INT_DIVIDE_BY_ZERO) { return false; } return true; } ; LLVM IR: define i32 @filter(i8* %e, i8* %fp) { %ehptrs = bitcast i8* %e to i32** %ehrec = load i32** %ehptrs %code = load i32* %ehrec %matches = icmp eq i32 %code, i32 u0xC0000094 %matches.i32 = zext i1 %matches to i32 ret i32 %matches.i32 } define i1 zeroext @safe_div(i32 %n, i32 %d, i32* %r) { %rr = invoke i32 @normal_div(i32 %n, i32 %d) to label %normal unwind to label %lpad normal: store i32 %rr, i32* %r ret i1 1 lpad: %ehvals = landingpad {i8*, i32} personality i32 (...)* @__C_specific_handler catch i8* bitcast (i32 (i8*, i8*)* @filter to i8*) %ehptr = extractvalue {i8*, i32} %ehvals, i32 0 %sel = extractvalue {i8*, i32} %ehvals, i32 1 %filter_sel = call i32 @llvm.eh.seh.typeid.for(i8* bitcast (i32 (i8*, i8*)* @filter to i8*)) %matches = icmp eq i32 %sel, %filter_sel br i1 %matches, label %eh.except, label %eh.resume eh.except: ret i1 false eh.resume: resume } Reviewers: rjmccall, rsmith, majnemer Differential Revision: http://reviews.llvm.org/D5607 llvm-svn: 226760
2015-01-22 09:36:17 +08:00
// FIXME: Diagnose this case. For now we produce undef in codegen.
template <typename T, T FN()>
T func_template() {
return FN();
}
void inject_builtins() {
func_template<void *, __exception_info>();
func_template<unsigned long, __exception_code>();
}
#endif
void use_seh_after_cxx() {
try { // expected-note {{conflicting 'try' here}}
might_crash();
} catch (int) {
}
__try { // expected-error {{cannot use C++ 'try' in the same function as SEH '__try'}}
might_crash();
} __except(1) {
}
}
void use_cxx_after_seh() {
__try { // expected-note {{conflicting '__try' here}}
might_crash();
} __except(1) {
}
try { // expected-error {{cannot use C++ 'try' in the same function as SEH '__try'}}
might_crash();
} catch (int) {
}
}
#if __cplusplus >= 201103L
void use_seh_in_lambda() {
([]() {
__try {
might_crash();
} __except(1) {
}
})();
try {
might_crash();
} catch (int) {
}
}
#endif
void use_seh_in_block() {
void (^b)() = ^{
__try { // expected-error {{cannot use SEH '__try' in blocks, captured regions, or Obj-C method decls}}
might_crash();
} __except(1) {
}
};
try {
b();
} catch (int) {
}
}
void (^use_seh_in_global_block)() = ^{
__try { // expected-error {{cannot use SEH '__try' in blocks, captured regions, or Obj-C method decls}}
might_crash();
} __except(1) {
}
};
void (^use_cxx_in_global_block)() = ^{
try {
might_crash();
} catch(int) {
}
};