diff mbox series

[v3,08/50] qapi: mcgen() shouldn't indent # lines

Message ID 20170911110623.24981-9-marcandre.lureau@redhat.com
State New
Headers show
Series Hi, | expand

Commit Message

Marc-André Lureau Sept. 11, 2017, 11:05 a.m. UTC
Skip preprocessor lines when adding indentation, since that would
likely result in invalid code.

Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com>
---
 scripts/qapi.py | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

Comments

Markus Armbruster Dec. 6, 2017, 5:41 p.m. UTC | #1
Marc-André Lureau <marcandre.lureau@redhat.com> writes:

> Skip preprocessor lines when adding indentation, since that would
> likely result in invalid code.
>
> Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com>
> ---
>  scripts/qapi.py | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
>
> diff --git a/scripts/qapi.py b/scripts/qapi.py
> index f2b5a7e131..2a8e60e975 100644
> --- a/scripts/qapi.py
> +++ b/scripts/qapi.py
> @@ -1862,7 +1862,7 @@ def cgen(code, **kwds):
>      if indent_level:
>          indent = genindent(indent_level)
>          # re.subn() lacks flags support before Python 2.7, use re.compile()
> -        raw = re.subn(re.compile(r'^.', re.MULTILINE),
> +        raw = re.subn(re.compile(r'^[^#\n].', re.MULTILINE),
>                        indent + r'\g<0>', raw)
>          raw = raw[0]
>      return re.sub(re.escape(eatspace) + r' *', '', raw)

Old: we want to indent all non-empty lines.  Such a line starts with a
character other than newline, matched by '.'.  Replace that character by
indent + the character.

New regexp: we want to indent all non-empty lines not starting with '#'.
Such a line starts with a character other than newline and '#', matched
by '[^#\n]'.  But there's a '.' afterwards, and therefore we don't match
*any* lines consisting of just one character:

    >>> cgen('a\n')
    'a\n'

I think you should drop the '.'.

Alternatively, use a negative lookahead assertion:

        raw = re.subn(re.compile(r'^(?!(#|$))', re.MULTILINE),
                      indent, raw)
diff mbox series

Patch

diff --git a/scripts/qapi.py b/scripts/qapi.py
index f2b5a7e131..2a8e60e975 100644
--- a/scripts/qapi.py
+++ b/scripts/qapi.py
@@ -1862,7 +1862,7 @@  def cgen(code, **kwds):
     if indent_level:
         indent = genindent(indent_level)
         # re.subn() lacks flags support before Python 2.7, use re.compile()
-        raw = re.subn(re.compile(r'^.', re.MULTILINE),
+        raw = re.subn(re.compile(r'^[^#\n].', re.MULTILINE),
                       indent + r'\g<0>', raw)
         raw = raw[0]
     return re.sub(re.escape(eatspace) + r' *', '', raw)