[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Qemu-devel] [PATCH v4 10/51] qapi: add #if/#endif helpers
From: |
Marc-André Lureau |
Subject: |
[Qemu-devel] [PATCH v4 10/51] qapi: add #if/#endif helpers |
Date: |
Thu, 11 Jan 2018 22:32:09 +0100 |
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 | 47 +++++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 47 insertions(+)
diff --git a/scripts/qapi.py b/scripts/qapi.py
index 3d33ed7d76..71f28fc6d8 100644
--- a/scripts/qapi.py
+++ b/scripts/qapi.py
@@ -1911,6 +1911,53 @@ def guardend(name):
name=guardname(name))
+def gen_if(ifcond):
+ ret = ''
+ for ifc in ifcond:
+ ret += mcgen('''
+#if %(cond)s
+''', cond=ifc)
+ return ret
+
+
+def gen_endif(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.16.0.rc1.1.gef27df75a1
- [Qemu-devel] [PATCH v4 01/51] qlit: use QType instead of int, (continued)
- [Qemu-devel] [PATCH v4 01/51] qlit: use QType instead of int, Marc-André Lureau, 2018/01/11
- [Qemu-devel] [PATCH v4 02/51] qlit: add qobject_from_qlit(), Marc-André Lureau, 2018/01/11
- [Qemu-devel] [PATCH v4 03/51] qapi: generate a literal qobject for introspection, Marc-André Lureau, 2018/01/11
- [Qemu-devel] [PATCH v4 04/51] qapi2texi: minor python code simplification, Marc-André Lureau, 2018/01/11
- [Qemu-devel] [PATCH v4 05/51] qapi: add 'if' to top-level expressions, Marc-André Lureau, 2018/01/11
- [Qemu-devel] [PATCH v4 06/51] qapi: pass 'if' condition into QAPISchemaEntity objects, Marc-André Lureau, 2018/01/11
- [Qemu-devel] [PATCH v4 07/51] qapi: leave the ifcond attribute undefined until check(), Marc-André Lureau, 2018/01/11
- [Qemu-devel] [PATCH v4 08/51] qapi: add 'ifcond' to visitor methods, Marc-André Lureau, 2018/01/11
- [Qemu-devel] [PATCH v4 09/51] qapi: mcgen() shouldn't indent # lines, Marc-André Lureau, 2018/01/11
- [Qemu-devel] [PATCH v4 10/51] qapi: add #if/#endif helpers,
Marc-André Lureau <=
- [Qemu-devel] [PATCH v4 11/51] qapi-introspect: modify to_qlit() to append ', ' on level > 0, Marc-André Lureau, 2018/01/11
- [Qemu-devel] [PATCH v4 12/51] qapi-introspect: add preprocessor conditions to generated QLit, Marc-André Lureau, 2018/01/11
- [Qemu-devel] [PATCH v4 13/51] qapi-commands: add #if conditions to commands, Marc-André Lureau, 2018/01/11
- [Qemu-devel] [PATCH v4 14/51] qapi-event: add #if conditions to events, Marc-André Lureau, 2018/01/11
- [Qemu-devel] [PATCH v4 15/51] qapi-types: refactor variants handling, Marc-André Lureau, 2018/01/11
- [Qemu-devel] [PATCH v4 16/51] qapi-types: add #if conditions to types & visitors, Marc-André Lureau, 2018/01/11
- [Qemu-devel] [PATCH v4 17/51] qapi: do not define enumeration value explicitely, Marc-André Lureau, 2018/01/11
- [Qemu-devel] [PATCH v4 18/51] qapi: rename QAPISchemaEnumType.values to .members, Marc-André Lureau, 2018/01/11
- [Qemu-devel] [PATCH v4 20/51] tests: modify visit_enum_type() in test-qapi to print members, Marc-André Lureau, 2018/01/11