[clang] 0998e3c - [clang] Add method 'backupStr' to ASTContext (#99417)
via cfe-commits
cfe-commits at lists.llvm.org
Tue Jul 23 01:13:08 PDT 2024
Author: Youngsuk Kim
Date: 2024-07-23T04:13:04-04:00
New Revision: 0998e3c4e66ded6e42b8ce162748df05b5ddb627
URL: https://github.com/llvm/llvm-project/commit/0998e3c4e66ded6e42b8ce162748df05b5ddb627
DIFF: https://github.com/llvm/llvm-project/commit/0998e3c4e66ded6e42b8ce162748df05b5ddb627.diff
LOG: [clang] Add method 'backupStr' to ASTContext (#99417)
Method 'backupStr' extracts common code of dynamically allocating memory
with ASTContext to hold a copy of a string's contents.
Added:
Modified:
clang/include/clang/AST/ASTContext.h
clang/lib/AST/ASTConcept.cpp
clang/lib/Sema/SemaTemplateInstantiate.cpp
clang/lib/Serialization/ASTReaderStmt.cpp
Removed:
################################################################################
diff --git a/clang/include/clang/AST/ASTContext.h b/clang/include/clang/AST/ASTContext.h
index f2a17a56dc201..6d1c8ca8a2f96 100644
--- a/clang/include/clang/AST/ASTContext.h
+++ b/clang/include/clang/AST/ASTContext.h
@@ -738,6 +738,12 @@ class ASTContext : public RefCountedBase<ASTContext> {
}
void Deallocate(void *Ptr) const {}
+ llvm::StringRef backupStr(llvm::StringRef S) const {
+ char *Buf = new (*this) char[S.size()];
+ std::copy(S.begin(), S.end(), Buf);
+ return llvm::StringRef(Buf, S.size());
+ }
+
/// Allocates a \c DeclListNode or returns one from the \c ListNodeFreeList
/// pool.
DeclListNode *AllocateDeclListNode(clang::NamedDecl *ND) {
diff --git a/clang/lib/AST/ASTConcept.cpp b/clang/lib/AST/ASTConcept.cpp
index 95e7ac1a3d775..d8efbe44dbecb 100644
--- a/clang/lib/AST/ASTConcept.cpp
+++ b/clang/lib/AST/ASTConcept.cpp
@@ -28,11 +28,9 @@ CreateUnsatisfiedConstraintRecord(const ASTContext &C,
else {
auto &SubstitutionDiagnostic =
*Detail.get<std::pair<SourceLocation, StringRef> *>();
- unsigned MessageSize = SubstitutionDiagnostic.second.size();
- char *Mem = new (C) char[MessageSize];
- memcpy(Mem, SubstitutionDiagnostic.second.data(), MessageSize);
+ StringRef Message = C.backupStr(SubstitutionDiagnostic.second);
auto *NewSubstDiag = new (C) std::pair<SourceLocation, StringRef>(
- SubstitutionDiagnostic.first, StringRef(Mem, MessageSize));
+ SubstitutionDiagnostic.first, Message);
new (TrailingObject) UnsatisfiedConstraintRecord(NewSubstDiag);
}
}
diff --git a/clang/lib/Sema/SemaTemplateInstantiate.cpp b/clang/lib/Sema/SemaTemplateInstantiate.cpp
index 725b62db5e80a..8995d461362d7 100644
--- a/clang/lib/Sema/SemaTemplateInstantiate.cpp
+++ b/clang/lib/Sema/SemaTemplateInstantiate.cpp
@@ -2576,16 +2576,12 @@ createSubstDiag(Sema &S, TemplateDeductionInfo &Info,
} else {
ErrorLoc = Info.getLocation();
}
- char *MessageBuf = new (S.Context) char[Message.size()];
- std::copy(Message.begin(), Message.end(), MessageBuf);
SmallString<128> Entity;
llvm::raw_svector_ostream OS(Entity);
Printer(OS);
- char *EntityBuf = new (S.Context) char[Entity.size()];
- std::copy(Entity.begin(), Entity.end(), EntityBuf);
- return new (S.Context) concepts::Requirement::SubstitutionDiagnostic{
- StringRef(EntityBuf, Entity.size()), ErrorLoc,
- StringRef(MessageBuf, Message.size())};
+ const ASTContext &C = S.Context;
+ return new (C) concepts::Requirement::SubstitutionDiagnostic{
+ C.backupStr(Entity), ErrorLoc, C.backupStr(Message)};
}
concepts::Requirement::SubstitutionDiagnostic *
@@ -2594,10 +2590,9 @@ concepts::createSubstDiagAt(Sema &S, SourceLocation Location,
SmallString<128> Entity;
llvm::raw_svector_ostream OS(Entity);
Printer(OS);
- char *EntityBuf = new (S.Context) char[Entity.size()];
- llvm::copy(Entity, EntityBuf);
- return new (S.Context) concepts::Requirement::SubstitutionDiagnostic{
- /*SubstitutedEntity=*/StringRef(EntityBuf, Entity.size()),
+ const ASTContext &C = S.Context;
+ return new (C) concepts::Requirement::SubstitutionDiagnostic{
+ /*SubstitutedEntity=*/C.backupStr(Entity),
/*DiagLoc=*/Location, /*DiagMessage=*/StringRef()};
}
@@ -2773,23 +2768,21 @@ TemplateInstantiator::TransformNestedRequirement(
assert(!Trap.hasErrorOccurred() && "Substitution failures must be handled "
"by CheckConstraintSatisfaction.");
}
+ ASTContext &C = SemaRef.Context;
if (TransConstraint.isUsable() &&
TransConstraint.get()->isInstantiationDependent())
- return new (SemaRef.Context)
- concepts::NestedRequirement(TransConstraint.get());
+ return new (C) concepts::NestedRequirement(TransConstraint.get());
if (TransConstraint.isInvalid() || !TransConstraint.get() ||
Satisfaction.HasSubstitutionFailure()) {
SmallString<128> Entity;
llvm::raw_svector_ostream OS(Entity);
Req->getConstraintExpr()->printPretty(OS, nullptr,
SemaRef.getPrintingPolicy());
- char *EntityBuf = new (SemaRef.Context) char[Entity.size()];
- std::copy(Entity.begin(), Entity.end(), EntityBuf);
- return new (SemaRef.Context) concepts::NestedRequirement(
- SemaRef.Context, StringRef(EntityBuf, Entity.size()), Satisfaction);
+ return new (C) concepts::NestedRequirement(
+ SemaRef.Context, C.backupStr(Entity), Satisfaction);
}
- return new (SemaRef.Context) concepts::NestedRequirement(
- SemaRef.Context, TransConstraint.get(), Satisfaction);
+ return new (C)
+ concepts::NestedRequirement(C, TransConstraint.get(), Satisfaction);
}
TypeSourceInfo *Sema::SubstType(TypeSourceInfo *T,
diff --git a/clang/lib/Serialization/ASTReaderStmt.cpp b/clang/lib/Serialization/ASTReaderStmt.cpp
index a7fe20bd0a466..c1361efd8c5f2 100644
--- a/clang/lib/Serialization/ASTReaderStmt.cpp
+++ b/clang/lib/Serialization/ASTReaderStmt.cpp
@@ -785,29 +785,22 @@ void ASTStmtReader::VisitUnaryExprOrTypeTraitExpr(UnaryExprOrTypeTraitExpr *E) {
E->setRParenLoc(readSourceLocation());
}
-static StringRef saveStrToCtx(const std::string &S, ASTContext &Ctx) {
- char *Buf = new (Ctx) char[S.size()];
- std::copy(S.begin(), S.end(), Buf);
- return StringRef(Buf, S.size());
-}
-
static ConstraintSatisfaction
readConstraintSatisfaction(ASTRecordReader &Record) {
ConstraintSatisfaction Satisfaction;
Satisfaction.IsSatisfied = Record.readInt();
Satisfaction.ContainsErrors = Record.readInt();
+ const ASTContext &C = Record.getContext();
if (!Satisfaction.IsSatisfied) {
unsigned NumDetailRecords = Record.readInt();
for (unsigned i = 0; i != NumDetailRecords; ++i) {
if (/* IsDiagnostic */Record.readInt()) {
SourceLocation DiagLocation = Record.readSourceLocation();
- StringRef DiagMessage =
- saveStrToCtx(Record.readString(), Record.getContext());
+ StringRef DiagMessage = C.backupStr(Record.readString());
Satisfaction.Details.emplace_back(
- new (Record.getContext())
- ConstraintSatisfaction::SubstitutionDiagnostic(DiagLocation,
- DiagMessage));
+ new (C) ConstraintSatisfaction::SubstitutionDiagnostic(
+ DiagLocation, DiagMessage));
} else
Satisfaction.Details.emplace_back(Record.readExpr());
}
@@ -828,12 +821,10 @@ void ASTStmtReader::VisitConceptSpecializationExpr(
static concepts::Requirement::SubstitutionDiagnostic *
readSubstitutionDiagnostic(ASTRecordReader &Record) {
- StringRef SubstitutedEntity =
- saveStrToCtx(Record.readString(), Record.getContext());
-
+ const ASTContext &C = Record.getContext();
+ StringRef SubstitutedEntity = C.backupStr(Record.readString());
SourceLocation DiagLoc = Record.readSourceLocation();
- StringRef DiagMessage =
- saveStrToCtx(Record.readString(), Record.getContext());
+ StringRef DiagMessage = C.backupStr(Record.readString());
return new (Record.getContext())
concepts::Requirement::SubstitutionDiagnostic{SubstitutedEntity, DiagLoc,
@@ -919,22 +910,21 @@ void ASTStmtReader::VisitRequiresExpr(RequiresExpr *E) {
std::move(*Req));
} break;
case concepts::Requirement::RK_Nested: {
+ ASTContext &C = Record.getContext();
bool HasInvalidConstraint = Record.readInt();
if (HasInvalidConstraint) {
- StringRef InvalidConstraint =
- saveStrToCtx(Record.readString(), Record.getContext());
- R = new (Record.getContext()) concepts::NestedRequirement(
+ StringRef InvalidConstraint = C.backupStr(Record.readString());
+ R = new (C) concepts::NestedRequirement(
Record.getContext(), InvalidConstraint,
readConstraintSatisfaction(Record));
break;
}
Expr *E = Record.readExpr();
if (E->isInstantiationDependent())
- R = new (Record.getContext()) concepts::NestedRequirement(E);
+ R = new (C) concepts::NestedRequirement(E);
else
- R = new (Record.getContext())
- concepts::NestedRequirement(Record.getContext(), E,
- readConstraintSatisfaction(Record));
+ R = new (C) concepts::NestedRequirement(
+ C, E, readConstraintSatisfaction(Record));
} break;
}
if (!R)
More information about the cfe-commits
mailing list