]> granicus.if.org Git - clang/commitdiff
Implement codegen support for:
authorChris Lattner <sabre@nondot.org>
Sun, 9 Dec 2007 23:49:42 +0000 (23:49 +0000)
committerChris Lattner <sabre@nondot.org>
Sun, 9 Dec 2007 23:49:42 +0000 (23:49 +0000)
char text[8] = "string";

Big fixme remains.

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@44750 91177308-0d34-0410-b5e6-96231b3b80d8

CodeGen/CodeGenModule.cpp
test/CodeGen/globalinit.c

index ecabb814ae767d2572c75fc4a7d41038cd04f3b4..55336510c637b9f14de5066b58f3cc815bdefab8 100644 (file)
@@ -407,6 +407,27 @@ static llvm::Constant *GenerateConstantExpr(const Expr *Expression,
       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);
   }
 
index bfa909bd753427514504cde67b89f93980dc2c3a..996d2acabb52622f257ef1c298b6697412b685c2 100644 (file)
@@ -15,3 +15,6 @@ void *g = y;
 int latin_ptr2len (char *p);
 int (*mb_ptr2len) (char *p) = latin_ptr2len;
 
+
+char string[8] = "string";
+