Skip to content

Commit 8553fb7

Browse files
committed
tools: ynl-gen: print setters for multi-val attrs
For basic types we "flatten" setters. If a request "a" has a simple nest "b" with value "val" we print helpers like: req_set_a_b(struct a *req, int val) { req->_present.a = 1; req->b._present.val = 1; req->b.val = ... } This is not possible for multi-attr because they have to be allocated dynamically by the user. Print "object level" setters so that user preparing the object doesn't have to futz with the presence bits and other YNL internals. Add the ability to pass in the variable name to generated setters. Using "req" here doesn't feel right, while the attr is part of a request it's not the request itself, so it seems cleaner to call it "obj". Example: static inline void netdev_queue_id_set_id(struct netdev_queue_id *obj, __u32 id) { obj->_present.id = 1; obj->id = id; } Reviewed-by: Donald Hunter <donald.hunter@gmail.com> Acked-by: Stanislav Fomichev <sdf@fomichev.me> Link: https://patch.msgid.link/20250723171046.4027470-5-kuba@kernel.org Signed-off-by: Jakub Kicinski <kuba@kernel.org>
1 parent 2c222dd commit 8553fb7

File tree

1 file changed

+13
-6
lines changed

1 file changed

+13
-6
lines changed

tools/net/ynl/pyynl/ynl_gen_c.py

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -275,9 +275,8 @@ def attr_get(self, ri, var, first):
275275
def _setter_lines(self, ri, member, presence):
276276
raise Exception(f"Setter not implemented for class type {self.type}")
277277

278-
def setter(self, ri, space, direction, deref=False, ref=None):
278+
def setter(self, ri, space, direction, deref=False, ref=None, var="req"):
279279
ref = (ref if ref else []) + [self.c_name]
280-
var = "req"
281280
member = f"{var}->{'.'.join(ref)}"
282281

283282
local_vars = []
@@ -332,7 +331,7 @@ def attr_put(self, ri, var):
332331
def attr_get(self, ri, var, first):
333332
pass
334333

335-
def setter(self, ri, space, direction, deref=False, ref=None):
334+
def setter(self, ri, space, direction, deref=False, ref=None, var=None):
336335
pass
337336

338337

@@ -355,7 +354,7 @@ def attr_get(self, ri, var, first):
355354
def attr_policy(self, cw):
356355
pass
357356

358-
def setter(self, ri, space, direction, deref=False, ref=None):
357+
def setter(self, ri, space, direction, deref=False, ref=None, var=None):
359358
pass
360359

361360

@@ -695,13 +694,14 @@ def _attr_get(self, ri, var):
695694
f"parg.data = &{var}->{self.c_name};"]
696695
return get_lines, init_lines, None
697696

698-
def setter(self, ri, space, direction, deref=False, ref=None):
697+
def setter(self, ri, space, direction, deref=False, ref=None, var="req"):
699698
ref = (ref if ref else []) + [self.c_name]
700699

701700
for _, attr in ri.family.pure_nested_structs[self.nested_attrs].member_list():
702701
if attr.is_recursive():
703702
continue
704-
attr.setter(ri, self.nested_attrs, direction, deref=deref, ref=ref)
703+
attr.setter(ri, self.nested_attrs, direction, deref=deref, ref=ref,
704+
var=var)
705705

706706

707707
class TypeMultiAttr(Type):
@@ -2563,6 +2563,13 @@ def print_type_full(ri, struct):
25632563
free_rsp_nested_prototype(ri)
25642564
ri.cw.nl()
25652565

2566+
# Name conflicts are too hard to deal with with the current code base,
2567+
# they are very rare so don't bother printing setters in that case.
2568+
if ri.ku_space == 'user' and not ri.type_name_conflict:
2569+
for _, attr in struct.member_list():
2570+
attr.setter(ri, ri.attr_set, "", var="obj")
2571+
ri.cw.nl()
2572+
25662573

25672574
def print_type_helpers(ri, direction, deref=False):
25682575
print_free_prototype(ri, direction)

0 commit comments

Comments
 (0)