Skip to content

Commit 6fe62e4

Browse files
committed
[lldb] [disassembler] chore: enhance VariableAnnotator to return structured data: simplify test_structured_annotations_api
Signed-off-by: Nikita B <[email protected]>
1 parent 35c036f commit 6fe62e4

File tree

1 file changed

+6
-99
lines changed

1 file changed

+6
-99
lines changed

lldb/test/API/functionalities/disassembler-variables/TestVariableAnnotationsDisassembler.py

Lines changed: 6 additions & 99 deletions
Original file line numberDiff line numberDiff line change
@@ -138,13 +138,9 @@ def test_structured_annotations_api(self):
138138
if self.TraceOn():
139139
print(f"\nTesting GetVariableAnnotations() API on {instructions.GetSize()} instructions")
140140

141-
# Track what we find.
142-
found_annotations = False
141+
expected_vars = ["argc", "argv", "i"]
143142
found_variables = set()
144143

145-
# Track variable locations to detect changes (for selective printing).
146-
prev_locations = {}
147-
148144
# Test each instruction
149145
for i in range(instructions.GetSize()):
150146
inst = instructions.GetInstructionAtIndex(i)
@@ -155,18 +151,14 @@ def test_structured_annotations_api(self):
155151
self.assertIsInstance(annotations, lldb.SBStructuredData,
156152
"GetVariableAnnotations should return SBStructuredData")
157153

158-
if annotations.GetSize() > 0:
159-
found_annotations = True
160-
161-
# Track current locations and detect changes.
162-
current_locations = {}
163-
should_print = False
154+
self.assertTrue(annotations.GetSize() > 0,
155+
"GetVariableAnnotations should return non empty array")
164156

157+
if annotations.GetSize() > 0:
165158
# Validate each annotation.
166159
for j in range(annotations.GetSize()):
167160
ann = annotations.GetItemAtIndex(j)
168-
self.assertTrue(ann.IsValid(),
169-
f"Invalid annotation at index {j}")
161+
self.assertTrue(ann.IsValid(), f"Invalid annotation at index {j}")
170162

171163
self.assertEqual(ann.GetType(), lldb.eStructuredDataTypeDictionary,
172164
"Each annotation should be a dictionary")
@@ -195,98 +187,13 @@ def test_structured_annotations_api(self):
195187
self.assertTrue(register_kind_obj.IsValid(),
196188
"Missing 'register_kind' field")
197189

198-
# Extract and validate values.
199190
var_name = var_name_obj.GetStringValue(1024)
200-
location = location_obj.GetStringValue(1024)
201-
is_live = is_live_obj.GetBooleanValue()
202-
start_addr = start_addr_obj.GetUnsignedIntegerValue()
203-
end_addr = end_addr_obj.GetUnsignedIntegerValue()
204-
register_kind = register_kind_obj.GetUnsignedIntegerValue()
205-
206-
# Validate types and values.
207-
self.assertIsInstance(var_name, str, "variable_name should be string")
208-
self.assertGreater(len(var_name), 0, "variable_name should not be empty")
209-
210-
self.assertIsInstance(location, str, "location_description should be string")
211-
self.assertGreater(len(location), 0, "location_description should not be empty")
212-
213-
self.assertIsInstance(is_live, bool, "is_live should be boolean")
214-
215-
self.assertIsInstance(start_addr, int, "start_address should be integer")
216-
self.assertIsInstance(end_addr, int, "end_address should be integer")
217-
self.assertGreater(end_addr, start_addr,
218-
"end_address should be greater than start_address")
219-
220-
self.assertIsInstance(register_kind, int, "register_kind should be integer")
221191

222192
# Check for expected variables in this function.
223-
self.assertIn(var_name, ["argc", "argv", "i"],
193+
self.assertIn(var_name, expected_vars,
224194
f"Unexpected variable name: {var_name}")
225195

226196
found_variables.add(var_name)
227197

228-
# Track current location.
229-
current_locations[var_name] = location
230-
231-
# Detect if this is a new variable or location changed.
232-
if var_name not in prev_locations or prev_locations[var_name] != location:
233-
should_print = True
234-
235-
# Check optional fields (may or may not be present).
236-
decl_file_obj = ann.GetValueForKey("decl_file")
237-
if decl_file_obj.IsValid():
238-
decl_file = decl_file_obj.GetStringValue(1024)
239-
self.assertIsInstance(decl_file, str)
240-
self.assertIn("d_original_example.c", decl_file,
241-
f"Expected source file d_original_example.c in {decl_file}")
242-
243-
decl_line_obj = ann.GetValueForKey("decl_line")
244-
if decl_line_obj.IsValid():
245-
decl_line = decl_line_obj.GetUnsignedIntegerValue()
246-
self.assertIsInstance(decl_line, int)
247-
248-
# Validate declaration line matches the source code (according to d_original_example.c).
249-
if var_name == "argc":
250-
self.assertEqual(decl_line, 3, "argc should be declared on line 3")
251-
elif var_name == "argv":
252-
self.assertEqual(decl_line, 3, "argv should be declared on line 3")
253-
elif var_name == "i":
254-
self.assertEqual(decl_line, 4, "i should be declared on line 4")
255-
256-
type_name_obj = ann.GetValueForKey("type_name")
257-
if type_name_obj.IsValid():
258-
type_name = type_name_obj.GetStringValue(1024)
259-
self.assertIsInstance(type_name, str)
260-
261-
# Validate declaration line matches the source code (according to d_original_example.c).
262-
if var_name == "argc":
263-
self.assertEqual(type_name, "int", "argc should be type 'int'")
264-
elif var_name == "argv":
265-
self.assertEqual(type_name, "char **", "argv should be type 'char **'")
266-
elif var_name == "i":
267-
self.assertEqual(type_name, "int", "i should be type 'int'")
268-
269-
if self.TraceOn():
270-
# Only print if something happened (location changed or variable appeared/disappeared).
271-
if should_print or len(current_locations) != len(prev_locations):
272-
print(f"\nInstruction {i} at {inst.GetAddress()}: {annotations.GetSize()} annotations")
273-
for var_name, location in current_locations.items():
274-
change_marker = " <- CHANGED" if var_name in prev_locations and prev_locations[var_name] != location else ""
275-
new_marker = " <- NEW" if var_name not in prev_locations else ""
276-
print(f" {var_name} = {location}{change_marker}{new_marker}")
277-
# Check for disappeared variables.
278-
for var_name in prev_locations:
279-
if var_name not in current_locations:
280-
print(f" {var_name} <- GONE")
281-
282-
# Update tracking.
283-
prev_locations = current_locations.copy()
284-
285-
self.assertTrue(found_annotations,
286-
"Should find at least one instruction with variable annotations")
287-
288-
self.assertGreater(len(found_variables), 0,
289-
"Should find at least one variable")
290-
291198
if self.TraceOn():
292199
print(f"\nTest complete. Found variables: {found_variables}")

0 commit comments

Comments
 (0)