linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Jani Nikula <jani.nikula@intel.com>
To: Jonathan Corbet <corbet@lwn.net>
Cc: Jani Nikula <jani.nikula@intel.com>,
	Markus Heiser <markus.heiser@darmarit.de>,
	Daniel Vetter <daniel.vetter@ffwll.ch>,
	Grant Likely <grant.likely@secretlab.ca>,
	Mauro Carvalho Chehab <mchehab@osg.samsung.com>,
	Keith Packard <keithp@keithp.com>,
	LKML <linux-kernel@vger.kernel.org>,
	linux-doc@vger.kernel.org, Hans Verkuil <hverkuil@xs4all.nl>
Subject: [PATCH v2 37/38] scripts/kernel-doc: Add option to inject line numbers
Date: Sat,  4 Jun 2016 14:37:38 +0300	[thread overview]
Message-ID: <0b0f5f29b282b18d26ce698e1aab0267234f77bf.1465031816.git.jani.nikula@intel.com> (raw)
In-Reply-To: <cover.1465031816.git.jani.nikula@intel.com>
In-Reply-To: <cover.1465031816.git.jani.nikula@intel.com>

From: Daniel Vetter <daniel.vetter@ffwll.ch>

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: Daniel Vetter <daniel.vetter@ffwll.ch>
Signed-off-by: Jani Nikula <jani.nikula@intel.com>
---
 scripts/kernel-doc | 41 +++++++++++++++++++++++++++++++++++++++++
 1 file changed, 41 insertions(+)

diff --git a/scripts/kernel-doc b/scripts/kernel-doc
index 4da6f952d18b..5192213c5005 100755
--- a/scripts/kernel-doc
+++ b/scripts/kernel-doc
@@ -74,6 +74,8 @@ Output selection (mutually exclusive):
 
 Output selection modifiers:
   -no-doc-sections	Do not output DOC: sections.
+  -enable-lineno        Enable output of #define LINENO lines. Only works with
+                        reStructuredText format.
 
 Other parameters:
   -v			Verbose output, more warnings and other information.
@@ -319,6 +321,7 @@ my $verbose = 0;
 my $output_mode = "man";
 my $output_preformatted = 0;
 my $no_doc_sections = 0;
+my $enable_lineno = 0;
 my @highlights = @highlights_man;
 my $blankline = $blankline_man;
 my $modulename = "Kernel API";
@@ -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
 # could cause "use of undefined value" or other bugs.
 my ($function, %function_table, %parametertypes, $declaration_purpose);
+my $declaration_start_line;
 my ($type, $declaration_name, $return_type);
 my ($newsection, $newcontents, $prototype, $brcount, %source_map);
 
@@ -411,13 +415,16 @@ my $doc_inline_end = '^\s*\*/\s*$';
 my $export_symbol = '^\s*EXPORT_SYMBOL(_GPL)?\s*\(\s*(\w+)\s*\)\s*;';
 
 my %parameterdescs;
+my %parameterdesc_start_lines;
 my @parameterlist;
 my %sections;
 my @sectionlist;
+my %section_start_lines;
 my $sectcheck;
 my $struct_actual;
 
 my $contents = "";
+my $new_start_line = 0;
 
 # the canonical section names. see also $doc_sect above.
 my $section_default = "Description";	# default section
@@ -486,6 +493,8 @@ while ($ARGV[0] =~ m/^-(.*)/) {
 	usage();
     } elsif ($cmd eq '-no-doc-sections') {
 	    $no_doc_sections = 1;
+    } elsif ($cmd eq '-enable-lineno') {
+	    $enable_lineno = 1;
     } elsif ($cmd eq '-show-not-found') {
 	$show_not_found = 1;
     }
@@ -503,6 +512,13 @@ sub get_kernel_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.
 #
@@ -516,11 +532,15 @@ sub dump_section {
 	$name = $1;
 	$parameterdescs{$name} = $contents;
 	$sectcheck = $sectcheck . $name . " ";
+        $parameterdesc_start_lines{$name} = $new_start_line;
+        $new_start_line = 0;
     } elsif ($name eq "@\.\.\.") {
 #	print STDERR "parameter def '...' = '$contents'\n";
 	$name = "...";
 	$parameterdescs{$name} = $contents;
 	$sectcheck = $sectcheck . $name . " ";
+        $parameterdesc_start_lines{$name} = $new_start_line;
+        $new_start_line = 0;
     } else {
 #	print STDERR "other section '$name' = '$contents'\n";
 	if (defined($sections{$name}) && ($sections{$name} ne "")) {
@@ -530,6 +550,8 @@ sub dump_section {
 	} else {
 	    $sections{$name} = $contents;
 	    push @sectionlist, $name;
+            $section_start_lines{$name} = $new_start_line;
+            $new_start_line = 0;
 	}
     }
 }
@@ -1775,6 +1797,7 @@ sub output_blockhead_rst(%) {
 	if ($output_selection != OUTPUT_INCLUDE) {
 	    print "**$section**\n\n";
 	}
+        print_lineno($section_start_lines{$section});
 	output_highlight_rst($args{'sections'}{$section});
 	print "\n";
     }
@@ -1824,6 +1847,7 @@ sub output_function_rst(%) {
 	}
     }
     print ")\n\n";
+    print_lineno($declaration_start_line);
     $lineprefix = "   ";
     output_highlight_rst($args{'purpose'});
     print "\n";
@@ -1840,6 +1864,9 @@ sub output_function_rst(%) {
 	} else {
 	    print "``$parameter``\n";
 	}
+
+        print_lineno($parameterdesc_start_lines{$parameter_name});
+
 	if (defined($args{'parameterdescs'}{$parameter_name}) &&
 	    $args{'parameterdescs'}{$parameter_name} ne $undescribed) {
 	    output_highlight_rst($args{'parameterdescs'}{$parameter_name});
@@ -1861,6 +1888,7 @@ sub output_section_rst(%) {
 
     foreach $section (@{$args{'sectionlist'}}) {
 	print "**$section**\n\n";
+        print_lineno($section_start_lines{$section});
 	output_highlight_rst($args{'sections'}{$section});
 	print "\n";
     }
@@ -1876,6 +1904,7 @@ sub output_enum_rst(%) {
     my $name = "enum " . $args{'enum'};
 
     print "\n\n.. c:type:: " . $name . "\n\n";
+    print_lineno($declaration_start_line);
     $lineprefix = "   ";
     output_highlight_rst($args{'purpose'});
     print "\n";
@@ -1903,6 +1932,7 @@ sub output_typedef_rst(%) {
     my $name = "typedef " . $args{'typedef'};
 
     print "\n\n.. c:type:: " . $name . "\n\n";
+    print_lineno($declaration_start_line);
     $lineprefix = "   ";
     output_highlight_rst($args{'purpose'});
     print "\n";
@@ -1918,6 +1948,7 @@ sub output_struct_rst(%) {
     my $name = $args{'type'} . " " . $args{'struct'};
 
     print "\n\n.. c:type:: " . $name . "\n\n";
+    print_lineno($declaration_start_line);
     $lineprefix = "   ";
     output_highlight_rst($args{'purpose'});
     print "\n";
@@ -1958,6 +1989,7 @@ sub output_struct_rst(%) {
 
 	($args{'parameterdescs'}{$parameter_name} ne $undescribed) || next;
 	$type = $args{'parametertypes'}{$parameter};
+        print_lineno($parameterdesc_start_lines{$parameter_name});
 	print "``$type $parameter``\n";
 	output_highlight_rst($args{'parameterdescs'}{$parameter_name});
 	print "\n";
@@ -2745,11 +2777,14 @@ sub process_file($) {
 	    if (/$doc_start/o) {
 		$state = STATE_NAME;	# next line is always the function name
 		$in_doc_sect = 0;
+		$declaration_start_line = $. + 1;
 	    }
 	} elsif ($state == STATE_NAME) {# this line is the function name (always)
 	    if (/$doc_block/o) {
 		$state = STATE_DOCBLOCK;
 		$contents = "";
+                $new_start_line = $. + 1;
+
 		if ( $1 eq "" ) {
 			$section = $section_intro;
 		} else {
@@ -2763,8 +2798,11 @@ sub process_file($) {
 		}
 
 		$state = STATE_FIELD;
+		# if there's no @param blocks need to set up default section
+		# here
 		$contents = "";
 		$section = $section_default;
+		$new_start_line = $. + 1;
 		if (/-(.*)/) {
 		    # strip leading/trailing/multiple spaces
 		    $descr= $1;
@@ -2833,6 +2871,7 @@ sub process_file($) {
 		$in_doc_sect = 1;
 		$in_purpose = 0;
 		$contents = $newcontents;
+                $new_start_line = $.;
 		while ((substr($contents, 0, 1) eq " ") ||
 		       substr($contents, 0, 1) eq "\t") {
 		    $contents = substr($contents, 1);
@@ -2866,6 +2905,7 @@ sub process_file($) {
 			dump_section($file, $section, xml_escape($contents));
 			$section = $section_default;
 			$contents = "";
+                        $new_start_line = $.;
 		    } else {
 			$contents .= "\n";
 		    }
@@ -2900,6 +2940,7 @@ sub process_file($) {
 	    if ($inline_doc_state == STATE_INLINE_NAME && /$doc_inline_sect/o) {
 		$section = $1;
 		$contents = $2;
+                $new_start_line = $.;
 		if ($contents ne "") {
 		    while ((substr($contents, 0, 1) eq " ") ||
 		           substr($contents, 0, 1) eq "\t") {
-- 
2.1.4

  parent reply	other threads:[~2016-06-04 11:41 UTC|newest]

Thread overview: 47+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-06-04 11:37 [PATCH v2 00/38] Documentation/sphinx Jani Nikula
2016-06-04 11:37 ` [PATCH v2 01/38] kernel-doc/rst: fix use of uninitialized value Jani Nikula
2016-06-04 11:37 ` [PATCH v2 02/38] kernel-doc: support printing exported and non-exported symbols Jani Nikula
2016-06-04 11:37 ` [PATCH v2 03/38] Documentation/sphinx: add basic working Sphinx configuration and build Jani Nikula
2016-06-04 11:37 ` [PATCH v2 04/38] Documentation: add .gitignore Jani Nikula
2016-06-04 11:37 ` [PATCH v2 05/38] Documentation/sphinx: add Sphinx kernel-doc directive extension Jani Nikula
2016-06-04 11:37 ` [PATCH v2 06/38] Documentation/sphinx: configure the kernel-doc extension Jani Nikula
2016-06-04 11:37 ` [PATCH v2 07/38] Documentation/sphinx: set version and release properly Jani Nikula
2016-06-04 11:37 ` [PATCH v2 08/38] sphinx: cheesy script to convert .tmpl files Jani Nikula
2016-06-04 11:37 ` [PATCH v2 09/38] sphinx: update docbook->rst conversion script match C domain spec Jani Nikula
2016-06-04 11:37 ` [PATCH v2 10/38] Documentation/sphinx: nicer referencing of struct in docbook->rst conversion Jani Nikula
2016-06-04 11:37 ` [PATCH v2 11/38] kernel-doc: add names for states and substates Jani Nikula
2016-06-04 11:37 ` [PATCH v2 12/38] kernel-doc: add names for output selection Jani Nikula
2016-06-04 11:37 ` [PATCH v2 13/38] kernel-doc/rst: do not output DOC: section titles for requested ones Jani Nikula
2016-06-04 11:37 ` [PATCH v2 14/38] kernel-doc/rst: reference functions according to C domain spec Jani Nikula
2016-06-04 11:37 ` [PATCH v2 15/38] kernel-doc/rst: &foo references are more universal than structs Jani Nikula
2016-06-04 11:37 ` [PATCH v2 16/38] kernel-doc/rst: add support for &union foo and &typedef foo references Jani Nikula
2016-06-04 11:37 ` [PATCH v2 17/38] kernel-doc/rst: add support for struct/union/enum member references Jani Nikula
2016-06-04 11:37 ` [PATCH v2 18/38] kernel-doc/rst: drop redundant unescape in highlighting Jani Nikula
2016-06-04 11:37 ` [PATCH v2 19/38] kernel-doc/rst: highlight function/struct/enum purpose lines too Jani Nikula
2016-06-04 11:37 ` [PATCH v2 20/38] kernel-doc: do not regard $, %, or & prefixes as special in section names Jani Nikula
2016-06-04 11:37 ` [PATCH v2 21/38] kernel-doc: fix wrong code indentation Jani Nikula
2016-06-04 11:37 ` [PATCH v2 22/38] kernel-doc/rst: blank lines in output are not needed Jani Nikula
2016-06-04 11:37 ` [PATCH v2 23/38] kernel-doc: strip leading blank lines from inline doc comments Jani Nikula
2016-06-04 11:37 ` [PATCH v2 24/38] kernel-doc/rst: change the output layout Jani Nikula
2016-06-04 11:37 ` [PATCH v2 25/38] kernel-doc: improve handling of whitespace on the first line param description Jani Nikula
2016-06-04 11:37 ` [PATCH v2 26/38] kernel-doc: strip leading whitespace from continued param descs Jani Nikula
2016-06-04 11:37 ` [PATCH v2 27/38] kernel-doc/rst: use *undescribed* instead of _undescribed_ Jani Nikula
2016-06-04 11:37 ` [PATCH v2 28/38] kernel-doc/rst: remove fixme comment Jani Nikula
2016-06-04 11:37 ` [PATCH v2 29/38] kernel-doc: limit the "section header:" detection to a select few Jani Nikula
2016-06-09 15:03   ` Jonathan Corbet
2016-06-09 16:39     ` Jani Nikula
2016-06-04 11:37 ` [PATCH v2 30/38] kernel-doc: concatenate contents of colliding sections Jani Nikula
2016-06-04 11:37 ` [PATCH v2 31/38] kernel-doc: reset contents and section harder Jani Nikula
2016-06-04 11:37 ` [PATCH v2 32/38] Documentation/sphinx: fix kernel-doc extension on python3 Jani Nikula
2016-06-04 11:37 ` [PATCH v2 33/38] doc/sphinx: Pass right filename as source Jani Nikula
2016-06-04 11:37 ` [PATCH v2 34/38] scripts/kernel-doc: Remove duplicated DOC: start handling Jani Nikula
2016-06-04 11:37 ` [PATCH v2 35/38] doc/sphinx: Stop touching state_machine internals Jani Nikula
2016-06-04 11:37 ` [PATCH v2 36/38] scripts/kernel-doc: Also give functions symbolic names Jani Nikula
2016-06-04 11:37 ` Jani Nikula [this message]
2016-06-04 11:37 ` [PATCH v2 38/38] doc/sphinx: Track line-number of starting blocks Jani Nikula
2016-06-04 12:15 ` [PATCH v2 00/38] Documentation/sphinx Daniel Vetter
2016-06-09 19:55 ` Jonathan Corbet
2016-06-10 18:17   ` Daniel Vetter
2016-06-10 20:41     ` Dave Airlie
2016-06-14  8:15       ` Daniel Vetter
2016-06-14 19:23         ` Jonathan Corbet

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=0b0f5f29b282b18d26ce698e1aab0267234f77bf.1465031816.git.jani.nikula@intel.com \
    --to=jani.nikula@intel.com \
    --cc=corbet@lwn.net \
    --cc=daniel.vetter@ffwll.ch \
    --cc=grant.likely@secretlab.ca \
    --cc=hverkuil@xs4all.nl \
    --cc=keithp@keithp.com \
    --cc=linux-doc@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=markus.heiser@darmarit.de \
    --cc=mchehab@osg.samsung.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).