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 "llvm/Demangle/StringViewExtras.h" 15 #include <cstdlib> 16 17 using llvm::itanium_demangle::starts_with; 18 19 std::string llvm::demangle(const std::string_view MangledName) { 20 std::string Result; 21 const char *S = MangledName.data(); 22 23 if (nonMicrosoftDemangle(S, Result)) 24 return Result; 25 26 if (S[0] == '_' && nonMicrosoftDemangle(S + 1, Result)) 27 return Result; 28 29 if (char *Demangled = microsoftDemangle(S, nullptr, nullptr)) { 30 Result = Demangled; 31 std::free(Demangled); 32 } else { 33 Result = MangledName; 34 } 35 return Result; 36 } 37 38 static bool isItaniumEncoding(std::string_view S) { 39 return starts_with(S, "_Z") || starts_with(S, "___Z"); 40 } 41 42 static bool isRustEncoding(std::string_view S) { return starts_with(S, "_R"); } 43 44 static bool isDLangEncoding(std::string_view S) { return starts_with(S, "_D"); } 45 46 bool llvm::nonMicrosoftDemangle(const char *MangledName, std::string &Result) { 47 char *Demangled = nullptr; 48 if (isItaniumEncoding(MangledName)) 49 Demangled = itaniumDemangle(MangledName, nullptr, nullptr, nullptr); 50 else if (isRustEncoding(MangledName)) 51 Demangled = rustDemangle(MangledName); 52 else if (isDLangEncoding(MangledName)) 53 Demangled = dlangDemangle(MangledName); 54 55 if (!Demangled) 56 return false; 57 58 Result = Demangled; 59 std::free(Demangled); 60 return true; 61 } 62