Re: [PATCH 04/27] qapi: Start to elide redundant has_FOO in generated C

2022-09-16 Thread Daniel P . Berrangé
On Thu, Sep 15, 2022 at 10:42:54PM +0200, Markus Armbruster wrote:
> In QAPI, absent optional members are distinct from any present value.
> We thus represent an optional schema member FOO as two C members: a
> FOO with the member's type, and a bool has_FOO.  Likewise for function
> arguments.
> 
> However, has_FOO is actually redundant for a pointer-valued FOO, which
> can be null only when has_FOO is false, i.e. has_FOO == !!FOO.  Except
> for arrays, where we a null FOO can also be a present empty array.
> 
> The redundant has_FOO are a nuisance to work with.  Improve the
> generator to elide them.  Uses of has_FOO need to be replaced as
> follows.
> 
> Tests of has_FOO become the equivalent comparison of FOO with null.
> For brevity, this is commonly done by implicit conversion to bool.
> 
> Assignments to has_FOO get dropped.
> 
> Likewise for arguments to has_FOO parameters.
> 
> Beware: code may violate the invariant has_FOO == !!FOO before the
> transformation, and get away with it.  The above transformation can
> then break things.  Two cases:
> 
> * Absent: if code ignores FOO entirely when !has_FOO (except for
>   freeing it if necessary), even non-null / uninitialized FOO works.
>   Such code is known to exist.
> 
> * Present: if code ignores FOO entirely when has_FOO, even null FOO
>   works.  Such code should not exist.
> 
> In both cases, replacing tests of has_FOO by FOO reverts their sense.
> We have to fix the value of FOO then.
> 
> To facilitate review of the necessary updates to handwritten code, add
> means to opt out of this change, and opt out for all QAPI schema
> modules where the change requires updates to handwritten code.  The
> next few commits will remove these opt-outs in reviewable chunks, then
> drop the means to opt out.
> 
> Signed-off-by: Markus Armbruster 
> ---
>  docs/devel/qapi-code-gen.rst|  5 +--
>  docs/devel/writing-monitor-commands.rst | 14 
>  scripts/qapi/commands.py|  2 +-
>  scripts/qapi/events.py  |  2 +-
>  scripts/qapi/gen.py |  2 +-
>  scripts/qapi/schema.py  | 47 +
>  scripts/qapi/types.py   |  2 +-
>  scripts/qapi/visit.py   | 17 +++--
>  8 files changed, 76 insertions(+), 15 deletions(-)
> 

Reviewed-by: Daniel P. Berrangé 

With regards,
Daniel
-- 
|: https://berrange.com  -o-https://www.flickr.com/photos/dberrange :|
|: https://libvirt.org -o-https://fstop138.berrange.com :|
|: https://entangle-photo.org-o-https://www.instagram.com/dberrange :|




[PATCH 04/27] qapi: Start to elide redundant has_FOO in generated C

2022-09-15 Thread Markus Armbruster
In QAPI, absent optional members are distinct from any present value.
We thus represent an optional schema member FOO as two C members: a
FOO with the member's type, and a bool has_FOO.  Likewise for function
arguments.

However, has_FOO is actually redundant for a pointer-valued FOO, which
can be null only when has_FOO is false, i.e. has_FOO == !!FOO.  Except
for arrays, where we a null FOO can also be a present empty array.

The redundant has_FOO are a nuisance to work with.  Improve the
generator to elide them.  Uses of has_FOO need to be replaced as
follows.

Tests of has_FOO become the equivalent comparison of FOO with null.
For brevity, this is commonly done by implicit conversion to bool.

Assignments to has_FOO get dropped.

Likewise for arguments to has_FOO parameters.

Beware: code may violate the invariant has_FOO == !!FOO before the
transformation, and get away with it.  The above transformation can
then break things.  Two cases:

* Absent: if code ignores FOO entirely when !has_FOO (except for
  freeing it if necessary), even non-null / uninitialized FOO works.
  Such code is known to exist.

* Present: if code ignores FOO entirely when has_FOO, even null FOO
  works.  Such code should not exist.

In both cases, replacing tests of has_FOO by FOO reverts their sense.
We have to fix the value of FOO then.

To facilitate review of the necessary updates to handwritten code, add
means to opt out of this change, and opt out for all QAPI schema
modules where the change requires updates to handwritten code.  The
next few commits will remove these opt-outs in reviewable chunks, then
drop the means to opt out.

Signed-off-by: Markus Armbruster 
---
 docs/devel/qapi-code-gen.rst|  5 +--
 docs/devel/writing-monitor-commands.rst | 14 
 scripts/qapi/commands.py|  2 +-
 scripts/qapi/events.py  |  2 +-
 scripts/qapi/gen.py |  2 +-
 scripts/qapi/schema.py  | 47 +
 scripts/qapi/types.py   |  2 +-
 scripts/qapi/visit.py   | 17 +++--
 8 files changed, 76 insertions(+), 15 deletions(-)

diff --git a/docs/devel/qapi-code-gen.rst b/docs/devel/qapi-code-gen.rst
index 3a817ba498..5edc49aa74 100644
--- a/docs/devel/qapi-code-gen.rst
+++ b/docs/devel/qapi-code-gen.rst
@@ -1410,7 +1410,6 @@ Example::
 
 struct UserDefOne {
 int64_t integer;
-bool has_string;
 char *string;
 bool has_flag;
 bool flag;
@@ -1525,10 +1524,12 @@ Example::
 
 bool visit_type_UserDefOne_members(Visitor *v, UserDefOne *obj, Error 
**errp)
 {
+bool has_string = !!obj->string;
+
 if (!visit_type_int(v, "integer", >integer, errp)) {
 return false;
 }
-if (visit_optional(v, "string", >has_string)) {
+if (visit_optional(v, "string", _string)) {
 if (!visit_type_str(v, "string", >string, errp)) {
 return false;
 }
diff --git a/docs/devel/writing-monitor-commands.rst 
b/docs/devel/writing-monitor-commands.rst
index 4aa2bb904d..bdd4094cab 100644
--- a/docs/devel/writing-monitor-commands.rst
+++ b/docs/devel/writing-monitor-commands.rst
@@ -166,9 +166,9 @@ and user defined types.
 
 Now, let's update our C implementation in monitor/qmp-cmds.c::
 
- void qmp_hello_world(bool has_message, const char *message, Error **errp)
+ void qmp_hello_world(const char *message, Error **errp)
  {
- if (has_message) {
+ if (message) {
  printf("%s\n", message);
  } else {
  printf("Hello, world\n");
@@ -210,9 +210,9 @@ file. Basically, most errors are set by calling the 
error_setg() function.
 Let's say we don't accept the string "message" to contain the word "love". If
 it does contain it, we want the "hello-world" command to return an error::
 
- void qmp_hello_world(bool has_message, const char *message, Error **errp)
+ void qmp_hello_world(const char *message, Error **errp)
  {
- if (has_message) {
+ if (message) {
  if (strstr(message, "love")) {
  error_setg(errp, "the word 'love' is not allowed");
  return;
@@ -467,9 +467,9 @@ There are a number of things to be noticed:
allocated by the regular g_malloc0() function. Note that we chose to
initialize the memory to zero. This is recommended for all QAPI types, as
it helps avoiding bad surprises (specially with booleans)
-4. Remember that "next_deadline" is optional? All optional members have a
-   'has_TYPE_NAME' member that should be properly set by the implementation,
-   as shown above
+4. Remember that "next_deadline" is optional? Non-pointer optional
+   members have a 'has_TYPE_NAME' member that should be properly set
+   by the implementation, as shown above
 5. Even static strings, such as "alarm_timer->name", should be dynamically
allocated by the implementation. This is so because the QAPI also generates
a function to free its