Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 1 | // Copyright (c) FIRST and other WPILib contributors. |
| 2 | // Open Source Software; you can modify and/or share it under the terms of |
| 3 | // the WPILib BSD license file in the root directory of this project. |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 4 | |
| 5 | #include "wpi/Demangle.h" |
| 6 | |
| 7 | #include <cxxabi.h> |
| 8 | |
| 9 | #include <cstdio> |
| 10 | |
Austin Schuh | 1e69f94 | 2020-11-14 15:06:14 -0800 | [diff] [blame] | 11 | #include "wpi/SmallString.h" |
| 12 | |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 13 | namespace wpi { |
| 14 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 15 | std::string Demangle(std::string_view mangledSymbol) { |
| 16 | SmallString<128> buf{mangledSymbol}; |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 17 | size_t length; |
| 18 | int32_t status; |
| 19 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 20 | char* symbol = abi::__cxa_demangle(buf.c_str(), nullptr, &length, &status); |
Austin Schuh | 1e69f94 | 2020-11-14 15:06:14 -0800 | [diff] [blame] | 21 | if (status == 0) { |
| 22 | std::string rv{symbol}; |
| 23 | std::free(symbol); |
| 24 | return rv; |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 25 | } |
| 26 | |
| 27 | // If everything else failed, just return the mangled symbol |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 28 | return std::string{mangledSymbol}; |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 29 | } |
| 30 | |
| 31 | } // namespace wpi |