[cfe-commits] r63958 - 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

Ted Kremenek kremenek at apple.com
Fri Feb 6 11:55:15 PST 2009


Author: kremenek
Date: Fri Feb  6 13:55:15 2009
New Revision: 63958

URL: http://llvm.org/viewvc/llvm-project?rev=63958&view=rev
Log:
Move StringLiteral to allocate its internal string data using the allocator in
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).

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=63958&r1=63957&r2=63958&view=diff

==============================================================================
--- cfe/trunk/Driver/RewriteObjC.cpp (original)
+++ cfe/trunk/Driver/RewriteObjC.cpp Fri Feb  6 13:55:15 2009
@@ -1717,7 +1717,7 @@
   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 @@
   // 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 @@
             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 @@
     } 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 @@
       
       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 @@
   // 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 @@
   // 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], 

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

==============================================================================
--- cfe/trunk/include/clang/AST/Expr.h (original)
+++ cfe/trunk/include/clang/AST/Expr.h Fri Feb  6 13:55:15 2009
@@ -479,9 +479,10 @@
   // 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; }

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

==============================================================================
--- cfe/trunk/lib/AST/Expr.cpp (original)
+++ cfe/trunk/lib/AST/Expr.cpp Fri Feb  6 13:55:15 2009
@@ -38,12 +38,13 @@
 }
 
 
-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 @@
   lastTokLoc = lastLoc;
 }
 
-StringLiteral::~StringLiteral() {
-  delete[] StrData;
+void StringLiteral::Destroy(ASTContext &C) {
+  C.Deallocate(const_cast<char*>(StrData));
+  this->~StringLiteral();
 }
 
 bool UnaryOperator::isPostfix(Opcode Op) {

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

==============================================================================
--- cfe/trunk/lib/AST/StmtSerialization.cpp (original)
+++ cfe/trunk/lib/AST/StmtSerialization.cpp Fri Feb  6 13:55:15 2009
@@ -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 @@
   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 @@
   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 @@
   
   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 @@
   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 @@
 
 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 @@
   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 @@
 
 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 @@
   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 @@
   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 @@
   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 @@
   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 @@
   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 @@
                                                      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 @@
   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();
 

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

==============================================================================
--- cfe/trunk/lib/Sema/SemaExpr.cpp (original)
+++ cfe/trunk/lib/Sema/SemaExpr.cpp Fri Feb  6 13:55:15 2009
@@ -319,7 +319,7 @@
                                        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(),

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

==============================================================================
--- cfe/trunk/lib/Sema/SemaExprObjC.cpp (original)
+++ cfe/trunk/lib/Sema/SemaExprObjC.cpp Fri Feb  6 13:55:15 2009
@@ -40,9 +40,9 @@
       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))





More information about the cfe-commits mailing list