blob: e2a6ff488bb24807f6fe70bc40a632e496cb6e3e [file] [log] [blame]
James Hendersonce5b5b42019-01-17 15:18:44 +00001//===-- Demangle.cpp - Common demangling functions ------------------------===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
James Hendersonce5b5b42019-01-17 15:18:44 +00006//
7//===----------------------------------------------------------------------===//
8///
9/// \file This file contains definitions of common demangling functions.
10///
11//===----------------------------------------------------------------------===//
12
13#include "llvm/Demangle/Demangle.h"
14
James Hendersonf5356942019-01-18 13:58:41 +000015static bool isItaniumEncoding(const std::string &MangledName) {
16 size_t Pos = MangledName.find_first_not_of('_');
17 // A valid Itanium encoding requires 1-4 leading underscores, followed by 'Z'.
18 return Pos > 0 && Pos <= 4 && MangledName[Pos] == 'Z';
19}
20
James Hendersonce5b5b42019-01-17 15:18:44 +000021std::string llvm::demangle(const std::string &MangledName) {
22 char *Demangled;
James Hendersonf5356942019-01-18 13:58:41 +000023 if (isItaniumEncoding(MangledName))
James Hendersonce5b5b42019-01-17 15:18:44 +000024 Demangled = itaniumDemangle(MangledName.c_str(), nullptr, nullptr, nullptr);
25 else
26 Demangled =
27 microsoftDemangle(MangledName.c_str(), nullptr, nullptr, nullptr);
28
29 if (!Demangled)
30 return MangledName;
31
32 std::string Ret = Demangled;
33 free(Demangled);
34 return Ret;
35}