blob: 1b0a8a736375fc1ad91807fa12c0bfbd8134a539 [file] [log] [blame]
Austin Schuh812d0d12021-11-04 20:16:48 -07001// 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 Silverman8fce7482020-01-05 13:18:21 -08004
5#include "wpi/Demangle.h"
6
7#include <windows.h> // NOLINT(build/include_order)
8
9#include <dbghelp.h>
10
Austin Schuh1e69f942020-11-14 15:06:14 -080011#include "wpi/SmallString.h"
Brian Silverman8fce7482020-01-05 13:18:21 -080012#include "wpi/mutex.h"
13
14#pragma comment(lib, "Dbghelp.lib")
15
16namespace wpi {
17
Austin Schuh812d0d12021-11-04 20:16:48 -070018std::string Demangle(std::string_view mangledSymbol) {
Brian Silverman8fce7482020-01-05 13:18:21 -080019 static wpi::mutex m;
20 std::scoped_lock lock(m);
Austin Schuh812d0d12021-11-04 20:16:48 -070021 SmallString<128> buf{mangledSymbol};
Brian Silverman8fce7482020-01-05 13:18:21 -080022 char buffer[256];
Austin Schuh812d0d12021-11-04 20:16:48 -070023 DWORD sz = UnDecorateSymbolName(buf.c_str(), buffer, sizeof(buffer),
24 UNDNAME_COMPLETE);
25 if (sz == 0)
26 return std::string{mangledSymbol};
Brian Silverman8fce7482020-01-05 13:18:21 -080027 return std::string(buffer, sz);
28}
29
30} // namespace wpi