blob: 34530842bd4e4092152b58f56fdb01d79b4b4bdc [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 <cxxabi.h>
11
12#include <cstdio>
13
Austin Schuh1e69f942020-11-14 15:06:14 -080014#include "wpi/SmallString.h"
15
Brian Silverman8fce7482020-01-05 13:18:21 -080016namespace wpi {
17
Austin Schuh1e69f942020-11-14 15:06:14 -080018std::string Demangle(const Twine& mangledSymbol) {
19 SmallString<128> buf;
Brian Silverman8fce7482020-01-05 13:18:21 -080020 size_t length;
21 int32_t status;
22
Austin Schuh1e69f942020-11-14 15:06:14 -080023 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 Silverman8fce7482020-01-05 13:18:21 -080030 }
31
32 // If everything else failed, just return the mangled symbol
Austin Schuh1e69f942020-11-14 15:06:14 -080033 return mangledSymbol.str();
Brian Silverman8fce7482020-01-05 13:18:21 -080034}
35
36} // namespace wpi