LLVM 23.0.0git
Demangle.cpp
Go to the documentation of this file.
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
15#include <cctype>
16#include <cstdlib>
17#include <string_view>
18
19using llvm::itanium_demangle::starts_with;
20
21std::string llvm::demangle(std::string_view MangledName) {
22 std::string Result;
23
24 if (nonMicrosoftDemangle(MangledName, Result))
25 return Result;
26
27 if (starts_with(MangledName, '_') &&
28 nonMicrosoftDemangle(MangledName.substr(1), Result,
29 /*CanHaveLeadingDot=*/false))
30 return Result;
31
32 if (char *Demangled = microsoftDemangle(MangledName, nullptr, nullptr)) {
33 Result = Demangled;
34 std::free(Demangled);
35 } else {
36 Result = MangledName;
37 }
38 return Result;
39}
40
41static bool isItaniumEncoding(std::string_view S) {
42 if (starts_with(S, "__alloc_token_")) {
43 S.remove_prefix(sizeof("__alloc_token_") - 1);
44 if (!S.empty() && std::isdigit(S[0])) {
45 while (!S.empty() && std::isdigit(S[0]))
46 S.remove_prefix(1);
47 if (starts_with(S, "_"))
48 S.remove_prefix(1);
49 }
50 }
51 // Itanium demangler supports prefixes with 1-4 underscores.
52 const size_t Pos = S.find_first_not_of('_');
53 return Pos > 0 && Pos <= 4 && S[Pos] == 'Z';
54}
55
56static bool isRustEncoding(std::string_view S) { return starts_with(S, "_R"); }
57
58static bool isDLangEncoding(std::string_view S) { return starts_with(S, "_D"); }
59
60bool llvm::nonMicrosoftDemangle(std::string_view MangledName,
61 std::string &Result, bool CanHaveLeadingDot,
62 bool ParseParams) {
63 char *Demangled = nullptr;
64
65 // Do not consider the dot prefix as part of the demangled symbol name.
66 if (CanHaveLeadingDot && MangledName.size() > 0 && MangledName[0] == '.') {
67 MangledName.remove_prefix(1);
68 Result = ".";
69 }
70
71 if (isItaniumEncoding(MangledName))
72 Demangled = itaniumDemangle(MangledName, ParseParams);
73 else if (isRustEncoding(MangledName))
74 Demangled = rustDemangle(MangledName);
75 else if (isDLangEncoding(MangledName))
76 Demangled = dlangDemangle(MangledName);
77
78 if (!Demangled)
79 return false;
80
81 Result += Demangled;
82 std::free(Demangled);
83 return true;
84}
static bool isRustEncoding(std::string_view S)
Definition Demangle.cpp:56
static bool isDLangEncoding(std::string_view S)
Definition Demangle.cpp:58
static bool isItaniumEncoding(std::string_view S)
Definition Demangle.cpp:41
DEMANGLE_NAMESPACE_BEGIN bool starts_with(std::string_view self, char C) noexcept
DEMANGLE_ABI bool nonMicrosoftDemangle(std::string_view MangledName, std::string &Result, bool CanHaveLeadingDot=true, bool ParseParams=true)
Definition Demangle.cpp:60
DEMANGLE_ABI char * itaniumDemangle(std::string_view mangled_name, bool ParseParams=true)
Returns a non-NULL pointer to a NUL-terminated C style string that should be explicitly freed,...
DEMANGLE_ABI char * dlangDemangle(std::string_view MangledName)
DEMANGLE_ABI char * rustDemangle(std::string_view MangledName)
DEMANGLE_ABI char * microsoftDemangle(std::string_view mangled_name, size_t *n_read, int *status, MSDemangleFlags Flags=MSDF_None)
Demangles the Microsoft symbol pointed at by mangled_name and returns it.
DEMANGLE_ABI std::string demangle(std::string_view MangledName)
Attempt to demangle a string using different demangling schemes.
Definition Demangle.cpp:21