Skip to content

Commit

Permalink
tools: ynl-gen: don't declare loop iterator in place
Browse files Browse the repository at this point in the history
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 <donald.hunter@gmail.com>
Reviewed-by: Jacob Keller <jacob.e.keller@intel.com>
Link: https://patch.msgid.link/20250414211851.602096-2-kuba@kernel.org
Signed-off-by: Jakub Kicinski <kuba@kernel.org>
  • Loading branch information
Jakub Kicinski committed Apr 17, 2025
1 parent 00ffb37 commit 4d07bbf
Showing 1 changed file with 21 additions and 4 deletions.
25 changes: 21 additions & 4 deletions tools/net/ynl/pyynl/ynl_gen_c.py
Original file line number Diff line number Diff line change
Expand Up @@ -654,10 +654,10 @@ def _attr_get(self, ri, var):
def attr_put(self, ri, var):
if self.attr['type'] in scalars:
put_type = self.type
ri.cw.p(f"for (unsigned int i = 0; i < {var}->n_{self.c_name}; i++)")
ri.cw.p(f"for (i = 0; i < {var}->n_{self.c_name}; i++)")
ri.cw.p(f"ynl_attr_put_{put_type}(nlh, {self.enum_name}, {var}->{self.c_name}[i]);")
elif 'type' not in self.attr or self.attr['type'] == 'nest':
ri.cw.p(f"for (unsigned int i = 0; i < {var}->n_{self.c_name}; i++)")
ri.cw.p(f"for (i = 0; i < {var}->n_{self.c_name}; i++)")
self._attr_put_line(ri, var, f"{self.nested_render_name}_put(nlh, " +
f"{self.enum_name}, &{var}->{self.c_name}[i])")
else:
Expand Down Expand Up @@ -1644,11 +1644,23 @@ def put_req_nested_prototype(ri, struct, suffix=';'):


def put_req_nested(ri, struct):
local_vars = []
init_lines = []

local_vars.append('struct nlattr *nest;')
init_lines.append("nest = ynl_attr_nest_start(nlh, attr_type);")

for _, arg in struct.member_list():
if arg.presence_type() == 'count':
local_vars.append('unsigned int i;')
break

put_req_nested_prototype(ri, struct, suffix='')
ri.cw.block_start()
ri.cw.write_func_lvar('struct nlattr *nest;')
ri.cw.write_func_lvar(local_vars)

ri.cw.p("nest = ynl_attr_nest_start(nlh, attr_type);")
for line in init_lines:
ri.cw.p(line)

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

for _, attr in ri.struct["request"].member_list():
if attr.presence_type() == 'count':
local_vars += ['unsigned int i;']
break

print_prototype(ri, direction, terminate=False)
ri.cw.block_start()
ri.cw.write_func_lvar(local_vars)
Expand Down

0 comments on commit 4d07bbf

Please sign in to comment.