From b5bdd70eb6a3fad961e39460e1460fd160c87bb9 Mon Sep 17 00:00:00 2001 From: Marko Lindqvist Date: Thu, 12 May 2022 19:53:35 +0300 Subject: [PATCH 14/27] generate_packets.py: Do not introduce unused variables See osdn #44563 Patch by myself and alien-valkyrie Signed-off-by: Marko Lindqvist --- common/generate_packets.py | 87 +++++++++++++++++++++++++++++--------- 1 file changed, 66 insertions(+), 21 deletions(-) diff --git a/common/generate_packets.py b/common/generate_packets.py index 04d068342d..81e8ba47bc 100755 --- a/common/generate_packets.py +++ b/common/generate_packets.py @@ -295,32 +295,53 @@ class Field: } }'''%self.get_dict(vars()) + @property + def folded_into_head(self): + return ( + fold_bool_into_header + and self.struct_type == "bool" + and not self.is_array + ) + # Returns a code fragment which updates the bit of the this field # in the "fields" bitvector. The bit is either a "content-differs" # bit or (for bools which gets folded in the header) the actual # value of the bool. - def get_cmp_wrapper(self,i): - cmp=self.get_cmp() - if fold_bool_into_header and self.struct_type=="bool" and \ - not self.is_array: - b="packet->%(name)s"%self.get_dict(vars()) - return '''%s + def get_cmp_wrapper(self, i, pack): + if self.folded_into_head: + if pack.is_info != "no": + cmp = self.get_cmp() + differ_part = ''' if (differ) { different++; } - if (%s) { +''' + else: + cmp = '''''' + differ_part = '''''' + b="packet->%(name)s"%self.get_dict(vars()) + return '''%s'''%(cmp) + differ_part + ''' if (%s) { BV_SET(fields, %d); } -'''%(cmp,b,i) +'''%(b,i) else: - return '''%s + cmp = self.get_cmp() + if pack.is_info != "no": + return '''%s if (differ) { different++; BV_SET(fields, %d); } -'''%(cmp,i) +'''%(cmp, i) + else: + return '''%s + if (differ) { + BV_SET(fields, %d); + } + +'''%(cmp, i) # Returns a code fragment which will put this field if the # content has changed. Does nothing for bools-in-header. @@ -733,21 +754,22 @@ field_addr.name = \"%(name)s\"; elif deltafragment and self.diff and self.is_array == 1: return ''' { +#ifdef FREECIV_JSON_CONNECTION int count; -#ifdef FREECIV_JSON_CONNECTION /* Enter array. */ field_addr.sub_location = plocation_elem_new(0); -#endif /* FREECIV_JSON_CONNECTION */ for (count = 0;; count++) { int i; -#ifdef FREECIV_JSON_CONNECTION field_addr.sub_location->number = count; /* Enter diff array element (start at the index address). */ field_addr.sub_location->sub_location = plocation_elem_new(0); +#else /* FREECIV_JSON_CONNECTION */ +while (TRUE) { + int i; #endif /* FREECIV_JSON_CONNECTION */ if (!DIO_GET(uint8, &din, &field_addr, &i)) { @@ -821,6 +843,17 @@ class Variant: self.type=packet.type self.delta=packet.delta self.is_info=packet.is_info + self.differ_used = ( + (not self.no_packet) + and self.delta + and ( + self.is_info != "no" + or any( + not field.folded_into_head + for field in self.fields + ) + ) + ) self.cancel=packet.cancel self.want_force=packet.want_force @@ -1031,13 +1064,16 @@ static char *stats_%(name)s_names[] = {%(names)s}; diff='force_to_send' else: diff='0' - delta_header='''#ifdef FREECIV_DELTA_PROTOCOL + delta_header = '''#ifdef FREECIV_DELTA_PROTOCOL %(name)s_fields fields; struct %(packet_name)s *old; - bool differ; - struct genhash **hash = pc->phs.sent + %(type)s; - int different = %(diff)s; -#endif /* FREECIV_DELTA_PROTOCOL */ +''' + delta_header2 = ''' struct genhash **hash = pc->phs.sent + %(type)s; +''' + if self.is_info != "no": + delta_header2 = delta_header2 + ''' int different = %(diff)s; +''' + delta_header2 = delta_header2 + '''#endif /* FREECIV_DELTA_PROTOCOL */ ''' body=self.get_delta_send_body()+"\n#ifndef FREECIV_DELTA_PROTOCOL" else: @@ -1072,6 +1108,12 @@ static char *stats_%(name)s_names[] = {%(names)s}; else: faddr = "" + if delta_header != "": + if self.differ_used: + delta_header = delta_header + ''' bool differ; +''' + delta_header2 + else: + delta_header = delta_header + delta_header2 for i in range(2): for k,v in vars().items(): if type(v)==type(""): @@ -1095,13 +1137,16 @@ static char *stats_%(name)s_names[] = {%(names)s}; *old = *real_packet; genhash_insert(*hash, old, old); memset(old, 0, sizeof(*old)); - different = 1; /* Force to send. */ - } +''' + if self.is_info != "no": + intro = intro + ''' different = 1; /* Force to send. */ +''' + intro = intro + ''' } ''' body="" for i in range(len(self.other_fields)): field=self.other_fields[i] - body=body+field.get_cmp_wrapper(i) + body=body+field.get_cmp_wrapper(i, self) if self.gen_log: fl=' %(log_macro)s(" no change -> discard");\n' else: -- 2.35.1