[cfe-commits] r44834 - in /cfe/trunk: CodeGen/CodeGenModule.cpp test/CodeGen/globalinit.c
Chris Lattner
sabre at nondot.org
Mon Dec 10 17:38:46 PST 2007
Author: lattner
Date: Mon Dec 10 19:38:45 2007
New Revision: 44834
URL: http://llvm.org/viewvc/llvm-project?rev=44834&view=rev
Log:
Reimplement support for strings that initialize global inits now that
the types are right in sema. Thanks Steve.
Modified:
cfe/trunk/CodeGen/CodeGenModule.cpp
cfe/trunk/test/CodeGen/globalinit.c
Modified: cfe/trunk/CodeGen/CodeGenModule.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/CodeGen/CodeGenModule.cpp?rev=44834&r1=44833&r2=44834&view=diff
==============================================================================
--- cfe/trunk/CodeGen/CodeGenModule.cpp (original)
+++ cfe/trunk/CodeGen/CodeGenModule.cpp Mon Dec 10 19:38:45 2007
@@ -362,10 +362,32 @@
// Generate constant for string literal values.
case Stmt::StringLiteralClass: {
- const StringLiteral *SLiteral = cast<StringLiteral>(Expression);
- const char *StrData = SLiteral->getStrData();
- unsigned Len = SLiteral->getByteLength();
- return CGM.GetAddrOfConstantString(std::string(StrData, StrData + Len));
+ const StringLiteral *String = cast<StringLiteral>(Expression);
+ const char *StrData = String->getStrData();
+ unsigned Len = String->getByteLength();
+
+ // If the string has a pointer type, emit it as a global and use the pointer
+ // to the global as its value.
+ if (String->getType()->isPointerType())
+ return CGM.GetAddrOfConstantString(std::string(StrData, StrData + Len));
+
+ // 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 = String->getType()->getAsConstantArrayType();
+ 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);
}
// Elide parenthesis.
@@ -414,36 +436,6 @@
return llvm::ConstantExpr::getBitCast(C, DestPTy);
}
- // If this is an implicit cast of a string literal to an array type, this
- // must be a string initializing an array. Don't emit it as the address of
- // the string, emit the string data itself as an inline array.
- if (const StringLiteral *String =
- dyn_cast<StringLiteral>(ICExpr->getSubExpr()))
- if (const ArrayType *AT = ICExpr->getType()->getAsArrayType()) {
- // Verify that this is an array of char or wchar. Array of const char*
- // can be initialized with a string literal, which does not expand the
- // characters inline.
- // FIXME: What about wchar_t??
- if (AT->getElementType()->isCharType()) {
- const char *StrData = String->getStrData();
- 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);
- }
- }
-
return GenerateConstantCast(ICExpr->getSubExpr(), type, CGM);
}
Modified: cfe/trunk/test/CodeGen/globalinit.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/CodeGen/globalinit.c?rev=44834&r1=44833&r2=44834&view=diff
==============================================================================
--- cfe/trunk/test/CodeGen/globalinit.c (original)
+++ cfe/trunk/test/CodeGen/globalinit.c Mon Dec 10 19:38:45 2007
@@ -16,6 +16,6 @@
int (*mb_ptr2len) (char *p) = latin_ptr2len;
-//char string[8] = "string"; // extend init
-//char string2[4] = "string"; // truncate init
+char string[8] = "string"; // extend init
+char string2[4] = "string"; // truncate init
More information about the cfe-commits
mailing list