Skip to content

Commit c07c10c

Browse files
committed
AST: add error handling to deserializer
1 parent b262a1f commit c07c10c

File tree

2 files changed

+30
-16
lines changed

2 files changed

+30
-16
lines changed

chb/ast/ASTDeserializer.py

Lines changed: 28 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
# ------------------------------------------------------------------------------
55
# The MIT License (MIT)
66
#
7-
# Copyright (c) 2022-2024 Aarno Labs LLC
7+
# Copyright (c) 2022-2025 Aarno Labs LLC
88
#
99
# Permission is hereby granted, free of charge, to any person obtaining a copy
1010
# of this software and associated documentation files (the "Software"), to deal
@@ -289,7 +289,7 @@ def _initialize_function_prototype(
289289
astree.set_function_prototype(fprototype)
290290
else:
291291
raise Exception(
292-
"Index for prototype "
292+
"Deserializer: Index for prototype "
293293
+ str(index)
294294
+ " not found in nodes deserialized")
295295

@@ -332,25 +332,23 @@ def _initialize_provenance(self, fdata: Dict[str, Any]) -> None:
332332
spans = fdata["spans"]
333333
rds = prov["reaching-definitions"]
334334

335-
def find_expr_by_id(nodes: Dict[int, AST.ASTNode], exprid: int) -> AST.ASTNode:
335+
def find_expr_by_id(
336+
nodes: Dict[int, AST.ASTNode], exprid: int) -> AST.ASTNode:
336337
for node in fdata["ast"]["nodes"]:
337338
if "exprid" in node and int(node["exprid"]) == exprid:
338339
return nodes[int(node["id"])]
339340
else:
340341
raise Exception(
341-
"Exprid: "
342-
+ str(exprid)
343-
+ " not found")
342+
"Deserializer: Exprid: " + str(exprid) + " not found")
344343

345-
def find_instr_by_id(nodes: Dict[int, AST.ASTNode], instrid: int) -> AST.ASTNode:
344+
def find_instr_by_id(
345+
nodes: Dict[int, AST.ASTNode], instrid: int) -> AST.ASTNode:
346346
for node in fdata["ast"]["nodes"]:
347347
if "instrid" in node and int(node["instrid"]) == instrid:
348348
return nodes[int(node["id"])]
349349
else:
350350
raise Exception(
351-
"Instrid: "
352-
+ str(instrid)
353-
+ " not found")
351+
"Deserializer: Instrid: " + str(instrid) + " not found")
354352

355353
def find_instr_address_by_locationid(locationid: int) -> str:
356354
for node in fdata["spans"]:
@@ -366,21 +364,37 @@ def find_expr_address_by_exprid(exprid: int) -> str:
366364
return node["spans"][0]["base_va"]
367365
else:
368366
raise Exception(
369-
"No span found for exprid: " + str(exprid))
367+
"Deserializer: No span found for exprid: " + str(exprid))
370368

371369
result: List[Tuple[str, List[str]]] = []
372370
for (exprid, instrids) in rds.items():
373371
exprnode = find_expr_by_id(nodes, int(exprid))
374372
p_instrs: List[str] = []
375373
for instrid in instrids:
376-
instrnode = cast(AST.ASTInstruction, find_instr_by_id(nodes, instrid))
377374
try:
378-
address = find_instr_address_by_locationid(instrnode.locationid)
375+
instrnode = cast(
376+
AST.ASTInstruction, find_instr_by_id(nodes, instrid))
377+
except Exception as e:
378+
raise Exception(
379+
"Deserializer: instrnode for instrid "
380+
+ str(instrid)
381+
+ " and exprid "
382+
+ str(exprid)
383+
+ " not found")
384+
try:
385+
address = find_instr_address_by_locationid(
386+
instrnode.locationid)
379387
except Exception as e:
380388
p_instrs.append(str(instrnode) + ": " + str(e))
381389
address = "?"
382390
p_instrs.append(
383-
" <" + str(instrid) + "> " + str(instrnode) + " (" + address + ")")
391+
" <"
392+
+ str(instrid)
393+
+ "> "
394+
+ str(instrnode)
395+
+ " ("
396+
+ address
397+
+ ")")
384398
result.append((str(exprnode), p_instrs))
385399
self._reachingdefinitions[faddr] = result
386400

chb/ast/ASTViewer.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
# ------------------------------------------------------------------------------
55
# The MIT License (MIT)
66
#
7-
# Copyright (c) 2023-2024 Aarno Labs LLC
7+
# Copyright (c) 2023-2025 Aarno Labs LLC
88
#
99
# Permission is hereby granted, free of charge, to any person obtaining a copy
1010
# of this software and associated documentation files (the "Software"), to deal
@@ -250,7 +250,7 @@ def instr_span(self, instr: AST.ASTInstruction) -> str:
250250
if locationid in self.astree.spanmap():
251251
span = self.astree.spanmap()[locationid]
252252
else:
253-
span = "?"
253+
span = "?(locationid:" + str(locationid) + ")"
254254
return "\\n" + span
255255

256256
def visit_assign_instr(self, instr: AST.ASTAssign) -> None:

0 commit comments

Comments
 (0)