Skip to content

[NFC][Clang][AST] Use llvm::copy instead of memcpy in StringLiteral #145187

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 23, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 3 additions & 12 deletions clang/include/clang/AST/Expr.h
Original file line number Diff line number Diff line change
Expand Up @@ -1834,8 +1834,7 @@ class StringLiteral final

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

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

public:
/// This is the "fully general" constructor that allows representation of
/// strings formed from multiple concatenated tokens.
/// strings formed from one or more concatenated tokens.
static StringLiteral *Create(const ASTContext &Ctx, StringRef Str,
StringLiteralKind Kind, bool Pascal, QualType Ty,
const SourceLocation *Loc,
unsigned NumConcatenated);

/// Simple constructor for string literals made from one token.
static StringLiteral *Create(const ASTContext &Ctx, StringRef Str,
StringLiteralKind Kind, bool Pascal, QualType Ty,
SourceLocation Loc) {
return Create(Ctx, Str, Kind, Pascal, Ty, &Loc, 1);
}
ArrayRef<SourceLocation> Locs);

/// Construct an empty string literal.
static StringLiteral *CreateEmpty(const ASTContext &Ctx,
Expand Down
6 changes: 3 additions & 3 deletions clang/lib/AST/ASTImporter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7695,9 +7695,9 @@ ExpectedStmt ASTNodeImporter::VisitStringLiteral(StringLiteral *E) {
E->tokloc_begin(), E->tokloc_end(), ToLocations.begin()))
return std::move(Err);

return StringLiteral::Create(
Importer.getToContext(), E->getBytes(), E->getKind(), E->isPascal(),
*ToTypeOrErr, ToLocations.data(), ToLocations.size());
return StringLiteral::Create(Importer.getToContext(), E->getBytes(),
E->getKind(), E->isPascal(), *ToTypeOrErr,
ToLocations);
}

ExpectedStmt ASTNodeImporter::VisitCompoundLiteralExpr(CompoundLiteralExpr *E) {
Expand Down
21 changes: 9 additions & 12 deletions clang/lib/AST/Expr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1123,14 +1123,13 @@ unsigned StringLiteral::mapCharByteWidth(TargetInfo const &Target,

StringLiteral::StringLiteral(const ASTContext &Ctx, StringRef Str,
StringLiteralKind Kind, bool Pascal, QualType Ty,
const SourceLocation *Loc,
unsigned NumConcatenated)
ArrayRef<SourceLocation> Locs)
: Expr(StringLiteralClass, Ty, VK_LValue, OK_Ordinary) {

unsigned Length = Str.size();

StringLiteralBits.Kind = llvm::to_underlying(Kind);
StringLiteralBits.NumConcatenated = NumConcatenated;
StringLiteralBits.NumConcatenated = Locs.size();

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

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

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

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

StringLiteral *StringLiteral::Create(const ASTContext &Ctx, StringRef Str,
StringLiteralKind Kind, bool Pascal,
QualType Ty, const SourceLocation *Loc,
unsigned NumConcatenated) {
QualType Ty,
ArrayRef<SourceLocation> Locs) {
void *Mem = Ctx.Allocate(totalSizeToAlloc<unsigned, SourceLocation, char>(
1, NumConcatenated, Str.size()),
1, Locs.size(), Str.size()),
alignof(StringLiteral));
return new (Mem)
StringLiteral(Ctx, Str, Kind, Pascal, Ty, Loc, NumConcatenated);
return new (Mem) StringLiteral(Ctx, Str, Kind, Pascal, Ty, Locs);
}

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

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

GenericSelectionExpr::GenericSelectionExpr(
Expand Down
14 changes: 6 additions & 8 deletions clang/lib/Sema/SemaExpr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2070,9 +2070,9 @@ ExprResult Sema::ActOnUnevaluatedStringLiteral(ArrayRef<Token> StringToks) {
for (const Token &Tok : StringToks)
StringTokLocs.push_back(Tok.getLocation());

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

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

// Pass &StringTokLocs[0], StringTokLocs.size() to factory!
StringLiteral *Lit = StringLiteral::Create(Context, Literal.GetString(),
Kind, Literal.Pascal, StrTy,
&StringTokLocs[0],
StringTokLocs.size());
StringLiteral *Lit = StringLiteral::Create(
Context, Literal.GetString(), Kind, Literal.Pascal, StrTy, StringTokLocs);
if (Literal.getUDSuffix().empty())
return Lit;

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

Expand Down
3 changes: 1 addition & 2 deletions clang/lib/Sema/SemaExprObjC.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,7 @@ ExprResult SemaObjC::ParseObjCStringLiteral(SourceLocation *AtLocs,
CAT->getElementType(), llvm::APInt(32, StrBuf.size() + 1), nullptr,
CAT->getSizeModifier(), CAT->getIndexTypeCVRQualifiers());
S = StringLiteral::Create(Context, StrBuf, StringLiteralKind::Ordinary,
/*Pascal=*/false, StrTy, &StrLocs[0],
StrLocs.size());
/*Pascal=*/false, StrTy, StrLocs);
}

return BuildObjCStringLiteral(AtLocs[0], S);
Expand Down
Loading