blob: d68a62a825937f6ec9e579a3bd63bee9bab3b565 [file] [log] [blame]
Ryan Harrisondbc13af2022-02-21 15:19:07 +00001// 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
19namespace tint {
20namespace {
21
22constexpr char kSymbol[] = "$";
23constexpr size_t kSymbolLen = sizeof(kSymbol) - 1;
24
25} // namespace
26
27Demangler::Demangler() = default;
28
29Demangler::~Demangler() = default;
30
dan sinclair41e4d9a2022-05-01 14:40:55 +000031std::string Demangler::Demangle(const SymbolTable& symbols, const std::string& str) const {
32 std::stringstream out;
Ryan Harrisondbc13af2022-02-21 15:19:07 +000033
dan sinclair41e4d9a2022-05-01 14:40:55 +000034 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 sinclair3a2a2792022-06-29 14:38:15 +000052 Symbol sym(static_cast<uint32_t>(std::stoi(id)), symbols.ProgramID());
dan sinclair41e4d9a2022-05-01 14:40:55 +000053 out << symbols.NameFor(sym);
54
55 pos = end_idx;
Ryan Harrisondbc13af2022-02-21 15:19:07 +000056 }
57
dan sinclair41e4d9a2022-05-01 14:40:55 +000058 return out.str();
Ryan Harrisondbc13af2022-02-21 15:19:07 +000059}
60
61} // namespace tint