Skip to content

[lldb] Add SBType::GetByteAlign #90960

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions lldb/include/lldb/API/SBType.h
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,8 @@ class SBType {

uint64_t GetByteSize();

uint64_t GetByteAlign();

bool IsPointerType();

bool IsReferenceType();
Expand Down
13 changes: 13 additions & 0 deletions lldb/source/API/SBType.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#include "lldb/Utility/Stream.h"

#include "llvm/ADT/APSInt.h"
#include "llvm/Support/MathExtras.h"

#include <memory>
#include <optional>
Expand Down Expand Up @@ -132,6 +133,18 @@ uint64_t SBType::GetByteSize() {
return 0;
}

uint64_t SBType::GetByteAlign() {
LLDB_INSTRUMENT_VA(this);

if (!IsValid())
return 0;

std::optional<uint64_t> bit_align =
m_opaque_sp->GetCompilerType(/*prefer_dynamic=*/false)
.GetTypeBitAlign(nullptr);
return llvm::divideCeil(bit_align.value_or(0), 8);
}

bool SBType::IsPointerType() {
LLDB_INSTRUMENT_VA(this);

Expand Down
21 changes: 21 additions & 0 deletions lldb/test/API/python_api/type/TestTypeList.py
Original file line number Diff line number Diff line change
Expand Up @@ -272,3 +272,24 @@ def test(self):
self.assertTrue(int_enum_uchar)
self.DebugSBType(int_enum_uchar)
self.assertEqual(int_enum_uchar.GetName(), "unsigned char")

def test_GetByteAlign(self):
"""Exercise SBType::GetByteAlign"""
self.build()
spec = lldb.SBModuleSpec()
spec.SetFileSpec(lldb.SBFileSpec(self.getBuildArtifact()))
module = lldb.SBModule(spec)
self.assertTrue(module)

# Invalid types should not crash.
self.assertEqual(lldb.SBType().GetByteAlign(), 0)

# Try a type with natural alignment.
void_ptr = module.GetBasicType(lldb.eBasicTypeVoid).GetPointerType()
self.assertTrue(void_ptr)
# Not exactly guaranteed by the spec, but should be true everywhere we
# care about.
self.assertEqual(void_ptr.GetByteSize(), void_ptr.GetByteAlign())

# And an over-aligned type.
self.assertEqual(module.FindFirstType("OverAlignedStruct").GetByteAlign(), 128)
3 changes: 3 additions & 0 deletions lldb/test/API/python_api/type/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ enum EnumType {};
enum class ScopedEnumType {};
enum class EnumUChar : unsigned char {};

struct alignas(128) OverAlignedStruct {};
OverAlignedStruct over_aligned_struct;

int main (int argc, char const *argv[])
{
Task *task_head = new Task(-1, NULL);
Expand Down