diff --git a/clang/lib/CodeGen/ItaniumCXXABI.cpp b/clang/lib/CodeGen/ItaniumCXXABI.cpp index bcd171724c41d..a84412bd5c045 100644 --- a/clang/lib/CodeGen/ItaniumCXXABI.cpp +++ b/clang/lib/CodeGen/ItaniumCXXABI.cpp @@ -5150,9 +5150,14 @@ WebAssemblyCXXABI::emitTerminateForUnexpectedException(CodeGenFunction &CGF, // Itanium ABI calls __clang_call_terminate(), which __cxa_begin_catch() on // the violating exception to mark it handled, but it is currently hard to do // with wasm EH instruction structure with catch/catch_all, we just call - // std::terminate and ignore the violating exception as in CGCXXABI. + // std::terminate and ignore the violating exception as in CGCXXABI in Wasm EH + // and call __clang_call_terminate only in Emscripten EH. // TODO Consider code transformation that makes calling __clang_call_terminate - // possible. + // in Wasm EH possible. + if (Exn && !EHPersonality::get(CGF).isWasmPersonality()) { + assert(CGF.CGM.getLangOpts().CPlusPlus); + return CGF.EmitNounwindRuntimeCall(getClangCallTerminateFn(CGF.CGM), Exn); + } return CGCXXABI::emitTerminateForUnexpectedException(CGF, Exn); } diff --git a/clang/test/CodeGenCXX/wasm-eh.cpp b/clang/test/CodeGenCXX/wasm-eh.cpp index 9dc15633bfed9..e8797794e7c1e 100644 --- a/clang/test/CodeGenCXX/wasm-eh.cpp +++ b/clang/test/CodeGenCXX/wasm-eh.cpp @@ -6,6 +6,9 @@ // RUN: %clang_cc1 %s -triple wasm32-unknown-unknown -fms-extensions -fexceptions -fcxx-exceptions -mllvm -wasm-enable-eh -exception-model=wasm -target-feature +exception-handling -emit-llvm -o - -std=c++11 | FileCheck %s // RUN: %clang_cc1 %s -triple wasm64-unknown-unknown -fms-extensions -fexceptions -fcxx-exceptions -mllvm -wasm-enable-eh -exception-model=wasm -target-feature +exception-handling -emit-llvm -o - -std=c++11 | FileCheck %s +// Test code generation for Wasm EH using WebAssembly EH proposal. +// (https://github.com/WebAssembly/exception-handling/blob/main/proposals/exception-handling/Exceptions.md) + void may_throw(); void dont_throw() noexcept; @@ -381,6 +384,15 @@ void test8() { // CHECK: unreachable +void noexcept_throw() noexcept { + throw 3; +} + +// CATCH-LABEL: define void @_Z14noexcept_throwv() +// CHECK: %{{.*}} = cleanuppad within none [] +// CHECK-NEXT: call void @_ZSt9terminatev() + + // RUN: %clang_cc1 %s -triple wasm32-unknown-unknown -fms-extensions -fexceptions -fcxx-exceptions -exception-model=wasm -target-feature +exception-handling -emit-llvm -o - -std=c++11 2>&1 | FileCheck %s --check-prefix=WARNING-DEFAULT // RUN: %clang_cc1 %s -triple wasm32-unknown-unknown -fms-extensions -fexceptions -fcxx-exceptions -exception-model=wasm -target-feature +exception-handling -Wwasm-exception-spec -emit-llvm -o - -std=c++11 2>&1 | FileCheck %s --check-prefix=WARNING-ON // RUN: %clang_cc1 %s -triple wasm32-unknown-unknown -fms-extensions -fexceptions -fcxx-exceptions -exception-model=wasm -target-feature +exception-handling -Wno-wasm-exception-spec -emit-llvm -o - -std=c++11 2>&1 | FileCheck %s --check-prefix=WARNING-OFF diff --git a/clang/test/CodeGenCXX/wasm-em-eh.cpp b/clang/test/CodeGenCXX/wasm-em-eh.cpp new file mode 100644 index 0000000000000..fc96fa96b5140 --- /dev/null +++ b/clang/test/CodeGenCXX/wasm-em-eh.cpp @@ -0,0 +1,13 @@ +// RUN: %clang_cc1 %s -triple wasm32-unknown-emscripten -fexceptions -fcxx-exceptions -emit-llvm -o - -std=c++11 2>&1 | FileCheck %s + +// Test code generation for Wasm's Emscripten (JavaScript-style) EH. + +void noexcept_throw() noexcept { + throw 3; +} + +// CATCH-LABEL: define void @_Z14noexcept_throwv() +// CHECK: %[[LPAD:.*]] = landingpad { ptr, i32 } +// CHECK-NEXT: catch ptr null +// CHECK-NEXT: %[[EXN:.*]] = extractvalue { ptr, i32 } %[[LPAD]], 0 +// CHECK-NEXT: call void @__clang_call_terminate(ptr %[[EXN]])