Clean up generated .pb.h files a bit
diff --git a/docs/migration.rst b/docs/migration.rst
index 1fee491..8a73d5b 100644
--- a/docs/migration.rst
+++ b/docs/migration.rst
@@ -165,6 +165,16 @@
**Error indications:** Assignment from incompatible pointer type.
+protoc insertion points are no longer included by default
+---------------------------------------------------------
+**Rationale:** Protoc allows including comments in form `@@protoc_insertion_point`
+to identify locations for other plugins to insert their own extra content.
+Previously these were included by default, but they clutter the generated files
+and are rarely used.
+
+**Changes:** Insertion points are now included only when `--protoc-insertion-points`
+option is passed to the generator.
+
Nanopb-0.3.9.4, 0.4.0 (2019-xx-xx)
==================================
diff --git a/generator/nanopb_generator.py b/generator/nanopb_generator.py
index 327801c..1fa98ee 100755
--- a/generator/nanopb_generator.py
+++ b/generator/nanopb_generator.py
@@ -122,6 +122,13 @@
(FieldD.TYPE_UINT64, nanopb_pb2.IS_64): ('uint64_t','UINT64', 10, 8),
}
+class Globals:
+ '''Ugly global variables, should find a good way to pass these.'''
+ verbose_options = False
+ separate_options = []
+ matched_namemasks = set()
+ protoc_insertion_points = False
+
# String types (for python 2 / python 3 compatibility)
try:
strtypes = (unicode, str)
@@ -252,22 +259,23 @@
result += ' pb_packed'
result += ' %s;' % self.names
+ return result
+ def auxiliary_defines(self):
# sort the enum by value
sorted_values = sorted(self.values, key = lambda x: (x[1], x[0]))
-
- result += '\n#define _%s_MIN %s' % (self.names, sorted_values[0][0])
- result += '\n#define _%s_MAX %s' % (self.names, sorted_values[-1][0])
- result += '\n#define _%s_ARRAYSIZE ((%s)(%s+1))' % (self.names, self.names, sorted_values[-1][0])
+ result = '#define _%s_MIN %s\n' % (self.names, sorted_values[0][0])
+ result += '#define _%s_MAX %s\n' % (self.names, sorted_values[-1][0])
+ result += '#define _%s_ARRAYSIZE ((%s)(%s+1))\n' % (self.names, self.names, sorted_values[-1][0])
if not self.options.long_names:
# Define the long names always so that enum value references
# from other files work properly.
for i, x in enumerate(self.values):
- result += '\n#define %s %s' % (self.value_longnames[i], x[0])
+ result += '#define %s %s\n' % (self.value_longnames[i], x[0])
if self.options.enum_to_string:
- result += '\nconst char *%s_name(%s v);\n' % (self.names, self.names)
+ result += 'const char *%s_name(%s v);\n' % (self.names, self.names)
return result
@@ -995,7 +1003,10 @@
result += ' char dummy_field;'
result += '\n'.join([str(f) for f in sorted(self.fields)])
- result += '\n/* @@protoc_insertion_point(struct:%s) */' % self.name
+
+ if Globals.protoc_insertion_points:
+ result += '\n/* @@protoc_insertion_point(struct:%s) */' % self.name
+
result += '\n}'
if self.packed:
@@ -1430,7 +1441,8 @@
yield options.genformat % (noext + options.extension + options.header_extension)
yield '\n'
- yield '/* @@protoc_insertion_point(includes) */\n'
+ if Globals.protoc_insertion_points:
+ yield '/* @@protoc_insertion_point(includes) */\n'
yield '#if PB_PROTO_HEADER_VERSION != 40\n'
yield '#error Regenerate this file with the current version of nanopb generator.\n'
@@ -1450,7 +1462,8 @@
yield '/* Struct definitions */\n'
for msg in sort_dependencies(self.messages):
yield msg.types()
- yield str(msg) + '\n\n'
+ yield str(msg) + '\n'
+ yield '\n'
if self.extensions:
yield '/* Extensions */\n'
@@ -1458,6 +1471,12 @@
yield extension.extension_decl()
yield '\n'
+ if self.enums:
+ yield '/* Helper constants for enums */\n'
+ for enum in self.enums:
+ yield enum.auxiliary_defines() + '\n'
+ yield '\n'
+
if self.messages:
yield '/* Initializer values for message structs */\n'
for msg in self.messages:
@@ -1538,8 +1557,10 @@
yield '#endif /* __cplusplus */\n'
yield '\n'
+ if Globals.protoc_insertion_points:
+ yield '/* @@protoc_insertion_point(eof) */\n'
+
# End of header
- yield '/* @@protoc_insertion_point(eof) */\n'
yield '\n#endif\n'
def generate_source(self, headername, options):
@@ -1552,7 +1573,9 @@
yield '/* Generated by %s at %s. */\n\n' % (nanopb_version, time.asctime())
yield options.genformat % (headername)
yield '\n'
- yield '/* @@protoc_insertion_point(includes) */\n'
+
+ if Globals.protoc_insertion_points:
+ yield '/* @@protoc_insertion_point(includes) */\n'
yield '#if PB_PROTO_HEADER_VERSION != 40\n'
yield '#error Regenerate this file with the current version of nanopb generator.\n'
@@ -1597,7 +1620,9 @@
yield '#endif\n'
yield '\n'
- yield '/* @@protoc_insertion_point(eof) */\n'
+
+ if Globals.protoc_insertion_points:
+ yield '/* @@protoc_insertion_point(eof) */\n'
# ---------------------------------------------------------------------------
# Options parsing for the .proto files
@@ -1640,12 +1665,6 @@
return results
-class Globals:
- '''Ugly global variables, should find a good way to pass these.'''
- verbose_options = False
- separate_options = []
- matched_namemasks = set()
-
def get_nanopb_suboptions(subdesc, options, name):
'''Get copy of options, and merge information from subdesc.'''
new_options = nanopb_pb2.NanoPBOptions()
@@ -1738,6 +1757,8 @@
help="Print more information.")
optparser.add_option("-s", dest="settings", metavar="OPTION:VALUE", action="append", default=[],
help="Set generator option (max_size, max_count etc.).")
+optparser.add_option("--protoc-insertion-points", dest="protoc_insertion_points", action="store_true", default=False,
+ help="Include insertion point comments in output for use by custom protoc plugins")
def parse_file(filename, fdesc, options):
'''Parse a single file. Returns a ProtoFile instance.'''
@@ -1775,6 +1796,7 @@
Globals.separate_options = []
Globals.matched_namemasks = set()
+ Globals.protoc_insertion_points = options.protoc_insertion_points
# Parse the file
file_options = get_nanopb_suboptions(fdesc, toplevel_options, Names([filename]))