qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: "Philippe Mathieu-Daudé" <philmd@linaro.org>
To: Markus Armbruster <armbru@redhat.com>
Cc: qemu-devel@nongnu.org, Pavel Dovgalyuk <pavel.dovgaluk@ispras.ru>,
	Paolo Bonzini <pbonzini@redhat.com>,
	Michael Roth <michael.roth@amd.com>,
	"Dr. David Alan Gilbert" <dgilbert@redhat.com>,
	Stefan Berger <stefanb@linux.vnet.ibm.com>,
	Gerd Hoffmann <kraxel@redhat.com>,
	Juan Quintela <quintela@redhat.com>,
	Eric Blake <eblake@redhat.com>,
	Richard Henderson <richard.henderson@linaro.org>
Subject: Re: [PATCH 2/2] qapi: Generate enum count as definition in gen_enum_lookup()
Date: Wed, 15 Mar 2023 11:32:14 +0100	[thread overview]
Message-ID: <1f129089-6793-2e96-e6dd-f94bf66f3cde@linaro.org> (raw)
In-Reply-To: <87ilfnqmnc.fsf@pond.sub.org>

On 27/2/23 14:10, Markus Armbruster wrote:
> Philippe Mathieu-Daudé <philmd@linaro.org> writes:
> 
>> QAPI's gen_enum() generates QAPI enum values and the number
>> of this values (as foo__MAX).
>> The number of entries in an enum type is not part of the
>> enumerated values, but we generate it as such. See for
>> example:
>>
>>    typedef enum OnOffAuto {
>>        ON_OFF_AUTO_AUTO,
>>        ON_OFF_AUTO_ON,
>>        ON_OFF_AUTO_OFF,
>>        ON_OFF_AUTO__MAX,        <---------
>>    } OnOffAuto;
>>
>> Instead of declaring the enum count as the last enumerated
>> value, #define it, so it is not part of the enum. The previous
>> example becomes:
>>
>>    typedef enum OnOffAuto {
>>        ON_OFF_AUTO_AUTO,
>>        ON_OFF_AUTO_ON,
>>        ON_OFF_AUTO_OFF,
>>    #define ON_OFF_AUTO__MAX 3   <---------
>>    } OnOffAuto;
> 
> I'm in favour.  In fact, I've wanted to do this for a while, just never
> got around to it.
> 
>> Since Clang enables the -Wswitch warning by default [*], remove all
>> pointless foo__MAX cases in switch statement, in order to avoid:
>>
>>   audio/audio.c:2231:10: error: case value not in enumerated type 'AudioFormat' (aka 'enum AudioFormat') [-Wswitch]
>>      case AUDIO_FORMAT__MAX:
>>           ^
>>   ui/input.c:233:14: error: case value not in enumerated type 'KeyValueKind' (aka 'enum KeyValueKind') [-Wswitch]
>>          case KEY_VALUE_KIND__MAX:
>>               ^
>>   ...
>>
>> [*] https://clang.llvm.org/docs/DiagnosticsReference.html#wswitch
>> Signed-off-by: Philippe Mathieu-Daudé <philmd@linaro.org>
> 
> Falls apart when the enum is empty:
> 
>      gcc -m64 -mcx16 -Iqga/qemu-ga.p -Iqga -I../qga -I. -Iqapi -Itrace -Iui -Iui/shader -I/usr/include/glib-2.0 -I/usr/lib64/glib-2.0/include -I/usr/include/sysprof-4 -fdiagnostics-color=auto -Wall -Winvalid-pch -std=gnu11 -O0 -g -isystem /work/armbru/qemu/linux-headers -isystem linux-headers -iquote . -iquote /work/armbru/qemu -iquote /work/armbru/qemu/include -iquote /work/armbru/qemu/tcg/i386 -pthread -D_GNU_SOURCE -D_FILE_OFFSET_BITS=64 -D_LARGEFILE_SOURCE -fno-strict-aliasing -fno-common -fwrapv -Wundef -Wwrite-strings -Wmissing-prototypes -Wstrict-prototypes -Wredundant-decls -Wold-style-declaration -Wold-style-definition -Wtype-limits -Wformat-security -Wformat-y2k -Winit-self -Wignored-qualifiers -Wempty-body -Wnested-externs -Wendif-labels -Wexpansion-to-defined -Wimplicit-fallthrough=2 -Wmissing-format-attribute -Wno-missing-include-dirs -Wno-shift-negative-value -Wno-psabi -fstack-protector-strong -fPIE -MD -MQ qga/qemu-ga.p/meson-generated_.._qga-qapi-emit-events.c.o -MF qga/qemu-ga.p/meson-generated_.._qga-qapi-emit-events.c.o.d -o qga/qemu-ga.p/meson-generated_.._qga-qapi-emit-events.c.o -c qga/qga-qapi-emit-events.c
>      In file included from qga/qga-qapi-emit-events.c:14:
>      qga/qga-qapi-emit-events.h:20:1: error: empty enum is invalid
>         20 | } qga_QAPIEvent;
>            | ^

While I could find that in the C++ standard, I couldn't in the C one.

I wonder if in that case QAPI should generate foo__MAX = 0. No code /
structure should use an enum type which doesn't contain any member...

      reply	other threads:[~2023-03-15 10:33 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-02-24 15:54 [PATCH 0/2] qapi: Simplify enum generation Philippe Mathieu-Daudé
2023-02-24 15:54 ` [PATCH 1/2] qapi: Do not generate default switch case in gen_visit_object_members() Philippe Mathieu-Daudé
2023-02-27 12:40   ` Juan Quintela
2023-02-27 13:14   ` Markus Armbruster
2023-02-24 15:54 ` [PATCH 2/2] qapi: Generate enum count as definition in gen_enum_lookup() Philippe Mathieu-Daudé
2023-02-24 21:16   ` Richard Henderson
2023-02-27 12:41   ` Juan Quintela
2023-02-27 13:10   ` Markus Armbruster
2023-03-15 10:32     ` Philippe Mathieu-Daudé [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1f129089-6793-2e96-e6dd-f94bf66f3cde@linaro.org \
    --to=philmd@linaro.org \
    --cc=armbru@redhat.com \
    --cc=dgilbert@redhat.com \
    --cc=eblake@redhat.com \
    --cc=kraxel@redhat.com \
    --cc=michael.roth@amd.com \
    --cc=pavel.dovgaluk@ispras.ru \
    --cc=pbonzini@redhat.com \
    --cc=qemu-devel@nongnu.org \
    --cc=quintela@redhat.com \
    --cc=richard.henderson@linaro.org \
    --cc=stefanb@linux.vnet.ibm.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).