Commit 0b0f5f29 authored by Daniel Vetter's avatar Daniel Vetter Committed by Jani Nikula

scripts/kernel-doc: Add option to inject line numbers

Opt-in since this wreaks the rst output and must be removed
by consumers again. This is useful to adjust the linenumbers
for included kernel-doc snippets in shinx. With that sphinx
error message will be accurate when there's issues with the
rst-ness of the kernel-doc comments.

Especially when transitioning a new docbook .tmpl to .rst this
is extremely useful, since you can just use your editors compilation
quickfix list to accurately jump from error to error.

v2:
- Also make sure that we filter the LINENO for purpose/at declaration
  start so it only shows for selected blocks, not all of them (Jani).
  While at it make it a notch more accurate.
- Avoid undefined $lineno issues. I tried filtering these out at the
  callsite, but Jani spotted more when linting the entire kernel.
  Unamed unions and similar things aren't stored consistently and end
  up with an undefined line number (but also no kernel-doc text, just
  the parameter type). Simplify things and filter undefined line
  numbers in print_lineno() to catch them all.

v3: Fix LINENO 0 issue for kernel-doc comments without @param: lines
or any other special sections that directly jump to the description
after the "name - purpose" line. Only really possible for functions
without parameters. Noticed by Jani.

Cc: Jani Nikula <jani.nikula@intel.com>
Cc: linux-doc@vger.kernel.org
Cc: Jonathan Corbet <corbet@lwn.net>
Signed-off-by: default avatarDaniel Vetter <daniel.vetter@ffwll.ch>
Signed-off-by: default avatarJani Nikula <jani.nikula@intel.com>
parent b7afa92b
...@@ -74,6 +74,8 @@ Output selection (mutually exclusive): ...@@ -74,6 +74,8 @@ Output selection (mutually exclusive):
Output selection modifiers: Output selection modifiers:
-no-doc-sections Do not output DOC: sections. -no-doc-sections Do not output DOC: sections.
-enable-lineno Enable output of #define LINENO lines. Only works with
reStructuredText format.
Other parameters: Other parameters:
-v Verbose output, more warnings and other information. -v Verbose output, more warnings and other information.
...@@ -319,6 +321,7 @@ my $verbose = 0; ...@@ -319,6 +321,7 @@ my $verbose = 0;
my $output_mode = "man"; my $output_mode = "man";
my $output_preformatted = 0; my $output_preformatted = 0;
my $no_doc_sections = 0; my $no_doc_sections = 0;
my $enable_lineno = 0;
my @highlights = @highlights_man; my @highlights = @highlights_man;
my $blankline = $blankline_man; my $blankline = $blankline_man;
my $modulename = "Kernel API"; my $modulename = "Kernel API";
...@@ -351,6 +354,7 @@ my $man_date = ('January', 'February', 'March', 'April', 'May', 'June', ...@@ -351,6 +354,7 @@ my $man_date = ('January', 'February', 'March', 'April', 'May', 'June',
# CAVEAT EMPTOR! Some of the others I localised may not want to be, which # CAVEAT EMPTOR! Some of the others I localised may not want to be, which
# could cause "use of undefined value" or other bugs. # could cause "use of undefined value" or other bugs.
my ($function, %function_table, %parametertypes, $declaration_purpose); my ($function, %function_table, %parametertypes, $declaration_purpose);
my $declaration_start_line;
my ($type, $declaration_name, $return_type); my ($type, $declaration_name, $return_type);
my ($newsection, $newcontents, $prototype, $brcount, %source_map); my ($newsection, $newcontents, $prototype, $brcount, %source_map);
...@@ -411,13 +415,16 @@ my $doc_inline_end = '^\s*\*/\s*$'; ...@@ -411,13 +415,16 @@ my $doc_inline_end = '^\s*\*/\s*$';
my $export_symbol = '^\s*EXPORT_SYMBOL(_GPL)?\s*\(\s*(\w+)\s*\)\s*;'; my $export_symbol = '^\s*EXPORT_SYMBOL(_GPL)?\s*\(\s*(\w+)\s*\)\s*;';
my %parameterdescs; my %parameterdescs;
my %parameterdesc_start_lines;
my @parameterlist; my @parameterlist;
my %sections; my %sections;
my @sectionlist; my @sectionlist;
my %section_start_lines;
my $sectcheck; my $sectcheck;
my $struct_actual; my $struct_actual;
my $contents = ""; my $contents = "";
my $new_start_line = 0;
# the canonical section names. see also $doc_sect above. # the canonical section names. see also $doc_sect above.
my $section_default = "Description"; # default section my $section_default = "Description"; # default section
...@@ -486,6 +493,8 @@ while ($ARGV[0] =~ m/^-(.*)/) { ...@@ -486,6 +493,8 @@ while ($ARGV[0] =~ m/^-(.*)/) {
usage(); usage();
} elsif ($cmd eq '-no-doc-sections') { } elsif ($cmd eq '-no-doc-sections') {
$no_doc_sections = 1; $no_doc_sections = 1;
} elsif ($cmd eq '-enable-lineno') {
$enable_lineno = 1;
} elsif ($cmd eq '-show-not-found') { } elsif ($cmd eq '-show-not-found') {
$show_not_found = 1; $show_not_found = 1;
} }
...@@ -503,6 +512,13 @@ sub get_kernel_version() { ...@@ -503,6 +512,13 @@ sub get_kernel_version() {
return $version; return $version;
} }
#
sub print_lineno {
my $lineno = shift;
if ($enable_lineno && defined($lineno)) {
print "#define LINENO " . $lineno . "\n";
}
}
## ##
# dumps section contents to arrays/hashes intended for that purpose. # dumps section contents to arrays/hashes intended for that purpose.
# #
...@@ -516,11 +532,15 @@ sub dump_section { ...@@ -516,11 +532,15 @@ sub dump_section {
$name = $1; $name = $1;
$parameterdescs{$name} = $contents; $parameterdescs{$name} = $contents;
$sectcheck = $sectcheck . $name . " "; $sectcheck = $sectcheck . $name . " ";
$parameterdesc_start_lines{$name} = $new_start_line;
$new_start_line = 0;
} elsif ($name eq "@\.\.\.") { } elsif ($name eq "@\.\.\.") {
# print STDERR "parameter def '...' = '$contents'\n"; # print STDERR "parameter def '...' = '$contents'\n";
$name = "..."; $name = "...";
$parameterdescs{$name} = $contents; $parameterdescs{$name} = $contents;
$sectcheck = $sectcheck . $name . " "; $sectcheck = $sectcheck . $name . " ";
$parameterdesc_start_lines{$name} = $new_start_line;
$new_start_line = 0;
} else { } else {
# print STDERR "other section '$name' = '$contents'\n"; # print STDERR "other section '$name' = '$contents'\n";
if (defined($sections{$name}) && ($sections{$name} ne "")) { if (defined($sections{$name}) && ($sections{$name} ne "")) {
...@@ -530,6 +550,8 @@ sub dump_section { ...@@ -530,6 +550,8 @@ sub dump_section {
} else { } else {
$sections{$name} = $contents; $sections{$name} = $contents;
push @sectionlist, $name; push @sectionlist, $name;
$section_start_lines{$name} = $new_start_line;
$new_start_line = 0;
} }
} }
} }
...@@ -1775,6 +1797,7 @@ sub output_blockhead_rst(%) { ...@@ -1775,6 +1797,7 @@ sub output_blockhead_rst(%) {
if ($output_selection != OUTPUT_INCLUDE) { if ($output_selection != OUTPUT_INCLUDE) {
print "**$section**\n\n"; print "**$section**\n\n";
} }
print_lineno($section_start_lines{$section});
output_highlight_rst($args{'sections'}{$section}); output_highlight_rst($args{'sections'}{$section});
print "\n"; print "\n";
} }
...@@ -1824,6 +1847,7 @@ sub output_function_rst(%) { ...@@ -1824,6 +1847,7 @@ sub output_function_rst(%) {
} }
} }
print ")\n\n"; print ")\n\n";
print_lineno($declaration_start_line);
$lineprefix = " "; $lineprefix = " ";
output_highlight_rst($args{'purpose'}); output_highlight_rst($args{'purpose'});
print "\n"; print "\n";
...@@ -1840,6 +1864,9 @@ sub output_function_rst(%) { ...@@ -1840,6 +1864,9 @@ sub output_function_rst(%) {
} else { } else {
print "``$parameter``\n"; print "``$parameter``\n";
} }
print_lineno($parameterdesc_start_lines{$parameter_name});
if (defined($args{'parameterdescs'}{$parameter_name}) && if (defined($args{'parameterdescs'}{$parameter_name}) &&
$args{'parameterdescs'}{$parameter_name} ne $undescribed) { $args{'parameterdescs'}{$parameter_name} ne $undescribed) {
output_highlight_rst($args{'parameterdescs'}{$parameter_name}); output_highlight_rst($args{'parameterdescs'}{$parameter_name});
...@@ -1861,6 +1888,7 @@ sub output_section_rst(%) { ...@@ -1861,6 +1888,7 @@ sub output_section_rst(%) {
foreach $section (@{$args{'sectionlist'}}) { foreach $section (@{$args{'sectionlist'}}) {
print "**$section**\n\n"; print "**$section**\n\n";
print_lineno($section_start_lines{$section});
output_highlight_rst($args{'sections'}{$section}); output_highlight_rst($args{'sections'}{$section});
print "\n"; print "\n";
} }
...@@ -1876,6 +1904,7 @@ sub output_enum_rst(%) { ...@@ -1876,6 +1904,7 @@ sub output_enum_rst(%) {
my $name = "enum " . $args{'enum'}; my $name = "enum " . $args{'enum'};
print "\n\n.. c:type:: " . $name . "\n\n"; print "\n\n.. c:type:: " . $name . "\n\n";
print_lineno($declaration_start_line);
$lineprefix = " "; $lineprefix = " ";
output_highlight_rst($args{'purpose'}); output_highlight_rst($args{'purpose'});
print "\n"; print "\n";
...@@ -1903,6 +1932,7 @@ sub output_typedef_rst(%) { ...@@ -1903,6 +1932,7 @@ sub output_typedef_rst(%) {
my $name = "typedef " . $args{'typedef'}; my $name = "typedef " . $args{'typedef'};
print "\n\n.. c:type:: " . $name . "\n\n"; print "\n\n.. c:type:: " . $name . "\n\n";
print_lineno($declaration_start_line);
$lineprefix = " "; $lineprefix = " ";
output_highlight_rst($args{'purpose'}); output_highlight_rst($args{'purpose'});
print "\n"; print "\n";
...@@ -1918,6 +1948,7 @@ sub output_struct_rst(%) { ...@@ -1918,6 +1948,7 @@ sub output_struct_rst(%) {
my $name = $args{'type'} . " " . $args{'struct'}; my $name = $args{'type'} . " " . $args{'struct'};
print "\n\n.. c:type:: " . $name . "\n\n"; print "\n\n.. c:type:: " . $name . "\n\n";
print_lineno($declaration_start_line);
$lineprefix = " "; $lineprefix = " ";
output_highlight_rst($args{'purpose'}); output_highlight_rst($args{'purpose'});
print "\n"; print "\n";
...@@ -1958,6 +1989,7 @@ sub output_struct_rst(%) { ...@@ -1958,6 +1989,7 @@ sub output_struct_rst(%) {
($args{'parameterdescs'}{$parameter_name} ne $undescribed) || next; ($args{'parameterdescs'}{$parameter_name} ne $undescribed) || next;
$type = $args{'parametertypes'}{$parameter}; $type = $args{'parametertypes'}{$parameter};
print_lineno($parameterdesc_start_lines{$parameter_name});
print "``$type $parameter``\n"; print "``$type $parameter``\n";
output_highlight_rst($args{'parameterdescs'}{$parameter_name}); output_highlight_rst($args{'parameterdescs'}{$parameter_name});
print "\n"; print "\n";
...@@ -2745,11 +2777,14 @@ sub process_file($) { ...@@ -2745,11 +2777,14 @@ sub process_file($) {
if (/$doc_start/o) { if (/$doc_start/o) {
$state = STATE_NAME; # next line is always the function name $state = STATE_NAME; # next line is always the function name
$in_doc_sect = 0; $in_doc_sect = 0;
$declaration_start_line = $. + 1;
} }
} elsif ($state == STATE_NAME) {# this line is the function name (always) } elsif ($state == STATE_NAME) {# this line is the function name (always)
if (/$doc_block/o) { if (/$doc_block/o) {
$state = STATE_DOCBLOCK; $state = STATE_DOCBLOCK;
$contents = ""; $contents = "";
$new_start_line = $. + 1;
if ( $1 eq "" ) { if ( $1 eq "" ) {
$section = $section_intro; $section = $section_intro;
} else { } else {
...@@ -2763,8 +2798,11 @@ sub process_file($) { ...@@ -2763,8 +2798,11 @@ sub process_file($) {
} }
$state = STATE_FIELD; $state = STATE_FIELD;
# if there's no @param blocks need to set up default section
# here
$contents = ""; $contents = "";
$section = $section_default; $section = $section_default;
$new_start_line = $. + 1;
if (/-(.*)/) { if (/-(.*)/) {
# strip leading/trailing/multiple spaces # strip leading/trailing/multiple spaces
$descr= $1; $descr= $1;
...@@ -2833,6 +2871,7 @@ sub process_file($) { ...@@ -2833,6 +2871,7 @@ sub process_file($) {
$in_doc_sect = 1; $in_doc_sect = 1;
$in_purpose = 0; $in_purpose = 0;
$contents = $newcontents; $contents = $newcontents;
$new_start_line = $.;
while ((substr($contents, 0, 1) eq " ") || while ((substr($contents, 0, 1) eq " ") ||
substr($contents, 0, 1) eq "\t") { substr($contents, 0, 1) eq "\t") {
$contents = substr($contents, 1); $contents = substr($contents, 1);
...@@ -2866,6 +2905,7 @@ sub process_file($) { ...@@ -2866,6 +2905,7 @@ sub process_file($) {
dump_section($file, $section, xml_escape($contents)); dump_section($file, $section, xml_escape($contents));
$section = $section_default; $section = $section_default;
$contents = ""; $contents = "";
$new_start_line = $.;
} else { } else {
$contents .= "\n"; $contents .= "\n";
} }
...@@ -2900,6 +2940,7 @@ sub process_file($) { ...@@ -2900,6 +2940,7 @@ sub process_file($) {
if ($inline_doc_state == STATE_INLINE_NAME && /$doc_inline_sect/o) { if ($inline_doc_state == STATE_INLINE_NAME && /$doc_inline_sect/o) {
$section = $1; $section = $1;
$contents = $2; $contents = $2;
$new_start_line = $.;
if ($contents ne "") { if ($contents ne "") {
while ((substr($contents, 0, 1) eq " ") || while ((substr($contents, 0, 1) eq " ") ||
substr($contents, 0, 1) eq "\t") { substr($contents, 0, 1) eq "\t") {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment