blob: 5f921537b9bd7c8ef1fe5ce736084180aae579c0 [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"
Konstantin Zhuravlyov8456cdd2019-01-22 19:18:18 +000014#include <cstdlib>
James Hendersonce5b5b42019-01-17 15:18:44 +000015
James Hendersonf5356942019-01-18 13:58:41 +000016static bool isItaniumEncoding(const std::string &MangledName) {
17 size_t Pos = MangledName.find_first_not_of('_');
18 // A valid Itanium encoding requires 1-4 leading underscores, followed by 'Z'.
19 return Pos > 0 && Pos <= 4 && MangledName[Pos] == 'Z';
20}
21
James Hendersonce5b5b42019-01-17 15:18:44 +000022std::string llvm::demangle(const std::string &MangledName) {
23 char *Demangled;
James Hendersonf5356942019-01-18 13:58:41 +000024 if (isItaniumEncoding(MangledName))
James Hendersonce5b5b42019-01-17 15:18:44 +000025 Demangled = itaniumDemangle(MangledName.c_str(), nullptr, nullptr, nullptr);
26 else
27 Demangled =
28 microsoftDemangle(MangledName.c_str(), nullptr, nullptr, nullptr);
29
30 if (!Demangled)
31 return MangledName;
32
33 std::string Ret = Demangled;
34 free(Demangled);
35 return Ret;
36}