1/* Copyright 2017 The TensorFlow Authors. All Rights Reserved.
2
3Licensed under the Apache License, Version 2.0 (the "License");
4you may not use this file except in compliance with the License.
5You may obtain a copy of the License at
6
7 http://www.apache.org/licenses/LICENSE-2.0
8
9Unless required by applicable law or agreed to in writing, software
10distributed under the License is distributed on an "AS IS" BASIS,
11WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12See the License for the specific language governing permissions and
13limitations under the License.
14==============================================================================*/
15
16#include "tensorflow/tsl/platform/abi.h"
17
18#if defined(_MSC_VER)
19#include <windows.h>
20#include <cstring>
21#else
22#include <cxxabi.h>
23#include <cstdlib>
24#endif
25
26#include <memory>
27#include <string>
28
29#if defined(_MSC_VER)
30
31extern "C" char* __unDName(char* output_string, const char* name,
32 int max_string_length, void* (*p_alloc)(std::size_t),
33 void (*p_free)(void*), unsigned short disable_flags);
34
35#endif // defined(_MSC_VER)
36
37namespace tsl {
38namespace port {
39
40string MaybeAbiDemangle(const char* name) {
41#if defined(_MSC_VER)
42 std::unique_ptr<char> demangled{__unDName(nullptr, name, 0, std::malloc,
43 std::free,
44 static_cast<unsigned short>(0))};
45
46 return string(demangled.get() != nullptr ? demangled.get() : name);
47#else
48 int status = 0;
49 std::unique_ptr<char, void (*)(void*)> res{
50 abi::__cxa_demangle(name, nullptr, nullptr, &status), std::free};
51 return (status == 0) ? res.get() : name;
52#endif
53}
54
55} // namespace port
56} // namespace tsl
57