LLVM  14.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 std::string llvm::demangle(const std::string &MangledName) {
25  std::string Result;
26  const char *S = MangledName.c_str();
27 
28  if (nonMicrosoftDemangle(S, Result))
29  return Result;
30 
31  if (S[0] == '_' && nonMicrosoftDemangle(S + 1, Result))
32  return Result;
33 
34  if (char *Demangled =
35  microsoftDemangle(S, nullptr, nullptr, nullptr, nullptr)) {
36  Result = Demangled;
37  std::free(Demangled);
38  return Result;
39  }
40 
41  return MangledName;
42 }
43 
44 bool llvm::nonMicrosoftDemangle(const char *MangledName, std::string &Result) {
45  char *Demangled = nullptr;
46  if (isItaniumEncoding(MangledName))
47  Demangled = itaniumDemangle(MangledName, nullptr, nullptr, nullptr);
48  else if (isRustEncoding(MangledName))
49  Demangled = rustDemangle(MangledName, nullptr, nullptr, nullptr);
50 
51  if (!Demangled)
52  return false;
53 
54  Result = Demangled;
55  std::free(Demangled);
56  return true;
57 }
isItaniumEncoding
static bool isItaniumEncoding(const char *S)
Definition: Demangle.cpp:17
llvm::demangle
std::string demangle(const std::string &MangledName)
Attempt to demangle a string using different demangling schemes.
Definition: Demangle.cpp:24
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
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:2341
llvm::nonMicrosoftDemangle
bool nonMicrosoftDemangle(const char *MangledName, std::string &Result)
Definition: Demangle.cpp:44
llvm::itaniumDemangle
char * itaniumDemangle(const char *mangled_name, char *buf, size_t *n, int *status)
Definition: ItaniumDemangle.cpp:326
llvm::rustDemangle
char * rustDemangle(const char *MangledName, char *Buf, size_t *N, int *Status)
Definition: RustDemangle.cpp:150