Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 1 | // Copyright 2020 The Tint Authors. |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | #include "src/tint/demangler.h" |
| 16 | |
| 17 | #include "src/tint/program.h" |
| 18 | |
| 19 | namespace tint { |
| 20 | namespace { |
| 21 | |
| 22 | constexpr char kSymbol[] = "$"; |
| 23 | constexpr size_t kSymbolLen = sizeof(kSymbol) - 1; |
| 24 | |
| 25 | } // namespace |
| 26 | |
| 27 | Demangler::Demangler() = default; |
| 28 | |
| 29 | Demangler::~Demangler() = default; |
| 30 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 31 | std::string Demangler::Demangle(const SymbolTable& symbols, const std::string& str) const { |
| 32 | std::stringstream out; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 33 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 34 | size_t pos = 0; |
| 35 | for (;;) { |
| 36 | auto idx = str.find(kSymbol, pos); |
| 37 | if (idx == std::string::npos) { |
| 38 | out << str.substr(pos); |
| 39 | break; |
| 40 | } |
| 41 | |
| 42 | out << str.substr(pos, idx - pos); |
| 43 | |
| 44 | auto start_idx = idx + kSymbolLen; |
| 45 | auto end_idx = start_idx; |
| 46 | while (str[end_idx] >= '0' && str[end_idx] <= '9') { |
| 47 | end_idx++; |
| 48 | } |
| 49 | auto len = end_idx - start_idx; |
| 50 | |
| 51 | auto id = str.substr(start_idx, len); |
dan sinclair | 3a2a279 | 2022-06-29 14:38:15 +0000 | [diff] [blame] | 52 | Symbol sym(static_cast<uint32_t>(std::stoi(id)), symbols.ProgramID()); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 53 | out << symbols.NameFor(sym); |
| 54 | |
| 55 | pos = end_idx; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 56 | } |
| 57 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 58 | return out.str(); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | } // namespace tint |