From: Markus Armbruster <armbru@redhat.com>
To: Eric Blake <eblake@redhat.com>
Cc: "Daniel P. Berrangé" <berrange@redhat.com>,
"Philippe Mathieu-Daudé" <philmd@linaro.org>,
qemu-devel@nongnu.org,
"Pavel Dovgalyuk" <pavel.dovgaluk@ispras.ru>,
"Dr. David Alan Gilbert" <dgilbert@redhat.com>,
"Michael Roth" <michael.roth@amd.com>,
"Stefan Berger" <stefanb@linux.vnet.ibm.com>,
"Paolo Bonzini" <pbonzini@redhat.com>,
"Marc-André Lureau" <marcandre.lureau@redhat.com>,
"Juan Quintela" <quintela@redhat.com>,
"Gerd Hoffmann" <kraxel@redhat.com>
Subject: Re: [PATCH v3 2/3] qapi: Do not generate empty enum
Date: Wed, 22 Mar 2023 06:45:39 +0100 [thread overview]
Message-ID: <87sfdx9w58.fsf@pond.sub.org> (raw)
In-Reply-To: <20230321214327.xgxyvm2iw7pkiogz@redhat.com> (Eric Blake's message of "Tue, 21 Mar 2023 16:43:27 -0500")
Eric Blake <eblake@redhat.com> writes:
> On Tue, Mar 21, 2023 at 03:19:28PM +0000, Daniel P. Berrangé wrote:
>> On Tue, Mar 21, 2023 at 03:31:56PM +0100, Philippe Mathieu-Daudé wrote:
>> > On 16/3/23 15:57, Markus Armbruster wrote:
>> > > Daniel P. Berrangé <berrange@redhat.com> writes:
>> > >
>> > > > On Thu, Mar 16, 2023 at 01:31:04PM +0100, Markus Armbruster wrote:
>> > > > > Philippe Mathieu-Daudé <philmd@linaro.org> writes:
>> > > > >
>> > > > > > Per the C++ standard, empty enum are ill-formed. Do not generate
>> > >
>> > > The C standard. The C++ standard doesn't apply here :)
>> >
>> > I can't find how empty enums are considered by the C standard...
C99 §6.7.2.2 Enumeration specifiers
enum-specifier:
enum identifier-opt { enumerator-list }
enum identifier-opt { enumerator-list , }
enum identifier
enumerator-list:
enumerator
enumerator-list , enumerator
enumerator:
enumeration-constant
enumeration-constant = constant-expr
Empty enum is a syntax error.
>> The C standard doesn't really matter either.
>>
>> What we actually care about is whether GCC and CLang consider the
>> empty enums to be permissible or not. or to put it another way...
>> if it compiles, ship it :-)
>
> But it doesn't compile:
>
> $ cat foo.c
> typedef enum Blah {
> } Blah;
> int main(void) {
> Blah b = 0;
> }
> $ gcc -o foo -Wall foo.c
> foo.c:2:1: error: empty enum is invalid
> 2 | } Blah;
> | ^
Gcc opts for an error message more useful than "identifier expected".
> foo.c: In function ‘main’:
> foo.c:4:5: error: unknown type name ‘Blah’; use ‘enum’ keyword to refer to the type
> 4 | Blah b = 0;
> | ^~~~
> | enum
> foo.c:4:10: warning: unused variable ‘b’ [-Wunused-variable]
> 4 | Blah b = 0;
> | ^
>
> So we _do_ need to avoid creating an enum with all members optional in
> the configuration where all options are disabled, if we want that
> configuration to compile. Or require that all QAPI enums have at
> least one non-optional member.
There is just one way to avoid creating such an enum: make sure the QAPI
enum has members in all configurations we care for.
The issue at hand is whether catching empty enums in qapi-gen already is
practical. Desirable, because qapi-gen errors are friendlier than C
compiler errors in generated code.
Note "practical": qapi-gen makes an effort to catch errors before the C
compiler catches them. But catching all of them is impractical.
Having qapi-gen catch empty enums sure is practical for truly empty
enums. But I doubt an enum without any members is a mistake people make
much.
It isn't for enums with only conditional members: the configuration that
makes them all vanish may or may not actually matter, or even exist, and
qapi-gen can't tell. The C compiler can tell, but only for the
configuration being compiled.
Requiring at least one non-optional member is a restriction: enums with
only conditional members are now rejected even when the configuration
that makes them all vanish does not actually matter.
Is shielding the user from C compiler errors about empty enums worth the
restriction?
next prev parent reply other threads:[~2023-03-22 5:47 UTC|newest]
Thread overview: 19+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-03-15 11:28 [PATCH v3 0/3] qapi: Simplify enum generation Philippe Mathieu-Daudé
2023-03-15 11:28 ` [PATCH v3 1/3] scripts/git.orderfile: Display QAPI script changes before schema ones Philippe Mathieu-Daudé
2023-03-15 15:14 ` Philippe Mathieu-Daudé
2023-03-15 20:30 ` Juan Quintela
2023-03-15 11:28 ` [PATCH v3 2/3] qapi: Do not generate empty enum Philippe Mathieu-Daudé
2023-03-15 15:02 ` Richard Henderson
2023-03-16 12:31 ` Markus Armbruster
2023-03-16 13:54 ` Daniel P. Berrangé
2023-03-16 14:39 ` Juan Quintela
2023-03-16 14:42 ` Daniel P. Berrangé
2023-03-16 14:57 ` Markus Armbruster
2023-03-21 14:31 ` Philippe Mathieu-Daudé
2023-03-21 15:19 ` Daniel P. Berrangé
2023-03-21 21:43 ` Eric Blake
2023-03-22 5:45 ` Markus Armbruster [this message]
2023-03-21 14:48 ` Philippe Mathieu-Daudé
2023-03-21 19:00 ` Markus Armbruster
2023-03-15 11:28 ` [PATCH v3 3/3] qapi: Generate enum count as definition Philippe Mathieu-Daudé
2023-03-15 19:58 ` Dr. David Alan Gilbert
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=87sfdx9w58.fsf@pond.sub.org \
--to=armbru@redhat.com \
--cc=berrange@redhat.com \
--cc=dgilbert@redhat.com \
--cc=eblake@redhat.com \
--cc=kraxel@redhat.com \
--cc=marcandre.lureau@redhat.com \
--cc=michael.roth@amd.com \
--cc=pavel.dovgaluk@ispras.ru \
--cc=pbonzini@redhat.com \
--cc=philmd@linaro.org \
--cc=qemu-devel@nongnu.org \
--cc=quintela@redhat.com \
--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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.