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
diff --git a/lib/AST/Expr.cpp b/lib/AST/Expr.cpp
index 81da444..e436a41 100644
--- a/lib/AST/Expr.cpp
+++ b/lib/AST/Expr.cpp
@@ -37,35 +37,29 @@
return V.convertToDouble();
}
-
-StringLiteral::StringLiteral(ASTContext& C, const char *strData,
- unsigned byteLength, bool Wide, QualType Ty,
- SourceLocation Loc) :
- 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;
- NumConcatenated = 1;
-}
+ 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;
-StringLiteral::StringLiteral(ASTContext &C, const char *strData,
- unsigned byteLength, bool Wide, QualType Ty,
- SourceLocation *Loc, unsigned NumStrs) :
- 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[0];
- 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 @@
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 074969e..e1d85aa 100644
--- a/lib/AST/StmtSerialization.cpp
+++ b/lib/AST/StmtSerialization.cpp
@@ -971,8 +971,8 @@
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)