[libvirt] [PATCH v2 2/7] internal: introduce macro helpers to check flag requirements

Jeff Nelson jenelson at redhat.com
Fri Mar 27 16:13:07 UTC 2015


On Fri, Mar 27, 2015 at 11:01:23AM +0100, Pavel Hrdina wrote:
> Similar to VIR_EXLUSIVE_FLAGS, it will error out if flag requirement is
> not met.
> 
> Signed-off-by: Pavel Hrdina <phrdina at redhat.com>
> ---
>  src/internal.h | 43 +++++++++++++++++++++++++++++++++++++++++++
>  1 file changed, 43 insertions(+)
> 
> diff --git a/src/internal.h b/src/internal.h
> index eb8d231..6363e58 100644
> --- a/src/internal.h
> +++ b/src/internal.h
> @@ -371,6 +371,49 @@
>          goto LABEL;                                                         \
>      }
>  
> +/* Macros to help dealing with flag requirements. */
> +
> +/**
> + * VIR_REQUIRE_FLAG_RET:
> + *
> + * @FLAG1: First flag to be checked.
> + * @FLAG2: Second flag that is required by first flag.
> + * @RET: Return value.
> + *
> + * Check whether required flag is set.  The checked flags are compared
> + * with flags variable.
> + *
> + * This helper does an early return and therefore it has to be called
> + * before anything that would require cleanup.
> + */
> +# define VIR_REQUIRE_FLAG_RET(FLAG1, FLAG2, RET)                            \
> +    if ((flags & FLAG1) && !(flags & FLAG2)) {                              \
> +        virReportInvalidArg(ctl,                                            \
> +                            _("Flag '%s' is required by flag '%s'"),        \
> +                            #FLAG2, #FLAG1);                                \
> +        return RET;                                                         \
> +    }
> +

I recommend encapsulating this within a "do { ... } while (1)"
statement. The problem is that the macro hides/exposes an -if-
statement to which an -else- keyword can be bound:

     if (something)
         VIR_REQUIRE_FLAG_RET(f1, f2, foo)
     else  // matches with -if- inside VIR_REQUIRE_FLAG_RET
         // doesn't execute when you think it does

Note that other macros (see definition of virCheckNonNullArgReturn
below) follow this practice.

> +/**
> + * VIR_REQUIRE_FLAG_GOTO:
> + *
> + * @FLAG1: First flag to be checked.
> + * @FLAG2: Second flag that is required by first flag.
> + * @LABEL: Label to jump to.
> + *
> + * Check whether required flag is set.  The checked flags are compared
> + * with flags variable.
> + *
> + * Returns nothing.  Jumps to a label if required flag is not set.
> + */
> +# define VIR_REQUIRE_FLAG_GOTO(FLAG1, FLAG2, LABEL)                         \
> +    if ((flags & FLAG1) && !(flags & FLAG2)) {                              \
> +        virReportInvalidArg(ctl,                                            \
> +                            _("Flag '%s' is required by flag '%s'"),        \
> +                            #FLAG2, #FLAG1);                                \
> +        goto LABEL;                                                         \
> +    }
> +

Same feedback as above.

-Jeff

>  # define virCheckNonNullArgReturn(argname, retval)  \
>      do {                                            \
>          if (argname == NULL) {                      \
> -- 
> 2.0.5
> 
> --
> libvir-list mailing list
> libvir-list at redhat.com
> https://www.redhat.com/mailman/listinfo/libvir-list




More information about the libvir-list mailing list