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

Christophe Fergeau cfergeau at redhat.com
Thu Nov 16 12:05:23 UTC 2017


Hey,

Some concrete examples of what you are going to do with it would be
useful ;)

Christophe

On Mon, Nov 13, 2017 at 01:16:32PM +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.
> 
> 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(+)
> 
> 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/20171116/eebcd293/attachment.sig>


More information about the Spice-devel mailing list