isr.py¶
clew/isr.py
Interrupt handlers recognised at their DEFINITION rather than at a registration call.
count_macro_body_registrations
int clew.isr.count_macro_body_registrations(Any tree, bytes src, frozenset[str] spellings)
Count interrupt registrations written INSIDE a macro body.
Source · 34 lines
def count_macro_body_registrations(tree: Any, src: bytes, spellings: frozenset[str]) -> int:
"""THE ONE NUMBER THAT MAKES A ZERO HONEST. tree-sitter exposes a `#define` body as opaque
text, so a registration written inside one is not a call node and no call-site matcher can
see it — and Zephyr writes 718 of its 1110 `IRQ_CONNECT` sites that way (64.7%; 69.0% under
drivers/), because a driver instantiates itself once per devicetree node through
`DT_INST_FOREACH_STATUS_OKAY`.
Reporting `0 interrupt handlers` on such a tree would be a measured-looking claim about a
tree the layer could not read. This counts what it could not read, by TEXT, and claims
nothing more: no handler name is recovered, because 46 of those sites name a macro
parameter or a token-pasted identifier that exists only after preprocessing.
@brief Count registrations hidden in macro bodies.
@return The count.
@version 1
"""
found = 0
stack = [tree.root_node]
while stack:
node = stack.pop()
stack.extend(node.children)
if node.type not in ("preproc_def", "preproc_function_def"):
continue
value = node.child_by_field_name("value")
if value is None:
continue
text = src[value.start_byte : value.end_byte].decode("utf-8", errors="replace")
## AN IDENTIFIER BOUNDARY, and ONE count per macro body. Matching `f"{name}("` as a
## bare substring counts `devm_request_irq(` once for itself and again for the
## `request_irq(` inside it, and counts a body holding several spellings several times —
## inflating a number whose whole purpose is to be trusted as "what I could not read".
joined = "|".join(re.escape(name) for name in sorted(spellings))
found += 1 if joined and re.search(rf"(?<![A-Za-z0-9_$:]) ?({joined})\s*\(", text) else 0
return found1 more lines, not shown
Activity · 14 steps
Requirements
isr_definition
tuple[str, str] | None clew.isr.isr_definition(Any node, bytes src)
The interrupt-handler evidence a function definition carries, if any.
Source · 31 lines
def isr_definition(node: Any, src: bytes) -> tuple[str, str] | None:
"""Ordered by strength: the attribute and the defining macro are read off the source, the
name conventions are an inference and are labelled as one.
@brief Classify one function definition as an interrupt handler.
@return (entry, source) or None.
@version 1
"""
declarator = node.child_by_field_name("declarator")
name = _declared_name(declarator, src)
if not name or name in NOT_AN_ISR:
return None
header = src[node.start_byte : (declarator.start_byte if declarator else node.end_byte)]
header_text = header.decode("utf-8", errors="replace")
if any(token in header_text for token in ISR_ATTRIBUTE_TOKENS):
return (name, ISR_SOURCE_DECLARED)
type_node = node.child_by_field_name("type")
macro = (
src[type_node.start_byte : type_node.end_byte].decode("utf-8", errors="replace")
if type_node
else ""
)
if macro in ISR_DEFINING_MACROS:
return (name, ISR_SOURCE_DECLARED)
if name in ISR_EXACT_NAMES:
return (name, ISR_SOURCE_NAMED)
if any(fnmatchcase(name, glob) for glob in ISR_NAME_GLOBS) and _takes_no_parameters(node, src):
return (name, ISR_SOURCE_NAMED)
return None