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 <windows.h> // NOLINT(build/include_order) |
| 11 | |
| 12 | #include <dbghelp.h> |
| 13 | |
Austin Schuh | 1e69f94 | 2020-11-14 15:06:14 -0800 | [diff] [blame^] | 14 | #include "wpi/SmallString.h" |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 15 | #include "wpi/mutex.h" |
| 16 | |
| 17 | #pragma comment(lib, "Dbghelp.lib") |
| 18 | |
| 19 | namespace wpi { |
| 20 | |
Austin Schuh | 1e69f94 | 2020-11-14 15:06:14 -0800 | [diff] [blame^] | 21 | std::string Demangle(const Twine& mangledSymbol) { |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 22 | static wpi::mutex m; |
| 23 | std::scoped_lock lock(m); |
Austin Schuh | 1e69f94 | 2020-11-14 15:06:14 -0800 | [diff] [blame^] | 24 | SmallString<128> buf; |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 25 | char buffer[256]; |
Austin Schuh | 1e69f94 | 2020-11-14 15:06:14 -0800 | [diff] [blame^] | 26 | DWORD sz = |
| 27 | UnDecorateSymbolName(mangledSymbol.toNullTerminatedStringRef(buf).data(), |
| 28 | buffer, sizeof(buffer), UNDNAME_COMPLETE); |
| 29 | if (sz == 0) return mangledSymbol.str(); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 30 | return std::string(buffer, sz); |
| 31 | } |
| 32 | |
| 33 | } // namespace wpi |