[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 GitOrigin-RevId: 51f6caf2fbb5118569f2de33a85ffcc52568e0a7
diff --git a/src/demangle/ItaniumDemangle.h b/src/demangle/ItaniumDemangle.h index fd6835a..36fe26b 100644 --- a/src/demangle/ItaniumDemangle.h +++ b/src/demangle/ItaniumDemangle.h
@@ -656,7 +656,7 @@ 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 @@ 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 @@ 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 @@ 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 @@ 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 @@ 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 @@ 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 @@ 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 @@ 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 @@ 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 @@ 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 @@ return nullptr; Node *Ty; { - SwapAndRestore<bool> SaveTemp(TryToParseTemplateArgs, false); + ScopedOverride<bool> SaveTemp(TryToParseTemplateArgs, false); Ty = getDerived().parseType(); } @@ -4634,7 +4634,7 @@ // 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/src/demangle/Utility.h b/src/demangle/Utility.h index 1390f14..6e0fa38 100644 --- a/src/demangle/Utility.h +++ b/src/demangle/Utility.h
@@ -181,21 +181,20 @@ 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,