[libcxx-commits] [libcxxabi] 51f6caf - [demangler][NFC] Rename SwapAndRestore to ScopedOverride
Nathan Sidwell via libcxx-commits
libcxx-commits at lists.llvm.org
Wed Apr 6 12:55:45 PDT 2022
Author: Nathan Sidwell
Date: 2022-04-06T12:55:35-07:00
New Revision: 51f6caf2fbb5118569f2de33a85ffcc52568e0a7
URL: https://github.com/llvm/llvm-project/commit/51f6caf2fbb5118569f2de33a85ffcc52568e0a7
DIFF: https://github.com/llvm/llvm-project/commit/51f6caf2fbb5118569f2de33a85ffcc52568e0a7.diff
LOG: [demangler][NFC] Rename SwapAndRestore to ScopedOverride
The demangler has a utility class 'SwapAndRestore'. That name is
confusing. It's not swapping anything, and the restore part happens at
the object's destruction. What it's actually doing is allowing a
override of some value that is dynamically accessible within the
lifetime of a lexical scope. Thus rename it to ScopedOverride, and
tweak it's member variable names.
Reviewed By: dblaikie
Differential Revision: https://reviews.llvm.org/D122606
Added:
Modified:
libcxxabi/src/demangle/ItaniumDemangle.h
libcxxabi/src/demangle/Utility.h
llvm/include/llvm/Demangle/ItaniumDemangle.h
llvm/include/llvm/Demangle/Utility.h
llvm/lib/Demangle/RustDemangle.cpp
Removed:
################################################################################
diff --git a/libcxxabi/src/demangle/ItaniumDemangle.h b/libcxxabi/src/demangle/ItaniumDemangle.h
index fd6835ab9aba4..36fe26b44620c 100644
--- a/libcxxabi/src/demangle/ItaniumDemangle.h
+++ b/libcxxabi/src/demangle/ItaniumDemangle.h
@@ -656,7 +656,7 @@ class ReferenceType : public Node {
void printLeft(OutputBuffer &OB) const override {
if (Printing)
return;
- SwapAndRestore<bool> SavePrinting(Printing, true);
+ ScopedOverride<bool> SavePrinting(Printing, true);
std::pair<ReferenceKind, const Node *> Collapsed = collapse(OB);
if (!Collapsed.second)
return;
@@ -671,7 +671,7 @@ class ReferenceType : public Node {
void printRight(OutputBuffer &OB) const override {
if (Printing)
return;
- SwapAndRestore<bool> SavePrinting(Printing, true);
+ ScopedOverride<bool> SavePrinting(Printing, true);
std::pair<ReferenceKind, const Node *> Collapsed = collapse(OB);
if (!Collapsed.second)
return;
@@ -1180,7 +1180,7 @@ class TemplateTemplateParamDecl final : public Node {
template<typename Fn> void match(Fn F) const { F(Name, Params); }
void printLeft(OutputBuffer &OB) const override {
- SwapAndRestore<unsigned> LT(OB.GtIsGt, 0);
+ ScopedOverride<unsigned> LT(OB.GtIsGt, 0);
OB += "template<";
Params.printWithComma(OB);
OB += "> typename ";
@@ -1316,8 +1316,8 @@ class ParameterPackExpansion final : public Node {
void printLeft(OutputBuffer &OB) const override {
constexpr unsigned Max = std::numeric_limits<unsigned>::max();
- SwapAndRestore<unsigned> SavePackIdx(OB.CurrentPackIndex, Max);
- SwapAndRestore<unsigned> SavePackMax(OB.CurrentPackMax, Max);
+ ScopedOverride<unsigned> SavePackIdx(OB.CurrentPackIndex, Max);
+ ScopedOverride<unsigned> SavePackMax(OB.CurrentPackMax, Max);
size_t StreamPos = OB.getCurrentPosition();
// Print the first element in the pack. If Child contains a ParameterPack,
@@ -1358,7 +1358,7 @@ class TemplateArgs final : public Node {
NodeArray getParams() { return Params; }
void printLeft(OutputBuffer &OB) const override {
- SwapAndRestore<unsigned> LT(OB.GtIsGt, 0);
+ ScopedOverride<unsigned> LT(OB.GtIsGt, 0);
OB += "<";
Params.printWithComma(OB);
if (OB.back() == '>')
@@ -1408,38 +1408,38 @@ struct ForwardTemplateReference : Node {
bool hasRHSComponentSlow(OutputBuffer &OB) const override {
if (Printing)
return false;
- SwapAndRestore<bool> SavePrinting(Printing, true);
+ ScopedOverride<bool> SavePrinting(Printing, true);
return Ref->hasRHSComponent(OB);
}
bool hasArraySlow(OutputBuffer &OB) const override {
if (Printing)
return false;
- SwapAndRestore<bool> SavePrinting(Printing, true);
+ ScopedOverride<bool> SavePrinting(Printing, true);
return Ref->hasArray(OB);
}
bool hasFunctionSlow(OutputBuffer &OB) const override {
if (Printing)
return false;
- SwapAndRestore<bool> SavePrinting(Printing, true);
+ ScopedOverride<bool> SavePrinting(Printing, true);
return Ref->hasFunction(OB);
}
const Node *getSyntaxNode(OutputBuffer &OB) const override {
if (Printing)
return this;
- SwapAndRestore<bool> SavePrinting(Printing, true);
+ ScopedOverride<bool> SavePrinting(Printing, true);
return Ref->getSyntaxNode(OB);
}
void printLeft(OutputBuffer &OB) const override {
if (Printing)
return;
- SwapAndRestore<bool> SavePrinting(Printing, true);
+ ScopedOverride<bool> SavePrinting(Printing, true);
Ref->printLeft(OB);
}
void printRight(OutputBuffer &OB) const override {
if (Printing)
return;
- SwapAndRestore<bool> SavePrinting(Printing, true);
+ ScopedOverride<bool> SavePrinting(Printing, true);
Ref->printRight(OB);
}
};
@@ -1656,7 +1656,7 @@ class ClosureTypeName : public Node {
void printDeclarator(OutputBuffer &OB) const {
if (!TemplateParams.empty()) {
- SwapAndRestore<unsigned> LT(OB.GtIsGt, 0);
+ ScopedOverride<unsigned> LT(OB.GtIsGt, 0);
OB += "<";
TemplateParams.printWithComma(OB);
OB += ">";
@@ -1880,7 +1880,7 @@ class CastExpr : public Node {
void printLeft(OutputBuffer &OB) const override {
OB += CastKind;
{
- SwapAndRestore<unsigned> LT(OB.GtIsGt, 0);
+ ScopedOverride<unsigned> LT(OB.GtIsGt, 0);
OB += "<";
To->printLeft(OB);
if (OB.back() == '>')
@@ -2857,7 +2857,7 @@ AbstractManglingParser<Derived, Alloc>::parseUnnamedTypeName(NameState *State) {
return make<UnnamedTypeName>(Count);
}
if (consumeIf("Ul")) {
- SwapAndRestore<size_t> SwapParams(ParsingLambdaParamsAtLevel,
+ ScopedOverride<size_t> SwapParams(ParsingLambdaParamsAtLevel,
TemplateParams.size());
ScopedTemplateParamList LambdaTemplateParams(this);
@@ -3062,11 +3062,11 @@ AbstractManglingParser<Derived, Alloc>::parseOperatorName(NameState *State) {
if (const auto *Op = parseOperatorEncoding()) {
if (Op->getKind() == OperatorInfo::CCast) {
// ::= cv <type> # (cast)
- SwapAndRestore<bool> SaveTemplate(TryToParseTemplateArgs, false);
+ ScopedOverride<bool> SaveTemplate(TryToParseTemplateArgs, false);
// If we're parsing an encoding, State != nullptr and the conversion
// operators' <type> could have a <template-param> that refers to some
// <template-arg>s further ahead in the mangled name.
- SwapAndRestore<bool> SavePermit(PermitForwardTemplateReferences,
+ ScopedOverride<bool> SavePermit(PermitForwardTemplateReferences,
PermitForwardTemplateReferences ||
State != nullptr);
Node *Ty = getDerived().parseType();
@@ -3718,8 +3718,8 @@ Node *AbstractManglingParser<Derived, Alloc>::parseQualifiedType() {
StringView ProtoSourceName = Qual.dropFront(std::strlen("objcproto"));
StringView Proto;
{
- SwapAndRestore<const char *> SaveFirst(First, ProtoSourceName.begin()),
- SaveLast(Last, ProtoSourceName.end());
+ ScopedOverride<const char *> SaveFirst(First, ProtoSourceName.begin()),
+ SaveLast(Last, ProtoSourceName.end());
Proto = parseBareSourceName();
}
if (Proto.empty())
@@ -4199,7 +4199,7 @@ Node *AbstractManglingParser<Derived, Alloc>::parseConversionExpr() {
return nullptr;
Node *Ty;
{
- SwapAndRestore<bool> SaveTemp(TryToParseTemplateArgs, false);
+ ScopedOverride<bool> SaveTemp(TryToParseTemplateArgs, false);
Ty = getDerived().parseType();
}
@@ -4634,7 +4634,7 @@ Node *AbstractManglingParser<Derived, Alloc>::parseExpr() {
// C Cast: (type)expr
Node *Ty;
{
- SwapAndRestore<bool> SaveTemp(TryToParseTemplateArgs, false);
+ ScopedOverride<bool> SaveTemp(TryToParseTemplateArgs, false);
Ty = getDerived().parseType();
}
if (Ty == nullptr)
diff --git a/libcxxabi/src/demangle/Utility.h b/libcxxabi/src/demangle/Utility.h
index 1390f14b10d45..6e0fa38632c89 100644
--- a/libcxxabi/src/demangle/Utility.h
+++ b/libcxxabi/src/demangle/Utility.h
@@ -181,21 +181,20 @@ class OutputBuffer {
size_t getBufferCapacity() const { return BufferCapacity; }
};
-template <class T> class SwapAndRestore {
- T &Restore;
- T OriginalValue;
+template <class T> class ScopedOverride {
+ T &Loc;
+ T Original;
public:
- SwapAndRestore(T &Restore_) : SwapAndRestore(Restore_, Restore_) {}
+ ScopedOverride(T &Loc_) : ScopedOverride(Loc_, Loc_) {}
- SwapAndRestore(T &Restore_, T NewVal)
- : Restore(Restore_), OriginalValue(Restore) {
- Restore = std::move(NewVal);
+ ScopedOverride(T &Loc_, T NewVal) : Loc(Loc_), Original(Loc_) {
+ Loc_ = std::move(NewVal);
}
- ~SwapAndRestore() { Restore = std::move(OriginalValue); }
+ ~ScopedOverride() { Loc = std::move(Original); }
- SwapAndRestore(const SwapAndRestore &) = delete;
- SwapAndRestore &operator=(const SwapAndRestore &) = delete;
+ ScopedOverride(const ScopedOverride &) = delete;
+ ScopedOverride &operator=(const ScopedOverride &) = delete;
};
inline bool initializeOutputBuffer(char *Buf, size_t *N, OutputBuffer &OB,
diff --git a/llvm/include/llvm/Demangle/ItaniumDemangle.h b/llvm/include/llvm/Demangle/ItaniumDemangle.h
index f293fbce5aae5..10e13571fab3d 100644
--- a/llvm/include/llvm/Demangle/ItaniumDemangle.h
+++ b/llvm/include/llvm/Demangle/ItaniumDemangle.h
@@ -656,7 +656,7 @@ class ReferenceType : public Node {
void printLeft(OutputBuffer &OB) const override {
if (Printing)
return;
- SwapAndRestore<bool> SavePrinting(Printing, true);
+ ScopedOverride<bool> SavePrinting(Printing, true);
std::pair<ReferenceKind, const Node *> Collapsed = collapse(OB);
if (!Collapsed.second)
return;
@@ -671,7 +671,7 @@ class ReferenceType : public Node {
void printRight(OutputBuffer &OB) const override {
if (Printing)
return;
- SwapAndRestore<bool> SavePrinting(Printing, true);
+ ScopedOverride<bool> SavePrinting(Printing, true);
std::pair<ReferenceKind, const Node *> Collapsed = collapse(OB);
if (!Collapsed.second)
return;
@@ -1180,7 +1180,7 @@ class TemplateTemplateParamDecl final : public Node {
template<typename Fn> void match(Fn F) const { F(Name, Params); }
void printLeft(OutputBuffer &OB) const override {
- SwapAndRestore<unsigned> LT(OB.GtIsGt, 0);
+ ScopedOverride<unsigned> LT(OB.GtIsGt, 0);
OB += "template<";
Params.printWithComma(OB);
OB += "> typename ";
@@ -1316,8 +1316,8 @@ class ParameterPackExpansion final : public Node {
void printLeft(OutputBuffer &OB) const override {
constexpr unsigned Max = std::numeric_limits<unsigned>::max();
- SwapAndRestore<unsigned> SavePackIdx(OB.CurrentPackIndex, Max);
- SwapAndRestore<unsigned> SavePackMax(OB.CurrentPackMax, Max);
+ ScopedOverride<unsigned> SavePackIdx(OB.CurrentPackIndex, Max);
+ ScopedOverride<unsigned> SavePackMax(OB.CurrentPackMax, Max);
size_t StreamPos = OB.getCurrentPosition();
// Print the first element in the pack. If Child contains a ParameterPack,
@@ -1358,7 +1358,7 @@ class TemplateArgs final : public Node {
NodeArray getParams() { return Params; }
void printLeft(OutputBuffer &OB) const override {
- SwapAndRestore<unsigned> LT(OB.GtIsGt, 0);
+ ScopedOverride<unsigned> LT(OB.GtIsGt, 0);
OB += "<";
Params.printWithComma(OB);
if (OB.back() == '>')
@@ -1408,38 +1408,38 @@ struct ForwardTemplateReference : Node {
bool hasRHSComponentSlow(OutputBuffer &OB) const override {
if (Printing)
return false;
- SwapAndRestore<bool> SavePrinting(Printing, true);
+ ScopedOverride<bool> SavePrinting(Printing, true);
return Ref->hasRHSComponent(OB);
}
bool hasArraySlow(OutputBuffer &OB) const override {
if (Printing)
return false;
- SwapAndRestore<bool> SavePrinting(Printing, true);
+ ScopedOverride<bool> SavePrinting(Printing, true);
return Ref->hasArray(OB);
}
bool hasFunctionSlow(OutputBuffer &OB) const override {
if (Printing)
return false;
- SwapAndRestore<bool> SavePrinting(Printing, true);
+ ScopedOverride<bool> SavePrinting(Printing, true);
return Ref->hasFunction(OB);
}
const Node *getSyntaxNode(OutputBuffer &OB) const override {
if (Printing)
return this;
- SwapAndRestore<bool> SavePrinting(Printing, true);
+ ScopedOverride<bool> SavePrinting(Printing, true);
return Ref->getSyntaxNode(OB);
}
void printLeft(OutputBuffer &OB) const override {
if (Printing)
return;
- SwapAndRestore<bool> SavePrinting(Printing, true);
+ ScopedOverride<bool> SavePrinting(Printing, true);
Ref->printLeft(OB);
}
void printRight(OutputBuffer &OB) const override {
if (Printing)
return;
- SwapAndRestore<bool> SavePrinting(Printing, true);
+ ScopedOverride<bool> SavePrinting(Printing, true);
Ref->printRight(OB);
}
};
@@ -1656,7 +1656,7 @@ class ClosureTypeName : public Node {
void printDeclarator(OutputBuffer &OB) const {
if (!TemplateParams.empty()) {
- SwapAndRestore<unsigned> LT(OB.GtIsGt, 0);
+ ScopedOverride<unsigned> LT(OB.GtIsGt, 0);
OB += "<";
TemplateParams.printWithComma(OB);
OB += ">";
@@ -1880,7 +1880,7 @@ class CastExpr : public Node {
void printLeft(OutputBuffer &OB) const override {
OB += CastKind;
{
- SwapAndRestore<unsigned> LT(OB.GtIsGt, 0);
+ ScopedOverride<unsigned> LT(OB.GtIsGt, 0);
OB += "<";
To->printLeft(OB);
if (OB.back() == '>')
@@ -2857,7 +2857,7 @@ AbstractManglingParser<Derived, Alloc>::parseUnnamedTypeName(NameState *State) {
return make<UnnamedTypeName>(Count);
}
if (consumeIf("Ul")) {
- SwapAndRestore<size_t> SwapParams(ParsingLambdaParamsAtLevel,
+ ScopedOverride<size_t> SwapParams(ParsingLambdaParamsAtLevel,
TemplateParams.size());
ScopedTemplateParamList LambdaTemplateParams(this);
@@ -3062,11 +3062,11 @@ AbstractManglingParser<Derived, Alloc>::parseOperatorName(NameState *State) {
if (const auto *Op = parseOperatorEncoding()) {
if (Op->getKind() == OperatorInfo::CCast) {
// ::= cv <type> # (cast)
- SwapAndRestore<bool> SaveTemplate(TryToParseTemplateArgs, false);
+ ScopedOverride<bool> SaveTemplate(TryToParseTemplateArgs, false);
// If we're parsing an encoding, State != nullptr and the conversion
// operators' <type> could have a <template-param> that refers to some
// <template-arg>s further ahead in the mangled name.
- SwapAndRestore<bool> SavePermit(PermitForwardTemplateReferences,
+ ScopedOverride<bool> SavePermit(PermitForwardTemplateReferences,
PermitForwardTemplateReferences ||
State != nullptr);
Node *Ty = getDerived().parseType();
@@ -3718,8 +3718,8 @@ Node *AbstractManglingParser<Derived, Alloc>::parseQualifiedType() {
StringView ProtoSourceName = Qual.dropFront(std::strlen("objcproto"));
StringView Proto;
{
- SwapAndRestore<const char *> SaveFirst(First, ProtoSourceName.begin()),
- SaveLast(Last, ProtoSourceName.end());
+ ScopedOverride<const char *> SaveFirst(First, ProtoSourceName.begin()),
+ SaveLast(Last, ProtoSourceName.end());
Proto = parseBareSourceName();
}
if (Proto.empty())
@@ -4199,7 +4199,7 @@ Node *AbstractManglingParser<Derived, Alloc>::parseConversionExpr() {
return nullptr;
Node *Ty;
{
- SwapAndRestore<bool> SaveTemp(TryToParseTemplateArgs, false);
+ ScopedOverride<bool> SaveTemp(TryToParseTemplateArgs, false);
Ty = getDerived().parseType();
}
@@ -4634,7 +4634,7 @@ Node *AbstractManglingParser<Derived, Alloc>::parseExpr() {
// C Cast: (type)expr
Node *Ty;
{
- SwapAndRestore<bool> SaveTemp(TryToParseTemplateArgs, false);
+ ScopedOverride<bool> SaveTemp(TryToParseTemplateArgs, false);
Ty = getDerived().parseType();
}
if (Ty == nullptr)
diff --git a/llvm/include/llvm/Demangle/Utility.h b/llvm/include/llvm/Demangle/Utility.h
index 3ea92c9059162..b17eadcfa3bee 100644
--- a/llvm/include/llvm/Demangle/Utility.h
+++ b/llvm/include/llvm/Demangle/Utility.h
@@ -181,21 +181,20 @@ class OutputBuffer {
size_t getBufferCapacity() const { return BufferCapacity; }
};
-template <class T> class SwapAndRestore {
- T &Restore;
- T OriginalValue;
+template <class T> class ScopedOverride {
+ T &Loc;
+ T Original;
public:
- SwapAndRestore(T &Restore_) : SwapAndRestore(Restore_, Restore_) {}
+ ScopedOverride(T &Loc_) : ScopedOverride(Loc_, Loc_) {}
- SwapAndRestore(T &Restore_, T NewVal)
- : Restore(Restore_), OriginalValue(Restore) {
- Restore = std::move(NewVal);
+ ScopedOverride(T &Loc_, T NewVal) : Loc(Loc_), Original(Loc_) {
+ Loc_ = std::move(NewVal);
}
- ~SwapAndRestore() { Restore = std::move(OriginalValue); }
+ ~ScopedOverride() { Loc = std::move(Original); }
- SwapAndRestore(const SwapAndRestore &) = delete;
- SwapAndRestore &operator=(const SwapAndRestore &) = delete;
+ ScopedOverride(const ScopedOverride &) = delete;
+ ScopedOverride &operator=(const ScopedOverride &) = delete;
};
inline bool initializeOutputBuffer(char *Buf, size_t *N, OutputBuffer &OB,
diff --git a/llvm/lib/Demangle/RustDemangle.cpp b/llvm/lib/Demangle/RustDemangle.cpp
index dcac0bd638598..d62891d69dc3f 100644
--- a/llvm/lib/Demangle/RustDemangle.cpp
+++ b/llvm/lib/Demangle/RustDemangle.cpp
@@ -24,8 +24,8 @@
using namespace llvm;
using llvm::itanium_demangle::OutputBuffer;
+using llvm::itanium_demangle::ScopedOverride;
using llvm::itanium_demangle::StringView;
-using llvm::itanium_demangle::SwapAndRestore;
namespace {
@@ -119,7 +119,7 @@ class Demangler {
if (!Print)
return;
- SwapAndRestore<size_t> SavePosition(Position, Position);
+ ScopedOverride<size_t> SavePosition(Position, Position);
Position = Backref;
Demangler();
}
@@ -241,7 +241,7 @@ bool Demangler::demangle(StringView Mangled) {
demanglePath(IsInType::No);
if (Position != Input.size()) {
- SwapAndRestore<bool> SavePrint(Print, false);
+ ScopedOverride<bool> SavePrint(Print, false);
demanglePath(IsInType::No);
}
@@ -279,7 +279,7 @@ bool Demangler::demanglePath(IsInType InType, LeaveGenericsOpen LeaveOpen) {
Error = true;
return false;
}
- SwapAndRestore<size_t> SaveRecursionLevel(RecursionLevel, RecursionLevel + 1);
+ ScopedOverride<size_t> SaveRecursionLevel(RecursionLevel, RecursionLevel + 1);
switch (consume()) {
case 'C': {
@@ -380,7 +380,7 @@ bool Demangler::demanglePath(IsInType InType, LeaveGenericsOpen LeaveOpen) {
// <impl-path> = [<disambiguator>] <path>
// <disambiguator> = "s" <base-62-number>
void Demangler::demangleImplPath(IsInType InType) {
- SwapAndRestore<bool> SavePrint(Print, false);
+ ScopedOverride<bool> SavePrint(Print, false);
parseOptionalBase62Number('s');
demanglePath(InType);
}
@@ -574,7 +574,7 @@ void Demangler::demangleType() {
Error = true;
return;
}
- SwapAndRestore<size_t> SaveRecursionLevel(RecursionLevel, RecursionLevel + 1);
+ ScopedOverride<size_t> SaveRecursionLevel(RecursionLevel, RecursionLevel + 1);
size_t Start = Position;
char C = consume();
@@ -657,7 +657,7 @@ void Demangler::demangleType() {
// <abi> = "C"
// | <undisambiguated-identifier>
void Demangler::demangleFnSig() {
- SwapAndRestore<size_t> SaveBoundLifetimes(BoundLifetimes, BoundLifetimes);
+ ScopedOverride<size_t> SaveBoundLifetimes(BoundLifetimes, BoundLifetimes);
demangleOptionalBinder();
if (consumeIf('U'))
@@ -699,7 +699,7 @@ void Demangler::demangleFnSig() {
// <dyn-bounds> = [<binder>] {<dyn-trait>} "E"
void Demangler::demangleDynBounds() {
- SwapAndRestore<size_t> SaveBoundLifetimes(BoundLifetimes, BoundLifetimes);
+ ScopedOverride<size_t> SaveBoundLifetimes(BoundLifetimes, BoundLifetimes);
print("dyn ");
demangleOptionalBinder();
for (size_t I = 0; !Error && !consumeIf('E'); ++I) {
@@ -763,7 +763,7 @@ void Demangler::demangleConst() {
Error = true;
return;
}
- SwapAndRestore<size_t> SaveRecursionLevel(RecursionLevel, RecursionLevel + 1);
+ ScopedOverride<size_t> SaveRecursionLevel(RecursionLevel, RecursionLevel + 1);
char C = consume();
BasicType Type;
More information about the libcxx-commits
mailing list