1*0b57cec5SDimitry Andric //===-- Demangle.cpp - Common demangling functions ------------------------===// 2*0b57cec5SDimitry Andric // 3*0b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4*0b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information. 5*0b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6*0b57cec5SDimitry Andric // 7*0b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 8*0b57cec5SDimitry Andric /// 9*0b57cec5SDimitry Andric /// \file This file contains definitions of common demangling functions. 10*0b57cec5SDimitry Andric /// 11*0b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 12*0b57cec5SDimitry Andric 13*0b57cec5SDimitry Andric #include "llvm/Demangle/Demangle.h" 14*0b57cec5SDimitry Andric #include <cstdlib> 15*0b57cec5SDimitry Andric 16*0b57cec5SDimitry Andric static bool isItaniumEncoding(const std::string &MangledName) { 17*0b57cec5SDimitry Andric size_t Pos = MangledName.find_first_not_of('_'); 18*0b57cec5SDimitry Andric // A valid Itanium encoding requires 1-4 leading underscores, followed by 'Z'. 19*0b57cec5SDimitry Andric return Pos > 0 && Pos <= 4 && MangledName[Pos] == 'Z'; 20*0b57cec5SDimitry Andric } 21*0b57cec5SDimitry Andric 22*0b57cec5SDimitry Andric std::string llvm::demangle(const std::string &MangledName) { 23*0b57cec5SDimitry Andric char *Demangled; 24*0b57cec5SDimitry Andric if (isItaniumEncoding(MangledName)) 25*0b57cec5SDimitry Andric Demangled = itaniumDemangle(MangledName.c_str(), nullptr, nullptr, nullptr); 26*0b57cec5SDimitry Andric else 27*0b57cec5SDimitry Andric Demangled = 28*0b57cec5SDimitry Andric microsoftDemangle(MangledName.c_str(), nullptr, nullptr, nullptr); 29*0b57cec5SDimitry Andric 30*0b57cec5SDimitry Andric if (!Demangled) 31*0b57cec5SDimitry Andric return MangledName; 32*0b57cec5SDimitry Andric 33*0b57cec5SDimitry Andric std::string Ret = Demangled; 34*0b57cec5SDimitry Andric free(Demangled); 35*0b57cec5SDimitry Andric return Ret; 36*0b57cec5SDimitry Andric } 37