[PATCH v2 23/28] qapi tpm: Elide redundant has_FOO in generated C

Markus Armbruster posted 28 patches 3 years, 3 months ago
Maintainers: Gerd Hoffmann <kraxel@redhat.com>, Christian Schoenebeck <qemu_oss@crudebyte.com>, Stefan Berger <stefanb@linux.vnet.ibm.com>, Kevin Wolf <kwolf@redhat.com>, Hanna Reitz <hreitz@redhat.com>, John Snow <jsnow@redhat.com>, Vladimir Sementsov-Ogievskiy <vsementsov@yandex-team.ru>, Eric Blake <eblake@redhat.com>, Xie Yongji <xieyongji@bytedance.com>, Markus Armbruster <armbru@redhat.com>, Stefan Hajnoczi <stefanha@redhat.com>, Alberto Garcia <berto@igalia.com>, Ilya Dryomov <idryomov@gmail.com>, Peter Lieven <pl@kamp.de>, "Richard W.M. Jones" <rjones@redhat.com>, "Marc-André Lureau" <marcandre.lureau@redhat.com>, Paolo Bonzini <pbonzini@redhat.com>, "Daniel P. Berrangé" <berrange@redhat.com>, Michael Roth <michael.roth@amd.com>, "Michael S. Tsirkin" <mst@redhat.com>, Igor Mammedov <imammedo@redhat.com>, Ani Sinha <ani@anisinha.ca>, Eduardo Habkost <eduardo@habkost.net>, Marcel Apfelbaum <marcel.apfelbaum@gmail.com>, "Philippe Mathieu-Daudé" <philmd@linaro.org>, Yanan Wang <wangyanan55@huawei.com>, David Hildenbrand <david@redhat.com>, Xiao Guangrong <xiaoguangrong.eric@gmail.com>, Jiri Pirko <jiri@resnulli.us>, Jason Wang <jasowang@redhat.com>, "Cédric Le Goater" <clg@kaod.org>, Daniel Henrique Barboza <danielhb413@gmail.com>, David Gibson <david@gibson.dropbear.id.au>, Greg Kurz <groug@kaod.org>, Fam Zheng <fam@euphon.net>, Juan Quintela <quintela@redhat.com>, "Dr. David Alan Gilbert" <dgilbert@redhat.com>, Hailiang Zhang <zhanghailiang@xfusion.com>, Samuel Thibault <samuel.thibault@ens-lyon.org>, Stefan Weil <sw@weilnetz.de>, Konstantin Kostiuk <kkostiuk@redhat.com>, Pavel Dovgalyuk <pavel.dovgaluk@ispras.ru>, Peter Maydell <peter.maydell@linaro.org>, Cornelia Huck <cohuck@redhat.com>, Thomas Huth <thuth@redhat.com>, Richard Henderson <richard.henderson@linaro.org>, Alexander Bulekov <alxndr@bu.edu>, Bandan Das <bsd@redhat.com>, Darren Kenny <darren.kenny@oracle.com>, Qiuhao Li <Qiuhao.Li@outlook.com>, Laurent Vivier <lvivier@redhat.com>
There is a newer version of this series
[PATCH v2 23/28] qapi tpm: Elide redundant has_FOO in generated C
Posted by Markus Armbruster 3 years, 3 months ago
The has_FOO for pointer-valued FOO are redundant, except for arrays.
They are also a nuisance to work with.  Recent commit "qapi: Start to
elide redundant has_FOO in generated C" provided the means to elide
them step by step.  This is the step for qapi/tpm.json.

Said commit explains the transformation in more detail.  The invariant
violations mentioned there do not occur here.

Cc: Stefan Berger <stefanb@linux.vnet.ibm.com>
Signed-off-by: Markus Armbruster <armbru@redhat.com>
Reviewed-by: Stefan Berger <stefanb@linux.ibm.com>
---
 backends/tpm/tpm_passthrough.c | 2 --
 monitor/hmp-cmds.c             | 8 ++++----
 scripts/qapi/schema.py         | 1 -
 3 files changed, 4 insertions(+), 7 deletions(-)

diff --git a/backends/tpm/tpm_passthrough.c b/backends/tpm/tpm_passthrough.c
index 5a2f74db1b..179697a3a9 100644
--- a/backends/tpm/tpm_passthrough.c
+++ b/backends/tpm/tpm_passthrough.c
@@ -259,12 +259,10 @@ tpm_passthrough_handle_device_opts(TPMPassthruState *tpm_pt, QemuOpts *opts)
     value = qemu_opt_get(opts, "cancel-path");
     if (value) {
         tpm_pt->options->cancel_path = g_strdup(value);
-        tpm_pt->options->has_cancel_path = true;
     }
 
     value = qemu_opt_get(opts, "path");
     if (value) {
-        tpm_pt->options->has_path = true;
         tpm_pt->options->path = g_strdup(value);
     }
 
diff --git a/monitor/hmp-cmds.c b/monitor/hmp-cmds.c
index 8f8bd93df1..378f5b083d 100644
--- a/monitor/hmp-cmds.c
+++ b/monitor/hmp-cmds.c
@@ -885,10 +885,10 @@ void hmp_info_tpm(Monitor *mon, const QDict *qdict)
         case TPM_TYPE_PASSTHROUGH:
             tpo = ti->options->u.passthrough.data;
             monitor_printf(mon, "%s%s%s%s",
-                           tpo->has_path ? ",path=" : "",
-                           tpo->has_path ? tpo->path : "",
-                           tpo->has_cancel_path ? ",cancel-path=" : "",
-                           tpo->has_cancel_path ? tpo->cancel_path : "");
+                           tpo->path ? ",path=" : "",
+                           tpo->path ?: "",
+                           tpo->cancel_path ? ",cancel-path=" : "",
+                           tpo->cancel_path ?: "");
             break;
         case TPM_TYPE_EMULATOR:
             teo = ti->options->u.emulator.data;
diff --git a/scripts/qapi/schema.py b/scripts/qapi/schema.py
index 0544037e71..f0726af876 100644
--- a/scripts/qapi/schema.py
+++ b/scripts/qapi/schema.py
@@ -759,7 +759,6 @@ def need_has(self):
         assert self.type
         # Temporary hack to support dropping the has_FOO in reviewable chunks
         opt_out = [
-            'qapi/tpm.json',
             'qapi/transaction.json',
             'qapi/ui.json',
             'qapi/virtio.json',
-- 
2.37.2
Re: [PATCH v2 23/28] qapi tpm: Elide redundant has_FOO in generated C
Posted by Philippe Mathieu-Daudé 3 years, 3 months ago
On 18/10/22 08:28, Markus Armbruster wrote:
> The has_FOO for pointer-valued FOO are redundant, except for arrays.
> They are also a nuisance to work with.  Recent commit "qapi: Start to
> elide redundant has_FOO in generated C" provided the means to elide
> them step by step.  This is the step for qapi/tpm.json.
> 
> Said commit explains the transformation in more detail.  The invariant
> violations mentioned there do not occur here.
> 
> Cc: Stefan Berger <stefanb@linux.vnet.ibm.com>
> Signed-off-by: Markus Armbruster <armbru@redhat.com>
> Reviewed-by: Stefan Berger <stefanb@linux.ibm.com>
> ---
>   backends/tpm/tpm_passthrough.c | 2 --
>   monitor/hmp-cmds.c             | 8 ++++----
>   scripts/qapi/schema.py         | 1 -
>   3 files changed, 4 insertions(+), 7 deletions(-)

> diff --git a/monitor/hmp-cmds.c b/monitor/hmp-cmds.c
> index 8f8bd93df1..378f5b083d 100644
> --- a/monitor/hmp-cmds.c
> +++ b/monitor/hmp-cmds.c
> @@ -885,10 +885,10 @@ void hmp_info_tpm(Monitor *mon, const QDict *qdict)
>           case TPM_TYPE_PASSTHROUGH:
>               tpo = ti->options->u.passthrough.data;
>               monitor_printf(mon, "%s%s%s%s",
> -                           tpo->has_path ? ",path=" : "",
> -                           tpo->has_path ? tpo->path : "",
> -                           tpo->has_cancel_path ? ",cancel-path=" : "",
> -                           tpo->has_cancel_path ? tpo->cancel_path : "");
> +                           tpo->path ? ",path=" : "",
> +                           tpo->path ?: "",
> +                           tpo->cancel_path ? ",cancel-path=" : "",
> +                           tpo->cancel_path ?: "");

Or simply:

             monitor_printf(mon, "%s%s",
                            tpo->path ? ",path=" : "",
                            tpo->cancel_path ? ",cancel-path=" : "");
Re: [PATCH v2 23/28] qapi tpm: Elide redundant has_FOO in generated C
Posted by Stefan Berger 3 years, 3 months ago

On 10/18/22 08:01, Philippe Mathieu-Daudé wrote:
> On 18/10/22 08:28, Markus Armbruster wrote:
>> The has_FOO for pointer-valued FOO are redundant, except for arrays.
>> They are also a nuisance to work with.  Recent commit "qapi: Start to
>> elide redundant has_FOO in generated C" provided the means to elide
>> them step by step.  This is the step for qapi/tpm.json.
>>
>> Said commit explains the transformation in more detail.  The invariant
>> violations mentioned there do not occur here.
>>
>> Cc: Stefan Berger <stefanb@linux.vnet.ibm.com>
>> Signed-off-by: Markus Armbruster <armbru@redhat.com>
>> Reviewed-by: Stefan Berger <stefanb@linux.ibm.com>
>> ---
>>   backends/tpm/tpm_passthrough.c | 2 --
>>   monitor/hmp-cmds.c             | 8 ++++----
>>   scripts/qapi/schema.py         | 1 -
>>   3 files changed, 4 insertions(+), 7 deletions(-)
> 
>> diff --git a/monitor/hmp-cmds.c b/monitor/hmp-cmds.c
>> index 8f8bd93df1..378f5b083d 100644
>> --- a/monitor/hmp-cmds.c
>> +++ b/monitor/hmp-cmds.c
>> @@ -885,10 +885,10 @@ void hmp_info_tpm(Monitor *mon, const QDict *qdict)
>>           case TPM_TYPE_PASSTHROUGH:
>>               tpo = ti->options->u.passthrough.data;
>>               monitor_printf(mon, "%s%s%s%s",
>> -                           tpo->has_path ? ",path=" : "",
>> -                           tpo->has_path ? tpo->path : "",
>> -                           tpo->has_cancel_path ? ",cancel-path=" : "",
>> -                           tpo->has_cancel_path ? tpo->cancel_path : "");
>> +                           tpo->path ? ",path=" : "",
>> +                           tpo->path ?: "",
>> +                           tpo->cancel_path ? ",cancel-path=" : "",
>> +                           tpo->cancel_path ?: "");
> 
> Or simply:
> 
>              monitor_printf(mon, "%s%s",
>                             tpo->path ? ",path=" : "",
>                             tpo->cancel_path ? ",cancel-path=" : "");
> 

this would never print any path...

Re: [PATCH v2 23/28] qapi tpm: Elide redundant has_FOO in generated C
Posted by Philippe Mathieu-Daudé 3 years, 3 months ago
On Tue, 18 Oct 2022 at 14:12, Stefan Berger <stefanb@linux.ibm.com> wrote:
> On 10/18/22 08:01, Philippe Mathieu-Daudé wrote:
> > On 18/10/22 08:28, Markus Armbruster wrote:
> >> The has_FOO for pointer-valued FOO are redundant, except for arrays.
> >> They are also a nuisance to work with.  Recent commit "qapi: Start to
> >> elide redundant has_FOO in generated C" provided the means to elide
> >> them step by step.  This is the step for qapi/tpm.json.
> >>
> >> Said commit explains the transformation in more detail.  The invariant
> >> violations mentioned there do not occur here.
> >>
> >> Cc: Stefan Berger <stefanb@linux.vnet.ibm.com>
> >> Signed-off-by: Markus Armbruster <armbru@redhat.com>
> >> Reviewed-by: Stefan Berger <stefanb@linux.ibm.com>
> >> ---
> >>   backends/tpm/tpm_passthrough.c | 2 --
> >>   monitor/hmp-cmds.c             | 8 ++++----
> >>   scripts/qapi/schema.py         | 1 -
> >>   3 files changed, 4 insertions(+), 7 deletions(-)
> >
> >> diff --git a/monitor/hmp-cmds.c b/monitor/hmp-cmds.c
> >> index 8f8bd93df1..378f5b083d 100644
> >> --- a/monitor/hmp-cmds.c
> >> +++ b/monitor/hmp-cmds.c
> >> @@ -885,10 +885,10 @@ void hmp_info_tpm(Monitor *mon, const QDict *qdict)
> >>           case TPM_TYPE_PASSTHROUGH:
> >>               tpo = ti->options->u.passthrough.data;
> >>               monitor_printf(mon, "%s%s%s%s",
> >> -                           tpo->has_path ? ",path=" : "",
> >> -                           tpo->has_path ? tpo->path : "",
> >> -                           tpo->has_cancel_path ? ",cancel-path=" : "",
> >> -                           tpo->has_cancel_path ? tpo->cancel_path : "");
> >> +                           tpo->path ? ",path=" : "",
> >> +                           tpo->path ?: "",
> >> +                           tpo->cancel_path ? ",cancel-path=" : "",
> >> +                           tpo->cancel_path ?: "");
> >
> > Or simply:
> >
> >              monitor_printf(mon, "%s%s",
> >                             tpo->path ? ",path=" : "",
> >                             tpo->cancel_path ? ",cancel-path=" : "");
> >
>
> this would never print any path...

I need more coffee... =) Trying to KISS:

    if (tpo->path) {
        monitor_printf(mon, ",path=%s", tpo->path);
    }
    if (tpo->cancel_path) {
        monitor_printf(mon, ",cancel-path=%s", tpo->cancel_path);
    }
Re: [PATCH v2 23/28] qapi tpm: Elide redundant has_FOO in generated C
Posted by Markus Armbruster 3 years, 3 months ago
Philippe Mathieu-Daudé <philmd@linaro.org> writes:

> On Tue, 18 Oct 2022 at 14:12, Stefan Berger <stefanb@linux.ibm.com> wrote:
>> On 10/18/22 08:01, Philippe Mathieu-Daudé wrote:
>> > On 18/10/22 08:28, Markus Armbruster wrote:
>> >> The has_FOO for pointer-valued FOO are redundant, except for arrays.
>> >> They are also a nuisance to work with.  Recent commit "qapi: Start to
>> >> elide redundant has_FOO in generated C" provided the means to elide
>> >> them step by step.  This is the step for qapi/tpm.json.
>> >>
>> >> Said commit explains the transformation in more detail.  The invariant
>> >> violations mentioned there do not occur here.
>> >>
>> >> Cc: Stefan Berger <stefanb@linux.vnet.ibm.com>
>> >> Signed-off-by: Markus Armbruster <armbru@redhat.com>
>> >> Reviewed-by: Stefan Berger <stefanb@linux.ibm.com>
>> >> ---
>> >>   backends/tpm/tpm_passthrough.c | 2 --
>> >>   monitor/hmp-cmds.c             | 8 ++++----
>> >>   scripts/qapi/schema.py         | 1 -
>> >>   3 files changed, 4 insertions(+), 7 deletions(-)
>> >
>> >> diff --git a/monitor/hmp-cmds.c b/monitor/hmp-cmds.c
>> >> index 8f8bd93df1..378f5b083d 100644
>> >> --- a/monitor/hmp-cmds.c
>> >> +++ b/monitor/hmp-cmds.c
>> >> @@ -885,10 +885,10 @@ void hmp_info_tpm(Monitor *mon, const QDict *qdict)
>> >>           case TPM_TYPE_PASSTHROUGH:
>> >>               tpo = ti->options->u.passthrough.data;
>> >>               monitor_printf(mon, "%s%s%s%s",
>> >> -                           tpo->has_path ? ",path=" : "",
>> >> -                           tpo->has_path ? tpo->path : "",
>> >> -                           tpo->has_cancel_path ? ",cancel-path=" : "",
>> >> -                           tpo->has_cancel_path ? tpo->cancel_path : "");
>> >> +                           tpo->path ? ",path=" : "",
>> >> +                           tpo->path ?: "",
>> >> +                           tpo->cancel_path ? ",cancel-path=" : "",
>> >> +                           tpo->cancel_path ?: "");
>> >
>> > Or simply:
>> >
>> >              monitor_printf(mon, "%s%s",
>> >                             tpo->path ? ",path=" : "",
>> >                             tpo->cancel_path ? ",cancel-path=" : "");
>> >
>>
>> this would never print any path...
>
> I need more coffee... =) Trying to KISS:
>
>     if (tpo->path) {
>         monitor_printf(mon, ",path=%s", tpo->path);
>     }
>     if (tpo->cancel_path) {
>         monitor_printf(mon, ",cancel-path=%s", tpo->cancel_path);
>     }

I'd prefer to keep this one mechanical.  Feel free to simplify on top!