diff --git a/Driver/RewriteObjC.cpp b/Driver/RewriteObjC.cpp index f860433c29..4e262963a9 100644 --- a/Driver/RewriteObjC.cpp +++ b/Driver/RewriteObjC.cpp @@ -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 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 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 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 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 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 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], diff --git a/include/clang/AST/Expr.h b/include/clang/AST/Expr.h index c933a0c94b..3884d7dc2b 100644 --- a/include/clang/AST/Expr.h +++ b/include/clang/AST/Expr.h @@ -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; } diff --git a/lib/AST/Expr.cpp b/lib/AST/Expr.cpp index ce13c34012..d627ab03b2 100644 --- a/lib/AST/Expr.cpp +++ b/lib/AST/Expr.cpp @@ -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(StrData)); + this->~StringLiteral(); } bool UnaryOperator::isPostfix(Opcode Op) { diff --git a/lib/AST/StmtSerialization.cpp b/lib/AST/StmtSerialization.cpp index 3bb98b46f4..8d6636d6a6 100644 --- a/lib/AST/StmtSerialization.cpp +++ b/lib/AST/StmtSerialization.cpp @@ -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(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(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(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(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(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*[NumArgs+1]; D.BatchReadOwnedPtrs(NumArgs+1, SubExprs, C); - return new CallExpr(SC, SubExprs,NumArgs,t,L); + return new (C, llvm::alignof()) 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(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(C); - return new CStyleCastExpr(t,Op,writtenTy,LPLoc,RPLoc); + return new (C, llvm::alignof()) + 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(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(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(C); - return new CompoundLiteralExpr(L, Q, Init, fileScope); + return new (C, llvm::alignof()) + 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(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(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(C, NULL, 0, isWide, t, firstTokLoc, lastTokLoc); - char* StrData = new char[ByteLength]; + char* StrData = new (C, llvm::alignof()) char[ByteLength]; for (unsigned i = 0; i < ByteLength; ++i) StrData[i] = (char) D.ReadInt(); diff --git a/lib/Sema/SemaExpr.cpp b/lib/Sema/SemaExpr.cpp index 3ee6e52e2e..8c8207a39d 100644 --- a/lib/Sema/SemaExpr.cpp +++ b/lib/Sema/SemaExpr.cpp @@ -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(), diff --git a/lib/Sema/SemaExprObjC.cpp b/lib/Sema/SemaExprObjC.cpp index 983231ccf4..003e121f37 100644 --- a/lib/Sema/SemaExprObjC.cpp +++ b/lib/Sema/SemaExprObjC.cpp @@ -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))