Strip comments from templates
This removes comments related to the emboss backend code generator from
the generated emboss headers. For an example project we see several MB
of reduction in size of the generated headers:
|File | src (KB) | .h (KB) before | .h (KB) after |
|------------------|----------|----------------|---------------|
| hci_events.emb | 76 | 7488 | 5356 |
| hci_commands.emb | 108 | 7376 | 5612 |
| hci_vendor.emb | 32 | 2328 | 1748 |
| hci_common.emb | 32 | 824 | 644 |
| hci_data.emb | 4 | 204 | 152 |
| l2cap_frames.emb | 4 | 60 | 48 |
| hci_h4.emb | 4 | 8 | 8 |
The comments are stripped while generating the code fragment templates,
this allows us to keep comments useful to emboss development in-place
while reducing the impact on generated code size.
diff --git a/compiler/back_end/cpp/generated_code_templates b/compiler/back_end/cpp/generated_code_templates
index 2854233..df9ecac 100644
--- a/compiler/back_end/cpp/generated_code_templates
+++ b/compiler/back_end/cpp/generated_code_templates
@@ -24,7 +24,9 @@
// clang-format off
// ** outline ** ///////////////////////////////////////////////////////////////
-// Generated by the Emboss compiler. DO NOT EDIT!
+/**
+ * Generated by the Emboss compiler. DO NOT EDIT!
+ */
#ifndef ${header_guard}
#define ${header_guard}
#include <stdint.h>
@@ -39,9 +41,9 @@
${includes}
-// NOLINTBEGIN
+/* NOLINTBEGIN */
${body}
-// NOLINTEND
+/* NOLINTEND */
#endif // ${header_guard}
diff --git a/compiler/back_end/util/code_template.py b/compiler/back_end/util/code_template.py
index 5374676..519fb99 100644
--- a/compiler/back_end/util/code_template.py
+++ b/compiler/back_end/util/code_template.py
@@ -59,6 +59,23 @@
of the template. [name] should match [A-Za-z][A-Za-z0-9_]* -- that is, it
should be a valid ASCII Python identifier.
+ Additionally any `//` style comment without leading space of the form:
+ ```C++
+ // This is an emboss developer related comment, it's useful internally
+ // but not relevant to end-users of generated code.
+ ```
+ will be stripped out of the generated code.
+
+ If a template wants to define a comment that will be included in the
+ generated code a C-style comment is recommended:
+ ```C++
+ /** This will be included in the generated source. */
+
+ /**
+ * So will this!
+ */
+ ```
+
Arguments:
text: The text to parse into templates.
@@ -66,6 +83,7 @@
A namedtuple object whose attributes are the templates from text.
"""
delimiter_re = re.compile(r"^\W*\*\* ([A-Za-z][A-Za-z0-9_]*) \*\*\W*$")
+ comment_re = re.compile(r"^\s*//.*$")
templates = {}
name = None
template = []
@@ -79,7 +97,8 @@
name = delimiter_re.match(line).group(1)
template = []
else:
- template.append(line)
+ if not comment_re.match(line):
+ template.append(line)
if name:
templates[name] = finish_template(template)
return collections.namedtuple("Templates",