qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: "Marc-André Lureau" <marcandre.lureau@redhat.com>
To: qemu-devel@nongnu.org
Cc: eblake@redhat.com, armbru@redhat.com,
	"Marc-André Lureau" <marcandre.lureau@redhat.com>,
	"Michael Roth" <mdroth@linux.vnet.ibm.com>,
	"Dr. David Alan Gilbert" <dgilbert@redhat.com>,
	"Eduardo Habkost" <ehabkost@redhat.com>,
	"Cleber Rosa" <crosa@redhat.com>
Subject: [Qemu-devel] [PATCH v4 03/51] qapi: generate a literal qobject for introspection
Date: Thu, 11 Jan 2018 22:32:02 +0100	[thread overview]
Message-ID: <20180111213250.16511-4-marcandre.lureau@redhat.com> (raw)
In-Reply-To: <20180111213250.16511-1-marcandre.lureau@redhat.com>

Replace the generated json string with a literal qobject. The later is
easier to deal with, at run time as well as compile time: adding #if
conditionals will be easier than in a json string.

Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com>
Reviewed-by: Markus Armbruster <armbru@redhat.com>
---
 scripts/qapi-introspect.py         | 83 ++++++++++++++++++++++----------------
 monitor.c                          |  2 +-
 tests/test-qobject-input-visitor.c | 11 +++--
 docs/devel/qapi-code-gen.txt       | 29 ++++++++-----
 4 files changed, 76 insertions(+), 49 deletions(-)

diff --git a/scripts/qapi-introspect.py b/scripts/qapi-introspect.py
index 032bcea491..0002bc1a68 100644
--- a/scripts/qapi-introspect.py
+++ b/scripts/qapi-introspect.py
@@ -12,26 +12,36 @@
 from qapi import *
 
 
-# Caveman's json.dumps() replacement (we're stuck at Python 2.4)
-# TODO try to use json.dumps() once we get unstuck
-def to_json(obj, level=0):
+def to_qlit(obj, level=0, suppress_first_indent=False):
+
+    def indent(level):
+        return level * 4 * ' '
+
+    ret = ''
+    if not suppress_first_indent:
+        ret += indent(level)
     if obj is None:
-        ret = 'null'
+        ret += 'QLIT_QNULL'
     elif isinstance(obj, str):
-        ret = '"' + obj.replace('"', r'\"') + '"'
+        ret += 'QLIT_QSTR(' + to_c_string(obj) + ')'
     elif isinstance(obj, list):
-        elts = [to_json(elt, level + 1)
+        elts = [to_qlit(elt, level + 1)
                 for elt in obj]
-        ret = '[' + ', '.join(elts) + ']'
+        elts.append(indent(level + 1) + "{}")
+        ret += 'QLIT_QLIST(((QLitObject[]) {\n'
+        ret += ',\n'.join(elts) + '\n'
+        ret += indent(level) + '}))'
     elif isinstance(obj, dict):
-        elts = ['"%s": %s' % (key.replace('"', r'\"'),
-                              to_json(obj[key], level + 1))
-                for key in sorted(obj.keys())]
-        ret = '{' + ', '.join(elts) + '}'
+        elts = []
+        for key, value in sorted(obj.iteritems()):
+            elts.append(indent(level + 1) + '{ %s, %s }' %
+                        (to_c_string(key), to_qlit(value, level + 1, True)))
+        elts.append(indent(level + 1) + '{}')
+        ret += 'QLIT_QDICT(((QLitDictEntry[]) {\n'
+        ret += ',\n'.join(elts) + '\n'
+        ret += indent(level) + '}))'
     else:
         assert False                # not implemented
-    if level == 1:
-        ret = '\n' + ret
     return ret
 
 
@@ -45,39 +55,37 @@ class QAPISchemaGenIntrospectVisitor(QAPISchemaVisitor):
         self.defn = None
         self.decl = None
         self._schema = None
-        self._jsons = None
+        self._qlits = None
         self._used_types = None
         self._name_map = None
 
     def visit_begin(self, schema):
         self._schema = schema
-        self._jsons = []
+        self._qlits = []
         self._used_types = []
         self._name_map = {}
 
     def visit_end(self):
         # visit the types that are actually used
-        jsons = self._jsons
-        self._jsons = []
+        qlits = self._qlits
+        self._qlits = []
         for typ in self._used_types:
             typ.visit(self)
         # generate C
         # TODO can generate awfully long lines
-        jsons.extend(self._jsons)
-        name = c_name(prefix, protect=False) + 'qmp_schema_json'
+        qlits.extend(self._qlits)
+        name = c_name(prefix, protect=False) + 'qmp_schema_qlit'
         self.decl = mcgen('''
-extern const char %(c_name)s[];
+extern const QLitObject %(c_name)s;
 ''',
                           c_name=c_name(name))
-        lines = to_json(jsons).split('\n')
-        c_string = '\n    '.join([to_c_string(line) for line in lines])
         self.defn = mcgen('''
-const char %(c_name)s[] = %(c_string)s;
+const QLitObject %(c_name)s = %(c_string)s;
 ''',
                           c_name=c_name(name),
-                          c_string=c_string)
+                          c_string=to_qlit(qlits))
         self._schema = None
-        self._jsons = None
+        self._qlits = None
         self._used_types = None
         self._name_map = None
 
@@ -111,12 +119,12 @@ const char %(c_name)s[] = %(c_string)s;
             return '[' + self._use_type(typ.element_type) + ']'
         return self._name(typ.name)
 
-    def _gen_json(self, name, mtype, obj):
+    def _gen_qlit(self, name, mtype, obj):
         if mtype not in ('command', 'event', 'builtin', 'array'):
             name = self._name(name)
         obj['name'] = name
         obj['meta-type'] = mtype
-        self._jsons.append(obj)
+        self._qlits.append(obj)
 
     def _gen_member(self, member):
         ret = {'name': member.name, 'type': self._use_type(member.type)}
@@ -132,24 +140,24 @@ const char %(c_name)s[] = %(c_string)s;
         return {'case': variant.name, 'type': self._use_type(variant.type)}
 
     def visit_builtin_type(self, name, info, json_type):
-        self._gen_json(name, 'builtin', {'json-type': json_type})
+        self._gen_qlit(name, 'builtin', {'json-type': json_type})
 
     def visit_enum_type(self, name, info, values, prefix):
-        self._gen_json(name, 'enum', {'values': values})
+        self._gen_qlit(name, 'enum', {'values': values})
 
     def visit_array_type(self, name, info, element_type):
         element = self._use_type(element_type)
-        self._gen_json('[' + element + ']', 'array', {'element-type': element})
+        self._gen_qlit('[' + element + ']', 'array', {'element-type': element})
 
     def visit_object_type_flat(self, name, info, members, variants):
         obj = {'members': [self._gen_member(m) for m in members]}
         if variants:
             obj.update(self._gen_variants(variants.tag_member.name,
                                           variants.variants))
-        self._gen_json(name, 'object', obj)
+        self._gen_qlit(name, 'object', obj)
 
     def visit_alternate_type(self, name, info, variants):
-        self._gen_json(name, 'alternate',
+        self._gen_qlit(name, 'alternate',
                        {'members': [{'type': self._use_type(m.type)}
                                     for m in variants.variants]})
 
@@ -157,13 +165,13 @@ const char %(c_name)s[] = %(c_string)s;
                       gen, success_response, boxed):
         arg_type = arg_type or self._schema.the_empty_object_type
         ret_type = ret_type or self._schema.the_empty_object_type
-        self._gen_json(name, 'command',
+        self._gen_qlit(name, 'command',
                        {'arg-type': self._use_type(arg_type),
                         'ret-type': self._use_type(ret_type)})
 
     def visit_event(self, name, info, arg_type, boxed):
         arg_type = arg_type or self._schema.the_empty_object_type
-        self._gen_json(name, 'event', {'arg-type': self._use_type(arg_type)})
+        self._gen_qlit(name, 'event', {'arg-type': self._use_type(arg_type)})
 
 # Debugging aid: unmask QAPI schema's type names
 # We normally mask them, because they're not QMP wire ABI
@@ -205,11 +213,18 @@ h_comment = '''
 
 fdef.write(mcgen('''
 #include "qemu/osdep.h"
+#include "qapi/qmp/qlit.h"
 #include "%(prefix)sqmp-introspect.h"
 
 ''',
                  prefix=prefix))
 
+fdecl.write(mcgen('''
+#include "qemu/osdep.h"
+#include "qapi/qmp/qlit.h"
+
+'''))
+
 schema = QAPISchema(input_file)
 gen = QAPISchemaGenIntrospectVisitor(opt_unmask)
 schema.visit(gen)
diff --git a/monitor.c b/monitor.c
index d682eee2d8..b8931364f2 100644
--- a/monitor.c
+++ b/monitor.c
@@ -953,7 +953,7 @@ EventInfoList *qmp_query_events(Error **errp)
 static void qmp_query_qmp_schema(QDict *qdict, QObject **ret_data,
                                  Error **errp)
 {
-    *ret_data = qobject_from_json(qmp_schema_json, &error_abort);
+    *ret_data = qobject_from_qlit(&qmp_schema_qlit);
 }
 
 /*
diff --git a/tests/test-qobject-input-visitor.c b/tests/test-qobject-input-visitor.c
index fe591814e4..7c1d10e274 100644
--- a/tests/test-qobject-input-visitor.c
+++ b/tests/test-qobject-input-visitor.c
@@ -1247,24 +1247,27 @@ static void test_visitor_in_fail_alternate(TestInputVisitorData *data,
 }
 
 static void do_test_visitor_in_qmp_introspect(TestInputVisitorData *data,
-                                              const char *schema_json)
+                                              const QLitObject *qlit)
 {
     SchemaInfoList *schema = NULL;
+    QObject *obj = qobject_from_qlit(qlit);
     Visitor *v;
 
-    v = visitor_input_test_init_raw(data, schema_json);
+    v = qobject_input_visitor_new(obj);
 
     visit_type_SchemaInfoList(v, NULL, &schema, &error_abort);
     g_assert(schema);
 
     qapi_free_SchemaInfoList(schema);
+    qobject_decref(obj);
+    visit_free(v);
 }
 
 static void test_visitor_in_qmp_introspect(TestInputVisitorData *data,
                                            const void *unused)
 {
-    do_test_visitor_in_qmp_introspect(data, test_qmp_schema_json);
-    do_test_visitor_in_qmp_introspect(data, qmp_schema_json);
+    do_test_visitor_in_qmp_introspect(data, &test_qmp_schema_qlit);
+    do_test_visitor_in_qmp_introspect(data, &qmp_schema_qlit);
 }
 
 int main(int argc, char **argv)
diff --git a/docs/devel/qapi-code-gen.txt b/docs/devel/qapi-code-gen.txt
index 06ab699066..f58d62686a 100644
--- a/docs/devel/qapi-code-gen.txt
+++ b/docs/devel/qapi-code-gen.txt
@@ -1327,18 +1327,27 @@ Example:
     #ifndef EXAMPLE_QMP_INTROSPECT_H
     #define EXAMPLE_QMP_INTROSPECT_H
 
-    extern const char example_qmp_schema_json[];
+    extern const QLitObject qmp_schema_qlit;
 
     #endif
     $ cat qapi-generated/example-qmp-introspect.c
 [Uninteresting stuff omitted...]
 
-    const char example_qmp_schema_json[] = "["
-        "{\"arg-type\": \"0\", \"meta-type\": \"event\", \"name\": \"MY_EVENT\"}, "
-        "{\"arg-type\": \"1\", \"meta-type\": \"command\", \"name\": \"my-command\", \"ret-type\": \"2\"}, "
-        "{\"members\": [], \"meta-type\": \"object\", \"name\": \"0\"}, "
-        "{\"members\": [{\"name\": \"arg1\", \"type\": \"[2]\"}], \"meta-type\": \"object\", \"name\": \"1\"}, "
-        "{\"members\": [{\"name\": \"integer\", \"type\": \"int\"}, {\"default\": null, \"name\": \"string\", \"type\": \"str\"}], \"meta-type\": \"object\", \"name\": \"2\"}, "
-        "{\"element-type\": \"2\", \"meta-type\": \"array\", \"name\": \"[2]\"}, "
-        "{\"json-type\": \"int\", \"meta-type\": \"builtin\", \"name\": \"int\"}, "
-        "{\"json-type\": \"string\", \"meta-type\": \"builtin\", \"name\": \"str\"}]";
+    const QLitObject example_qmp_schema_qlit = QLIT_QLIST(((QLitObject[]) {
+        QLIT_QDICT(((QLitDictEntry[]) {
+            { "arg-type", QLIT_QSTR("0") },
+            { "meta-type", QLIT_QSTR("event") },
+            { "name", QLIT_QSTR("Event") },
+            { }
+        })),
+        QLIT_QDICT(((QLitDictEntry[]) {
+            { "members", QLIT_QLIST(((QLitObject[]) {
+                { }
+            })) },
+            { "meta-type", QLIT_QSTR("object") },
+            { "name", QLIT_QSTR("0") },
+            { }
+        })),
+        ...
+        { }
+    }));
-- 
2.16.0.rc1.1.gef27df75a1

  parent reply	other threads:[~2018-01-11 21:33 UTC|newest]

Thread overview: 74+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-01-11 21:31 [Qemu-devel] [PATCH v4 00/51] Hi, Marc-André Lureau
2018-01-11 21:32 ` [Qemu-devel] [PATCH v4 01/51] qlit: use QType instead of int Marc-André Lureau
2018-01-11 22:52   ` Eric Blake
2018-02-05  6:05   ` Markus Armbruster
2018-01-11 21:32 ` [Qemu-devel] [PATCH v4 02/51] qlit: add qobject_from_qlit() Marc-André Lureau
2018-01-11 21:32 ` Marc-André Lureau [this message]
2018-02-06 10:04   ` [Qemu-devel] [PATCH v4 03/51] qapi: generate a literal qobject for introspection Markus Armbruster
2018-02-06 11:01     ` Marc-André Lureau
2018-01-11 21:32 ` [Qemu-devel] [PATCH v4 04/51] qapi2texi: minor python code simplification Marc-André Lureau
2018-01-11 21:32 ` [Qemu-devel] [PATCH v4 05/51] qapi: add 'if' to top-level expressions Marc-André Lureau
2018-02-05  6:12   ` Markus Armbruster
2018-01-11 21:32 ` [Qemu-devel] [PATCH v4 06/51] qapi: pass 'if' condition into QAPISchemaEntity objects Marc-André Lureau
2018-02-05  6:16   ` Markus Armbruster
2018-02-06 10:12   ` Markus Armbruster
2018-02-06 11:06     ` Marc-André Lureau
2018-02-06 12:14       ` Markus Armbruster
2018-01-11 21:32 ` [Qemu-devel] [PATCH v4 07/51] qapi: leave the ifcond attribute undefined until check() Marc-André Lureau
2018-02-05  6:22   ` Markus Armbruster
2018-02-06 11:20   ` Markus Armbruster
2018-01-11 21:32 ` [Qemu-devel] [PATCH v4 08/51] qapi: add 'ifcond' to visitor methods Marc-André Lureau
2018-01-11 21:32 ` [Qemu-devel] [PATCH v4 09/51] qapi: mcgen() shouldn't indent # lines Marc-André Lureau
2018-01-11 21:32 ` [Qemu-devel] [PATCH v4 10/51] qapi: add #if/#endif helpers Marc-André Lureau
2018-02-05  7:03   ` Markus Armbruster
2018-01-11 21:32 ` [Qemu-devel] [PATCH v4 11/51] qapi-introspect: modify to_qlit() to append ', ' on level > 0 Marc-André Lureau
2018-01-11 21:32 ` [Qemu-devel] [PATCH v4 12/51] qapi-introspect: add preprocessor conditions to generated QLit Marc-André Lureau
2018-01-12 10:27   ` Marc-André Lureau
2018-01-11 21:32 ` [Qemu-devel] [PATCH v4 13/51] qapi-commands: add #if conditions to commands Marc-André Lureau
2018-01-11 21:32 ` [Qemu-devel] [PATCH v4 14/51] qapi-event: add #if conditions to events Marc-André Lureau
2018-01-11 21:32 ` [Qemu-devel] [PATCH v4 15/51] qapi-types: refactor variants handling Marc-André Lureau
2018-01-11 21:32 ` [Qemu-devel] [PATCH v4 16/51] qapi-types: add #if conditions to types & visitors Marc-André Lureau
2018-01-11 21:32 ` [Qemu-devel] [PATCH v4 17/51] qapi: do not define enumeration value explicitely Marc-André Lureau
2018-01-11 21:32 ` [Qemu-devel] [PATCH v4 18/51] qapi: rename QAPISchemaEnumType.values to .members Marc-André Lureau
2018-01-11 21:32 ` [Qemu-devel] [PATCH v4 19/51] qapi: change enum visitor to take QAPISchemaMember Marc-André Lureau
2018-01-11 21:32 ` [Qemu-devel] [PATCH v4 20/51] tests: modify visit_enum_type() in test-qapi to print members Marc-André Lureau
2018-01-11 21:32 ` [Qemu-devel] [PATCH v4 21/51] qapi: factor out check_known_keys() Marc-André Lureau
2018-01-11 21:32 ` [Qemu-devel] [PATCH v4 22/51] qapi: add a dictionnary form with 'name' key for enum members Marc-André Lureau
2018-01-11 21:32 ` [Qemu-devel] [PATCH v4 23/51] qapi: add 'if' to " Marc-André Lureau
2018-01-11 21:32 ` [Qemu-devel] [PATCH v4 24/51] qapi-event: add 'if' condition to implicit event enum Marc-André Lureau
2018-01-11 21:32 ` [Qemu-devel] [PATCH v4 25/51] qapi: rename allow_dict to allow_implicit Marc-André Lureau
2018-01-11 21:32 ` [Qemu-devel] [PATCH v4 26/51] qapi: add a dictionary form with 'type' key for members Marc-André Lureau
2018-01-11 21:32 ` [Qemu-devel] [PATCH v4 27/51] qapi: add 'if' to implicit struct members Marc-André Lureau
2018-01-11 21:32 ` [Qemu-devel] [PATCH v4 28/51] qapi: add an error in case a discriminator is conditionnal Marc-André Lureau
2018-01-11 21:32 ` [Qemu-devel] [PATCH v4 29/51] qapi: add 'if' on union members Marc-André Lureau
2018-01-11 21:32 ` [Qemu-devel] [PATCH v4 30/51] qapi: add 'if' to alternate members Marc-André Lureau
2018-01-11 21:32 ` [Qemu-devel] [PATCH v4 31/51] qapi: add #if conditions to generated code Marc-André Lureau
2018-01-11 21:32 ` [Qemu-devel] [PATCH v4 32/51] docs: document schema configuration Marc-André Lureau
2018-01-11 21:32 ` [Qemu-devel] [PATCH v4 33/51] qapi2texi: add 'If:' section to generated documentation Marc-André Lureau
2018-01-11 21:32 ` [Qemu-devel] [PATCH v4 34/51] qapi2texi: add 'If:' condition to enum values Marc-André Lureau
2018-01-11 21:32 ` [Qemu-devel] [PATCH v4 35/51] qapi2texi: add 'If:' condition to struct members Marc-André Lureau
2018-01-11 21:32 ` [Qemu-devel] [PATCH v4 36/51] qapi2texi: add condition to variants Marc-André Lureau
2018-01-11 21:32 ` [Qemu-devel] [PATCH v4 37/51] qapi: add conditions to VNC type/commands/events on the schema Marc-André Lureau
2018-01-12 13:04   ` Gerd Hoffmann
2018-01-12 13:22     ` Marc-André Lureau
2018-01-12 13:55       ` Gerd Hoffmann
2018-01-11 21:32 ` [Qemu-devel] [PATCH v4 38/51] qapi: add conditions to SPICE " Marc-André Lureau
2018-01-12 13:09   ` Gerd Hoffmann
2018-01-11 21:32 ` [Qemu-devel] [PATCH v4 39/51] qapi: add conditions to REPLICATION type/commands " Marc-André Lureau
2018-01-11 21:32 ` [Qemu-devel] [PATCH v4 40/51] qapi-commands: don't initialize command list in qmp_init_marshall() Marc-André Lureau
2018-01-11 21:32 ` [Qemu-devel] [PATCH v4 41/51] qapi: add -i/--include filename.h Marc-André Lureau
2018-02-05 16:58   ` Markus Armbruster
2018-01-11 21:32 ` [Qemu-devel] [PATCH v4 42/51] qapi: add a 'unit' pragma Marc-André Lureau
2018-01-12 11:54   ` Marc-André Lureau
2018-02-05 18:13     ` Markus Armbruster
2018-02-06 11:01       ` Marc-André Lureau
2018-02-06 12:31         ` Markus Armbruster
2018-01-11 21:32 ` [Qemu-devel] [PATCH v4 43/51] build-sys: move qmp-introspect per target Marc-André Lureau
2018-01-11 21:32 ` [Qemu-devel] [PATCH v4 44/51] build-sys: add a target schema Marc-André Lureau
2018-01-11 21:32 ` [Qemu-devel] [PATCH v4 45/51] qapi: make rtc-reset-reinjection depend on TARGET_I386 Marc-André Lureau
2018-01-11 21:32 ` [Qemu-devel] [PATCH v4 46/51] qapi: make s390 commands depend on TARGET_S390X Marc-André Lureau
2018-01-11 21:32 ` [Qemu-devel] [PATCH v4 47/51] target.json: add a note about query-cpu* not being s390x-specific Marc-André Lureau
2018-01-11 21:32 ` [Qemu-devel] [PATCH v4 48/51] qapi: make query-gic-capabilities depend on TARGET_ARM Marc-André Lureau
2018-01-11 21:32 ` [Qemu-devel] [PATCH v4 49/51] qapi: make query-cpu-model-expansion depend on s390 or x86 Marc-André Lureau
2018-01-11 21:32 ` [Qemu-devel] [PATCH v4 50/51] qapi: make query-cpu-definitions depend on specific targets Marc-André Lureau
2018-01-11 21:32 ` [Qemu-devel] [PATCH v4 51/51] qapi: remove qmp_unregister_command() Marc-André Lureau

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20180111213250.16511-4-marcandre.lureau@redhat.com \
    --to=marcandre.lureau@redhat.com \
    --cc=armbru@redhat.com \
    --cc=crosa@redhat.com \
    --cc=dgilbert@redhat.com \
    --cc=eblake@redhat.com \
    --cc=ehabkost@redhat.com \
    --cc=mdroth@linux.vnet.ibm.com \
    --cc=qemu-devel@nongnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).