cgen.py: _CodeGen.gen_src(): remove ugly empty lines before `}`
authorPhilippe Proulx <eeppeliteloop@gmail.com>
Fri, 4 Sep 2020 01:41:44 +0000 (21:41 -0400)
committerPhilippe Proulx <eeppeliteloop@gmail.com>
Fri, 4 Sep 2020 01:41:44 +0000 (21:41 -0400)
Jinja 2 makes it hard to have multiple contiguous blocks delimited with
empty lines when using a for loop, while not also having an empty line
at the end.

Therefore, we often get something like this:

    /* Serialize payload */
    {
        /* Align for payload structure */
        _ALIGN(ctx->at, 32);

        /* Write `value` field */
        bt_bitfield_write_le(&ctx->buf[_BITS_TO_BYTES(ctx->at)],
            uint8_t, 0, 32, uint32_t,
            (uint32_t) p_value);
        ctx->at += 32;

    }

This empty line before `}` is really ugly, so use a regex substitution
to fix this.

Signed-off-by: Philippe Proulx <eeppeliteloop@gmail.com>
barectf/cgen.py

index 86ebd1f78cc592801422ca02add959d496a0e237..8027b2b3a318ac6fd9b9aac2358d1f14a705a920 100644 (file)
@@ -25,6 +25,7 @@ import barectf.template as barectf_template
 import barectf.config as barectf_config
 import collections
 import copy
+import re
 from typing import List, Optional, Mapping, Callable, Any, Set, Tuple
 import typing
 from barectf.typing import Count, Alignment
@@ -629,9 +630,29 @@ class _CodeGen:
             return typing.cast(_Op, ret_op)
 
         stream_ops = create_stream_ops()
-        return self._create_file_template('barectf.c.j2').render(header_file_name=header_file_name,
-                                                                 bitfield_header_file_name=bitfield_header_file_name,
-                                                                 root_ft_prefixes=_RootFtPrefixes,
-                                                                 root_ft_prefix_names=_ROOT_FT_PREFIX_NAMES,
-                                                                 stream_ops=stream_ops,
-                                                                 stream_op_pkt_ctx_op=stream_op_pkt_ctx_op)
+        c_src = self._create_file_template('barectf.c.j2').render(header_file_name=header_file_name,
+                                                                  bitfield_header_file_name=bitfield_header_file_name,
+                                                                  root_ft_prefixes=_RootFtPrefixes,
+                                                                  root_ft_prefix_names=_ROOT_FT_PREFIX_NAMES,
+                                                                  stream_ops=stream_ops,
+                                                                  stream_op_pkt_ctx_op=stream_op_pkt_ctx_op)
+
+        # Jinja 2 makes it hard to have multiple contiguous blocks
+        # delimited with empty lines when using a for loop, while not
+        # also having an empty line at the end.
+        #
+        # Therefore, we often get this rendered pattern:
+        #
+        #         /* ... */
+        #         ...;
+        #         ...;
+        #
+        #         /* ... */
+        #         ...;
+        #         ...;
+        #         ...;
+        #
+        #     }
+        #
+        # It's ugly, so fix it here.
+        return re.sub(r'(\n)\s*\n(\s*})', r'\1\2', c_src)
This page took 0.0256 seconds and 4 git commands to generate.