summaryrefslogtreecommitdiffstats
path: root/src/common/demangle.cpp
blob: f4246f66680797470675ab7a71ae88ab4fc70d5c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
// SPDX-FileCopyrightText: Copyright 2020 yuzu Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later

#include "common/demangle.h"
#include "common/scope_exit.h"

namespace llvm {
char* itaniumDemangle(const char* mangled_name, char* buf, size_t* n, int* status);
}

namespace Common {

std::string DemangleSymbol(const std::string& mangled) {
    auto is_itanium = [](const std::string& name) -> bool {
        // A valid Itanium encoding requires 1-4 leading underscores, followed by 'Z'.
        auto pos = name.find_first_not_of('_');
        return pos > 0 && pos <= 4 && pos < name.size() && name[pos] == 'Z';
    };

    if (mangled.empty()) {
        return mangled;
    }

    char* demangled = nullptr;
    SCOPE_EXIT({ std::free(demangled); });

    if (is_itanium(mangled)) {
        demangled = llvm::itaniumDemangle(mangled.c_str(), nullptr, nullptr, nullptr);
    }

    if (!demangled) {
        return mangled;
    }
    return demangled;
}

} // namespace Common