Path: blob/main/contrib/llvm-project/llvm/lib/Demangle/Demangle.cpp
35232 views
//===-- Demangle.cpp - Common demangling functions ------------------------===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//7///8/// \file This file contains definitions of common demangling functions.9///10//===----------------------------------------------------------------------===//1112#include "llvm/Demangle/Demangle.h"13#include "llvm/Demangle/StringViewExtras.h"14#include <cstdlib>15#include <string_view>1617using llvm::itanium_demangle::starts_with;1819std::string llvm::demangle(std::string_view MangledName) {20std::string Result;2122if (nonMicrosoftDemangle(MangledName, Result))23return Result;2425if (starts_with(MangledName, '_') &&26nonMicrosoftDemangle(MangledName.substr(1), Result,27/*CanHaveLeadingDot=*/false))28return Result;2930if (char *Demangled = microsoftDemangle(MangledName, nullptr, nullptr)) {31Result = Demangled;32std::free(Demangled);33} else {34Result = MangledName;35}36return Result;37}3839static bool isItaniumEncoding(std::string_view S) {40// Itanium encoding requires 1 or 3 leading underscores, followed by 'Z'.41return starts_with(S, "_Z") || starts_with(S, "___Z");42}4344static bool isRustEncoding(std::string_view S) { return starts_with(S, "_R"); }4546static bool isDLangEncoding(std::string_view S) { return starts_with(S, "_D"); }4748bool llvm::nonMicrosoftDemangle(std::string_view MangledName,49std::string &Result, bool CanHaveLeadingDot,50bool ParseParams) {51char *Demangled = nullptr;5253// Do not consider the dot prefix as part of the demangled symbol name.54if (CanHaveLeadingDot && MangledName.size() > 0 && MangledName[0] == '.') {55MangledName.remove_prefix(1);56Result = ".";57}5859if (isItaniumEncoding(MangledName))60Demangled = itaniumDemangle(MangledName, ParseParams);61else if (isRustEncoding(MangledName))62Demangled = rustDemangle(MangledName);63else if (isDLangEncoding(MangledName))64Demangled = dlangDemangle(MangledName);6566if (!Demangled)67return false;6869Result += Demangled;70std::free(Demangled);71return true;72}737475