Skip to content

Diagnostic

diagnostic

Diagnostic dataclass

Source code in xdsl/utils/diagnostic.py
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
@dataclass
class Diagnostic:
    op_messages: dict[Operation, list[str]] = field(
        default_factory=dict[Operation, list[str]]
    )

    def add_message(self, op: Operation, message: str) -> None:
        """Add a message to an operation."""
        self.op_messages.setdefault(op, []).append(message)

    def raise_exception(self, ir: IRNode, underlying_error: Exception) -> NoReturn:
        """Raise an exception, that will also print all messages in the IR."""
        from xdsl.printer import Printer

        f = StringIO()
        p = Printer(stream=f, diagnostic=self, print_generic_format=True)
        toplevel = ir.get_toplevel_object()
        match toplevel:
            case Operation():
                p.print_op(toplevel)
                p.print_string("\n")
            case Block():
                p.print_block(toplevel)
                p.print_string("\n")
            case Region():
                p.print_region(toplevel)

        # __notes__ only in 3.11 and above
        if hasattr(underlying_error, "add_note"):
            # Use official API if present
            getattr(underlying_error, "add_note")(f.getvalue())
        else:
            # Add our own __notes__ if not
            if not hasattr(underlying_error, "__notes__"):
                notes: list[str] = []
                setattr(underlying_error, "__notes__", notes)
            else:
                notes = getattr(underlying_error, "__notes__")
            notes.append(f.getvalue())

        raise underlying_error

op_messages: dict[Operation, list[str]] = field(default_factory=(dict[Operation, list[str]])) class-attribute instance-attribute

__init__(op_messages: dict[Operation, list[str]] = dict[Operation, list[str]]()) -> None

add_message(op: Operation, message: str) -> None

Add a message to an operation.

Source code in xdsl/utils/diagnostic.py
16
17
18
def add_message(self, op: Operation, message: str) -> None:
    """Add a message to an operation."""
    self.op_messages.setdefault(op, []).append(message)

raise_exception(ir: IRNode, underlying_error: Exception) -> NoReturn

Raise an exception, that will also print all messages in the IR.

Source code in xdsl/utils/diagnostic.py
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
def raise_exception(self, ir: IRNode, underlying_error: Exception) -> NoReturn:
    """Raise an exception, that will also print all messages in the IR."""
    from xdsl.printer import Printer

    f = StringIO()
    p = Printer(stream=f, diagnostic=self, print_generic_format=True)
    toplevel = ir.get_toplevel_object()
    match toplevel:
        case Operation():
            p.print_op(toplevel)
            p.print_string("\n")
        case Block():
            p.print_block(toplevel)
            p.print_string("\n")
        case Region():
            p.print_region(toplevel)

    # __notes__ only in 3.11 and above
    if hasattr(underlying_error, "add_note"):
        # Use official API if present
        getattr(underlying_error, "add_note")(f.getvalue())
    else:
        # Add our own __notes__ if not
        if not hasattr(underlying_error, "__notes__"):
            notes: list[str] = []
            setattr(underlying_error, "__notes__", notes)
        else:
            notes = getattr(underlying_error, "__notes__")
        notes.append(f.getvalue())

    raise underlying_error