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.

clew/isr.py:205

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 found

1 more lines, not shown

Activity · 14 steps
lines 221–221found = 0lines 222–222stack = [tree.root_node]lines 223–223◇ while stacklines 224–224node = stack.pop()lines 225–225stack.extend(node.children)lines 226–226◇ if node.type not in ("preproc_def", "preproc_function_def")lines 227–227continuelines 228–228value = node.child_by_field_name("value")lines 229–229◇ if value is Nonelines 230–230continuelines 231–231text = src[value.start_byte : value.end_byte].decode("utf-8", errors="replace")lines 236–236joined = "|".join(re.escape(name) for name in sorted(spellings))lines 237–237found += 1 if joined and re.search(rf"(?<![A-Za-z0-9_$:]) ?({joined})\s*\(", text) else 0lines 238–238return found

Requirements

REQ-DDB-SCHEMA-001

isr_definition

tuple[str, str] | None clew.isr.isr_definition(Any node, bytes src)

The interrupt-handler evidence a function definition carries, if any.

clew/isr.py:167

Show on the map →

Called by _walk_spawn_sites

Calls _declared_name, _takes_no_parameters

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

Activity · 17 steps
lines 175–175declarator = node.child_by_field_name("declarator")lines 176–176name = _declared_name(declarator, src)lines 177–177◇ if not name or name in NOT_AN_ISRlines 178–178return Nonelines 179–179header = src[node.start_byte : (declarator.start_byte if declarator else node.end_byte)]lines 180–180header_text = header.decode("utf-8", errors="replace")lines 181–181◇ if any(token in header_text for token in ISR_ATTRIBUTE_TOKENS)lines 182–182return (name, ISR_SOURCE_DECLARED)lines 183–183type_node = node.child_by_field_name("type")lines 184–188macro = ( src[type_node.start_byte : type_node.end_byte].decode("utf-8", errors="replace") if type_node else "" )lines 189–189◇ if macro in ISR_DEFINING_MACROSlines 190–190return (name, ISR_SOURCE_DECLARED)lines 191–191◇ if name in ISR_EXACT_NAMESlines 192–192return (name, ISR_SOURCE_NAMED)lines 193–193◇ if any(fnmatchcase(name, glob) for glob in ISR_NAME_GLOBS) and _takes_no_parameters(node, src)lines 194–194return (name, ISR_SOURCE_NAMED)lines 195–195return None

Requirements

REQ-DDB-SCHEMA-001