zahiraam updated this revision to Diff 484840.
CHANGES SINCE LAST ACTION
https://reviews.llvm.org/D137107/new/
https://reviews.llvm.org/D137107
Files:
clang/include/clang/Basic/DiagnosticCommonKinds.td
clang/lib/AST/ExprConstant.cpp
clang/lib/CodeGen/CGDeclCXX.cpp
clang/lib/CodeGen/CGExprConstant.cpp
clang/lib/CodeGen/CodeGenFunction.h
clang/lib/CodeGen/CodeGenModule.cpp
clang/lib/CodeGen/CodeGenModule.h
clang/test/CodeGenCXX/PR19955.cpp
clang/test/CodeGenCXX/dllimport.cpp
clang/test/SemaCXX/PR19955.cpp
clang/test/SemaCXX/constant-expression-cxx11.cpp
clang/test/SemaCXX/dllimport-constexpr.cpp
Index: clang/test/SemaCXX/dllimport-constexpr.cpp
===================================================================
--- clang/test/SemaCXX/dllimport-constexpr.cpp
+++ clang/test/SemaCXX/dllimport-constexpr.cpp
@@ -40,7 +40,6 @@
// constexpr initialization doesn't work for dllimport things.
// expected-error@+1{{must be initialized by a constant expression}}
constexpr void (*constexpr_import_func)() = &imported_func;
-// expected-error@+1{{must be initialized by a constant expression}}
constexpr int *constexpr_import_int = &imported_int;
// expected-error@+1{{must be initialized by a constant expression}}
constexpr void (Foo::*constexpr_memptr)() = &Foo::imported_method;
@@ -60,3 +59,11 @@
// expected-note@+1 {{requested here}}
StaticConstexpr<imported_func>::g_fp();
}
+
+extern int __declspec(dllimport) val;
+constexpr int& val_ref = val;
+
+void assigndllimporttoconst () {
+ extern int _declspec(dllimport) val;
+ constexpr int& val_ref = val;
+}
Index: clang/test/SemaCXX/constant-expression-cxx11.cpp
===================================================================
--- clang/test/SemaCXX/constant-expression-cxx11.cpp
+++ clang/test/SemaCXX/constant-expression-cxx11.cpp
@@ -1595,7 +1595,7 @@
void f(int k) { // expected-note {{here}}
int arr[k]; // expected-warning {{C99}} expected-note {{function parameter 'k'}}
constexpr int n = 1 +
- sizeof(arr) // expected-error {{constant expression}}
+ sizeof(arr) // expected-error{{constexpr variable 'n' must be initialized by a constant expression}}
* 3;
}
}
Index: clang/test/SemaCXX/PR19955.cpp
===================================================================
--- clang/test/SemaCXX/PR19955.cpp
+++ clang/test/SemaCXX/PR19955.cpp
@@ -2,7 +2,7 @@
// RUN: %clang_cc1 -triple i686-mingw32 -verify -std=c++11 %s
extern int __attribute__((dllimport)) var;
-constexpr int *varp = &var; // expected-error {{must be initialized by a constant expression}}
+constexpr int *varp = &var;
extern __attribute__((dllimport)) void fun();
constexpr void (*funp)(void) = &fun; // expected-error {{must be initialized by a constant expression}}
Index: clang/test/CodeGenCXX/dllimport.cpp
===================================================================
--- clang/test/CodeGenCXX/dllimport.cpp
+++ clang/test/CodeGenCXX/dllimport.cpp
@@ -1,5 +1,5 @@
-// RUN: %clang_cc1 -no-enable-noundef-analysis -triple i686-windows-msvc -fno-rtti -fno-threadsafe-statics -fms-extensions -emit-llvm -std=c++1y -O0 -o - %s -DMSABI -w | FileCheck --check-prefix=MSC --check-prefix=M32 %s
-// RUN: %clang_cc1 -no-enable-noundef-analysis -triple x86_64-windows-msvc -fno-rtti -fno-threadsafe-statics -fms-extensions -emit-llvm -std=c++1y -O0 -o - %s -DMSABI -w | FileCheck --check-prefix=MSC --check-prefix=M64 %s
+// RUN: %clang_cc1 -no-enable-noundef-analysis -triple i686-windows-msvc -fno-rtti -fno-threadsafe-statics -fms-extensions -emit-llvm -std=c++1y -O0 -o - %s -DMSABI -w | FileCheck --check-prefix=MSC --check-prefix=M32 --check-prefix=GL32 %s
+// RUN: %clang_cc1 -no-enable-noundef-analysis -triple x86_64-windows-msvc -fno-rtti -fno-threadsafe-statics -fms-extensions -emit-llvm -std=c++1y -O0 -o - %s -DMSABI -w | FileCheck --check-prefix=MSC --check-prefix=M64 --check-prefix=GL64 %s
// RUN: %clang_cc1 -no-enable-noundef-analysis -triple i686-windows-gnu -fno-rtti -fno-threadsafe-statics -fms-extensions -emit-llvm -std=c++1y -O0 -o - %s -w | FileCheck --check-prefix=GNU --check-prefix=G32 %s
// RUN: %clang_cc1 -no-enable-noundef-analysis -triple x86_64-windows-gnu -fno-rtti -fno-threadsafe-statics -fms-extensions -emit-llvm -std=c++1y -O0 -o - %s -w | FileCheck --check-prefix=GNU %s
// RUN: %clang_cc1 -no-enable-noundef-analysis -triple i686-windows-msvc -fno-rtti -fno-threadsafe-statics -fms-extensions -fms-compatibility-version=18.00 -emit-llvm -std=c++1y -O1 -disable-llvm-passes -o - %s -DMSABI -w | FileCheck --check-prefix=MO1 --check-prefix=M18 %s
@@ -737,8 +737,6 @@
namespace PR19933 {
// Don't dynamically initialize dllimport vars.
-// MSC2-NOT: @llvm.global_ctors
-// GNU2-NOT: @llvm.global_ctors
struct NonPOD { NonPOD(); };
template <typename T> struct A { static NonPOD x; };
@@ -856,6 +854,12 @@
USEMEMFUNC(PR23770BaseTemplate<int>, f);
// M32-DAG: declare dllimport x86_thiscallcc void @"?f@?$PR23770BaseTemplate@H@@QAEXXZ"
+// MSC-DAG: @"?val@@3HA" = external dllimport global i32
+// GL32-DAG: @"?x@@3PAHA" = dso_local global ptr null
+// GL64-DAG: @"?x@@3PEAHEA" = dso_local global ptr null
+// MSC-DAG: @"?impx@@3HA" = external dllimport global i32
+// MSC-DAG: @llvm.global_ctors = appending global [1 x { i32, ptr, ptr }] [{ i32, ptr, ptr } { i32 201, ptr @__ctor, ptr null }]
+
namespace PR27810 {
template <class T>
struct basic_ostream {
@@ -1026,3 +1030,15 @@
void baz() { U<int> u; u.foo(); } // No diagnostic.
}
+
+void assigndllimporttoconst () {
+ // MSC-DAG: define dso_local void @"?assigndllimporttoconst@@YAXXZ"()
+ // MSC-DAG: %[[VAL_REF:.*]] = alloca ptr
+ // MSC-DAG-NEXT: store ptr @"?val@@3HA", ptr %[[VAL_REF]]
+ extern int _declspec(dllimport) val;
+ constexpr int& val_ref = val;
+}
+
+_declspec(dllimport) int impx;
+constexpr int *y = &impx;
+int *x = &impx;
Index: clang/test/CodeGenCXX/PR19955.cpp
===================================================================
--- clang/test/CodeGenCXX/PR19955.cpp
+++ clang/test/CodeGenCXX/PR19955.cpp
@@ -1,27 +1,34 @@
// RUN: %clang_cc1 -triple i686-windows-msvc -fms-extensions -fno-rtti -emit-llvm -std=c++1y -O0 -o - %s | FileCheck %s
-// RUN: %clang_cc1 -triple x86_64-windows-msvc -fms-extensions -fno-rtti -emit-llvm -std=c++1y -O0 -o - %s | FileCheck %s --check-prefix X64
+// RUN: %clang_cc1 -triple x86_64-windows-msvc -fms-extensions -fno-rtti -emit-llvm -std=c++1y -O0 -o - %s | FileCheck %s --check-prefix=CHECK-X64
extern int __declspec(dllimport) var;
extern void __declspec(dllimport) fun();
extern int *varp;
int *varp = &var;
-// CHECK-DAG: @"?varp@@3PAHA" = dso_local global ptr null
-// X64-DAG: @"?varp@@3PEAHEA" = dso_local global ptr null
+// CHECK: @"?varp@@3PAHA" = dso_local global ptr null
+// CHECK-X64: @"?varp@@3PEAHEA" = dso_local global ptr null
+// CHECK: @"?var@@3HA" = external dllimport global i32
+// CHECK: @"?funp@@3P6AXXZA" = dso_local global ptr null
-extern void (*funp)();
-void (*funp)() = &fun;
-// CHECK-DAG: @"?funp@@3P6AXXZA" = dso_local global ptr null
-// X64-DAG: @"?funp@@3P6AXXZEA" = dso_local global ptr null
-
-// CHECK-LABEL: @"??__Evarp@@YAXXZ"
-// CHECK-DAG: store ptr @"?var@@3HA", ptr @"?varp@@3PAHA"
+// CHECK-X64: @"?funp@@3P6AXXZEA" = dso_local global ptr null
+// CHECK: @llvm.global_ctors = appending global [2 x { i32, ptr, ptr }] [{ i32, ptr, ptr } { i32 201, ptr @__ctor, ptr null }, { i32, ptr, ptr } { i32 65535, ptr @_GLOBAL__sub_I_PR19955.cpp, ptr null }]
-// X64-LABEL: @"??__Evarp@@YAXXZ"
-// X64-DAG: store ptr @"?var@@3HA", ptr @"?varp@@3PEAHEA"
+// CHECK-LABEL: define internal void @__ctor()
+// CHECK: entry
+// CHECK: store ptr @"?var@@3HA", ptr @"?varp@@3PAHA"
+// CHECK-X64: store ptr @"?var@@3HA", ptr @"?varp@@3PEAHEA"
+// CHECK: ret void
-// CHECK-LABEL: @"??__Efunp@@YAXXZ"()
-// CHECK-DAG: store ptr @"?fun@@YAXXZ", ptr @"?funp@@3P6AXXZA"
+extern void (*funp)();
+void (*funp)() = &fun;
+// CHECK-LABEL: define internal void @"??__Efunp@@YAXXZ"
+// CHECK: entry
+// CHECK: store ptr @"?fun@@YAXXZ", ptr @"?funp@@3P6AXXZA"
+// CHECK-X64: store ptr @"?fun@@YAXXZ", ptr @"?funp@@3P6AXXZEA"
+// CHECK: ret void
-// X64-LABEL: @"??__Efunp@@YAXXZ"()
-// X64-DAG: store ptr @"?fun@@YAXXZ", ptr @"?funp@@3P6AXXZEA"
+// CHECK-LABEL: define internal void @_GLOBAL__sub_I_PR19955.cpp()
+// CHECK: entry:
+// CHECK: call void @"??__Efunp@@YAXXZ"()
+// CHECK: ret void
Index: clang/lib/CodeGen/CodeGenModule.h
===================================================================
--- clang/lib/CodeGen/CodeGenModule.h
+++ clang/lib/CodeGen/CodeGenModule.h
@@ -1623,6 +1623,9 @@
void EmitCXXGlobalVarDeclInitFunc(const VarDecl *D,
llvm::GlobalVariable *Addr,
bool PerformInit);
+ void EmitCXXCtorInit(const VarDecl *D, llvm::GlobalVariable *Addr,
+ bool PerformInit, int Priority,
+ llvm::StringLiteral FnName, bool PerformDtor);
void EmitPointerToInitFunc(const VarDecl *VD, llvm::GlobalVariable *Addr,
llvm::Function *InitFunc, InitSegAttr *ISA);
Index: clang/lib/CodeGen/CodeGenModule.cpp
===================================================================
--- clang/lib/CodeGen/CodeGenModule.cpp
+++ clang/lib/CodeGen/CodeGenModule.cpp
@@ -4756,6 +4756,10 @@
GO.setComdat(TheModule.getOrInsertComdat(GO.getName()));
}
+bool isStaticInit(const VarDecl *D, const LangOptions Opts) {
+ return Opts.CPlusPlus && D->hasConstantInitialization();
+}
+
/// Pass IsTentative as true if you want to create a tentative definition.
void CodeGenModule::EmitGlobalVarDefinition(const VarDecl *D,
bool IsTentative) {
@@ -4992,9 +4996,17 @@
maybeSetTrivialComdat(*D, *GV);
// Emit the initializer function if necessary.
- if (NeedsGlobalCtor || NeedsGlobalDtor)
- EmitCXXGlobalVarDeclInitFunc(D, GV, NeedsGlobalCtor);
+ if (isStaticInit(D, getLangOpts()) && NeedsGlobalCtor) {
+ EmitCXXCtorInit(D, GV, true, 201, llvm::StringLiteral("__ctor"), false);
+ if (NeedsGlobalDtor)
+ EmitCXXCtorInit(D, GV, false, 65535, llvm::StringLiteral("__dtor"), true);
+ if (NeedsGlobalCtor || NeedsGlobalDtor)
+ DelayedCXXInitPosition[D] = ~0U;
+ } else {
+ if (NeedsGlobalCtor || NeedsGlobalDtor)
+ EmitCXXGlobalVarDeclInitFunc(D, GV, NeedsGlobalCtor);
+ }
SanitizerMD->reportGlobal(GV, *D, NeedsGlobalCtor);
// Emit global variable debug information.
Index: clang/lib/CodeGen/CodeGenFunction.h
===================================================================
--- clang/lib/CodeGen/CodeGenFunction.h
+++ clang/lib/CodeGen/CodeGenFunction.h
@@ -4441,7 +4441,7 @@
/// EmitCXXGlobalVarDeclInit - Create the initializer for a C++
/// variable with global storage.
void EmitCXXGlobalVarDeclInit(const VarDecl &D, llvm::GlobalVariable *GV,
- bool PerformInit);
+ bool PerformInit, bool PerformDtor = true);
llvm::Function *createAtExitStub(const VarDecl &VD, llvm::FunctionCallee Dtor,
llvm::Constant *Addr);
@@ -4493,10 +4493,10 @@
llvm::Constant *>>
DtorsOrStermFinalizers);
- void GenerateCXXGlobalVarDeclInitFunc(llvm::Function *Fn,
- const VarDecl *D,
+ void GenerateCXXGlobalVarDeclInitFunc(llvm::Function *Fn, const VarDecl *D,
llvm::GlobalVariable *Addr,
- bool PerformInit);
+ bool PerformInit,
+ bool PerformDtor = true);
void EmitCXXConstructExpr(const CXXConstructExpr *E, AggValueSlot Dest);
Index: clang/lib/CodeGen/CGExprConstant.cpp
===================================================================
--- clang/lib/CodeGen/CGExprConstant.cpp
+++ clang/lib/CodeGen/CGExprConstant.cpp
@@ -1920,8 +1920,14 @@
if (auto VD = dyn_cast<VarDecl>(D)) {
// We can never refer to a variable with local storage.
if (!VD->hasLocalStorage()) {
- if (VD->isFileVarDecl() || VD->hasExternalStorage())
+ if (VD->isFileVarDecl() || VD->hasExternalStorage()) {
+ if (VD->hasAttr<DLLImportAttr>()) {
+ DiagnosticsEngine &Diags = CGM.getContext().getDiagnostics();
+ Diags.Report(diag::warn_for_global_ctor_for_dllimport) << D;
+ return nullptr;
+ }
return CGM.GetAddrOfGlobalVar(VD);
+ }
if (VD->isLocalVarDecl()) {
return CGM.getOrCreateStaticVarDecl(
Index: clang/lib/CodeGen/CGDeclCXX.cpp
===================================================================
--- clang/lib/CodeGen/CGDeclCXX.cpp
+++ clang/lib/CodeGen/CGDeclCXX.cpp
@@ -175,7 +175,8 @@
void CodeGenFunction::EmitCXXGlobalVarDeclInit(const VarDecl &D,
llvm::GlobalVariable *GV,
- bool PerformInit) {
+ bool PerformInit,
+ bool PerformDtor) {
const Expr *Init = D.getInit();
QualType T = D.getType();
@@ -218,7 +219,7 @@
EmitDeclInit(*this, D, DeclAddr);
if (CGM.isTypeConstant(D.getType(), true))
EmitDeclInvariant(*this, D, DeclPtr);
- else
+ else if (PerformDtor)
EmitDeclDestroy(*this, D, DeclAddr);
return;
}
@@ -504,6 +505,23 @@
PtrArray->setComdat(C);
}
+void CodeGenModule::EmitCXXCtorInit(const VarDecl *D,
+ llvm::GlobalVariable *Addr,
+ bool PerformInit, int Priority,
+ llvm::StringLiteral FuncName,
+ bool PerformDtor) {
+ auto I = DelayedCXXInitPosition.find(D);
+ if (I != DelayedCXXInitPosition.end() && I->second == ~0U)
+ return;
+
+ llvm::FunctionType *FTy = llvm::FunctionType::get(VoidTy, false);
+ llvm::Function *Fn = CreateGlobalInitOrCleanUpFunction(
+ FTy, FuncName.str(), getTypes().arrangeNullaryFunction());
+ CodeGenFunction(*this).GenerateCXXGlobalVarDeclInitFunc(
+ Fn, D, Addr, PerformInit, PerformDtor);
+ AddGlobalCtor(Fn, Priority);
+}
+
void
CodeGenModule::EmitCXXGlobalVarDeclInitFunc(const VarDecl *D,
llvm::GlobalVariable *Addr,
@@ -966,10 +984,9 @@
}
/// Emit the code necessary to initialize the given global variable.
-void CodeGenFunction::GenerateCXXGlobalVarDeclInitFunc(llvm::Function *Fn,
- const VarDecl *D,
- llvm::GlobalVariable *Addr,
- bool PerformInit) {
+void CodeGenFunction::GenerateCXXGlobalVarDeclInitFunc(
+ llvm::Function *Fn, const VarDecl *D, llvm::GlobalVariable *Addr,
+ bool PerformInit, bool PerformDtor) {
// Check if we need to emit debug info for variable initializer.
if (D->hasAttr<NoDebugAttr>())
DebugInfo = nullptr; // disable debug info indefinitely for this function
@@ -994,7 +1011,7 @@
isTemplateInstantiation(D->getTemplateSpecializationKind()))) {
EmitCXXGuardedInit(*D, Addr, PerformInit);
} else {
- EmitCXXGlobalVarDeclInit(*D, Addr, PerformInit);
+ EmitCXXGlobalVarDeclInit(*D, Addr, PerformInit, PerformDtor);
}
if (getLangOpts().HLSL)
Index: clang/lib/AST/ExprConstant.cpp
===================================================================
--- clang/lib/AST/ExprConstant.cpp
+++ clang/lib/AST/ExprConstant.cpp
@@ -2228,11 +2228,13 @@
// FIXME: Diagnostic!
return false;
- // A dllimport variable never acts like a constant, unless we're
- // evaluating a value for use only in name mangling.
- if (!isForManglingOnly(Kind) && Var->hasAttr<DLLImportAttr>())
- // FIXME: Diagnostic!
- return false;
+ // In C mode, a dllimport variable never acts like a constant, unless
+ // we're evaluating a value for use only in name mangling.
+
+ if (!Info.getCtx().getLangOpts().CPlusPlus)
+ if (!isForManglingOnly(Kind) && Var->hasAttr<DLLImportAttr>())
+ // FIXME: Diagnostic!
+ return false;
// In CUDA/HIP device compilation, only device side variables have
// constant addresses.
Index: clang/include/clang/Basic/DiagnosticCommonKinds.td
===================================================================
--- clang/include/clang/Basic/DiagnosticCommonKinds.td
+++ clang/include/clang/Basic/DiagnosticCommonKinds.td
@@ -251,6 +251,10 @@
def err_unimplemented_conversion_with_fixed_point_type : Error<
"conversion between fixed point and %0 is not yet supported">;
+def warn_for_global_ctor_for_dllimport : Warning<
+ "global constructor is generated for dllimport global var %0">,
+ InGroup<GlobalConstructors>;
+
// SEH
def err_seh_expected_handler : Error<
"expected '__except' or '__finally' block">;
_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits