aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorChris Lattner <sabre@nondot.org>2009-02-18 06:40:38 +0000
committerChris Lattner <sabre@nondot.org>2009-02-18 06:40:38 +0000
commit2085fd6cd22ec5c268175251db10d7c60caf7aaa (patch)
tree117d3027fce1afe6b0a10f81be576c8119e0faf8
parentc6666f8e9bbb7f31bf2e52f97137e738c4ca01d0 (diff)
privatize all of the string literal memory allocation/creation
stuff behind a private static function. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@64898 91177308-0d34-0410-b5e6-96231b3b80d8
-rw-r--r--Driver/RewriteObjC.cpp26
-rw-r--r--include/clang/AST/Expr.h26
-rw-r--r--lib/AST/Expr.cpp68
-rw-r--r--lib/AST/StmtSerialization.cpp4
-rw-r--r--lib/Sema/SemaExpr.cpp14
-rw-r--r--lib/Sema/SemaExprObjC.cpp6
6 files changed, 64 insertions, 80 deletions
diff --git a/Driver/RewriteObjC.cpp b/Driver/RewriteObjC.cpp
index 878a94c1f1..c6b9b43a9e 100644
--- a/Driver/RewriteObjC.cpp
+++ b/Driver/RewriteObjC.cpp
@@ -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()));
diff --git a/include/clang/AST/Expr.h b/include/clang/AST/Expr.h
index 8130f4f631..54167c9c37 100644
--- a/include/clang/AST/Expr.h
+++ b/include/clang/AST/Expr.h
@@ -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); }
diff --git a/lib/AST/Expr.cpp b/lib/AST/Expr.cpp
index 81da44407b..e436a41b55 100644
--- a/lib/AST/Expr.cpp
+++ b/lib/AST/Expr.cpp
@@ -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) {
diff --git a/lib/AST/StmtSerialization.cpp b/lib/AST/StmtSerialization.cpp
index 074969e9de..e1d85aa01c 100644
--- a/lib/AST/StmtSerialization.cpp
+++ b/lib/AST/StmtSerialization.cpp
@@ -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)
diff --git a/lib/Sema/SemaExpr.cpp b/lib/Sema/SemaExpr.cpp
index 48f338e475..b2df86abe6 100644
--- a/lib/Sema/SemaExpr.cpp
+++ b/lib/Sema/SemaExpr.cpp
@@ -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
diff --git a/lib/Sema/SemaExprObjC.cpp b/lib/Sema/SemaExprObjC.cpp
index 6e40b3c4a3..886a3ca1dc 100644
--- a/lib/Sema/SemaExprObjC.cpp
+++ b/lib/Sema/SemaExprObjC.cpp
@@ -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.