Files
llvm-project/llvm/lib/Demangle/Demangle.cpp
Marco Elver fb6f1bde00 [ItaniumDemangle] Strip __alloc_token_ to transparently demangle allocation functions (#191048)
Update the Itanium demangler to recognize and strip `__alloc_token_`
prefixes introduced by AllocToken instrumentation [1]. This ensures that
instrumented allocation functions (e.g., `__alloc_token__Znwm`) demangle
back to their original source-level names (e.g., `operator new(unsigned
long)`) with a suffix `(.alloc_token)` indicating their non-standard
origin.

Since AllocToken is intended to be transparent to users who continue to
use `operator new` as before, the demangled name should reflect this
reality instead of confusing users with internal instrumentation names
in stack traces or symbolization output.

Synchronize changes across llvm and libcxxabi copies.

[1] https://clang.llvm.org/docs/AllocToken.html

Original RFC: https://discourse.llvm.org/t/rfc-a-framework-for-allocator-partitioning-hints/87434
2026-04-22 17:57:30 +02:00

85 lines
2.6 KiB
C++

//===-- 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"
#include "llvm/Demangle/StringViewExtras.h"
#include <cctype>
#include <cstdlib>
#include <string_view>
using llvm::itanium_demangle::starts_with;
std::string llvm::demangle(std::string_view MangledName) {
std::string Result;
if (nonMicrosoftDemangle(MangledName, Result))
return Result;
if (starts_with(MangledName, '_') &&
nonMicrosoftDemangle(MangledName.substr(1), Result,
/*CanHaveLeadingDot=*/false))
return Result;
if (char *Demangled = microsoftDemangle(MangledName, nullptr, nullptr)) {
Result = Demangled;
std::free(Demangled);
} else {
Result = MangledName;
}
return Result;
}
static bool isItaniumEncoding(std::string_view S) {
if (starts_with(S, "__alloc_token_")) {
S.remove_prefix(sizeof("__alloc_token_") - 1);
if (!S.empty() && std::isdigit(S[0])) {
while (!S.empty() && std::isdigit(S[0]))
S.remove_prefix(1);
if (starts_with(S, "_"))
S.remove_prefix(1);
}
}
// Itanium demangler supports prefixes with 1-4 underscores.
const size_t Pos = S.find_first_not_of('_');
return Pos > 0 && Pos <= 4 && S[Pos] == 'Z';
}
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,
std::string &Result, bool CanHaveLeadingDot,
bool ParseParams) {
char *Demangled = nullptr;
// 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 = ".";
}
if (isItaniumEncoding(MangledName))
Demangled = itaniumDemangle(MangledName, ParseParams);
else if (isRustEncoding(MangledName))
Demangled = rustDemangle(MangledName);
else if (isDLangEncoding(MangledName))
Demangled = dlangDemangle(MangledName);
if (!Demangled)
return false;
Result += Demangled;
std::free(Demangled);
return true;
}