From: David Majnemer Date: Tue, 24 Jun 2014 05:59:13 +0000 (+0000) Subject: AST: Address of dllimport variables isn't constant X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=99ea974909aecf110b60b2d273f7cec97ebc0f11;p=clang AST: Address of dllimport variables isn't constant 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 --- diff --git a/lib/AST/ExprConstant.cpp b/lib/AST/ExprConstant.cpp index c77d5e888a..d25ecd043d 100644 --- a/lib/AST/ExprConstant.cpp +++ b/lib/AST/ExprConstant.cpp @@ -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()) + return ZeroInitialization(E); return Success(VD); } diff --git a/test/CodeGenCXX/PR19955.cpp b/test/CodeGenCXX/PR19955.cpp new file mode 100644 index 0000000000..7d54ac3899 --- /dev/null +++ b/test/CodeGenCXX/PR19955.cpp @@ -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 index 0000000000..81fa70d7f5 --- /dev/null +++ b/test/SemaCXX/PR19955.cpp @@ -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}}