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 <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(self: MangledName, C: '_') &&
28 nonMicrosoftDemangle(MangledName: MangledName.substr(pos: 1), Result,
29 /*CanHaveLeadingDot=*/false))
30 return Result;
31
32 if (char *Demangled = microsoftDemangle(mangled_name: MangledName, n_read: nullptr, status: nullptr)) {
33 Result = Demangled;
34 std::free(ptr: Demangled);
35 } else {
36 Result = MangledName;
37 }
38 return Result;
39}
40
41static bool isItaniumEncoding(std::string_view S) {
42 if (starts_with(haystack: S, needle: "__alloc_token_")) {
43 S.remove_prefix(n: sizeof("__alloc_token_") - 1);
44 if (!S.empty() && std::isdigit(S[0])) {
45 while (!S.empty() && std::isdigit(S[0]))
46 S.remove_prefix(n: 1);
47 if (starts_with(haystack: S, needle: "_"))
48 S.remove_prefix(n: 1);
49 }
50 }
51 // Itanium demangler supports prefixes with 1-4 underscores.
52 const size_t Pos = S.find_first_not_of(c: '_');
53 return Pos > 0 && Pos <= 4 && S[Pos] == 'Z';
54}
55
56static bool isRustEncoding(std::string_view S) { return starts_with(haystack: S, needle: "_R"); }
57
58static bool isDLangEncoding(std::string_view S) { return starts_with(haystack: S, needle: "_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(n: 1);
68 Result = ".";
69 }
70
71 if (isItaniumEncoding(S: MangledName))
72 Demangled = itaniumDemangle(mangled_name: MangledName, ParseParams);
73 else if (isRustEncoding(S: MangledName))
74 Demangled = rustDemangle(MangledName);
75 else if (isDLangEncoding(S: MangledName))
76 Demangled = dlangDemangle(MangledName);
77
78 if (!Demangled)
79 return false;
80
81 Result += Demangled;
82 std::free(ptr: Demangled);
83 return true;
84}
85