2022-08-09 13:51:03 +02:00
|
|
|
//===-- Demangle.cpp - Common demangling functions ------------------------===//
|
|
|
|
//
|
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
///
|
|
|
|
/// \file This file contains definitions of common demangling functions.
|
|
|
|
///
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "llvm/Demangle/Demangle.h"
|
2023-11-10 20:47:08 +01:00
|
|
|
#include "llvm/Demangle/StringViewExtras.h"
|
2022-08-09 13:51:03 +02:00
|
|
|
#include <cstdlib>
|
2023-11-10 20:47:08 +01:00
|
|
|
#include <string_view>
|
2022-08-09 13:51:03 +02:00
|
|
|
|
2023-11-10 20:47:08 +01:00
|
|
|
using llvm::itanium_demangle::starts_with;
|
2022-08-09 13:51:03 +02:00
|
|
|
|
2023-11-10 20:47:08 +01:00
|
|
|
std::string llvm::demangle(std::string_view MangledName) {
|
2022-08-09 13:51:03 +02:00
|
|
|
std::string Result;
|
|
|
|
|
2023-11-10 20:47:08 +01:00
|
|
|
if (nonMicrosoftDemangle(MangledName, Result))
|
2022-08-09 13:51:03 +02:00
|
|
|
return Result;
|
|
|
|
|
2023-11-10 20:47:08 +01:00
|
|
|
if (starts_with(MangledName, '_') &&
|
2024-12-26 14:41:43 +01:00
|
|
|
nonMicrosoftDemangle(MangledName.substr(1), Result,
|
|
|
|
/*CanHaveLeadingDot=*/false))
|
2022-08-09 13:51:03 +02:00
|
|
|
return Result;
|
|
|
|
|
2023-11-10 20:47:08 +01:00
|
|
|
if (char *Demangled = microsoftDemangle(MangledName, nullptr, nullptr)) {
|
2022-08-09 13:51:03 +02:00
|
|
|
Result = Demangled;
|
|
|
|
std::free(Demangled);
|
2023-11-10 20:47:08 +01:00
|
|
|
} else {
|
|
|
|
Result = MangledName;
|
2022-08-09 13:51:03 +02:00
|
|
|
}
|
2023-11-10 20:47:08 +01:00
|
|
|
return Result;
|
|
|
|
}
|
2022-08-09 13:51:03 +02:00
|
|
|
|
2023-11-10 20:47:08 +01:00
|
|
|
static bool isItaniumEncoding(std::string_view S) {
|
|
|
|
// Itanium encoding requires 1 or 3 leading underscores, followed by 'Z'.
|
|
|
|
return starts_with(S, "_Z") || starts_with(S, "___Z");
|
2022-08-09 13:51:03 +02:00
|
|
|
}
|
|
|
|
|
2023-11-10 20:47:08 +01:00
|
|
|
static bool isRustEncoding(std::string_view S) { return starts_with(S, "_R"); }
|
|
|
|
|
|
|
|
static bool isDLangEncoding(std::string_view S) { return starts_with(S, "_D"); }
|
|
|
|
|
|
|
|
bool llvm::nonMicrosoftDemangle(std::string_view MangledName,
|
2024-12-26 14:41:43 +01:00
|
|
|
std::string &Result, bool CanHaveLeadingDot,
|
|
|
|
bool ParseParams) {
|
2022-08-09 13:51:03 +02:00
|
|
|
char *Demangled = nullptr;
|
2024-12-26 14:41:43 +01:00
|
|
|
|
|
|
|
// Do not consider the dot prefix as part of the demangled symbol name.
|
|
|
|
if (CanHaveLeadingDot && MangledName.size() > 0 && MangledName[0] == '.') {
|
|
|
|
MangledName.remove_prefix(1);
|
|
|
|
Result = ".";
|
|
|
|
}
|
|
|
|
|
2022-08-09 13:51:03 +02:00
|
|
|
if (isItaniumEncoding(MangledName))
|
2024-12-26 14:41:43 +01:00
|
|
|
Demangled = itaniumDemangle(MangledName, ParseParams);
|
2022-08-09 13:51:03 +02:00
|
|
|
else if (isRustEncoding(MangledName))
|
|
|
|
Demangled = rustDemangle(MangledName);
|
|
|
|
else if (isDLangEncoding(MangledName))
|
|
|
|
Demangled = dlangDemangle(MangledName);
|
|
|
|
|
|
|
|
if (!Demangled)
|
|
|
|
return false;
|
|
|
|
|
2024-12-26 14:41:43 +01:00
|
|
|
Result += Demangled;
|
2022-08-09 13:51:03 +02:00
|
|
|
std::free(Demangled);
|
|
|
|
return true;
|
|
|
|
}
|