OSDN Git Service

tools: ynl-gen: generate enum-to-string helpers
authorJakub Kicinski <kuba@kernel.org>
Fri, 2 Jun 2023 02:35:44 +0000 (19:35 -0700)
committerJakub Kicinski <kuba@kernel.org>
Sat, 3 Jun 2023 05:10:47 +0000 (22:10 -0700)
commit21b6e302789c412bdde84439b9325c76e2a5c428
treedfe24da2cb28675c559b0842b1bd98ccc9a75f86
parenteef9b794eac87022464c28a3763f9030e1d53f80
tools: ynl-gen: generate enum-to-string helpers

It's sometimes useful to print the name of an enum value,
flag or name of the op. Python can do it, add C helper
code gen for getting names of things.

Example:

  static const char * const netdev_xdp_act_strmap[] = {
[0] = "basic",
[1] = "redirect",
[2] = "ndo-xmit",
[3] = "xsk-zerocopy",
[4] = "hw-offload",
[5] = "rx-sg",
[6] = "ndo-xmit-sg",
  };

  const char *netdev_xdp_act_str(enum netdev_xdp_act value)
  {
value = ffs(value) - 1;
if (value < 0 || value >= (int)MNL_ARRAY_SIZE(netdev_xdp_act_strmap))
return NULL;
return netdev_xdp_act_strmap[value];
  }

Signed-off-by: Jakub Kicinski <kuba@kernel.org>
tools/net/ynl/ynl-gen-c.py