diff --git a/lldb/source/Plugins/TypeSystem/Clang/TypeSystemClang.cpp b/lldb/source/Plugins/TypeSystem/Clang/TypeSystemClang.cpp --- a/lldb/source/Plugins/TypeSystem/Clang/TypeSystemClang.cpp +++ b/lldb/source/Plugins/TypeSystem/Clang/TypeSystemClang.cpp @@ -5097,7 +5097,9 @@ case clang::Type::Record: break; case clang::Type::Enum: - return lldb::eEncodingSint; + return qual_type->isUnsignedIntegerOrEnumerationType() + ? lldb::eEncodingUint + : lldb::eEncodingSint; case clang::Type::DependentSizedArray: case clang::Type::DependentSizedExtVector: case clang::Type::UnresolvedUsing: diff --git a/lldb/test/API/python_api/sbvalue_unsigned_enum_bitfield_value/TestSBValueUnsignedEnumBitField.py b/lldb/test/API/python_api/sbvalue_unsigned_enum_bitfield_value/TestSBValueUnsignedEnumBitField.py new file mode 100644 --- /dev/null +++ b/lldb/test/API/python_api/sbvalue_unsigned_enum_bitfield_value/TestSBValueUnsignedEnumBitField.py @@ -0,0 +1,18 @@ +""" +Test that SBValue doesn't incorrectly sign-extend +the Scalar value of a bitfield that has an unsigned +enum type. + +This test relies on implementation-defined behaviour +of initializing a bit-field with an out-of-range value. +On clang this will do the expected thing of writing +the full value into the bit-field and the trailing +padding bits. Then during sign-extension (which +shouldn't occur for unsigned bit-fields) it would +left-fill the resulting Scalar with 1s; we test +for this not to happen. +""" + +import lldbsuite.test.lldbinline as lldbinline + +lldbinline.MakeInlineTest(__file__, globals()) diff --git a/lldb/test/API/python_api/sbvalue_unsigned_enum_bitfield_value/main.cpp b/lldb/test/API/python_api/sbvalue_unsigned_enum_bitfield_value/main.cpp new file mode 100644 --- /dev/null +++ b/lldb/test/API/python_api/sbvalue_unsigned_enum_bitfield_value/main.cpp @@ -0,0 +1,14 @@ +#include + +enum class EnumVals : uint16_t { VAL0 = 0 }; + +struct Foo { + EnumVals b : 4; +}; + +int main(int argc, char const *argv[], char const *envp[]) { + Foo f{.b = static_cast(8)}; + return 0; //% b = self.frame().FindVariable("f").GetChildMemberWithName("b") + //% val = b.GetValueAsUnsigned() + //% self.assertEqual(val, 8, "Bit-field not correctly extracted") +}