Skip to content

Demangler: Add option to omit closure signatures (#73331) #73353

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
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions include/swift/Demangling/Demangle.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
19 changes: 18 additions & 1 deletion lib/Demangling/NodePrinter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1348,6 +1348,23 @@ 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;
}
}

NodePointer NodePrinter::print(NodePointer Node, unsigned depth,
bool asPrefixContext) {
if (depth > NodePrinter::MaxDepth) {
Expand Down Expand Up @@ -3484,7 +3501,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 << ' ';
Expand Down
3 changes: 3 additions & 0 deletions test/Demangle/demangle-special-options.test
Original file line number Diff line number Diff line change
Expand Up @@ -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
6 changes: 6 additions & 0 deletions tools/swift-demangle/swift-demangle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,11 @@ static llvm::cl::opt<std::string> HidingModule(
"hiding-module",
llvm::cl::desc("Don't qualify types originating from this module"),
llvm::cl::Hidden);

static llvm::cl::opt<bool>
ShowClosureSignature("show-closure-signature", llvm::cl::init(true),
llvm::cl::desc("Show type signature of closures"),
llvm::cl::Hidden);
/// \}


Expand Down Expand Up @@ -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;
Expand Down