mirror of
https://github.com/apple/swift.git
synced 2025-12-21 12:14:44 +01:00
This makes the demangler about 10 times faster. It also changes the lifetimes of nodes. Previously nodes were reference-counted. Now the returned demangle node-tree is owned by the Demangler class and it’s lifetime ends with the lifetime of the Demangler. Therefore the old (and already deprecated) global functions demangleSymbolAsNode and demangleTypeAsNode are no longer available. Another change is that the demangling for reflection now only supports the new mangling (which should be no problem because we are generating only new mangled names for reflection).
87 lines
2.5 KiB
C++
87 lines
2.5 KiB
C++
//===--- DemangleWrappers.cpp - Swift Name Demangling ---------------------===//
|
|
//
|
|
// This source file is part of the Swift.org open source project
|
|
//
|
|
// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors
|
|
// Licensed under Apache License v2.0 with Runtime Library Exception
|
|
//
|
|
// See https://swift.org/LICENSE.txt for license information
|
|
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
#include "swift/Basic/DemangleWrappers.h"
|
|
#include "swift/Basic/PrettyStackTrace.h"
|
|
#include "llvm/Support/raw_ostream.h"
|
|
#include "llvm/Support/ErrorHandling.h"
|
|
|
|
using namespace swift;
|
|
using namespace demangle_wrappers;
|
|
|
|
static StringRef getNodeKindString(swift::Demangle::Node::Kind k) {
|
|
switch (k) {
|
|
#define NODE(ID) \
|
|
case Node::Kind::ID: \
|
|
return #ID;
|
|
#include "swift/Basic/DemangleNodes.def"
|
|
}
|
|
llvm_unreachable("bad node kind");
|
|
}
|
|
|
|
static void printNode(llvm::raw_ostream &Out, const Node *node,
|
|
unsigned depth) {
|
|
// Indent two spaces per depth.
|
|
Out.indent(depth * 2);
|
|
if (!node) {
|
|
Out << "<<NULL>>";
|
|
return;
|
|
}
|
|
Out << "kind=" << getNodeKindString(node->getKind());
|
|
if (node->hasText()) {
|
|
Out << ", text=\"" << node->getText() << '\"';
|
|
}
|
|
if (node->hasIndex()) {
|
|
Out << ", index=" << node->getIndex();
|
|
}
|
|
Out << '\n';
|
|
for (auto &child : *node) {
|
|
printNode(Out, child, depth + 1);
|
|
}
|
|
}
|
|
|
|
void NodeDumper::dump() const { print(llvm::errs()); }
|
|
|
|
void NodeDumper::print(llvm::raw_ostream &Out) const {
|
|
printNode(Out, Root, 0);
|
|
}
|
|
|
|
void swift::demangle_wrappers::dumpNode(const NodePointer &Root) {
|
|
NodeDumper(Root).dump();
|
|
}
|
|
|
|
namespace {
|
|
/// A pretty-stack-trace node for demangling trees.
|
|
class PrettyStackTraceNode : public llvm::PrettyStackTraceEntry {
|
|
const char *Action;
|
|
Node *TheNode;
|
|
public:
|
|
PrettyStackTraceNode(const char *action, Node *node)
|
|
: Action(action), TheNode(node) {}
|
|
void print(llvm::raw_ostream &out) const override {
|
|
out << "While " << Action << ' ';
|
|
if (!TheNode) {
|
|
out << "<<null demangling node>>\n";
|
|
} else {
|
|
out << "demangling tree:\n";
|
|
printNode(out, TheNode, 4);
|
|
}
|
|
}
|
|
};
|
|
} // end unnamed namespace
|
|
|
|
std::string nodeToString(NodePointer Root,
|
|
const DemangleOptions &Options) {
|
|
PrettyStackTraceNode trace("printing", Root);
|
|
return swift::Demangle::nodeToString(Root, Options);
|
|
}
|