From: Jakub Kicinski Date: Wed, 7 Jun 2023 20:23:55 +0000 (-0700) Subject: tools: ynl-gen: refactor strmap helper generation X-Git-Tag: v6.6.7~2536^2~149^2~8 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=6f115d4575ab7225fc16ba816b9d25d88976a540;p=platform%2Fkernel%2Flinux-starfive.git tools: ynl-gen: refactor strmap helper generation Move generating strmap lookup function to a helper. No functional changes. Signed-off-by: Jakub Kicinski --- diff --git a/tools/net/ynl/ynl-gen-c.py b/tools/net/ynl/ynl-gen-c.py index 8a0abf9..efcf916 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):