qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: "Marc-André Lureau" <marcandre.lureau@redhat.com>
To: Markus Armbruster <armbru@redhat.com>
Cc: qemu-devel@nongnu.org, "Beraldo Leal" <bleal@redhat.com>,
	"Eric Blake" <eblake@redhat.com>, "Stefan Weil" <sw@weilnetz.de>,
	"Alex Bennée" <alex.bennee@linaro.org>,
	"Paolo Bonzini" <pbonzini@redhat.com>,
	"Laurent Vivier" <lvivier@redhat.com>,
	"Dr. David Alan Gilbert" <dgilbert@redhat.com>,
	"Gerd Hoffmann" <kraxel@redhat.com>,
	"Michael Roth" <michael.roth@amd.com>,
	"Philippe Mathieu-Daudé" <philmd@linaro.org>,
	"Daniel P. Berrangé" <berrange@redhat.com>,
	"Thomas Huth" <thuth@redhat.com>,
	"Wainer dos Santos Moschetta" <wainersm@redhat.com>
Subject: Re: [PATCH v3 07/10] qapi: implement conditional command arguments
Date: Sat, 18 Feb 2023 14:45:19 +0400	[thread overview]
Message-ID: <CAMxuvax6qPYQCzNX7vESJM9_f5k4C1Yat0sJcJjrHkh_1WGpQA@mail.gmail.com> (raw)
In-Reply-To: <87fsb4k85h.fsf@pond.sub.org>

[-- Attachment #1: Type: text/plain, Size: 7393 bytes --]

Hi Markus

On Fri, Feb 17, 2023 at 12:28 PM Markus Armbruster <armbru@redhat.com>
wrote:

> marcandre.lureau@redhat.com writes:
>
> > From: Marc-André Lureau <marcandre.lureau@redhat.com>
> >
> > The generated code doesn't quite handle the conditional arguments.
> > For example, 'bar' in 'test-if-cmd' is not correctly surrounded by #if
> > conditions. See generated code in qmp_marshal_test_if_cmd().
> >
> > Note that if there are multiple optional arguments at the last position,
> > there might be compilation issues due to extra comas. I left an assert
> > and FIXME for later.
> >
> > Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com>
> > ---
> >  scripts/qapi/commands.py                |  4 ++++
> >  scripts/qapi/gen.py                     | 19 ++++++++++++++-----
> >  scripts/qapi/visit.py                   |  2 ++
> >  tests/qapi-schema/qapi-schema-test.json |  3 ++-
> >  4 files changed, 22 insertions(+), 6 deletions(-)
> >
> > diff --git a/scripts/qapi/commands.py b/scripts/qapi/commands.py
> > index 79c5e5c3a9..07997d1586 100644
> > --- a/scripts/qapi/commands.py
> > +++ b/scripts/qapi/commands.py
> > @@ -64,9 +64,13 @@ def gen_call(name: str,
> >      elif arg_type:
> >          assert not arg_type.variants
> >          for memb in arg_type.members:
> > +            if memb.ifcond.is_present():
> > +                argstr += '\n' + memb.ifcond.gen_if()
> >              if memb.need_has():
> >                  argstr += 'arg.has_%s, ' % c_name(memb.name)
> >              argstr += 'arg.%s, ' % c_name(memb.name)
> > +            if memb.ifcond.is_present():
> > +                argstr += '\n' + memb.ifcond.gen_endif()
> >
> >      lhs = ''
> >      if ret_type:
>
> @argstr is emitted further down:
>
>        %(lhs)sqmp_%(name)s(%(args)s&err);
>    ''',
>                     name=name, args=argstr, lhs=lhs)
>
>        ret += mcgen('''
>        if (err) {
>    ''')
>
> Before the patch, @argstr contains no newlines.  Works.
>
> After the patch, it may contain newlines, and if it does, intentation is
> messed up.  For instance, in the code generated for
> qapi-schema-test.json:
>
>         retval = qmp_test_if_cmd(arg.foo,
>     #if defined(TEST_IF_CMD_BAR)
>     arg.bar,
>     #endif /* defined(TEST_IF_CMD_BAR) */
>     &err);
>
> Strings interpolated into the mcgen() argument should not contain
> newlines.  I'm afraid you have to rewrite the code emitting the call.
>

Why it should not contain newlines?

What are you asking exactly? that the caller be changed? (this does not
work well if there are multiple optional arguments..)

    #if defined(TEST_IF_CMD_BAR)
        retval = qmp_test_if_cmd(arg.foo, arg.bar, &err);
    #else
        retval = qmp_test_if_cmd(arg.foo, &err);
    #endif /* defined(TEST_IF_CMD_BAR) */


>
> > diff --git a/scripts/qapi/gen.py b/scripts/qapi/gen.py
> > index b5a8d03e8e..ba57e72c9b 100644
> > --- a/scripts/qapi/gen.py
> > +++ b/scripts/qapi/gen.py
> > @@ -111,22 +111,31 @@ def build_params(arg_type:
> Optional[QAPISchemaObjectType],
> >                   boxed: bool,
> >                   extra: Optional[str] = None) -> str:
> >      ret = ''
> > -    sep = ''
> >      if boxed:
> >          assert arg_type
> >          ret += '%s arg' % arg_type.c_param_type()
> > -        sep = ', '
> > +        if extra:
> > +            ret += ', '
> >      elif arg_type:
> >          assert not arg_type.variants
> > +        n = 0
> >          for memb in arg_type.members:
> > -            ret += sep
> > -            sep = ', '
> > +            n += 1
> > +            if memb.ifcond.is_present():
> > +                ret += '\n' + memb.ifcond.gen_if()
> >              if memb.need_has():
> >                  ret += 'bool has_%s, ' % c_name(memb.name)
> >              ret += '%s %s' % (memb.type.c_param_type(),
> >                                c_name(memb.name))
> > +            if extra or n != len(arg_type.members):
> > +                ret += ', '
> > +            else:
> > +                # FIXME: optional last argument may break compilation
> > +                assert not memb.ifcond.is_present()
>
> Does the assertion guard against the C compilation failure?
>

Yes


>
> Is it possible to write schema code that triggers it?
>

Yes, the one we have for TEST_IF_EVENT for example:

{ 'event': 'TEST_IF_EVENT',
  'data': { 'foo': 'TestIfStruct',
            'bar': { 'type': ['TestIfEnum'], 'if': 'TEST_IF_EVT_BAR' } },

produces:

void qapi_event_send_test_if_event(TestIfStruct *foo,
#if defined(TEST_IF_EVT_BAR)
TestIfEnumList *bar,
#endif /* defined(TEST_IF_EVT_BAR) */
);

Which will fail to compile if TEST_IF_EVT_BAR is undefined.

So I would rather assert that we don't introduce such a schema, until we
fix the code generator. Or we acknowledge the limitation, and treat it as a
schema error. Other ideas?


> > +            if memb.ifcond.is_present():
> > +                ret += '\n' + memb.ifcond.gen_endif()
> >      if extra:
> > -        ret += sep + extra
> > +        ret += extra
> >      return ret if ret else 'void'
> >
> >
>
> Same newline issue as in gen_call().  Generated code:
>
>     UserDefThree *qmp_test_if_cmd(TestIfStruct *foo,
>     #if defined(TEST_IF_CMD_BAR)
>     TestIfEnum bar,
>     #endif /* defined(TEST_IF_CMD_BAR) */
>     Error **errp);
>
> > diff --git a/scripts/qapi/visit.py b/scripts/qapi/visit.py
> > index 26a584ee4c..c56ea4d724 100644
> > --- a/scripts/qapi/visit.py
> > +++ b/scripts/qapi/visit.py
> > @@ -74,11 +74,13 @@ def gen_visit_object_members(name: str,
> >      sep = ''
> >      for memb in members:
> >          if memb.optional and not memb.need_has():
> > +            ret += memb.ifcond.gen_if()
> >              ret += mcgen('''
> >      bool has_%(c_name)s = !!obj->%(c_name)s;
> >  ''',
> >                           c_name=c_name(memb.name))
> >              sep = '\n'
> > +            ret += memb.ifcond.gen_endif()
> >      ret += sep
> >
> >      if base:
>
> This hunk has no effect on the code generated for our schemas as far as
> I can tell.  Is it superfluous?  Incorrect?  Gap in test coverage?  Or
> am I confused?
>
>
Right, we could change the test this way to exercise it:

--- a/tests/qapi-schema/qapi-schema-test.json
+++ b/tests/qapi-schema/qapi-schema-test.json
@@ -250,7 +250,7 @@
 { 'command': 'test-if-cmd',
   'data': {
     'foo': 'TestIfStruct',
-    'bar': { 'type': 'TestIfEnum', 'if': 'TEST_IF_CMD_BAR' } },
+    '*bar': { 'type': 'TestIfStruct', 'if': 'TEST_IF_STRUCT' } },
   'returns': 'UserDefThree',


> > diff --git a/tests/qapi-schema/qapi-schema-test.json
> b/tests/qapi-schema/qapi-schema-test.json
> > index ba7302f42b..baa4e69f63 100644
> > --- a/tests/qapi-schema/qapi-schema-test.json
> > +++ b/tests/qapi-schema/qapi-schema-test.json
> > @@ -258,7 +258,8 @@
> >
> >  { 'event': 'TEST_IF_EVENT',
> >    'data': { 'foo': 'TestIfStruct',
> > -            'bar': { 'type': ['TestIfEnum'], 'if': 'TEST_IF_EVT_BAR' }
> },
> > +            'bar': { 'type': ['TestIfEnum'], 'if': 'TEST_IF_EVT_BAR' },
> > +            'baz': 'int' },
> >    'if': { 'all': ['TEST_IF_EVT', 'TEST_IF_STRUCT'] } }
> >
> >  { 'event': 'TEST_IF_EVENT2', 'data': {},
>
>

[-- Attachment #2: Type: text/html, Size: 10988 bytes --]

  reply	other threads:[~2023-02-18 10:46 UTC|newest]

Thread overview: 54+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-02-07 14:25 [PATCH v3 00/10] Teach 'getfd' QMP command to import win32 sockets marcandre.lureau
2023-02-07 14:25 ` [PATCH v3 01/10] tests: fix path separator, use g_build_filename() marcandre.lureau
2023-02-07 14:25 ` [PATCH v3 02/10] char: do not double-close fd when failing to add client marcandre.lureau
2023-02-07 14:43   ` Thomas Huth
2023-02-07 14:25 ` [PATCH v3 03/10] tests/docker: fix a win32 error due to portability marcandre.lureau
2023-02-27 12:11   ` Alex Bennée
2023-02-07 14:25 ` [PATCH v3 04/10] osdep: implement qemu_socketpair() for win32 marcandre.lureau
2023-02-07 14:25 ` [PATCH v3 05/10] qmp: 'add_client' actually expects sockets marcandre.lureau
2023-02-14 13:25   ` Markus Armbruster
2023-02-07 14:25 ` [PATCH v3 06/10] monitor: release the lock before calling close() marcandre.lureau
2023-02-07 14:52   ` Philippe Mathieu-Daudé
2023-02-14 13:33   ` Markus Armbruster
2023-02-14 13:36     ` Marc-André Lureau
2023-02-14 13:49       ` Daniel P. Berrangé
2023-02-14 16:23         ` Markus Armbruster
2023-02-14 16:55           ` Peter Xu
2023-02-28 18:51         ` Dr. David Alan Gilbert
2023-03-02  9:34   ` Alex Bennée
2023-03-06 15:29     ` Markus Armbruster
2023-03-06 15:35     ` Markus Armbruster
2023-02-07 14:25 ` [PATCH v3 07/10] qapi: implement conditional command arguments marcandre.lureau
2023-02-09 12:41   ` Markus Armbruster
2023-02-12 20:59     ` Marc-André Lureau
2023-02-17  8:28   ` Markus Armbruster
2023-02-18 10:45     ` Marc-André Lureau [this message]
2023-02-20  8:09       ` Markus Armbruster
2023-02-22  8:05         ` Marc-André Lureau
2023-02-22 10:23           ` Markus Armbruster
2023-02-22 10:29             ` Marc-André Lureau
2023-02-27 11:22               ` Marc-André Lureau
2023-02-28 15:58             ` Eric Blake
2023-03-01  9:24               ` Daniel P. Berrangé
2023-03-01 13:16                 ` Markus Armbruster
2023-03-01 13:21                   ` Marc-André Lureau
2023-03-02  6:58                     ` Markus Armbruster
2023-03-02  9:31                       ` Daniel P. Berrangé
2023-03-02 11:09                         ` Markus Armbruster
2023-03-02 13:30                           ` Markus Armbruster
2023-02-28 15:54   ` Eric Blake
2023-02-28 19:16     ` Marc-André Lureau
2023-02-07 14:25 ` [PATCH v3 08/10] qmp: teach 'getfd' to import sockets on win32 marcandre.lureau
2023-02-07 14:50   ` Philippe Mathieu-Daudé
2023-02-07 14:54   ` Daniel P. Berrangé
2023-02-08  7:28     ` Marc-André Lureau
2023-02-17  9:48   ` Markus Armbruster
2023-02-18 10:15     ` Marc-André Lureau
2023-02-20  8:26       ` Markus Armbruster
2023-02-20  9:30         ` Daniel P. Berrangé
2023-02-20  9:52         ` Marc-André Lureau
2023-02-20 10:50           ` Markus Armbruster
2023-02-07 14:25 ` [PATCH v3 09/10] libqtest: make qtest_qmp_add_client work " marcandre.lureau
2023-02-07 14:50   ` Philippe Mathieu-Daudé
2023-02-07 14:25 ` [PATCH v3 10/10] qtest: enable vnc-display test " marcandre.lureau
2023-02-07 14:37   ` Philippe Mathieu-Daudé

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=CAMxuvax6qPYQCzNX7vESJM9_f5k4C1Yat0sJcJjrHkh_1WGpQA@mail.gmail.com \
    --to=marcandre.lureau@redhat.com \
    --cc=alex.bennee@linaro.org \
    --cc=armbru@redhat.com \
    --cc=berrange@redhat.com \
    --cc=bleal@redhat.com \
    --cc=dgilbert@redhat.com \
    --cc=eblake@redhat.com \
    --cc=kraxel@redhat.com \
    --cc=lvivier@redhat.com \
    --cc=michael.roth@amd.com \
    --cc=pbonzini@redhat.com \
    --cc=philmd@linaro.org \
    --cc=qemu-devel@nongnu.org \
    --cc=sw@weilnetz.de \
    --cc=thuth@redhat.com \
    --cc=wainersm@redhat.com \
    /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).