-
Notifications
You must be signed in to change notification settings - Fork 13.4k
[clang] Add method 'backupStr' to ASTContext #99417
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
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Method 'backupStr' extracts common code of manually allocating memory with ASTContext to hold a copy of a string's contents.
@llvm/pr-subscribers-clang-modules @llvm/pr-subscribers-clang Author: Youngsuk Kim (JOE1994) ChangesMethod 'backupStr' extracts common code of manually allocating memory with ASTContext to hold a copy of a string's contents. Full diff: https://github.com/llvm/llvm-project/pull/99417.diff 4 Files Affected:
diff --git a/clang/include/clang/AST/ASTContext.h b/clang/include/clang/AST/ASTContext.h
index 608bd90fcc3ff..50a1659b90388 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 a7bc6749c5852..e73fced86deeb 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 6955b42f14e06..4180d3225ff17 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)
|
yuxuanchen1997
pushed a commit
that referenced
this pull request
Jul 25, 2024
Summary: Method 'backupStr' extracts common code of dynamically allocating memory with ASTContext to hold a copy of a string's contents. Test Plan: Reviewers: Subscribers: Tasks: Tags: Differential Revision: https://phabricator.intern.facebook.com/D60251287
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Labels
clang:frontend
Language frontend issues, e.g. anything involving "Sema"
clang:modules
C++20 modules and Clang Header Modules
clang
Clang issues not falling into any other category
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Method 'backupStr' extracts common code of dynamically allocating memory with ASTContext to hold a copy of a string's contents.