]> granicus.if.org Git - clang/commitdiff
extend or truncate the initializer for a string initializer to match its type.
authorChris Lattner <sabre@nondot.org>
Mon, 10 Dec 2007 00:00:56 +0000 (00:00 +0000)
committerChris Lattner <sabre@nondot.org>
Mon, 10 Dec 2007 00:00:56 +0000 (00:00 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@44751 91177308-0d34-0410-b5e6-96231b3b80d8

CodeGen/CodeGenModule.cpp
test/CodeGen/globalinit.c

index 55336510c637b9f14de5066b58f3cc815bdefab8..afd23c45012ee7610349b6996b1930fb5ca13358 100644 (file)
@@ -419,12 +419,21 @@ static llvm::Constant *GenerateConstantExpr(const Expr *Expression,
         // 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);
         }
       }
     
index 996d2acabb52622f257ef1c298b6697412b685c2..f47fd5c457754d9ab20aacf9bf7641c4fd7ba5ab 100644 (file)
@@ -16,5 +16,6 @@ int latin_ptr2len (char *p);
 int (*mb_ptr2len) (char *p) = latin_ptr2len;
 
 
-char string[8] = "string";
+char string[8] = "string";   // extend init
+char string2[4] = "string";  // truncate init