Skip to content

Commit 0998e3c

Browse files
authored
[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.
1 parent 2d2d685 commit 0998e3c

File tree

4 files changed

+33
-46
lines changed

4 files changed

+33
-46
lines changed

clang/include/clang/AST/ASTContext.h

+6
Original file line numberDiff line numberDiff line change
@@ -738,6 +738,12 @@ class ASTContext : public RefCountedBase<ASTContext> {
738738
}
739739
void Deallocate(void *Ptr) const {}
740740

741+
llvm::StringRef backupStr(llvm::StringRef S) const {
742+
char *Buf = new (*this) char[S.size()];
743+
std::copy(S.begin(), S.end(), Buf);
744+
return llvm::StringRef(Buf, S.size());
745+
}
746+
741747
/// Allocates a \c DeclListNode or returns one from the \c ListNodeFreeList
742748
/// pool.
743749
DeclListNode *AllocateDeclListNode(clang::NamedDecl *ND) {

clang/lib/AST/ASTConcept.cpp

+2-4
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,9 @@ CreateUnsatisfiedConstraintRecord(const ASTContext &C,
2828
else {
2929
auto &SubstitutionDiagnostic =
3030
*Detail.get<std::pair<SourceLocation, StringRef> *>();
31-
unsigned MessageSize = SubstitutionDiagnostic.second.size();
32-
char *Mem = new (C) char[MessageSize];
33-
memcpy(Mem, SubstitutionDiagnostic.second.data(), MessageSize);
31+
StringRef Message = C.backupStr(SubstitutionDiagnostic.second);
3432
auto *NewSubstDiag = new (C) std::pair<SourceLocation, StringRef>(
35-
SubstitutionDiagnostic.first, StringRef(Mem, MessageSize));
33+
SubstitutionDiagnostic.first, Message);
3634
new (TrailingObject) UnsatisfiedConstraintRecord(NewSubstDiag);
3735
}
3836
}

clang/lib/Sema/SemaTemplateInstantiate.cpp

+12-19
Original file line numberDiff line numberDiff line change
@@ -2576,16 +2576,12 @@ createSubstDiag(Sema &S, TemplateDeductionInfo &Info,
25762576
} else {
25772577
ErrorLoc = Info.getLocation();
25782578
}
2579-
char *MessageBuf = new (S.Context) char[Message.size()];
2580-
std::copy(Message.begin(), Message.end(), MessageBuf);
25812579
SmallString<128> Entity;
25822580
llvm::raw_svector_ostream OS(Entity);
25832581
Printer(OS);
2584-
char *EntityBuf = new (S.Context) char[Entity.size()];
2585-
std::copy(Entity.begin(), Entity.end(), EntityBuf);
2586-
return new (S.Context) concepts::Requirement::SubstitutionDiagnostic{
2587-
StringRef(EntityBuf, Entity.size()), ErrorLoc,
2588-
StringRef(MessageBuf, Message.size())};
2582+
const ASTContext &C = S.Context;
2583+
return new (C) concepts::Requirement::SubstitutionDiagnostic{
2584+
C.backupStr(Entity), ErrorLoc, C.backupStr(Message)};
25892585
}
25902586

25912587
concepts::Requirement::SubstitutionDiagnostic *
@@ -2594,10 +2590,9 @@ concepts::createSubstDiagAt(Sema &S, SourceLocation Location,
25942590
SmallString<128> Entity;
25952591
llvm::raw_svector_ostream OS(Entity);
25962592
Printer(OS);
2597-
char *EntityBuf = new (S.Context) char[Entity.size()];
2598-
llvm::copy(Entity, EntityBuf);
2599-
return new (S.Context) concepts::Requirement::SubstitutionDiagnostic{
2600-
/*SubstitutedEntity=*/StringRef(EntityBuf, Entity.size()),
2593+
const ASTContext &C = S.Context;
2594+
return new (C) concepts::Requirement::SubstitutionDiagnostic{
2595+
/*SubstitutedEntity=*/C.backupStr(Entity),
26012596
/*DiagLoc=*/Location, /*DiagMessage=*/StringRef()};
26022597
}
26032598

@@ -2773,23 +2768,21 @@ TemplateInstantiator::TransformNestedRequirement(
27732768
assert(!Trap.hasErrorOccurred() && "Substitution failures must be handled "
27742769
"by CheckConstraintSatisfaction.");
27752770
}
2771+
ASTContext &C = SemaRef.Context;
27762772
if (TransConstraint.isUsable() &&
27772773
TransConstraint.get()->isInstantiationDependent())
2778-
return new (SemaRef.Context)
2779-
concepts::NestedRequirement(TransConstraint.get());
2774+
return new (C) concepts::NestedRequirement(TransConstraint.get());
27802775
if (TransConstraint.isInvalid() || !TransConstraint.get() ||
27812776
Satisfaction.HasSubstitutionFailure()) {
27822777
SmallString<128> Entity;
27832778
llvm::raw_svector_ostream OS(Entity);
27842779
Req->getConstraintExpr()->printPretty(OS, nullptr,
27852780
SemaRef.getPrintingPolicy());
2786-
char *EntityBuf = new (SemaRef.Context) char[Entity.size()];
2787-
std::copy(Entity.begin(), Entity.end(), EntityBuf);
2788-
return new (SemaRef.Context) concepts::NestedRequirement(
2789-
SemaRef.Context, StringRef(EntityBuf, Entity.size()), Satisfaction);
2781+
return new (C) concepts::NestedRequirement(
2782+
SemaRef.Context, C.backupStr(Entity), Satisfaction);
27902783
}
2791-
return new (SemaRef.Context) concepts::NestedRequirement(
2792-
SemaRef.Context, TransConstraint.get(), Satisfaction);
2784+
return new (C)
2785+
concepts::NestedRequirement(C, TransConstraint.get(), Satisfaction);
27932786
}
27942787

27952788
TypeSourceInfo *Sema::SubstType(TypeSourceInfo *T,

clang/lib/Serialization/ASTReaderStmt.cpp

+13-23
Original file line numberDiff line numberDiff line change
@@ -785,29 +785,22 @@ void ASTStmtReader::VisitUnaryExprOrTypeTraitExpr(UnaryExprOrTypeTraitExpr *E) {
785785
E->setRParenLoc(readSourceLocation());
786786
}
787787

788-
static StringRef saveStrToCtx(const std::string &S, ASTContext &Ctx) {
789-
char *Buf = new (Ctx) char[S.size()];
790-
std::copy(S.begin(), S.end(), Buf);
791-
return StringRef(Buf, S.size());
792-
}
793-
794788
static ConstraintSatisfaction
795789
readConstraintSatisfaction(ASTRecordReader &Record) {
796790
ConstraintSatisfaction Satisfaction;
797791
Satisfaction.IsSatisfied = Record.readInt();
798792
Satisfaction.ContainsErrors = Record.readInt();
793+
const ASTContext &C = Record.getContext();
799794
if (!Satisfaction.IsSatisfied) {
800795
unsigned NumDetailRecords = Record.readInt();
801796
for (unsigned i = 0; i != NumDetailRecords; ++i) {
802797
if (/* IsDiagnostic */Record.readInt()) {
803798
SourceLocation DiagLocation = Record.readSourceLocation();
804-
StringRef DiagMessage =
805-
saveStrToCtx(Record.readString(), Record.getContext());
799+
StringRef DiagMessage = C.backupStr(Record.readString());
806800

807801
Satisfaction.Details.emplace_back(
808-
new (Record.getContext())
809-
ConstraintSatisfaction::SubstitutionDiagnostic(DiagLocation,
810-
DiagMessage));
802+
new (C) ConstraintSatisfaction::SubstitutionDiagnostic(
803+
DiagLocation, DiagMessage));
811804
} else
812805
Satisfaction.Details.emplace_back(Record.readExpr());
813806
}
@@ -828,12 +821,10 @@ void ASTStmtReader::VisitConceptSpecializationExpr(
828821

829822
static concepts::Requirement::SubstitutionDiagnostic *
830823
readSubstitutionDiagnostic(ASTRecordReader &Record) {
831-
StringRef SubstitutedEntity =
832-
saveStrToCtx(Record.readString(), Record.getContext());
833-
824+
const ASTContext &C = Record.getContext();
825+
StringRef SubstitutedEntity = C.backupStr(Record.readString());
834826
SourceLocation DiagLoc = Record.readSourceLocation();
835-
StringRef DiagMessage =
836-
saveStrToCtx(Record.readString(), Record.getContext());
827+
StringRef DiagMessage = C.backupStr(Record.readString());
837828

838829
return new (Record.getContext())
839830
concepts::Requirement::SubstitutionDiagnostic{SubstitutedEntity, DiagLoc,
@@ -919,22 +910,21 @@ void ASTStmtReader::VisitRequiresExpr(RequiresExpr *E) {
919910
std::move(*Req));
920911
} break;
921912
case concepts::Requirement::RK_Nested: {
913+
ASTContext &C = Record.getContext();
922914
bool HasInvalidConstraint = Record.readInt();
923915
if (HasInvalidConstraint) {
924-
StringRef InvalidConstraint =
925-
saveStrToCtx(Record.readString(), Record.getContext());
926-
R = new (Record.getContext()) concepts::NestedRequirement(
916+
StringRef InvalidConstraint = C.backupStr(Record.readString());
917+
R = new (C) concepts::NestedRequirement(
927918
Record.getContext(), InvalidConstraint,
928919
readConstraintSatisfaction(Record));
929920
break;
930921
}
931922
Expr *E = Record.readExpr();
932923
if (E->isInstantiationDependent())
933-
R = new (Record.getContext()) concepts::NestedRequirement(E);
924+
R = new (C) concepts::NestedRequirement(E);
934925
else
935-
R = new (Record.getContext())
936-
concepts::NestedRequirement(Record.getContext(), E,
937-
readConstraintSatisfaction(Record));
926+
R = new (C) concepts::NestedRequirement(
927+
C, E, readConstraintSatisfaction(Record));
938928
} break;
939929
}
940930
if (!R)

0 commit comments

Comments
 (0)