From 223b9e606ceb3f689bf2f5622d94056d699e2bf8 Mon Sep 17 00:00:00 2001 From: neargye Date: Tue, 26 Mar 2019 12:45:00 +0500 Subject: [PATCH] nameof_enum supported on the GCC >= 9 --- include/nameof.hpp | 10 ++-------- test/test.cpp | 37 ------------------------------------- 2 files changed, 2 insertions(+), 45 deletions(-) diff --git a/include/nameof.hpp b/include/nameof.hpp index 1bb9b43..86a6055 100644 --- a/include/nameof.hpp +++ b/include/nameof.hpp @@ -165,12 +165,8 @@ template #if defined(__clang__) std::string_view name{__PRETTY_FUNCTION__}; constexpr auto suffix = sizeof("]") - 1; -#elif defined(__GNUC__) +#elif defined(__GNUC__) && __GNUC__ >= 9 std::string_view name{__PRETTY_FUNCTION__}; -# if __GNUC__ < 9 - constexpr auto prefix = sizeof("constexpr std::string_view nameof::detail::nameof_enum_impl() [with E = ") + nameof_type_impl>().length() + sizeof("; V = "); - name.remove_prefix(prefix); -# endif constexpr auto suffix = sizeof("; std::string_view = std::basic_string_view]") - 1; #elif defined(_MSC_VER) std::string_view name{__FUNCSIG__}; @@ -179,16 +175,14 @@ template return "nameof_enum::unsupported_compiler"; #endif -#if defined(__clang__) || defined(__GNUC__) || defined(_MSC_VER) +#if defined(__clang__) || (defined(__GNUC__) && __GNUC__ >= 9) || defined(_MSC_VER) name.remove_suffix(suffix); -# if defined(__clang__) || (defined(__GNUC__) && __GNUC__ >= 9) || defined(_MSC_VER) for (std::size_t i = name.size(); i > 0; --i) { if (!is_name_char(name[i - 1])) { name.remove_prefix(i); break; } } -# endif return name; #endif } diff --git a/test/test.cpp b/test/test.cpp index 7b3a3c3..971faad 100644 --- a/test/test.cpp +++ b/test/test.cpp @@ -223,17 +223,6 @@ TEST_CASE("NAMEOF_ENUM") { REQUIRE(NAMEOF_ENUM(Directions::Right) == "Right"); REQUIRE(NAMEOF_ENUM(directions) == "Right"); -#elif defined(__GNUC__) - REQUIRE(NAMEOF_ENUM(Color::RED) == "(Color)-1"); - REQUIRE(NAMEOF_ENUM(color) == "(Color)-1"); - - REQUIRE(NAMEOF_ENUM(Color::BLUE) == "(Color)1"); - REQUIRE(NAMEOF_ENUM(color_) == "(Color)1"); - - REQUIRE(NAMEOF_ENUM(m[1]) == "(Color)0"); - - REQUIRE(NAMEOF_ENUM(Directions::Right) == "(Directions)2"); - REQUIRE(NAMEOF_ENUM(directions) == "(Directions)2"); #endif } @@ -251,17 +240,6 @@ TEST_CASE("nameof::nameof_enum(value)") { REQUIRE(nameof::nameof_enum(Directions::Right) == "Right"); REQUIRE(nameof::nameof_enum(directions) == "Right"); -#elif defined(__GNUC__) - REQUIRE(nameof::nameof_enum(Color::RED) == "(Color)-1"); - REQUIRE(nameof::nameof_enum(color) == "(Color)-1"); - - REQUIRE(nameof::nameof_enum(Color::BLUE) == "(Color)1"); - REQUIRE(nameof::nameof_enum(color_) == "(Color)1"); - - REQUIRE(nameof::nameof_enum(m[1]) == "(Color)0"); - - REQUIRE(nameof::nameof_enum(Directions::Right) == "(Directions)2"); - REQUIRE(nameof::nameof_enum(directions) == "(Directions)2"); #endif } @@ -279,17 +257,6 @@ TEST_CASE("nameof::nameof_enum()") { REQUIRE(nameof::nameof_enum() == "Right"); REQUIRE(nameof::nameof_enum() == "Right"); -#elif defined(__GNUC__) - REQUIRE(nameof::nameof_enum() == "(Color)-1"); - REQUIRE(nameof::nameof_enum() == "(Color)-1"); - - REQUIRE(nameof::nameof_enum() == "(Color)1"); - REQUIRE(nameof::nameof_enum() == "(Color)1"); - - REQUIRE(nameof::nameof_enum() == "(Color)0"); - - REQUIRE(nameof::nameof_enum() == "(Directions)2"); - REQUIRE(nameof::nameof_enum() == "(Directions)2"); #endif } @@ -456,8 +423,6 @@ TEST_CASE("Spaces and Tabs ignored") { REQUIRE(NAMEOF_RAW( struct_var ) == "struct_var"); #if defined(__clang__) || (defined(__GNUC__) && __GNUC__ >= 9) || defined(_MSC_VER) REQUIRE(NAMEOF_ENUM( color ) == "RED"); -#elif defined(__GNUC__) - REQUIRE(NAMEOF_ENUM( color ) == "(Color)-1"); #endif REQUIRE(NAMEOF_TYPE( struct_var ) == "SomeStruct"); REQUIRE(NAMEOF_TYPE_T( decltype(struct_var) ) == "SomeStruct"); @@ -469,8 +434,6 @@ TEST_CASE("Spaces and Tabs ignored") { REQUIRE(NAMEOF_RAW( struct_var ) == "struct_var"); #if defined(__clang__) || (defined(__GNUC__) && __GNUC__ >= 9) || defined(_MSC_VER) REQUIRE(NAMEOF_ENUM( color ) == "RED"); -#elif defined(__GNUC__) - REQUIRE(NAMEOF_ENUM( color ) == "(Color)-1"); #endif REQUIRE(NAMEOF_TYPE( struct_var ) == "SomeStruct"); REQUIRE(NAMEOF_TYPE_T( decltype(struct_var) ) == "SomeStruct");