Skip to content

Commit

Permalink
error: Eliminate error_propagate() with Coccinelle, part 1
Browse files Browse the repository at this point in the history
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 <[email protected]>
Reviewed-by: Eric Blake <[email protected]>
Message-Id: <[email protected]>
  • Loading branch information
Markus Armbruster committed Jul 10, 2020
1 parent dcfe480 commit 668f62e
Show file tree
Hide file tree
Showing 114 changed files with 383 additions and 896 deletions.
4 changes: 1 addition & 3 deletions accel/kvm/kvm-all.c
Original file line number Diff line number Diff line change
Expand Up @@ -3113,11 +3113,9 @@ static void kvm_set_kvm_shadow_mem(Object *obj, Visitor *v,
Error **errp)
{
KVMState *s = KVM_STATE(obj);
Error *error = NULL;
int64_t value;

if (!visit_type_int(v, name, &value, &error)) {
error_propagate(errp, error);
if (!visit_type_int(v, name, &value, errp)) {
return;
}

Expand Down
4 changes: 1 addition & 3 deletions accel/tcg/tcg-all.c
Original file line number Diff line number Diff line change
Expand Up @@ -182,11 +182,9 @@ static void tcg_set_tb_size(Object *obj, Visitor *v,
Error **errp)
{
TCGState *s = TCG_STATE(obj);
Error *error = NULL;
uint32_t value;

if (!visit_type_uint32(v, name, &value, &error)) {
error_propagate(errp, error);
if (!visit_type_uint32(v, name, &value, errp)) {
return;
}

Expand Down
3 changes: 1 addition & 2 deletions backends/cryptodev-vhost-user.c
Original file line number Diff line number Diff line change
Expand Up @@ -209,8 +209,7 @@ static void cryptodev_vhost_user_init(
backend->conf.peers.ccs[i] = cc;

if (i == 0) {
if (!qemu_chr_fe_init(&s->chr, chr, &local_err)) {
error_propagate(errp, local_err);
if (!qemu_chr_fe_init(&s->chr, chr, errp)) {
return;
}
}
Expand Down
4 changes: 1 addition & 3 deletions backends/cryptodev.c
Original file line number Diff line number Diff line change
Expand Up @@ -154,11 +154,9 @@ cryptodev_backend_set_queues(Object *obj, Visitor *v, const char *name,
void *opaque, Error **errp)
{
CryptoDevBackend *backend = CRYPTODEV_BACKEND(obj);
Error *local_err = NULL;
uint32_t value;

if (!visit_type_uint32(v, name, &value, &local_err)) {
error_propagate(errp, local_err);
if (!visit_type_uint32(v, name, &value, errp)) {
return;
}
if (!value) {
Expand Down
4 changes: 1 addition & 3 deletions backends/hostmem-file.c
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,6 @@ static void file_memory_backend_set_align(Object *o, Visitor *v,
{
HostMemoryBackend *backend = MEMORY_BACKEND(o);
HostMemoryBackendFile *fb = MEMORY_BACKEND_FILE(o);
Error *local_err = NULL;
uint64_t val;

if (host_memory_backend_mr_inited(backend)) {
Expand All @@ -119,8 +118,7 @@ static void file_memory_backend_set_align(Object *o, Visitor *v,
return;
}

if (!visit_type_size(v, name, &val, &local_err)) {
error_propagate(errp, local_err);
if (!visit_type_size(v, name, &val, errp)) {
return;
}
fb->align = val;
Expand Down
4 changes: 1 addition & 3 deletions backends/hostmem-memfd.c
Original file line number Diff line number Diff line change
Expand Up @@ -77,16 +77,14 @@ memfd_backend_set_hugetlbsize(Object *obj, Visitor *v, const char *name,
void *opaque, Error **errp)
{
HostMemoryBackendMemfd *m = MEMORY_BACKEND_MEMFD(obj);
Error *local_err = NULL;
uint64_t value;

if (host_memory_backend_mr_inited(MEMORY_BACKEND(obj))) {
error_setg(errp, "cannot change property value");
return;
}

if (!visit_type_size(v, name, &value, &local_err)) {
error_propagate(errp, local_err);
if (!visit_type_size(v, name, &value, errp)) {
return;
}
if (!value) {
Expand Down
8 changes: 2 additions & 6 deletions backends/hostmem.c
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ host_memory_backend_set_size(Object *obj, Visitor *v, const char *name,
void *opaque, Error **errp)
{
HostMemoryBackend *backend = MEMORY_BACKEND(obj);
Error *local_err = NULL;
uint64_t value;

if (host_memory_backend_mr_inited(backend)) {
Expand All @@ -63,8 +62,7 @@ host_memory_backend_set_size(Object *obj, Visitor *v, const char *name,
return;
}

if (!visit_type_size(v, name, &value, &local_err)) {
error_propagate(errp, local_err);
if (!visit_type_size(v, name, &value, errp)) {
return;
}
if (!value) {
Expand Down Expand Up @@ -252,11 +250,9 @@ static void host_memory_backend_set_prealloc_threads(Object *obj, Visitor *v,
const char *name, void *opaque, Error **errp)
{
HostMemoryBackend *backend = MEMORY_BACKEND(obj);
Error *local_err = NULL;
uint32_t value;

if (!visit_type_uint32(v, name, &value, &local_err)) {
error_propagate(errp, local_err);
if (!visit_type_uint32(v, name, &value, errp)) {
return;
}
if (value <= 0) {
Expand Down
4 changes: 1 addition & 3 deletions backends/tpm/tpm_util.c
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ static void set_tpm(Object *obj, Visitor *v, const char *name, void *opaque,
Error **errp)
{
DeviceState *dev = DEVICE(obj);
Error *local_err = NULL;
Property *prop = opaque;
TPMBackend *s, **be = qdev_get_prop_ptr(dev, prop);
char *str;
Expand All @@ -58,8 +57,7 @@ static void set_tpm(Object *obj, Visitor *v, const char *name, void *opaque,
return;
}

if (!visit_type_str(v, name, &str, &local_err)) {
error_propagate(errp, local_err);
if (!visit_type_str(v, name, &str, errp)) {
return;
}

Expand Down
3 changes: 1 addition & 2 deletions block.c
Original file line number Diff line number Diff line change
Expand Up @@ -5663,10 +5663,9 @@ static void coroutine_fn bdrv_co_invalidate_cache(BlockDriverState *bs,
if (bs->open_flags & BDRV_O_INACTIVE) {
bs->open_flags &= ~BDRV_O_INACTIVE;
bdrv_get_cumulative_perm(bs, &perm, &shared_perm);
ret = bdrv_check_perm(bs, NULL, perm, shared_perm, NULL, NULL, &local_err);
ret = bdrv_check_perm(bs, NULL, perm, shared_perm, NULL, NULL, errp);
if (ret < 0) {
bs->open_flags |= BDRV_O_INACTIVE;
error_propagate(errp, local_err);
return;
}
bdrv_set_perm(bs, perm, shared_perm);
Expand Down
4 changes: 1 addition & 3 deletions block/curl.c
Original file line number Diff line number Diff line change
Expand Up @@ -669,7 +669,6 @@ static int curl_open(BlockDriverState *bs, QDict *options, int flags,
BDRVCURLState *s = bs->opaque;
CURLState *state = NULL;
QemuOpts *opts;
Error *local_err = NULL;
const char *file;
const char *cookie;
const char *cookie_secret;
Expand All @@ -695,8 +694,7 @@ static int curl_open(BlockDriverState *bs, QDict *options, int flags,

qemu_mutex_init(&s->mutex);
opts = qemu_opts_create(&runtime_opts, NULL, 0, &error_abort);
if (!qemu_opts_absorb_qdict(opts, options, &local_err)) {
error_propagate(errp, local_err);
if (!qemu_opts_absorb_qdict(opts, options, errp)) {
goto out_noclean;
}

Expand Down
8 changes: 2 additions & 6 deletions block/file-posix.c
Original file line number Diff line number Diff line change
Expand Up @@ -3331,7 +3331,6 @@ static int hdev_open(BlockDriverState *bs, QDict *options, int flags,
Error **errp)
{
BDRVRawState *s = bs->opaque;
Error *local_err = NULL;
int ret;

#if defined(__APPLE__) && defined(__MACH__)
Expand Down Expand Up @@ -3396,9 +3395,8 @@ static int hdev_open(BlockDriverState *bs, QDict *options, int flags,

s->type = FTYPE_FILE;

ret = raw_open_common(bs, options, flags, 0, true, &local_err);
ret = raw_open_common(bs, options, flags, 0, true, errp);
if (ret < 0) {
error_propagate(errp, local_err);
#if defined(__APPLE__) && defined(__MACH__)
if (*bsd_path) {
filename = bsd_path;
Expand Down Expand Up @@ -3674,14 +3672,12 @@ static int cdrom_open(BlockDriverState *bs, QDict *options, int flags,
Error **errp)
{
BDRVRawState *s = bs->opaque;
Error *local_err = NULL;
int ret;

s->type = FTYPE_CD;

ret = raw_open_common(bs, options, flags, 0, true, &local_err);
ret = raw_open_common(bs, options, flags, 0, true, errp);
if (ret) {
error_propagate(errp, local_err);
return ret;
}

Expand Down
3 changes: 1 addition & 2 deletions block/parallels.c
Original file line number Diff line number Diff line change
Expand Up @@ -646,9 +646,8 @@ static int coroutine_fn parallels_co_create_opts(BlockDriver *drv,
}

/* Create and open the file (protocol layer) */
ret = bdrv_create_file(filename, opts, &local_err);
ret = bdrv_create_file(filename, opts, errp);
if (ret < 0) {
error_propagate(errp, local_err);
goto done;
}

Expand Down
3 changes: 1 addition & 2 deletions block/qcow.c
Original file line number Diff line number Diff line change
Expand Up @@ -973,9 +973,8 @@ static int coroutine_fn qcow_co_create_opts(BlockDriver *drv,
}

/* Create and open the file (protocol layer) */
ret = bdrv_create_file(filename, opts, &local_err);
ret = bdrv_create_file(filename, opts, errp);
if (ret < 0) {
error_propagate(errp, local_err);
goto fail;
}

Expand Down
3 changes: 1 addition & 2 deletions block/qed.c
Original file line number Diff line number Diff line change
Expand Up @@ -749,9 +749,8 @@ static int coroutine_fn bdrv_qed_co_create_opts(BlockDriver *drv,
}

/* Create and open the file (protocol layer) */
ret = bdrv_create_file(filename, opts, &local_err);
ret = bdrv_create_file(filename, opts, errp);
if (ret < 0) {
error_propagate(errp, local_err);
goto fail;
}

Expand Down
4 changes: 1 addition & 3 deletions block/throttle-groups.c
Original file line number Diff line number Diff line change
Expand Up @@ -811,7 +811,6 @@ static void throttle_group_set(Object *obj, Visitor *v, const char * name,
ThrottleGroup *tg = THROTTLE_GROUP(obj);
ThrottleConfig *cfg;
ThrottleParamInfo *info = opaque;
Error *local_err = NULL;
int64_t value;

/* If we have finished initialization, don't accept individual property
Expand All @@ -823,8 +822,7 @@ static void throttle_group_set(Object *obj, Visitor *v, const char * name,
return;
}

if (!visit_type_int64(v, name, &value, &local_err)) {
error_propagate(errp, local_err);
if (!visit_type_int64(v, name, &value, errp)) {
return;
}
if (value < 0) {
Expand Down
3 changes: 1 addition & 2 deletions block/vhdx.c
Original file line number Diff line number Diff line change
Expand Up @@ -2083,9 +2083,8 @@ static int coroutine_fn vhdx_co_create_opts(BlockDriver *drv,
}

/* Create and open the file (protocol layer) */
ret = bdrv_create_file(filename, opts, &local_err);
ret = bdrv_create_file(filename, opts, errp);
if (ret < 0) {
error_propagate(errp, local_err);
goto fail;
}

Expand Down
3 changes: 1 addition & 2 deletions block/vmdk.c
Original file line number Diff line number Diff line change
Expand Up @@ -2252,9 +2252,8 @@ static int vmdk_create_extent(const char *filename, int64_t filesize,
BlockBackend *blk = NULL;
Error *local_err = NULL;

ret = bdrv_create_file(filename, opts, &local_err);
ret = bdrv_create_file(filename, opts, errp);
if (ret < 0) {
error_propagate(errp, local_err);
goto exit;
}

Expand Down
3 changes: 1 addition & 2 deletions block/vpc.c
Original file line number Diff line number Diff line change
Expand Up @@ -1113,9 +1113,8 @@ static int coroutine_fn vpc_co_create_opts(BlockDriver *drv,
}

/* Create and open the file (protocol layer) */
ret = bdrv_create_file(filename, opts, &local_err);
ret = bdrv_create_file(filename, opts, errp);
if (ret < 0) {
error_propagate(errp, local_err);
goto fail;
}

Expand Down
6 changes: 2 additions & 4 deletions blockdev.c
Original file line number Diff line number Diff line change
Expand Up @@ -509,8 +509,7 @@ static BlockBackend *blockdev_init(const char *file, QDict *bs_opts,
goto err_no_opts;
}

if (!qemu_opts_absorb_qdict(opts, bs_opts, &error)) {
error_propagate(errp, error);
if (!qemu_opts_absorb_qdict(opts, bs_opts, errp)) {
goto early_err;
}

Expand Down Expand Up @@ -827,8 +826,7 @@ DriveInfo *drive_new(QemuOpts *all_opts, BlockInterfaceType block_default_type,

for (i = 0; i < ARRAY_SIZE(opt_renames); i++) {
if (!qemu_opt_rename(all_opts, opt_renames[i].from,
opt_renames[i].to, &local_err)) {
error_propagate(errp, local_err);
opt_renames[i].to, errp)) {
return NULL;
}
}
Expand Down
16 changes: 5 additions & 11 deletions hw/arm/allwinner-a10.c
Original file line number Diff line number Diff line change
Expand Up @@ -72,15 +72,12 @@ static void aw_a10_realize(DeviceState *dev, Error **errp)
{
AwA10State *s = AW_A10(dev);
SysBusDevice *sysbusdev;
Error *err = NULL;

if (!qdev_realize(DEVICE(&s->cpu), NULL, &err)) {
error_propagate(errp, err);
if (!qdev_realize(DEVICE(&s->cpu), NULL, errp)) {
return;
}

if (!sysbus_realize(SYS_BUS_DEVICE(&s->intc), &err)) {
error_propagate(errp, err);
if (!sysbus_realize(SYS_BUS_DEVICE(&s->intc), errp)) {
return;
}
sysbusdev = SYS_BUS_DEVICE(&s->intc);
Expand All @@ -91,8 +88,7 @@ static void aw_a10_realize(DeviceState *dev, Error **errp)
qdev_get_gpio_in(DEVICE(&s->cpu), ARM_CPU_FIQ));
qdev_pass_gpios(DEVICE(&s->intc), dev, NULL);

if (!sysbus_realize(SYS_BUS_DEVICE(&s->timer), &err)) {
error_propagate(errp, err);
if (!sysbus_realize(SYS_BUS_DEVICE(&s->timer), errp)) {
return;
}
sysbusdev = SYS_BUS_DEVICE(&s->timer);
Expand All @@ -114,16 +110,14 @@ static void aw_a10_realize(DeviceState *dev, Error **errp)
qemu_check_nic_model(&nd_table[0], TYPE_AW_EMAC);
qdev_set_nic_properties(DEVICE(&s->emac), &nd_table[0]);
}
if (!sysbus_realize(SYS_BUS_DEVICE(&s->emac), &err)) {
error_propagate(errp, err);
if (!sysbus_realize(SYS_BUS_DEVICE(&s->emac), errp)) {
return;
}
sysbusdev = SYS_BUS_DEVICE(&s->emac);
sysbus_mmio_map(sysbusdev, 0, AW_A10_EMAC_BASE);
sysbus_connect_irq(sysbusdev, 0, qdev_get_gpio_in(dev, 55));

if (!sysbus_realize(SYS_BUS_DEVICE(&s->sata), &err)) {
error_propagate(errp, err);
if (!sysbus_realize(SYS_BUS_DEVICE(&s->sata), errp)) {
return;
}
sysbus_mmio_map(SYS_BUS_DEVICE(&s->sata), 0, AW_A10_SATA_BASE);
Expand Down
Loading

0 comments on commit 668f62e

Please sign in to comment.