From: Douglas Gregor Date: Sat, 7 May 2011 22:06:45 +0000 (+0000) Subject: In C++, allow us to emit a global as 'constant' even if it has class X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=da55074866ca2fe7f718c5d3334648d6e340fb15;p=clang In C++, allow us to emit a global as 'constant' even if it has class type, so long as it is known to have a constant initializer and the class type is a POD class. Fixes . git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@131060 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/lib/CodeGen/CodeGenModule.cpp b/lib/CodeGen/CodeGenModule.cpp index 2ccd6a782c..d99230b556 100644 --- a/lib/CodeGen/CodeGenModule.cpp +++ b/lib/CodeGen/CodeGenModule.cpp @@ -938,14 +938,17 @@ CodeGenModule::CreateRuntimeFunction(const llvm::FunctionType *FTy, return GetOrCreateLLVMFunction(Name, FTy, GlobalDecl(), /*ForVTable=*/false); } -static bool DeclIsConstantGlobal(ASTContext &Context, const VarDecl *D) { +static bool DeclIsConstantGlobal(ASTContext &Context, const VarDecl *D, + bool ConstantInit) { if (!D->getType().isConstant(Context) && !D->getType()->isReferenceType()) return false; - if (Context.getLangOptions().CPlusPlus && - Context.getBaseElementType(D->getType())->getAs()) { - // FIXME: We should do something fancier here! - return false; + + if (Context.getLangOptions().CPlusPlus) { + if (const RecordType *Record + = Context.getBaseElementType(D->getType())->getAs()) + return ConstantInit && cast(Record->getDecl())->isPOD(); } + return true; } @@ -1002,7 +1005,7 @@ CodeGenModule::GetOrCreateLLVMGlobal(llvm::StringRef MangledName, if (D) { // FIXME: This code is overly simple and should be merged with other global // handling. - GV->setConstant(DeclIsConstantGlobal(Context, D)); + GV->setConstant(DeclIsConstantGlobal(Context, D, false)); // Set linkage and visibility in case we never see a definition. NamedDecl::LinkageInfo LV = D->getLinkageAndVisibility(); @@ -1284,7 +1287,7 @@ void CodeGenModule::EmitGlobalVarDefinition(const VarDecl *D) { // If it is safe to mark the global 'constant', do so now. GV->setConstant(false); - if (!NonConstInit && DeclIsConstantGlobal(Context, D)) + if (!NonConstInit && DeclIsConstantGlobal(Context, D, true)) GV->setConstant(true); GV->setAlignment(getContext().getDeclAlign(D).getQuantity()); diff --git a/test/CodeGenCXX/global-llvm-constant.cpp b/test/CodeGenCXX/global-llvm-constant.cpp index ef1dcf0c59..b23337d634 100644 --- a/test/CodeGenCXX/global-llvm-constant.cpp +++ b/test/CodeGenCXX/global-llvm-constant.cpp @@ -8,3 +8,13 @@ struct A { const A x; // CHECK: @_ZL1x = internal global + +struct X { + int (*fp)(int, int); +}; + +int add(int x, int y) { return x + y; } + +// CHECK: @x2 = constant +extern const X x2; +const X x2 = { &add };