[cfe-commits] r44750 - in /cfe/trunk: CodeGen/CodeGenModule.cpp test/CodeGen/globalinit.c

Chris Lattner sabre at nondot.org
Sun Dec 9 15:49:42 PST 2007


Author: lattner
Date: Sun Dec  9 17:49:42 2007
New Revision: 44750

URL: http://llvm.org/viewvc/llvm-project?rev=44750&view=rev
Log:
Implement codegen support for:
char text[8] = "string";

Big fixme remains.


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=44750&r1=44749&r2=44750&view=diff

==============================================================================
--- cfe/trunk/CodeGen/CodeGenModule.cpp (original)
+++ cfe/trunk/CodeGen/CodeGenModule.cpp Sun Dec  9 17:49:42 2007
@@ -407,6 +407,27 @@
       return llvm::ConstantExpr::getGetElementPtr(C, Ops, 2);
     }
     
+    // 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();
+          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;
+        }
+      }
+    
     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=44750&r1=44749&r2=44750&view=diff

==============================================================================
--- cfe/trunk/test/CodeGen/globalinit.c (original)
+++ cfe/trunk/test/CodeGen/globalinit.c Sun Dec  9 17:49:42 2007
@@ -15,3 +15,6 @@
 int latin_ptr2len (char *p);
 int (*mb_ptr2len) (char *p) = latin_ptr2len;
 
+
+char string[8] = "string";
+





More information about the cfe-commits mailing list