Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 1 | /*----------------------------------------------------------------------------*/ |
Austin Schuh | 1e69f94 | 2020-11-14 15:06:14 -0800 | [diff] [blame^] | 2 | /* Copyright (c) 2008-2020 FIRST. All Rights Reserved. */ |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 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 | |
Austin Schuh | 1e69f94 | 2020-11-14 15:06:14 -0800 | [diff] [blame^] | 14 | #include "wpi/SmallString.h" |
| 15 | |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 16 | namespace wpi { |
| 17 | |
Austin Schuh | 1e69f94 | 2020-11-14 15:06:14 -0800 | [diff] [blame^] | 18 | std::string Demangle(const Twine& mangledSymbol) { |
| 19 | SmallString<128> buf; |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 20 | size_t length; |
| 21 | int32_t status; |
| 22 | |
Austin Schuh | 1e69f94 | 2020-11-14 15:06:14 -0800 | [diff] [blame^] | 23 | char* symbol = |
| 24 | abi::__cxa_demangle(mangledSymbol.toNullTerminatedStringRef(buf).data(), |
| 25 | nullptr, &length, &status); |
| 26 | if (status == 0) { |
| 27 | std::string rv{symbol}; |
| 28 | std::free(symbol); |
| 29 | return rv; |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 30 | } |
| 31 | |
| 32 | // If everything else failed, just return the mangled symbol |
Austin Schuh | 1e69f94 | 2020-11-14 15:06:14 -0800 | [diff] [blame^] | 33 | return mangledSymbol.str(); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 34 | } |
| 35 | |
| 36 | } // namespace wpi |