Skip to content

Commit 4d07bbf

Browse files
committed
tools: ynl-gen: don't declare loop iterator in place
The codegen tries to follow the "old" C style and declare loop iterators at the start of the block / function. Only nested request handling breaks this style, so adjust it. Reviewed-by: Donald Hunter <[email protected]> Reviewed-by: Jacob Keller <[email protected]> Link: https://patch.msgid.link/[email protected] Signed-off-by: Jakub Kicinski <[email protected]>
1 parent 00ffb37 commit 4d07bbf

File tree

1 file changed

+21
-4
lines changed

1 file changed

+21
-4
lines changed

tools/net/ynl/pyynl/ynl_gen_c.py

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -654,10 +654,10 @@ def _attr_get(self, ri, var):
654654
def attr_put(self, ri, var):
655655
if self.attr['type'] in scalars:
656656
put_type = self.type
657-
ri.cw.p(f"for (unsigned int i = 0; i < {var}->n_{self.c_name}; i++)")
657+
ri.cw.p(f"for (i = 0; i < {var}->n_{self.c_name}; i++)")
658658
ri.cw.p(f"ynl_attr_put_{put_type}(nlh, {self.enum_name}, {var}->{self.c_name}[i]);")
659659
elif 'type' not in self.attr or self.attr['type'] == 'nest':
660-
ri.cw.p(f"for (unsigned int i = 0; i < {var}->n_{self.c_name}; i++)")
660+
ri.cw.p(f"for (i = 0; i < {var}->n_{self.c_name}; i++)")
661661
self._attr_put_line(ri, var, f"{self.nested_render_name}_put(nlh, " +
662662
f"{self.enum_name}, &{var}->{self.c_name}[i])")
663663
else:
@@ -1644,11 +1644,23 @@ def put_req_nested_prototype(ri, struct, suffix=';'):
16441644

16451645

16461646
def put_req_nested(ri, struct):
1647+
local_vars = []
1648+
init_lines = []
1649+
1650+
local_vars.append('struct nlattr *nest;')
1651+
init_lines.append("nest = ynl_attr_nest_start(nlh, attr_type);")
1652+
1653+
for _, arg in struct.member_list():
1654+
if arg.presence_type() == 'count':
1655+
local_vars.append('unsigned int i;')
1656+
break
1657+
16471658
put_req_nested_prototype(ri, struct, suffix='')
16481659
ri.cw.block_start()
1649-
ri.cw.write_func_lvar('struct nlattr *nest;')
1660+
ri.cw.write_func_lvar(local_vars)
16501661

1651-
ri.cw.p("nest = ynl_attr_nest_start(nlh, attr_type);")
1662+
for line in init_lines:
1663+
ri.cw.p(line)
16521664

16531665
for _, arg in struct.member_list():
16541666
arg.attr_put(ri, "obj")
@@ -1850,6 +1862,11 @@ def print_req(ri):
18501862
local_vars += ['size_t hdr_len;',
18511863
'void *hdr;']
18521864

1865+
for _, attr in ri.struct["request"].member_list():
1866+
if attr.presence_type() == 'count':
1867+
local_vars += ['unsigned int i;']
1868+
break
1869+
18531870
print_prototype(ri, direction, terminate=False)
18541871
ri.cw.block_start()
18551872
ri.cw.write_func_lvar(local_vars)

0 commit comments

Comments
 (0)