Skip to contentSkip to conversation

qapi/command: Avoid generating unused qmp_marshal_output_T()

PATCH In review Markus Armbruster 2 participants 3 messages Aug 4, 2025 – Nov 4, 2025

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 qapi/command: Avoid generating unused qmp_marshal_output_T()

qmp_marshal_output_T() is only ever called by qmp_marshal_C() for a command C that returns type T.

We've always generated it as a static function on demand, i.e. when we generate a call.

Since we split up monolithic generated code into modules (commit 252dc3105fc "qapi: Generate separate .h, .c for each module"), we do this per module. As noted in the commit message, this can result in identical (static) qmp_marshal_output_T() in several modules. Was deemed not worth avoiding.

A bit later, we added 'if' conditionals to the schema language (merge commit 5dafaf4fbce).

When a conditional definition uses a type, then its condition must imply the type's condition. We made this the user's responsibility. Hasn't been an issue in practice.

However, the sharing of qmp_marshal_output_T() among commands complicates matters. To avoid both undefined function errors and unused function warnings, qmp_marshal_output_T() must be defined exactly when it's used. It is used when any of the qmp_marshal_C() calling it is defined, i.e. when any C's condition holds.

The generator uses T's condition instead. To avoid both error and warning, T's condition must be the conjunction of all C's conditions.

Unfortunately, this can be impossible:

* Conditional command returning a builtin type

A builtin type cannot be conditional. This is noted in a FIXME comment.

* Commands in multiple modules where the conjunction differs between modules

An instance of this came up recenrly. we have unconditional commands returning HumanReadableText. If we add a conditional one to a module that does not have unconditional ones, compilation fails with "defined but not used". If we make HumanReadableText conditional to fix this module, we break the others.

Instead of complicating the code to compute the conjunction, simplify
it: generate the output marshalling code right into qmp_marshal_C().

This duplicates it when multiple commands return the same type. The impact on code size is negligible: qemu-system-x86_64's text segment grows by 1448 bytes.

---
 docs/devel/qapi-code-gen.rst | 25 ++++++++------------
 scripts/qapi/commands.py     | 44 ++++++++----------------------------
 2 files changed, 19 insertions(+), 50 deletions(-)
docs/devel/qapi-code-gen.rst +9 -16
index dfdbeac5a5..091e4f6f4a 100644
@@ -1804,27 +1804,13 @@ Example::
     $ cat qapi-generated/example-qapi-commands.c
     [Uninteresting stuff omitted...]
 
-    static void qmp_marshal_output_UserDefOne(UserDefOne *ret_in,
-                                    QObject **ret_out, Error **errp)
-    {
-        Visitor *v;
-
-        v = qobject_output_visitor_new_qmp(ret_out);
-        if (visit_type_UserDefOne(v, "unused", &ret_in, errp)) {
-            visit_complete(v, ret_out);
-        }
-        visit_free(v);
-        v = qapi_dealloc_visitor_new();
-        visit_type_UserDefOne(v, "unused", &ret_in, NULL);
-        visit_free(v);
-    }
-
     void qmp_marshal_my_command(QDict *args, QObject **ret, Error **errp)
     {
         Error *err = NULL;
         bool ok = false;
         Visitor *v;
         UserDefOne *retval;
+        Visitor *ov;
         q_obj_my_command_arg arg = {0};
 
         v = qobject_input_visitor_new_qmp(QOBJECT(args));
@@ -1852,7 +1838,14 @@ Example::
             goto out;
         }
 
-        qmp_marshal_output_UserDefOne(retval, ret, errp);
+        ov = qobject_output_visitor_new_qmp(ret);
+        if (visit_type_UserDefOne(ov, "unused", &retval, errp)) {
+            visit_complete(ov, ret);
+        }
+        visit_free(ov);
+        ov = qapi_dealloc_visitor_new();
+        visit_type_UserDefOne(ov, "unused", &retval, NULL);
+        visit_free(ov);
 
         if (trace_event_get_state_backends(TRACE_QMP_EXIT_MY_COMMAND)) {
             g_autoptr(GString) ret_json = qobject_to_json(*ret);
scripts/qapi/commands.py +10 -34

91 lines not on this page · raw message

Signature · 1 line
2.49.0
Reviewed-by on v1 1/1
#1 lore raw
Quoted 51 lines · Markus Armbruster

qmp_marshal_output_T() is only ever called by qmp_marshal_C() for a command C that returns type T.

We've always generated it as a static function on demand, i.e. when we generate a call.

Since we split up monolithic generated code into modules (commit 252dc3105fc "qapi: Generate separate .h, .c for each module"), we do this per module. As noted in the commit message, this can result in identical (static) qmp_marshal_output_T() in several modules. Was deemed not worth avoiding.

A bit later, we added 'if' conditionals to the schema language (merge commit 5dafaf4fbce).

When a conditional definition uses a type, then its condition must imply the type's condition. We made this the user's responsibility. Hasn't been an issue in practice.

However, the sharing of qmp_marshal_output_T() among commands complicates matters. To avoid both undefined function errors and unused function warnings, qmp_marshal_output_T() must be defined exactly when it's used. It is used when any of the qmp_marshal_C() calling it is defined, i.e. when any C's condition holds.

The generator uses T's condition instead. To avoid both error and warning, T's condition must be the conjunction of all C's conditions.

Unfortunately, this can be impossible:

* Conditional command returning a builtin type

A builtin type cannot be conditional. This is noted in a FIXME comment.

* Commands in multiple modules where the conjunction differs between modules

An instance of this came up recenrly. we have unconditional commands returning HumanReadableText. If we add a conditional one to a module that does not have unconditional ones, compilation fails with "defined but not used". If we make HumanReadableText conditional to fix this module, we break the others.

Instead of complicating the code to compute the conjunction, simplify
it: generate the output marshalling code right into qmp_marshal_C().

This duplicates it when multiple commands return the same type. The impact on code size is negligible: qemu-system-x86_64's text segment grows by 1448 bytes.

Quoted 5 lines
---
  docs/devel/qapi-code-gen.rst | 25 ++++++++------------
  scripts/qapi/commands.py     | 44 ++++++++----------------------------
  2 files changed, 19 insertions(+), 50 deletions(-)