diff --git a/clang/lib/Sema/SemaTemplateInstantiate.cpp b/clang/lib/Sema/SemaTemplateInstantiate.cpp index 6db0916e4459..539b4c409fdb 100644 --- a/clang/lib/Sema/SemaTemplateInstantiate.cpp +++ b/clang/lib/Sema/SemaTemplateInstantiate.cpp @@ -1067,6 +1067,10 @@ ParmVarDecl *Sema::SubstParmVarDecl(ParmVarDecl *OldParm, NewParm->setHasInheritedDefaultArg(OldParm->hasInheritedDefaultArg()); CurrentInstantiationScope->InstantiatedLocal(OldParm, NewParm); + // Set DeclContext if inside a Block. + if (BlockScopeInfo *CurBlock = getCurBlock()) + NewParm->setDeclContext(CurBlock->TheDecl); + return NewParm; } diff --git a/clang/lib/Sema/SemaTemplateInstantiateDecl.cpp b/clang/lib/Sema/SemaTemplateInstantiateDecl.cpp index 2fd35285324e..b80e824bfd67 100644 --- a/clang/lib/Sema/SemaTemplateInstantiateDecl.cpp +++ b/clang/lib/Sema/SemaTemplateInstantiateDecl.cpp @@ -400,6 +400,9 @@ Decl *TemplateDeclInstantiator::VisitVarDecl(VarDecl *D) { SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Var); } InstantiateAttrs(D, Var); + // Set DeclContext if inside a Block. + if (BlockScopeInfo *CurBlock = SemaRef.getCurBlock()) + D->setDeclContext(CurBlock->TheDecl); // Link instantiations of static data members back to the template from // which they were instantiated. diff --git a/clang/lib/Sema/TreeTransform.h b/clang/lib/Sema/TreeTransform.h index 132d04927b94..17103c515f8b 100644 --- a/clang/lib/Sema/TreeTransform.h +++ b/clang/lib/Sema/TreeTransform.h @@ -4198,10 +4198,6 @@ TreeTransform::TransformDeclRefExpr(DeclRefExpr *E) { if (!ND) return SemaRef.ExprError(); - // Set DeclContext if inside a Block. - if (BlockScopeInfo *CurBlock = SemaRef.getCurBlock()) - ND->setDeclContext(CurBlock->TheDecl); - if (!getDerived().AlwaysRebuild() && Qualifier == E->getQualifier() && ND == E->getDecl() && diff --git a/clang/test/CodeGenCXX/instantiate-blocks.cpp b/clang/test/CodeGenCXX/instantiate-blocks.cpp index c8f897de8200..e206582191ca 100644 --- a/clang/test/CodeGenCXX/instantiate-blocks.cpp +++ b/clang/test/CodeGenCXX/instantiate-blocks.cpp @@ -31,3 +31,29 @@ void test2(void) { foo(100, 'a'); } + +namespace rdar6182276 { +extern "C" { +int printf(const char *, ...); +} + +template T foo(T t) +{ + void (^testing)(int) = ^(int bar) { printf("bar is %d\n", bar); }; + printf("bar is\n"); + return 1; +} + +template void gorf(T t) +{ + foo(t); +} + + +void test(void) +{ + gorf(2); +} +} + +