1818
1919#include " gtest/gtest.h"
2020
21+ #include < type_traits>
22+
2123using namespace lldb ;
2224using namespace lldb_private ;
2325
@@ -70,28 +72,12 @@ class ValueObjectMockProcessTest : public ::testing::Test {
7072 m_type_system = m_holder->GetAST ();
7173 }
7274
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);
9581 StreamString strm;
9682 ConstString var_name (" test_var" );
9783 ByteOrder endian = endian::InlHostByteOrder ();
@@ -107,6 +93,27 @@ class ValueObjectMockProcessTest : public ::testing::Test {
10793 }
10894 }
10995
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+
110117 ExecutionContext m_exe_ctx;
111118 TypeSystemClang *m_type_system;
112119
@@ -123,12 +130,25 @@ class ValueObjectMockProcessTest : public ::testing::Test {
123130 lldb::ProcessSP m_process_sp;
124131};
125132
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+
126146TEST_F (ValueObjectMockProcessTest, Enum) {
127147 // This is not a bitfield-like enum, so values are printed as decimal by
128148 // default. Also we only show the enumerator name if the value is an
129149 // exact match.
130- TestDumpValueObject (
131- MakeEnumType ( {{" test_2" , 2 }, {" test_3" , 3 }}) ,
150+ TestDumpEnum< unsigned > (
151+ {{" test_2" , 2 }, {" test_3" , 3 }},
132152 {{0 , {}, " (TestEnum) test_var = 0\n " },
133153 {1 , {}, " (TestEnum) test_var = 1\n " },
134154 {2 , {}, " (TestEnum) test_var = test_2\n " },
@@ -151,8 +171,8 @@ TEST_F(ValueObjectMockProcessTest, BitFieldLikeEnum) {
151171 // set. lldb treats this as a "bitfield like enum". This means we show values
152172 // as hex, and values without exact matches are shown as a combination of
153173 // 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 }},
156176 {
157177 {0 , {}, " (TestEnum) test_var = 0x0\n " },
158178 {1 , {}, " (TestEnum) test_var = 0x1\n " },
0 commit comments