All of lore.kernel.org
 help / color / mirror / Atom feed
From: Markus Armbruster <armbru@redhat.com>
To: marcandre.lureau@redhat.com
Cc: jsnow@redhat.com, qemu-devel@nongnu.org
Subject: Re: [PATCH v7 04/10] qapi: introduce QAPISchemaIfCond.cgen()
Date: Thu, 05 Aug 2021 13:53:29 +0200	[thread overview]
Message-ID: <87o8ac9kg6.fsf@dusky.pond.sub.org> (raw)
In-Reply-To: <20210804083105.97531-5-marcandre.lureau@redhat.com> (marcandre lureau's message of "Wed, 4 Aug 2021 12:30:59 +0400")

marcandre.lureau@redhat.com writes:

> From: Marc-André Lureau <marcandre.lureau@redhat.com>
>
> Instead of building prepocessor conditions from a list of string, use
> the result generated from QAPISchemaIfCond.cgen() and hide the
> implementation details.
>
> Note: this patch introduces a minor regression, generating a redundant
> pair of parenthesis. This is fixed in a later patch in this
> series ("qapi: replace if condition list with dict [..]")

Fixed in most, but not all instances, actually.  I can see some 50
remaining in generated qapi-*.[ch] at the end of this series.  Let's
s/fixed/mostly fixed/, and move on.

>
> Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com>
> ---
>  scripts/qapi/common.py     | 35 ++++++++++++++++++++++-------------
>  scripts/qapi/gen.py        |  4 ++--
>  scripts/qapi/introspect.py |  4 ++--
>  scripts/qapi/schema.py     |  5 ++++-
>  scripts/qapi/types.py      | 20 ++++++++++----------
>  scripts/qapi/visit.py      | 12 ++++++------
>  6 files changed, 46 insertions(+), 34 deletions(-)
>
> diff --git a/scripts/qapi/common.py b/scripts/qapi/common.py
> index 6ad1eeb61d..ba9fe14e4b 100644
> --- a/scripts/qapi/common.py
> +++ b/scripts/qapi/common.py
> @@ -12,7 +12,12 @@
>  # See the COPYING file in the top-level directory.
>  
>  import re
> -from typing import Match, Optional, Sequence
> +from typing import (
> +    List,
> +    Match,
> +    Optional,
> +    Union,
> +)
>  
>  
>  #: Magic string that gets removed along with all space to its right.
> @@ -194,22 +199,26 @@ def guardend(name: str) -> str:
>                   name=c_fname(name).upper())
>  
>  
> -def gen_if(ifcond: Sequence[str]) -> str:
> -    ret = ''
> -    for ifc in ifcond:
> -        ret += mcgen('''
> +def cgen_ifcond(ifcond: Union[str, List[str]]) -> str:
> +    if not ifcond:
> +        return ''
> +    return '(' + ') && ('.join(ifcond) + ')'
> +
> +
> +def gen_if(cond: str) -> str:
> +    if not cond:
> +        return ''
> +    return mcgen('''
>  #if %(cond)s
> -''', cond=ifc)
> -    return ret
> +''', cond=cond)
>  
>  
> -def gen_endif(ifcond: Sequence[str]) -> str:
> -    ret = ''
> -    for ifc in reversed(ifcond):
> -        ret += mcgen('''
> +def gen_endif(cond: str) -> str:
> +    if not cond:
> +        return ''
> +    return mcgen('''
>  #endif /* %(cond)s */
> -''', cond=ifc)
> -    return ret
> +''', cond=cond)
>  
>  
>  def must_match(pattern: str, string: str) -> Match[str]:
> diff --git a/scripts/qapi/gen.py b/scripts/qapi/gen.py
> index 1c5b190276..51a597a025 100644
> --- a/scripts/qapi/gen.py
> +++ b/scripts/qapi/gen.py
> @@ -95,9 +95,9 @@ def _wrap_ifcond(ifcond: QAPISchemaIfCond, before: str, after: str) -> str:
>      if added[0] == '\n':
>          out += '\n'
>          added = added[1:]
> -    out += gen_if(ifcond.ifcond)
> +    out += gen_if(ifcond.cgen())
>      out += added
> -    out += gen_endif(ifcond.ifcond)
> +    out += gen_endif(ifcond.cgen())
>      return out
>  
>  
> diff --git a/scripts/qapi/introspect.py b/scripts/qapi/introspect.py
> index e23725e2f9..bd4233ecee 100644
> --- a/scripts/qapi/introspect.py
> +++ b/scripts/qapi/introspect.py
> @@ -124,10 +124,10 @@ def indent(level: int) -> str:
>          if obj.comment:
>              ret += indent(level) + f"/* {obj.comment} */\n"
>          if obj.ifcond.is_present():
> -            ret += gen_if(obj.ifcond.ifcond)
> +            ret += gen_if(obj.ifcond.cgen())
>          ret += _tree_to_qlit(obj.value, level)
>          if obj.ifcond.is_present():
> -            ret += '\n' + gen_endif(obj.ifcond.ifcond)
> +            ret += '\n' + gen_endif(obj.ifcond.cgen())
>          return ret
>  
>      ret = ''
> diff --git a/scripts/qapi/schema.py b/scripts/qapi/schema.py
> index 24caa4ad43..f018cfc511 100644
> --- a/scripts/qapi/schema.py
> +++ b/scripts/qapi/schema.py
> @@ -19,7 +19,7 @@
>  import re
>  from typing import Optional
>  
> -from .common import POINTER_SUFFIX, c_name
> +from .common import POINTER_SUFFIX, c_name, cgen_ifcond
>  from .error import QAPIError, QAPISemError, QAPISourceError
>  from .expr import check_exprs
>  from .parser import QAPISchemaParser
> @@ -29,6 +29,9 @@ class QAPISchemaIfCond:
>      def __init__(self, ifcond=None):
>          self.ifcond = ifcond or []
>  
> +    def cgen(self):
> +        return cgen_ifcond(self.ifcond)
> +
>      def is_present(self):
>          return bool(self.ifcond)
>  

Possible improvement: have methods gen_if() and gen_endif(), so we can
replace

    gen_if(IFCOND.cgen())

and

    gen_endif(IFCOND.cgen())

by

    IFCOND.gen_if()

and

    IFCOND.gen_endif()

Can be done on top.

[...]



  reply	other threads:[~2021-08-05 11:54 UTC|newest]

Thread overview: 26+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-08-04  8:30 [PATCH v7 00/10] qapi: untie 'if' conditions from C preprocessor marcandre.lureau
2021-08-04  8:30 ` [PATCH v7 01/10] docs: update the documentation upfront about schema configuration marcandre.lureau
2021-08-04  8:30 ` [PATCH v7 02/10] qapi: wrap Sequence[str] in an object marcandre.lureau
2021-08-04  8:30 ` [PATCH v7 03/10] qapi: add QAPISchemaIfCond.is_present() marcandre.lureau
2021-08-04  8:30 ` [PATCH v7 04/10] qapi: introduce QAPISchemaIfCond.cgen() marcandre.lureau
2021-08-05 11:53   ` Markus Armbruster [this message]
2021-08-04  8:31 ` [PATCH v7 05/10] qapidoc: introduce QAPISchemaIfCond.docgen() marcandre.lureau
2021-08-05 11:55   ` Markus Armbruster
2021-08-05 12:02     ` Marc-André Lureau
2021-08-05 17:34       ` Markus Armbruster
2021-08-04  8:31 ` [PATCH v7 06/10] qapi: replace if condition list with dict {'all': [...]} marcandre.lureau
2021-08-05 13:41   ` Markus Armbruster
2021-08-05 14:00     ` Marc-André Lureau
2021-08-05 16:06     ` Markus Armbruster
2021-08-05 15:14   ` Markus Armbruster
2021-08-04  8:31 ` [PATCH v7 07/10] qapi: add 'any' condition marcandre.lureau
2021-08-04  8:31 ` [PATCH v7 08/10] qapi: Use 'if': { 'any': ... } where appropriate marcandre.lureau
2021-08-05 13:56   ` Markus Armbruster
2021-08-05 14:41     ` Marc-André Lureau
2021-08-06  6:48       ` Markus Armbruster
2021-08-04  8:31 ` [PATCH v7 09/10] qapi: add 'not' condition operation marcandre.lureau
2021-08-06  6:52   ` Markus Armbruster
2021-08-04  8:31 ` [PATCH v7 10/10] qapi: make 'if' condition strings simple identifiers marcandre.lureau
2021-08-05 19:21   ` Eric Blake
2021-08-05 17:36 ` [PATCH v7 00/10] qapi: untie 'if' conditions from C preprocessor Markus Armbruster
2021-08-07  6:05   ` 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=87o8ac9kg6.fsf@dusky.pond.sub.org \
    --to=armbru@redhat.com \
    --cc=jsnow@redhat.com \
    --cc=marcandre.lureau@redhat.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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.