xref: /llvm-project/llvm/lib/Demangle/Demangle.cpp (revision c2709fcb0a5b32e42a35661fab4d5744e11d04c0)
1ce5b5b48SJames Henderson //===-- Demangle.cpp - Common demangling functions ------------------------===//
2ce5b5b48SJames Henderson //
32946cd70SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
42946cd70SChandler Carruth // See https://llvm.org/LICENSE.txt for license information.
52946cd70SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6ce5b5b48SJames Henderson //
7ce5b5b48SJames Henderson //===----------------------------------------------------------------------===//
8ce5b5b48SJames Henderson ///
9ce5b5b48SJames Henderson /// \file This file contains definitions of common demangling functions.
10ce5b5b48SJames Henderson ///
11ce5b5b48SJames Henderson //===----------------------------------------------------------------------===//
12ce5b5b48SJames Henderson 
13ce5b5b48SJames Henderson #include "llvm/Demangle/Demangle.h"
148456cddeSKonstantin Zhuravlyov #include <cstdlib>
1541a6fc84STomasz Miąsko #include <cstring>
16ce5b5b48SJames Henderson 
1741a6fc84STomasz Miąsko static bool isItaniumEncoding(const char *S) {
1841a6fc84STomasz Miąsko   // Itanium encoding requires 1 or 3 leading underscores, followed by 'Z'.
1941a6fc84STomasz Miąsko   return std::strncmp(S, "_Z", 2) == 0 || std::strncmp(S, "___Z", 4) == 0;
20f5356944SJames Henderson }
21f5356944SJames Henderson 
2241a6fc84STomasz Miąsko static bool isRustEncoding(const char *S) { return S[0] == '_' && S[1] == 'R'; }
239b108560STomasz Miąsko 
2422a1aa5aSLuís Ferreira static bool isDLangEncoding(const std::string &MangledName) {
2522a1aa5aSLuís Ferreira   return MangledName.size() >= 2 && MangledName[0] == '_' &&
2622a1aa5aSLuís Ferreira          MangledName[1] == 'D';
2722a1aa5aSLuís Ferreira }
2822a1aa5aSLuís Ferreira 
29ce5b5b48SJames Henderson std::string llvm::demangle(const std::string &MangledName) {
3041a6fc84STomasz Miąsko   std::string Result;
3141a6fc84STomasz Miąsko   const char *S = MangledName.c_str();
3241a6fc84STomasz Miąsko 
3341a6fc84STomasz Miąsko   if (nonMicrosoftDemangle(S, Result))
3441a6fc84STomasz Miąsko     return Result;
3541a6fc84STomasz Miąsko 
3641a6fc84STomasz Miąsko   if (S[0] == '_' && nonMicrosoftDemangle(S + 1, Result))
3741a6fc84STomasz Miąsko     return Result;
3841a6fc84STomasz Miąsko 
39*c2709fcbSNick Desaulniers   if (char *Demangled = microsoftDemangle(S, nullptr, nullptr)) {
4041a6fc84STomasz Miąsko     Result = Demangled;
4141a6fc84STomasz Miąsko     std::free(Demangled);
4241a6fc84STomasz Miąsko     return Result;
4341a6fc84STomasz Miąsko   }
4441a6fc84STomasz Miąsko 
4541a6fc84STomasz Miąsko   return MangledName;
4641a6fc84STomasz Miąsko }
4741a6fc84STomasz Miąsko 
4841a6fc84STomasz Miąsko bool llvm::nonMicrosoftDemangle(const char *MangledName, std::string &Result) {
4941a6fc84STomasz Miąsko   char *Demangled = nullptr;
50f5356944SJames Henderson   if (isItaniumEncoding(MangledName))
5141a6fc84STomasz Miąsko     Demangled = itaniumDemangle(MangledName, nullptr, nullptr, nullptr);
529b108560STomasz Miąsko   else if (isRustEncoding(MangledName))
53201c4b9cSNathan Sidwell     Demangled = rustDemangle(MangledName);
5422a1aa5aSLuís Ferreira   else if (isDLangEncoding(MangledName))
5522a1aa5aSLuís Ferreira     Demangled = dlangDemangle(MangledName);
56ce5b5b48SJames Henderson 
57ce5b5b48SJames Henderson   if (!Demangled)
5841a6fc84STomasz Miąsko     return false;
59ce5b5b48SJames Henderson 
6041a6fc84STomasz Miąsko   Result = Demangled;
6178f543e5SLouis Dionne   std::free(Demangled);
6241a6fc84STomasz Miąsko   return true;
63ce5b5b48SJames Henderson }
64