[cfe-commits] r44751 - in /cfe/trunk: CodeGen/CodeGenModule.cpp test/CodeGen/globalinit.c

Chris Lattner sabre at nondot.org
Sun Dec 9 16:00:56 PST 2007


Author: lattner
Date: Sun Dec  9 18:00:56 2007
New Revision: 44751

URL: http://llvm.org/viewvc/llvm-project?rev=44751&view=rev
Log:
extend or truncate the initializer for a string initializer to match its type.

Modified:
    cfe/trunk/CodeGen/CodeGenModule.cpp
    cfe/trunk/test/CodeGen/globalinit.c

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

==============================================================================
--- cfe/trunk/CodeGen/CodeGenModule.cpp (original)
+++ cfe/trunk/CodeGen/CodeGenModule.cpp Sun Dec  9 18:00:56 2007
@@ -419,12 +419,21 @@
         // FIXME: What about wchar_t??
         if (AT->getElementType()->isCharType()) {
           const char *StrData = String->getStrData();
-          unsigned Len = String->getByteLength();
-          llvm::Constant *C =
-            llvm::ConstantArray::get(std::string(StrData, StrData + Len));
-          // FIXME: This should return a string of the proper type: this
-          // mishandles things like 'char x[4] = "1234567";
-          return C;
+          std::string Str(StrData, StrData + String->getByteLength());
+          // Null terminate the string before potentially truncating it.
+          Str.push_back(0);
+
+          // FIXME: The size of the cast is not always specified yet, fix this
+          // in sema.
+          if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT)) {
+            uint64_t RealLen = CAT->getSize().getZExtValue();
+            // String or grow the initializer to the required size.
+            if (RealLen != Str.size())
+              Str.resize(RealLen);
+          }
+          
+          
+          return llvm::ConstantArray::get(Str, false);
         }
       }
     

Modified: cfe/trunk/test/CodeGen/globalinit.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/CodeGen/globalinit.c?rev=44751&r1=44750&r2=44751&view=diff

==============================================================================
--- cfe/trunk/test/CodeGen/globalinit.c (original)
+++ cfe/trunk/test/CodeGen/globalinit.c Sun Dec  9 18:00:56 2007
@@ -16,5 +16,6 @@
 int (*mb_ptr2len) (char *p) = latin_ptr2len;
 
 
-char string[8] = "string";
+char string[8] = "string";   // extend init
+char string2[4] = "string";  // truncate init
 





More information about the cfe-commits mailing list