|
| 1 | +/* |
| 2 | + * Copyright 2019 WebAssembly Community Group participants |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +#ifndef WABT_DECOMPILER_NAMING_H_ |
| 18 | +#define WABT_DECOMPILER_NAMING_H_ |
| 19 | + |
| 20 | +#include "src/decompiler-ast.h" |
| 21 | + |
| 22 | +namespace wabt { |
| 23 | + |
| 24 | +inline void RenameToIdentifier(std::string& name, Index i, |
| 25 | + BindingHash& bh, |
| 26 | + const std::set<string_view>* filter) { |
| 27 | + // Filter out non-identifier characters, and try to reduce the size of |
| 28 | + // gigantic C++ signature names. |
| 29 | + std::string s; |
| 30 | + size_t nesting = 0; |
| 31 | + size_t read = 0; |
| 32 | + size_t word_start = 0; |
| 33 | + for (auto c : name) { |
| 34 | + read++; |
| 35 | + // We most certainly don't want to parse the entirety of C++ signatures, |
| 36 | + // but these names are sometimes several lines long, so would be great |
| 37 | + // to trim down. One quick way to do that is to remove anything between |
| 38 | + // nested (), which usually means the parameter list. |
| 39 | + if (c == '(') { |
| 40 | + nesting++; |
| 41 | + } |
| 42 | + if (c == ')') { |
| 43 | + nesting--; |
| 44 | + } |
| 45 | + if (nesting) { |
| 46 | + continue; |
| 47 | + } |
| 48 | + if (!isalnum(static_cast<unsigned char>(c))) { |
| 49 | + c = '_'; |
| 50 | + } |
| 51 | + if (c == '_') { |
| 52 | + if (s.empty()) { |
| 53 | + continue; // Skip leading. |
| 54 | + } |
| 55 | + if (s.back() == '_') { |
| 56 | + continue; // Consecutive. |
| 57 | + } |
| 58 | + } |
| 59 | + s += c; |
| 60 | + if (filter && (c == '_' || read == name.size())) { |
| 61 | + // We found a "word" inside a snake_case identifier. |
| 62 | + auto word_end = s.size(); |
| 63 | + if (c == '_') { |
| 64 | + word_end--; |
| 65 | + } |
| 66 | + assert(word_end > word_start); |
| 67 | + auto word = string_view(s.c_str() + word_start, word_end - word_start); |
| 68 | + if (filter->find(word) != filter->end()) { |
| 69 | + s.resize(word_start); |
| 70 | + } |
| 71 | + word_start = s.size(); |
| 72 | + } |
| 73 | + } |
| 74 | + if (!s.empty() && s.back() == '_') { |
| 75 | + s.pop_back(); // Trailing. |
| 76 | + } |
| 77 | + // If after all this culling, we're still gigantic (STL identifier can |
| 78 | + // easily be hundreds of chars in size), just cut the identifier |
| 79 | + // down, it will be disambiguated below, if needed. |
| 80 | + const size_t max_identifier_length = 100; |
| 81 | + if (s.size() > max_identifier_length) { |
| 82 | + s.resize(max_identifier_length); |
| 83 | + } |
| 84 | + // Remove original binding first, such that it doesn't match with our |
| 85 | + // new name. |
| 86 | + bh.erase(name); |
| 87 | + // Find a unique name. |
| 88 | + Index disambiguator = 0; |
| 89 | + auto base_len = s.size(); |
| 90 | + for (;;) { |
| 91 | + if (bh.count(s) == 0) { |
| 92 | + break; |
| 93 | + } |
| 94 | + disambiguator++; |
| 95 | + s.resize(base_len); |
| 96 | + s += '_'; |
| 97 | + s += std::to_string(disambiguator); |
| 98 | + } |
| 99 | + // Replace name in bindings. |
| 100 | + name = s; |
| 101 | + bh.emplace(s, Binding(i)); |
| 102 | +} |
| 103 | + |
| 104 | +template<typename T> |
| 105 | +void RenameToIdentifiers(std::vector<T*>& things, BindingHash& bh, |
| 106 | + const std::set<string_view>* filter) { |
| 107 | + Index i = 0; |
| 108 | + for (auto thing : things) { |
| 109 | + RenameToIdentifier(thing->name, i++, bh, filter); |
| 110 | + } |
| 111 | +} |
| 112 | + |
| 113 | +// Function names may contain arbitrary C++ syntax, so we want to |
| 114 | +// filter those to look like identifiers. A function name may be set |
| 115 | +// by a name section (applied in ReadBinaryIr, called before this function) |
| 116 | +// or by an export (applied by GenerateNames, called before this function), |
| 117 | +// to both the Func and func_bindings. |
| 118 | +// Those names then further perculate down the IR in ApplyNames (called after |
| 119 | +// this function). |
| 120 | +// To not have to add too many decompiler-specific code into those systems |
| 121 | +// (using a callback??) we instead rename everything here. |
| 122 | +void RenameAll(Module& module) { |
| 123 | + // We also filter common C++ keywords/STL idents that make for huge |
| 124 | + // identifiers. |
| 125 | + // FIXME: this can obviously give bad results if the input is not C++.. |
| 126 | + std::set<string_view> filter = { |
| 127 | + { "const" }, |
| 128 | + { "std" }, |
| 129 | + { "allocator" }, |
| 130 | + { "char" }, |
| 131 | + { "basic" }, |
| 132 | + { "traits" }, |
| 133 | + { "wchar" }, |
| 134 | + { "t" }, |
| 135 | + { "void" }, |
| 136 | + { "int" }, |
| 137 | + { "unsigned" }, |
| 138 | + { "2" }, |
| 139 | + { "cxxabiv1" }, |
| 140 | + { "short" }, |
| 141 | + { "4096ul" }, |
| 142 | + }; |
| 143 | + RenameToIdentifiers(module.funcs, module.func_bindings, &filter); |
| 144 | + // Also do this for some other kinds of names. |
| 145 | + RenameToIdentifiers(module.globals, module.global_bindings, nullptr); |
| 146 | + RenameToIdentifiers(module.tables, module.table_bindings, nullptr); |
| 147 | +} |
| 148 | + |
| 149 | +} // namespace wabt |
| 150 | + |
| 151 | +#endif // WABT_DECOMPILER_NAMING_H_ |
0 commit comments