summaryrefslogtreecommitdiffstats
path: root/clang/CodeGen/CodeGenModule.cpp
diff options
context:
space:
mode:
authorChris Lattner <sabre@nondot.org>2007-12-10 00:00:56 +0000
committerChris Lattner <sabre@nondot.org>2007-12-10 00:00:56 +0000
commit686628e052861c16263683c4c77579153bd206d5 (patch)
tree3e2ac9cb3552b3fd3668f6f976e4aac5432ee916 /clang/CodeGen/CodeGenModule.cpp
parentc25c42f3ca3436d3b90e770d5488286043da956b (diff)
downloadbcm5719-llvm-686628e052861c16263683c4c77579153bd206d5.tar.gz
bcm5719-llvm-686628e052861c16263683c4c77579153bd206d5.zip
extend or truncate the initializer for a string initializer to match its type.
llvm-svn: 44751
Diffstat (limited to 'clang/CodeGen/CodeGenModule.cpp')
-rw-r--r--clang/CodeGen/CodeGenModule.cpp21
1 files changed, 15 insertions, 6 deletions
diff --git a/clang/CodeGen/CodeGenModule.cpp b/clang/CodeGen/CodeGenModule.cpp
index 55336510c63..afd23c45012 100644
--- a/clang/CodeGen/CodeGenModule.cpp
+++ b/clang/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);
}
}
OpenPOWER on IntegriCloud