xref: /freebsd/contrib/llvm-project/llvm/lib/Demangle/Demangle.cpp (revision 06c3fb2749bda94cb5201f81ffdb8fa6c3161b2e)
10b57cec5SDimitry Andric //===-- Demangle.cpp - Common demangling functions ------------------------===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric ///
90b57cec5SDimitry Andric /// \file This file contains definitions of common demangling functions.
100b57cec5SDimitry Andric ///
110b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
120b57cec5SDimitry Andric 
130b57cec5SDimitry Andric #include "llvm/Demangle/Demangle.h"
14*06c3fb27SDimitry Andric #include "llvm/Demangle/StringViewExtras.h"
150b57cec5SDimitry Andric #include <cstdlib>
16*06c3fb27SDimitry Andric #include <string_view>
170b57cec5SDimitry Andric 
18*06c3fb27SDimitry Andric using llvm::itanium_demangle::starts_with;
190b57cec5SDimitry Andric 
20*06c3fb27SDimitry Andric std::string llvm::demangle(std::string_view MangledName) {
21349cc55cSDimitry Andric   std::string Result;
22349cc55cSDimitry Andric 
23*06c3fb27SDimitry Andric   if (nonMicrosoftDemangle(MangledName, Result))
24349cc55cSDimitry Andric     return Result;
25349cc55cSDimitry Andric 
26*06c3fb27SDimitry Andric   if (starts_with(MangledName, '_') &&
27*06c3fb27SDimitry Andric       nonMicrosoftDemangle(MangledName.substr(1), Result))
28349cc55cSDimitry Andric     return Result;
29349cc55cSDimitry Andric 
30*06c3fb27SDimitry Andric   if (char *Demangled = microsoftDemangle(MangledName, nullptr, nullptr)) {
31349cc55cSDimitry Andric     Result = Demangled;
32349cc55cSDimitry Andric     std::free(Demangled);
33*06c3fb27SDimitry Andric   } else {
34*06c3fb27SDimitry Andric     Result = MangledName;
35*06c3fb27SDimitry Andric   }
36349cc55cSDimitry Andric   return Result;
37349cc55cSDimitry Andric }
38349cc55cSDimitry Andric 
39*06c3fb27SDimitry Andric static bool isItaniumEncoding(std::string_view S) {
40*06c3fb27SDimitry Andric   // Itanium encoding requires 1 or 3 leading underscores, followed by 'Z'.
41*06c3fb27SDimitry Andric   return starts_with(S, "_Z") || starts_with(S, "___Z");
42349cc55cSDimitry Andric }
43349cc55cSDimitry Andric 
44*06c3fb27SDimitry Andric static bool isRustEncoding(std::string_view S) { return starts_with(S, "_R"); }
45*06c3fb27SDimitry Andric 
46*06c3fb27SDimitry Andric static bool isDLangEncoding(std::string_view S) { return starts_with(S, "_D"); }
47*06c3fb27SDimitry Andric 
48*06c3fb27SDimitry Andric bool llvm::nonMicrosoftDemangle(std::string_view MangledName,
49*06c3fb27SDimitry Andric                                 std::string &Result) {
50349cc55cSDimitry Andric   char *Demangled = nullptr;
510b57cec5SDimitry Andric   if (isItaniumEncoding(MangledName))
52*06c3fb27SDimitry Andric     Demangled = itaniumDemangle(MangledName);
53fe6060f1SDimitry Andric   else if (isRustEncoding(MangledName))
5481ad6265SDimitry Andric     Demangled = rustDemangle(MangledName);
55349cc55cSDimitry Andric   else if (isDLangEncoding(MangledName))
56349cc55cSDimitry Andric     Demangled = dlangDemangle(MangledName);
570b57cec5SDimitry Andric 
580b57cec5SDimitry Andric   if (!Demangled)
59349cc55cSDimitry Andric     return false;
600b57cec5SDimitry Andric 
61349cc55cSDimitry Andric   Result = Demangled;
62e8d8bef9SDimitry Andric   std::free(Demangled);
63349cc55cSDimitry Andric   return true;
640b57cec5SDimitry Andric }
65