netdev.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Jakub Kicinski <kuba@kernel.org>
To: davem@davemloft.net
Cc: netdev@vger.kernel.org, edumazet@google.com, pabeni@redhat.com,
	Jakub Kicinski <kuba@kernel.org>
Subject: [PATCH net-next 04/12] tools: ynl-gen: combine else with closing bracket
Date: Thu,  8 Jun 2023 14:11:52 -0700	[thread overview]
Message-ID: <20230608211200.1247213-5-kuba@kernel.org> (raw)
In-Reply-To: <20230608211200.1247213-1-kuba@kernel.org>

Code gen currently prints:

  }
  else if (...

This is really ugly. Fix it by delaying printing of closing
brackets in anticipation of else coming along.

Signed-off-by: Jakub Kicinski <kuba@kernel.org>
---
 tools/net/ynl/ynl-gen-c.py | 23 +++++++++++++++++++----
 1 file changed, 19 insertions(+), 4 deletions(-)

diff --git a/tools/net/ynl/ynl-gen-c.py b/tools/net/ynl/ynl-gen-c.py
index 9b6ff256f80e..d2edded5f747 100755
--- a/tools/net/ynl/ynl-gen-c.py
+++ b/tools/net/ynl/ynl-gen-c.py
@@ -1017,6 +1017,7 @@ from lib import SpecFamily, SpecAttrSet, SpecAttr, SpecOperation, SpecEnumSet, S
         self.nlib = nlib
 
         self._nl = False
+        self._block_end = False
         self._silent_block = False
         self._ind = 0
         self._out = out_file
@@ -1025,11 +1026,18 @@ from lib import SpecFamily, SpecAttrSet, SpecAttr, SpecOperation, SpecEnumSet, S
     def _is_cond(cls, line):
         return line.startswith('if') or line.startswith('while') or line.startswith('for')
 
-    def p(self, line, add_ind=0, eat_nl=False):
+    def p(self, line, add_ind=0):
+        if self._block_end:
+            self._block_end = False
+            if line.startswith('else'):
+                line = '} ' + line
+            else:
+                self._out.write('\t' * self._ind + '}\n')
+
         if self._nl:
-            if not eat_nl:
-                self._out.write('\n')
+            self._out.write('\n')
             self._nl = False
+
         ind = self._ind
         if line[-1] == ':':
             ind -= 1
@@ -1053,7 +1061,14 @@ from lib import SpecFamily, SpecAttrSet, SpecAttr, SpecOperation, SpecEnumSet, S
         if line and line[0] not in {';', ','}:
             line = ' ' + line
         self._ind -= 1
-        self.p('}' + line, eat_nl=True)
+        self._nl = False
+        if not line:
+            # Delay printing closing bracket in case "else" comes next
+            if self._block_end:
+                self._out.write('\t' * (self._ind + 1) + '}\n')
+            self._block_end = True
+        else:
+            self.p('}' + line)
 
     def write_doc_line(self, doc, indent=True):
         words = doc.split()
-- 
2.40.1


  parent reply	other threads:[~2023-06-08 21:12 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-06-08 21:11 [PATCH net-next 00/12] tools: ynl-gen: code gen improvements before ethtool Jakub Kicinski
2023-06-08 21:11 ` [PATCH net-next 01/12] tools: ynl-gen: cleanup user space header includes Jakub Kicinski
2023-06-08 21:11 ` [PATCH net-next 02/12] tools: ynl: regen: " Jakub Kicinski
2023-06-08 21:11 ` [PATCH net-next 03/12] tools: ynl-gen: complete the C keyword list Jakub Kicinski
2023-06-08 21:11 ` Jakub Kicinski [this message]
2023-06-08 21:11 ` [PATCH net-next 05/12] tools: ynl-gen: get attr type outside of if() Jakub Kicinski
2023-06-08 21:11 ` [PATCH net-next 06/12] tools: ynl: regen: regenerate the if ladders Jakub Kicinski
2023-06-08 21:11 ` [PATCH net-next 07/12] tools: ynl-gen: stop generating common notification handlers Jakub Kicinski
2023-06-08 21:11 ` [PATCH net-next 08/12] tools: ynl: regen: " Jakub Kicinski
2023-06-08 21:11 ` [PATCH net-next 09/12] tools: ynl-gen: sanitize notification tracking Jakub Kicinski
2023-06-08 21:11 ` [PATCH net-next 10/12] tools: ynl-gen: support code gen for events Jakub Kicinski
2023-06-08 21:11 ` [PATCH net-next 11/12] tools: ynl-gen: don't pass op_name to RenderInfo Jakub Kicinski
2023-06-08 21:12 ` [PATCH net-next 12/12] tools: ynl-gen: support / skip pads on the way to kernel Jakub Kicinski
2023-06-09 21:50 ` [PATCH net-next 00/12] tools: ynl-gen: code gen improvements before ethtool patchwork-bot+netdevbpf

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=20230608211200.1247213-5-kuba@kernel.org \
    --to=kuba@kernel.org \
    --cc=davem@davemloft.net \
    --cc=edumazet@google.com \
    --cc=netdev@vger.kernel.org \
    --cc=pabeni@redhat.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).