From 28125b7374a15e096cd2ffc26706ec4d8fced29a Mon Sep 17 00:00:00 2001 From: Hans Wennborg Date: Tue, 10 Jun 2014 00:55:51 +0000 Subject: [PATCH] Improve checking for dynamic initializers of dllimport fields in template instantiation We would previously assert if the initializer was dependent. I also think that checking isConstantInitializer is more correct here than checkInitIsICE. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@210505 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/Sema/SemaTemplateInstantiateDecl.cpp | 16 ++++++++-------- test/CodeGenCXX/dllimport.cpp | 7 +++++++ 2 files changed, 15 insertions(+), 8 deletions(-) diff --git a/lib/Sema/SemaTemplateInstantiateDecl.cpp b/lib/Sema/SemaTemplateInstantiateDecl.cpp index 2fa28a33a1..bce2fd552e 100644 --- a/lib/Sema/SemaTemplateInstantiateDecl.cpp +++ b/lib/Sema/SemaTemplateInstantiateDecl.cpp @@ -3671,12 +3671,6 @@ void Sema::InstantiateVariableInitializer( // We already have an initializer in the class. return; - if (Var->hasAttr() && - !(OldVar->getInit() && OldVar->checkInitIsICE())) { - // Do not dynamically initialize dllimport variables. - return; - } - if (OldVar->getInit()) { if (Var->isStaticDataMember() && !OldVar->isOutOfLine()) PushExpressionEvaluationContext(Sema::ConstantEvaluated, OldVar); @@ -3689,9 +3683,15 @@ void Sema::InstantiateVariableInitializer( OldVar->getInitStyle() == VarDecl::CallInit); if (!Init.isInvalid()) { bool TypeMayContainAuto = true; - if (Init.get()) { + Expr *InitExpr = Init.get(); + + if (Var->hasAttr() && InitExpr && + !InitExpr->isConstantInitializer(getASTContext(), false)) { + // Do not dynamically initialize dllimport variables. + return; + } else if (InitExpr) { bool DirectInit = OldVar->isDirectInit(); - AddInitializerToDecl(Var, Init.get(), DirectInit, TypeMayContainAuto); + AddInitializerToDecl(Var, InitExpr, DirectInit, TypeMayContainAuto); } else ActOnUninitializedDecl(Var, TypeMayContainAuto); } else { diff --git a/test/CodeGenCXX/dllimport.cpp b/test/CodeGenCXX/dllimport.cpp index 1c79c23ca8..62ed9cc3b8 100644 --- a/test/CodeGenCXX/dllimport.cpp +++ b/test/CodeGenCXX/dllimport.cpp @@ -621,4 +621,11 @@ namespace PR19933 { template int C::x = g(); template struct __declspec(dllimport) C; // MSC-DAG: @"\01?x@?$C@H@PR19933@@2HA" = available_externally dllimport global i32 42 + + template struct D { static int x, y; }; + template int D::x = I + 1; + template int D::y = I + f(); + template struct __declspec(dllimport) D<42>; + // MSC-DAG: @"\01?x@?$D@$0CK@@PR19933@@2HA" = available_externally dllimport global i32 43 + // MSC-DAG: @"\01?y@?$D@$0CK@@PR19933@@2HA" = available_externally dllimport global i32 0 } -- 2.40.0