1 //===-- Demangle.cpp - Common demangling functions ------------------------===//
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
7 //===----------------------------------------------------------------------===//
9 /// \file This file contains definitions of common demangling functions.
11 //===----------------------------------------------------------------------===//
13 #include "llvm/Demangle/Demangle.h"
17 static bool isItaniumEncoding(const char *S
) {
18 // Itanium encoding requires 1 or 3 leading underscores, followed by 'Z'.
19 return std::strncmp(S
, "_Z", 2) == 0 || std::strncmp(S
, "___Z", 4) == 0;
22 static bool isRustEncoding(const char *S
) { return S
[0] == '_' && S
[1] == 'R'; }
24 static bool isDLangEncoding(const std::string
&MangledName
) {
25 return MangledName
.size() >= 2 && MangledName
[0] == '_' &&
26 MangledName
[1] == 'D';
29 std::string
llvm::demangle(const std::string
&MangledName
) {
31 const char *S
= MangledName
.c_str();
33 if (nonMicrosoftDemangle(S
, Result
))
36 if (S
[0] == '_' && nonMicrosoftDemangle(S
+ 1, Result
))
40 microsoftDemangle(S
, nullptr, nullptr, nullptr, nullptr)) {
49 bool llvm::nonMicrosoftDemangle(const char *MangledName
, std::string
&Result
) {
50 char *Demangled
= nullptr;
51 if (isItaniumEncoding(MangledName
))
52 Demangled
= itaniumDemangle(MangledName
, nullptr, nullptr, nullptr);
53 else if (isRustEncoding(MangledName
))
54 Demangled
= rustDemangle(MangledName
, nullptr, nullptr, nullptr);
55 else if (isDLangEncoding(MangledName
))
56 Demangled
= dlangDemangle(MangledName
);