Skip to content

Commit 5a0faf4

Browse files
committed
swift-demangle: add an option -type to demangle runtime type strings
The motivation for this is to test the `Demangler::demangleType` API.
1 parent 2388075 commit 5a0faf4

File tree

2 files changed

+29
-2
lines changed

2 files changed

+29
-2
lines changed

test/Demangle/demangle-types.test

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
RUN: %swift-demangle -type Si | %FileCheck %s --check-prefix=SWIFT-INT
2+
SWIFT-INT: Swift.Int
3+
4+
RUN: %swift-demangle -type SS_ | %FileCheck %s --check-prefix=MULTI-NODE-ERROR
5+
MULTI-NODE-ERROR: <<invalid type>>
6+
7+
RUN: %swift-demangle -type SSIeAghrx_ | %FileCheck %s --check-prefix=PARSE-ERROR
8+
PARSE-ERROR: <<invalid type>>
9+

tools/swift-demangle/swift-demangle.cpp

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,10 @@ static llvm::cl::opt<bool>
4545
TreeOnly("tree-only",
4646
llvm::cl::desc("Tree-only mode (do not show the demangled string)"));
4747

48+
static llvm::cl::opt<bool>
49+
DemangleType("type",
50+
llvm::cl::desc("Demangle a runtime type string"));
51+
4852
static llvm::cl::opt<bool>
4953
StripSpecialization("strip-specialization",
5054
llvm::cl::desc("Remangle the origin of a specialized function"));
@@ -141,7 +145,17 @@ static void demangle(llvm::raw_ostream &os, llvm::StringRef name,
141145
hadLeadingUnderscore = true;
142146
name = name.substr(1);
143147
}
144-
swift::Demangle::NodePointer pointer = DCtx.demangleSymbolAsNode(name);
148+
swift::Demangle::NodePointer pointer;
149+
if (DemangleType) {
150+
pointer = DCtx.demangleTypeAsNode(name);
151+
if (!pointer) {
152+
os << "<<invalid type>>";
153+
return;
154+
}
155+
} else {
156+
pointer = DCtx.demangleSymbolAsNode(name);
157+
}
158+
145159
if (ExpandMode || TreeOnly) {
146160
os << "Demangling for " << name << '\n';
147161
os << getNodeTreeAsString(pointer);
@@ -399,6 +413,10 @@ int main(int argc, char **argv) {
399413

400414
if (InputNames.empty()) {
401415
CompactMode = true;
416+
if (DemangleType) {
417+
llvm::errs() << "The option -type cannot be used to demangle stdin.\n";
418+
return EXIT_FAILURE;
419+
}
402420
return demangleSTDIN(options);
403421
} else {
404422
swift::Demangle::Context DCtx;
@@ -415,7 +433,7 @@ int main(int argc, char **argv) {
415433
"is quoted or escaped.\n";
416434
continue;
417435
}
418-
if (name.starts_with("S") || name.starts_with("s") ) {
436+
if (!DemangleType && (name.starts_with("S") || name.starts_with("s"))) {
419437
std::string correctedName = std::string("$") + name.str();
420438
demangle(llvm::outs(), correctedName, DCtx, options);
421439
} else {

0 commit comments

Comments
 (0)