[Spice-devel] [PATCH spice-common v2] codegen: Define identity macros for enum types

Christophe Fergeau cfergeau at redhat.com
Tue Nov 21 10:00:46 UTC 2017


One side effect of this patch is that it makes enums.h fairly
unreadable, which is not so good as it's exposed as public API.

Do you expect to use this that often? I agree it's convenient, but on
the other hand, it's going to make it fairly easy to make "broken"
builds if this is used a lot in the code, as some features will be
silently disabled if a too old spice-protocol happens to be installed.
The availability of enum members is not very hard to check at configure
time, and since we bump spice-protocol version after release, a version
check could be enough.

Christophe



On Thu, Nov 16, 2017 at 12:48:14PM +0000, Frediano Ziglio wrote:
> An identity macro in C is a macro defined as
> 
>   #define SOME_NAME SOME_NAME
> 
> They are often used to make possible to use preprocessor to check
> for values existence like
> 
>   #ifdef SOME_NAME
>   ... use SOME_NAME ...
>   #endif
> 
> Defining these macro for each enumeration value allows to do finer
> check in the code that uses spice-protocol.
> 
> More specifically you could check a message existence with
> 
>   #ifdef SPICE_MSG_MAIN_CHANNELS_LIST
>   ... use channels_list message ..
>   #endif
> 
> or if a given channel exists with
> 
>   #ifdef SPICE_CHANNEL_WEBDAV
>   ... use WebDAVChannel ...
>   #endif
> 
> Signed-off-by: Frediano Ziglio <fziglio at redhat.com>
> ---
>  python_modules/codegen.py | 10 ++++++++++
>  python_modules/ptypes.py  |  2 ++
>  spice_codegen.py          |  2 ++
>  3 files changed, 14 insertions(+)
> 
> Changes since v1:
> - better commit message.
> 
> diff --git a/python_modules/codegen.py b/python_modules/codegen.py
> index f7a2048..6143905 100644
> --- a/python_modules/codegen.py
> +++ b/python_modules/codegen.py
> @@ -327,6 +327,16 @@ class CodeWriter:
>      def macro(self, name, args, define):
>          self.write("#define %s(%s) %s" % (name, args, define)).newline()
>  
> +    def identity_macro(self, name):
> +        """Define a preprocessor macro with same name as value.
> +        This helps writing conditional code using preprocessor defines.
> +        """
> +        assert(self.at_line_start)
> +        indentation = self.indentation
> +        self.indentation = 0
> +        self.writeln("#define %s %s" % (name, name))
> +        self.indentation = indentation
> +
>      def ifdef(self, name):
>          indentation = self.indentation
>          self.indentation = 0;
> diff --git a/python_modules/ptypes.py b/python_modules/ptypes.py
> index 63a7a2f..47b32b5 100644
> --- a/python_modules/ptypes.py
> +++ b/python_modules/ptypes.py
> @@ -381,6 +381,7 @@ class EnumType(EnumBaseType):
>                  writer.write(" = %d" % (i))
>              writer.write(",")
>              writer.newline()
> +            writer.identity_macro(self.c_enumname(i))
>              current_default = i + 1
>          writer.newline()
>          writer.write(codegen.prefix_underscore_upper(self.name.upper(), "ENUM_END"))
> @@ -434,6 +435,7 @@ class FlagsType(EnumBaseType):
>              writer.write(" = (1 << %d)" % (i))
>              writer.write(",")
>              writer.newline()
> +            writer.identity_macro(self.c_enumname(i))
>              current_default = i + 1
>          writer.newline()
>          writer.write(codegen.prefix_underscore_upper(self.name.upper(), "MASK"))
> diff --git a/spice_codegen.py b/spice_codegen.py
> index 76d7c5e..bb50f31 100755
> --- a/spice_codegen.py
> +++ b/spice_codegen.py
> @@ -40,6 +40,7 @@ def write_channel_enums(writer, channel, client, describe):
>              else:
>                  writer.writeln("%s = %s," % (enum, m.value))
>                  i = m.value + 1
> +            writer.identity_macro(enum)
>      if describe:
>          writer.writeln("{ 0, NULL }");
>      else:
> @@ -68,6 +69,7 @@ def write_channel_type_enum(writer, describe=False):
>              else:
>                  writer.writeln("%s = %s," % (enum, c.value))
>                  i = c.value + 1
> +            writer.identity_macro(enum)
>      writer.newline()
>      if describe:
>          writer.writeln("{ 0, NULL }")
> -- 
> 2.13.6
> 
> _______________________________________________
> Spice-devel mailing list
> Spice-devel at lists.freedesktop.org
> https://lists.freedesktop.org/mailman/listinfo/spice-devel
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 833 bytes
Desc: not available
URL: <https://lists.freedesktop.org/archives/spice-devel/attachments/20171121/043fee4b/attachment.sig>


More information about the Spice-devel mailing list