Skip to content
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

Improve RTTI use for error messages #486

Merged
merged 2 commits into from
Mar 24, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 40 additions & 3 deletions core/test/base/exception_helpers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,48 @@ TEST(NotCompiled, ThrowsWhenUsed)
}


void does_not_support_int() { GKO_NOT_SUPPORTED(int); }
template <typename Expected, typename T>
void test_not_supported_impl(const T &obj)
{
try {
GKO_NOT_SUPPORTED(obj);
FAIL();
} catch (gko::NotSupported &m) {
// check for equal suffix
std::string msg{m.what()};
auto expected = gko::name_demangling::get_type_name(typeid(Expected));
ASSERT_TRUE(
std::equal(expected.rbegin(), expected.rend(), msg.rbegin()));
}
}


TEST(NotSupported, ReturnsIntNotSupportedException)
{
test_not_supported_impl<int>(int{});
}


struct Base {
virtual ~Base() = default;
};

struct Derived : Base {};


TEST(NotSupported, ReturnsPtrNotSupportedException)
{
Derived d;
Base *b = &d;
test_not_supported_impl<Derived>(b);
}


TEST(NotSupported, ReturnsNotSupportedException)
TEST(NotSupported, ReturnsRefNotSupportedException)
{
ASSERT_THROW(does_not_support_int(), gko::NotSupported);
Derived d;
Base &b = d;
test_not_supported_impl<Derived>(b);
}


Expand Down
20 changes: 18 additions & 2 deletions core/test/base/utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,15 @@ TEST(As, FailsToConvertIfNotRelated)
Derived d;
Base *b = &d;

ASSERT_THROW(gko::as<NonRelated>(b), gko::NotSupported);
try {
gko::as<NonRelated>(b);
FAIL();
} catch (gko::NotSupported &m) {
std::string msg{m.what()};
auto expected = gko::name_demangling::get_type_name(typeid(Derived));
ASSERT_TRUE(
std::equal(expected.rbegin(), expected.rend(), msg.rbegin()));
}
}


Expand All @@ -266,7 +274,15 @@ TEST(As, FailsToConvertConstantIfNotRelated)
Derived d;
const Base *b = &d;

ASSERT_THROW(gko::as<NonRelated>(b), gko::NotSupported);
try {
gko::as<NonRelated>(b);
FAIL();
} catch (gko::NotSupported &m) {
std::string msg{m.what()};
auto expected = gko::name_demangling::get_type_name(typeid(Derived));
ASSERT_TRUE(
std::equal(expected.rbegin(), expected.rend(), msg.rbegin()));
}
}


Expand Down
2 changes: 1 addition & 1 deletion cuda/base/executor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ void CudaExecutor::raw_copy_to(const HipExecutor *src, size_type num_bytes,
GKO_ASSERT_NO_CUDA_ERRORS(cudaMemcpyPeer(
dest_ptr, this->device_id_, src_ptr, src->get_device_id(), num_bytes));
#else
GKO_NOT_SUPPORTED(CudaExecutor);
GKO_NOT_SUPPORTED(this);
#endif
}

Expand Down
2 changes: 1 addition & 1 deletion hip/base/executor.hip.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ void HipExecutor::raw_copy_to(const CudaExecutor *src, size_type num_bytes,
GKO_ASSERT_NO_HIP_ERRORS(hipMemcpyPeer(dest_ptr, this->device_id_, src_ptr,
src->get_device_id(), num_bytes));
#else
GKO_NOT_SUPPORTED(HipExecutor);
GKO_NOT_SUPPORTED(this);
#endif
}

Expand Down
44 changes: 36 additions & 8 deletions include/ginkgo/core/base/exception_helpers.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,21 +88,49 @@ namespace gko {
"semi-colon warnings")


namespace detail {


template <typename T, typename T2 = void>
struct dynamic_type_helper {
static const std::type_info &get(const T &obj) { return typeid(obj); }
};

template <typename T>
struct dynamic_type_helper<T,
typename std::enable_if<std::is_pointer<T>::value ||
have_ownership<T>()>::type> {
static const std::type_info &get(const T &obj)
{
return obj ? typeid(*obj) : typeid(nullptr);
}
};

template <typename T>
const std::type_info &get_dynamic_type(const T &obj)
{
return dynamic_type_helper<T>::get(obj);
}


} // namespace detail


/**
* Throws a NotSupported exception.
* This macro sets the correct information about the location of the error
* and fills the exception with data about _obj, followed by throwing it.
*
* @param _obj the object referenced by NotSupported exception
*/
#define GKO_NOT_SUPPORTED(_obj) \
{ \
throw ::gko::NotSupported( \
__FILE__, __LINE__, __func__, \
::gko::name_demangling::get_type_name(typeid(_obj))); \
} \
static_assert(true, \
"This assert is used to counter the false positive extra " \
#define GKO_NOT_SUPPORTED(_obj) \
{ \
throw ::gko::NotSupported(__FILE__, __LINE__, __func__, \
::gko::name_demangling::get_type_name( \
::gko::detail::get_dynamic_type(_obj))); \
} \
static_assert(true, \
"This assert is used to counter the false positive extra " \
"semi-colon warnings")


Expand Down
12 changes: 8 additions & 4 deletions include/ginkgo/core/base/utils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -291,8 +291,10 @@ inline typename std::decay<T>::type *as(U *obj)
if (auto p = dynamic_cast<typename std::decay<T>::type *>(obj)) {
return p;
} else {
throw NotSupported(__FILE__, __LINE__, __func__,
name_demangling::get_type_name(typeid(obj)));
throw NotSupported(__FILE__, __LINE__,
std::string{"gko::as<"} +
name_demangling::get_type_name(typeid(T)) + ">",
name_demangling::get_type_name(typeid(*obj)));
}
}

Expand All @@ -315,8 +317,10 @@ inline const typename std::decay<T>::type *as(const U *obj)
if (auto p = dynamic_cast<const typename std::decay<T>::type *>(obj)) {
return p;
} else {
throw NotSupported(__FILE__, __LINE__, __func__,
name_demangling::get_type_name(typeid(obj)));
throw NotSupported(__FILE__, __LINE__,
std::string{"gko::as<"} +
name_demangling::get_type_name(typeid(T)) + ">",
name_demangling::get_type_name(typeid(*obj)));
}
}

Expand Down