summaryrefslogtreecommitdiffstats
path: root/clang/lib/CodeGen/CodeGenModule.cpp
diff options
context:
space:
mode:
authorDaniel Dunbar <daniel@zuster.org>2008-08-10 20:25:57 +0000
committerDaniel Dunbar <daniel@zuster.org>2008-08-10 20:25:57 +0000
commit6dfdf8c97a79b14ecca3aba0bc1aff612cd62226 (patch)
tree77a7f034595384b4c5d93cb367c245fa669223f4 /clang/lib/CodeGen/CodeGenModule.cpp
parent4c3116437cc19d05b3031b8e99566ef47bf463e8 (diff)
downloadbcm5719-llvm-6dfdf8c97a79b14ecca3aba0bc1aff612cd62226.tar.gz
bcm5719-llvm-6dfdf8c97a79b14ecca3aba0bc1aff612cd62226.zip
Back out r54608 (inline string literals were getting an extra '\0')
temporarily, I assumed GetAddrForConstantString literal was being used consistently but it doesn't look like it is. Factored out a CodeGenModule::getStringForStringLiteral which handles extracting a std::string for the bytes of a StringLiteral, padded to match the type. Update EmitLValue to use getStringForStringLiteral, this was previously not padding strings correctly. Good thing we only emit strings in 4 different places! llvm-svn: 54621
Diffstat (limited to 'clang/lib/CodeGen/CodeGenModule.cpp')
-rw-r--r--clang/lib/CodeGen/CodeGenModule.cpp22
1 files changed, 21 insertions, 1 deletions
diff --git a/clang/lib/CodeGen/CodeGenModule.cpp b/clang/lib/CodeGen/CodeGenModule.cpp
index 2d7e41c334c..d17a6c88e41 100644
--- a/clang/lib/CodeGen/CodeGenModule.cpp
+++ b/clang/lib/CodeGen/CodeGenModule.cpp
@@ -902,12 +902,32 @@ GetAddrOfConstantCFString(const std::string &str) {
return GV;
}
+/// getStringForStringLiteral - Return the appropriate bytes for a
+/// string literal, properly padded to match the literal type.
+std::string CodeGenModule::getStringForStringLiteral(const StringLiteral *E) {
+ assert(!E->isWide() && "FIXME: Wide strings not supported yet!");
+ const char *StrData = E->getStrData();
+ unsigned Len = E->getByteLength();
+
+ const ConstantArrayType *CAT =
+ getContext().getAsConstantArrayType(E->getType());
+ assert(CAT && "String isn't pointer or array!");
+
+ // Resize the string to the right size
+ // FIXME: What about wchar_t strings?
+ std::string Str(StrData, StrData+Len);
+ uint64_t RealLen = CAT->getSize().getZExtValue();
+ Str.resize(RealLen, '\0');
+
+ return Str;
+}
+
/// GenerateWritableString -- Creates storage for a string literal.
static llvm::Constant *GenerateStringLiteral(const std::string &str,
bool constant,
CodeGenModule &CGM) {
// Create Constant for this string literal
- llvm::Constant *C = llvm::ConstantArray::get(str, false);
+ llvm::Constant *C = llvm::ConstantArray::get(str);
// Create a global variable for this string
C = new llvm::GlobalVariable(C->getType(), constant,
OpenPOWER on IntegriCloud