[cfe-commits] r51259 - in /cfe/trunk: lib/CodeGen/CGExpr.cpp lib/CodeGen/CGExprAgg.cpp test/CodeGen/string-init.c

Eli Friedman eli.friedman at gmail.com
Mon May 19 10:51:16 PDT 2008


Author: efriedma
Date: Mon May 19 12:51:16 2008
New Revision: 51259

URL: http://llvm.org/viewvc/llvm-project?rev=51259&view=rev
Log:
Fix the emission of expressions like char a[10] = "asdf"; previously, 
they were causing bad code to be emitted.  There are two fixes here: one 
makes sure we emit a string that is long enough, and one makes sure we 
properly handle string initialization in init lists.


Added:
    cfe/trunk/test/CodeGen/string-init.c
Modified:
    cfe/trunk/lib/CodeGen/CGExpr.cpp
    cfe/trunk/lib/CodeGen/CGExprAgg.cpp

Modified: cfe/trunk/lib/CodeGen/CGExpr.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/CodeGen/CGExpr.cpp?rev=51259&r1=51258&r2=51259&view=diff

==============================================================================
--- cfe/trunk/lib/CodeGen/CGExpr.cpp (original)
+++ cfe/trunk/lib/CodeGen/CGExpr.cpp Mon May 19 12:51:16 2008
@@ -395,9 +395,16 @@
 
 LValue CodeGenFunction::EmitStringLiteralLValue(const StringLiteral *E) {
   assert(!E->isWide() && "FIXME: Wide strings not supported yet!");
+  // Get the string data
   const char *StrData = E->getStrData();
   unsigned Len = E->getByteLength();
   std::string StringLiteral(StrData, StrData+Len);
+
+  // Resize the string to the right size
+  const ConstantArrayType *CAT = E->getType()->getAsConstantArrayType();
+  uint64_t RealLen = CAT->getSize().getZExtValue();
+  StringLiteral.resize(RealLen, '\0');
+
   return LValue::MakeAddr(CGM.GetAddrOfConstantString(StringLiteral));
 }
 

Modified: cfe/trunk/lib/CodeGen/CGExprAgg.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/CodeGen/CGExprAgg.cpp?rev=51259&r1=51258&r2=51259&view=diff

==============================================================================
--- cfe/trunk/lib/CodeGen/CGExprAgg.cpp (original)
+++ cfe/trunk/lib/CodeGen/CGExprAgg.cpp Mon May 19 12:51:16 2008
@@ -53,7 +53,7 @@
   void EmitAggregateClear(llvm::Value *DestPtr, QualType Ty);
 
   void EmitNonConstInit(InitListExpr *E);
-  
+
   //===--------------------------------------------------------------------===//
   //                            Visitor Methods
   //===--------------------------------------------------------------------===//
@@ -336,7 +336,6 @@
   }
 }
 
-
 void AggExprEmitter::VisitInitListExpr(InitListExpr *E) {
   if (E->isConstantExpr(CGF.getContext(), 0)) {
     // FIXME: call into const expr emitter so that we can emit
@@ -355,6 +354,14 @@
       cast<llvm::ArrayType>(APType->getElementType());
     
     uint64_t NumInitElements = E->getNumInits();
+
+    if (E->getNumInits() > 0 &&
+        E->getType().getCanonicalType().getUnqualifiedType() == 
+          E->getInit(0)->getType().getCanonicalType().getUnqualifiedType()) {
+      EmitAggLoadOfLValue(E->getInit(0));
+      return;
+    }
+
     uint64_t NumArrayElements = AType->getNumElements();
     QualType ElementType = E->getType()->getAsArrayType()->getElementType();
     

Added: cfe/trunk/test/CodeGen/string-init.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/CodeGen/string-init.c?rev=51259&view=auto

==============================================================================
--- cfe/trunk/test/CodeGen/string-init.c (added)
+++ cfe/trunk/test/CodeGen/string-init.c Mon May 19 12:51:16 2008
@@ -0,0 +1,8 @@
+// RUN: clang -emit-llvm %s -o - | not grep "[5 x i8]"
+// RUN: clang -emit-llvm %s -o - | not grep "store"
+
+void test(void) {
+  char a[10] = "asdf";
+  char b[10] = { "asdf" };
+}
+





More information about the cfe-commits mailing list