18
18
19
19
#include " gtest/gtest.h"
20
20
21
+ #include < type_traits>
22
+
21
23
using namespace lldb ;
22
24
using namespace lldb_private ;
23
25
@@ -70,28 +72,12 @@ class ValueObjectMockProcessTest : public ::testing::Test {
70
72
m_type_system = m_holder->GetAST ();
71
73
}
72
74
73
- CompilerType
74
- MakeEnumType (const std::vector<std::pair<const char *, int >> enumerators) {
75
- CompilerType uint_type = m_type_system->GetBuiltinTypeForEncodingAndBitSize (
76
- lldb::eEncodingUint, 32 );
77
- CompilerType enum_type = m_type_system->CreateEnumerationType (
78
- " TestEnum" , m_type_system->GetTranslationUnitDecl (),
79
- OptionalClangModuleID (), Declaration (), uint_type, false );
80
-
81
- m_type_system->StartTagDeclarationDefinition (enum_type);
82
- Declaration decl;
83
- for (auto [name, value] : enumerators)
84
- m_type_system->AddEnumerationValueToEnumerationType (enum_type, decl, name,
85
- value, 32 );
86
- m_type_system->CompleteTagDeclarationDefinition (enum_type);
87
-
88
- return enum_type;
89
- }
90
-
91
- void TestDumpValueObject (
92
- CompilerType enum_type,
93
- const std::vector<
94
- std::tuple<uint32_t , DumpValueObjectOptions, const char *>> &tests) {
75
+ template <typename UnderlyingType>
76
+ void TestDumpEnum (
77
+ const std::vector<std::pair<const char *, UnderlyingType>> enumerators,
78
+ const std::vector<std::tuple<UnderlyingType, DumpValueObjectOptions,
79
+ const char *>> &tests) {
80
+ CompilerType enum_type = MakeEnumType (enumerators);
95
81
StreamString strm;
96
82
ConstString var_name (" test_var" );
97
83
ByteOrder endian = endian::InlHostByteOrder ();
@@ -107,6 +93,27 @@ class ValueObjectMockProcessTest : public ::testing::Test {
107
93
}
108
94
}
109
95
96
+ template <typename UnderlyingType>
97
+ CompilerType MakeEnumType (
98
+ const std::vector<std::pair<const char *, UnderlyingType>> enumerators) {
99
+ CompilerType int_type = m_type_system->GetBuiltinTypeForEncodingAndBitSize (
100
+ std::is_same<UnderlyingType, int >::value ? lldb::eEncodingSint
101
+ : lldb::eEncodingUint,
102
+ 32 );
103
+ CompilerType enum_type = m_type_system->CreateEnumerationType (
104
+ " TestEnum" , m_type_system->GetTranslationUnitDecl (),
105
+ OptionalClangModuleID (), Declaration (), int_type, false );
106
+
107
+ m_type_system->StartTagDeclarationDefinition (enum_type);
108
+ Declaration decl;
109
+ for (auto [name, value] : enumerators)
110
+ m_type_system->AddEnumerationValueToEnumerationType (enum_type, decl, name,
111
+ value, 32 );
112
+ m_type_system->CompleteTagDeclarationDefinition (enum_type);
113
+
114
+ return enum_type;
115
+ }
116
+
110
117
ExecutionContext m_exe_ctx;
111
118
TypeSystemClang *m_type_system;
112
119
@@ -123,12 +130,25 @@ class ValueObjectMockProcessTest : public ::testing::Test {
123
130
lldb::ProcessSP m_process_sp;
124
131
};
125
132
133
+ TEST_F (ValueObjectMockProcessTest, EmptyEnum) {
134
+ // All values of an empty enum should be shown as plain numbers.
135
+ TestDumpEnum<unsigned >({}, {{0 , {}, " (TestEnum) test_var = 0\n " },
136
+ {1 , {}, " (TestEnum) test_var = 1\n " },
137
+ {2 , {}, " (TestEnum) test_var = 2\n " }});
138
+
139
+ TestDumpEnum<int >({}, {{-2 , {}, " (TestEnum) test_var = -2\n " },
140
+ {-1 , {}, " (TestEnum) test_var = -1\n " },
141
+ {0 , {}, " (TestEnum) test_var = 0\n " },
142
+ {1 , {}, " (TestEnum) test_var = 1\n " },
143
+ {2 , {}, " (TestEnum) test_var = 2\n " }});
144
+ }
145
+
126
146
TEST_F (ValueObjectMockProcessTest, Enum) {
127
147
// This is not a bitfield-like enum, so values are printed as decimal by
128
148
// default. Also we only show the enumerator name if the value is an
129
149
// exact match.
130
- TestDumpValueObject (
131
- MakeEnumType ( {{" test_2" , 2 }, {" test_3" , 3 }}) ,
150
+ TestDumpEnum< unsigned > (
151
+ {{" test_2" , 2 }, {" test_3" , 3 }},
132
152
{{0 , {}, " (TestEnum) test_var = 0\n " },
133
153
{1 , {}, " (TestEnum) test_var = 1\n " },
134
154
{2 , {}, " (TestEnum) test_var = test_2\n " },
@@ -151,8 +171,8 @@ TEST_F(ValueObjectMockProcessTest, BitFieldLikeEnum) {
151
171
// set. lldb treats this as a "bitfield like enum". This means we show values
152
172
// as hex, and values without exact matches are shown as a combination of
153
173
// enumerators and any remaining value left over.
154
- TestDumpValueObject (
155
- MakeEnumType ( {{" test_2" , 2 }, {" test_4" , 4 }}) ,
174
+ TestDumpEnum< unsigned > (
175
+ {{" test_2" , 2 }, {" test_4" , 4 }},
156
176
{
157
177
{0 , {}, " (TestEnum) test_var = 0x0\n " },
158
178
{1 , {}, " (TestEnum) test_var = 0x1\n " },
0 commit comments