-
Notifications
You must be signed in to change notification settings - Fork 5.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add macro BOOST_GET to enrich the error information of boost :: get #24175
Add macro BOOST_GET to enrich the error information of boost :: get #24175
Conversation
paddle/fluid/platform/enforce.h
Outdated
"(%s) by type %s, its type is %s.", \ | ||
#__VALUE, paddle::platform::demangle(typeid(__TYPE).name()), \ | ||
paddle::platform::demangle(__VALUE.type().name()))); \ | ||
} \ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这个boost::get 除了 bad_get 还有其他情况的异常么, 需要不要 catch(...) 捕获其他的异常
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
看了下源码:https://www.boost.org/doc/libs/1_58_0/boost/variant/get.hpp
里面如果get失败,一律抛出bad_get
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM!!
boost::get
is not a completely safe interface, although it will not go wrong in most cases, but in extreme case, if may fail and directly throw aboost::bad_get
exception without any stack information. This kind of problem is difficult to debug.Related issues:
Now there are nearly 600
boost :: get
in paddle without catch exception when used.In this PR, I add new series macros
BOOST_GET{***}
, It encapsulatesboost :: get
as follows:Use this macro instead of
boost :: get
, when something goes wrong, we can get richer error information . Including the error file, line number, expected value type and actual value type, this can help users to locate the cause of the error easily.difficulty
The
boost :: get
has 4 types of return value, includeT, const T, T&, const T&
, so here need some tricks to cover these 4 scenarios with only one macro. With the help of @sneaxiy, under the gcc5.4 environment, we solved this problem with only one macro, see the commit 11bbbb2.But CI now using gcc4.8, the writing above cannot be compiled under gcc4.8, this is a gcc4.8’s bug, gcc official recommends to use a higher version to solve. So I had to write three macros here to solve this problem, as follows:
Examples:
Case 1: Place Get Error
1. original
boost::get
this is a C++ exception, but using
boost::get
directly cannot throw C++ error info.code piece
2. new macro
BOOST_GET
Case 2: Attr Get Error
1. original
boost::get
2. new macro
BOOST_GET
CI check
In order to avoid the direct use of
boost :: get
, I added a new rule to CI to limit the use ofboost :: get
. If found, it will remind developer to use the macroBOOST_GET(_**) series macros
.