[cfe-commits] r64898 - in /cfe/trunk: Driver/RewriteObjC.cpp include/clang/AST/Expr.h lib/AST/Expr.cpp lib/AST/StmtSerialization.cpp lib/Sema/SemaExpr.cpp lib/Sema/SemaExprObjC.cpp

Chris Lattner sabre at nondot.org
Tue Feb 17 22:40:38 PST 2009


Author: lattner
Date: Wed Feb 18 00:40:38 2009
New Revision: 64898

URL: http://llvm.org/viewvc/llvm-project?rev=64898&view=rev
Log:
privatize all of the string literal memory allocation/creation
stuff behind a private static function.


Modified:
    cfe/trunk/Driver/RewriteObjC.cpp
    cfe/trunk/include/clang/AST/Expr.h
    cfe/trunk/lib/AST/Expr.cpp
    cfe/trunk/lib/AST/StmtSerialization.cpp
    cfe/trunk/lib/Sema/SemaExpr.cpp
    cfe/trunk/lib/Sema/SemaExprObjC.cpp

Modified: cfe/trunk/Driver/RewriteObjC.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/Driver/RewriteObjC.cpp?rev=64898&r1=64897&r2=64898&view=diff

==============================================================================
--- cfe/trunk/Driver/RewriteObjC.cpp (original)
+++ cfe/trunk/Driver/RewriteObjC.cpp Wed Feb 18 00:40:38 2009
@@ -1734,9 +1734,9 @@
   QualType StrType = Context->getPointerType(Context->CharTy);
   std::string StrEncoding;
   Context->getObjCEncodingForType(Exp->getEncodedType(), StrEncoding);
-  Expr *Replacement = new (Context) StringLiteral(*Context,StrEncoding.c_str(),
-                                        StrEncoding.length(), false, StrType, 
-                                        SourceLocation());
+  Expr *Replacement = StringLiteral::Create(*Context,StrEncoding.c_str(),
+                                            StrEncoding.length(), false,StrType,
+                                            SourceLocation());
   ReplaceStmt(Exp, Replacement);
   
   // Replace this subexpr in the parent.
@@ -1751,7 +1751,7 @@
   // Create a call to sel_registerName("selName").
   llvm::SmallVector<Expr*, 8> SelExprs;
   QualType argType = Context->getPointerType(Context->CharTy);
-  SelExprs.push_back(new (Context) StringLiteral((*Context), 
+  SelExprs.push_back(StringLiteral::Create(*Context, 
                                        Exp->getSelector().getAsString().c_str(),
                                        Exp->getSelector().getAsString().size(),
                                        false, argType, SourceLocation()));
@@ -2289,7 +2289,7 @@
             SourceLocation())); 
       llvm::SmallVector<Expr*, 8> ClsExprs;
       QualType argType = Context->getPointerType(Context->CharTy);
-      ClsExprs.push_back(new (Context) StringLiteral(*Context,
+      ClsExprs.push_back(StringLiteral::Create(*Context,
                                         SuperDecl->getIdentifier()->getName(), 
                                         SuperDecl->getIdentifier()->getLength(),
                                         false, argType, SourceLocation()));
@@ -2341,10 +2341,11 @@
     } else {
       llvm::SmallVector<Expr*, 8> ClsExprs;
       QualType argType = Context->getPointerType(Context->CharTy);
-      ClsExprs.push_back(new (Context) StringLiteral(*Context,
-                                           clsName->getName(), 
-                                           clsName->getLength(),
-                                           false, argType, SourceLocation()));
+      ClsExprs.push_back(StringLiteral::Create(*Context,
+                                               clsName->getName(), 
+                                               clsName->getLength(),
+                                               false, argType,
+                                               SourceLocation()));
       CallExpr *Cls = SynthesizeCallToFunctionDecl(GetClassFunctionDecl,
                                                    &ClsExprs[0], 
                                                    ClsExprs.size());
@@ -2371,7 +2372,7 @@
       
       llvm::SmallVector<Expr*, 8> ClsExprs;
       QualType argType = Context->getPointerType(Context->CharTy);
-      ClsExprs.push_back(new (Context) StringLiteral(*Context, 
+      ClsExprs.push_back(StringLiteral::Create(*Context, 
                                         SuperDecl->getIdentifier()->getName(), 
                                         SuperDecl->getIdentifier()->getLength(),
                                         false, argType, SourceLocation()));
@@ -2429,7 +2430,7 @@
   // Create a call to sel_registerName("selName"), it will be the 2nd argument.
   llvm::SmallVector<Expr*, 8> SelExprs;
   QualType argType = Context->getPointerType(Context->CharTy);
-  SelExprs.push_back(new (Context) StringLiteral(*Context, 
+  SelExprs.push_back(StringLiteral::Create(*Context, 
                                        Exp->getSelector().getAsString().c_str(),
                                        Exp->getSelector().getAsString().size(),
                                        false, argType, SourceLocation()));
@@ -2596,8 +2597,7 @@
   // Create a call to objc_getProtocol("ProtocolName").
   llvm::SmallVector<Expr*, 8> ProtoExprs;
   QualType argType = Context->getPointerType(Context->CharTy);
-  ProtoExprs.push_back(new (Context) 
-                       StringLiteral(*Context,
+  ProtoExprs.push_back(StringLiteral::Create(*Context,
                                 Exp->getProtocol()->getNameAsCString(),
                                 strlen(Exp->getProtocol()->getNameAsCString()),
                                 false, argType, SourceLocation()));

Modified: cfe/trunk/include/clang/AST/Expr.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/AST/Expr.h?rev=64898&r1=64897&r2=64898&view=diff

==============================================================================
--- cfe/trunk/include/clang/AST/Expr.h (original)
+++ cfe/trunk/include/clang/AST/Expr.h Wed Feb 18 00:40:38 2009
@@ -486,11 +486,21 @@
   bool IsWide;
   unsigned NumConcatenated;
   SourceLocation TokLocs[1];
+
+  StringLiteral(QualType Ty) : Expr(StringLiteralClass, Ty) {}
 public:
-  StringLiteral(ASTContext &C, const char *StrData, unsigned ByteLength,
-                bool Wide, QualType t, SourceLocation Loc);
-  StringLiteral(ASTContext &C, const char *StrData, unsigned ByteLength,
-                bool Wide, QualType t, SourceLocation *Loc, unsigned NumStrs);
+  /// This is the "fully general" constructor that allows representation of
+  /// strings formed from multiple concatenated tokens.
+  static StringLiteral *Create(ASTContext &C, const char *StrData,
+                               unsigned ByteLength, bool Wide, QualType Ty,
+                               SourceLocation *Loc, unsigned NumStrs);
+
+  /// Simple constructor for string literals made from one token.
+  static StringLiteral *Create(ASTContext &C, const char *StrData, 
+                               unsigned ByteLength,
+                               bool Wide, QualType Ty, SourceLocation Loc) {
+    return Create(C, StrData, ByteLength, Wide, Ty, &Loc, 1);
+  }
   
   void Destroy(ASTContext &C);
   
@@ -598,10 +608,14 @@
   SourceLocation getOperatorLoc() const { return Loc; }
   
   /// isPostfix - Return true if this is a postfix operation, like x++.
-  static bool isPostfix(Opcode Op);
+  static bool isPostfix(Opcode Op) {
+    return Op == PostInc || Op == PostDec;
+  }
 
   /// isPostfix - Return true if this is a prefix operation, like --x.
-  static bool isPrefix(Opcode Op);
+  static bool isPrefix(Opcode Op) {
+    return Op == PreInc || Op == PreDec;
+  }
 
   bool isPrefix() const { return isPrefix(Opc); }
   bool isPostfix() const { return isPostfix(Opc); }

Modified: cfe/trunk/lib/AST/Expr.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/AST/Expr.cpp?rev=64898&r1=64897&r2=64898&view=diff

==============================================================================
--- cfe/trunk/lib/AST/Expr.cpp (original)
+++ cfe/trunk/lib/AST/Expr.cpp Wed Feb 18 00:40:38 2009
@@ -37,35 +37,29 @@
   return V.convertToDouble();
 }
 
-
-StringLiteral::StringLiteral(ASTContext& C, const char *strData,
-                             unsigned byteLength, bool Wide, QualType Ty,
-                             SourceLocation Loc) : 
-    Expr(StringLiteralClass, Ty) {
+StringLiteral *StringLiteral::Create(ASTContext &C, const char *StrData,
+                                     unsigned ByteLength, bool Wide,
+                                     QualType Ty,
+                                     SourceLocation *Loc, unsigned NumStrs) {
+  // Allocate enough space for the StringLiteral plus an array of locations for
+  // any concatenated string tokens.
+  void *Mem = C.Allocate(sizeof(StringLiteral)+
+                         sizeof(SourceLocation)*(NumStrs-1),
+                         llvm::alignof<StringLiteral>());
+  StringLiteral *SL = new (Mem) StringLiteral(Ty);
+  
   // OPTIMIZE: could allocate this appended to the StringLiteral.
-  char *AStrData = new (C, 1) char[byteLength];
-  memcpy(AStrData, strData, byteLength);
-  StrData = AStrData;
-  ByteLength = byteLength;
-  IsWide = Wide;
-  TokLocs[0] = Loc;
-  NumConcatenated = 1;
-}
+  char *AStrData = new (C, 1) char[ByteLength];
+  memcpy(AStrData, StrData, ByteLength);
+  SL->StrData = AStrData;
+  SL->ByteLength = ByteLength;
+  SL->IsWide = Wide;
+  SL->TokLocs[0] = Loc[0];
+  SL->NumConcatenated = NumStrs;
 
-StringLiteral::StringLiteral(ASTContext &C, const char *strData, 
-                             unsigned byteLength, bool Wide, QualType Ty,
-                             SourceLocation *Loc, unsigned NumStrs) : 
-    Expr(StringLiteralClass, Ty) {
-  // OPTIMIZE: could allocate this appended to the StringLiteral.
-  char *AStrData = new (C, 1) char[byteLength];
-  memcpy(AStrData, strData, byteLength);
-  StrData = AStrData;
-  ByteLength = byteLength;
-  IsWide = Wide;
-  TokLocs[0] = Loc[0];
-  NumConcatenated = NumStrs;
   if (NumStrs != 1)
-    memcpy(&TokLocs[1], Loc+1, sizeof(SourceLocation)*(NumStrs-1));
+    memcpy(&SL->TokLocs[1], Loc+1, sizeof(SourceLocation)*(NumStrs-1));
+  return SL;
 }
 
 
@@ -75,26 +69,6 @@
   C.Deallocate(this);
 }
 
-bool UnaryOperator::isPostfix(Opcode Op) {
-  switch (Op) {
-  case PostInc:
-  case PostDec:
-    return true;
-  default:
-    return false;
-  }
-}
-
-bool UnaryOperator::isPrefix(Opcode Op) {
-  switch (Op) {
-    case PreInc:
-    case PreDec:
-      return true;
-    default:
-      return false;
-  }
-}
-
 /// getOpcodeStr - Turn an Opcode enum value into the punctuation char it
 /// corresponds to, e.g. "sizeof" or "[pre]++".
 const char *UnaryOperator::getOpcodeStr(Opcode Op) {

Modified: cfe/trunk/lib/AST/StmtSerialization.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/AST/StmtSerialization.cpp?rev=64898&r1=64897&r2=64898&view=diff

==============================================================================
--- cfe/trunk/lib/AST/StmtSerialization.cpp (original)
+++ cfe/trunk/lib/AST/StmtSerialization.cpp Wed Feb 18 00:40:38 2009
@@ -971,8 +971,8 @@
   bool isWide = D.ReadBool();
   unsigned ByteLength = D.ReadInt();
   
-  StringLiteral* sl = new (C, llvm::alignof<StringLiteral>())
-    StringLiteral(C, NULL, 0, isWide, t, SourceLocation());
+  StringLiteral* sl = StringLiteral::Create(C, NULL, 0, isWide, t,
+                                            SourceLocation());
 
   char* StrData = new (C, llvm::alignof<char>()) char[ByteLength];
   for (unsigned i = 0; i < ByteLength; ++i)

Modified: cfe/trunk/lib/Sema/SemaExpr.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaExpr.cpp?rev=64898&r1=64897&r2=64898&view=diff

==============================================================================
--- cfe/trunk/lib/Sema/SemaExpr.cpp (original)
+++ cfe/trunk/lib/Sema/SemaExpr.cpp Wed Feb 18 00:40:38 2009
@@ -352,17 +352,13 @@
   StrTy = Context.getConstantArrayType(StrTy,
                                    llvm::APInt(32, Literal.GetStringLength()+1),
                                        ArrayType::Normal, 0);
-  // Allocate enough space for the StringLiteral plus an array of locations for
-  // any concatenated strings.
-  void *Mem = Context.Allocate(sizeof(StringLiteral)+
-                               sizeof(SourceLocation)*(NumStringToks-1));
   
   // Pass &StringTokLocs[0], StringTokLocs.size() to factory!
-  return Owned(new (Mem) StringLiteral(Context, Literal.GetString(), 
-                                       Literal.GetStringLength(),
-                                       Literal.AnyWide, StrTy,
-                                       &StringTokLocs[0],
-                                       StringTokLocs.size()));
+  return Owned(StringLiteral::Create(Context, Literal.GetString(), 
+                                     Literal.GetStringLength(),
+                                     Literal.AnyWide, StrTy,
+                                     &StringTokLocs[0],
+                                     StringTokLocs.size()));
 }
 
 /// ShouldSnapshotBlockValueReference - Return true if a reference inside of

Modified: cfe/trunk/lib/Sema/SemaExprObjC.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaExprObjC.cpp?rev=64898&r1=64897&r2=64898&view=diff

==============================================================================
--- cfe/trunk/lib/Sema/SemaExprObjC.cpp (original)
+++ cfe/trunk/lib/Sema/SemaExprObjC.cpp Wed Feb 18 00:40:38 2009
@@ -51,9 +51,9 @@
       S->Destroy(Context);
     }
     // FIXME: PASS LOCATIONS PROPERLY.
-    S = new (Context) StringLiteral(Context, strBuf, Length, false,
-                                    Context.getPointerType(Context.CharTy),
-                                    AtLocs[0]);
+    S = StringLiteral::Create(Context, strBuf, Length, false,
+                              Context.getPointerType(Context.CharTy),
+                              AtLocs[0]);
   }
   
   // Verify that this composite string is acceptable for ObjC strings.





More information about the cfe-commits mailing list