LLVM  15.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 
13 #include "llvm/Demangle/Demangle.h"
14 #include <cstdlib>
15 #include <cstring>
16 
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;
20 }
21 
22 static bool isRustEncoding(const char *S) { return S[0] == '_' && S[1] == 'R'; }
23 
24 static bool isDLangEncoding(const std::string &MangledName) {
25  return MangledName.size() >= 2 && MangledName[0] == '_' &&
26  MangledName[1] == 'D';
27 }
28 
29 std::string llvm::demangle(const std::string &MangledName) {
30  std::string Result;
31  const char *S = MangledName.c_str();
32 
33  if (nonMicrosoftDemangle(S, Result))
34  return Result;
35 
36  if (S[0] == '_' && nonMicrosoftDemangle(S + 1, Result))
37  return Result;
38 
39  if (char *Demangled =
40  microsoftDemangle(S, nullptr, nullptr, nullptr, nullptr)) {
41  Result = Demangled;
42  std::free(Demangled);
43  return Result;
44  }
45 
46  return MangledName;
47 }
48 
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);
55  else if (isDLangEncoding(MangledName))
56  Demangled = dlangDemangle(MangledName);
57 
58  if (!Demangled)
59  return false;
60 
61  Result = Demangled;
62  std::free(Demangled);
63  return true;
64 }
isItaniumEncoding
static bool isItaniumEncoding(const char *S)
Definition: Demangle.cpp:17
llvm::rustDemangle
char * rustDemangle(const char *MangledName)
Definition: RustDemangle.cpp:150
llvm::demangle
std::string demangle(const std::string &MangledName)
Attempt to demangle a string using different demangling schemes.
Definition: Demangle.cpp:29
Demangle.h
S
add sub stmia L5 ldr r0 bl L_printf $stub Instead of a and a wouldn t it be better to do three moves *Return an aggregate type is even return S
Definition: README.txt:210
llvm::dlangDemangle
char * dlangDemangle(const char *MangledName)
Definition: DLangDemangle.cpp:546
isRustEncoding
static bool isRustEncoding(const char *S)
Definition: Demangle.cpp:22
llvm::microsoftDemangle
char * microsoftDemangle(const char *mangled_name, size_t *n_read, char *buf, size_t *n_buf, int *status, MSDemangleFlags Flags=MSDF_None)
Demangles the Microsoft symbol pointed at by mangled_name and returns it.
Definition: MicrosoftDemangle.cpp:2334
llvm::nonMicrosoftDemangle
bool nonMicrosoftDemangle(const char *MangledName, std::string &Result)
Definition: Demangle.cpp:49
llvm::itaniumDemangle
char * itaniumDemangle(const char *mangled_name, char *buf, size_t *n, int *status)
Definition: ItaniumDemangle.cpp:368
isDLangEncoding
static bool isDLangEncoding(const std::string &MangledName)
Definition: Demangle.cpp:24