blob: 69404ed792bfd475363267740c64821f487d7f2a [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 <cxxabi.h>
8
9#include <cstdio>
10
Austin Schuh1e69f942020-11-14 15:06:14 -080011#include "wpi/SmallString.h"
12
Brian Silverman8fce7482020-01-05 13:18:21 -080013namespace wpi {
14
Austin Schuh812d0d12021-11-04 20:16:48 -070015std::string Demangle(std::string_view mangledSymbol) {
16 SmallString<128> buf{mangledSymbol};
Brian Silverman8fce7482020-01-05 13:18:21 -080017 size_t length;
18 int32_t status;
19
Austin Schuh812d0d12021-11-04 20:16:48 -070020 char* symbol = abi::__cxa_demangle(buf.c_str(), nullptr, &length, &status);
Austin Schuh1e69f942020-11-14 15:06:14 -080021 if (status == 0) {
22 std::string rv{symbol};
23 std::free(symbol);
24 return rv;
Brian Silverman8fce7482020-01-05 13:18:21 -080025 }
26
27 // If everything else failed, just return the mangled symbol
Austin Schuh812d0d12021-11-04 20:16:48 -070028 return std::string{mangledSymbol};
Brian Silverman8fce7482020-01-05 13:18:21 -080029}
30
31} // namespace wpi