[cfe-commits] r61621 - in /cfe/trunk: lib/CodeGen/CodeGenModule.cpp lib/CodeGen/CodeGenModule.h test/CodeGen/static-order.c

Anders Carlsson andersca at mac.com
Sat Jan 3 18:08:04 PST 2009


Author: andersca
Date: Sat Jan  3 20:08:04 2009
New Revision: 61621

URL: http://llvm.org/viewvc/llvm-project?rev=61621&view=rev
Log:
Fix the bug that would cause Python to crash at startup.

When emitting the static variables we need to make sure that the order is preserved. 
Fix this by making StaticDecls a std::list which has O(1) random removal.

Added:
    cfe/trunk/test/CodeGen/static-order.c
Modified:
    cfe/trunk/lib/CodeGen/CodeGenModule.cpp
    cfe/trunk/lib/CodeGen/CodeGenModule.h

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

==============================================================================
--- cfe/trunk/lib/CodeGen/CodeGenModule.cpp (original)
+++ cfe/trunk/lib/CodeGen/CodeGenModule.cpp Sat Jan  3 20:08:04 2009
@@ -361,26 +361,27 @@
   bool Changed;
   do {
     Changed = false;
-    for (unsigned i = 0, e = StaticDecls.size(); i != e; ++i) {
-      const ValueDecl *D = StaticDecls[i];
-
+    
+    for (std::list<const ValueDecl*>::iterator i = StaticDecls.begin(),
+         e = StaticDecls.end(); i != e; ) {
+      const ValueDecl *D = *i;
+      
       // Check if we have used a decl with the same name
       // FIXME: The AST should have some sort of aggregate decls or
       // global symbol map.
       // FIXME: This is missing some important cases. For example, we
       // need to check for uses in an alias and in a constructor.
-      if (!GlobalDeclMap.count(D->getIdentifier()))
+      if (!GlobalDeclMap.count(D->getIdentifier())) {
+        i++;
         continue;
-
+      }
+      
       // Emit the definition.
       EmitGlobalDefinition(D);
 
       // Erase the used decl from the list.
-      StaticDecls[i] = StaticDecls.back();
-      StaticDecls.pop_back();
-      --i;
-      --e;
-      
+      i = StaticDecls.erase(i);
+
       // Remember that we made a change.
       Changed = true;
     }

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

==============================================================================
--- cfe/trunk/lib/CodeGen/CodeGenModule.h (original)
+++ cfe/trunk/lib/CodeGen/CodeGenModule.h Sat Jan  3 20:08:04 2009
@@ -21,6 +21,8 @@
 
 #include "CGCall.h"
 
+#include <list>
+
 namespace llvm {
   class Module;
   class Constant;
@@ -97,7 +99,7 @@
   /// will lazily emit definitions for only the decls that were
   /// actually used.  This should contain only Function and Var decls,
   /// and only those which actually define something.
-  std::vector<const ValueDecl*> StaticDecls;
+  std::list<const ValueDecl*> StaticDecls;
   
   /// GlobalCtors - Store the list of global constructors and their
   /// respective priorities to be emitted when the translation unit is

Added: cfe/trunk/test/CodeGen/static-order.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/CodeGen/static-order.c?rev=61621&view=auto

==============================================================================
--- cfe/trunk/test/CodeGen/static-order.c (added)
+++ cfe/trunk/test/CodeGen/static-order.c Sat Jan  3 20:08:04 2009
@@ -0,0 +1,19 @@
+// RUN: clang -emit-llvm -o - %s | not grep "zeroinitializer"
+
+struct s {
+    int a;
+};
+
+static void *v;
+
+static struct s a;
+
+static struct s a = {
+    10
+};
+
+void *f()
+{
+  if (a.a)
+    return v;
+}





More information about the cfe-commits mailing list