Last active
September 26, 2016 16:39
-
-
Save socantre/e7e54dcf3c5209f14dbb786c93cd71b6 to your computer and use it in GitHub Desktop.
Example use of the Itanium ABI demangling API.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <cxxabi.h> | |
#include <cstdlib> | |
#include <cstddef> | |
#include <iostream> | |
#include <memory> | |
struct clib_free { | |
template<typename T> | |
void operator() (T *t) { | |
std::free(t); | |
} | |
}; | |
static std::string demangle(char const *mangled) { | |
int status; | |
std::size_t n; | |
std::unique_ptr<char[], clib_free> demangled(abi::__cxa_demangle(mangled, nullptr, &n, &status)); | |
switch (status) { | |
case -1: | |
throw std::bad_alloc(); | |
case -2: | |
throw std::runtime_error("invalid mangled name"); | |
case -3: | |
abort(); | |
} | |
return std::string(demangled.get(), n-1); | |
} | |
int main() { | |
std::cout << demangle("_Z3foodc") << '\n'; | |
std::cout << demangle("i") << '\n'; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment