]>
Commit | Line | Data |
---|---|---|
46abada0 BK |
1 | <?xml version="1.0" encoding="UTF-8" standalone="no"?> |
2 | <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> | |
3 | <html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /><title>Chapter 39. Demangling</title><meta name="generator" content="DocBook XSL Stylesheets V1.73.2" /><meta name="keywords" content=" ISO C++ , library " /><link rel="start" href="../spine.html" title="The GNU C++ Library Documentation" /><link rel="up" href="extensions.html" title="Part XII. Extensions" /><link rel="prev" href="bk01pt12ch38.html" title="Chapter 38. Input and Output" /><link rel="next" href="concurrency.html" title="Chapter 40. Concurrency" /></head><body><div class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="3" align="center">Chapter 39. Demangling</th></tr><tr><td width="20%" align="left"><a accesskey="p" href="bk01pt12ch38.html">Prev</a> </td><th width="60%" align="center">Part XII. Extensions</th><td width="20%" align="right"> <a accesskey="n" href="concurrency.html">Next</a></td></tr></table><hr /></div><div class="chapter" lang="en" xml:lang="en"><div class="titlepage"><div><div><h2 class="title"><a id="manual.ext.demangle"></a>Chapter 39. Demangling</h2></div></div></div><p> | |
4394b61e | 4 | Transforming C++ ABI identifiers (like RTTI symbols) into the |
46abada0 BK |
5 | original C++ source identifiers is called |
6 | “<span class="quote">demangling.</span>” | |
7 | </p><p> | |
8 | If you have read the <a class="ulink" href="http://gcc.gnu.org/onlinedocs/libstdc++/latest-doxygen/namespaceabi.html" target="_top">source | |
9 | documentation for <code class="code">namespace abi</code></a> then you are | |
10 | aware of the cross-vendor C++ ABI in use by GCC. One of the | |
11 | exposed functions is used for demangling, | |
12 | <code class="code">abi::__cxa_demangle</code>. | |
13 | </p><p> | |
14 | In programs like <span class="command"><strong>c++filt</strong></span>, the linker, and other tools | |
15 | have the ability to decode C++ ABI names, and now so can you. | |
16 | </p><p> | |
17 | (The function itself might use different demanglers, but that's the | |
18 | whole point of abstract interfaces. If we change the implementation, | |
19 | you won't notice.) | |
20 | </p><p> | |
21 | Probably the only times you'll be interested in demangling at runtime | |
22 | are when you're seeing <code class="code">typeid</code> strings in RTTI, or when | |
23 | you're handling the runtime-support exception classes. For example: | |
24 | </p><pre class="programlisting"> | |
25 | #include <exception> | |
26 | #include <iostream> | |
27 | #include <cxxabi.h> | |
28 | ||
29 | struct empty { }; | |
30 | ||
31 | template <typename T, int N> | |
32 | struct bar { }; | |
33 | ||
34 | ||
35 | int main() | |
36 | { | |
37 | int status; | |
38 | char *realname; | |
39 | ||
40 | // exception classes not in <stdexcept>, thrown by the implementation | |
41 | // instead of the user | |
42 | std::bad_exception e; | |
43 | realname = abi::__cxa_demangle(e.what(), 0, 0, &status); | |
44 | std::cout << e.what() << "\t=> " << realname << "\t: " << status << '\n'; | |
45 | free(realname); | |
46 | ||
47 | ||
48 | // typeid | |
49 | bar<empty,17> u; | |
50 | const std::type_info &ti = typeid(u); | |
51 | ||
52 | realname = abi::__cxa_demangle(ti.name(), 0, 0, &status); | |
53 | std::cout << ti.name() << "\t=> " << realname << "\t: " << status << '\n'; | |
54 | free(realname); | |
55 | ||
56 | return 0; | |
57 | } | |
58 | </pre><p> | |
59 | This prints | |
60 | </p><pre class="screen"> | |
61 | <code class="computeroutput"> | |
62 | St13bad_exception => std::bad_exception : 0 | |
63 | 3barI5emptyLi17EE => bar<empty, 17> : 0 | |
64 | </code> | |
65 | </pre><p> | |
66 | The demangler interface is described in the source documentation | |
67 | linked to above. It is actually written in C, so you don't need to | |
68 | be writing C++ in order to demangle C++. (That also means we have to | |
69 | use crummy memory management facilities, so don't forget to free() | |
70 | the returned char array.) | |
71 | </p></div><div class="navfooter"><hr /><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="bk01pt12ch38.html">Prev</a> </td><td width="20%" align="center"><a accesskey="u" href="extensions.html">Up</a></td><td width="40%" align="right"> <a accesskey="n" href="concurrency.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">Chapter 38. Input and Output </td><td width="20%" align="center"><a accesskey="h" href="../spine.html">Home</a></td><td width="40%" align="right" valign="top"> Chapter 40. Concurrency</td></tr></table></div></body></html> |