-
Notifications
You must be signed in to change notification settings - Fork 14.4k
[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
Conversation
Also change the constructor and `StringLiteral::Create` to take an `ArrayRef<SourceLocation>` instead of a pointer and size, so that it can be directly passed to `llvm::copy`. This also eliminates the need of a specialized single location constructor and also simplifies existing calls to `StringLiteral::Create`.
@llvm/pr-subscribers-clang Author: Rahul Joshi (jurahul) ChangesAlso change the constructor and Full diff: https://github.com/llvm/llvm-project/pull/145187.diff 5 Files Affected:
diff --git a/clang/include/clang/AST/Expr.h b/clang/include/clang/AST/Expr.h
index 41e50359962ee..37ddcdf068148 100644
--- a/clang/include/clang/AST/Expr.h
+++ b/clang/include/clang/AST/Expr.h
@@ -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,
@@ -1853,18 +1852,11 @@ class StringLiteral final
public:
/// This is the "fully general" constructor that allows representation of
- /// strings formed from multiple concatenated tokens.
+ /// strings formed from multiple concatenated tokens. This same constructor
+ /// also work for a single token case.
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,
diff --git a/clang/lib/AST/ASTImporter.cpp b/clang/lib/AST/ASTImporter.cpp
index 4621ebb854d8e..3e4be21adb6bb 100644
--- a/clang/lib/AST/ASTImporter.cpp
+++ b/clang/lib/AST/ASTImporter.cpp
@@ -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) {
diff --git a/clang/lib/AST/Expr.cpp b/clang/lib/AST/Expr.cpp
index c3722c65abf6e..0914374b7ffdc 100644
--- a/clang/lib/AST/Expr.cpp
+++ b/clang/lib/AST/Expr.cpp
@@ -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) &&
@@ -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);
}
@@ -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,
@@ -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(
diff --git a/clang/lib/Sema/SemaExpr.cpp b/clang/lib/Sema/SemaExpr.cpp
index fc2819458a4ff..7307b01bb2df2 100644
--- a/clang/lib/Sema/SemaExpr.cpp
+++ b/clang/lib/Sema/SemaExpr.cpp
@@ -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 =
@@ -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;
@@ -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);
}
diff --git a/clang/lib/Sema/SemaExprObjC.cpp b/clang/lib/Sema/SemaExprObjC.cpp
index 395f2f340dbd6..e0662d82914f4 100644
--- a/clang/lib/Sema/SemaExprObjC.cpp
+++ b/clang/lib/Sema/SemaExprObjC.cpp
@@ -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);
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice cleanup, thanks!
Co-authored-by: Sirraide <[email protected]>
Also change the constructor and
StringLiteral::Create
to take anArrayRef<SourceLocation>
instead of a pointer and size, so that it can be directly passed tollvm::copy
. This also eliminates the need of a specialized single location constructor and also simplifies existing calls toStringLiteral::Create
.