From 9ca55f4ba588211ece65d89891223d8a5d1ab407 Mon Sep 17 00:00:00 2001 From: Eric Blake Date: Mon, 19 Feb 2018 20:34:51 -0500 Subject: [PATCH] qapi: Don't let implicit enum MAX member collide Now that we guarantee the user doesn't have any enum values beginning with a single underscore, we can use that for our own purposes. Renaming ENUM_MAX to ENUM__MAX makes it obvious that the sentinel is generated. This patch was mostly generated by applying a temporary patch: |diff --git a/scripts/qapi.py b/scripts/qapi.py |index e6d014b..b862ec9 100644 |--- a/scripts/qapi.py |+++ b/scripts/qapi.py |@@ -1570,6 +1570,7 @@ const char *const %(c_name)s_lookup[] = { | max_index = c_enum_const(name, 'MAX', prefix) | ret += mcgen(''' | [%(max_index)s] = NULL, |+// %(max_index)s | }; | ''', | max_index=max_index) then running: $ cat qapi-{types,event}.c tests/test-qapi-types.c | sed -n 's,^// \(.*\)MAX,s|\1MAX|\1_MAX|g,p' > list $ git grep -l _MAX | xargs sed -i -f list The only things not generated are the changes in scripts/qapi.py. Rejecting enum members named 'MAX' is now useless, and will be dropped in the next patch. Backports commit 7fb1cf1606c78c9d5b538f29176fd5a101726a9d from qemu --- qemu/scripts/qapi.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/qemu/scripts/qapi.py b/qemu/scripts/qapi.py index 41c4b4f4..5c5b73fc 100644 --- a/qemu/scripts/qapi.py +++ b/qemu/scripts/qapi.py @@ -920,7 +920,7 @@ class QAPISchemaEnumType(QAPISchemaType): return c_name(self.name) def c_null(self): - return c_enum_const(self.name, (self.values + ['MAX'])[0], + return c_enum_const(self.name, (self.values + ['_MAX'])[0], self.prefix) def json_type(self): @@ -1570,7 +1570,7 @@ const char *const %(c_name)s_lookup[] = { # Unicorn: We don't use C99 [ARRAY_INDICING] = Thing because # MSVC is still in the stone-age with this part of C compiler # support. - max_index = c_enum_const(name, 'MAX', prefix) + max_index = c_enum_const(name, '_MAX', prefix) ret += mcgen(''' NULL, }; @@ -1581,7 +1581,7 @@ const char *const %(c_name)s_lookup[] = { def gen_enum(name, values, prefix=None): # append automatically generated _MAX value - enum_values = values + ['MAX'] + enum_values = values + ['_MAX'] ret = mcgen(''' typedef enum %(c_name)s {