qemu-e2k/hw/char
Markus Armbruster 668f62ec62 error: Eliminate error_propagate() with Coccinelle, part 1
When all we do with an Error we receive into a local variable is
propagating to somewhere else, we can just as well receive it there
right away.  Convert

    if (!foo(..., &err)) {
        ...
        error_propagate(errp, err);
        ...
        return ...
    }

to

    if (!foo(..., errp)) {
        ...
        ...
        return ...
    }

where nothing else needs @err.  Coccinelle script:

    @rule1 forall@
    identifier fun, err, errp, lbl;
    expression list args, args2;
    binary operator op;
    constant c1, c2;
    symbol false;
    @@
         if (
    (
    -        fun(args, &err, args2)
    +        fun(args, errp, args2)
    |
    -        !fun(args, &err, args2)
    +        !fun(args, errp, args2)
    |
    -        fun(args, &err, args2) op c1
    +        fun(args, errp, args2) op c1
    )
            )
         {
             ... when != err
                 when != lbl:
                 when strict
    -        error_propagate(errp, err);
             ... when != err
    (
             return;
    |
             return c2;
    |
             return false;
    )
         }

    @rule2 forall@
    identifier fun, err, errp, lbl;
    expression list args, args2;
    expression var;
    binary operator op;
    constant c1, c2;
    symbol false;
    @@
    -    var = fun(args, &err, args2);
    +    var = fun(args, errp, args2);
         ... when != err
         if (
    (
             var
    |
             !var
    |
             var op c1
    )
            )
         {
             ... when != err
                 when != lbl:
                 when strict
    -        error_propagate(errp, err);
             ... when != err
    (
             return;
    |
             return c2;
    |
             return false;
    |
             return var;
    )
         }

    @depends on rule1 || rule2@
    identifier err;
    @@
    -    Error *err = NULL;
         ... when != err

Not exactly elegant, I'm afraid.

The "when != lbl:" is necessary to avoid transforming

         if (fun(args, &err)) {
             goto out
         }
         ...
     out:
         error_propagate(errp, err);

even though other paths to label out still need the error_propagate().
For an actual example, see sclp_realize().

Without the "when strict", Coccinelle transforms vfio_msix_setup(),
incorrectly.  I don't know what exactly "when strict" does, only that
it helps here.

The match of return is narrower than what I want, but I can't figure
out how to express "return where the operand doesn't use @err".  For
an example where it's too narrow, see vfio_intx_enable().

Silently fails to convert hw/arm/armsse.c, because Coccinelle gets
confused by ARMSSE being used both as typedef and function-like macro
there.  Converted manually.

Line breaks tidied up manually.  One nested declaration of @local_err
deleted manually.  Preexisting unwanted blank line dropped in
hw/riscv/sifive_e.c.

Signed-off-by: Markus Armbruster <armbru@redhat.com>
Reviewed-by: Eric Blake <eblake@redhat.com>
Message-Id: <20200707160613.848843-35-armbru@redhat.com>
2020-07-10 15:18:08 +02:00
..
bcm2835_aux.c qdev: set properties with device_class_set_props() 2020-01-24 20:59:15 +01:00
cadence_uart.c hw/char/cadence_uart: add clock support 2020-04-30 15:35:41 +01:00
cmsdk-apb-uart.c qdev: set properties with device_class_set_props() 2020-01-24 20:59:15 +01:00
debugcon.c qdev: set properties with device_class_set_props() 2020-01-24 20:59:15 +01:00
digic-uart.c qdev: set properties with device_class_set_props() 2020-01-24 20:59:15 +01:00
escc.c qdev: set properties with device_class_set_props() 2020-01-24 20:59:15 +01:00
etraxfs_ser.c qdev: set properties with device_class_set_props() 2020-01-24 20:59:15 +01:00
exynos4210_uart.c sysbus: Convert to sysbus_realize() etc. with Coccinelle 2020-06-15 22:05:28 +02:00
grlib_apbuart.c qdev: set properties with device_class_set_props() 2020-01-24 20:59:15 +01:00
ibex_uart.c ibex_uart: fix XOR-as-pow 2020-06-26 09:39:40 -04:00
imx_serial.c qdev: set properties with device_class_set_props() 2020-01-24 20:59:15 +01:00
ipoctal232.c qdev: set properties with device_class_set_props() 2020-01-24 20:59:15 +01:00
Kconfig hw/char: RX62N serial communication interface (SCI) 2020-06-22 18:37:12 +02:00
lm32_juart.c qdev: set properties with device_class_set_props() 2020-01-24 20:59:15 +01:00
lm32_uart.c qdev: set properties with device_class_set_props() 2020-01-24 20:59:15 +01:00
Makefile.objs hw/char: RX62N serial communication interface (SCI) 2020-06-22 18:37:12 +02:00
mcf_uart.c sysbus: Convert to sysbus_realize() etc. with Coccinelle 2020-06-15 22:05:28 +02:00
milkymist-uart.c qdev: set properties with device_class_set_props() 2020-01-24 20:59:15 +01:00
nrf51_uart.c qdev: set properties with device_class_set_props() 2020-01-24 20:59:15 +01:00
omap_uart.c
parallel-isa.c isa: Convert uses of isa_create() with Coccinelle 2020-06-15 22:05:28 +02:00
parallel.c acpi: move aml builder code for parallel device 2020-06-09 12:46:45 -04:00
pl011.c qdev: set properties with device_class_set_props() 2020-01-24 20:59:15 +01:00
renesas_sci.c hw/char: RX62N serial communication interface (SCI) 2020-06-22 18:37:12 +02:00
sclpconsole-lm.c misc: Replace zero-length arrays with flexible array member (manual) 2020-03-16 22:07:42 +01:00
sclpconsole.c misc: Replace zero-length arrays with flexible array member (manual) 2020-03-16 22:07:42 +01:00
serial-isa.c qdev: Convert bus-less devices to qdev_realize() with Coccinelle 2020-06-15 22:06:04 +02:00
serial-pci-multi.c error: Eliminate error_propagate() with Coccinelle, part 1 2020-07-10 15:18:08 +02:00
serial-pci.c error: Eliminate error_propagate() with Coccinelle, part 1 2020-07-10 15:18:08 +02:00
serial.c error: Eliminate error_propagate() with Coccinelle, part 1 2020-07-10 15:18:08 +02:00
sh_serial.c
spapr_vty.c qdev: Convert uses of qdev_create() with Coccinelle 2020-06-15 22:00:10 +02:00
stm32f2xx_usart.c qdev: set properties with device_class_set_props() 2020-01-24 20:59:15 +01:00
terminal3270.c qdev: set properties with device_class_set_props() 2020-01-24 20:59:15 +01:00
trace-events hw/char/cadence_uart: add clock support 2020-04-30 15:35:41 +01:00
virtio-console.c qdev: Unrealize must not fail 2020-05-15 07:08:14 +02:00
virtio-serial-bus.c qdev: Drop qbus_set_hotplug_handler() parameter @errp 2020-07-02 06:25:29 +02:00
xen_console.c
xilinx_uartlite.c hw/char/xilinx_uartlite: Replace hw_error() by qemu_log_mask() 2020-05-21 22:05:27 +01:00