[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Qemu-devel] [PATCH v3 09/50] qapi: add #if/#endif helpers
From: |
Marc-André Lureau |
Subject: |
[Qemu-devel] [PATCH v3 09/50] qapi: add #if/#endif helpers |
Date: |
Mon, 11 Sep 2017 13:05:42 +0200 |
Add helpers to wrap generated code with #if/#endif lines.
Add a function decorator that will be used to wrap visitor methods.
The decorator will check if code was generated before adding #if/#endif
lines. Used in the following patches.
Signed-off-by: Marc-André Lureau <address@hidden>
---
scripts/qapi.py | 55 +++++++++++++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 55 insertions(+)
diff --git a/scripts/qapi.py b/scripts/qapi.py
index 2a8e60e975..94b735d8d6 100644
--- a/scripts/qapi.py
+++ b/scripts/qapi.py
@@ -1897,6 +1897,61 @@ def guardend(name):
name=guardname(name))
+def gen_if(ifcond):
+ if not ifcond:
+ return ''
+ if isinstance(ifcond, str):
+ ifcond = [ifcond]
+ ret = ''
+ for ifc in ifcond:
+ ret += mcgen('''
+#if %(cond)s
+''', cond=ifc)
+ return ret
+
+
+def gen_endif(ifcond):
+ if not ifcond:
+ return ''
+ if isinstance(ifcond, str):
+ ifcond = [ifcond]
+ ret = ''
+ for ifc in reversed(ifcond):
+ ret += mcgen('''
+#endif /* %(cond)s */
+''', cond=ifc)
+ return ret
+
+
+# Wrap a method to add #if / #endif to generated code, only if some
+# code was generated. The method must have an 'ifcond' argument.
+# self must have 'if_members' listing the attributes to wrap.
+def ifcond_decorator(func):
+
+ def func_wrapper(self, *args, **kwargs):
+ import inspect
+ idx = inspect.getargspec(func).args.index('ifcond')
+ ifcond = args[idx - 1]
+ save = {}
+ for mem in self.if_members:
+ save[mem] = getattr(self, mem)
+ func(self, *args, **kwargs)
+ for mem, val in save.items():
+ newval = getattr(self, mem)
+ if newval != val:
+ assert newval.startswith(val)
+ newval = newval[len(val):]
+ if newval[0] == '\n':
+ val += '\n'
+ newval = newval[1:]
+ val += gen_if(ifcond)
+ val += newval
+ val += gen_endif(ifcond)
+ setattr(self, mem, val)
+
+ return func_wrapper
+
+
def gen_enum_lookup(name, values, prefix=None):
ret = mcgen('''
--
2.14.1.146.gd35faa819
- [Qemu-devel] [PATCH v3 03/50] qapi2texi: minor python code simplification, (continued)
- [Qemu-devel] [PATCH v3 03/50] qapi2texi: minor python code simplification, Marc-André Lureau, 2017/09/11
- [Qemu-devel] [PATCH v3 02/50] qapi: generate a literal qobject for introspection, Marc-André Lureau, 2017/09/11
- [Qemu-devel] [PATCH v3 01/50] qlit: add qobject_from_qlit(), Marc-André Lureau, 2017/09/11
- [Qemu-devel] [PATCH v3 04/50] qapi: add 'if' to top-level expressions, Marc-André Lureau, 2017/09/11
- [Qemu-devel] [PATCH v3 05/50] qapi: add tests for invalid 'if', Marc-André Lureau, 2017/09/11
- [Qemu-devel] [PATCH v3 07/50] qapi: add 'ifcond' to visitor methods, Marc-André Lureau, 2017/09/11
- [Qemu-devel] [PATCH v3 06/50] qapi: pass 'if' condition into QAPISchemaEntity objects, Marc-André Lureau, 2017/09/11
- [Qemu-devel] [PATCH v3 08/50] qapi: mcgen() shouldn't indent # lines, Marc-André Lureau, 2017/09/11
- [Qemu-devel] [PATCH v3 10/50] qapi-introspect: modify to_qlit() to append ', ' on level > 0, Marc-André Lureau, 2017/09/11
- [Qemu-devel] [PATCH v3 11/50] qapi-introspect: modify to_qlit() to generate #if code, Marc-André Lureau, 2017/09/11
- [Qemu-devel] [PATCH v3 09/50] qapi: add #if/#endif helpers,
Marc-André Lureau <=
- [Qemu-devel] [PATCH v3 12/50] qapi-introspect: add preprocessor conditions to generated QLit, Marc-André Lureau, 2017/09/11
- [Qemu-devel] [PATCH v3 13/50] qapi-commands: add #if conditions to commands, Marc-André Lureau, 2017/09/11
- [Qemu-devel] [PATCH v3 14/50] qapi-event: add #if conditions to events, Marc-André Lureau, 2017/09/11
- [Qemu-devel] [PATCH v3 17/50] qapi: do not define enumeration value explicitely, Marc-André Lureau, 2017/09/11
- [Qemu-devel] [PATCH v3 16/50] qapi-types: add #if conditions to types & visitors, Marc-André Lureau, 2017/09/11
- [Qemu-devel] [PATCH v3 15/50] qapi-types: refactor variants handling, Marc-André Lureau, 2017/09/11
- [Qemu-devel] [PATCH v3 19/50] qapi: add 'if' to enum members, Marc-André Lureau, 2017/09/11
- [Qemu-devel] [PATCH v3 18/50] qapi: change enum visitor to take QAPISchemaMember, Marc-André Lureau, 2017/09/11
- [Qemu-devel] [PATCH v3 21/50] qapi: add #if conditions on generated enum members, Marc-André Lureau, 2017/09/11
- [Qemu-devel] [PATCH v3 20/50] qapi-event: add 'if' condition to generated enum, Marc-André Lureau, 2017/09/11