From 87162c2e528921aabb84e96fe7272348a4b3354e Mon Sep 17 00:00:00 2001 From: Richard Smith Date: Tue, 17 Apr 2012 22:30:01 +0000 Subject: PR12569: Instantiate exception specifications of explicit instantiations and explicit specializations of function templates appropriately. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@154956 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/Sema/SemaDecl.cpp | 7 ++- lib/Sema/SemaTemplateInstantiateDecl.cpp | 11 +++- test/CodeGenCXX/cxx11-exception-spec.cpp | 64 +++++++++++++++++++--- .../instantiate-exception-spec-cxx11.cpp | 13 +++++ 4 files changed, 84 insertions(+), 11 deletions(-) diff --git a/lib/Sema/SemaDecl.cpp b/lib/Sema/SemaDecl.cpp index c7bd72b03e..1550993079 100644 --- a/lib/Sema/SemaDecl.cpp +++ b/lib/Sema/SemaDecl.cpp @@ -7185,8 +7185,7 @@ void Sema::ActOnFinishKNRParamDeclarations(Scope *S, Declarator &D, } } -Decl *Sema::ActOnStartOfFunctionDef(Scope *FnBodyScope, - Declarator &D) { +Decl *Sema::ActOnStartOfFunctionDef(Scope *FnBodyScope, Declarator &D) { assert(getCurFunctionDecl() == 0 && "Function parsing confused"); assert(D.isFunctionDeclarator() && "Not a function declarator!"); Scope *ParentScope = FnBodyScope->getParent(); @@ -7359,6 +7358,10 @@ Decl *Sema::ActOnStartOfFunctionDef(Scope *FnBodyScope, Decl *D) { } } + // Ensure that the function's exception specification is instantiated. + if (const FunctionProtoType *FPT = FD->getType()->getAs()) + ResolveExceptionSpec(D->getLocation(), FPT); + // Checking attributes of current function definition // dllimport attribute. DLLImportAttr *DA = FD->getAttr(); diff --git a/lib/Sema/SemaTemplateInstantiateDecl.cpp b/lib/Sema/SemaTemplateInstantiateDecl.cpp index 39c27fc497..4d312f855f 100644 --- a/lib/Sema/SemaTemplateInstantiateDecl.cpp +++ b/lib/Sema/SemaTemplateInstantiateDecl.cpp @@ -2377,7 +2377,16 @@ static void InstantiateExceptionSpec(Sema &SemaRef, FunctionDecl *New, void Sema::InstantiateExceptionSpec(SourceLocation PointOfInstantiation, FunctionDecl *Decl) { - FunctionDecl *Tmpl = Decl->getTemplateInstantiationPattern(); + // Find the template declaration which contains the exception specification. + // Per [except.spec]p4, prefer the exception spec on the primary template + // if this is an explicit instantiation. + FunctionDecl *Tmpl = 0; + if (Decl->getPrimaryTemplate()) + Tmpl = Decl->getPrimaryTemplate()->getTemplatedDecl(); + else if (FunctionDecl *MemTmpl = Decl->getInstantiatedFromMemberFunction()) + Tmpl = MemTmpl; + else + Tmpl = Decl->getTemplateInstantiationPattern(); assert(Tmpl && "can't instantiate non-template"); if (Decl->getType()->castAs()->getExceptionSpecType() diff --git a/test/CodeGenCXX/cxx11-exception-spec.cpp b/test/CodeGenCXX/cxx11-exception-spec.cpp index 461af78eae..4e08dce2ae 100644 --- a/test/CodeGenCXX/cxx11-exception-spec.cpp +++ b/test/CodeGenCXX/cxx11-exception-spec.cpp @@ -1,18 +1,66 @@ -// RUN: %clang_cc1 -std=c++11 -verify -emit-llvm %s -o - | FileCheck %s +// RUN: %clang_cc1 -std=c++11 -emit-llvm %s -o - -verify -fexceptions -fcxx-exceptions -triple x86_64-linux-gnu | FileCheck %s -template void f() noexcept(sizeof(T) == 4); +void h(); + +template void f() noexcept(sizeof(T) == 4) { h(); } + +template struct S { + static void f() noexcept(sizeof(T) == 4) { h(); } +}; + +// CHECK: define {{.*}} @_Z1fIsEvv() { +template<> void f() { h(); } +// CHECK: define {{.*}} @_Z1fIA2_sEvv() nounwind { +template<> void f() noexcept { h(); } + +// CHECK: define {{.*}} @_ZN1SIsE1fEv() +// CHECK-NOT: nounwind +template<> void S::f() { h(); } +// CHECK: define {{.*}} @_ZN1SIA2_sE1fEv() nounwind +template<> void S::f() noexcept { h(); } + +// CHECK: define {{.*}} @_Z1fIDsEvv() { +template void f(); +// CHECK: define {{.*}} @_Z1fIA2_DsEvv() nounwind { +template void f(); + +// CHECK: define {{.*}} @_ZN1SIDsE1fEv() +// CHECK-NOT: nounwind +template void S::f(); +// CHECK: define {{.*}} @_ZN1SIA2_DsE1fEv() nounwind +template void S::f(); void g() { - // CHECK: declare void @_Z1fIiEvv() nounwind + // CHECK: define {{.*}} @_Z1fIiEvv() nounwind { f(); - // CHECK: declare void @_Z1fIA2_iEvv() + // CHECK: define {{.*}} @_Z1fIA2_iEvv() { f(); - // CHECK: declare void @_Z1fIfEvv() nounwind + + // CHECK: define {{.*}} @_ZN1SIiE1fEv() nounwind + S::f(); + // CHECK: define {{.*}} @_ZN1SIA2_iE1fEv() + // CHECK-NOT: nounwind + S::f(); + + // CHECK: define {{.*}} @_Z1fIfEvv() nounwind { void (*f1)() = &f; - // CHECK: declare void @_Z1fIdEvv() + // CHECK: define {{.*}} @_Z1fIdEvv() { void (*f2)() = &f; - // CHECK: declare void @_Z1fIA4_cEvv() nounwind + + // CHECK: define {{.*}} @_ZN1SIfE1fEv() nounwind + void (*f3)() = &S::f; + // CHECK: define {{.*}} @_ZN1SIdE1fEv() + // CHECK-NOT: nounwind + void (*f4)() = &S::f; + + // CHECK: define {{.*}} @_Z1fIA4_cEvv() nounwind { (void)&f; - // CHECK: declare void @_Z1fIcEvv() + // CHECK: define {{.*}} @_Z1fIcEvv() { (void)&f; + + // CHECK: define {{.*}} @_ZN1SIA4_cE1fEv() nounwind + (void)&S::f; + // CHECK: define {{.*}} @_ZN1SIcE1fEv() + // CHECK-NOT: nounwind + (void)&S::f; } diff --git a/test/SemaTemplate/instantiate-exception-spec-cxx11.cpp b/test/SemaTemplate/instantiate-exception-spec-cxx11.cpp index 763b9ed41b..d29c8862b9 100644 --- a/test/SemaTemplate/instantiate-exception-spec-cxx11.cpp +++ b/test/SemaTemplate/instantiate-exception-spec-cxx11.cpp @@ -105,3 +105,16 @@ namespace core_19754_example { base val = base(); } + +namespace pr9485 { + template void f1(T) throw(typename T::exception); // expected-note {{candidate}} + template void f1(T, int = 0) throw(typename T::noitpecxe); // expected-note {{candidate}} + + template void f2(T) noexcept(T::throws); // expected-note {{candidate}} + template void f2(T, int = 0) noexcept(T::sworht); // expected-note {{candidate}} + + void test() { + f1(0); // expected-error {{ambiguous}} + f2(0); // expected-error {{ambiguous}} + } +} -- cgit v1.2.3