RSC  0.17.1
Demangle.cpp
Go to the documentation of this file.
1 /* ============================================================
2  *
3  * This file is part of the RSC project
4  *
5  * Copyright (C) 2010, 2011 Jan Moringen
6  *
7  * This file may be licensed under the terms of the
8  * GNU Lesser General Public License Version 3 (the ``LGPL''),
9  * or (at your option) any later version.
10  *
11  * Software distributed under the License is distributed
12  * on an ``AS IS'' basis, WITHOUT WARRANTY OF ANY KIND, either
13  * express or implied. See the LGPL for the specific language
14  * governing rights and limitations.
15  *
16  * You should have received a copy of the LGPL along with this
17  * program. If not, go to http://www.gnu.org/licenses/lgpl.html
18  * or write to the Free Software Foundation, Inc.,
19  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20  *
21  * The development of this software was supported by:
22  * CoR-Lab, Research Institute for Cognition and Robotics
23  * Bielefeld University
24  *
25  * ============================================================ */
26 
27 #include "Demangle.h"
28 
29 #include <boost/format.hpp>
30 
31 // GCC implementation
32 #if defined DEMANGLE_GCC
33 #include <cxxabi.h>
34 
35 namespace rsc {
36 namespace runtime {
37 
38 std::string demangle(const char* mangledSymbol) {
39 
40  // Try to demangle the symbol.
41  int status;
42  char* demangled_symbol_ =
43  abi::__cxa_demangle(mangledSymbol, 0, 0, &status);
44 
45  // Check whether demangling worked.
46  if (status == -1) {
47  throw std::runtime_error(
48  "out of memory when allocating buffer for demangling");
49  }
50 
51  if (status == -2 || status == -3) {
52  throw InvalidMangledName(boost::str(boost::format(
53  "invalid mangled name: `%1%'") % mangledSymbol)); // TODO is buffer allocated or not?
54  }
55 
56  // Convert result to string and free temporary buffer.
57  std::string demangled_symbol(demangled_symbol_);
58  free(demangled_symbol_);
59 
60  return demangled_symbol;
61 
62 }
63 
64 }
65 }
66 
67 #elif defined DEMANGLE_MSVC
68 // MSVC implementation
69 
70 namespace rsc {
71 namespace runtime {
72 
73 std::string demangle(const char* mangled_symbol) {
74  // MSVC does everything on its own with typeid.
75  return mangled_symbol;
76 }
77 
78 }
79 }
80 
81 #else
82 
83 namespace rsc {
84 namespace runtime {
85 
86 std::string demangle(const char* mangled_symbol) {
87  return std::string(boost::str(boost::format("<cannot demangle %1%>")
88  % mangled_symbol));
89 }
90 
91 }
92 }
93 
94 #endif
95 
96 namespace rsc {
97 namespace runtime {
98 
99 std::string demangle(const std::string& mangled_symbol) {
100  return demangle(mangled_symbol.c_str());
101 }
102 
103 }
104 }
std::string demangle(const char *mangled_symbol)
This function takes the mangled name of a symbol and returns the demangled name of the symbol...
Definition: Demangle.cpp:86