Skip to content

Commit b96e130

Browse files
Merge pull request #32336 from adrian-prantl/64222171
Add a demangler option to hide local decl name contexts. …
2 parents 1e52852 + f4a4e4c commit b96e130

File tree

4 files changed

+20
-4
lines changed

4 files changed

+20
-4
lines changed

include/swift/Demangling/Demangle.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ struct DemangleOptions {
5050
bool DisplayProtocolConformances = true;
5151
bool DisplayWhereClauses = true;
5252
bool DisplayEntityTypes = true;
53+
bool DisplayLocalNameContexts = true;
5354
bool ShortenPartialApply = false;
5455
bool ShortenThunk = false;
5556
bool ShortenValueWitness = false;

lib/Demangling/NodePrinter.cpp

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1200,7 +1200,8 @@ NodePointer NodePrinter::print(NodePointer Node, bool asPrefixContext) {
12001200
/*hasName*/true);
12011201
case Node::Kind::LocalDeclName:
12021202
print(Node->getChild(1));
1203-
Printer << " #" << (Node->getChild(0)->getIndex() + 1);
1203+
if (Options.DisplayLocalNameContexts)
1204+
Printer << " #" << (Node->getChild(0)->getIndex() + 1);
12041205
return nullptr;
12051206
case Node::Kind::PrivateDeclName:
12061207
if (Node->getNumChildren() > 1) {
@@ -2468,8 +2469,9 @@ printEntity(NodePointer Entity, bool asPrefixContext, TypePrinting TypePr,
24682469
bool MultiWordName = ExtraName.contains(' ');
24692470
// Also a local name (e.g. Mystruct #1) does not look good if its context is
24702471
// printed in prefix form.
2471-
if (hasName &&
2472-
Entity->getChild(1)->getKind() == Node::Kind::LocalDeclName)
2472+
bool LocalName =
2473+
hasName && Entity->getChild(1)->getKind() == Node::Kind::LocalDeclName;
2474+
if (LocalName && Options.DisplayLocalNameContexts)
24732475
MultiWordName = true;
24742476

24752477
if (asPrefixContext && (TypePr != TypePrinting::NoType || MultiWordName)) {
@@ -2554,7 +2556,8 @@ printEntity(NodePointer Entity, bool asPrefixContext, TypePrinting TypePr,
25542556
printEntityType(Entity, type, genericFunctionTypeList);
25552557
}
25562558
}
2557-
if (!asPrefixContext && PostfixContext) {
2559+
if (!asPrefixContext && PostfixContext &&
2560+
(!LocalName || Options.DisplayLocalNameContexts)) {
25582561
// Print any left over context which couldn't be printed in prefix form.
25592562
if (Entity->getKind() == Node::Kind::DefaultArgumentInitializer ||
25602563
Entity->getKind() == Node::Kind::Initializer ||

test/Demangle/demangle-special-options.test

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,9 @@ CGRECT: {{ CGRect$}}
1010

1111
RUN: swift-demangle -hiding-module=foo _TtC3foo3bar | %FileCheck %s --check-prefix=BAR
1212
BAR: {{ bar$}}
13+
14+
RUN: swift-demangle -display-local-name-contexts=true s1a4mainyyFySRys5UInt8VGXEfU4_10ByteBufferL_aD | %FileCheck %s --check-prefix=LOCAL
15+
LOCAL: ByteBuffer #1 in closure #6
16+
17+
RUN: swift-demangle -display-local-name-contexts=false s1a4mainyyFySRys5UInt8VGXEfU4_10ByteBufferL_aD | %FileCheck %s --check-prefix=NOLOCAL
18+
NOLOCAL: {{ ByteBuffer$}}

tools/swift-demangle/swift-demangle.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,11 @@ Classify("classify",
7676

7777
/// Options that are primarily used for testing.
7878
/// \{
79+
static llvm::cl::opt<bool> DisplayLocalNameContexts(
80+
"display-local-name-contexts", llvm::cl::init(true),
81+
llvm::cl::desc("Qualify local names"),
82+
llvm::cl::Hidden);
83+
7984
static llvm::cl::opt<bool> DisplayStdlibModule(
8085
"display-stdlib-module", llvm::cl::init(true),
8186
llvm::cl::desc("Qualify types originating from the Swift standard library"),
@@ -255,6 +260,7 @@ int main(int argc, char **argv) {
255260
options.DisplayStdlibModule = DisplayStdlibModule;
256261
options.DisplayObjCModule = DisplayObjCModule;
257262
options.HidingCurrentModule = HidingModule;
263+
options.DisplayLocalNameContexts = DisplayLocalNameContexts;
258264

259265
if (InputNames.empty()) {
260266
CompactMode = true;

0 commit comments

Comments
 (0)