llvm-project
73 строки · 2.3 Кб
1//===-- Demangle.cpp - Common demangling functions ------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8///
9/// \file This file contains definitions of common demangling functions.
10///
11//===----------------------------------------------------------------------===//
12
13#include "llvm/Demangle/Demangle.h"
14#include "llvm/Demangle/StringViewExtras.h"
15#include <cstdlib>
16#include <string_view>
17
18using llvm::itanium_demangle::starts_with;
19
20std::string llvm::demangle(std::string_view MangledName) {
21std::string Result;
22
23if (nonMicrosoftDemangle(MangledName, Result))
24return Result;
25
26if (starts_with(MangledName, '_') &&
27nonMicrosoftDemangle(MangledName.substr(1), Result,
28/*CanHaveLeadingDot=*/false))
29return Result;
30
31if (char *Demangled = microsoftDemangle(MangledName, nullptr, nullptr)) {
32Result = Demangled;
33std::free(Demangled);
34} else {
35Result = MangledName;
36}
37return Result;
38}
39
40static bool isItaniumEncoding(std::string_view S) {
41// Itanium encoding requires 1 or 3 leading underscores, followed by 'Z'.
42return starts_with(S, "_Z") || starts_with(S, "___Z");
43}
44
45static bool isRustEncoding(std::string_view S) { return starts_with(S, "_R"); }
46
47static bool isDLangEncoding(std::string_view S) { return starts_with(S, "_D"); }
48
49bool llvm::nonMicrosoftDemangle(std::string_view MangledName,
50std::string &Result, bool CanHaveLeadingDot,
51bool ParseParams) {
52char *Demangled = nullptr;
53
54// Do not consider the dot prefix as part of the demangled symbol name.
55if (CanHaveLeadingDot && MangledName.size() > 0 && MangledName[0] == '.') {
56MangledName.remove_prefix(1);
57Result = ".";
58}
59
60if (isItaniumEncoding(MangledName))
61Demangled = itaniumDemangle(MangledName, ParseParams);
62else if (isRustEncoding(MangledName))
63Demangled = rustDemangle(MangledName);
64else if (isDLangEncoding(MangledName))
65Demangled = dlangDemangle(MangledName);
66
67if (!Demangled)
68return false;
69
70Result += Demangled;
71std::free(Demangled);
72return true;
73}
74