[cfe-commits] r91156 - in /cfe/trunk: lib/CodeGen/CodeGenModule.cpp test/CodeGenCXX/global-llvm-constant.cpp

Eli Friedman eli.friedman at gmail.com
Fri Dec 11 13:23:04 PST 2009


Author: efriedma
Date: Fri Dec 11 15:23:03 2009
New Revision: 91156

URL: http://llvm.org/viewvc/llvm-project?rev=91156&view=rev
Log:
Fix for PR5714: make sure globals that will be modified aren't marked const.


Added:
    cfe/trunk/test/CodeGenCXX/global-llvm-constant.cpp
Modified:
    cfe/trunk/lib/CodeGen/CodeGenModule.cpp

Modified: cfe/trunk/lib/CodeGen/CodeGenModule.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/CodeGen/CodeGenModule.cpp?rev=91156&r1=91155&r2=91156&view=diff

==============================================================================
--- cfe/trunk/lib/CodeGen/CodeGenModule.cpp (original)
+++ cfe/trunk/lib/CodeGen/CodeGenModule.cpp Fri Dec 11 15:23:03 2009
@@ -760,6 +760,17 @@
   return GetOrCreateLLVMFunction(Name, FTy, GlobalDecl());
 }
 
+static bool DeclIsConstantGlobal(ASTContext &Context, const VarDecl *D) {
+  if (!D->getType().isConstant(Context))
+    return false;
+  if (Context.getLangOptions().CPlusPlus &&
+      Context.getBaseElementType(D->getType())->getAs<RecordType>()) {
+    // FIXME: We should do something fancier here!
+    return false;
+  }
+  return true;
+}
+
 /// GetOrCreateLLVMGlobal - If the specified mangled name is not in the module,
 /// create and return an llvm GlobalVariable with the specified type.  If there
 /// is something in the module with the specified name, return it potentially
@@ -803,7 +814,7 @@
   if (D) {
     // FIXME: This code is overly simple and should be merged with other global
     // handling.
-    GV->setConstant(D->getType().isConstant(Context));
+    GV->setConstant(DeclIsConstantGlobal(Context, D));
 
     // FIXME: Merge with other attribute handling code.
     if (D->getStorageClass() == VarDecl::PrivateExtern)
@@ -978,11 +989,8 @@
 
   // If it is safe to mark the global 'constant', do so now.
   GV->setConstant(false);
-  if (D->getType().isConstant(Context)) {
-    // FIXME: In C++, if the variable has a non-trivial ctor/dtor or any mutable
-    // members, it cannot be declared "LLVM const".
+  if (DeclIsConstantGlobal(Context, D))
     GV->setConstant(true);
-  }
 
   GV->setAlignment(getContext().getDeclAlignInBytes(D));
 

Added: cfe/trunk/test/CodeGenCXX/global-llvm-constant.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/CodeGenCXX/global-llvm-constant.cpp?rev=91156&view=auto

==============================================================================
--- cfe/trunk/test/CodeGenCXX/global-llvm-constant.cpp (added)
+++ cfe/trunk/test/CodeGenCXX/global-llvm-constant.cpp Fri Dec 11 15:23:03 2009
@@ -0,0 +1,10 @@
+// RUN: clang-cc -emit-llvm -o - %s | FileCheck %s
+
+struct A {
+  A() { x = 10; }
+  int x;
+};
+
+const A x;
+
+// CHECK: @x = internal global





More information about the cfe-commits mailing list