From dd868cf280b9a23ca8e3f0a8a1598131040af05a Mon Sep 17 00:00:00 2001 From: Dave Lee Date: Mon, 29 Apr 2024 15:30:27 -0700 Subject: [PATCH 1/3] Demangler: Add option to omit closure signatures --- include/swift/Demangling/Demangle.h | 1 + lib/Demangling/NodePrinter.cpp | 13 ++++++++++++- 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/include/swift/Demangling/Demangle.h b/include/swift/Demangling/Demangle.h index e76f78a5b972a..cb398d25b676d 100644 --- a/include/swift/Demangling/Demangle.h +++ b/include/swift/Demangling/Demangle.h @@ -63,6 +63,7 @@ struct DemangleOptions { bool DisplayObjCModule = true; bool PrintForTypeName = false; bool ShowAsyncResumePartial = true; + bool ShowClosureSignature = true; /// If this is nonempty, entities in this module name will not be qualified. llvm::StringRef HidingCurrentModule; diff --git a/lib/Demangling/NodePrinter.cpp b/lib/Demangling/NodePrinter.cpp index 7b683d7530211..58d779af6c879 100644 --- a/lib/Demangling/NodePrinter.cpp +++ b/lib/Demangling/NodePrinter.cpp @@ -1349,6 +1349,17 @@ static bool needSpaceBeforeType(NodePointer Type) { } } +static bool shouldShowEntityType(Node::Kind EntityKind, + const DemangleOptions &Options) { + switch (EntityKind) { + case Node::Kind::ExplicitClosure: + case Node::Kind::ImplicitClosure: + return Options.ShowClosureSignature; + default: + return true; + } +} + NodePointer NodePrinter::print(NodePointer Node, unsigned depth, bool asPrefixContext) { if (depth > NodePrinter::MaxDepth) { @@ -3492,7 +3503,7 @@ NodePointer NodePrinter::printEntity(NodePointer Entity, unsigned depth, Printer << " : "; printEntityType(Entity, type, genericFunctionTypeList, depth); } - } else { + } else if (shouldShowEntityType(Entity->getKind(), Options)) { assert(TypePr == TypePrinting::FunctionStyle); if (MultiWordName || needSpaceBeforeType(type)) Printer << ' '; From aeb9039b3ab0b31b5b41c5b020cc7d3aa425159e Mon Sep 17 00:00:00 2001 From: Dave Lee Date: Tue, 30 Apr 2024 08:11:29 -0700 Subject: [PATCH 2/3] Document shouldShowEntityType --- lib/Demangling/NodePrinter.cpp | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/lib/Demangling/NodePrinter.cpp b/lib/Demangling/NodePrinter.cpp index 58d779af6c879..701299187dfba 100644 --- a/lib/Demangling/NodePrinter.cpp +++ b/lib/Demangling/NodePrinter.cpp @@ -1349,11 +1349,17 @@ static bool needSpaceBeforeType(NodePointer Type) { } } +/// Determine whether to print an entity's type. static bool shouldShowEntityType(Node::Kind EntityKind, const DemangleOptions &Options) { switch (EntityKind) { case Node::Kind::ExplicitClosure: case Node::Kind::ImplicitClosure: + /// The signature of a closure (its `Type` node) can optionally be omitted. + /// Unlike functions which can have overloads, the signature of a closure is + /// not needed to be uniquely identified. A closure is uniquely identified + /// by its index and parent. Omitting the signature improves the readability + /// when long type names are in use. return Options.ShowClosureSignature; default: return true; From a171a8f17ddf60b41d400799a18864f716e6a692 Mon Sep 17 00:00:00 2001 From: Dave Lee Date: Tue, 30 Apr 2024 08:12:08 -0700 Subject: [PATCH 3/3] Add test --- test/Demangle/demangle-special-options.test | 3 +++ tools/swift-demangle/swift-demangle.cpp | 6 ++++++ 2 files changed, 9 insertions(+) diff --git a/test/Demangle/demangle-special-options.test b/test/Demangle/demangle-special-options.test index 4d3ab0b3c874e..f6e1e00c789df 100644 --- a/test/Demangle/demangle-special-options.test +++ b/test/Demangle/demangle-special-options.test @@ -16,3 +16,6 @@ LOCAL: ByteBuffer #1 in closure #6 RUN: swift-demangle -display-local-name-contexts=false s1a4mainyyFySRys5UInt8VGXEfU4_10ByteBufferL_aD | %FileCheck %s --check-prefix=NOLOCAL NOLOCAL: {{ ByteBuffer$}} + +RUN: swift-demangle -show-closure-signature=false s4mainySiXEfU_ySiXEfU_ | %FileCheck %s --check-prefix=CLOSURE +CLOSURE: closure #1 in closure #1 in main diff --git a/tools/swift-demangle/swift-demangle.cpp b/tools/swift-demangle/swift-demangle.cpp index 35e25378ecd16..f3cff2ba3c9ca 100644 --- a/tools/swift-demangle/swift-demangle.cpp +++ b/tools/swift-demangle/swift-demangle.cpp @@ -94,6 +94,11 @@ static llvm::cl::opt HidingModule( "hiding-module", llvm::cl::desc("Don't qualify types originating from this module"), llvm::cl::Hidden); + +static llvm::cl::opt + ShowClosureSignature("show-closure-signature", llvm::cl::init(true), + llvm::cl::desc("Show type signature of closures"), + llvm::cl::Hidden); /// \} @@ -390,6 +395,7 @@ int main(int argc, char **argv) { options.DisplayObjCModule = DisplayObjCModule; options.HidingCurrentModule = HidingModule; options.DisplayLocalNameContexts = DisplayLocalNameContexts; + options.ShowClosureSignature = ShowClosureSignature; if (InputNames.empty()) { CompactMode = true;