Skip to content

Commit 691a270

Browse files
jurahulJaddyen
authored andcommitted
[NFC][Clang][AST] Use llvm::copy instead of memcpy in StringLiteral (llvm#145187)
1 parent 82311df commit 691a270

File tree

5 files changed

+22
-37
lines changed

5 files changed

+22
-37
lines changed

clang/include/clang/AST/Expr.h

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1834,8 +1834,7 @@ class StringLiteral final
18341834

18351835
/// Build a string literal.
18361836
StringLiteral(const ASTContext &Ctx, StringRef Str, StringLiteralKind Kind,
1837-
bool Pascal, QualType Ty, const SourceLocation *Loc,
1838-
unsigned NumConcatenated);
1837+
bool Pascal, QualType Ty, ArrayRef<SourceLocation> Locs);
18391838

18401839
/// Build an empty string literal.
18411840
StringLiteral(EmptyShell Empty, unsigned NumConcatenated, unsigned Length,
@@ -1853,18 +1852,10 @@ class StringLiteral final
18531852

18541853
public:
18551854
/// This is the "fully general" constructor that allows representation of
1856-
/// strings formed from multiple concatenated tokens.
1855+
/// strings formed from one or more concatenated tokens.
18571856
static StringLiteral *Create(const ASTContext &Ctx, StringRef Str,
18581857
StringLiteralKind Kind, bool Pascal, QualType Ty,
1859-
const SourceLocation *Loc,
1860-
unsigned NumConcatenated);
1861-
1862-
/// Simple constructor for string literals made from one token.
1863-
static StringLiteral *Create(const ASTContext &Ctx, StringRef Str,
1864-
StringLiteralKind Kind, bool Pascal, QualType Ty,
1865-
SourceLocation Loc) {
1866-
return Create(Ctx, Str, Kind, Pascal, Ty, &Loc, 1);
1867-
}
1858+
ArrayRef<SourceLocation> Locs);
18681859

18691860
/// Construct an empty string literal.
18701861
static StringLiteral *CreateEmpty(const ASTContext &Ctx,

clang/lib/AST/ASTImporter.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7695,9 +7695,9 @@ ExpectedStmt ASTNodeImporter::VisitStringLiteral(StringLiteral *E) {
76957695
E->tokloc_begin(), E->tokloc_end(), ToLocations.begin()))
76967696
return std::move(Err);
76977697

7698-
return StringLiteral::Create(
7699-
Importer.getToContext(), E->getBytes(), E->getKind(), E->isPascal(),
7700-
*ToTypeOrErr, ToLocations.data(), ToLocations.size());
7698+
return StringLiteral::Create(Importer.getToContext(), E->getBytes(),
7699+
E->getKind(), E->isPascal(), *ToTypeOrErr,
7700+
ToLocations);
77017701
}
77027702

77037703
ExpectedStmt ASTNodeImporter::VisitCompoundLiteralExpr(CompoundLiteralExpr *E) {

clang/lib/AST/Expr.cpp

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1123,14 +1123,13 @@ unsigned StringLiteral::mapCharByteWidth(TargetInfo const &Target,
11231123

11241124
StringLiteral::StringLiteral(const ASTContext &Ctx, StringRef Str,
11251125
StringLiteralKind Kind, bool Pascal, QualType Ty,
1126-
const SourceLocation *Loc,
1127-
unsigned NumConcatenated)
1126+
ArrayRef<SourceLocation> Locs)
11281127
: Expr(StringLiteralClass, Ty, VK_LValue, OK_Ordinary) {
11291128

11301129
unsigned Length = Str.size();
11311130

11321131
StringLiteralBits.Kind = llvm::to_underlying(Kind);
1133-
StringLiteralBits.NumConcatenated = NumConcatenated;
1132+
StringLiteralBits.NumConcatenated = Locs.size();
11341133

11351134
if (Kind != StringLiteralKind::Unevaluated) {
11361135
assert(Ctx.getAsConstantArrayType(Ty) &&
@@ -1169,11 +1168,10 @@ StringLiteral::StringLiteral(const ASTContext &Ctx, StringRef Str,
11691168

11701169
// Initialize the trailing array of SourceLocation.
11711170
// This is safe since SourceLocation is POD-like.
1172-
std::memcpy(getTrailingObjects<SourceLocation>(), Loc,
1173-
NumConcatenated * sizeof(SourceLocation));
1171+
llvm::copy(Locs, getTrailingObjects<SourceLocation>());
11741172

11751173
// Initialize the trailing array of char holding the string data.
1176-
std::memcpy(getTrailingObjects<char>(), Str.data(), Str.size());
1174+
llvm::copy(Str, getTrailingObjects<char>());
11771175

11781176
setDependence(ExprDependence::None);
11791177
}
@@ -1188,13 +1186,12 @@ StringLiteral::StringLiteral(EmptyShell Empty, unsigned NumConcatenated,
11881186

11891187
StringLiteral *StringLiteral::Create(const ASTContext &Ctx, StringRef Str,
11901188
StringLiteralKind Kind, bool Pascal,
1191-
QualType Ty, const SourceLocation *Loc,
1192-
unsigned NumConcatenated) {
1189+
QualType Ty,
1190+
ArrayRef<SourceLocation> Locs) {
11931191
void *Mem = Ctx.Allocate(totalSizeToAlloc<unsigned, SourceLocation, char>(
1194-
1, NumConcatenated, Str.size()),
1192+
1, Locs.size(), Str.size()),
11951193
alignof(StringLiteral));
1196-
return new (Mem)
1197-
StringLiteral(Ctx, Str, Kind, Pascal, Ty, Loc, NumConcatenated);
1194+
return new (Mem) StringLiteral(Ctx, Str, Kind, Pascal, Ty, Locs);
11981195
}
11991196

12001197
StringLiteral *StringLiteral::CreateEmpty(const ASTContext &Ctx,
@@ -4406,7 +4403,7 @@ void ShuffleVectorExpr::setExprs(const ASTContext &C, ArrayRef<Expr *> Exprs) {
44064403

44074404
this->ShuffleVectorExprBits.NumExprs = Exprs.size();
44084405
SubExprs = new (C) Stmt *[ShuffleVectorExprBits.NumExprs];
4409-
memcpy(SubExprs, Exprs.data(), sizeof(Expr *) * Exprs.size());
4406+
llvm::copy(Exprs, SubExprs);
44104407
}
44114408

44124409
GenericSelectionExpr::GenericSelectionExpr(

clang/lib/Sema/SemaExpr.cpp

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2070,9 +2070,9 @@ ExprResult Sema::ActOnUnevaluatedStringLiteral(ArrayRef<Token> StringToks) {
20702070
for (const Token &Tok : StringToks)
20712071
StringTokLocs.push_back(Tok.getLocation());
20722072

2073-
StringLiteral *Lit = StringLiteral::Create(
2074-
Context, Literal.GetString(), StringLiteralKind::Unevaluated, false, {},
2075-
&StringTokLocs[0], StringTokLocs.size());
2073+
StringLiteral *Lit = StringLiteral::Create(Context, Literal.GetString(),
2074+
StringLiteralKind::Unevaluated,
2075+
false, {}, StringTokLocs);
20762076

20772077
if (!Literal.getUDSuffix().empty()) {
20782078
SourceLocation UDSuffixLoc =
@@ -2206,10 +2206,8 @@ Sema::ActOnStringLiteral(ArrayRef<Token> StringToks, Scope *UDLScope) {
22062206
Context.getStringLiteralArrayType(CharTy, Literal.GetNumStringChars());
22072207

22082208
// Pass &StringTokLocs[0], StringTokLocs.size() to factory!
2209-
StringLiteral *Lit = StringLiteral::Create(Context, Literal.GetString(),
2210-
Kind, Literal.Pascal, StrTy,
2211-
&StringTokLocs[0],
2212-
StringTokLocs.size());
2209+
StringLiteral *Lit = StringLiteral::Create(
2210+
Context, Literal.GetString(), Kind, Literal.Pascal, StrTy, StringTokLocs);
22132211
if (Literal.getUDSuffix().empty())
22142212
return Lit;
22152213

@@ -3793,7 +3791,7 @@ ExprResult Sema::ActOnNumericConstant(const Token &Tok, Scope *UDLScope) {
37933791
Expr *Lit =
37943792
StringLiteral::Create(Context, StringRef(TokSpelling.data(), Length),
37953793
StringLiteralKind::Ordinary,
3796-
/*Pascal*/ false, StrTy, &TokLoc, 1);
3794+
/*Pascal*/ false, StrTy, TokLoc);
37973795
return BuildLiteralOperatorCall(R, OpNameInfo, Lit, TokLoc);
37983796
}
37993797

clang/lib/Sema/SemaExprObjC.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,7 @@ ExprResult SemaObjC::ParseObjCStringLiteral(SourceLocation *AtLocs,
7474
CAT->getElementType(), llvm::APInt(32, StrBuf.size() + 1), nullptr,
7575
CAT->getSizeModifier(), CAT->getIndexTypeCVRQualifiers());
7676
S = StringLiteral::Create(Context, StrBuf, StringLiteralKind::Ordinary,
77-
/*Pascal=*/false, StrTy, &StrLocs[0],
78-
StrLocs.size());
77+
/*Pascal=*/false, StrTy, StrLocs);
7978
}
8079

8180
return BuildObjCStringLiteral(AtLocs[0], S);

0 commit comments

Comments
 (0)