[PATCH v2 27/28] docs: c_lex: produce a cleaner str() representation

Mauro Carvalho Chehab
2026-03-12T14:55:02Z

Avoid adding whitespaces before ";" and have duplicated ones
at the output after converting to string.

Signed-off-by: Mauro Carvalho Chehab <mchehab+huawei@kernel.org>
---
tools/lib/python/kdoc/c_lex.py | 27 ++++++++++++++++++++++++---
1 file changed, 24 insertions(+), 3 deletions(-)

diff --git a/tools/lib/python/kdoc/c_lex.py b/tools/lib/python/kdoc/c_lex.py
index e641bace5d69..95c4dd5afe77 100644
--- a/tools/lib/python/kdoc/c_lex.py
+++ b/tools/lib/python/kdoc/c_lex.py
@@ -241,7 +241,7 @@ class CTokenizer():
out=""
show_stack = [True]

- for tok in self.tokens:
+ for i, tok in enumerate(self.tokens):
if tok.kind == CToken.BEGIN:
show_stack.append(show_stack[-1])

@@ -270,8 +270,29 @@ class CTokenizer():

continue

- if show_stack[-1]:
- out += str(tok.value)
+ if not show_stack[-1]:
+ continue
+
+ if i < len(self.tokens) - 1:
+ next_tok = self.tokens[i + 1]
+
+ # Do some cleanups before ";"
+
+ if (tok.kind == CToken.SPACE and
+ next_tok.kind == CToken.PUNC and
+ next_tok.value == ";"):
+
+ continue
+
+ if (tok.kind == CToken.PUNC and
+ next_tok.kind == CToken.PUNC and
+ tok.value == ";" and
+ next_tok.kind == CToken.PUNC and
+ next_tok.value == ";"):
+
+ continue
+
+ out += str(tok.value)

return out

--
2.52.0