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!
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@54621 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/lib/CodeGen/CGExprConstant.cpp b/lib/CodeGen/CGExprConstant.cpp
index 0951532..db8003e 100644
--- a/lib/CodeGen/CGExprConstant.cpp
+++ b/lib/CodeGen/CGExprConstant.cpp
@@ -354,28 +354,12 @@
}
llvm::Constant *VisitStringLiteral(StringLiteral *E) {
- const char *StrData = E->getStrData();
- unsigned Len = E->getByteLength();
assert(!E->getType()->isPointerType() && "Strings are always arrays");
// Otherwise this must be a string initializing an array in a static
// initializer. Don't emit it as the address of the string, emit the string
// data itself as an inline array.
- const ConstantArrayType *CAT =
- CGM.getContext().getAsConstantArrayType(E->getType());
- assert(CAT && "String isn't pointer or array!");
-
- std::string Str(StrData, StrData + Len);
- // Null terminate the string before potentially truncating it.
- // FIXME: What about wchar_t strings?
- Str.push_back(0);
-
- 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);
+ return llvm::ConstantArray::get(CGM.getStringForStringLiteral(E), false);
}
llvm::Constant *VisitDeclRefExpr(DeclRefExpr *E) {
@@ -775,12 +759,8 @@
return llvm::ConstantExpr::getGetElementPtr(Base, &Index, 1);
}
case Expr::StringLiteralClass: {
- StringLiteral *String = cast<StringLiteral>(E);
- assert(!String->isWide() && "Cannot codegen wide strings yet");
- const char *StrData = String->getStrData();
- unsigned Len = String->getByteLength();
-
- return CGM.GetAddrOfConstantString(std::string(StrData, StrData + Len));
+ StringLiteral *S = cast<StringLiteral>(E);
+ return CGM.GetAddrOfConstantString(CGM.getStringForStringLiteral(S));
}
case Expr::UnaryOperatorClass: {
UnaryOperator *Exp = cast<UnaryOperator>(E);