]> granicus.if.org Git - clang/commitdiff
Move StringLiteral to allocate its internal string data using the allocator in
authorTed Kremenek <kremenek@apple.com>
Fri, 6 Feb 2009 19:55:15 +0000 (19:55 +0000)
committerTed Kremenek <kremenek@apple.com>
Fri, 6 Feb 2009 19:55:15 +0000 (19:55 +0000)
ASTContext. This required changing all clients to pass in the ASTContext& to the
constructor of StringLiteral. I also changed all allocations of StringLiteral to
use new(ASTContext&).

Along the way, I updated a bunch of new()'s in StmtSerialization.cpp to use the
allocator from ASTContext& (not complete).

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@63958 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 f860433c29eb46dd85edf00f2a17de02fad0bd25..4e262963a973411bd98ba9aad8a7eb0c45ec2265 100644 (file)
@@ -1717,7 +1717,7 @@ Stmt *RewriteObjC::RewriteAtEncode(ObjCEncodeExpr *Exp) {
   QualType StrType = Context->getPointerType(Context->CharTy);
   std::string StrEncoding;
   Context->getObjCEncodingForType(Exp->getEncodedType(), StrEncoding);
-  Expr *Replacement = new StringLiteral(StrEncoding.c_str(),
+  Expr *Replacement = new (*Context) StringLiteral(*Context,StrEncoding.c_str(),
                                         StrEncoding.length(), false, StrType, 
                                         SourceLocation(), SourceLocation());
   ReplaceStmt(Exp, Replacement);
@@ -1734,7 +1734,8 @@ 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 StringLiteral(Exp->getSelector().getAsString().c_str(),
+  SelExprs.push_back(new (*Context) StringLiteral((*Context), 
+                                       Exp->getSelector().getAsString().c_str(),
                                        Exp->getSelector().getAsString().size(),
                                        false, argType, SourceLocation(),
                                        SourceLocation()));
@@ -2271,10 +2272,11 @@ Stmt *RewriteObjC::SynthMessageExpr(ObjCMessageExpr *Exp) {
             SourceLocation())); 
       llvm::SmallVector<Expr*, 8> ClsExprs;
       QualType argType = Context->getPointerType(Context->CharTy);
-      ClsExprs.push_back(new StringLiteral(SuperDecl->getIdentifier()->getName(), 
-                                           SuperDecl->getIdentifier()->getLength(),
-                                           false, argType, SourceLocation(),
-                                           SourceLocation()));
+      ClsExprs.push_back(new (*Context) StringLiteral(*Context,
+                                        SuperDecl->getIdentifier()->getName(), 
+                                        SuperDecl->getIdentifier()->getLength(),
+                                        false, argType, SourceLocation(),
+                                        SourceLocation()));
       CallExpr *Cls = SynthesizeCallToFunctionDecl(GetMetaClassFunctionDecl,
                                                    &ClsExprs[0], 
                                                    ClsExprs.size());
@@ -2322,7 +2324,8 @@ Stmt *RewriteObjC::SynthMessageExpr(ObjCMessageExpr *Exp) {
     } else {
       llvm::SmallVector<Expr*, 8> ClsExprs;
       QualType argType = Context->getPointerType(Context->CharTy);
-      ClsExprs.push_back(new StringLiteral(clsName->getName(), 
+      ClsExprs.push_back(new (*Context) StringLiteral(*Context,
+                                           clsName->getName(), 
                                            clsName->getLength(),
                                            false, argType, SourceLocation(),
                                            SourceLocation()));
@@ -2352,10 +2355,11 @@ Stmt *RewriteObjC::SynthMessageExpr(ObjCMessageExpr *Exp) {
       
       llvm::SmallVector<Expr*, 8> ClsExprs;
       QualType argType = Context->getPointerType(Context->CharTy);
-      ClsExprs.push_back(new StringLiteral(SuperDecl->getIdentifier()->getName(), 
-                                           SuperDecl->getIdentifier()->getLength(),
-                                           false, argType, SourceLocation(),
-                                           SourceLocation()));
+      ClsExprs.push_back(new (*Context) StringLiteral(*Context, 
+                                        SuperDecl->getIdentifier()->getName(), 
+                                        SuperDecl->getIdentifier()->getLength(),
+                                        false, argType, SourceLocation(),
+                                        SourceLocation()));
       CallExpr *Cls = SynthesizeCallToFunctionDecl(GetClassFunctionDecl,
                                                    &ClsExprs[0], 
                                                    ClsExprs.size());
@@ -2409,7 +2413,8 @@ 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 StringLiteral(Exp->getSelector().getAsString().c_str(),
+  SelExprs.push_back(new (*Context) StringLiteral(*Context, 
+                                       Exp->getSelector().getAsString().c_str(),
                                        Exp->getSelector().getAsString().size(),
                                        false, argType, SourceLocation(),
                                        SourceLocation()));
@@ -2574,9 +2579,11 @@ 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 StringLiteral(Exp->getProtocol()->getNameAsCString(),
-                                       strlen(Exp->getProtocol()->getNameAsCString()),
-                                       false, argType, SourceLocation(),
+  ProtoExprs.push_back(new (*Context) 
+                       StringLiteral(*Context,
+                                Exp->getProtocol()->getNameAsCString(),
+                                strlen(Exp->getProtocol()->getNameAsCString()),
+                                false, argType, SourceLocation(),
                                        SourceLocation()));
   CallExpr *ProtoExp = SynthesizeCallToFunctionDecl(GetProtocolFunctionDecl,
                                                     &ProtoExprs[0], 
index c933a0c94bb57ee62f0694c3600e1382b9defd17..3884d7dc2bd96267d4a486548acd83994190685f 100644 (file)
@@ -479,9 +479,10 @@ class StringLiteral : public Expr {
   // FIXME: if space becomes an issue, we should create a sub-class.
   SourceLocation firstTokLoc, lastTokLoc;
 public:
-  StringLiteral(const char *strData, unsigned byteLength, bool Wide, 
-                QualType t, SourceLocation b, SourceLocation e);
-  virtual ~StringLiteral();
+  StringLiteral(ASTContext& C, const char *strData, unsigned byteLength,
+                bool Wide, QualType t, SourceLocation b, SourceLocation e);
+  
+  void Destroy(ASTContext& C);
   
   const char *getStrData() const { return StrData; }
   unsigned getByteLength() const { return ByteLength; }
index ce13c34012ae875a49aa146602394eaf8597f3c3..d627ab03b291fd014bff425005eae3abec83217e 100644 (file)
@@ -38,12 +38,13 @@ double FloatingLiteral::getValueAsApproximateDouble() const {
 }
 
 
-StringLiteral::StringLiteral(const char *strData, unsigned byteLength, 
-                             bool Wide, QualType t, SourceLocation firstLoc,
+StringLiteral::StringLiteral(ASTContext& C, const char *strData,
+                             unsigned byteLength, bool Wide, QualType t,
+                             SourceLocation firstLoc,
                              SourceLocation lastLoc) : 
   Expr(StringLiteralClass, t) {
   // OPTIMIZE: could allocate this appended to the StringLiteral.
-  char *AStrData = new char[byteLength];
+  char *AStrData = new (C, 1) char[byteLength];
   memcpy(AStrData, strData, byteLength);
   StrData = AStrData;
   ByteLength = byteLength;
@@ -52,8 +53,9 @@ StringLiteral::StringLiteral(const char *strData, unsigned byteLength,
   lastTokLoc = lastLoc;
 }
 
-StringLiteral::~StringLiteral() {
-  delete[] StrData;
+void StringLiteral::Destroy(ASTContext &C) {
+  C.Deallocate(const_cast<char*>(StrData));
+  this->~StringLiteral();
 }
 
 bool UnaryOperator::isPostfix(Opcode Op) {
index 3bb98b46f494dec24144fa827d4ab8f3db248fb9..8d6636d6a642ba3caee7f9714ebaef83954fff68 100644 (file)
@@ -17,6 +17,7 @@
 #include "clang/AST/Expr.h"
 #include "clang/AST/ExprCXX.h"
 #include "clang/AST/ExprObjC.h"
+#include "clang/AST/ASTContext.h"
 #include "llvm/Bitcode/Serialize.h"
 #include "llvm/Bitcode/Deserialize.h"
 
@@ -274,7 +275,8 @@ AddrLabelExpr* AddrLabelExpr::CreateImpl(Deserializer& D, ASTContext& C) {
   QualType t = QualType::ReadVal(D);
   SourceLocation AALoc = SourceLocation::ReadVal(D);
   SourceLocation LLoc = SourceLocation::ReadVal(D);
-  AddrLabelExpr* expr = new AddrLabelExpr(AALoc,LLoc,NULL,t);
+  AddrLabelExpr* expr = new (C, llvm::alignof<AddrLabelExpr>())
+    AddrLabelExpr(AALoc,LLoc,NULL,t);
   D.ReadPtr(expr->Label); // Pointer may be backpatched.
   return expr;
 }
@@ -290,7 +292,8 @@ ArraySubscriptExpr* ArraySubscriptExpr::CreateImpl(Deserializer& D, ASTContext&
   SourceLocation L = SourceLocation::ReadVal(D);
   Expr *LHS, *RHS;
   D.BatchReadOwnedPtrs(LHS, RHS, C);
-  return new ArraySubscriptExpr(LHS,RHS,t,L);  
+  return new (C, llvm::alignof<ArraySubscriptExpr>())
+    ArraySubscriptExpr(LHS,RHS,t,L);  
 }
 
 void AsmStmt::EmitImpl(Serializer& S) const {
@@ -327,9 +330,8 @@ AsmStmt* AsmStmt::CreateImpl(Deserializer& D, ASTContext& C) {
   
   bool IsVolatile = D.ReadBool();
   bool IsSimple = D.ReadBool();
-  AsmStmt *Stmt = new AsmStmt(ALoc, IsSimple, IsVolatile, 0, 0, 0, 0, 0,  
-                              AsmStr, 
-                              0, 0, PLoc);  
+  AsmStmt *Stmt = new (C, llvm::alignof<AsmStmt>())
+    AsmStmt(ALoc, IsSimple, IsVolatile, 0, 0, 0, 0, 0, AsmStr, 0, 0, PLoc);  
 
   Stmt->NumOutputs = D.ReadInt();
   Stmt->NumInputs = D.ReadInt();
@@ -374,7 +376,8 @@ BinaryOperator* BinaryOperator::CreateImpl(Deserializer& D, ASTContext& C) {
   Expr *LHS, *RHS;
   D.BatchReadOwnedPtrs(LHS, RHS, C);
 
-  return new BinaryOperator(LHS,RHS,Opc,Result,OpLoc);
+  return new (C, llvm::alignof<BinaryOperator>())
+    BinaryOperator(LHS,RHS,Opc,Result,OpLoc);
 }
 
 void BreakStmt::EmitImpl(Serializer& S) const {
@@ -383,7 +386,7 @@ void BreakStmt::EmitImpl(Serializer& S) const {
 
 BreakStmt* BreakStmt::CreateImpl(Deserializer& D, ASTContext& C) {
   SourceLocation Loc = SourceLocation::ReadVal(D);
-  return new BreakStmt(Loc);
+  return new (C, llvm::alignof<BreakStmt>()) BreakStmt(Loc);
 }
 
 void CallExpr::EmitImpl(Serializer& S) const {
@@ -397,10 +400,10 @@ CallExpr* CallExpr::CreateImpl(Deserializer& D, ASTContext& C, StmtClass SC) {
   QualType t = QualType::ReadVal(D);
   SourceLocation L = SourceLocation::ReadVal(D);
   unsigned NumArgs = D.ReadInt();
-  Stmt** SubExprs = new Stmt*[NumArgs+1];
+  Stmt** SubExprs = new (C, llvm::alignof<Stmt*>()) Stmt*[NumArgs+1];
   D.BatchReadOwnedPtrs(NumArgs+1, SubExprs, C);
 
-  return new CallExpr(SC, SubExprs,NumArgs,t,L);  
+  return new (C, llvm::alignof<CallExpr>()) CallExpr(SC, SubExprs,NumArgs,t,L);  
 }
 
 void CaseStmt::EmitImpl(Serializer& S) const {
@@ -411,7 +414,8 @@ void CaseStmt::EmitImpl(Serializer& S) const {
 
 CaseStmt* CaseStmt::CreateImpl(Deserializer& D, ASTContext& C) {
   SourceLocation CaseLoc = SourceLocation::ReadVal(D);
-  CaseStmt* stmt = new CaseStmt(NULL,NULL,NULL,CaseLoc);  
+  CaseStmt* stmt = new (C, llvm::alignof<CaseStmt>())
+                    CaseStmt(NULL,NULL,NULL,CaseLoc);
   D.ReadPtr(stmt->NextSwitchCase);
   D.BatchReadOwnedPtrs((unsigned) END_EXPR, &stmt->SubExprs[0], C);
   return stmt;
@@ -431,7 +435,8 @@ CStyleCastExpr* CStyleCastExpr::CreateImpl(Deserializer& D, ASTContext& C) {
   SourceLocation LPLoc = SourceLocation::ReadVal(D);
   SourceLocation RPLoc = SourceLocation::ReadVal(D);
   Expr* Op = D.ReadOwnedPtr<Expr>(C);
-  return new CStyleCastExpr(t,Op,writtenTy,LPLoc,RPLoc);
+  return new (C, llvm::alignof<CStyleCastExpr>())
+    CStyleCastExpr(t,Op,writtenTy,LPLoc,RPLoc);
 }
 
 void CharacterLiteral::EmitImpl(Serializer& S) const {
@@ -446,7 +451,8 @@ CharacterLiteral* CharacterLiteral::CreateImpl(Deserializer& D, ASTContext& C) {
   SourceLocation Loc = SourceLocation::ReadVal(D);
   bool iswide = D.ReadBool();
   QualType T = QualType::ReadVal(D);
-  return new CharacterLiteral(value,iswide,T,Loc);
+  return new (C, llvm::alignof<CharacterLiteral>())
+    CharacterLiteral(value,iswide,T,Loc);
 }
 
 void CompoundAssignOperator::EmitImpl(Serializer& S) const {
@@ -466,7 +472,8 @@ CompoundAssignOperator::CreateImpl(Deserializer& D, ASTContext& C) {
   Expr* LHS, *RHS;
   D.BatchReadOwnedPtrs(LHS, RHS, C);
   
-  return new CompoundAssignOperator(LHS,RHS,Opc,t,c,L);
+  return new (C, llvm::alignof<CompoundAssignOperator>())
+    CompoundAssignOperator(LHS,RHS,Opc,t,c,L);
 }
 
 void CompoundLiteralExpr::EmitImpl(Serializer& S) const {
@@ -481,7 +488,8 @@ CompoundLiteralExpr* CompoundLiteralExpr::CreateImpl(Deserializer& D, ASTContext
   SourceLocation L = SourceLocation::ReadVal(D);
   bool fileScope = D.ReadBool();
   Expr* Init = D.ReadOwnedPtr<Expr>(C);
-  return new CompoundLiteralExpr(L, Q, Init, fileScope);
+  return new (C, llvm::alignof<CompoundLiteralExpr>())
+    CompoundLiteralExpr(L, Q, Init, fileScope);
 }
 
 void CompoundStmt::EmitImpl(Serializer& S) const {
@@ -498,7 +506,8 @@ CompoundStmt* CompoundStmt::CreateImpl(Deserializer& D, ASTContext& C) {
   SourceLocation RB = SourceLocation::ReadVal(D);
   unsigned size = D.ReadInt();
   
-  CompoundStmt* stmt = new CompoundStmt(NULL,0,LB,RB);
+  CompoundStmt* stmt = new (C, llvm::alignof<CompoundStmt>())
+    CompoundStmt(NULL, 0, LB, RB);
   
   stmt->Body.reserve(size);
   
@@ -517,7 +526,8 @@ ConditionalOperator* ConditionalOperator::CreateImpl(Deserializer& D,
                                                      ASTContext& C) {
 
   QualType t = QualType::ReadVal(D);
-  ConditionalOperator* c = new ConditionalOperator(NULL,NULL,NULL,t);
+  ConditionalOperator* c = new (C, llvm::alignof<ConditionalOperator>())
+    ConditionalOperator(NULL,NULL,NULL,t);
   D.BatchReadOwnedPtrs((unsigned) END_EXPR, c->SubExprs, C);
   return c;
 }
@@ -962,9 +972,10 @@ StringLiteral* StringLiteral::CreateImpl(Deserializer& D, ASTContext& C) {
   bool isWide = D.ReadBool();
   unsigned ByteLength = D.ReadInt();
   
-  StringLiteral* sl = new StringLiteral(NULL,0,isWide,t,firstTokLoc,lastTokLoc);
+  StringLiteral* sl = new (C, llvm::alignof<StringLiteral>())
+    StringLiteral(C, NULL, 0, isWide, t, firstTokLoc, lastTokLoc);
 
-  char* StrData = new char[ByteLength];
+  char* StrData = new (C, llvm::alignof<char>()) char[ByteLength];
   for (unsigned i = 0; i < ByteLength; ++i)
     StrData[i] = (char) D.ReadInt();
 
index 3ee6e52e2e8470a4c90f2c8c2428c7864f226352..8c8207a39d65534b23b2e9a68f60641989063df3 100644 (file)
@@ -319,7 +319,7 @@ Sema::ActOnStringLiteral(const Token *StringToks, unsigned NumStringToks) {
                                        ArrayType::Normal, 0);
 
   // Pass &StringTokLocs[0], StringTokLocs.size() to factory!
-  return Owned(new (Context) StringLiteral(Literal.GetString(), 
+  return Owned(new (Context) StringLiteral(Context, Literal.GetString(), 
                                  Literal.GetStringLength(),
                                  Literal.AnyWide, StrTy,
                                  StringToks[0].getLocation(),
index 983231ccf48547852b3f425e78185c7198ca38f8..003e121f371fc702845e2d64aae74943f8f80d39 100644 (file)
@@ -40,9 +40,9 @@ Sema::ExprResult Sema::ParseObjCStringLiteral(SourceLocation *AtLocs,
       p += S->getByteLength();
       delete S;
     }
-    S = new StringLiteral(strBuf, Length,
-                          isWide, Context.getPointerType(Context.CharTy),
-                          AtLoc, EndLoc);
+    S = new (Context, 8) StringLiteral(Context, strBuf, Length, isWide,
+                                       Context.getPointerType(Context.CharTy),
+                                       AtLoc, EndLoc);
   }
   
   if (CheckBuiltinCFStringArgument(S))