Files
llvm-project/lldb/bindings/interface/SBInstructionExtensions.i
n2h9 a617b901cd [lldb] [disassembler] chore: add GetVariableAnnotations to SBInstruction api (#177676)
## Description
Contribution to this topic [Rich Disassembler for
LLDB](https://discourse.llvm.org/t/rich-disassembler-for-lldb/76952),
this part.
```
The rich disassembler output should be exposed as structured data and made available through LLDB’s scripting API so more tooling could be built on top of this
```

----

This pr replaces #174847

As was suggested in [this
comment](https://github.com/llvm/llvm-project/pull/174847#issuecomment-3757015552),
implement access to variable annotations from `SBInstruction` class
itself.

Notes:
-   did run black formatter on the python file;

## Testing
Run test with
```sh
./build/bin/lldb-dotest -v -p TestVariableAnnotationsDisassembler.py lldb/test/API/functionalities/disassembler-variables
```

all tests (9 existing + 1 newly added) are passing

<details>
<summary>screenshot 2026-01-23</summary>

build from the latest commit  08f00730b5768a8e3f7039d810084fabaaa24470

<img width="1506" height="562" alt="image"
src="https://github.com/user-attachments/assets/69516353-3432-47df-ae45-c40b51ec14c4"
/>

</details>

<details>
<summary>screenshot 2026-01-29</summary>

build from the latest commit  f48a1a2c10f96a457ca6844be2ccc9406d3d57a0

<img width="1232" height="740" alt="image"
src="https://github.com/user-attachments/assets/9d104ce6-36c3-430b-98fe-f028f83a6b6d"
/>


</details>

---------

Signed-off-by: Nikita B <n2h9z4@gmail.com>
2026-01-29 20:54:42 +01:00

70 lines
3.0 KiB
OpenEdge ABL

STRING_EXTENSION_OUTSIDE(SBInstruction)
%extend lldb::SBInstruction {
#ifdef SWIGPYTHON
%pythoncode %{
def __hex__(self):
""" Returns the address of the instruction. """
return self.GetAddress()
def __len__(self):
""" Returns the size of the instruction. """
return self.GetByteSize()
def __mnemonic_property__ (self):
return self.GetMnemonic (target)
def __operands_property__ (self):
return self.GetOperands (target)
def __comment_property__ (self):
return self.GetComment (target)
def __file_addr_property__ (self):
return self.GetAddress ().GetFileAddress()
def __load_adrr_property__ (self):
return self.GetComment (target)
def variable_annotations(self):
"""Get variable annotations as a Python list of dictionaries.
Returns:
List of dictionaries, each containing variable annotation data
"""
structured_data = self.GetVariableAnnotations()
if not structured_data.IsValid():
return []
annotations = []
for i in range(structured_data.GetSize()):
item = structured_data.GetItemAtIndex(i)
if item.GetType() != eStructuredDataTypeDictionary:
continue
annotation = {}
integer_fields = ['start_address', 'end_address', 'register_kind', 'decl_line']
string_fields = ['variable_name', 'location_description', 'decl_file', 'type_name']
for field in integer_fields:
value = item.GetValueForKey(field)
if value.IsValid():
annotation[field] = value.GetUnsignedIntegerValue()
for field in string_fields:
value = item.GetValueForKey(field)
if value.IsValid():
annotation[field] = value.GetStringValue(1024)
annotations.append(annotation)
return annotations
mnemonic = property(__mnemonic_property__, None, doc='''A read only property that returns the mnemonic for this instruction as a string.''')
operands = property(__operands_property__, None, doc='''A read only property that returns the operands for this instruction as a string.''')
comment = property(__comment_property__, None, doc='''A read only property that returns the comment for this instruction as a string.''')
addr = property(GetAddress, None, doc='''A read only property that returns an lldb object that represents the address (lldb.SBAddress) for this instruction.''')
size = property(GetByteSize, None, doc='''A read only property that returns the size in bytes for this instruction as an integer.''')
is_branch = property(DoesBranch, None, doc='''A read only property that returns a boolean value that indicates if this instruction is a branch instruction.''')
%}
#endif
}