netdev.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Jakub Kicinski <kuba@kernel.org>
To: "Asbjørn Sloth Tønnesen" <ast@fiberby.net>
Cc: "Jason A. Donenfeld" <Jason@zx2c4.com>,
	"David S. Miller" <davem@davemloft.net>,
	Eric Dumazet <edumazet@google.com>,
	Paolo Abeni <pabeni@redhat.com>,
	Donald Hunter <donald.hunter@gmail.com>,
	Simon Horman <horms@kernel.org>,
	Jacob Keller <jacob.e.keller@intel.com>,
	Andrew Lunn <andrew+netdev@lunn.ch>,
	wireguard@lists.zx2c4.com, netdev@vger.kernel.org,
	linux-kernel@vger.kernel.org
Subject: Re: [PATCH net-next 05/11] tools: ynl-gen: define nlattr *array in a block scope
Date: Sat, 6 Sep 2025 12:07:54 -0700	[thread overview]
Message-ID: <20250906120754.7b90c718@kernel.org> (raw)
In-Reply-To: <4eda9c57-bde0-43c3-b8a0-3e45f2e672ac@fiberby.net>

On Sat, 6 Sep 2025 13:13:29 +0000 Asbjørn Sloth Tønnesen wrote:
> In patch 4, it is about a variable used by multiple Type classes having
> presence_type() = 'count', which is currently 3 classes:
> - TypeBinaryScalarArray
> - TypeMultiAttr
> - TypeArrayNest (later renamed to TypeIndexedArray)
> 
> In patch 5, I move code for a special variable used by one Type class,
> to be contained within that class. It makes it easier to ensure that the
> variable is only defined, when used, and vice versa. This comes at the
> cost of the generated code looking generated.

So you're agreeing?

> If we should make the generated code look like it was written by humans,
> then I would move the definition of these local variables into a class
> method, so `i` can be generated by the generic implementation, and `array`
> can be implemented in it's class. I will take a stab at this, but it might
> be too much refactoring for this series, eg. `len` is also defined local
> to conditional blocks multiple branches in a row.
> 
> tools/net/ynl/generated/nl80211-user.c:
> nl80211_iftype_data_attrs_parse(..) {
>    [..]
>    ynl_attr_for_each_nested(attr, nested) {
>      unsigned int type = ynl_attr_type(attr);
> 
>      if (type == NL80211_BAND_IFTYPE_ATTR_IFTYPES) {
>        unsigned int len;
>        [..]
>      } else if (type == NL80211_BAND_IFTYPE_ATTR_HE_CAP_MAC) {
>        unsigned int len;
>        [..]
>      [same pattern 8 times, so 11 times in total]
>      } else if (type == NL80211_BAND_IFTYPE_ATTR_EHT_CAP_PPE) {
>        unsigned int len;
>        [..]
>      }
>    }
>    return 0;
> }

It's pretty easily doable, I already gave up on not calling _attr_get()
for sub-messages.

> That looks very generated, I would have `len` defined together with `type`,
> and a switch statement would also look a lot more natural, but maybe leave
> the if->switch conversion for the compiler to detect.

diff --git a/tools/net/ynl/pyynl/ynl_gen_c.py b/tools/net/ynl/pyynl/ynl_gen_c.py
index fb7e03805a11..8a1f8a477566 100755
--- a/tools/net/ynl/pyynl/ynl_gen_c.py
+++ b/tools/net/ynl/pyynl/ynl_gen_c.py
@@ -243,7 +243,7 @@ from lib import SpecSubMessage, SpecSubMessageFormat
         raise Exception(f"Attr get not implemented for class type {self.type}")
 
     def attr_get(self, ri, var, first):
-        lines, init_lines, local_vars = self._attr_get(ri, var)
+        lines, init_lines, _ = self._attr_get(ri, var)
         if type(lines) is str:
             lines = [lines]
         if type(init_lines) is str:
@@ -251,10 +251,6 @@ from lib import SpecSubMessage, SpecSubMessageFormat
 
         kw = 'if' if first else 'else if'
         ri.cw.block_start(line=f"{kw} (type == {self.enum_name})")
-        if local_vars:
-            for local in local_vars:
-                ri.cw.p(local)
-            ri.cw.nl()
 
         if not self.is_multi_val():
             ri.cw.p("if (ynl_attr_validate(yarg, attr))")
@@ -2101,6 +2097,7 @@ _C_KW = {
             else:
                 raise Exception(f"Per-op fixed header not supported, yet")
 
+    var_set = set()
     array_nests = set()
     multi_attrs = set()
     needs_parg = False
@@ -2118,6 +2115,13 @@ _C_KW = {
             multi_attrs.add(arg)
         needs_parg |= 'nested-attributes' in aspec
         needs_parg |= 'sub-message' in aspec
+
+        try:
+            _, _, l_vars = aspec._attr_get(ri, '')
+            var_set |= set(l_vars) if l_vars else set()
+        except:
+            pass  # _attr_get() not implemented by simple types, ignore
+    local_vars += list(var_set)
     if array_nests or multi_attrs:
         local_vars.append('int i;')
     if needs_parg:

  reply	other threads:[~2025-09-06 19:07 UTC|newest]

Thread overview: 55+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-09-04 22:01 [PATCH net-next 00/11] tools: ynl: prepare for wireguard Asbjørn Sloth Tønnesen
2025-09-04 22:01 ` [PATCH net-next 01/11] tools: ynl-gen: allow overriding name-prefix for constants Asbjørn Sloth Tønnesen
2025-09-05 10:36   ` Donald Hunter
2025-09-06  0:07   ` Jakub Kicinski
2025-09-06  0:15   ` Jacob Keller
2025-09-04 22:01 ` [PATCH net-next 02/11] tools: ynl-gen: generate nested array policies Asbjørn Sloth Tønnesen
2025-09-05 10:37   ` Donald Hunter
2025-09-06  0:09   ` Jakub Kicinski
2025-09-06  0:19   ` Jacob Keller
2025-09-06 14:13     ` Asbjørn Sloth Tønnesen
2025-09-08  7:54       ` Johannes Berg
2025-09-08  9:08         ` Asbjørn Sloth Tønnesen
2025-09-08 13:22           ` Johannes Berg
2025-09-09 23:02       ` Jacob Keller
2025-09-04 22:01 ` [PATCH net-next 03/11] tools: ynl-gen: add sub-type check Asbjørn Sloth Tønnesen
2025-09-05 10:37   ` Donald Hunter
2025-09-06  0:12   ` Jakub Kicinski
2025-09-06  0:20   ` Jacob Keller
2025-09-04 22:01 ` [PATCH net-next 04/11] tools: ynl-gen: define count iterator in print_dump() Asbjørn Sloth Tønnesen
2025-09-05 10:37   ` Donald Hunter
2025-09-06  0:13   ` Jakub Kicinski
2025-09-06  0:20   ` Jacob Keller
2025-09-04 22:01 ` [PATCH net-next 05/11] tools: ynl-gen: define nlattr *array in a block scope Asbjørn Sloth Tønnesen
2025-09-05 10:44   ` Donald Hunter
2025-09-06  0:18   ` Jakub Kicinski
2025-09-06 13:13     ` Asbjørn Sloth Tønnesen
2025-09-06 19:07       ` Jakub Kicinski [this message]
2025-09-11  0:01         ` Asbjørn Sloth Tønnesen
2025-09-11  0:27           ` Jakub Kicinski
2025-09-04 22:01 ` [PATCH net-next 06/11] tools: ynl-gen: don't validate nested array attribute types Asbjørn Sloth Tønnesen
2025-09-06  0:23   ` Jakub Kicinski
2025-09-06 13:22     ` Asbjørn Sloth Tønnesen
2025-09-06 19:29       ` Jakub Kicinski
2025-09-06  0:24   ` Jacob Keller
2025-09-06 15:10     ` Asbjørn Sloth Tønnesen
2025-09-08  7:55       ` Johannes Berg
2025-09-10 16:58       ` Jacob Keller
2025-09-04 22:01 ` [PATCH net-next 07/11] tools: ynl-gen: rename TypeArrayNest to TypeIndexedArray Asbjørn Sloth Tønnesen
2025-09-05 10:44   ` Donald Hunter
2025-09-06  0:25   ` Jakub Kicinski
2025-09-04 22:01 ` [PATCH net-next 08/11] tools: ynl: move nest packing to a helper function Asbjørn Sloth Tønnesen
2025-09-05 10:45   ` Donald Hunter
2025-09-04 22:01 ` [PATCH net-next 09/11] tools: ynl: encode indexed-array Asbjørn Sloth Tønnesen
2025-09-05 10:49   ` Donald Hunter
2025-09-05 15:34     ` Asbjørn Sloth Tønnesen
2025-09-04 22:01 ` [PATCH net-next 10/11] tools: ynl: decode hex input Asbjørn Sloth Tønnesen
2025-09-05 10:51   ` Donald Hunter
2025-09-06  0:27     ` Jacob Keller
2025-09-06 14:31       ` Asbjørn Sloth Tønnesen
2025-09-08  8:28         ` Donald Hunter
2025-09-09 18:10   ` Sabrina Dubroca
2025-09-09 20:18     ` Asbjørn Sloth Tønnesen
2025-09-04 22:01 ` [PATCH net-next 11/11] tools: ynl: add ipv4-or-v6 display hint Asbjørn Sloth Tønnesen
2025-09-05 10:53   ` Donald Hunter
2025-09-06 15:59     ` Asbjørn Sloth Tønnesen

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20250906120754.7b90c718@kernel.org \
    --to=kuba@kernel.org \
    --cc=Jason@zx2c4.com \
    --cc=andrew+netdev@lunn.ch \
    --cc=ast@fiberby.net \
    --cc=davem@davemloft.net \
    --cc=donald.hunter@gmail.com \
    --cc=edumazet@google.com \
    --cc=horms@kernel.org \
    --cc=jacob.e.keller@intel.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=netdev@vger.kernel.org \
    --cc=pabeni@redhat.com \
    --cc=wireguard@lists.zx2c4.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).