Skip to content

Commit 4ca2b63

Browse files
thorsten-kleinkris-jusiak
authored andcommitted
fix type traits for inline namespaces
get_type_name result may not contain inline namespace v1::
1 parent 5f298ab commit 4ca2b63

2 files changed

Lines changed: 33 additions & 6 deletions

File tree

include/GUnit/Detail/TypeTraits.h

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -154,17 +154,36 @@ auto get_type_name_impl(const char *ptr, std::index_sequence<Ns...>) {
154154
return str;
155155
}
156156

157+
constexpr bool const_strncmp(const char* a, const char*b, uint8_t n)
158+
{
159+
bool retval = 1;
160+
161+
for(uint8_t i = 0; i<n; i++)
162+
{
163+
if(a[i] != b[i])
164+
{
165+
retval = 0;
166+
break;
167+
}
168+
}
169+
return retval;
170+
}
171+
157172
template <class T>
158173
const char *get_type_name() {
174+
159175
#if defined(__clang__)
160-
return get_type_name_impl<T, 54>(
161-
__PRETTY_FUNCTION__,
162-
std::make_index_sequence<sizeof(__PRETTY_FUNCTION__) - 54 - 2>{});
176+
constexpr char opt1[] = "const char *testing::v1::detail::get_type_name() [T = ";
177+
constexpr char opt2[] = "const char *testing::detail::get_type_name() [T = ";
178+
constexpr uint16_t offset = const_strncmp(__PRETTY_FUNCTION__, opt1, sizeof(opt1)-1)? sizeof(opt1)-1 : sizeof(opt2)-1;
163179
#elif defined(__GNUC__)
164-
return get_type_name_impl<T, 59>(
165-
__PRETTY_FUNCTION__,
166-
std::make_index_sequence<sizeof(__PRETTY_FUNCTION__) - 59 - 2>{});
180+
constexpr uint16_t offset = sizeof("const char* testing::v1::detail::get_type_name() [with T = ") - 1;
167181
#endif
182+
constexpr auto length = sizeof(__PRETTY_FUNCTION__) - offset - 2; // " ]" in the end has length 2
183+
184+
return get_type_name_impl<T, offset>(
185+
__PRETTY_FUNCTION__,
186+
std::make_index_sequence<length>{});
168187
}
169188

170189
} // namespace detail

test/Detail/TypeTraits.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
#include "GUnit/Detail/TypeTraits.h"
99
#include <gtest/gtest.h>
1010

11+
#include <algorithm>
12+
1113
struct a {};
1214

1315
namespace testing {
@@ -136,7 +138,13 @@ TEST(TypeTraits, ShouldGetTypeName) {
136138
EXPECT_STREQ("int", get_type_name<int>());
137139
EXPECT_STREQ("const double", get_type_name<const double>());
138140
EXPECT_STREQ("a", get_type_name<a>());
141+
#if defined(__clang__)
142+
std::vector<std::string> expected = {"testing::v1::detail::n","testing::detail::n"}; // get_type_name result may not contain v1::
143+
EXPECT_TRUE( std::find(expected.begin(), expected.end(), get_type_name<n>()) != expected.end() );
144+
#elif defined(__GNUC__)
139145
EXPECT_STREQ("testing::v1::detail::n", get_type_name<n>());
146+
#endif
147+
// EXPECT_STREQ("a", "b");
140148
}
141149
} // detail
142150
} // v1

0 commit comments

Comments
 (0)