Skip to content

Commit 21d5b94

Browse files
committed
error message refine: add demangle api to attribute type
1 parent 772ceee commit 21d5b94

File tree

4 files changed

+150
-3
lines changed

4 files changed

+150
-3
lines changed

paddle/fluid/framework/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,8 @@ cc_test(cow_ptr_tests SRCS details/cow_ptr_test.cc)
115115
# cc_test(channel_test SRCS channel_test.cc)
116116
cc_test(tuple_test SRCS tuple_test.cc )
117117

118+
cc_test(attribute_type_test SRCS attribute_type_test.cc)
119+
118120
# disable test temporarily.
119121
# TODO https://github.com/PaddlePaddle/Paddle/issues/11971
120122
# cc_test(concurrency_test SRCS concurrency_test.cc DEPS go_op channel_close_op channel_create_op

paddle/fluid/framework/attribute.h

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ limitations under the License. */
2020
#include <unordered_set>
2121
#include <vector>
2222

23+
#include "paddle/fluid/framework/attribute_type.h"
2324
#include "paddle/fluid/framework/framework.pb.h"
2425
#include "paddle/fluid/framework/type_defs.h"
2526
#include "paddle/fluid/platform/enforce.h"
@@ -128,7 +129,8 @@ struct ExtractAttribute {
128129
attr_value = &boost::get<T>(attr);
129130
} catch (boost::bad_get& bad_get) {
130131
PADDLE_THROW("Cannot get attribute %s by type %s, its type is %s",
131-
attr_name_, typeid(T).name(), attr.type().name());
132+
attr_name_, paddle::framework::demangle(typeid(T).name()),
133+
paddle::framework::demangle(attr.type().name()));
132134
}
133135
return attr_value;
134136
}
@@ -160,7 +162,7 @@ struct ExtractAttribute<bool> {
160162
attr_value = &boost::get<bool>(attr);
161163
} catch (boost::bad_get& bad_get) {
162164
PADDLE_THROW("Cannot get attribute %s by type bool, its type is %s",
163-
attr_name_, attr.type().name());
165+
attr_name_, paddle::framework::demangle(attr.type().name()));
164166
}
165167
return attr_value;
166168
}
@@ -186,7 +188,7 @@ struct ExtractAttribute<int64_t> {
186188
attr_value = &boost::get<int64_t>(attr);
187189
} catch (boost::bad_get& bad_get) {
188190
PADDLE_THROW("Cannot get attribute %s by type int64_t, its type is %s",
189-
attr_name_, attr.type().name());
191+
attr_name_, paddle::framework::demangle(attr.type().name()));
190192
}
191193
return attr_value;
192194
}
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
/* Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved.
2+
3+
Licensed under the Apache License, Version 2.0 (the "License");
4+
you may not use this file except in compliance with the License.
5+
You may obtain a copy of the License at
6+
7+
http://www.apache.org/licenses/LICENSE-2.0
8+
9+
Unless required by applicable law or agreed to in writing, software
10+
distributed under the License is distributed on an "AS IS" BASIS,
11+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
See the License for the specific language governing permissions and
13+
limitations under the License. */
14+
15+
#pragma once
16+
17+
#include <string>
18+
19+
// __has_include is currently supported by GCC and Clang. However GCC 4.9 may
20+
// have issues and
21+
// returns 1 for 'defined( __has_include )', while '__has_include' is actually
22+
// not supported:
23+
#if defined(__has_include) && (!defined(BOOST_GCC) || (__GNUC__ + 0) >= 5)
24+
#if __has_include(<cxxabi.h>)
25+
#define PADDLE_FRAMEWORK_HAS_CXXABI_H
26+
#endif
27+
#elif defined(__GLIBCXX__) || defined(__GLIBCPP__)
28+
#define PADDLE_FRAMEWORK_HAS_CXXABI_H
29+
#endif
30+
31+
#if defined(PADDLE_FRAMEWORK_HAS_CXXABI_H)
32+
#include <cxxabi.h>
33+
// For some archtectures (mips, mips64, x86, x86_64) cxxabi.h in Android NDK is
34+
// implemented by gabi++ library
35+
// which does not implement abi::__cxa_demangle(). We detect this implementation
36+
// by checking the include guard here.
37+
#if defined(__GABIXX_CXXABI_H__)
38+
#undef PADDLE_FRAMEWORK_HAS_CXXABI_H
39+
#else
40+
#include <cstddef>
41+
#include <cstdlib>
42+
#endif
43+
#endif
44+
45+
namespace paddle {
46+
namespace framework {
47+
48+
inline char const* demangle_alloc(char const* name);
49+
inline void demangle_free(char const* name);
50+
51+
class scoped_demangled_name {
52+
private:
53+
char const* m_p;
54+
55+
public:
56+
explicit scoped_demangled_name(char const* name)
57+
: m_p(demangle_alloc(name)) {}
58+
59+
~scoped_demangled_name() { demangle_free(m_p); }
60+
61+
char const* get() const { return m_p; }
62+
63+
scoped_demangled_name(scoped_demangled_name const&) = delete;
64+
scoped_demangled_name& operator=(scoped_demangled_name const&) = delete;
65+
};
66+
67+
#if defined(PADDLE_FRAMEWORK_HAS_CXXABI_H)
68+
69+
inline char const* demangle_alloc(char const* name) {
70+
int status = 0;
71+
std::size_t size = 0;
72+
return abi::__cxa_demangle(name, NULL, &size, &status);
73+
}
74+
75+
inline void demangle_free(char const* name) {
76+
std::free(const_cast<char*>(name));
77+
}
78+
79+
inline std::string demangle(char const* name) {
80+
scoped_demangled_name demangled_name(name);
81+
char const* p = demangled_name.get();
82+
if (!p) p = name;
83+
return p;
84+
}
85+
86+
#else
87+
88+
inline char const* demangle_alloc(char const* name) { return name; }
89+
90+
inline void demangle_free(char const*) {}
91+
92+
inline std::string demangle(char const* name) { return name; }
93+
94+
#endif
95+
96+
} // namespace framework
97+
} // namespace paddle
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/* Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved.
2+
*
3+
* Licensed under the Apache License, Version 2.0 (the "License");
4+
* you may not use this file except in compliance with the License.
5+
* You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software
10+
* distributed under the License is distributed on an "AS IS" BASIS,
11+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
* See the License for the specific language governing permissions and
13+
* limitations under the License. */
14+
15+
#include <string>
16+
#include <vector>
17+
18+
#include "gtest/gtest.h"
19+
#include "paddle/fluid/framework/attribute_type.h"
20+
21+
TEST(Attribute, TypeName) {
22+
bool boolean;
23+
int integer;
24+
float ft;
25+
std::string str;
26+
std::vector<bool> booleans;
27+
std::vector<int> integers;
28+
std::vector<std::string> strings;
29+
30+
EXPECT_EQ("bool", paddle::framework::demangle(typeid(boolean).name()));
31+
EXPECT_EQ("int", paddle::framework::demangle(typeid(integer).name()));
32+
EXPECT_EQ("float", paddle::framework::demangle(typeid(ft).name()));
33+
EXPECT_EQ(
34+
"std::__cxx11::basic_string<char, std::char_traits<char>, "
35+
"std::allocator<char> >",
36+
paddle::framework::demangle(typeid(str).name()));
37+
EXPECT_EQ("std::vector<bool, std::allocator<bool> >",
38+
paddle::framework::demangle(typeid(booleans).name()));
39+
EXPECT_EQ("std::vector<int, std::allocator<int> >",
40+
paddle::framework::demangle(typeid(integers).name()));
41+
EXPECT_EQ(
42+
"std::vector<std::__cxx11::basic_string<char, std::char_traits<char>, "
43+
"std::allocator<char> >, std::allocator<std::__cxx11::basic_string<char, "
44+
"std::char_traits<char>, std::allocator<char> > > >",
45+
paddle::framework::demangle(typeid(strings).name()));
46+
}

0 commit comments

Comments
 (0)