barectf/templates: use `loop.last` instead of hack in `barectf/cgen.py`
authorPhilippe Proulx <eeppeliteloop@gmail.com>
Thu, 24 Sep 2020 16:11:08 +0000 (12:11 -0400)
committerPhilippe Proulx <eeppeliteloop@gmail.com>
Thu, 24 Sep 2020 17:05:54 +0000 (13:05 -0400)
This patch removes the raw text replacement at the end of
_CodeGen.gen_src() (`barectf/cgen.py`).

Instead, to avoid empty lines before `}`, Jinja 2 templates only
output an empty line it's not the loop's last iteration:

    {% if not loop.last %}{{ '\n' }}{% endif %}

Signed-off-by: Philippe Proulx <eeppeliteloop@gmail.com>
barectf/cgen.py
barectf/templates/c/barectf.c.j2
barectf/templates/c/barectf.h.j2
barectf/templates/c/serialize-write-array-statements.j2
barectf/templates/c/serialize-write-struct-statements.j2
barectf/templates/c/size-write-array-statements.j2
barectf/templates/c/size-write-struct-statements.j2

index b4ec1c233599b08b605add8e320176039c21bc59..0bc03208d49013561ba8712d4b71919733c9b749 100644 (file)
@@ -25,7 +25,6 @@ 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
@@ -789,29 +788,9 @@ class _CodeGen:
             return typing.cast(_Op, ret_op)
 
         ds_ops = create_ds_ops()
-        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,
-                                                                  ds_ops=ds_ops,
-                                                                  ds_op_pkt_ctx_op=ds_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)
+        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,
+                                                                 ds_ops=ds_ops,
+                                                                 ds_op_pkt_ctx_op=ds_op_pkt_ctx_op)
index e42b3f407c76ef33b2e20b91b21bc49fadafc860..5207f64d23d837a22fbe3b5a31ea218ed4474fd2 100644 (file)
@@ -387,8 +387,8 @@ end:
 end:
        return;
 }
-       {% if dst._er_header_ft %}
 
+       {% if dst._er_header_ft %}
 static void _serialize_er_header_{{ dst.name }}(void * const vctx,
        const uint32_t ert_id)
 {
@@ -400,19 +400,19 @@ static void _serialize_er_header_{{ dst.name }}(void * const vctx,
 
        {{ this_ds_ops.er_header_op.serialize_str(dst=dst) | indent_tab }}
 }
+
        {% endif %}
        {% if dst.event_record_common_context_field_type %}
-
 static void _serialize_er_common_ctx_{{ dst.name }}(void * const vctx{{ dst | serialize_er_common_ctx_func_params_str(const_params) }})
 {
        struct {{ ctx_struct_name }} * const ctx = _FROM_VOID_PTR(struct {{ ctx_struct_name }}, vctx);
 
        {{ this_ds_ops.er_common_ctx_op.serialize_str(dst=dst) | indent_tab }}
 }
+
        {% endif %}
        {# internal serialization functions #}
        {% for ert in dst.event_record_types | sort %}
-
 static void _serialize_er_{{ dst.name }}_{{ ert.name }}(void * const vctx{{ (dst, ert) | trace_func_params_str(const_params) }})
 {
        struct {{ ctx_struct_name }} * const ctx = _FROM_VOID_PTR(struct {{ ctx_struct_name }}, vctx);
@@ -437,11 +437,11 @@ static void _serialize_er_{{ dst.name }}_{{ ert.name }}(void * const vctx{{ (dst
        {{ this_er_ops.payload_op.serialize_str(dst=dst, ert=ert) | indent_tab }}
                {% endif %}
 }
+
        {% endfor %}
        {# internal size functions #}
        {% for ert in dst.event_record_types | sort %}
                {% set this_er_ops = this_ds_ops.er_ops[ert] %}
-
 static uint32_t _er_size_{{ dst.name }}_{{ ert.name }}(void * const vctx{{ (dst, ert) | trace_func_params_str(const_params, only_dyn=true) }})
 {
        struct {{ ctx_struct_name }} * const ctx = _FROM_VOID_PTR(struct {{ ctx_struct_name }}, vctx);
@@ -465,10 +465,10 @@ static uint32_t _er_size_{{ dst.name }}_{{ ert.name }}(void * const vctx{{ (dst,
 
        return at - ctx->at;
 }
+
        {% endfor %}
        {# public tracing functions #}
        {% for ert in dst.event_record_types | sort %}
-
                {% include 'c/trace-func-proto.j2' %}
 
 {
@@ -517,5 +517,6 @@ static uint32_t _er_size_{{ dst.name }}_{{ ert.name }}(void * const vctx{{ (dst,
 end:
        return;
 }
+               {% if not loop.last %}{{ '\n' }}{% endif %}
        {% endfor %}
 {% endfor %}
index 7ffc98142aa4dcbe06945116e9a7a9c184923b34..ca9ae398e5507c7ebfc2974e2b286088c773f9c7 100644 (file)
@@ -165,9 +165,10 @@ struct {{ prefix }}{{ dst.name }}_ctx {
        {% include 'c/open-func-proto.j2' %};
 
        {% include 'c/close-func-proto.j2' %};
-       {% for ert in dst.event_record_types | sort %}
 
+       {% for ert in dst.event_record_types | sort %}
                {% include 'c/trace-func-proto.j2' %};
+               {% if not loop.last %}{{ '\n' }}{% endif %}
        {% endfor %}
 {% endfor %}
 
index b532be1bbe736e6bf4787228ab3a8783fcf41c78..e9b2b2c5f800ea73d4346bc31625b7c322f81ca1 100644 (file)
@@ -32,7 +32,7 @@
        for ({{ var_name }} = 0; {{ var_name }} < (uint32_t) {{ length_src }}; ++{{ var_name }}) {
 {% for subop in op.subops %}
                {{ subop.serialize_str(dst=dst, ert=ert) | indent_tab(2) }}
-
+       {% if not loop.last %}{{ '\n' }}{% endif %}
 {% endfor %}
        }
 }
index 16e3d8609b51b6a26e621013d7800107fea1bcee..d5395a8c830327df7cf095edad4978ee7da988ed 100644 (file)
@@ -32,6 +32,6 @@
 {
 {% for subop in op.subops %}
        {{ subop.serialize_str(dst=dst, ert=ert) | indent_tab }}
-
+       {% if not loop.last %}{{ '\n' }}{% endif %}
 {% endfor %}
 }
index 6340e853d3dd1d6a82cfaeef125bd4ccf114683d..d4ec458abd4ef0d0dc4e832067f1ffac09b3b71c 100644 (file)
@@ -32,7 +32,7 @@
        for ({{ var_name }} = 0; {{ var_name }} < (uint32_t) {{ length_src }}; ++{{ var_name }}) {
 {% for subop in op.subops %}
                {{ subop.size_str(dst=dst, ert=ert) | indent_tab(2) }}
-
+       {% if not loop.last %}{{ '\n' }}{% endif %}
 {% endfor %}
        }
 }
index d235299745e184513b776fddb85e2b1b40e3982f..0939be3bc8f21c9a52e5779f920caf871a5b71b0 100644 (file)
@@ -32,6 +32,6 @@
 {
 {% for subop in op.subops %}
        {{ subop.size_str(dst=dst, ert=ert) | indent_tab }}
-
+       {% if not loop.last %}{{ '\n' }}{% endif %}
 {% endfor %}
 }
This page took 0.029698 seconds and 4 git commands to generate.