13
13
14
14
#include " llvm/ADT/StringRef.h"
15
15
16
+ // Versions of GCC prior to GCC 9 don't declare __PRETTY_FUNCTION__ as constexpr
17
+ #if defined(__clang__) || defined(_MSC_VER) || \
18
+ (defined(__GNUC__) && __GNUC__ >= 9 )
19
+ #define LLVM_GET_TYPE_NAME_CONSTEXPR constexpr
20
+ #define LLVM_GET_TYPE_NAME_STATIC_ASSERT 1
21
+ #else
22
+ #define LLVM_GET_TYPE_NAME_CONSTEXPR
23
+ #define LLVM_GET_TYPE_NAME_STATIC_ASSERT 0
24
+ #include < cassert>
25
+ #endif
26
+
16
27
namespace llvm {
17
28
18
29
// / We provide a function which tries to compute the (demangled) name of a type
@@ -25,50 +36,65 @@ namespace llvm {
25
36
// / The returned StringRef will point into a static storage duration string.
26
37
// / However, it may not be null terminated and may be some strangely aligned
27
38
// / inner substring of a larger string.
28
- template <typename DesiredTypeName> inline constexpr StringRef getTypeName () {
39
+ template <typename DesiredTypeName>
40
+ inline LLVM_GET_TYPE_NAME_CONSTEXPR StringRef getTypeName () {
29
41
#if defined(__clang__) || defined(__GNUC__)
30
- constexpr std::string_view Name = __PRETTY_FUNCTION__;
42
+ LLVM_GET_TYPE_NAME_CONSTEXPR std::string_view Name = __PRETTY_FUNCTION__;
31
43
32
- constexpr std::string_view Key = " DesiredTypeName = " ;
33
- constexpr std::string_view TemplateParamsStart = Name.substr (Name.find (Key));
44
+ LLVM_GET_TYPE_NAME_CONSTEXPR std::string_view Key = " DesiredTypeName = " ;
45
+ LLVM_GET_TYPE_NAME_CONSTEXPR std::string_view TemplateParamsStart =
46
+ Name.substr (Name.find (Key));
47
+ #if LLVM_GET_TYPE_NAME_STATIC_ASSERT
34
48
static_assert (!TemplateParamsStart.empty (),
35
49
" Unable to find the template parameter!" );
36
- constexpr std::string_view SubstitutionKey =
50
+ #else
51
+ assert (!TemplateParamsStart.empty () &&
52
+ " Unable to find the template parameter!" );
53
+ #endif
54
+
55
+ LLVM_GET_TYPE_NAME_CONSTEXPR std::string_view SubstitutionKey =
37
56
TemplateParamsStart.substr (Key.size ());
38
57
58
+ #if LLVM_GET_TYPE_NAME_STATIC_ASSERT
39
59
// ends_with() is only available in c++20
40
60
static_assert (!SubstitutionKey.empty () && SubstitutionKey.back () == ' ]' ,
41
61
" Name doesn't end in the substitution key!" );
62
+ #else
63
+ assert (!SubstitutionKey.empty () && SubstitutionKey.back () == ' ]' &&
64
+ " Name doesn't end in the substitution key!" );
65
+ #endif
66
+
42
67
return SubstitutionKey.substr (0 , SubstitutionKey.size () - 1 );
43
68
#elif defined(_MSC_VER)
44
- constexpr std::string_view Name = __FUNCSIG__;
69
+ LLVM_GET_TYPE_NAME_CONSTEXPR std::string_view Name = __FUNCSIG__;
45
70
46
- constexpr std::string_view Key = " getTypeName<" ;
47
- constexpr std::string_view GetTypeNameStart = Name.substr (Name.find (Key));
71
+ LLVM_GET_TYPE_NAME_CONSTEXPR std::string_view Key = " getTypeName<" ;
72
+ LLVM_GET_TYPE_NAME_CONSTEXPR std::string_view GetTypeNameStart =
73
+ Name.substr (Name.find (Key));
48
74
static_assert (!GetTypeNameStart.empty (),
49
75
" Unable to find the template parameter!" );
50
- constexpr std::string_view SubstitutionKey =
76
+ LLVM_GET_TYPE_NAME_CONSTEXPR std::string_view SubstitutionKey =
51
77
GetTypeNameStart.substr (Key.size ());
52
78
53
79
// starts_with() only available in c++20
54
- constexpr std::string_view RmPrefixClass =
80
+ LLVM_GET_TYPE_NAME_CONSTEXPR std::string_view RmPrefixClass =
55
81
SubstitutionKey.find (" class " ) == 0
56
82
? SubstitutionKey.substr (sizeof (" class " ) - 1 )
57
83
: SubstitutionKey;
58
- constexpr std::string_view RmPrefixStruct =
84
+ LLVM_GET_TYPE_NAME_CONSTEXPR std::string_view RmPrefixStruct =
59
85
RmPrefixClass.find (" struct " ) == 0
60
86
? RmPrefixClass.substr (sizeof (" struct " ) - 1 )
61
87
: RmPrefixClass;
62
- constexpr std::string_view RmPrefixUnion =
88
+ LLVM_GET_TYPE_NAME_CONSTEXPR std::string_view RmPrefixUnion =
63
89
RmPrefixStruct.find (" union " ) == 0
64
90
? RmPrefixStruct.substr (sizeof (" union " ) - 1 )
65
91
: RmPrefixStruct;
66
- constexpr std::string_view RmPrefixEnum =
92
+ LLVM_GET_TYPE_NAME_CONSTEXPR std::string_view RmPrefixEnum =
67
93
RmPrefixUnion.find (" enum " ) == 0
68
94
? RmPrefixUnion.substr (sizeof (" enum " ) - 1 )
69
95
: RmPrefixUnion;
70
96
71
- constexpr auto AnglePos = RmPrefixEnum.rfind (' >' );
97
+ LLVM_GET_TYPE_NAME_CONSTEXPR auto AnglePos = RmPrefixEnum.rfind (' >' );
72
98
static_assert (AnglePos != std::string_view::npos,
73
99
" Unable to find the closing '>'!" );
74
100
return RmPrefixEnum.substr (0 , AnglePos);
@@ -81,4 +107,8 @@ template <typename DesiredTypeName> inline constexpr StringRef getTypeName() {
81
107
82
108
} // namespace llvm
83
109
110
+ // Don't leak out of this header file
111
+ #undef LLVM_GET_TYPE_NAME_CONSTEXPR
112
+ #undef LLVM_GET_TYPE_NAME_STATIC_ASSERT
113
+
84
114
#endif
0 commit comments