Skip to content

Commit cf967ad

Browse files
committed
non bitfield enum tests and refactoring
1 parent 0c6fe37 commit cf967ad

File tree

1 file changed

+85
-45
lines changed

1 file changed

+85
-45
lines changed

lldb/unittests/ValueObject/DumpValueObjectOptionsTests.cpp

Lines changed: 85 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,42 @@ class ValueObjectMockProcessTest : public ::testing::Test {
7070
m_type_system = m_holder->GetAST();
7171
}
7272

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) {
95+
StreamString strm;
96+
ConstString var_name("test_var");
97+
ByteOrder endian = endian::InlHostByteOrder();
98+
ExecutionContextScope *exe_scope = m_exe_ctx.GetBestExecutionContextScope();
99+
for (auto [value, options, expected] : tests) {
100+
DataExtractor data_extractor{&value, sizeof(value), endian, 4};
101+
ValueObjectConstResult::Create(exe_scope, enum_type, var_name,
102+
data_extractor)
103+
->Dump(strm, options);
104+
ASSERT_STREQ(strm.GetString().str().c_str(), expected);
105+
strm.Clear();
106+
}
107+
}
108+
73109
ExecutionContext m_exe_ctx;
74110
TypeSystemClang *m_type_system;
75111

@@ -87,50 +123,54 @@ class ValueObjectMockProcessTest : public ::testing::Test {
87123
};
88124

89125
TEST_F(ValueObjectMockProcessTest, Enum) {
90-
CompilerType uint_type = m_type_system->GetBuiltinTypeForEncodingAndBitSize(
91-
lldb::eEncodingUint, 32);
92-
CompilerType enum_type = m_type_system->CreateEnumerationType(
93-
"test_enum", m_type_system->GetTranslationUnitDecl(),
94-
OptionalClangModuleID(), Declaration(), uint_type, false);
95-
96-
m_type_system->StartTagDeclarationDefinition(enum_type);
97-
Declaration decl;
98-
// Each value sets one bit in the enum, to make this a "bitfield like enum".
99-
m_type_system->AddEnumerationValueToEnumerationType(enum_type, decl, "test_2",
100-
2, 32);
101-
m_type_system->AddEnumerationValueToEnumerationType(enum_type, decl, "test_4",
102-
4, 32);
103-
m_type_system->CompleteTagDeclarationDefinition(enum_type);
104-
105-
std::vector<std::tuple<uint32_t, DumpValueObjectOptions, const char *>> enums{
106-
{0, {}, "(test_enum) test_var =\n"},
107-
{1, {}, "(test_enum) test_var = 0x1\n"},
108-
{2, {}, "(test_enum) test_var = test_2\n"},
109-
{4, {}, "(test_enum) test_var = test_4\n"},
110-
{6, {}, "(test_enum) test_var = test_2 | test_4\n"},
111-
{7, {}, "(test_enum) test_var = test_2 | test_4 | 0x1\n"},
112-
{8, {}, "(test_enum) test_var = 0x8\n"},
113-
{1, DumpValueObjectOptions().SetHideRootName(true), "(test_enum) 0x1\n"},
114-
{1, DumpValueObjectOptions().SetHideRootType(true), "test_var = 0x1\n"},
115-
{1, DumpValueObjectOptions().SetHideRootName(true).SetHideRootType(true),
116-
"0x1\n"},
117-
{1, DumpValueObjectOptions().SetHideName(true), "(test_enum) 0x1\n"},
118-
{1, DumpValueObjectOptions().SetHideValue(true),
119-
"(test_enum) test_var =\n"},
120-
{1, DumpValueObjectOptions().SetHideName(true).SetHideValue(true),
121-
"(test_enum) \n"},
122-
};
126+
// This is not a bitfield-like enum, so values are printed as decimal by
127+
// default. Also we only show the enumerator name if the value is an
128+
// exact match.
129+
TestDumpValueObject(
130+
MakeEnumType({{"test_2", 2}, {"test_3", 3}}),
131+
{{0, {}, "(TestEnum) test_var = 0\n"},
132+
{1, {}, "(TestEnum) test_var = 1\n"},
133+
{2, {}, "(TestEnum) test_var = test_2\n"},
134+
{3, {}, "(TestEnum) test_var = test_3\n"},
135+
{4, {}, "(TestEnum) test_var = 4\n"},
136+
{5, {}, "(TestEnum) test_var = 5\n"},
137+
{1, DumpValueObjectOptions().SetHideRootName(true), "(TestEnum) 1\n"},
138+
{1, DumpValueObjectOptions().SetHideRootType(true), "test_var = 1\n"},
139+
{1, DumpValueObjectOptions().SetHideRootName(true).SetHideRootType(true),
140+
"1\n"},
141+
{1, DumpValueObjectOptions().SetHideName(true), "(TestEnum) 1\n"},
142+
{1, DumpValueObjectOptions().SetHideValue(true),
143+
"(TestEnum) test_var =\n"},
144+
{1, DumpValueObjectOptions().SetHideName(true).SetHideValue(true),
145+
"(TestEnum) \n"}});
146+
}
123147

124-
StreamString strm;
125-
ExecutionContextScope *exe_scope = m_exe_ctx.GetBestExecutionContextScope();
126-
ConstString var_name("test_var");
127-
ByteOrder endian = endian::InlHostByteOrder();
128-
for (auto [value, options, expected] : enums) {
129-
DataExtractor data_extractor{&value, sizeof(value), endian, 4};
130-
ValueObjectConstResult::Create(exe_scope, enum_type, var_name,
131-
data_extractor)
132-
->Dump(strm, options);
133-
ASSERT_STREQ(strm.GetString().str().c_str(), expected);
134-
strm.Clear();
135-
}
148+
TEST_F(ValueObjectMockProcessTest, BitFieldLikeEnum) {
149+
// These enumerators set individual bits in the value, as if it were a flag
150+
// set. lldb treats this as a "bitfield like enum". This means we show values
151+
// as hex, a value of 0 shows nothing, and values with no exact enumerator are
152+
// shown as combinations of the other values.
153+
TestDumpValueObject(
154+
MakeEnumType({{"test_2", 2}, {"test_4", 4}}),
155+
{
156+
{0, {}, "(TestEnum) test_var =\n"},
157+
{1, {}, "(TestEnum) test_var = 0x1\n"},
158+
{2, {}, "(TestEnum) test_var = test_2\n"},
159+
{4, {}, "(TestEnum) test_var = test_4\n"},
160+
{6, {}, "(TestEnum) test_var = test_2 | test_4\n"},
161+
{7, {}, "(TestEnum) test_var = test_2 | test_4 | 0x1\n"},
162+
{8, {}, "(TestEnum) test_var = 0x8\n"},
163+
{1, DumpValueObjectOptions().SetHideRootName(true),
164+
"(TestEnum) 0x1\n"},
165+
{1, DumpValueObjectOptions().SetHideRootType(true),
166+
"test_var = 0x1\n"},
167+
{1,
168+
DumpValueObjectOptions().SetHideRootName(true).SetHideRootType(true),
169+
"0x1\n"},
170+
{1, DumpValueObjectOptions().SetHideName(true), "(TestEnum) 0x1\n"},
171+
{1, DumpValueObjectOptions().SetHideValue(true),
172+
"(TestEnum) test_var =\n"},
173+
{1, DumpValueObjectOptions().SetHideName(true).SetHideValue(true),
174+
"(TestEnum) \n"},
175+
});
136176
}

0 commit comments

Comments
 (0)