{"id":812312,"url":"http://patchwork.ozlabs.org/api/patches/812312/?format=json","web_url":"http://patchwork.ozlabs.org/project/qemu-devel/patch/20170911110623.24981-3-marcandre.lureau@redhat.com/","project":{"id":14,"url":"http://patchwork.ozlabs.org/api/projects/14/?format=json","name":"QEMU Development","link_name":"qemu-devel","list_id":"qemu-devel.nongnu.org","list_email":"qemu-devel@nongnu.org","web_url":"","scm_url":"","webscm_url":"","list_archive_url":"","list_archive_url_format":"","commit_url_format":""},"msgid":"<20170911110623.24981-3-marcandre.lureau@redhat.com>","list_archive_url":null,"date":"2017-09-11T11:05:35","name":"[v3,02/50] qapi: generate a literal qobject for introspection","commit_ref":null,"pull_url":null,"state":"new","archived":false,"hash":"18d41b97d03b64b6820e3af47c04e9ed4a5b6aa3","submitter":{"id":66774,"url":"http://patchwork.ozlabs.org/api/people/66774/?format=json","name":"Marc-André Lureau","email":"marcandre.lureau@redhat.com"},"delegate":null,"mbox":"http://patchwork.ozlabs.org/project/qemu-devel/patch/20170911110623.24981-3-marcandre.lureau@redhat.com/mbox/","series":[{"id":2480,"url":"http://patchwork.ozlabs.org/api/series/2480/?format=json","web_url":"http://patchwork.ozlabs.org/project/qemu-devel/list/?series=2480","date":"2017-09-11T11:05:33","name":"Hi,","version":3,"mbox":"http://patchwork.ozlabs.org/series/2480/mbox/"}],"comments":"http://patchwork.ozlabs.org/api/patches/812312/comments/","check":"pending","checks":"http://patchwork.ozlabs.org/api/patches/812312/checks/","tags":{},"related":[],"headers":{"Return-Path":"<qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org>","X-Original-To":"incoming@patchwork.ozlabs.org","Delivered-To":"patchwork-incoming@bilbo.ozlabs.org","Authentication-Results":["ozlabs.org;\n\tspf=pass (mailfrom) smtp.mailfrom=nongnu.org\n\t(client-ip=2001:4830:134:3::11; helo=lists.gnu.org;\n\tenvelope-from=qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org;\n\treceiver=<UNKNOWN>)","ext-mx02.extmail.prod.ext.phx2.redhat.com;\n\tdmarc=none (p=none dis=none) header.from=redhat.com","ext-mx02.extmail.prod.ext.phx2.redhat.com;\n\tspf=fail smtp.mailfrom=marcandre.lureau@redhat.com"],"Received":["from lists.gnu.org (lists.gnu.org [IPv6:2001:4830:134:3::11])\n\t(using TLSv1 with cipher AES256-SHA (256/256 bits))\n\t(No client certificate requested)\n\tby ozlabs.org (Postfix) with ESMTPS id 3xrQBD11k3z9s7G\n\tfor <incoming@patchwork.ozlabs.org>;\n\tMon, 11 Sep 2017 21:07:32 +1000 (AEST)","from localhost ([::1]:56737 helo=lists.gnu.org)\n\tby lists.gnu.org with esmtp (Exim 4.71) (envelope-from\n\t<qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org>)\n\tid 1drMYs-0004q2-4t\n\tfor incoming@patchwork.ozlabs.org; Mon, 11 Sep 2017 07:07:30 -0400","from eggs.gnu.org ([2001:4830:134:3::10]:42539)\n\tby lists.gnu.org with esmtp (Exim 4.71)\n\t(envelope-from <marcandre.lureau@redhat.com>) id 1drMXz-0004kC-Lk\n\tfor qemu-devel@nongnu.org; Mon, 11 Sep 2017 07:06:40 -0400","from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71)\n\t(envelope-from <marcandre.lureau@redhat.com>) id 1drMXy-0005yq-79\n\tfor qemu-devel@nongnu.org; Mon, 11 Sep 2017 07:06:35 -0400","from mx1.redhat.com ([209.132.183.28]:53766)\n\tby eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32)\n\t(Exim 4.71) (envelope-from <marcandre.lureau@redhat.com>)\n\tid 1drMXx-0005yW-Tt\n\tfor qemu-devel@nongnu.org; Mon, 11 Sep 2017 07:06:34 -0400","from smtp.corp.redhat.com\n\t(int-mx03.intmail.prod.int.phx2.redhat.com [10.5.11.13])\n\t(using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits))\n\t(No client certificate requested)\n\tby mx1.redhat.com (Postfix) with ESMTPS id 102D9158604;\n\tMon, 11 Sep 2017 11:06:33 +0000 (UTC)","from localhost (ovpn-112-47.ams2.redhat.com [10.36.112.47])\n\tby smtp.corp.redhat.com (Postfix) with ESMTP id 8B34D60A98;\n\tMon, 11 Sep 2017 11:06:32 +0000 (UTC)"],"DMARC-Filter":"OpenDMARC Filter v1.3.2 mx1.redhat.com 102D9158604","From":"=?utf-8?q?Marc-Andr=C3=A9_Lureau?= <marcandre.lureau@redhat.com>","To":"qemu-devel@nongnu.org","Date":"Mon, 11 Sep 2017 13:05:35 +0200","Message-Id":"<20170911110623.24981-3-marcandre.lureau@redhat.com>","In-Reply-To":"<20170911110623.24981-1-marcandre.lureau@redhat.com>","References":"<20170911110623.24981-1-marcandre.lureau@redhat.com>","MIME-Version":"1.0","Content-Type":"text/plain; charset=UTF-8","X-Scanned-By":"MIMEDefang 2.79 on 10.5.11.13","X-Greylist":"Sender IP whitelisted, not delayed by milter-greylist-4.5.16\n\t(mx1.redhat.com [10.5.110.26]);\n\tMon, 11 Sep 2017 11:06:33 +0000 (UTC)","Content-Transfer-Encoding":"quoted-printable","X-detected-operating-system":"by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic]\n\t[fuzzy]","X-Received-From":"209.132.183.28","Subject":"[Qemu-devel] [PATCH v3 02/50] qapi: generate a literal qobject for\n\tintrospection","X-BeenThere":"qemu-devel@nongnu.org","X-Mailman-Version":"2.1.21","Precedence":"list","List-Id":"<qemu-devel.nongnu.org>","List-Unsubscribe":"<https://lists.nongnu.org/mailman/options/qemu-devel>,\n\t<mailto:qemu-devel-request@nongnu.org?subject=unsubscribe>","List-Archive":"<http://lists.nongnu.org/archive/html/qemu-devel/>","List-Post":"<mailto:qemu-devel@nongnu.org>","List-Help":"<mailto:qemu-devel-request@nongnu.org?subject=help>","List-Subscribe":"<https://lists.nongnu.org/mailman/listinfo/qemu-devel>,\n\t<mailto:qemu-devel-request@nongnu.org?subject=subscribe>","Cc":"Michael Roth <mdroth@linux.vnet.ibm.com>, =?utf-8?q?Marc-Andr=C3=A9_Lu?=\n\t=?utf-8?q?reau?= <marcandre.lureau@redhat.com>, armbru@redhat.com,\n\t\"Dr. David Alan Gilbert\" <dgilbert@redhat.com>","Errors-To":"qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org","Sender":"\"Qemu-devel\"\n\t<qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org>"},"content":"Replace the generated json string with a literal qobject. The later is\neasier to deal with, at run time as well as compile time: adding #if\nconditionals will be easier than in a json string.\n\nSigned-off-by: Marc-André Lureau <marcandre.lureau@redhat.com>\n---\n scripts/qapi-introspect.py         | 83 ++++++++++++++++++++++----------------\n monitor.c                          |  2 +-\n tests/test-qobject-input-visitor.c | 11 +++--\n docs/devel/qapi-code-gen.txt       | 29 ++++++++-----\n 4 files changed, 76 insertions(+), 49 deletions(-)","diff":"diff --git a/scripts/qapi-introspect.py b/scripts/qapi-introspect.py\nindex 032bcea491..0002bc1a68 100644\n--- a/scripts/qapi-introspect.py\n+++ b/scripts/qapi-introspect.py\n@@ -12,26 +12,36 @@\n from qapi import *\n \n \n-# Caveman's json.dumps() replacement (we're stuck at Python 2.4)\n-# TODO try to use json.dumps() once we get unstuck\n-def to_json(obj, level=0):\n+def to_qlit(obj, level=0, suppress_first_indent=False):\n+\n+    def indent(level):\n+        return level * 4 * ' '\n+\n+    ret = ''\n+    if not suppress_first_indent:\n+        ret += indent(level)\n     if obj is None:\n-        ret = 'null'\n+        ret += 'QLIT_QNULL'\n     elif isinstance(obj, str):\n-        ret = '\"' + obj.replace('\"', r'\\\"') + '\"'\n+        ret += 'QLIT_QSTR(' + to_c_string(obj) + ')'\n     elif isinstance(obj, list):\n-        elts = [to_json(elt, level + 1)\n+        elts = [to_qlit(elt, level + 1)\n                 for elt in obj]\n-        ret = '[' + ', '.join(elts) + ']'\n+        elts.append(indent(level + 1) + \"{}\")\n+        ret += 'QLIT_QLIST(((QLitObject[]) {\\n'\n+        ret += ',\\n'.join(elts) + '\\n'\n+        ret += indent(level) + '}))'\n     elif isinstance(obj, dict):\n-        elts = ['\"%s\": %s' % (key.replace('\"', r'\\\"'),\n-                              to_json(obj[key], level + 1))\n-                for key in sorted(obj.keys())]\n-        ret = '{' + ', '.join(elts) + '}'\n+        elts = []\n+        for key, value in sorted(obj.iteritems()):\n+            elts.append(indent(level + 1) + '{ %s, %s }' %\n+                        (to_c_string(key), to_qlit(value, level + 1, True)))\n+        elts.append(indent(level + 1) + '{}')\n+        ret += 'QLIT_QDICT(((QLitDictEntry[]) {\\n'\n+        ret += ',\\n'.join(elts) + '\\n'\n+        ret += indent(level) + '}))'\n     else:\n         assert False                # not implemented\n-    if level == 1:\n-        ret = '\\n' + ret\n     return ret\n \n \n@@ -45,39 +55,37 @@ class QAPISchemaGenIntrospectVisitor(QAPISchemaVisitor):\n         self.defn = None\n         self.decl = None\n         self._schema = None\n-        self._jsons = None\n+        self._qlits = None\n         self._used_types = None\n         self._name_map = None\n \n     def visit_begin(self, schema):\n         self._schema = schema\n-        self._jsons = []\n+        self._qlits = []\n         self._used_types = []\n         self._name_map = {}\n \n     def visit_end(self):\n         # visit the types that are actually used\n-        jsons = self._jsons\n-        self._jsons = []\n+        qlits = self._qlits\n+        self._qlits = []\n         for typ in self._used_types:\n             typ.visit(self)\n         # generate C\n         # TODO can generate awfully long lines\n-        jsons.extend(self._jsons)\n-        name = c_name(prefix, protect=False) + 'qmp_schema_json'\n+        qlits.extend(self._qlits)\n+        name = c_name(prefix, protect=False) + 'qmp_schema_qlit'\n         self.decl = mcgen('''\n-extern const char %(c_name)s[];\n+extern const QLitObject %(c_name)s;\n ''',\n                           c_name=c_name(name))\n-        lines = to_json(jsons).split('\\n')\n-        c_string = '\\n    '.join([to_c_string(line) for line in lines])\n         self.defn = mcgen('''\n-const char %(c_name)s[] = %(c_string)s;\n+const QLitObject %(c_name)s = %(c_string)s;\n ''',\n                           c_name=c_name(name),\n-                          c_string=c_string)\n+                          c_string=to_qlit(qlits))\n         self._schema = None\n-        self._jsons = None\n+        self._qlits = None\n         self._used_types = None\n         self._name_map = None\n \n@@ -111,12 +119,12 @@ const char %(c_name)s[] = %(c_string)s;\n             return '[' + self._use_type(typ.element_type) + ']'\n         return self._name(typ.name)\n \n-    def _gen_json(self, name, mtype, obj):\n+    def _gen_qlit(self, name, mtype, obj):\n         if mtype not in ('command', 'event', 'builtin', 'array'):\n             name = self._name(name)\n         obj['name'] = name\n         obj['meta-type'] = mtype\n-        self._jsons.append(obj)\n+        self._qlits.append(obj)\n \n     def _gen_member(self, member):\n         ret = {'name': member.name, 'type': self._use_type(member.type)}\n@@ -132,24 +140,24 @@ const char %(c_name)s[] = %(c_string)s;\n         return {'case': variant.name, 'type': self._use_type(variant.type)}\n \n     def visit_builtin_type(self, name, info, json_type):\n-        self._gen_json(name, 'builtin', {'json-type': json_type})\n+        self._gen_qlit(name, 'builtin', {'json-type': json_type})\n \n     def visit_enum_type(self, name, info, values, prefix):\n-        self._gen_json(name, 'enum', {'values': values})\n+        self._gen_qlit(name, 'enum', {'values': values})\n \n     def visit_array_type(self, name, info, element_type):\n         element = self._use_type(element_type)\n-        self._gen_json('[' + element + ']', 'array', {'element-type': element})\n+        self._gen_qlit('[' + element + ']', 'array', {'element-type': element})\n \n     def visit_object_type_flat(self, name, info, members, variants):\n         obj = {'members': [self._gen_member(m) for m in members]}\n         if variants:\n             obj.update(self._gen_variants(variants.tag_member.name,\n                                           variants.variants))\n-        self._gen_json(name, 'object', obj)\n+        self._gen_qlit(name, 'object', obj)\n \n     def visit_alternate_type(self, name, info, variants):\n-        self._gen_json(name, 'alternate',\n+        self._gen_qlit(name, 'alternate',\n                        {'members': [{'type': self._use_type(m.type)}\n                                     for m in variants.variants]})\n \n@@ -157,13 +165,13 @@ const char %(c_name)s[] = %(c_string)s;\n                       gen, success_response, boxed):\n         arg_type = arg_type or self._schema.the_empty_object_type\n         ret_type = ret_type or self._schema.the_empty_object_type\n-        self._gen_json(name, 'command',\n+        self._gen_qlit(name, 'command',\n                        {'arg-type': self._use_type(arg_type),\n                         'ret-type': self._use_type(ret_type)})\n \n     def visit_event(self, name, info, arg_type, boxed):\n         arg_type = arg_type or self._schema.the_empty_object_type\n-        self._gen_json(name, 'event', {'arg-type': self._use_type(arg_type)})\n+        self._gen_qlit(name, 'event', {'arg-type': self._use_type(arg_type)})\n \n # Debugging aid: unmask QAPI schema's type names\n # We normally mask them, because they're not QMP wire ABI\n@@ -205,11 +213,18 @@ h_comment = '''\n \n fdef.write(mcgen('''\n #include \"qemu/osdep.h\"\n+#include \"qapi/qmp/qlit.h\"\n #include \"%(prefix)sqmp-introspect.h\"\n \n ''',\n                  prefix=prefix))\n \n+fdecl.write(mcgen('''\n+#include \"qemu/osdep.h\"\n+#include \"qapi/qmp/qlit.h\"\n+\n+'''))\n+\n schema = QAPISchema(input_file)\n gen = QAPISchemaGenIntrospectVisitor(opt_unmask)\n schema.visit(gen)\ndiff --git a/monitor.c b/monitor.c\nindex 9239f7adde..5685697f59 100644\n--- a/monitor.c\n+++ b/monitor.c\n@@ -953,7 +953,7 @@ EventInfoList *qmp_query_events(Error **errp)\n static void qmp_query_qmp_schema(QDict *qdict, QObject **ret_data,\n                                  Error **errp)\n {\n-    *ret_data = qobject_from_json(qmp_schema_json, &error_abort);\n+    *ret_data = qobject_from_qlit(&qmp_schema_qlit);\n }\n \n /*\ndiff --git a/tests/test-qobject-input-visitor.c b/tests/test-qobject-input-visitor.c\nindex fe591814e4..7c1d10e274 100644\n--- a/tests/test-qobject-input-visitor.c\n+++ b/tests/test-qobject-input-visitor.c\n@@ -1247,24 +1247,27 @@ static void test_visitor_in_fail_alternate(TestInputVisitorData *data,\n }\n \n static void do_test_visitor_in_qmp_introspect(TestInputVisitorData *data,\n-                                              const char *schema_json)\n+                                              const QLitObject *qlit)\n {\n     SchemaInfoList *schema = NULL;\n+    QObject *obj = qobject_from_qlit(qlit);\n     Visitor *v;\n \n-    v = visitor_input_test_init_raw(data, schema_json);\n+    v = qobject_input_visitor_new(obj);\n \n     visit_type_SchemaInfoList(v, NULL, &schema, &error_abort);\n     g_assert(schema);\n \n     qapi_free_SchemaInfoList(schema);\n+    qobject_decref(obj);\n+    visit_free(v);\n }\n \n static void test_visitor_in_qmp_introspect(TestInputVisitorData *data,\n                                            const void *unused)\n {\n-    do_test_visitor_in_qmp_introspect(data, test_qmp_schema_json);\n-    do_test_visitor_in_qmp_introspect(data, qmp_schema_json);\n+    do_test_visitor_in_qmp_introspect(data, &test_qmp_schema_qlit);\n+    do_test_visitor_in_qmp_introspect(data, &qmp_schema_qlit);\n }\n \n int main(int argc, char **argv)\ndiff --git a/docs/devel/qapi-code-gen.txt b/docs/devel/qapi-code-gen.txt\nindex f04c63fe82..0a90f2278a 100644\n--- a/docs/devel/qapi-code-gen.txt\n+++ b/docs/devel/qapi-code-gen.txt\n@@ -1327,18 +1327,27 @@ Example:\n     #ifndef EXAMPLE_QMP_INTROSPECT_H\n     #define EXAMPLE_QMP_INTROSPECT_H\n \n-    extern const char example_qmp_schema_json[];\n+    extern const QLitObject qmp_schema_qlit;\n \n     #endif\n     $ cat qapi-generated/example-qmp-introspect.c\n [Uninteresting stuff omitted...]\n \n-    const char example_qmp_schema_json[] = \"[\"\n-        \"{\\\"arg-type\\\": \\\"0\\\", \\\"meta-type\\\": \\\"event\\\", \\\"name\\\": \\\"MY_EVENT\\\"}, \"\n-        \"{\\\"arg-type\\\": \\\"1\\\", \\\"meta-type\\\": \\\"command\\\", \\\"name\\\": \\\"my-command\\\", \\\"ret-type\\\": \\\"2\\\"}, \"\n-        \"{\\\"members\\\": [], \\\"meta-type\\\": \\\"object\\\", \\\"name\\\": \\\"0\\\"}, \"\n-        \"{\\\"members\\\": [{\\\"name\\\": \\\"arg1\\\", \\\"type\\\": \\\"[2]\\\"}], \\\"meta-type\\\": \\\"object\\\", \\\"name\\\": \\\"1\\\"}, \"\n-        \"{\\\"members\\\": [{\\\"name\\\": \\\"integer\\\", \\\"type\\\": \\\"int\\\"}, {\\\"default\\\": null, \\\"name\\\": \\\"string\\\", \\\"type\\\": \\\"str\\\"}], \\\"meta-type\\\": \\\"object\\\", \\\"name\\\": \\\"2\\\"}, \"\n-        \"{\\\"element-type\\\": \\\"2\\\", \\\"meta-type\\\": \\\"array\\\", \\\"name\\\": \\\"[2]\\\"}, \"\n-        \"{\\\"json-type\\\": \\\"int\\\", \\\"meta-type\\\": \\\"builtin\\\", \\\"name\\\": \\\"int\\\"}, \"\n-        \"{\\\"json-type\\\": \\\"string\\\", \\\"meta-type\\\": \\\"builtin\\\", \\\"name\\\": \\\"str\\\"}]\";\n+    const QLitObject example_qmp_schema_qlit = QLIT_QLIST(((QLitObject[]) {\n+        QLIT_QDICT(((QLitDictEntry[]) {\n+            { \"arg-type\", QLIT_QSTR(\"0\") },\n+            { \"meta-type\", QLIT_QSTR(\"event\") },\n+            { \"name\", QLIT_QSTR(\"Event\") },\n+            { }\n+        })),\n+        QLIT_QDICT(((QLitDictEntry[]) {\n+            { \"members\", QLIT_QLIST(((QLitObject[]) {\n+                { }\n+            })) },\n+            { \"meta-type\", QLIT_QSTR(\"object\") },\n+            { \"name\", QLIT_QSTR(\"0\") },\n+            { }\n+        })),\n+        ...\n+        { }\n+    }));\n","prefixes":["v3","02/50"]}