diff options
-rw-r--r-- | CodeGen/CodeGenModule.cpp | 21 | ||||
-rw-r--r-- | test/CodeGen/globalinit.c | 3 |
2 files changed, 17 insertions, 7 deletions
diff --git a/CodeGen/CodeGenModule.cpp b/CodeGen/CodeGenModule.cpp index 55336510c6..afd23c4501 100644 --- a/CodeGen/CodeGenModule.cpp +++ b/CodeGen/CodeGenModule.cpp @@ -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); } } diff --git a/test/CodeGen/globalinit.c b/test/CodeGen/globalinit.c index 996d2acabb..f47fd5c457 100644 --- a/test/CodeGen/globalinit.c +++ b/test/CodeGen/globalinit.c @@ -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 |