qemu-block
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [PATCH v3] osdep: Make MIN/MAX evaluate arguments only once


From: Eric Blake
Subject: Re: [PATCH v3] osdep: Make MIN/MAX evaluate arguments only once
Date: Wed, 3 Jun 2020 14:26:46 -0500
User-agent: Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101 Thunderbird/68.8.0

On 6/3/20 11:32 AM, Richard Henderson wrote:

I'd prefer we generate a compile-time error than a runtime trap (or nothing,
depending on compiler flags controlling __builtin_unreachable).

What we have DOES produce a compile-time error.  If either expression to
MIN_CONST() is not actually const, the fact that __builtin_unreachable()
returns void causes a compilation failure because a value is expected.

Ah!  Well, that's good and certainly sufficient for my needs.

I do now wonder if it wouldn't be clearer to use "(void)0"
instead of __builtin_unreachable, and add a note to the comment just above.

Yes, I just tested; using "((void)0)" in place of "__builtin_unreachable()" has the same effect (no change to valid use, and still a compiler error on misuse). gcc:

/home/eblake/qemu/qemu-img.c: In function ‘is_allocated_sectors’:
/home/eblake/qemu/qemu-img.c:1225:15: error: void value not ignored as it ought to be
 1225 |             i = MIN_CONST(i, n);
      |               ^

clang:

/home/eblake/qemu/qemu-img.c:1225:15: error: assigning to 'int' from incompatible type 'void'
            i = MIN_CONST(i, n);
              ^ ~~~~~~~~~~~~~~~


Of course, a comment explaining the intent can't hurt either. I'll wait to see if this gathers any other comments before spinning a v4 with that change.

--
Eric Blake, Principal Software Engineer
Red Hat, Inc.           +1-919-301-3226
Virtualization:  qemu.org | libvirt.org




reply via email to

[Prev in Thread] Current Thread [Next in Thread]