swift-demangle: add an option -type to demangle runtime type strings

The motivation for this is to test the `Demangler::demangleType` API.
This commit is contained in:
Erik Eckstein
2024-06-27 11:52:23 +02:00
parent 7fe2befd31
commit b9cb5939cf
2 changed files with 29 additions and 2 deletions

View File

@@ -0,0 +1,9 @@
RUN: %swift-demangle -type Si | %FileCheck %s --check-prefix=SWIFT-INT
SWIFT-INT: Swift.Int
RUN: %swift-demangle -type SS_ | %FileCheck %s --check-prefix=MULTI-NODE-ERROR
MULTI-NODE-ERROR: <<invalid type>>
RUN: %swift-demangle -type SSIeAghrx_ | %FileCheck %s --check-prefix=PARSE-ERROR
PARSE-ERROR: <<invalid type>>

View File

@@ -45,6 +45,10 @@ static llvm::cl::opt<bool>
TreeOnly("tree-only",
llvm::cl::desc("Tree-only mode (do not show the demangled string)"));
static llvm::cl::opt<bool>
DemangleType("type",
llvm::cl::desc("Demangle a runtime type string"));
static llvm::cl::opt<bool>
StripSpecialization("strip-specialization",
llvm::cl::desc("Remangle the origin of a specialized function"));
@@ -141,7 +145,17 @@ static void demangle(llvm::raw_ostream &os, llvm::StringRef name,
hadLeadingUnderscore = true;
name = name.substr(1);
}
swift::Demangle::NodePointer pointer = DCtx.demangleSymbolAsNode(name);
swift::Demangle::NodePointer pointer;
if (DemangleType) {
pointer = DCtx.demangleTypeAsNode(name);
if (!pointer) {
os << "<<invalid type>>";
return;
}
} else {
pointer = DCtx.demangleSymbolAsNode(name);
}
if (ExpandMode || TreeOnly) {
os << "Demangling for " << name << '\n';
os << getNodeTreeAsString(pointer);
@@ -399,6 +413,10 @@ int main(int argc, char **argv) {
if (InputNames.empty()) {
CompactMode = true;
if (DemangleType) {
llvm::errs() << "The option -type cannot be used to demangle stdin.\n";
return EXIT_FAILURE;
}
return demangleSTDIN(options);
} else {
swift::Demangle::Context DCtx;
@@ -415,7 +433,7 @@ int main(int argc, char **argv) {
"is quoted or escaped.\n";
continue;
}
if (name.starts_with("S") || name.starts_with("s") ) {
if (!DemangleType && (name.starts_with("S") || name.starts_with("s"))) {
std::string correctedName = std::string("$") + name.str();
demangle(llvm::outs(), correctedName, DCtx, options);
} else {