<div dir="ltr">I reverted this for a bit in r228262.</div><div class="gmail_extra"><br><div class="gmail_quote">On Tue, Feb 3, 2015 at 2:52 PM, Reid Kleckner <span dir="ltr"><<a href="mailto:reid@kleckner.net" target="_blank">reid@kleckner.net</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Author: rnk<br>
Date: Tue Feb 3 16:52:35 2015<br>
New Revision: 228058<br>
<br>
URL: <a href="http://llvm.org/viewvc/llvm-project?rev=228058&view=rev" target="_blank">http://llvm.org/viewvc/llvm-project?rev=228058&view=rev</a><br>
Log:<br>
SEH: Track users of __try so we can pick a per-func EH personality<br>
<br>
There are four major kinds of declarations that cause code generation:<br>
- FunctionDecl (includes CXXMethodDecl etc)<br>
- ObjCMethodDecl<br>
- BlockDecl<br>
- CapturedDecl<br>
<br>
This patch tracks __try usage on FunctionDecls and diagnoses __try usage<br>
in other decls. If someone wants to use __try from ObjC, they can use it<br>
from a free function, since the ObjC code will need an ObjC-style EH<br>
personality.<br>
<br>
Eventually we will want to look through CapturedDecls and track SEH<br>
usage on the parent FunctionDecl, if present.<br>
<br>
Added:<br>
cfe/trunk/test/CodeGenCXX/exceptions-seh.cpp<br>
Modified:<br>
cfe/trunk/include/clang/AST/Decl.h<br>
cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td<br>
cfe/trunk/lib/CodeGen/CGException.cpp<br>
cfe/trunk/lib/Sema/SemaStmt.cpp<br>
cfe/trunk/test/SemaCXX/exceptions-seh.cpp<br>
<br>
Modified: cfe/trunk/include/clang/AST/Decl.h<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/AST/Decl.h?rev=228058&r1=228057&r2=228058&view=diff" target="_blank">http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/AST/Decl.h?rev=228058&r1=228057&r2=228058&view=diff</a><br>
==============================================================================<br>
--- cfe/trunk/include/clang/AST/Decl.h (original)<br>
+++ cfe/trunk/include/clang/AST/Decl.h Tue Feb 3 16:52:35 2015<br>
@@ -1482,6 +1482,9 @@ private:<br>
bool IsLateTemplateParsed : 1;<br>
bool IsConstexpr : 1;<br>
<br>
+ /// \brief Indicates if the function uses __try.<br>
+ bool UsesSEHTry : 1;<br>
+<br>
/// \brief Indicates if the function was a definition but its body was<br>
/// skipped.<br>
unsigned HasSkippedBody : 1;<br>
@@ -1570,8 +1573,8 @@ protected:<br>
HasWrittenPrototype(true), IsDeleted(false), IsTrivial(false),<br>
IsDefaulted(false), IsExplicitlyDefaulted(false),<br>
HasImplicitReturnZero(false), IsLateTemplateParsed(false),<br>
- IsConstexpr(isConstexprSpecified), HasSkippedBody(false),<br>
- EndRangeLoc(NameInfo.getEndLoc()),<br>
+ IsConstexpr(isConstexprSpecified), UsesSEHTry(false),<br>
+ HasSkippedBody(false), EndRangeLoc(NameInfo.getEndLoc()),<br>
TemplateOrSpecialization(),<br>
DNLoc(NameInfo.getInfo()) {}<br>
<br>
@@ -1751,6 +1754,10 @@ public:<br>
bool isConstexpr() const { return IsConstexpr; }<br>
void setConstexpr(bool IC) { IsConstexpr = IC; }<br>
<br>
+ /// Whether this is a (C++11) constexpr function or constexpr constructor.<br>
+ bool usesSEHTry() const { return UsesSEHTry; }<br>
+ void setUsesSEHTry(bool UST) { UsesSEHTry = UST; }<br>
+<br>
/// \brief Whether this function has been deleted.<br>
///<br>
/// A function that is "deleted" (via the C++0x "= delete" syntax)<br>
<br>
Modified: cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td?rev=228058&r1=228057&r2=228058&view=diff" target="_blank">http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td?rev=228058&r1=228057&r2=228058&view=diff</a><br>
==============================================================================<br>
--- cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td (original)<br>
+++ cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td Tue Feb 3 16:52:35 2015<br>
@@ -5466,6 +5466,8 @@ def err_exceptions_disabled : Error<<br>
"cannot use '%0' with exceptions disabled">;<br>
def err_objc_exceptions_disabled : Error<<br>
"cannot use '%0' with Objective-C exceptions disabled">;<br>
+def err_seh_try_outside_functions : Error<<br>
+ "cannot use SEH '__try' in blocks, captured regions, or Obj-C method decls">;<br>
def err_mixing_cxx_try_seh_try : Error<<br>
"cannot use C++ 'try' in the same function as SEH '__try'">;<br>
def note_conflicting_try_here : Note<<br>
<br>
Modified: cfe/trunk/lib/CodeGen/CGException.cpp<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/CodeGen/CGException.cpp?rev=228058&r1=228057&r2=228058&view=diff" target="_blank">http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/CodeGen/CGException.cpp?rev=228058&r1=228057&r2=228058&view=diff</a><br>
==============================================================================<br>
--- cfe/trunk/lib/CodeGen/CGException.cpp (original)<br>
+++ cfe/trunk/lib/CodeGen/CGException.cpp Tue Feb 3 16:52:35 2015<br>
@@ -128,7 +128,12 @@ namespace {<br>
// This function must have prototype void(void*).<br>
const char *CatchallRethrowFn;<br>
<br>
- static const EHPersonality &get(CodeGenModule &CGM);<br>
+ static const EHPersonality &get(CodeGenModule &CGM,<br>
+ const FunctionDecl *FD);<br>
+ static const EHPersonality &get(CodeGenFunction &CGF) {<br>
+ return get(CGF.CGM, dyn_cast_or_null<FunctionDecl>(CGF.CurCodeDecl));<br>
+ }<br>
+<br>
static const EHPersonality GNU_C;<br>
static const EHPersonality GNU_C_SJLJ;<br>
static const EHPersonality GNU_C_SEH;<br>
@@ -141,6 +146,7 @@ namespace {<br>
static const EHPersonality GNU_CPlusPlus_SEH;<br>
static const EHPersonality MSVC_except_handler;<br>
static const EHPersonality MSVC_C_specific_handler;<br>
+ static const EHPersonality MSVC_CxxFrameHandler3;<br>
};<br>
}<br>
<br>
@@ -167,6 +173,8 @@ const EHPersonality<br>
EHPersonality::MSVC_except_handler = { "_except_handler3", nullptr };<br>
const EHPersonality<br>
EHPersonality::MSVC_C_specific_handler = { "__C_specific_handler", nullptr };<br>
+const EHPersonality<br>
+EHPersonality::MSVC_CxxFrameHandler3 = { "__CxxFrameHandler3", nullptr };<br>
<br>
/// On Win64, use libgcc's SEH personality function. We fall back to dwarf on<br>
/// other platforms, unless the user asked for SjLj exceptions.<br>
@@ -239,35 +247,27 @@ static const EHPersonality &getObjCXXPer<br>
llvm_unreachable("bad runtime kind");<br>
}<br>
<br>
-static const EHPersonality &getCPersonalityMSVC(const llvm::Triple &T,<br>
- const LangOptions &L) {<br>
- if (L.SjLjExceptions)<br>
- return EHPersonality::GNU_C_SJLJ;<br>
-<br>
+static const EHPersonality &getSEHPersonalityMSVC(const llvm::Triple &T) {<br>
if (T.getArch() == llvm::Triple::x86)<br>
return EHPersonality::MSVC_except_handler;<br>
return EHPersonality::MSVC_C_specific_handler;<br>
}<br>
<br>
-static const EHPersonality &getCXXPersonalityMSVC(const llvm::Triple &T,<br>
- const LangOptions &L) {<br>
- if (L.SjLjExceptions)<br>
- return EHPersonality::GNU_CPlusPlus_SJLJ;<br>
- // FIXME: Implement C++ exceptions.<br>
- return getCPersonalityMSVC(T, L);<br>
-}<br>
-<br>
-const EHPersonality &EHPersonality::get(CodeGenModule &CGM) {<br>
+const EHPersonality &EHPersonality::get(CodeGenModule &CGM,<br>
+ const FunctionDecl *FD) {<br>
const llvm::Triple &T = CGM.getTarget().getTriple();<br>
const LangOptions &L = CGM.getLangOpts();<br>
+<br>
// Try to pick a personality function that is compatible with MSVC if we're<br>
// not compiling Obj-C. Obj-C users better have an Obj-C runtime that supports<br>
// the GCC-style personality function.<br>
if (T.isWindowsMSVCEnvironment() && !L.ObjC1) {<br>
- if (L.CPlusPlus)<br>
- return getCXXPersonalityMSVC(T, L);<br>
+ if (L.SjLjExceptions)<br>
+ return EHPersonality::GNU_CPlusPlus_SJLJ;<br>
+ else if (FD && FD->usesSEHTry())<br>
+ return getSEHPersonalityMSVC(T);<br>
else<br>
- return getCPersonalityMSVC(T, L);<br>
+ return EHPersonality::MSVC_CxxFrameHandler3;<br>
}<br>
<br>
if (L.CPlusPlus && L.ObjC1)<br>
@@ -354,7 +354,7 @@ void CodeGenModule::SimplifyPersonality(<br>
if (!LangOpts.ObjCRuntime.isNeXTFamily())<br>
return;<br>
<br>
- const EHPersonality &ObjCXX = EHPersonality::get(*this);<br>
+ const EHPersonality &ObjCXX = EHPersonality::get(*this, /*FD=*/nullptr);<br>
const EHPersonality &CXX =<br>
getCXXPersonality(getTarget().getTriple(), LangOpts);<br>
if (&ObjCXX == &CXX)<br>
@@ -772,7 +772,7 @@ llvm::BasicBlock *CodeGenFunction::EmitL<br>
CGBuilderTy::InsertPoint savedIP = Builder.saveAndClearIP();<br>
auto DL = ApplyDebugLocation::CreateDefaultArtificial(*this, CurEHLocation);<br>
<br>
- const EHPersonality &personality = EHPersonality::get(CGM);<br>
+ const EHPersonality &personality = EHPersonality::get(*this);<br>
<br>
// Create and configure the landing pad.<br>
llvm::BasicBlock *lpad = createBasicBlock("lpad");<br>
@@ -1589,7 +1589,7 @@ llvm::BasicBlock *CodeGenFunction::getTe<br>
Builder.SetInsertPoint(TerminateLandingPad);<br>
<br>
// Tell the backend that this is a landing pad.<br>
- const EHPersonality &Personality = EHPersonality::get(CGM);<br>
+ const EHPersonality &Personality = EHPersonality::get(*this);<br>
llvm::LandingPadInst *LPadInst =<br>
Builder.CreateLandingPad(llvm::StructType::get(Int8PtrTy, Int32Ty, nullptr),<br>
getOpaquePersonalityFn(CGM, Personality), 0);<br>
@@ -1648,7 +1648,7 @@ llvm::BasicBlock *CodeGenFunction::getEH<br>
EHResumeBlock = createBasicBlock("eh.resume");<br>
Builder.SetInsertPoint(EHResumeBlock);<br>
<br>
- const EHPersonality &Personality = EHPersonality::get(CGM);<br>
+ const EHPersonality &Personality = EHPersonality::get(*this);<br>
<br>
// This can always be a call because we necessarily didn't find<br>
// anything on the EH stack which needs our help.<br>
<br>
Modified: cfe/trunk/lib/Sema/SemaStmt.cpp<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaStmt.cpp?rev=228058&r1=228057&r2=228058&view=diff" target="_blank">http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaStmt.cpp?rev=228058&r1=228057&r2=228058&view=diff</a><br>
==============================================================================<br>
--- cfe/trunk/lib/Sema/SemaStmt.cpp (original)<br>
+++ cfe/trunk/lib/Sema/SemaStmt.cpp Tue Feb 3 16:52:35 2015<br>
@@ -3303,11 +3303,12 @@ StmtResult Sema::ActOnCXXTryBlock(Source<br>
if (getCurScope() && getCurScope()->isOpenMPSimdDirectiveScope())<br>
Diag(TryLoc, diag::err_omp_simd_region_cannot_use_stmt) << "try";<br>
<br>
+ sema::FunctionScopeInfo *FSI = getCurFunction();<br>
+<br>
// C++ try is incompatible with SEH __try.<br>
- if (!getLangOpts().Borland && getCurFunction()->FirstSEHTryLoc.isValid()) {<br>
+ if (!getLangOpts().Borland && FSI->FirstSEHTryLoc.isValid()) {<br>
Diag(TryLoc, diag::err_mixing_cxx_try_seh_try);<br>
- Diag(getCurFunction()->FirstSEHTryLoc, diag::note_conflicting_try_here)<br>
- << "'__try'";<br>
+ Diag(FSI->FirstSEHTryLoc, diag::note_conflicting_try_here) << "'__try'";<br>
}<br>
<br>
const unsigned NumHandlers = Handlers.size();<br>
@@ -3352,7 +3353,7 @@ StmtResult Sema::ActOnCXXTryBlock(Source<br>
}<br>
}<br>
<br>
- getCurFunction()->setHasCXXTry(TryLoc);<br>
+ FSI->setHasCXXTry(TryLoc);<br>
<br>
// FIXME: We should detect handlers that cannot catch anything because an<br>
// earlier handler catches a superclass. Need to find a method that is not<br>
@@ -3367,15 +3368,29 @@ StmtResult Sema::ActOnSEHTryBlock(bool I<br>
Stmt *TryBlock, Stmt *Handler) {<br>
assert(TryBlock && Handler);<br>
<br>
+ sema::FunctionScopeInfo *FSI = getCurFunction();<br>
+<br>
// SEH __try is incompatible with C++ try. Borland appears to support this,<br>
// however.<br>
- if (!getLangOpts().Borland && getCurFunction()->FirstCXXTryLoc.isValid()) {<br>
- Diag(TryLoc, diag::err_mixing_cxx_try_seh_try);<br>
- Diag(getCurFunction()->FirstCXXTryLoc, diag::note_conflicting_try_here)<br>
- << "'try'";<br>
+ if (!getLangOpts().Borland) {<br>
+ if (FSI->FirstCXXTryLoc.isValid()) {<br>
+ Diag(TryLoc, diag::err_mixing_cxx_try_seh_try);<br>
+ Diag(FSI->FirstCXXTryLoc, diag::note_conflicting_try_here) << "'try'";<br>
+ }<br>
}<br>
<br>
- getCurFunction()->setHasSEHTry(TryLoc);<br>
+ FSI->setHasSEHTry(TryLoc);<br>
+<br>
+ // Reject __try in Obj-C methods, blocks, and captured decls, since we don't<br>
+ // track if they use SEH.<br>
+ DeclContext *DC = CurContext;<br>
+ while (DC && !DC->isFunctionOrMethod())<br>
+ DC = DC->getParent();<br>
+ FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(DC);<br>
+ if (FD)<br>
+ FD->setUsesSEHTry(true);<br>
+ else<br>
+ Diag(TryLoc, diag::err_seh_try_outside_functions);<br>
<br>
return SEHTryStmt::Create(Context, IsCXXTry, TryLoc, TryBlock, Handler);<br>
}<br>
<br>
Added: cfe/trunk/test/CodeGenCXX/exceptions-seh.cpp<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/cfe/trunk/test/CodeGenCXX/exceptions-seh.cpp?rev=228058&view=auto" target="_blank">http://llvm.org/viewvc/llvm-project/cfe/trunk/test/CodeGenCXX/exceptions-seh.cpp?rev=228058&view=auto</a><br>
==============================================================================<br>
--- cfe/trunk/test/CodeGenCXX/exceptions-seh.cpp (added)<br>
+++ cfe/trunk/test/CodeGenCXX/exceptions-seh.cpp Tue Feb 3 16:52:35 2015<br>
@@ -0,0 +1,76 @@<br>
+// RUN: %clang_cc1 -std=c++11 -fblocks -fms-extensions %s -triple=x86_64-windows-msvc -emit-llvm -o - -fcxx-exceptions -fexceptions -mconstructor-aliases | FileCheck %s<br>
+<br>
+extern "C" void might_throw();<br>
+<br>
+struct HasCleanup {<br>
+ HasCleanup();<br>
+ ~HasCleanup();<br>
+ int padding;<br>
+};<br>
+<br>
+extern "C" void use_cxx() {<br>
+ HasCleanup x;<br>
+ might_throw();<br>
+}<br>
+<br>
+// Make sure we use __CxxFrameHandler3 for C++ EH.<br>
+<br>
+// CHECK-LABEL: define void @use_cxx()<br>
+// CHECK: call %struct.HasCleanup* @"\01??0HasCleanup@@QEAA@XZ"(%struct.HasCleanup* %{{.*}})<br>
+// CHECK: invoke void @might_throw()<br>
+// CHECK: to label %[[cont:[^ ]*]] unwind label %[[lpad:[^ ]*]]<br>
+//<br>
+// CHECK: [[cont]]<br>
+// CHECK: call void @"\01??1HasCleanup@@QEAA@XZ"(%struct.HasCleanup* %{{.*}})<br>
+// CHECK: ret void<br>
+//<br>
+// CHECK: [[lpad]]<br>
+// CHECK: landingpad { i8*, i32 } personality i8* bitcast (i32 (...)* @__CxxFrameHandler3 to i8*)<br>
+// CHECK-NEXT: cleanup<br>
+// CHECK: call void @"\01??1HasCleanup@@QEAA@XZ"(%struct.HasCleanup* %{{.*}})<br>
+// CHECK: br label %[[resume:[^ ]*]]<br>
+//<br>
+// CHECK: [[resume]]<br>
+// CHECK: resume<br>
+<br>
+extern "C" void use_seh() {<br>
+ __try {<br>
+ might_throw();<br>
+ } __except(1) {<br>
+ }<br>
+}<br>
+<br>
+// Make sure we use __C_specific_handler for SEH.<br>
+<br>
+// CHECK-LABEL: define void @use_seh()<br>
+// CHECK: invoke void @might_throw()<br>
+// CHECK: to label %[[cont:[^ ]*]] unwind label %[[lpad:[^ ]*]]<br>
+//<br>
+// CHECK: [[cont]]<br>
+// CHECK: br label %[[ret:[^ ]*]]<br>
+//<br>
+// CHECK: [[lpad]]<br>
+// CHECK: landingpad { i8*, i32 } personality i8* bitcast (i32 (...)* @__C_specific_handler to i8*)<br>
+// CHECK-NEXT: catch i8*<br>
+//<br>
+// CHECK: br label %[[ret]]<br>
+//<br>
+// CHECK: [[ret]]<br>
+// CHECK: ret void<br>
+<br>
+void use_seh_in_lambda() {<br>
+ ([]() {<br>
+ __try {<br>
+ might_throw();<br>
+ } __except(1) {<br>
+ }<br>
+ })();<br>
+ HasCleanup x;<br>
+ might_throw();<br>
+}<br>
+<br>
+// CHECK-LABEL: define void @"\01?use_seh_in_lambda@@YAXXZ"()<br>
+// CHECK: landingpad { i8*, i32 } personality i8* bitcast (i32 (...)* @__CxxFrameHandler3 to i8*)<br>
+<br>
+// CHECK-LABEL: define internal void @"\01??R<lambda_0>@?use_seh_in_lambda@@YAXXZ@QEBAXXZ"(%class.anon* %this)<br>
+// CHECK: landingpad { i8*, i32 } personality i8* bitcast (i32 (...)* @__C_specific_handler to i8*)<br>
<br>
Modified: cfe/trunk/test/SemaCXX/exceptions-seh.cpp<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/cfe/trunk/test/SemaCXX/exceptions-seh.cpp?rev=228058&r1=228057&r2=228058&view=diff" target="_blank">http://llvm.org/viewvc/llvm-project/cfe/trunk/test/SemaCXX/exceptions-seh.cpp?rev=228058&r1=228057&r2=228058&view=diff</a><br>
==============================================================================<br>
--- cfe/trunk/test/SemaCXX/exceptions-seh.cpp (original)<br>
+++ cfe/trunk/test/SemaCXX/exceptions-seh.cpp Tue Feb 3 16:52:35 2015<br>
@@ -1,4 +1,5 @@<br>
-// RUN: %clang_cc1 -triple x86_64-windows-msvc -fms-extensions -fsyntax-only -fexceptions -fcxx-exceptions -verify %s<br>
+// RUN: %clang_cc1 -std=c++03 -fblocks -triple x86_64-windows-msvc -fms-extensions -fsyntax-only -fexceptions -fcxx-exceptions -verify %s<br>
+// RUN: %clang_cc1 -std=c++11 -fblocks -triple x86_64-windows-msvc -fms-extensions -fsyntax-only -fexceptions -fcxx-exceptions -verify %s<br>
<br>
// Basic usage should work.<br>
int safe_div(int n, int d) {<br>
@@ -37,6 +38,7 @@ void instantiate_bad_scope_tmpl() {<br>
bad_builtin_scope_template<might_crash>();<br>
}<br>
<br>
+#if __cplusplus < 201103L<br>
// FIXME: Diagnose this case. For now we produce undef in codegen.<br>
template <typename T, T FN()><br>
T func_template() {<br>
@@ -46,6 +48,7 @@ void inject_builtins() {<br>
func_template<void *, __exception_info>();<br>
func_template<unsigned long, __exception_code>();<br>
}<br>
+#endif<br>
<br>
void use_seh_after_cxx() {<br>
try { // expected-note {{conflicting 'try' here}}<br>
@@ -68,3 +71,45 @@ void use_cxx_after_seh() {<br>
} catch (int) {<br>
}<br>
}<br>
+<br>
+#if __cplusplus >= 201103L<br>
+void use_seh_in_lambda() {<br>
+ ([]() {<br>
+ __try {<br>
+ might_crash();<br>
+ } __except(1) {<br>
+ }<br>
+ })();<br>
+ try {<br>
+ might_crash();<br>
+ } catch (int) {<br>
+ }<br>
+}<br>
+#endif<br>
+<br>
+void use_seh_in_block() {<br>
+ void (^b)() = ^{<br>
+ __try { // expected-error {{cannot use SEH '__try' in blocks, captured regions, or Obj-C method decls}}<br>
+ might_crash();<br>
+ } __except(1) {<br>
+ }<br>
+ };<br>
+ try {<br>
+ b();<br>
+ } catch (int) {<br>
+ }<br>
+}<br>
+<br>
+void (^use_seh_in_global_block)() = ^{<br>
+ __try { // expected-error {{cannot use SEH '__try' in blocks, captured regions, or Obj-C method decls}}<br>
+ might_crash();<br>
+ } __except(1) {<br>
+ }<br>
+};<br>
+<br>
+void (^use_cxx_in_global_block)() = ^{<br>
+ try {<br>
+ might_crash();<br>
+ } catch(int) {<br>
+ }<br>
+};<br>
<br>
<br>
_______________________________________________<br>
cfe-commits mailing list<br>
<a href="mailto:cfe-commits@cs.uiuc.edu">cfe-commits@cs.uiuc.edu</a><br>
<a href="http://lists.cs.uiuc.edu/mailman/listinfo/cfe-commits" target="_blank">http://lists.cs.uiuc.edu/mailman/listinfo/cfe-commits</a><br>
</blockquote></div><br></div>