Received: from eggs.gnu.org ([2001:4830:134:3::10]:40653) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ZxIqY-0005jw-Bj for qemu-devel@nongnu.org; Fri, 13 Nov 2015 13:13:15 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1ZxIqX-0005cc-7M for qemu-devel@nongnu.org; Fri, 13 Nov 2015 13:13:14 -0500 From: Markus Armbruster References: <1447224690-9743-1-git-send-email-eblake@redhat.com> <1447224690-9743-20-git-send-email-eblake@redhat.com> <564621E7.9040103@redhat.com> Date: Fri, 13 Nov 2015 19:13:03 +0100 In-Reply-To: <564621E7.9040103@redhat.com> (Eric Blake's message of "Fri, 13 Nov 2015 10:46:15 -0700") Message-ID: <87wptlu5sg.fsf@blackfin.pond.sub.org> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Subject: Re: [Qemu-devel] [PATCH v11 19/28] qapi: Change munging of CamelCase enum values List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: Eric Blake Cc: Kevin Wolf , Peter Maydell , "open list:Block layer core" , "Michael S. Tsirkin" , Jason Wang , qemu-devel@nongnu.org, Michael Roth , Gerd Hoffmann , Amit Shah , Luiz Capitulino , Andreas =?utf-8?Q?F=C3=A4rber?= Eric Blake writes: > On 11/10/2015 11:51 PM, Eric Blake wrote: >> When munging enum values, the fact that we were passing the entire >> prefix + value through camel_to_upper() meant that enum values >> spelled with CamelCase could be turned into CAMEL_CASE. However, >> this provides a potential collision (both OneTwo and One-Two would >> munge into ONE_TWO). By changing the generation of enum constants >> to always be prefix + '_' + c_name(value).upper(), we can avoid >> any risk of collisions (if we can also ensure no case collisions, >> in the next patch) without having to think about what the >> heuristics in camel_to_upper() will actually do to the value. >>=20 > >> +++ b/scripts/qapi.py >> @@ -1439,7 +1439,7 @@ def camel_to_upper(value): >> def c_enum_const(type_name, const_name, prefix=3DNone): >> if prefix is not None: >> type_name =3D prefix >> - return camel_to_upper(type_name + '_' + const_name) >> + return camel_to_upper(type_name) + '_' + c_name(const_name, False).= upper() > > Doesn't match the commit message, because it used c_name(,False), while > c_name(name) is short for c_name(name, True). > > What's more, looking at it exposed a bug: c_name('_Thread-local') > returns '_Thread_local', but this collides with a C11 keyword (and was > supposed to be munged to q__Thread_local); add '_Thread-local':'int' to > a struct to see the resulting hilarity: > > CC tests/test-qmp-output-visitor.o > In file included from tests/test-qmp-output-visitor.c:17:0: > tests/test-qapi-types.h:781:13: error: expected identifier or =E2=80=98(= =E2=80=99 before > =E2=80=98_Thread_local=E2=80=99 > int64_t _Thread_local; > ^ c_name() first protects ticklish identifiers, then mangles. That's exactly backwards. > But it also made me realize that c_name('Q-int') happily returns 'Q_int' > (we only reserved the leading 'q_' namespace, not 'Q_'). Alas, that > means c_name('Q-int', False).upper() and c_name('int', False).upper() > both produce 'Q_INT', and we have a collision. So I think enum names > have to be munged by c_name(name, True). Our goal is to have simple rules for reserved names and collisions, and resonably simple code to catch them. "[PATCH 20] qapi: Forbid case-insensitive clashes" is incomplete --- if we make clashing case-insensitive, we need to reserve names case-insensitively, too. We need c_name() to protect ticklish identifiers only when its result is used as identifier. Not when it's *part* of an identifier, e.g. prefixed with qapi_, or camel_to_upper(type_name) + '_'. We can protect even when we don't need to, if that helps keeping things simple. The obvious simple way to check for collisions works like this: 1. Every QAPI name is mangled in exactly one way, modulo case: always with c_name(), and always with the same value of protect. 2. We require the mangled name to be case-insensitively unique in its name space. Any holes left?