demangle.qbk 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. [/
  2. Copyright 2014 Peter Dimov
  3. Copyright 2014 Andrey Semashev
  4. Distributed under the Boost Software License, Version 1.0.
  5. See accompanying file LICENSE_1_0.txt
  6. or copy at http://boost.org/LICENSE_1_0.txt
  7. ]
  8. [section:demangle demangle]
  9. [simplesect Authors]
  10. * Peter Dimov
  11. * Andrey Semashev
  12. [endsimplesect]
  13. [section Header <boost/core/demangle.hpp>]
  14. The header `<boost/core/demangle.hpp>` defines several tools for undecorating
  15. symbol names.
  16. [section Synopsis]
  17. namespace boost
  18. {
  19. namespace core
  20. {
  21. std::string demangle( char const * name );
  22. char const * demangle_alloc( char const * name ) noexcept;
  23. void demangle_free( char const * demangled_name ) noexcept;
  24. class scoped_demangled_name
  25. {
  26. public:
  27. explicit scoped_demangled_name( char const * name ) noexcept;
  28. ~scoped_demangled_name() noexcept;
  29. char const * get() const noexcept;
  30. scoped_demangled_name( scoped_demangled_name const& ) = delete;
  31. scoped_demangled_name& operator= ( scoped_demangled_name const& ) = delete;
  32. };
  33. }
  34. }
  35. [endsect]
  36. [section Conventional interface]
  37. The function `boost::core::demangle` is the conventional
  38. way to obtain demangled symbol name. It takes a mangled string such as
  39. those returned by `typeid(T).name()` on certain implementations
  40. such as `g++`, and returns its demangled, human-readable, form. In case if
  41. demangling fails (e.g. if `name` cannot be interpreted as a mangled name)
  42. the function returns `name`.
  43. [section Example]
  44. #include <boost/core/demangle.hpp>
  45. #include <typeinfo>
  46. #include <iostream>
  47. template<class T> struct X
  48. {
  49. };
  50. int main()
  51. {
  52. char const * name = typeid( X<int> ).name();
  53. std::cout << name << std::endl; // prints 1XIiE
  54. std::cout << boost::core::demangle( name ) << std::endl; // prints X<int>
  55. }
  56. [endsect]
  57. [endsect]
  58. [section Low level interface]
  59. In some cases more low level interface may be desirable. For example:
  60. * Assuming that symbol demangling may fail, the user wants to be able to handle such errors.
  61. * The user needs to post-process the demangled name (e.g. remove common namespaces), and
  62. allocating a temporary string with the complete demangled name is significant overhead.
  63. The function `boost::core::demangle_alloc` performs name demangling and returns a pointer
  64. to a string with the demangled name, if succeeded, or `nullptr` otherwise. The returned pointer
  65. must be passed to `boost::core::demangle_free` to reclaim resources. Note that on some platforms
  66. the pointer returned by `boost::core::demangle_alloc` may refer to the string denoted by `name`,
  67. so this string must be kept immutable for the whole life time of the returned pointer.
  68. The `boost::core::scoped_demangled_name` class is a scope guard that automates the calls to
  69. `boost::core::demangle_alloc` (on its construction) and `boost::core::demangle_free` (on destruction).
  70. The string with the demangled name can be obtained with its `get` method. Note that this method may
  71. return `nullptr` if demangling failed.
  72. [section Example]
  73. #include <boost/core/demangle.hpp>
  74. #include <typeinfo>
  75. #include <iostream>
  76. template<class T> struct X
  77. {
  78. };
  79. int main()
  80. {
  81. char const * name = typeid( X<int> ).name();
  82. boost::core::scoped_demangled_name demangled( name );
  83. std::cout << name << std::endl; // prints 1XIiE
  84. std::cout << (demangled.get() ? demangled.get() : "[unknown]") << std::endl; // prints X<int>
  85. }
  86. [endsect]
  87. [endsect]
  88. [endsect]
  89. [section Acknowledgments]
  90. The implementation of `core::demangle` was taken from
  91. `boost/exception/detail/type_info.hpp`, which in turn was adapted
  92. from `boost/units/detail/utility.hpp` and `boost/log/utility/type_info_wrapper.hpp`.
  93. [endsect]
  94. [endsect]