|
| 1 | +//===--- swift-demangle-yamldump.cpp --------------------------------------===// |
| 2 | +// |
| 3 | +// This source file is part of the Swift.org open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2014 - 2018 Apple Inc. and the Swift project authors |
| 6 | +// Licensed under Apache License v2.0 with Runtime Library Exception |
| 7 | +// |
| 8 | +// See https://swift.org/LICENSE.txt for license information |
| 9 | +// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors |
| 10 | +// |
| 11 | +//===----------------------------------------------------------------------===// |
| 12 | +/// |
| 13 | +/// \file |
| 14 | +/// |
| 15 | +/// This tool is similar to the demangler but is intended to /not/ be installed |
| 16 | +/// into the OS. This means that it can link against llvm support and friends |
| 17 | +/// and thus provide extra functionality. Today it is only used to dump the |
| 18 | +/// demangler tree in YAML form for easy processing. |
| 19 | +/// |
| 20 | +//===----------------------------------------------------------------------===// |
| 21 | + |
| 22 | +#include "swift/Basic/LLVM.h" |
| 23 | +#include "swift/Demangling/Demangle.h" |
| 24 | +#include "swift/Demangling/ManglingMacros.h" |
| 25 | +#include "llvm/ADT/StringRef.h" |
| 26 | +#include "llvm/ADT/SmallString.h" |
| 27 | +#include "llvm/Support/YAMLTraits.h" |
| 28 | +#include "llvm/Support/CommandLine.h" |
| 29 | +#include "llvm/Support/MemoryBuffer.h" |
| 30 | +#include "llvm/Support/PrettyStackTrace.h" |
| 31 | +#include "llvm/Support/Regex.h" |
| 32 | +#include "llvm/Support/Signals.h" |
| 33 | +#include "llvm/Support/raw_ostream.h" |
| 34 | + |
| 35 | +// For std::rand, to work around a bug if main()'s first function call passes |
| 36 | +// argv[0]. |
| 37 | +#if defined(__CYGWIN__) |
| 38 | +#include <cstdlib> |
| 39 | +#endif |
| 40 | + |
| 41 | +#include <iostream> |
| 42 | + |
| 43 | +using namespace swift; |
| 44 | +using namespace swift::Demangle; |
| 45 | + |
| 46 | +//===----------------------------------------------------------------------===// |
| 47 | +// YAML Dump Implementation |
| 48 | +//===----------------------------------------------------------------------===// |
| 49 | + |
| 50 | +namespace { |
| 51 | + |
| 52 | +using llvm::yaml::IO; |
| 53 | +using llvm::yaml::MappingTraits; |
| 54 | +using llvm::yaml::Output; |
| 55 | +using llvm::yaml::ScalarEnumerationTraits; |
| 56 | +using llvm::yaml::SequenceTraits; |
| 57 | + |
| 58 | +struct YAMLNode { |
| 59 | + Node::Kind kind; |
| 60 | + std::vector<YAMLNode *> children; |
| 61 | + StringRef name = StringRef(); |
| 62 | + |
| 63 | + YAMLNode(Node::Kind kind) : kind(kind), children() {} |
| 64 | +}; |
| 65 | + |
| 66 | +} // end anonymous namespace |
| 67 | + |
| 68 | +namespace llvm { |
| 69 | +namespace yaml { |
| 70 | + |
| 71 | +template <> struct ScalarEnumerationTraits<swift::Demangle::Node::Kind> { |
| 72 | + static void enumeration(IO &io, swift::Demangle::Node::Kind &value) { |
| 73 | +#define NODE(ID) io.enumCase(value, #ID, swift::Demangle::Node::Kind::ID); |
| 74 | +#include "swift/Demangling/DemangleNodes.def" |
| 75 | + } |
| 76 | +}; |
| 77 | + |
| 78 | +template <> struct MappingTraits<YAMLNode *> { |
| 79 | + static void mapping(IO &io, YAMLNode *&node) { |
| 80 | + io.mapOptional("name", node->name); |
| 81 | + io.mapRequired("kind", node->kind); |
| 82 | + io.mapRequired("children", node->children); |
| 83 | + } |
| 84 | +}; |
| 85 | + |
| 86 | +template <> struct MappingTraits<YAMLNode> { |
| 87 | + static void mapping(IO &io, YAMLNode &node) { |
| 88 | + io.mapOptional("name", node.name); |
| 89 | + io.mapRequired("kind", node.kind); |
| 90 | + io.mapRequired("children", node.children); |
| 91 | + } |
| 92 | +}; |
| 93 | + |
| 94 | +} // namespace yaml |
| 95 | +} // namespace llvm |
| 96 | + |
| 97 | +LLVM_YAML_IS_SEQUENCE_VECTOR(YAMLNode *) |
| 98 | + |
| 99 | +static std::string getNodeTreeAsYAML(llvm::StringRef name, |
| 100 | + NodePointer root) { |
| 101 | + std::vector<std::unique_ptr<YAMLNode>> nodes; |
| 102 | + |
| 103 | + std::vector<std::pair<NodePointer, YAMLNode *>> worklist; |
| 104 | + nodes.emplace_back(new YAMLNode(root->getKind())); |
| 105 | + nodes.back()->name = name; |
| 106 | + worklist.emplace_back(root, &*nodes.back()); |
| 107 | + |
| 108 | + while (!worklist.empty()) { |
| 109 | + NodePointer np; |
| 110 | + YAMLNode *node; |
| 111 | + std::tie(np, node) = worklist.back(); |
| 112 | + worklist.pop_back(); |
| 113 | + |
| 114 | + for (unsigned i = 0; i < np->getNumChildren(); ++i) { |
| 115 | + nodes.emplace_back(new YAMLNode(np->getChild(i)->getKind())); |
| 116 | + node->children.emplace_back(&*nodes.back()); |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | + std::string output; |
| 121 | + llvm::raw_string_ostream stream(output); |
| 122 | + llvm::yaml::Output yout(stream); |
| 123 | + yout << *nodes.front(); |
| 124 | + return stream.str(); |
| 125 | +} |
| 126 | + |
| 127 | +//===----------------------------------------------------------------------===// |
| 128 | +// Top Level Entrypoint |
| 129 | +//===----------------------------------------------------------------------===// |
| 130 | + |
| 131 | +static llvm::cl::opt<bool> |
| 132 | + DisableSugar("no-sugar", |
| 133 | + llvm::cl::desc("No sugar mode (disable common language idioms " |
| 134 | + "such as ? and [] from the output)")); |
| 135 | + |
| 136 | +static llvm::cl::opt<bool> Simplified( |
| 137 | + "simplified", |
| 138 | + llvm::cl::desc("Don't display module names or implicit self types")); |
| 139 | + |
| 140 | +static llvm::cl::list<std::string> |
| 141 | + InputNames(llvm::cl::Positional, llvm::cl::desc("[mangled name...]"), |
| 142 | + llvm::cl::ZeroOrMore); |
| 143 | + |
| 144 | +static llvm::StringRef substrBefore(llvm::StringRef whole, |
| 145 | + llvm::StringRef part) { |
| 146 | + return whole.slice(0, part.data() - whole.data()); |
| 147 | +} |
| 148 | + |
| 149 | +static llvm::StringRef substrAfter(llvm::StringRef whole, |
| 150 | + llvm::StringRef part) { |
| 151 | + return whole.substr((part.data() - whole.data()) + part.size()); |
| 152 | +} |
| 153 | + |
| 154 | +static void demangle(llvm::raw_ostream &os, llvm::StringRef name, |
| 155 | + swift::Demangle::Context &DCtx, |
| 156 | + const swift::Demangle::DemangleOptions &options) { |
| 157 | + bool hadLeadingUnderscore = false; |
| 158 | + if (name.startswith("__")) { |
| 159 | + hadLeadingUnderscore = true; |
| 160 | + name = name.substr(1); |
| 161 | + } |
| 162 | + swift::Demangle::NodePointer pointer = DCtx.demangleSymbolAsNode(name); |
| 163 | + // We do not emit a message so that we end up dumping |
| 164 | + llvm::outs() << getNodeTreeAsYAML(name, pointer); |
| 165 | + DCtx.clear(); |
| 166 | +} |
| 167 | + |
| 168 | +static int demangleSTDIN(const swift::Demangle::DemangleOptions &options) { |
| 169 | + // This doesn't handle Unicode symbols, but maybe that's okay. |
| 170 | + // Also accept the future mangling prefix. |
| 171 | + llvm::Regex maybeSymbol("(_T|_?\\$[Ss])[_a-zA-Z0-9$.]+"); |
| 172 | + |
| 173 | + swift::Demangle::Context DCtx; |
| 174 | + for (std::string mangled; std::getline(std::cin, mangled);) { |
| 175 | + llvm::StringRef inputContents(mangled); |
| 176 | + |
| 177 | + llvm::SmallVector<llvm::StringRef, 1> matches; |
| 178 | + while (maybeSymbol.match(inputContents, &matches)) { |
| 179 | + llvm::outs() << substrBefore(inputContents, matches.front()); |
| 180 | + demangle(llvm::outs(), matches.front(), DCtx, options); |
| 181 | + inputContents = substrAfter(inputContents, matches.front()); |
| 182 | + } |
| 183 | + |
| 184 | + llvm::errs() << "Failed to match: " << inputContents << '\n'; |
| 185 | + } |
| 186 | + |
| 187 | + return EXIT_SUCCESS; |
| 188 | +} |
| 189 | + |
| 190 | +int main(int argc, char **argv) { |
| 191 | +#if defined(__CYGWIN__) |
| 192 | + // Cygwin clang 3.5.2 with '-O3' generates CRASHING BINARY, |
| 193 | + // if main()'s first function call is passing argv[0]. |
| 194 | + std::rand(); |
| 195 | +#endif |
| 196 | + llvm::cl::ParseCommandLineOptions(argc, argv); |
| 197 | + |
| 198 | + swift::Demangle::DemangleOptions options; |
| 199 | + options.SynthesizeSugarOnTypes = !DisableSugar; |
| 200 | + if (Simplified) |
| 201 | + options = swift::Demangle::DemangleOptions::SimplifiedUIDemangleOptions(); |
| 202 | + |
| 203 | + if (InputNames.empty()) { |
| 204 | + return demangleSTDIN(options); |
| 205 | + } else { |
| 206 | + swift::Demangle::Context DCtx; |
| 207 | + for (llvm::StringRef name : InputNames) { |
| 208 | + if (name.startswith("S")) { |
| 209 | + std::string correctedName = std::string("$") + name.str(); |
| 210 | + demangle(llvm::outs(), correctedName, DCtx, options); |
| 211 | + } else { |
| 212 | + demangle(llvm::outs(), name, DCtx, options); |
| 213 | + } |
| 214 | + llvm::outs() << '\n'; |
| 215 | + } |
| 216 | + |
| 217 | + return EXIT_SUCCESS; |
| 218 | + } |
| 219 | +} |
0 commit comments