From: Philippe Proulx Date: Fri, 4 Sep 2020 01:41:44 +0000 (-0400) Subject: cgen.py: _CodeGen.gen_src(): remove ugly empty lines before `}` X-Git-Url: http://drtracing.org/?a=commitdiff_plain;h=c268d6699a96e924e6748dda1edd7f58cafc5c60;p=deliverable%2Fbarectf.git cgen.py: _CodeGen.gen_src(): remove ugly empty lines before `}` 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 --- diff --git a/barectf/cgen.py b/barectf/cgen.py index 86ebd1f..8027b2b 100644 --- a/barectf/cgen.py +++ b/barectf/cgen.py @@ -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)