[PATCH v2 1/2] docs: kdoc: handle nested commas in CMatch arguments

From: Omokefe Emmanuel Onanaroghene

Date: Fri Oct 23 2026 - 09:13:21 EST


CMatch argument extraction splits macro arguments at top-level commas,
but it currently also splits on commas inside nested expressions.
This breaks transformations that capture a macro argument containing a nested call.

Only treat delimiters at the outer macro argument level as separators.

add a regression test for a nested call with comma-separated arguments.

Signed-off-by: Omokefe Emmanuel Onanaroghene <emmaonana18@xxxxxxxxx>
---
tools/lib/python/kdoc/c_lex.py | 3 ++-
tools/unittests/test_cmatch.py | 7 +++++++
2 files changed, 9 insertions(+), 1 deletion(-)

diff --git a/tools/lib/python/kdoc/c_lex.py b/tools/lib/python/kdoc/c_lex.py
index efbc23c01abd..532e2c5a815e 100644
--- a/tools/lib/python/kdoc/c_lex.py
+++ b/tools/lib/python/kdoc/c_lex.py
@@ -430,7 +430,8 @@ class CTokenArgs:
if inner_level < 0:
break

- if tok.kind in [CToken.PUNC, CToken.ENDSTMT] and delim == tok.value:
+ if (inner_level == 0 and tok.value == delim and
+ tok.kind in [CToken.PUNC, CToken.ENDSTMT]):
pos += 1
if self.greedy and pos > self.max_group:
pos -= 1
diff --git a/tools/unittests/test_cmatch.py b/tools/unittests/test_cmatch.py
index 109141cd2ab8..978e02acfa4d 100755
--- a/tools/unittests/test_cmatch.py
+++ b/tools/unittests/test_cmatch.py
@@ -259,6 +259,13 @@ class TestSubSimple(TestCaseDiff):

self.assertLogicallyEqual("REPLACE(arg1, arg2)\nbar();", result)

+ def test_sub_nested_commas(self):
+ """Commas in nested expressions aren't macro argument delimiters"""
+ line = f"{self.MACRO}(a, inner(b, c), d)"
+ result = self.matcher.sub(r"\1|\2|\3", line)
+
+ self.assertLogicallyEqual("a| inner(b, c)| d", result)
+
def test_sub_mixed_placeholders(self):
"""Replacement rule for \0, \1 and additional text"""
line = f"{self.MACRO}(foo, bar)\nbaz();"
--
2.43.0