#include "tile/base/demangle.h"

#include "tile/base/deferred.h"

#include <string>

#if defined(__GNUC__) || defined(__clang__)
#include <cxxabi.h>
#endif

namespace tile {

std::string
Demangle(const char *s)
{
#ifdef _MSC_VER
    return s;
#elif defined(__GNUC__) || defined(__clang__)
    int status;
    char *demangled = abi::__cxa_demangle(s, nullptr, nullptr, &status);
    ScopedDeferred _([&] { free(demangled); });
    if (!demangled) { return s; }
    return demangled;
#else
#error "Demangle not supported on current compiler."

#endif
}

}// namespace tile
