]> granicus.if.org Git - clang/commitdiff
privatize all of the string literal memory allocation/creation
authorChris Lattner <sabre@nondot.org>
Wed, 18 Feb 2009 06:40:38 +0000 (06:40 +0000)
committerChris Lattner <sabre@nondot.org>
Wed, 18 Feb 2009 06:40:38 +0000 (06:40 +0000)
stuff behind a private static function.

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

Driver/RewriteObjC.cpp
include/clang/AST/Expr.h
lib/AST/Expr.cpp
lib/AST/StmtSerialization.cpp
lib/Sema/SemaExpr.cpp
lib/Sema/SemaExprObjC.cpp

index 878a94c1f1aaf48fefd9811d06fb370dacb74e62..c6b9b43a9ef990cf390862adaaf769a7c4dcd61a 100644 (file)
@@ -1734,9 +1734,9 @@ Stmt *RewriteObjC::RewriteAtEncode(ObjCEncodeExpr *Exp) {
   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 @@ Stmt *RewriteObjC::RewriteAtSelector(ObjCSelectorExpr *Exp) {
   // 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 @@ Stmt *RewriteObjC::SynthMessageExpr(ObjCMessageExpr *Exp) {
             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 @@ Stmt *RewriteObjC::SynthMessageExpr(ObjCMessageExpr *Exp) {
     } 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 @@ Stmt *RewriteObjC::SynthMessageExpr(ObjCMessageExpr *Exp) {
       
       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 @@ Stmt *RewriteObjC::SynthMessageExpr(ObjCMessageExpr *Exp) {
   // 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 @@ Stmt *RewriteObjC::RewriteObjCProtocolExpr(ObjCProtocolExpr *Exp) {
   // 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()));
index 8130f4f63107403c600ac573a518ec2779306717..54167c9c377ff6548f6029415d38d3cb98ac448c 100644 (file)
@@ -486,11 +486,21 @@ class StringLiteral : public Expr {
   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 @@ public:
   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); }
index 81da44407b1f9789992d5dd677097cffac23142e..e436a41b550ecf3a797ca6ac3e2d62ca97447132 100644 (file)
@@ -37,35 +37,29 @@ double FloatingLiteral::getValueAsApproximateDouble() const {
   return V.convertToDouble();
 }
 
-
-StringLiteral::StringLiteral(ASTContext& C, const char *strData,
-                             unsigned byteLength, bool Wide, QualType Ty,
-                             SourceLocation Loc) : 
-    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;
-  NumConcatenated = 1;
-}
-
-StringLiteral::StringLiteral(ASTContext &C, const char *strData, 
-                             unsigned byteLength, bool Wide, QualType Ty,
-                             SourceLocation *Loc, unsigned NumStrs) : 
-    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[0];
-  NumConcatenated = NumStrs;
+  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;
+
   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 @@ void StringLiteral::Destroy(ASTContext &C) {
   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) {
index 074969e9de270736a7d24e759e38026f7afbc4ce..e1d85aa01c50d8aa9718378d199b91e72bade4b5 100644 (file)
@@ -971,8 +971,8 @@ StringLiteral* StringLiteral::CreateImpl(Deserializer& D, ASTContext& C) {
   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)
index 48f338e475dfeb84329a97dd985027a54fe83d8c..b2df86abe63f70ddf05ff2531a2290b52b90d3aa 100644 (file)
@@ -352,17 +352,13 @@ Sema::ActOnStringLiteral(const Token *StringToks, unsigned NumStringToks) {
   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
index 6e40b3c4a3e3157c8929f1bd73a0c149300e03b1..886a3ca1dce583a22b0fabe8b25a15758c9da2ac 100644 (file)
@@ -51,9 +51,9 @@ Sema::ExprResult Sema::ParseObjCStringLiteral(SourceLocation *AtLocs,
       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.