Skip to content

Commit 8d73387

Browse files
author
Jonathan Corbet
committed
docs: kdoc: Centralize handling of the item section list
The section list always comes directly from the under-construction entry and is used uniformly. Formalize section handling in the KdocItem class, and have output_declaration() load the sections directly from the entry, eliminating a lot of duplicated, verbose parameters. Reviewed-by: Mauro Carvalho Chehab <mchehab+huawei@kernel.org> Signed-off-by: Jonathan Corbet <corbet@lwn.net>
1 parent 8d9d122 commit 8d73387

3 files changed

Lines changed: 25 additions & 40 deletions

File tree

scripts/lib/kdoc/kdoc_item.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ def __init__(self, name, type, start_line, **other_stuff):
99
self.name = name
1010
self.type = type
1111
self.declaration_start_line = start_line
12+
self.sections = {}
13+
self.sections_start_lines = {}
1214
#
1315
# Just save everything else into our own dict so that the output
1416
# side can grab it directly as before. As we move things into more
@@ -24,3 +26,10 @@ def get(self, key, default = None):
2426

2527
def __getitem__(self, key):
2628
return self.get(key)
29+
30+
#
31+
# Tracking of section information.
32+
#
33+
def set_sections(self, sections, start_lines):
34+
self.sections = sections
35+
self.section_start_lines = start_lines

scripts/lib/kdoc/kdoc_output.py

Lines changed: 13 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -338,11 +338,7 @@ def out_section(self, args, out_docblock=False):
338338
starts by putting out the name of the doc section itself, but that
339339
tends to duplicate a header already in the template file.
340340
"""
341-
342-
sections = args.get('sections', {})
343-
section_start_lines = args.get('section_start_lines', {})
344-
345-
for section in sections:
341+
for section, text in args.sections.items():
346342
# Skip sections that are in the nosymbol_table
347343
if section in self.nosymbol:
348344
continue
@@ -354,8 +350,8 @@ def out_section(self, args, out_docblock=False):
354350
else:
355351
self.data += f'{self.lineprefix}**{section}**\n\n'
356352

357-
self.print_lineno(section_start_lines.get(section, 0))
358-
self.output_highlight(sections[section])
353+
self.print_lineno(args.section_start_lines.get(section, 0))
354+
self.output_highlight(text)
359355
self.data += "\n"
360356
self.data += "\n"
361357

@@ -635,23 +631,20 @@ def output_highlight(self, block):
635631
self.data += line + "\n"
636632

637633
def out_doc(self, fname, name, args):
638-
sections = args.get('sections', {})
639-
640634
if not self.check_doc(name, args):
641635
return
642636

643637
self.data += f'.TH "{self.modulename}" 9 "{self.modulename}" "{self.man_date}" "API Manual" LINUX' + "\n"
644638

645-
for section in sections:
639+
for section, text in args.sections.items():
646640
self.data += f'.SH "{section}"' + "\n"
647-
self.output_highlight(sections.get(section))
641+
self.output_highlight(text)
648642

649643
def out_function(self, fname, name, args):
650644
"""output function in man"""
651645

652646
parameterlist = args.get('parameterlist', [])
653647
parameterdescs = args.get('parameterdescs', {})
654-
sections = args.get('sections', {})
655648

656649
self.data += f'.TH "{args["function"]}" 9 "{args["function"]}" "{self.man_date}" "Kernel Hacker\'s Manual" LINUX' + "\n"
657650

@@ -692,15 +685,14 @@ def out_function(self, fname, name, args):
692685
self.data += f'.IP "{parameter}" 12' + "\n"
693686
self.output_highlight(parameterdescs.get(parameter_name, ""))
694687

695-
for section in sections:
688+
for section, text in args.sections.items():
696689
self.data += f'.SH "{section.upper()}"' + "\n"
697-
self.output_highlight(sections[section])
690+
self.output_highlight(text)
698691

699692
def out_enum(self, fname, name, args):
700693

701694
name = args.get('enum', '')
702695
parameterlist = args.get('parameterlist', [])
703-
sections = args.get('sections', {})
704696

705697
self.data += f'.TH "{self.modulename}" 9 "enum {args["enum"]}" "{self.man_date}" "API Manual" LINUX' + "\n"
706698

@@ -727,24 +719,23 @@ def out_enum(self, fname, name, args):
727719
self.data += f'.IP "{parameter}" 12' + "\n"
728720
self.output_highlight(args['parameterdescs'].get(parameter_name, ""))
729721

730-
for section in sections:
722+
for section, text in args.sections.items():
731723
self.data += f'.SH "{section}"' + "\n"
732-
self.output_highlight(sections[section])
724+
self.output_highlight(text)
733725

734726
def out_typedef(self, fname, name, args):
735727
module = self.modulename
736728
typedef = args.get('typedef')
737729
purpose = args.get('purpose')
738-
sections = args.get('sections', {})
739730

740731
self.data += f'.TH "{module}" 9 "{typedef}" "{self.man_date}" "API Manual" LINUX' + "\n"
741732

742733
self.data += ".SH NAME\n"
743734
self.data += f"typedef {typedef} \\- {purpose}\n"
744735

745-
for section in sections:
736+
for section, text in args.sections.items():
746737
self.data += f'.SH "{section}"' + "\n"
747-
self.output_highlight(sections.get(section))
738+
self.output_highlight(text)
748739

749740
def out_struct(self, fname, name, args):
750741
module = self.modulename
@@ -753,7 +744,6 @@ def out_struct(self, fname, name, args):
753744
purpose = args.get('purpose')
754745
definition = args.get('definition')
755746
parameterlist = args.get('parameterlist', [])
756-
sections = args.get('sections', {})
757747
parameterdescs = args.get('parameterdescs', {})
758748

759749
self.data += f'.TH "{module}" 9 "{struct_type} {struct_name}" "{self.man_date}" "API Manual" LINUX' + "\n"
@@ -782,6 +772,6 @@ def out_struct(self, fname, name, args):
782772
self.data += f'.IP "{parameter}" 12' + "\n"
783773
self.output_highlight(parameterdescs.get(parameter_name))
784774

785-
for section in sections:
775+
for section, text in args.sections.items():
786776
self.data += f'.SH "{section}"' + "\n"
787-
self.output_highlight(sections.get(section))
777+
self.output_highlight(text)

scripts/lib/kdoc/kdoc_parser.py

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -272,13 +272,13 @@ def output_declaration(self, dtype, name, **args):
272272
item = KdocItem(name, dtype, self.entry.declaration_start_line, **args)
273273
item.warnings = self.entry.warnings
274274

275-
sections = item.get('sections', {})
276-
277275
# Drop empty sections
278276
# TODO: improve empty sections logic to emit warnings
277+
sections = self.entry.sections
279278
for section in ["Description", "Return"]:
280279
if section in sections and not sections[section].rstrip():
281280
del sections[section]
281+
item.set_sections(sections, self.entry.section_start_lines)
282282

283283
self.entries.append(item)
284284

@@ -824,8 +824,6 @@ def dump_struct(self, ln, proto):
824824
parameterdescs=self.entry.parameterdescs,
825825
parametertypes=self.entry.parametertypes,
826826
parameterdesc_start_lines=self.entry.parameterdesc_start_lines,
827-
sections=self.entry.sections,
828-
section_start_lines=self.entry.section_start_lines,
829827
purpose=self.entry.declaration_purpose)
830828

831829
def dump_enum(self, ln, proto):
@@ -908,8 +906,6 @@ def dump_enum(self, ln, proto):
908906
parameterlist=self.entry.parameterlist,
909907
parameterdescs=self.entry.parameterdescs,
910908
parameterdesc_start_lines=self.entry.parameterdesc_start_lines,
911-
sections=self.entry.sections,
912-
section_start_lines=self.entry.section_start_lines,
913909
purpose=self.entry.declaration_purpose)
914910

915911
def dump_declaration(self, ln, prototype):
@@ -1079,8 +1075,6 @@ def dump_function(self, ln, prototype):
10791075
parameterdescs=self.entry.parameterdescs,
10801076
parametertypes=self.entry.parametertypes,
10811077
parameterdesc_start_lines=self.entry.parameterdesc_start_lines,
1082-
sections=self.entry.sections,
1083-
section_start_lines=self.entry.section_start_lines,
10841078
purpose=self.entry.declaration_purpose,
10851079
func_macro=func_macro)
10861080
else:
@@ -1092,8 +1086,6 @@ def dump_function(self, ln, prototype):
10921086
parameterdescs=self.entry.parameterdescs,
10931087
parametertypes=self.entry.parametertypes,
10941088
parameterdesc_start_lines=self.entry.parameterdesc_start_lines,
1095-
sections=self.entry.sections,
1096-
section_start_lines=self.entry.section_start_lines,
10971089
purpose=self.entry.declaration_purpose,
10981090
func_macro=func_macro)
10991091

@@ -1137,8 +1129,6 @@ def dump_typedef(self, ln, proto):
11371129
parameterdescs=self.entry.parameterdescs,
11381130
parametertypes=self.entry.parametertypes,
11391131
parameterdesc_start_lines=self.entry.parameterdesc_start_lines,
1140-
sections=self.entry.sections,
1141-
section_start_lines=self.entry.section_start_lines,
11421132
purpose=self.entry.declaration_purpose)
11431133
return
11441134

@@ -1159,8 +1149,6 @@ def dump_typedef(self, ln, proto):
11591149

11601150
self.output_declaration('typedef', declaration_name,
11611151
typedef=declaration_name,
1162-
sections=self.entry.sections,
1163-
section_start_lines=self.entry.section_start_lines,
11641152
purpose=self.entry.declaration_purpose)
11651153
return
11661154

@@ -1642,9 +1630,7 @@ def process_docblock(self, ln, line):
16421630

16431631
if doc_end.search(line):
16441632
self.dump_section()
1645-
self.output_declaration("doc", self.entry.identifier,
1646-
sections=self.entry.sections,
1647-
section_start_lines=self.entry.section_start_lines)
1633+
self.output_declaration("doc", self.entry.identifier)
16481634
self.reset_state(ln)
16491635

16501636
elif doc_content.search(line):

0 commit comments

Comments
 (0)