Skip to content

Commit bfc1f45

Browse files
matglahsutter
andauthored
Fixes for arm-none-eabi-g++ (#1045)
* Typeid cannot be used when -fno-rtti is used. Fixed compilation of generated sources on arm-none-eabi-g++ 13.1. * fully compile time type deduction for GCC and clang * Restored exception message --------- Co-authored-by: Herb Sutter <[email protected]>
1 parent e6a0200 commit bfc1f45

File tree

1 file changed

+56
-3
lines changed

1 file changed

+56
-3
lines changed

Diff for: include/cpp2util.h

+56-3
Original file line numberDiff line numberDiff line change
@@ -709,6 +709,52 @@ auto assert_in_bounds(auto&& x, auto&& arg CPP2_SOURCE_LOCATION_PARAM_WITH_DEFAU
709709
#define CPP2_ASSERT_IN_BOUNDS(x,arg) (cpp2::impl::assert_in_bounds((x),(arg)))
710710
#define CPP2_ASSERT_IN_BOUNDS_LITERAL(x,arg) (cpp2::impl::assert_in_bounds<(arg)>(x))
711711

712+
#ifdef CPP2_NO_RTTI
713+
// Compile-Time type name deduction for -fno-rtti builds
714+
//
715+
constexpr auto process_type_name(std::string_view name) -> std::string_view {
716+
#if defined(__clang__) || defined(__GNUC__)
717+
constexpr auto type_prefix = std::string_view("T = ");
718+
constexpr auto types_close_parenthesis = ']';
719+
#elif defined(_MSC_VER)
720+
constexpr auto type_prefix = std::string_view("type_name<");
721+
constexpr auto types_close_parenthesis = '>';
722+
#endif
723+
auto pos = name.find(type_prefix);
724+
if (pos != std::string_view::npos) {
725+
name = name.substr(pos);
726+
name.remove_prefix(type_prefix.size());
727+
}
728+
729+
pos = name.find_last_of(types_close_parenthesis);
730+
if (pos != std::string_view::npos) {
731+
name = name.substr(0, pos);
732+
}
733+
734+
#if defined(__GNUC__)
735+
constexpr auto type_separator = ';';
736+
pos = name.find(type_separator);
737+
if (pos != std::string_view::npos) {
738+
name = name.substr(0, pos);
739+
}
740+
#endif
741+
742+
return name;
743+
}
744+
745+
template<typename T>
746+
constexpr auto type_name() -> std::string_view {
747+
#if defined(__clang__) || defined(__GNUC__)
748+
constexpr auto ret = process_type_name(__PRETTY_FUNCTION__);
749+
#elif defined(_MSC_VER)
750+
constexpr auto ret = process_type_name(__FUNCSIG__);
751+
#else
752+
constexpr auto ret = "<cannot determine type>";
753+
#endif
754+
return ret;
755+
}
756+
757+
#endif
712758

713759
//-----------------------------------------------------------------------
714760
//
@@ -727,11 +773,18 @@ auto assert_in_bounds(auto&& x, auto&& arg CPP2_SOURCE_LOCATION_PARAM_WITH_DEFAU
727773

728774
[[noreturn]] auto Throw(auto&& x, [[maybe_unused]] char const* msg) -> void {
729775
#ifdef CPP2_NO_EXCEPTIONS
730-
auto err = std::string{"exceptions are disabled with -fno-exceptions - attempted to throw exception with type \"" + typeid(decltype(x)).name() + "\""};
776+
auto err = std::string{"exceptions are disabled with -fno-exceptions - attempted to throw exception with type \""};
777+
778+
#ifdef CPP2_NO_RTTI
779+
err += type_name<decltype(x)>();
780+
#else
781+
err += typeid(decltype(x)).name();
782+
#endif
783+
err += "\"";
731784
if (msg) {
732-
err += " and the message \"" + msg + "\"";
785+
err += std::string{" and the message \""} + msg + "\"";
733786
}
734-
type_safety.report_violation( err );
787+
type_safety.report_violation( err.c_str() );
735788
std::terminate();
736789
#else
737790
throw CPP2_FORWARD(x);

0 commit comments

Comments
 (0)