[cfe-commits] r54621 - in /cfe/trunk/lib/CodeGen: CGExpr.cpp CGExprConstant.cpp CodeGenModule.cpp CodeGenModule.h

Daniel Dunbar daniel at zuster.org
Sun Aug 10 13:25:57 PDT 2008


Author: ddunbar
Date: Sun Aug 10 15:25:57 2008
New Revision: 54621

URL: http://llvm.org/viewvc/llvm-project?rev=54621&view=rev
Log:
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!

Modified:
    cfe/trunk/lib/CodeGen/CGExpr.cpp
    cfe/trunk/lib/CodeGen/CGExprConstant.cpp
    cfe/trunk/lib/CodeGen/CodeGenModule.cpp
    cfe/trunk/lib/CodeGen/CodeGenModule.h

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

==============================================================================
--- cfe/trunk/lib/CodeGen/CGExpr.cpp (original)
+++ cfe/trunk/lib/CodeGen/CGExpr.cpp Sun Aug 10 15:25:57 2008
@@ -492,19 +492,10 @@
 }
 
 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 =
-    getContext().getAsConstantArrayType(E->getType());
-  uint64_t RealLen = CAT->getSize().getZExtValue();
-  StringLiteral.resize(RealLen, '\0');
+  llvm::Constant *C = 
+    CGM.GetAddrOfConstantString(CGM.getStringForStringLiteral(E));
 
-  return LValue::MakeAddr(CGM.GetAddrOfConstantString(StringLiteral),0);
+  return LValue::MakeAddr(C,0);
 }
 
 LValue CodeGenFunction::EmitPredefinedLValue(const PredefinedExpr *E) {

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

==============================================================================
--- cfe/trunk/lib/CodeGen/CGExprConstant.cpp (original)
+++ cfe/trunk/lib/CodeGen/CGExprConstant.cpp Sun Aug 10 15:25:57 2008
@@ -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);

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

==============================================================================
--- cfe/trunk/lib/CodeGen/CodeGenModule.cpp (original)
+++ cfe/trunk/lib/CodeGen/CodeGenModule.cpp Sun Aug 10 15:25:57 2008
@@ -902,12 +902,32 @@
   return GV;
 }
 
+/// getStringForStringLiteral - Return the appropriate bytes for a
+/// string literal, properly padded to match the literal type.
+std::string CodeGenModule::getStringForStringLiteral(const StringLiteral *E) {
+  assert(!E->isWide() && "FIXME: Wide strings not supported yet!");
+  const char *StrData = E->getStrData();
+  unsigned Len = E->getByteLength();
+
+  const ConstantArrayType *CAT =
+    getContext().getAsConstantArrayType(E->getType());
+  assert(CAT && "String isn't pointer or array!");
+  
+  // Resize the string to the right size
+  // FIXME: What about wchar_t strings?
+  std::string Str(StrData, StrData+Len);
+  uint64_t RealLen = CAT->getSize().getZExtValue();
+  Str.resize(RealLen, '\0');
+  
+  return Str;
+}
+
 /// GenerateWritableString -- Creates storage for a string literal.
 static llvm::Constant *GenerateStringLiteral(const std::string &str, 
                                              bool constant,
                                              CodeGenModule &CGM) {
   // Create Constant for this string literal
-  llvm::Constant *C = llvm::ConstantArray::get(str, false);
+  llvm::Constant *C = llvm::ConstantArray::get(str);
   
   // Create a global variable for this string
   C = new llvm::GlobalVariable(C->getType(), constant, 

Modified: cfe/trunk/lib/CodeGen/CodeGenModule.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/CodeGen/CodeGenModule.h?rev=54621&r1=54620&r2=54621&view=diff

==============================================================================
--- cfe/trunk/lib/CodeGen/CodeGenModule.h (original)
+++ cfe/trunk/lib/CodeGen/CodeGenModule.h Sun Aug 10 15:25:57 2008
@@ -39,6 +39,7 @@
   class Decl;
   class Expr;
   class Stmt;
+  class StringLiteral;
   class NamedDecl;
   class ValueDecl;
   class VarDecl;
@@ -134,6 +135,10 @@
   llvm::Function *getBuiltinLibFunction(unsigned BuiltinID);
   llvm::Constant *GetAddrOfConstantCFString(const std::string& str);
 
+  /// getStringForStringLiteral - Return the appropriate bytes for a
+  /// string literal, properly padded to match the literal type.
+  std::string getStringForStringLiteral(const StringLiteral *E);
+
   /// GetAddrOfConstantString -- returns a pointer to the character
   /// array containing the literal.  The result is pointer to array type.
   llvm::Constant *GetAddrOfConstantString(const std::string& str);





More information about the cfe-commits mailing list