X-Git-Url: https://gerrit.automotivelinux.org/gerrit/gitweb?a=blobdiff_plain;f=generator%2Fnanopb_generator.py;h=2b1d63e5c6f6a0e031e6fecd7a9909dda3f48355;hb=708084e7883a95dd7fd315cdc909f6664491c043;hp=eebe54049e79b17248f01763120c110442a230de;hpb=3bcdd49eac3aabcc121173c0023834f8bcf0a3cb;p=apps%2Fagl-service-can-low-level.git diff --git a/generator/nanopb_generator.py b/generator/nanopb_generator.py index eebe5404..2b1d63e5 100755 --- a/generator/nanopb_generator.py +++ b/generator/nanopb_generator.py @@ -1,9 +1,10 @@ #!/usr/bin/python '''Generate header file for nanopb from a ProtoBuf FileDescriptorSet.''' -nanopb_version = "nanopb-0.3.3-dev" +nanopb_version = "nanopb-0.3.4-dev" import sys +import re try: # Add some dummy imports to keep packaging tools happy. @@ -159,11 +160,23 @@ class Enum: self.values = [(names + x.name, x.number) for x in desc.value] self.value_longnames = [self.names + x.name for x in desc.value] + self.packed = enum_options.packed_enum + + def has_negative(self): + for n, v in self.values: + if v < 0: + return True + return False def __str__(self): result = 'typedef enum _%s {\n' % self.names result += ',\n'.join([" %s = %d" % x for x in self.values]) - result += '\n} %s;' % self.names + result += '\n}' + + if self.packed: + result += ' pb_packed' + + result += ' %s;' % self.names if not self.options.long_names: # Define the long names always so that enum value references @@ -335,7 +348,7 @@ class Field: inner_init = '""' elif self.pbtype == 'BYTES': inner_init = '{0, {0}}' - elif self.pbtype == 'ENUM': + elif self.pbtype in ('ENUM', 'UENUM'): inner_init = '(%s)0' % self.ctype else: inner_init = '0' @@ -593,6 +606,7 @@ class OneOf(Field): self.struct_name = struct_name self.name = oneof_desc.name self.ctype = 'union' + self.pbtype = 'oneof' self.fields = [] self.allocation = 'ONEOF' self.default = None @@ -884,6 +898,14 @@ def parse_file(fdesc, file_options): idx = enum.value_longnames.index(field.default) field.default = enum.values[idx][0] + # Fix field data types where enums have negative values. + for enum in enums: + if not enum.has_negative(): + for message in messages: + for field in message.fields: + if field.pbtype == 'ENUM' and field.ctype == enum.names: + field.pbtype = 'UENUM' + return enums, messages, extensions def toposort2(data): @@ -1164,9 +1186,13 @@ def read_options_file(infile): [(namemask, options), ...] ''' results = [] - for i, line in enumerate(infile): + data = infile.read() + data = re.sub('/\*.*?\*/', '', data, flags = re.MULTILINE) + data = re.sub('//.*?$', '', data, flags = re.MULTILINE) + data = re.sub('#.*?$', '', data, flags = re.MULTILINE) + for i, line in enumerate(data.split('\n')): line = line.strip() - if not line or line.startswith('//') or line.startswith('#'): + if not line: continue parts = line.split(None, 1) @@ -1379,8 +1405,15 @@ def main_plugin(): data = sys.stdin.read() request = plugin_pb2.CodeGeneratorRequest.FromString(data) + try: + # Versions of Python prior to 2.7.3 do not support unicode + # input to shlex.split(). Try to convert to str if possible. + params = str(request.parameter) + except UnicodeEncodeError: + params = request.parameter + import shlex - args = shlex.split(request.parameter) + args = shlex.split(params) options, dummy = optparser.parse_args(args) Globals.verbose_options = options.verbose