util_demangle.hpp (2843B)
1 /* 2 Copyright 2010-2011, D. E. Shaw Research. 3 All rights reserved. 4 5 Redistribution and use in source and binary forms, with or without 6 modification, are permitted provided that the following conditions are 7 met: 8 9 * Redistributions of source code must retain the above copyright 10 notice, this list of conditions, and the following disclaimer. 11 12 * Redistributions in binary form must reproduce the above copyright 13 notice, this list of conditions, and the following disclaimer in the 14 documentation and/or other materials provided with the distribution. 15 16 * Neither the name of D. E. Shaw Research nor the names of its 17 contributors may be used to endorse or promote products derived from 18 this software without specific prior written permission. 19 20 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 21 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 22 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 23 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 24 OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 25 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 26 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 27 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 28 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 29 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 30 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 31 */ 32 #ifndef demangle_dot_hpp_ 33 #include <string> 34 35 // Every compiler has a demangling library *somewhere*. Unfortunately, they're 36 // all different... 37 38 #ifdef __GNUC__ 39 // Clang defines __GNUC__, but clang3.1 with -stdlib=libc++ can't 40 // find a <cxxabi.h> even though it *can* find the symbols at link 41 // time. I suspect this is a bug/oversight in the installation 42 // process (which, in June 2012 is still pretty fluid for libc++), so 43 // it might be fixed in the future. On the other hand, the API in 44 // cxxabi.h is locked down pretty tightly, so writing out an explicit 45 // extern declaration is pretty safe, and avoids a rats nest of 46 // ifdefs. It is tempting to use clang's __has_include(<cxxabi.h>), 47 // but it feels like more #ifdefs with no obvious upside. 48 // 49 // #include <cxxabi.h> 50 extern "C"{ 51 char* 52 __cxa_demangle(const char* __mangled_name, char* __output_buffer, 53 size_t* __length, int* __status); 54 } 55 #endif 56 #include <typeinfo> 57 58 template <typename T> 59 std::string demangle(const T& ignored){ 60 #ifdef __GNUC__ 61 int status; 62 char *realname = __cxa_demangle(typeid(ignored).name(), 0, 0, &status); 63 std::string ret; 64 if(status!=0 || realname==0) 65 ret = typeid(ignored).name(); 66 else 67 ret = realname; 68 free(realname); 69 return ret; 70 #else 71 return typeid(ignored).name(); 72 #endif 73 } 74 75 #endif