James Henderson | ce5b5b4 | 2019-01-17 15:18:44 +0000 | [diff] [blame] | 1 | //===-- Demangle.cpp - Common demangling functions ------------------------===// |
| 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 3 | // 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 Henderson | ce5b5b4 | 2019-01-17 15:18:44 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | /// |
| 9 | /// \file This file contains definitions of common demangling functions. |
| 10 | /// |
| 11 | //===----------------------------------------------------------------------===// |
| 12 | |
| 13 | #include "llvm/Demangle/Demangle.h" |
Konstantin Zhuravlyov | 8456cdd | 2019-01-22 19:18:18 +0000 | [diff] [blame] | 14 | #include <cstdlib> |
James Henderson | ce5b5b4 | 2019-01-17 15:18:44 +0000 | [diff] [blame] | 15 | |
James Henderson | f535694 | 2019-01-18 13:58:41 +0000 | [diff] [blame] | 16 | static 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 Henderson | ce5b5b4 | 2019-01-17 15:18:44 +0000 | [diff] [blame] | 22 | std::string llvm::demangle(const std::string &MangledName) { |
| 23 | char *Demangled; |
James Henderson | f535694 | 2019-01-18 13:58:41 +0000 | [diff] [blame] | 24 | if (isItaniumEncoding(MangledName)) |
James Henderson | ce5b5b4 | 2019-01-17 15:18:44 +0000 | [diff] [blame] | 25 | 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 | } |