Skip to content
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
14 changes: 9 additions & 5 deletions include/rfl/internal/cpp26/get_type_name.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,27 +11,31 @@
namespace rfl::internal::cpp26 {

template <class T>
consteval auto get_type_name() {
consteval auto get_type_name_str_view() {
using Type = std::remove_cvref_t<T>;
if constexpr (std::is_pointer_v<Type>) {
return get_type_name<std::remove_pointer_t<Type>>();
return get_type_name_str_view<std::remove_pointer_t<Type>>();
} else {
// TODO: Is there a way to get the name of the type without using
// compiler-specific extensions?
#if defined(__GNUC__)
constexpr auto display_string = std::meta::display_string_of(^^Type);
constexpr auto drop_last_char =
display_string.substr(0, display_string.size() - 1);
constexpr auto name =
drop_last_char.substr(drop_last_char.find("{aka ") + 5);
return StringLiteral<name.size() + 1>(name);
return drop_last_char.substr(drop_last_char.find("{aka ") + 5);
#else
static_assert(false,
"You are using an unsupported compiler. Please use GCC or "
"explicitly tag your structs using 'Tag' or 'Name'.");
#endif
}
}

template <class T>
consteval auto get_type_name() {
constexpr auto name = get_type_name_str_view<T>();
return StringLiteral<name.size() + 1>(name);
}
} // namespace rfl::internal::cpp26

#endif
Expand Down
6 changes: 5 additions & 1 deletion include/rfl/internal/get_type_name.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,11 @@ consteval auto get_type_name() {

template <class T>
consteval auto get_type_name_str_view() {
return get_type_name<T>().string_view();
#ifndef REFLECTCPP_USE_CPP26_REFLECTION
return cpp20::get_type_name_str_view<T>();
#else
return cpp26::get_type_name_str_view<T>();
#endif
}

} // namespace rfl::internal
Expand Down
29 changes: 29 additions & 0 deletions tests/json/test_regression_bugs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

#include <cstdlib>
#include <string>
#include <string_view>

#include <rfl.hpp>
#include <rfl/internal/get_type_name.hpp>
#include <rfl/json.hpp>

// transform_camel_case prepends '_' to names starting with uppercase
Expand Down Expand Up @@ -83,6 +85,33 @@ TEST(regression, json_writer_handles_non_null_terminated_field_names) {

} // namespace test_writer_strcpy_non_null_terminated

// rfl::internal::get_type_name_str_view<T>() previously returned
// get_type_name<T>().string_view(), which dangled into the temporary
// StringLiteral returned by value and failed constant evaluation.
namespace test_get_type_name_str_view {

struct SampleType {
int value = 0;
};

constexpr std::string_view kIntName = rfl::internal::get_type_name_str_view<int>();
constexpr std::string_view kStructName =
rfl::internal::get_type_name_str_view<SampleType>();

static_assert(kIntName == "int");
static_assert(kIntName == rfl::internal::get_type_name<int>().string_view());
static_assert(kStructName ==
rfl::internal::get_type_name<SampleType>().string_view());

TEST(regression, get_type_name_str_view_is_valid_constexpr) {
EXPECT_EQ(kIntName, "int");
EXPECT_EQ(kStructName,
rfl::internal::get_type_name<SampleType>().string_view());
EXPECT_NE(kStructName.find("SampleType"), std::string_view::npos);
}

} // namespace test_get_type_name_str_view

// Parser_string_view / Parser_span memory leak bugs are tested
// in tests/alloc/ — a separate executable with custom operator new[]/delete[]
// that tracks allocations to detect leaks without ASAN.
Loading