qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: Markus Armbruster <armbru@redhat.com>
To: qemu-devel@nongnu.org
Cc: mdroth@linux.vnet.ibm.com
Subject: [Qemu-devel] [PATCH RFC v5 32/32] qapi-introspect: Hide type names
Date: Mon,  7 Sep 2015 12:16:43 +0200	[thread overview]
Message-ID: <1441621003-2434-33-git-send-email-armbru@redhat.com> (raw)
In-Reply-To: <1441621003-2434-1-git-send-email-armbru@redhat.com>

To eliminate the temptation for clients to look up types by name
(which are not ABI), replace all type names by meaningless strings.

Reduces output of query-schema by 13 out of 85KiB.

As a debugging aid, provide option -u to suppress the hiding.

Signed-off-by: Markus Armbruster <armbru@redhat.com>
Reviewed-by: Eric Blake <eblake@redhat.com>
---
 docs/qapi-code-gen.txt     | 36 +++++++++++++++++-------------------
 qapi/introspect.json       | 11 +++--------
 scripts/qapi-introspect.py | 41 +++++++++++++++++++++++++++++++++++------
 3 files changed, 55 insertions(+), 33 deletions(-)

diff --git a/docs/qapi-code-gen.txt b/docs/qapi-code-gen.txt
index 458b217..01e292b 100644
--- a/docs/qapi-code-gen.txt
+++ b/docs/qapi-code-gen.txt
@@ -522,13 +522,16 @@ additional variant members depending on the value of meta-type.
 Each SchemaInfo object describes a wire ABI entity of a certain
 meta-type: a command, event or one of several kinds of type.
 
-SchemaInfo for entities defined in the QAPI schema have the same name
-as in the schema.  This is the case for all commands and events, and
-most types.
+SchemaInfo for commands and events have the same name as in the QAPI
+schema.
 
 Command and event names are part of the wire ABI, but type names are
-not.  Therefore, looking up a type by its name in the QAPI schema is
-wrong.  Look up the command or event, then follow references by name.
+not.  Therefore, the SchemaInfo for types have auto-generated
+meaningless names.  For readability, the examples in this section use
+meaningful type names instead.
+
+To examine a type, start with a command or event using it, then follow
+references by name.
 
 QAPI schema definitions not reachable that way are omitted.
 
@@ -559,8 +562,7 @@ object type without members.  The event may not have a data member on
 the wire then.
 
 Each command or event defined with dictionary-valued 'data' in the
-QAPI schema implicitly defines an object type called ":obj-NAME-arg",
-where NAME is the command or event's name.
+QAPI schema implicitly defines an object type.
 
 Example: the SchemaInfo for EVENT_C from section Events
 
@@ -615,12 +617,9 @@ Note that base types are "flattened": its members are included in the
 
 A simple union implicitly defines an enumeration type for its implicit
 discriminator (called "type" on the wire, see section Union types).
-Such a type's name is made by appending "Kind" to the simple union's
-name.
 
 A simple union implicitly defines an object type for each of its
-variants.  The type's name is ":obj-NAME-wrapper", where NAME is the
-name of the name of the variant's type.
+variants.
 
 Example: the SchemaInfo for simple union BlockdevOptions from section
 Union types
@@ -651,8 +650,7 @@ Example: the SchemaInfo for BlockRef from section Alternate types
 
 The SchemaInfo for an array type has meta-type "array", and variant
 member "element-type", which names the array's element type.  Array
-types are implicitly defined.  An array type's name is made by
-appending "List" to its element type's name.
+types are implicitly defined.
 
 Example: the SchemaInfo for ['str']
 
@@ -1059,13 +1057,13 @@ Example:
 [Uninteresting stuff omitted...]
 
     const char example_qmp_schema_json[] = "["
-        "{\"arg-type\": \":empty\", \"meta-type\": \"event\", \"name\": \"MY_EVENT\"}, "
+        "{\"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\"}, {\"name\": \"string\", \"type\": \"str\"}], \"meta-type\": \"object\", \"name\": \"2\"}, "
         "{\"json-type\": \"int\", \"meta-type\": \"builtin\", \"name\": \"int\"}, "
-        "{\"json-type\": \"string\", \"meta-type\": \"builtin\", \"name\": \"str\"}, "
-        "{\"members\": [], \"meta-type\": \"object\", \"name\": \":empty\"}, "
-        "{\"members\": [{\"name\": \"arg1\", \"type\": \"UserDefOne\"}], \"meta-type\": \"object\", \"name\": \":obj-my-command-arg\"}, "
-        "{\"members\": [{\"name\": \"integer\", \"type\": \"int\"}, {\"name\": \"string\", \"type\": \"str\"}], \"meta-type\": \"object\", \"name\": \"UserDefOne\"}, "
-        "{\"arg-type\": \":obj-my-command-arg\", \"meta-type\": \"command\", \"name\": \"my-command\", \"ret-type\": \"UserDefOne\"}]";
+        "{\"json-type\": \"string\", \"meta-type\": \"builtin\", \"name\": \"str\"}]";
     $ cat qapi-generated/example-qmp-introspect.h
 [Uninteresting stuff omitted...]
 
diff --git a/qapi/introspect.json b/qapi/introspect.json
index 5b4713b..d6faa11 100644
--- a/qapi/introspect.json
+++ b/qapi/introspect.json
@@ -75,17 +75,12 @@
 # @SchemaInfo
 #
 # @name: the entity's name, inherited from @base.
-# Entities defined in the QAPI schema have the name defined in the schema.
-# Implicitly defined entities have generated names.  See
-# docs/qapi-code-gen.txt section "Client JSON Protocol introspection"
-# for details.
+# Commands and events have the name defined in the QAPI schema.
+# Unlike command and event names, type names are not part of the wire
+# ABI.  Consequently, type names are meaningless strings here.
 #
 # All references to other SchemaInfo are by name.
 #
-# Command and event names are part of the wire ABI, but type names are
-# not.  Therefore, looking up a type by "well-known" name is wrong.
-# Look up the command or event, then follow the references.
-#
 # @meta-type: the entity's meta type, inherited from @base.
 #
 # Additional members depend on the value of @meta-type.
diff --git a/scripts/qapi-introspect.py b/scripts/qapi-introspect.py
index 1bc26f0..ce98321 100644
--- a/scripts/qapi-introspect.py
+++ b/scripts/qapi-introspect.py
@@ -40,32 +40,37 @@ def to_c_string(string):
 
 
 class QAPISchemaGenIntrospectVisitor(QAPISchemaVisitor):
-    def __init__(self):
+    def __init__(self, unmask):
+        self._unmask = unmask
         self.defn = None
         self.decl = None
         self._schema = None
         self._jsons = None
         self._used_types = None
+        self._name_map = None
 
     def visit_begin(self, schema):
         self._schema = schema
         self._jsons = []
         self._used_types = []
+        self._name_map = {}
         return QAPISchemaType   # don't visit types for now
 
     def visit_end(self):
         # visit the types that are actually used
+        jsons = self._jsons
+        self._jsons = []
         for typ in self._used_types:
             typ.visit(self)
-        self._jsons.sort()
         # generate C
         # TODO can generate awfully long lines
+        jsons.extend(self._jsons)
         name = prefix + 'qmp_schema_json'
         self.decl = mcgen('''
 extern const char %(c_name)s[];
 ''',
                           c_name=c_name(name))
-        lines = to_json(self._jsons).split('\n')
+        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;
@@ -75,6 +80,14 @@ const char %(c_name)s[] = %(c_string)s;
         self._schema = None
         self._jsons = None
         self._used_types = None
+        self._name_map = None
+
+    def _name(self, name):
+        if self._unmask:
+            return name
+        if name not in self._name_map:
+            self._name_map[name] = '%d' % len(self._name_map)
+        return self._name_map[name]
 
     def _use_type(self, typ):
         # Map the various integer types to plain int
@@ -86,9 +99,16 @@ const char %(c_name)s[] = %(c_string)s;
         # Add type to work queue if new
         if typ not in self._used_types:
             self._used_types.append(typ)
-        return typ.name
+        # Clients should examine commands and events, not types.  Hide
+        # type names to reduce the temptation.  Also saves a few
+        # characters.
+        if isinstance(typ, QAPISchemaBuiltinType):
+            return typ.name
+        return self._name(typ.name)
 
     def _gen_json(self, name, mtype, obj):
+        if mtype != 'command' and mtype != 'event' and mtype != 'builtin':
+            name = self._name(name)
         obj['name'] = name
         obj['meta-type'] = mtype
         self._jsons.append(obj)
@@ -140,7 +160,16 @@ const char %(c_name)s[] = %(c_string)s;
         arg_type = arg_type or self._schema.the_empty_object_type
         self._gen_json(name, 'event', {'arg-type': self._use_type(arg_type)})
 
-(input_file, output_dir, do_c, do_h, prefix, dummy) = parse_command_line()
+# Debugging aid: unmask QAPI schema's type names
+# We normally mask them, because they're not QMP wire ABI
+opt_unmask = False
+
+(input_file, output_dir, do_c, do_h, prefix, opts) = \
+    parse_command_line("u", ["unmask-non-abi-names"])
+
+for o, a in opts:
+    if o in ("-u", "--unmask-non-abi-names"):
+        opt_unmask = True
 
 c_comment = '''
 /*
@@ -176,7 +205,7 @@ fdef.write(mcgen('''
                  prefix=prefix))
 
 schema = QAPISchema(input_file)
-gen = QAPISchemaGenIntrospectVisitor()
+gen = QAPISchemaGenIntrospectVisitor(opt_unmask)
 schema.visit(gen)
 fdef.write(gen.defn)
 fdecl.write(gen.decl)
-- 
2.4.3

  parent reply	other threads:[~2015-09-07 10:17 UTC|newest]

Thread overview: 49+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-09-07 10:16 [Qemu-devel] [PATCH RFC v5 00/32] qapi: QMP introspection Markus Armbruster
2015-09-07 10:16 ` [Qemu-devel] [PATCH RFC v5 01/32] qapi: Rename class QAPISchema to QAPISchemaParser Markus Armbruster
2015-09-07 10:16 ` [Qemu-devel] [PATCH RFC v5 02/32] qapi: New QAPISchema intermediate reperesentation Markus Armbruster
2015-09-08  3:38   ` Eric Blake
2015-09-09  7:47     ` Markus Armbruster
2015-09-07 10:16 ` [Qemu-devel] [PATCH RFC v5 03/32] qapi: QAPISchema code generation helper methods Markus Armbruster
2015-09-07 10:16 ` [Qemu-devel] [PATCH RFC v5 04/32] qapi: New QAPISchemaVisitor Markus Armbruster
2015-09-07 10:16 ` [Qemu-devel] [PATCH RFC v5 05/32] tests/qapi-schema: Convert test harness to QAPISchemaVisitor Markus Armbruster
2015-09-07 10:16 ` [Qemu-devel] [PATCH RFC v5 06/32] qapi: Split up some typedefs to ease review Markus Armbruster
2015-09-08 12:25   ` Eric Blake
2015-09-07 10:16 ` [Qemu-devel] [PATCH RFC v5 07/32] qapi: Generate comments to simplify splitting for review Markus Armbruster
2015-09-07 10:16 ` [Qemu-devel] [PATCH RFC v5 08/32] Revert "qapi: Generate comments to simplify splitting for review" Markus Armbruster
2015-09-07 10:16 ` [Qemu-devel] [PATCH RFC v5 09/32] Revert "qapi: Split up some typedefs to ease review" Markus Armbruster
2015-09-08 12:26   ` Eric Blake
2015-09-08 12:58     ` Markus Armbruster
2015-09-07 10:16 ` [Qemu-devel] [PATCH RFC v5 10/32] qapi-types: Convert to QAPISchemaVisitor, fixing flat unions Markus Armbruster
2015-09-07 10:16 ` [Qemu-devel] [PATCH RFC v5 11/32] qapi-visit: Convert to QAPISchemaVisitor, fixing bugs Markus Armbruster
2015-09-07 10:16 ` [Qemu-devel] [PATCH RFC v5 12/32] qapi-commands: Convert to QAPISchemaVisitor Markus Armbruster
2015-09-07 10:16 ` [Qemu-devel] [PATCH RFC v5 13/32] qapi: De-duplicate enum code generation Markus Armbruster
2015-09-07 10:16 ` [Qemu-devel] [PATCH RFC v5 14/32] qapi-event: Eliminate global variable event_enum_value Markus Armbruster
2015-09-07 10:16 ` [Qemu-devel] [PATCH RFC v5 15/32] qapi-event: Convert to QAPISchemaVisitor, fixing data with base Markus Armbruster
2015-09-07 10:16 ` [Qemu-devel] [PATCH RFC v5 16/32] qapi: Generate comments to simplify splitting for review Markus Armbruster
2015-09-07 10:16 ` [Qemu-devel] [PATCH RFC v5 17/32] Revert "qapi: Generate comments to simplify splitting for review" Markus Armbruster
2015-09-07 10:16 ` [Qemu-devel] [PATCH RFC v5 18/32] qapi: Replace dirty is_c_ptr() by method c_null() Markus Armbruster
2015-09-07 10:16 ` [Qemu-devel] [PATCH RFC v5 19/32] qapi: Clean up after recent conversions to QAPISchemaVisitor Markus Armbruster
2015-09-07 10:16 ` [Qemu-devel] [PATCH RFC v5 20/32] qapi-visit: Rearrange code a bit Markus Armbruster
2015-09-07 10:16 ` [Qemu-devel] [PATCH RFC v5 21/32] qapi-commands: Rearrange code Markus Armbruster
2015-09-07 10:16 ` [Qemu-devel] [PATCH RFC v5 22/32] qapi: Rename qmp_marshal_input_FOO() to qmp_marshal_FOO() Markus Armbruster
2015-09-08 14:50   ` Eric Blake
2015-09-07 10:16 ` [Qemu-devel] [PATCH RFC v5 23/32] qapi: De-duplicate parameter list generation Markus Armbruster
2015-09-07 10:16 ` [Qemu-devel] [PATCH RFC v5 24/32] qapi-commands: De-duplicate output marshaling functions Markus Armbruster
2015-09-07 10:16 ` [Qemu-devel] [PATCH RFC v5 25/32] qapi: Improve built-in type documentation Markus Armbruster
2015-09-07 10:16 ` [Qemu-devel] [PATCH RFC v5 26/32] qapi: Introduce a first class 'any' type Markus Armbruster
2015-09-07 10:16 ` [Qemu-devel] [PATCH RFC v5 27/32] qom: Don't use 'gen': false for qom-get, qom-set, object-add Markus Armbruster
2015-09-07 10:16 ` [Qemu-devel] [PATCH RFC v5 28/32] qapi-schema: Fix up misleading specification of netdev_add Markus Armbruster
2015-09-07 10:16 ` [Qemu-devel] [PATCH RFC v5 29/32] qapi: Pseudo-type '**' is now unused, drop it Markus Armbruster
2015-09-07 10:16 ` [Qemu-devel] [PATCH RFC v5 30/32] qapi: New QMP command query-qmp-schema for QMP introspection Markus Armbruster
2015-09-08 16:11   ` Eric Blake
2015-09-08 18:48     ` Markus Armbruster
2015-09-08 20:06   ` Eric Blake
2015-09-09  6:31     ` Markus Armbruster
2015-09-10 22:12   ` Michael Roth
2015-09-11  7:02     ` Markus Armbruster
2015-09-11  8:33       ` Daniel P. Berrange
2015-09-11 13:30         ` Markus Armbruster
2015-09-07 10:16 ` [Qemu-devel] [PATCH RFC v5 31/32] qapi-introspect: Map all integer types to 'int' Markus Armbruster
2015-09-07 10:16 ` Markus Armbruster [this message]
2015-09-11  7:06 ` [Qemu-devel] [PATCH RFC v5 00/32] qapi: QMP introspection Markus Armbruster
2015-09-15 18:13 ` Markus Armbruster

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=1441621003-2434-33-git-send-email-armbru@redhat.com \
    --to=armbru@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).