10b57cec5SDimitry Andric //===-- Demangle.cpp - Common demangling functions ------------------------===// 20b57cec5SDimitry Andric // 30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information. 50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 60b57cec5SDimitry Andric // 70b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 80b57cec5SDimitry Andric /// 90b57cec5SDimitry Andric /// \file This file contains definitions of common demangling functions. 100b57cec5SDimitry Andric /// 110b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 120b57cec5SDimitry Andric 130b57cec5SDimitry Andric #include "llvm/Demangle/Demangle.h" 140b57cec5SDimitry Andric #include <cstdlib> 150b57cec5SDimitry Andric 160b57cec5SDimitry Andric static bool isItaniumEncoding(const std::string &MangledName) { 170b57cec5SDimitry Andric size_t Pos = MangledName.find_first_not_of('_'); 180b57cec5SDimitry Andric // A valid Itanium encoding requires 1-4 leading underscores, followed by 'Z'. 190b57cec5SDimitry Andric return Pos > 0 && Pos <= 4 && MangledName[Pos] == 'Z'; 200b57cec5SDimitry Andric } 210b57cec5SDimitry Andric 220b57cec5SDimitry Andric std::string llvm::demangle(const std::string &MangledName) { 230b57cec5SDimitry Andric char *Demangled; 240b57cec5SDimitry Andric if (isItaniumEncoding(MangledName)) 250b57cec5SDimitry Andric Demangled = itaniumDemangle(MangledName.c_str(), nullptr, nullptr, nullptr); 260b57cec5SDimitry Andric else 275ffd83dbSDimitry Andric Demangled = microsoftDemangle(MangledName.c_str(), nullptr, nullptr, 285ffd83dbSDimitry Andric nullptr, nullptr); 290b57cec5SDimitry Andric 300b57cec5SDimitry Andric if (!Demangled) 310b57cec5SDimitry Andric return MangledName; 320b57cec5SDimitry Andric 330b57cec5SDimitry Andric std::string Ret = Demangled; 34*e8d8bef9SDimitry Andric std::free(Demangled); 350b57cec5SDimitry Andric return Ret; 360b57cec5SDimitry Andric } 37