]> granicus.if.org Git - clang/commitdiff
AST: Address of dllimport variables isn't constant
authorDavid Majnemer <david.majnemer@gmail.com>
Tue, 24 Jun 2014 05:59:13 +0000 (05:59 +0000)
committerDavid Majnemer <david.majnemer@gmail.com>
Tue, 24 Jun 2014 05:59:13 +0000 (05:59 +0000)
The address of dllimport variables isn't something that can be
meaningfully used in a constexpr context and isn't suitable for
evaluation at load-time.  They require loads from memory to properly
evaluate.

This fixes PR19955.

Differential Revision: http://reviews.llvm.org/D4250

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@211568 91177308-0d34-0410-b5e6-96231b3b80d8

lib/AST/ExprConstant.cpp
test/CodeGenCXX/PR19955.cpp [new file with mode: 0644]
test/SemaCXX/PR19955.cpp [new file with mode: 0644]

index c77d5e888a3a17798ddf0e243d8e6d6cb9798eb3..d25ecd043d1bef0a596d06888409bd952927b395 100644 (file)
@@ -4390,6 +4390,9 @@ bool LValueExprEvaluator::VisitVarDecl(const Expr *E, const VarDecl *VD) {
       Result.set(VD, Frame->Index);
       return true;
     }
+    // The address of __declspec(dllimport) variables aren't constant.
+    if (VD->hasAttr<DLLImportAttr>())
+      return ZeroInitialization(E);
     return Success(VD);
   }
 
diff --git a/test/CodeGenCXX/PR19955.cpp b/test/CodeGenCXX/PR19955.cpp
new file mode 100644 (file)
index 0000000..7d54ac3
--- /dev/null
@@ -0,0 +1,10 @@
+// RUN: %clang_cc1 -triple i686-windows-msvc -fno-rtti -emit-llvm -std=c++1y -O0 -o - %s | FileCheck %s
+// RUN: %clang_cc1 -triple x86_64-windows-msvc -fno-rtti -emit-llvm -std=c++1y -O0 -o - %s | FileCheck %s
+
+extern int __declspec(dllimport) x;
+extern long long y;
+// CHECK-DAG: @"\01?y@@3_JA" = global i64 0
+long long y = (long long)&x;
+
+// CHECK-LABEL: @"\01??__Ey@@YAXXZ"
+// CHECK-DAG: @"\01?y@@3_JA"
diff --git a/test/SemaCXX/PR19955.cpp b/test/SemaCXX/PR19955.cpp
new file mode 100644 (file)
index 0000000..81fa70d
--- /dev/null
@@ -0,0 +1,4 @@
+// RUN: %clang_cc1 -triple i686-win32 -verify -std=c++11 %s
+
+extern int __attribute__((dllimport)) y;
+constexpr int *x = &y; // expected-error {{must be initialized by a constant expression}}