2020-12-11 18:29:03 +00:00
|
|
|
// Copyright 2020 The Tint Authors.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
#include "src/demangler.h"
|
|
|
|
|
2021-01-26 16:57:10 +00:00
|
|
|
#include "src/program.h"
|
|
|
|
|
2020-12-11 18:29:03 +00:00
|
|
|
namespace tint {
|
|
|
|
namespace {
|
|
|
|
|
2021-04-13 20:07:57 +00:00
|
|
|
constexpr char kSymbol[] = "$";
|
2020-12-11 18:29:03 +00:00
|
|
|
constexpr size_t kSymbolLen = sizeof(kSymbol) - 1;
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
Demangler::Demangler() = default;
|
|
|
|
|
|
|
|
Demangler::~Demangler() = default;
|
|
|
|
|
2021-01-26 16:57:10 +00:00
|
|
|
std::string Demangler::Demangle(const SymbolTable& symbols,
|
2020-12-11 18:29:03 +00:00
|
|
|
const std::string& str) const {
|
2021-03-04 16:29:05 +00:00
|
|
|
std::stringstream out;
|
2020-12-11 18:29:03 +00:00
|
|
|
|
|
|
|
size_t pos = 0;
|
|
|
|
for (;;) {
|
2021-03-04 16:29:05 +00:00
|
|
|
auto idx = str.find(kSymbol, pos);
|
|
|
|
if (idx == std::string::npos) {
|
|
|
|
out << str.substr(pos);
|
2020-12-11 18:29:03 +00:00
|
|
|
break;
|
2021-03-04 16:29:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
out << str.substr(pos, idx - pos);
|
2020-12-11 18:29:03 +00:00
|
|
|
|
|
|
|
auto start_idx = idx + kSymbolLen;
|
|
|
|
auto end_idx = start_idx;
|
2021-03-04 16:29:05 +00:00
|
|
|
while (str[end_idx] >= '0' && str[end_idx] <= '9') {
|
2020-12-11 18:29:03 +00:00
|
|
|
end_idx++;
|
|
|
|
}
|
|
|
|
auto len = end_idx - start_idx;
|
|
|
|
|
2021-03-04 16:29:05 +00:00
|
|
|
auto id = str.substr(start_idx, len);
|
2020-12-11 18:29:03 +00:00
|
|
|
Symbol sym(std::stoi(id));
|
2021-03-04 16:29:05 +00:00
|
|
|
out << symbols.NameFor(sym);
|
2020-12-11 18:29:03 +00:00
|
|
|
|
2021-03-04 16:29:05 +00:00
|
|
|
pos = end_idx;
|
2020-12-11 18:29:03 +00:00
|
|
|
}
|
|
|
|
|
2021-03-04 16:29:05 +00:00
|
|
|
return out.str();
|
2020-12-11 18:29:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace tint
|