blob: a1560d64c19260c7a2aec8aec1ac58232589833a [file] [log] [blame]
Brian Silverman8fce7482020-01-05 13:18:21 -08001/*----------------------------------------------------------------------------*/
Austin Schuh1e69f942020-11-14 15:06:14 -08002/* Copyright (c) 2008-2020 FIRST. All Rights Reserved. */
Brian Silverman8fce7482020-01-05 13:18:21 -08003/* 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 Schuh1e69f942020-11-14 15:06:14 -080014#include "wpi/SmallString.h"
Brian Silverman8fce7482020-01-05 13:18:21 -080015#include "wpi/mutex.h"
16
17#pragma comment(lib, "Dbghelp.lib")
18
19namespace wpi {
20
Austin Schuh1e69f942020-11-14 15:06:14 -080021std::string Demangle(const Twine& mangledSymbol) {
Brian Silverman8fce7482020-01-05 13:18:21 -080022 static wpi::mutex m;
23 std::scoped_lock lock(m);
Austin Schuh1e69f942020-11-14 15:06:14 -080024 SmallString<128> buf;
Brian Silverman8fce7482020-01-05 13:18:21 -080025 char buffer[256];
Austin Schuh1e69f942020-11-14 15:06:14 -080026 DWORD sz =
27 UnDecorateSymbolName(mangledSymbol.toNullTerminatedStringRef(buf).data(),
28 buffer, sizeof(buffer), UNDNAME_COMPLETE);
29 if (sz == 0) return mangledSymbol.str();
Brian Silverman8fce7482020-01-05 13:18:21 -080030 return std::string(buffer, sz);
31}
32
33} // namespace wpi