James Kuszmaul | 4f3ad3c | 2019-12-01 16:35:21 -0800 | [diff] [blame] | 1 | /*----------------------------------------------------------------------------*/ |
| 2 | /* Copyright (c) 2008-2019 FIRST. All Rights Reserved. */ |
| 3 | /* Open Source Software - may be modified and shared by FRC teams. The code */ |
| 4 | /* must be accompanied by the FIRST BSD license file in the root directory of */ |
| 5 | /* the project. */ |
| 6 | /*----------------------------------------------------------------------------*/ |
| 7 | |
| 8 | #include "wpi/Demangle.h" |
| 9 | |
| 10 | #include <cxxabi.h> |
| 11 | |
| 12 | #include <cstdio> |
| 13 | |
| 14 | namespace wpi { |
| 15 | |
| 16 | std::string Demangle(char const* mangledSymbol) { |
| 17 | char buffer[256]; |
| 18 | size_t length; |
| 19 | int32_t status; |
| 20 | |
| 21 | if (std::sscanf(mangledSymbol, "%*[^(]%*[(]%255[^)+]", buffer)) { |
| 22 | char* symbol = abi::__cxa_demangle(buffer, nullptr, &length, &status); |
| 23 | if (status == 0) { |
| 24 | return symbol; |
| 25 | } else { |
| 26 | // If the symbol couldn't be demangled, it's probably a C function, |
| 27 | // so just return it as-is. |
| 28 | return buffer; |
| 29 | } |
| 30 | } |
| 31 | |
| 32 | // If everything else failed, just return the mangled symbol |
| 33 | return mangledSymbol; |
| 34 | } |
| 35 | |
| 36 | } // namespace wpi |