From 580cd4a23ec4be39c765a245ed0cdc9e8c7799cd Mon Sep 17 00:00:00 2001 From: Douglas Gregor Date: Thu, 3 Dec 2009 17:10:37 +0000 Subject: [PATCH] When we're building a CXXExprWithTemporaries, only include those temporaries that are within our current evaluation context. That way, nested evaluation contexts (e.g., within a sizeof() expression) won't see temporaries from outer contexts. Also, make sure to push a new evaluation context when instantiating the initializer of a variable; this may be an unevaluated context or a potentially-evaluated context, depending on whether it's an in-class initializer or not. Fixes PR5672. llvm-svn: 90460 --- clang/lib/Sema/SemaExprCXX.cpp | 11 ++++++---- .../lib/Sema/SemaTemplateInstantiateDecl.cpp | 6 ++++++ clang/lib/Sema/SemaType.cpp | 3 +++ .../SemaTemplate/instantiate-static-var.cpp | 20 +++++++++++++++++++ 4 files changed, 36 insertions(+), 4 deletions(-) diff --git a/clang/lib/Sema/SemaExprCXX.cpp b/clang/lib/Sema/SemaExprCXX.cpp index 148dc639449a..44f781679813 100644 --- a/clang/lib/Sema/SemaExprCXX.cpp +++ b/clang/lib/Sema/SemaExprCXX.cpp @@ -2069,14 +2069,17 @@ Expr *Sema::MaybeCreateCXXExprWithTemporaries(Expr *SubExpr, bool ShouldDestroyTemps) { assert(SubExpr && "sub expression can't be null!"); - if (ExprTemporaries.empty()) + unsigned FirstTemporary = ExprEvalContexts.back().NumTemporaries; + assert(ExprTemporaries.size() >= FirstTemporary); + if (ExprTemporaries.size() == FirstTemporary) return SubExpr; Expr *E = CXXExprWithTemporaries::Create(Context, SubExpr, - &ExprTemporaries[0], - ExprTemporaries.size(), + &ExprTemporaries[FirstTemporary], + ExprTemporaries.size() - FirstTemporary, ShouldDestroyTemps); - ExprTemporaries.clear(); + ExprTemporaries.erase(ExprTemporaries.begin() + FirstTemporary, + ExprTemporaries.end()); return E; } diff --git a/clang/lib/Sema/SemaTemplateInstantiateDecl.cpp b/clang/lib/Sema/SemaTemplateInstantiateDecl.cpp index 95725bf44f7d..394f0eee72af 100644 --- a/clang/lib/Sema/SemaTemplateInstantiateDecl.cpp +++ b/clang/lib/Sema/SemaTemplateInstantiateDecl.cpp @@ -193,6 +193,11 @@ Decl *TemplateDeclInstantiator::VisitVarDecl(VarDecl *D) { TSK_ImplicitInstantiation); if (D->getInit()) { + if (Var->isStaticDataMember() && !D->isOutOfLine()) + SemaRef.PushExpressionEvaluationContext(Sema::Unevaluated); + else + SemaRef.PushExpressionEvaluationContext(Sema::PotentiallyEvaluated); + OwningExprResult Init = SemaRef.SubstExpr(D->getInit(), TemplateArgs); if (Init.isInvalid()) @@ -235,6 +240,7 @@ Decl *TemplateDeclInstantiator::VisitVarDecl(VarDecl *D) { } else SemaRef.AddInitializerToDecl(Sema::DeclPtrTy::make(Var), move(Init), D->hasCXXDirectInitializer()); + SemaRef.PopExpressionEvaluationContext(); } else if (!Var->isStaticDataMember() || Var->isOutOfLine()) SemaRef.ActOnUninitializedDecl(Sema::DeclPtrTy::make(Var), false); diff --git a/clang/lib/Sema/SemaType.cpp b/clang/lib/Sema/SemaType.cpp index afce5e33ba6b..526ab91e9fad 100644 --- a/clang/lib/Sema/SemaType.cpp +++ b/clang/lib/Sema/SemaType.cpp @@ -897,6 +897,9 @@ QualType Sema::GetTypeForDeclarator(Declarator &D, Scope *S, break; } + if (T.isNull()) + return T; + if (T == Context.UndeducedAutoTy) { int Error = -1; diff --git a/clang/test/SemaTemplate/instantiate-static-var.cpp b/clang/test/SemaTemplate/instantiate-static-var.cpp index 452fccf2244d..d4a7008b47bf 100644 --- a/clang/test/SemaTemplate/instantiate-static-var.cpp +++ b/clang/test/SemaTemplate/instantiate-static-var.cpp @@ -72,3 +72,23 @@ void Test() { Z1::value> x2; int y2[Y2::value]; } + +// PR5672 +template +struct X3 {}; + +class Y3 { + public: + ~Y3(); // The error isn't triggered without this dtor. + + void Foo(X3<1>); +}; + +template +struct SizeOf { + static const int value = sizeof(T); +}; + +void MyTest3() { + Y3().Foo(X3::value>()); +}