diff options
author | Jakub Kicinski <kuba@kernel.org> | 2023-06-07 13:23:55 -0700 |
---|---|---|
committer | Jakub Kicinski <kuba@kernel.org> | 2023-06-08 14:01:10 -0700 |
commit | 6f115d4575ab7225fc16ba816b9d25d88976a540 (patch) | |
tree | bb071be1637b1c07b8a7fceab05968a0bb14f664 /tools/net | |
parent | 9858bfc271de3cc61307a710044c304e6ec34f8a (diff) |
tools: ynl-gen: refactor strmap helper generation
Move generating strmap lookup function to a helper.
No functional changes.
Signed-off-by: Jakub Kicinski <kuba@kernel.org>
Diffstat (limited to 'tools/net')
-rwxr-xr-x | tools/net/ynl/ynl-gen-c.py | 36 |
1 files changed, 17 insertions, 19 deletions
diff --git a/tools/net/ynl/ynl-gen-c.py b/tools/net/ynl/ynl-gen-c.py index 8a0abf9048db..efcf91675dfa 100755 --- a/tools/net/ynl/ynl-gen-c.py +++ b/tools/net/ynl/ynl-gen-c.py @@ -1212,6 +1212,21 @@ def put_typol(cw, struct): cw.nl() +def _put_enum_to_str_helper(cw, render_name, map_name, arg_name, enum=None): + args = [f'int {arg_name}'] + if enum and not ('enum-name' in enum and not enum['enum-name']): + args = [f'enum {render_name} {arg_name}'] + cw.write_func_prot('const char *', f'{render_name}_str', args) + cw.block_start() + if enum and enum.type == 'flags': + cw.p(f'{arg_name} = ffs({arg_name}) - 1;') + cw.p(f'if ({arg_name} < 0 || {arg_name} >= (int)MNL_ARRAY_SIZE({map_name}))') + cw.p('return NULL;') + cw.p(f'return {map_name}[{arg_name}];') + cw.block_end() + cw.nl() + + def put_op_name_fwd(family, cw): cw.write_func_prot('const char *', f'{family.name}_op_str', ['int op'], suffix=';') @@ -1228,13 +1243,7 @@ def put_op_name(family, cw): cw.block_end(line=';') cw.nl() - cw.write_func_prot('const char *', f'{family.name}_op_str', ['int op']) - cw.block_start() - cw.p(f'if (op < 0 || op >= (int)MNL_ARRAY_SIZE({map_name}))') - cw.p('return NULL;') - cw.p(f'return {map_name}[op];') - cw.block_end() - cw.nl() + _put_enum_to_str_helper(cw, family.name + '_op', map_name, 'op') def put_enum_to_str_fwd(family, cw, enum): @@ -1252,18 +1261,7 @@ def put_enum_to_str(family, cw, enum): cw.block_end(line=';') cw.nl() - args = [f'enum {enum.render_name} value'] - if 'enum-name' in enum and not enum['enum-name']: - args = ['int value'] - cw.write_func_prot('const char *', f'{enum.render_name}_str', args) - cw.block_start() - if enum.type == 'flags': - cw.p('value = ffs(value) - 1;') - cw.p(f'if (value < 0 || value >= (int)MNL_ARRAY_SIZE({map_name}))') - cw.p('return NULL;') - cw.p(f'return {map_name}[value];') - cw.block_end() - cw.nl() + _put_enum_to_str_helper(cw, enum.render_name, map_name, 'value', enum=enum) def put_req_nested(ri, struct): |