Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

docs: kdoc: rework type prototype parsing

process_proto_type() is using a complex regex and a "while True" loop to
split a declaration into chunks and, in the end, count brackets. Switch to
using a simpler regex to just do the split directly, and handle each chunk
as it comes. The result is, IMO, easier to understand and reason about.

The old algorithm would occasionally elide the space between function
parameters; see struct rng_alg->generate(), foe example. The only output
difference is to not elide that space, which is more correct.

Signed-off-by: Jonathan Corbet <corbet@lwn.net>

+25 -18
+25 -18
scripts/lib/kdoc/kdoc_parser.py
··· 1594 1594 1595 1595 # Strip C99-style comments and surrounding whitespace 1596 1596 line = KernRe(r"//.*$", re.S).sub('', line).strip() 1597 + if not line: 1598 + return # nothing to see here 1597 1599 1598 1600 # To distinguish preprocessor directive from regular declaration later. 1599 1601 if line.startswith('#'): 1600 1602 line += ";" 1601 - 1602 - r = KernRe(r'([^\{\};]*)([\{\};])(.*)') 1603 - while True: 1604 - if r.search(line): 1605 - if self.entry.prototype: 1606 - self.entry.prototype += " " 1607 - self.entry.prototype += r.group(1) + r.group(2) 1608 - 1609 - self.entry.brcount += r.group(2).count('{') 1610 - self.entry.brcount -= r.group(2).count('}') 1611 - 1612 - if r.group(2) == ';' and self.entry.brcount <= 0: 1603 + # 1604 + # Split the declaration on any of { } or ;, and accumulate pieces 1605 + # until we hit a semicolon while not inside {brackets} 1606 + # 1607 + r = KernRe(r'(.*?)([{};])') 1608 + for chunk in r.split(line): 1609 + if chunk: # Ignore empty matches 1610 + self.entry.prototype += chunk 1611 + # 1612 + # This cries out for a match statement ... someday after we can 1613 + # drop Python 3.9 ... 1614 + # 1615 + if chunk == '{': 1616 + self.entry.brcount += 1 1617 + elif chunk == '}': 1618 + self.entry.brcount -= 1 1619 + elif chunk == ';' and self.entry.brcount <= 0: 1613 1620 self.dump_declaration(ln, self.entry.prototype) 1614 1621 self.reset_state(ln) 1615 - break 1616 - 1617 - line = r.group(3) 1618 - else: 1619 - self.entry.prototype += line 1620 - break 1622 + return 1623 + # 1624 + # We hit the end of the line while still in the declaration; put 1625 + # in a space to represent the newline. 1626 + # 1627 + self.entry.prototype += ' ' 1621 1628 1622 1629 def process_proto(self, ln, line): 1623 1630 """STATE_PROTO: reading a function/whatever prototype."""