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/demangler.h"
16
17 #include "src/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
Demangle(const SymbolTable& symbols, const std::string& str) const31 std::string Demangler::Demangle(const SymbolTable& symbols,
32 const std::string& str) const {
33 std::stringstream out;
34
35 size_t pos = 0;
36 for (;;) {
37 auto idx = str.find(kSymbol, pos);
38 if (idx == std::string::npos) {
39 out << str.substr(pos);
40 break;
41 }
42
43 out << str.substr(pos, idx - pos);
44
45 auto start_idx = idx + kSymbolLen;
46 auto end_idx = start_idx;
47 while (str[end_idx] >= '0' && str[end_idx] <= '9') {
48 end_idx++;
49 }
50 auto len = end_idx - start_idx;
51
52 auto id = str.substr(start_idx, len);
53 Symbol sym(std::stoi(id), symbols.ProgramID());
54 out << symbols.NameFor(sym);
55
56 pos = end_idx;
57 }
58
59 return out.str();
60 }
61
62 } // namespace tint
63