[PATCH net-next 2/6] tools: ynl-gen: refactor render-max enum generation

Asbjørn Sloth Tønnesen posted 6 patches 2 months, 1 week ago
[PATCH net-next 2/6] tools: ynl-gen: refactor render-max enum generation
Posted by Asbjørn Sloth Tønnesen 2 months, 1 week ago
This patch refactors the generation of the three render-max
private enum members: (__$pfx-MAX and $pfx-MAX) or $pfx-MASK.

The names, default or not, are now resolved in the EnumSet class.

This makes enum.enum_max_name re-usable for NLA_POLICY_MASK() in
the next patch in this series, so we don't have to re-define it.

This doesn't change the generated output.

Signed-off-by: Asbjørn Sloth Tønnesen <ast@fiberby.net>
---
 tools/net/ynl/pyynl/ynl_gen_c.py | 20 +++++++++-----------
 1 file changed, 9 insertions(+), 11 deletions(-)

diff --git a/tools/net/ynl/pyynl/ynl_gen_c.py b/tools/net/ynl/pyynl/ynl_gen_c.py
index e6df0e2b63a8c..2666cc54d09c0 100755
--- a/tools/net/ynl/pyynl/ynl_gen_c.py
+++ b/tools/net/ynl/pyynl/ynl_gen_c.py
@@ -1060,7 +1060,9 @@ class EnumSet(SpecEnumSet):
 
         self.value_pfx = yaml.get('name-prefix', f"{family.ident_name}-{yaml['name']}-")
         self.header = yaml.get('header', None)
-        self.enum_cnt_name = yaml.get('enum-cnt-name', None)
+        self.enum_cnt_name = yaml.get('enum-cnt-name', f'--{self.value_pfx}max')
+        suffix = yaml['type'] == 'flags' and 'mask' or 'max'
+        self.enum_max_name = f'{self.value_pfx}{suffix}'
 
         super().__init__(family, yaml)
 
@@ -3205,7 +3207,6 @@ def render_uapi(family, cw):
                 cw.p(' */')
 
             uapi_enum_start(family, cw, const, 'name')
-            name_pfx = const.get('name-prefix', f"{family.ident_name}-{const['name']}-")
             for entry in enum.entries.values():
                 suffix = ','
                 if entry.value_change:
@@ -3215,17 +3216,14 @@ def render_uapi(family, cw):
             if const.get('render-max', False):
                 cw.nl()
                 cw.p('/* private: */')
+                max_name = c_upper(enum.enum_max_name)
                 if const['type'] == 'flags':
-                    max_name = c_upper(name_pfx + 'mask')
-                    max_val = f' = {enum.get_mask()},'
-                    cw.p(max_name + max_val)
+                    max_val = f'{enum.get_mask()},'
                 else:
-                    cnt_name = enum.enum_cnt_name
-                    max_name = c_upper(name_pfx + 'max')
-                    if not cnt_name:
-                        cnt_name = '__' + name_pfx + 'max'
-                    cw.p(c_upper(cnt_name) + ',')
-                    cw.p(max_name + ' = (' + c_upper(cnt_name) + ' - 1)')
+                    cnt_name = c_upper(enum.enum_cnt_name)
+                    cw.p(f'{cnt_name},')
+                    max_val = f'({cnt_name} - 1)'
+                cw.p(f'{max_name} = {max_val}')
             cw.block_end(line=';')
             cw.nl()
         elif const['type'] == 'const':
-- 
2.51.0

Re: [PATCH net-next 2/6] tools: ynl-gen: refactor render-max enum generation
Posted by Jakub Kicinski 2 months ago
On Mon, 13 Oct 2025 16:49:59 +0000 Asbjørn Sloth Tønnesen wrote:
> +        suffix = yaml['type'] == 'flags' and 'mask' or 'max'

This construct looks highly non-pythonic to me

> +        self.enum_max_name = f'{self.value_pfx}{suffix}'

sometimes its max sometimes is mask, so we shouldn't call it max always