From a6c7efec2bb7386ea40f5203d286fb90520f5b71 Mon Sep 17 00:00:00 2001 From: Fariborz Jahanian Date: Tue, 13 Jul 2010 20:05:58 +0000 Subject: [PATCH] More block instantiation stuff. Set variable/param DeclContext to block context when first instantiating them. llvm-svn: 108266 --- clang/lib/Sema/SemaTemplateInstantiate.cpp | 4 +++ .../lib/Sema/SemaTemplateInstantiateDecl.cpp | 3 +++ clang/lib/Sema/TreeTransform.h | 4 --- clang/test/CodeGenCXX/instantiate-blocks.cpp | 26 +++++++++++++++++++ 4 files changed, 33 insertions(+), 4 deletions(-) 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); +} +} + +