Skip to content

Commit 30367cb

Browse files
authored
[lldb] Add SBType::GetByteAlign (#90960)
lldb already mostly(*) tracks this information. This just makes it available to the SB users. (*) It does not do that for typedefs right now see llvm.org/pr90958
1 parent eb75af2 commit 30367cb

File tree

4 files changed

+39
-0
lines changed

4 files changed

+39
-0
lines changed

lldb/include/lldb/API/SBType.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,8 @@ class SBType {
150150

151151
uint64_t GetByteSize();
152152

153+
uint64_t GetByteAlign();
154+
153155
bool IsPointerType();
154156

155157
bool IsReferenceType();

lldb/source/API/SBType.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#include "lldb/Utility/Stream.h"
2626

2727
#include "llvm/ADT/APSInt.h"
28+
#include "llvm/Support/MathExtras.h"
2829

2930
#include <memory>
3031
#include <optional>
@@ -132,6 +133,18 @@ uint64_t SBType::GetByteSize() {
132133
return 0;
133134
}
134135

136+
uint64_t SBType::GetByteAlign() {
137+
LLDB_INSTRUMENT_VA(this);
138+
139+
if (!IsValid())
140+
return 0;
141+
142+
std::optional<uint64_t> bit_align =
143+
m_opaque_sp->GetCompilerType(/*prefer_dynamic=*/false)
144+
.GetTypeBitAlign(nullptr);
145+
return llvm::divideCeil(bit_align.value_or(0), 8);
146+
}
147+
135148
bool SBType::IsPointerType() {
136149
LLDB_INSTRUMENT_VA(this);
137150

lldb/test/API/python_api/type/TestTypeList.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,3 +272,24 @@ def test(self):
272272
self.assertTrue(int_enum_uchar)
273273
self.DebugSBType(int_enum_uchar)
274274
self.assertEqual(int_enum_uchar.GetName(), "unsigned char")
275+
276+
def test_GetByteAlign(self):
277+
"""Exercise SBType::GetByteAlign"""
278+
self.build()
279+
spec = lldb.SBModuleSpec()
280+
spec.SetFileSpec(lldb.SBFileSpec(self.getBuildArtifact()))
281+
module = lldb.SBModule(spec)
282+
self.assertTrue(module)
283+
284+
# Invalid types should not crash.
285+
self.assertEqual(lldb.SBType().GetByteAlign(), 0)
286+
287+
# Try a type with natural alignment.
288+
void_ptr = module.GetBasicType(lldb.eBasicTypeVoid).GetPointerType()
289+
self.assertTrue(void_ptr)
290+
# Not exactly guaranteed by the spec, but should be true everywhere we
291+
# care about.
292+
self.assertEqual(void_ptr.GetByteSize(), void_ptr.GetByteAlign())
293+
294+
# And an over-aligned type.
295+
self.assertEqual(module.FindFirstType("OverAlignedStruct").GetByteAlign(), 128)

lldb/test/API/python_api/type/main.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,9 @@ enum EnumType {};
5050
enum class ScopedEnumType {};
5151
enum class EnumUChar : unsigned char {};
5252

53+
struct alignas(128) OverAlignedStruct {};
54+
OverAlignedStruct over_aligned_struct;
55+
5356
int main (int argc, char const *argv[])
5457
{
5558
Task *task_head = new Task(-1, NULL);

0 commit comments

Comments
 (0)