From fd6c685f2e937d3e3a5b37f9da556c52134e7c35 Mon Sep 17 00:00:00 2001 From: David Majnemer Date: Wed, 27 Nov 2013 22:57:44 +0000 Subject: [PATCH] Sema: Instantiation of variable definitions weren't local enough We wouldn't properly save and restore the pending local instantiations we had built up prior to instantiation of a variable definition. This would lead to us instantiating too much causing crashes and other general badness. This fixes PR14374. llvm-svn: 195887 --- clang/include/clang/Sema/Sema.h | 18 +++++++++++++++++ .../lib/Sema/SemaTemplateInstantiateDecl.cpp | 10 ++++------ .../SemaTemplate/instantiate-local-class.cpp | 20 +++++++++++++++++++ 3 files changed, 42 insertions(+), 6 deletions(-) diff --git a/clang/include/clang/Sema/Sema.h b/clang/include/clang/Sema/Sema.h index 300619b3896e..b635d9b271ca 100644 --- a/clang/include/clang/Sema/Sema.h +++ b/clang/include/clang/Sema/Sema.h @@ -6406,6 +6406,24 @@ public: /// types, static variables, enumerators, etc. std::deque PendingLocalImplicitInstantiations; + class SavePendingLocalImplicitInstantiationsRAII { + public: + SavePendingLocalImplicitInstantiationsRAII(Sema &S): S(S) { + SavedPendingLocalImplicitInstantiations.swap( + S.PendingLocalImplicitInstantiations); + } + + ~SavePendingLocalImplicitInstantiationsRAII() { + SavedPendingLocalImplicitInstantiations.swap( + S.PendingLocalImplicitInstantiations); + } + + private: + Sema &S; + std::deque + SavedPendingLocalImplicitInstantiations; + }; + void PerformPendingInstantiations(bool LocalOnly = false); TypeSourceInfo *SubstType(TypeSourceInfo *T, diff --git a/clang/lib/Sema/SemaTemplateInstantiateDecl.cpp b/clang/lib/Sema/SemaTemplateInstantiateDecl.cpp index 136bb5aa4f82..0e6e204e6610 100644 --- a/clang/lib/Sema/SemaTemplateInstantiateDecl.cpp +++ b/clang/lib/Sema/SemaTemplateInstantiateDecl.cpp @@ -3222,10 +3222,8 @@ void Sema::InstantiateFunctionDefinition(SourceLocation PointOfInstantiation, // while we're still within our own instantiation context. SmallVector SavedVTableUses; std::deque SavedPendingInstantiations; - std::deque - SavedPendingLocalImplicitInstantiations; - SavedPendingLocalImplicitInstantiations.swap( - PendingLocalImplicitInstantiations); + SavePendingLocalImplicitInstantiationsRAII + SavedPendingLocalImplicitInstantiations(*this); if (Recursive) { VTableUses.swap(SavedVTableUses); PendingInstantiations.swap(SavedPendingInstantiations); @@ -3306,8 +3304,6 @@ void Sema::InstantiateFunctionDefinition(SourceLocation PointOfInstantiation, "PendingInstantiations should be empty before it is discarded."); PendingInstantiations.swap(SavedPendingInstantiations); } - SavedPendingLocalImplicitInstantiations.swap( - PendingLocalImplicitInstantiations); } VarTemplateSpecializationDecl *Sema::BuildVarTemplateInstantiation( @@ -3727,6 +3723,8 @@ void Sema::InstantiateVariableDefinition(SourceLocation PointOfInstantiation, // while we're still within our own instantiation context. SmallVector SavedVTableUses; std::deque SavedPendingInstantiations; + SavePendingLocalImplicitInstantiationsRAII + SavedPendingLocalImplicitInstantiations(*this); if (Recursive) { VTableUses.swap(SavedVTableUses); PendingInstantiations.swap(SavedPendingInstantiations); diff --git a/clang/test/SemaTemplate/instantiate-local-class.cpp b/clang/test/SemaTemplate/instantiate-local-class.cpp index 2b5db0fda3ec..bdc5ea0d4a86 100644 --- a/clang/test/SemaTemplate/instantiate-local-class.cpp +++ b/clang/test/SemaTemplate/instantiate-local-class.cpp @@ -160,3 +160,23 @@ void call() { C::func([]() {}); } } + +namespace PR14373 { + struct function { + template function(_Functor __f) { __f(); } + }; + template function exec_func(Func f) { + struct functor { + functor(Func f) : func(f) {} + void operator()() const { func(); } + Func func; + }; + return functor(f); + } + struct Type { + void operator()() const {} + }; + int call() { + exec_func(Type()); + } +}