piranha  0.10
demangle.hpp
1 /* Copyright 2009-2017 Francesco Biscani (bluescarni@gmail.com)
2 
3 This file is part of the Piranha library.
4 
5 The Piranha library is free software; you can redistribute it and/or modify
6 it under the terms of either:
7 
8  * the GNU Lesser General Public License as published by the Free
9  Software Foundation; either version 3 of the License, or (at your
10  option) any later version.
11 
12 or
13 
14  * the GNU General Public License as published by the Free Software
15  Foundation; either version 3 of the License, or (at your option) any
16  later version.
17 
18 or both in parallel, as here.
19 
20 The Piranha library is distributed in the hope that it will be useful, but
21 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
22 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
23 for more details.
24 
25 You should have received copies of the GNU General Public License and the
26 GNU Lesser General Public License along with the Piranha library. If not,
27 see https://www.gnu.org/licenses/. */
28 
29 #ifndef PIRANHA_DETAIL_DEMANGLE_HPP
30 #define PIRANHA_DETAIL_DEMANGLE_HPP
31 
32 #include <boost/version.hpp>
33 #include <string>
34 #include <typeindex>
35 #include <typeinfo>
36 
37 #if BOOST_VERSION / 100000 > 1 || (BOOST_VERSION / 100000 == 1 && BOOST_VERSION / 100 % 1000 >= 56)
38 
39 // Boost demangle is available since 1.56.
40 #include <boost/core/demangle.hpp>
41 #define PIRANHA_HAVE_BOOST_DEMANGLE
42 
43 #elif defined(__GNUC__)
44 
45 // GCC demangle. This is available also for clang, both with libstdc++ and libc++.
46 #include <cstdlib>
47 #include <cxxabi.h>
48 #include <memory>
49 
50 #endif
51 
52 namespace piranha
53 {
54 namespace detail
55 {
56 
57 inline std::string demangle(const char *s)
58 {
59 #if defined(PIRANHA_HAVE_BOOST_DEMANGLE)
60  return boost::core::demangle(s);
61 #undef PIRANHA_HAVE_BOOST_DEMANGLE
62 #elif defined(__GNUC__)
63  int status = -4;
64  // NOTE: abi::__cxa_demangle will return a pointer allocated by std::malloc, which we will delete via std::free.
65  std::unique_ptr<char, void (*)(void *)> res{::abi::__cxa_demangle(s, nullptr, nullptr, &status), std::free};
66  // NOTE: it seems like clang with libc++ does not set the status variable properly.
67  // We then check if anything was allocated by __cxa_demangle(), as here it mentions
68  // that in case of failure the pointer will be set to null:
69  // https://gcc.gnu.org/onlinedocs/libstdc++/libstdc++-html-USERS-4.3/a01696.html
70  return res.get() ? std::string(res.get()) : std::string(s);
71 #else
72  // TODO demangling for other platforms. E.g.,
73  // http://stackoverflow.com/questions/13777681/demangling-in-msvc
74  // NOTE: it seems that the Boost implementation currently covers
75  // only GCC/Clang. So there might be value in having an MSVC demangler
76  // (eventually) and using it even if the Boost one is available.
77  return std::string(s);
78 #endif
79 }
80 
81 // C++ string overload.
82 inline std::string demangle(const std::string &s)
83 {
84  return demangle(s.c_str());
85 }
86 
87 // Convenience overload for demangling type_index. Will also work with type_info
88 // due to implicit conversion.
89 inline std::string demangle(const std::type_index &t_idx)
90 {
91  return demangle(t_idx.name());
92 }
93 
94 // Convenience overload with template type.
95 template <typename T>
96 inline std::string demangle()
97 {
98  return demangle(typeid(T));
99 }
100 }
101 }
102 
103 #endif
Root piranha namespace.
Definition: array_key.hpp:52