]> granicus.if.org Git - clang/commitdiff
Improve checking for dynamic initializers of dllimport fields in template instantiation
authorHans Wennborg <hans@hanshq.net>
Tue, 10 Jun 2014 00:55:51 +0000 (00:55 +0000)
committerHans Wennborg <hans@hanshq.net>
Tue, 10 Jun 2014 00:55:51 +0000 (00:55 +0000)
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
test/CodeGenCXX/dllimport.cpp

index 2fa28a33a10dffc031a2419b02e5e8425eb78f24..bce2fd552e3de87b4b0cd8a06c77e8f2efa82ba8 100644 (file)
@@ -3671,12 +3671,6 @@ void Sema::InstantiateVariableInitializer(
     // We already have an initializer in the class.
     return;
 
-  if (Var->hasAttr<DLLImportAttr>() &&
-      !(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<DLLImportAttr>() && 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 {
index 1c79c23ca8156845ed747c74ef24f9fbb56481cd..62ed9cc3b8acb9d84f19d7e9a3b682db7fa49c51 100644 (file)
@@ -621,4 +621,11 @@ namespace PR19933 {
   template <typename T> int C<T>::x = g();
   template struct __declspec(dllimport) C<int>;
   // MSC-DAG: @"\01?x@?$C@H@PR19933@@2HA" = available_externally dllimport global i32 42
+
+  template <int I> struct D { static int x, y; };
+  template <int I> int D<I>::x = I + 1;
+  template <int I> int D<I>::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
 }