]> git.proxmox.com Git - mirror_qemu.git/commit
error: Eliminate error_propagate() with Coccinelle, part 1
authorMarkus Armbruster <armbru@redhat.com>
Tue, 7 Jul 2020 16:06:02 +0000 (18:06 +0200)
committerMarkus Armbruster <armbru@redhat.com>
Fri, 10 Jul 2020 13:18:08 +0000 (15:18 +0200)
commit668f62ec621e4e2919fb7d4caa5d805764c5852d
tree8cb4b78b9ab596b0eb573a0cae77ac1893456826
parentdcfe480544eef72d666cb1695624449e2c22da2d
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>
114 files changed:
accel/kvm/kvm-all.c
accel/tcg/tcg-all.c
backends/cryptodev-vhost-user.c
backends/cryptodev.c
backends/hostmem-file.c
backends/hostmem-memfd.c
backends/hostmem.c
backends/tpm/tpm_util.c
block.c
block/curl.c
block/file-posix.c
block/parallels.c
block/qcow.c
block/qed.c
block/throttle-groups.c
block/vhdx.c
block/vmdk.c
block/vpc.c
blockdev.c
hw/arm/allwinner-a10.c
hw/arm/armsse.c
hw/arm/armv7m.c
hw/arm/aspeed_ast2600.c
hw/arm/aspeed_soc.c
hw/arm/bcm2835_peripherals.c
hw/arm/bcm2836.c
hw/arm/digic.c
hw/arm/fsl-imx25.c
hw/arm/fsl-imx31.c
hw/arm/fsl-imx6.c
hw/arm/msf2-soc.c
hw/arm/nrf51_soc.c
hw/arm/stm32f205_soc.c
hw/arm/stm32f405_soc.c
hw/arm/xlnx-zynqmp.c
hw/block/fdc.c
hw/block/xen-block.c
hw/char/serial-pci-multi.c
hw/char/serial-pci.c
hw/char/serial.c
hw/core/machine.c
hw/core/qdev-properties-system.c
hw/core/qdev-properties.c
hw/cpu/a15mpcore.c
hw/cpu/a9mpcore.c
hw/cpu/arm11mpcore.c
hw/cpu/core.c
hw/cpu/realview_mpcore.c
hw/display/virtio-gpu-pci.c
hw/display/virtio-vga.c
hw/gpio/aspeed_gpio.c
hw/i386/pc.c
hw/intc/apic_common.c
hw/intc/armv7m_nvic.c
hw/intc/pnv_xive.c
hw/intc/realview_gic.c
hw/intc/spapr_xive.c
hw/intc/xics_kvm.c
hw/isa/piix4.c
hw/mem/nvdimm.c
hw/microblaze/xlnx-zynqmp-pmu.c
hw/mips/cps.c
hw/misc/aspeed_sdmc.c
hw/misc/macio/cuda.c
hw/misc/macio/macio.c
hw/misc/macio/pmu.c
hw/misc/pca9552.c
hw/misc/tmp105.c
hw/misc/tmp421.c
hw/pci-host/pnv_phb3.c
hw/pci-host/pnv_phb4.c
hw/pci-host/pnv_phb4_pec.c
hw/ppc/e500.c
hw/ppc/pnv.c
hw/ppc/pnv_core.c
hw/ppc/pnv_psi.c
hw/ppc/spapr_caps.c
hw/ppc/spapr_drc.c
hw/ppc/spapr_irq.c
hw/riscv/opentitan.c
hw/riscv/sifive_e.c
hw/riscv/sifive_u.c
hw/s390x/css.c
hw/s390x/event-facility.c
hw/s390x/virtio-ccw-crypto.c
hw/s390x/virtio-ccw-rng.c
hw/scsi/scsi-bus.c
hw/sd/aspeed_sdhci.c
hw/smbios/smbios.c
hw/vfio/pci-quirks.c
hw/vfio/pci.c
hw/virtio/virtio-balloon.c
hw/virtio/virtio-rng-pci.c
hw/virtio/virtio-rng.c
iothread.c
net/colo-compare.c
net/dump.c
net/filter-buffer.c
net/tap.c
qapi/string-input-visitor.c
qdev-monitor.c
qga/commands-win32.c
qom/object.c
softmmu/vl.c
target/arm/cpu64.c
target/arm/monitor.c
target/i386/cpu.c
target/ppc/compat.c
target/s390x/cpu_models.c
target/sparc/cpu.c
tpm.c
util/main-loop.c
util/qemu-config.c
util/qemu-option.c