diff options
author | Chris Lattner <sabre@nondot.org> | 2007-12-10 00:00:56 +0000 |
---|---|---|
committer | Chris Lattner <sabre@nondot.org> | 2007-12-10 00:00:56 +0000 |
commit | 5ccc7011e6b4a4151598a2608a6acd56dbd14b9c (patch) | |
tree | 88cd1df70390e0b78c7f4dcd09a8e0cdf741f26a /CodeGen/CodeGenModule.cpp | |
parent | a44cf73c741ebc7312f90f7b989d96985a4f354f (diff) |
extend or truncate the initializer for a string initializer to match its type.
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@44751 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'CodeGen/CodeGenModule.cpp')
-rw-r--r-- | CodeGen/CodeGenModule.cpp | 21 |
1 files changed, 15 insertions, 6 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); } } |