Skip to content

Commit

Permalink
improve RTTI use for error messages
Browse files Browse the repository at this point in the history
Instead of printing the static type of a pointer,
we now print the dynamic type of its pointee.
  • Loading branch information
upsj committed Mar 24, 2020
1 parent 8e54163 commit d9a1d7a
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 15 deletions.
2 changes: 1 addition & 1 deletion core/test/base/exception_helpers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ TEST(NotCompiled, ThrowsWhenUsed)
}


void does_not_support_int() { GKO_NOT_SUPPORTED(int); }
void does_not_support_int() { GKO_NOT_SUPPORTED(int{}); }

TEST(NotSupported, ReturnsNotSupportedException)
{
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
39 changes: 31 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,44 @@ namespace gko {
"semi-colon warnings")


namespace detail {


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

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


} // 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) \
{ \
auto obj = _obj; \
throw ::gko::NotSupported( \
__FILE__, __LINE__, __func__, \
::gko::name_demangling::get_type_name( \
::gko::detail::dynamic_type_helper<decltype(obj)>::get(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

0 comments on commit d9a1d7a

Please sign in to comment.