Skip to content

Commit a85dbaf

Browse files
authored
Demangler: Add option to omit closure signatures (swiftlang#73331) (swiftlang#73353)
Add a new demangler option which excludes a closure's type signature. This will be used in lldb. Closures are not subject to overloading, and so the signature will never be used to disambiguate. A demangled closure is uniquely identifiable by its index(s) and parent. Where opaque types are involved, the concrete type signature can be quite complex. This demangling option allows callers to avoid printing the underlying complex nested concrete types. Example: before: `closure #1 (Swift.Int) -> () in closure #1 (Swift.Int) -> () in main` after: `closure #1 in closure #1 in main`
1 parent d5e0ca5 commit a85dbaf

File tree

4 files changed

+28
-1
lines changed

4 files changed

+28
-1
lines changed

include/swift/Demangling/Demangle.h

+1
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ struct DemangleOptions {
6363
bool DisplayObjCModule = true;
6464
bool PrintForTypeName = false;
6565
bool ShowAsyncResumePartial = true;
66+
bool ShowClosureSignature = true;
6667

6768
/// If this is nonempty, entities in this module name will not be qualified.
6869
llvm::StringRef HidingCurrentModule;

lib/Demangling/NodePrinter.cpp

+18-1
Original file line numberDiff line numberDiff line change
@@ -1348,6 +1348,23 @@ static bool needSpaceBeforeType(NodePointer Type) {
13481348
}
13491349
}
13501350

1351+
/// Determine whether to print an entity's type.
1352+
static bool shouldShowEntityType(Node::Kind EntityKind,
1353+
const DemangleOptions &Options) {
1354+
switch (EntityKind) {
1355+
case Node::Kind::ExplicitClosure:
1356+
case Node::Kind::ImplicitClosure:
1357+
// The signature of a closure (its `Type` node) can optionally be omitted.
1358+
// Unlike functions which can have overloads, the signature of a closure is
1359+
// not needed to be uniquely identified. A closure is uniquely identified by
1360+
// its index and parent. Omitting the signature improves the readability
1361+
// when long type names are in use.
1362+
return Options.ShowClosureSignature;
1363+
default:
1364+
return true;
1365+
}
1366+
}
1367+
13511368
NodePointer NodePrinter::print(NodePointer Node, unsigned depth,
13521369
bool asPrefixContext) {
13531370
if (depth > NodePrinter::MaxDepth) {
@@ -3484,7 +3501,7 @@ NodePointer NodePrinter::printEntity(NodePointer Entity, unsigned depth,
34843501
Printer << " : ";
34853502
printEntityType(Entity, type, genericFunctionTypeList, depth);
34863503
}
3487-
} else {
3504+
} else if (shouldShowEntityType(Entity->getKind(), Options)) {
34883505
assert(TypePr == TypePrinting::FunctionStyle);
34893506
if (MultiWordName || needSpaceBeforeType(type))
34903507
Printer << ' ';

test/Demangle/demangle-special-options.test

+3
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,6 @@ LOCAL: ByteBuffer #1 in closure #6
1616

1717
RUN: swift-demangle -display-local-name-contexts=false s1a4mainyyFySRys5UInt8VGXEfU4_10ByteBufferL_aD | %FileCheck %s --check-prefix=NOLOCAL
1818
NOLOCAL: {{ ByteBuffer$}}
19+
20+
RUN: swift-demangle -show-closure-signature=false s4mainySiXEfU_ySiXEfU_ | %FileCheck %s --check-prefix=CLOSURE
21+
CLOSURE: closure #1 in closure #1 in main

tools/swift-demangle/swift-demangle.cpp

+6
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,11 @@ static llvm::cl::opt<std::string> HidingModule(
9494
"hiding-module",
9595
llvm::cl::desc("Don't qualify types originating from this module"),
9696
llvm::cl::Hidden);
97+
98+
static llvm::cl::opt<bool>
99+
ShowClosureSignature("show-closure-signature", llvm::cl::init(true),
100+
llvm::cl::desc("Show type signature of closures"),
101+
llvm::cl::Hidden);
97102
/// \}
98103

99104

@@ -390,6 +395,7 @@ int main(int argc, char **argv) {
390395
options.DisplayObjCModule = DisplayObjCModule;
391396
options.HidingCurrentModule = HidingModule;
392397
options.DisplayLocalNameContexts = DisplayLocalNameContexts;
398+
options.ShowClosureSignature = ShowClosureSignature;
393399

394400
if (InputNames.empty()) {
395401
CompactMode = true;

0 commit comments

Comments
 (0)