summaryrefslogtreecommitdiff
path: root/python/languagerepresentation.py
diff options
context:
space:
mode:
authorGlenn Smith <glenn@vector35.com>2025-05-16 16:24:46 -0400
committerGlenn Smith <glenn@vector35.com>2025-05-16 16:37:00 -0400
commit64706bbd526c22e38b0be34f5d2b5a75766a6c02 (patch)
tree4b48507f26b3eb1c2af318018bc0adfb5cf400ac /python/languagerepresentation.py
parentc0ef307ab4241257ec86ece0351bf1604cb8f232 (diff)
[Python] Support HLIL collapsing and update PseudoPython
Fixes Vector35/binaryninja-api#6679
Diffstat (limited to 'python/languagerepresentation.py')
-rw-r--r--python/languagerepresentation.py56
1 files changed, 55 insertions, 1 deletions
diff --git a/python/languagerepresentation.py b/python/languagerepresentation.py
index 51c14da0..44c34bf2 100644
--- a/python/languagerepresentation.py
+++ b/python/languagerepresentation.py
@@ -35,7 +35,7 @@ from . import variable
from . import types
from .log import log_error
from .enums import BraceRequirement, HighlightStandardColor, InstructionTextTokenType, OperatorPrecedence, ScopeType, \
- SymbolDisplayType, SymbolDisplayResult
+ SymbolDisplayType, SymbolDisplayResult, InstructionTextTokenContext
class HighLevelILTokenEmitter:
@@ -92,6 +92,60 @@ class HighLevelILTokenEmitter:
"""Forces there to be no indentation for the next line."""
core.BNHighLevelILTokenEmitterNoIndentForThisLine(self.handle)
+ def prepend_blank_collapse_indicator(self):
+ """
+ Insert, at the beginning of the line, a collapse indicator token.
+ The indicator will be a blank space and not have any functionality.
+ """
+ core.BNHighLevelILTokenPrependCollapseBlankIndicator(self.handle);
+
+ def prepend_instr_collapse_indicator(
+ self,
+ function_: 'function.Function',
+ instr: 'highlevelil.HighLevelILInstruction',
+ discriminator: int = 0
+ ):
+ """
+ Insert, at the beginning of the line, a collapse indicator token.
+
+ The indicator will allow the user to collapse the region specified by
+ (instr, discriminator) on the function.
+ Implementations can use :py:func:`Function.is_instruction_collapsed` and
+ :py:func:`Function.is_region_collapsed` to account for collapsed regions in rendering.
+
+ :param function_: Function whose instructions are being emitted
+ :param instr: Instruction being emitted which can be collapsed
+ :param discriminator: Unique discriminator id for the region
+ """
+ if not self.has_collapsable_regions:
+ return
+
+ # Insert the collapse indicator at the beginning of the line if one isn't already there or the
+ # one that is there is empty
+ context = InstructionTextTokenContext.ContentCollapsiblePadding
+ if instr.can_collapse:
+ if function_ and function_.is_instruction_collapsed(instr, discriminator):
+ context = InstructionTextTokenContext.ContentCollapsedContext
+ else:
+ context = InstructionTextTokenContext.ContentExpandedContext
+ self.prepend_region_collapse_indicator(context, instr.get_instruction_hash(discriminator))
+
+ def prepend_region_collapse_indicator(
+ self,
+ context: InstructionTextTokenContext,
+ hash: int
+ ):
+ core.BNHighLevelILTokenPrependCollapseIndicator(self.handle, context, hash)
+
+ @property
+ def has_collapsable_regions(self) -> bool:
+ """If the emitter can emit regions which can be collapsed"""
+ return core.BNHighLevelILTokenEmitterHasCollapsableRegions(self.handle)
+
+ @has_collapsable_regions.setter
+ def has_collapsable_regions(self, value: bool):
+ core.BNHighLevelILTokenEmitterSetHasCollapsableRegions(self.handle, value)
+
class ZeroConfidenceContext:
"""
``class ZeroConfidenceContext`` is a context manager that optionally forces tokens to be of zero confidence