1// Copyright 2015 Klemens Morgenstern
2//
3// This file provides a demangling for function names, i.e. entry points of a dll.
4//
5// Distributed under the Boost Software License, Version 1.0.
6// See http://www.boost.org/LICENSE_1_0.txt
7
8#ifndef BOOST_DLL_DEMANGLE_SYMBOL_HPP_
9#define BOOST_DLL_DEMANGLE_SYMBOL_HPP_
10
11#include <boost/dll/config.hpp>
12#include <string>
13#include <algorithm>
14#include <memory>
15
16#if defined(_MSC_VER) // MSVC, Clang-cl, and ICC on Windows
17
18namespace boost
19{
20namespace dll
21{
22namespace detail
23{
24
25typedef void * (__cdecl * allocation_function)(std::size_t);
26typedef void (__cdecl * free_function)(void *);
27
28extern "C" char* __unDName( char* outputString,
29 const char* name,
30 int maxStringLength, // Note, COMMA is leading following optional arguments
31 allocation_function pAlloc,
32 free_function pFree,
33 unsigned short disableFlags
34 );
35
36
37inline std::string demangle_symbol(const char *mangled_name)
38{
39
40 allocation_function alloc = [](std::size_t size){return static_cast<void*>(new char[size]);};
41 free_function free_f = [](void* p){delete [] static_cast<char*>(p);};
42
43
44
45 std::unique_ptr<char> name { __unDName(
46 nullptr,
47 mangled_name,
48 0,
49 alloc,
50 free_f,
51 static_cast<unsigned short>(0))};
52
53 return std::string(name.get());
54}
55inline std::string demangle_symbol(const std::string& mangled_name)
56{
57 return demangle_symbol(mangled_name.c_str());
58}
59
60
61}}}
62#else
63
64#include <boost/core/demangle.hpp>
65
66namespace boost
67{
68namespace dll
69{
70namespace detail
71{
72
73inline std::string demangle_symbol(const char *mangled_name)
74{
75
76 if (*mangled_name == '_')
77 {
78 //because it start's with an underline _
79 auto dm = boost::core::demangle(name: mangled_name);
80 if (!dm.empty())
81 return dm;
82 else
83 return (mangled_name);
84 }
85
86 //could not demangled
87 return "";
88
89
90}
91
92//for my personal convenience
93inline std::string demangle_symbol(const std::string& mangled_name)
94{
95 return demangle_symbol(mangled_name: mangled_name.c_str());
96}
97
98
99}
100namespace experimental
101{
102using ::boost::dll::detail::demangle_symbol;
103}
104
105}}
106
107#endif
108
109#endif /* BOOST_DEMANGLE_HPP_ */
110

source code of boost/libs/dll/include/boost/dll/detail/demangling/demangle_symbol.hpp