2 # SPDX-License-Identifier: GPL-2.0
7 ## Copyright (c) 1998 Michael Zucchi, All Rights Reserved ##
9 ## Copyright (C) 2001 Simon Huggins ##
10 ## Copyright (C) 2005-2012 Randy Dunlap ##
11 ## Copyright (C) 2012 Dan Luedtke ##
14 ## Copyright (c) 2000 MontaVista Software, Inc. ##
16 # Copyright (C) 2022 Tomasz Warniełło (POD)
18 use Pod::Usage qw/pod2usage/;
22 kernel-doc - Print formatted kernel documentation to stdout
26 kernel-doc [-h] [-v] [-Werror]
28 -rst [-sphinx-version VERSION] [-enable-lineno] |
34 [-function NAME] ... |
38 [-export-file FILE] ...
41 Run `kernel-doc -h` for details.
45 Read C language source or header FILEs, extract embedded documentation comments,
46 and print formatted documentation to standard output.
48 The documentation comments are identified by the "/**" opening comment mark.
50 See Documentation/doc-guide/kernel-doc.rst for the documentation comment syntax.
54 # more perldoc at the end of the file
60 my $anon_struct_union = 0;
62 # match expressions used to find embedded type information
63 my $type_constant = '\b``([^\`]+)``\b';
64 my $type_constant2 = '\%([-_\w]+)';
65 my $type_func = '(\w+)\(\)';
66 my $type_param = '\@(\w*((\.\w+)|(->\w+))*(\.\.\.)?)';
67 my $type_param_ref = '([\!]?)\@(\w*((\.\w+)|(->\w+))*(\.\.\.)?)';
68 my $type_fp_param = '\@(\w+)\(\)'; # Special RST handling for func ptr params
69 my $type_fp_param2 = '\@(\w+->\S+)\(\)'; # Special RST handling for structs with func ptr params
70 my $type_env = '(\$\w+)';
71 my $type_enum = '\&(enum\s*([_\w]+))';
72 my $type_struct = '\&(struct\s*([_\w]+))';
73 my $type_typedef = '\&(typedef\s*([_\w]+))';
74 my $type_union = '\&(union\s*([_\w]+))';
75 my $type_member = '\&([_\w]+)(\.|->)([_\w]+)';
76 my $type_fallback = '\&([_\w]+)';
77 my $type_member_func = $type_member . '\(\)';
79 # Output conversion substitutions.
80 # One for each output format
82 # these are pretty rough
83 my @highlights_man = (
84 [$type_constant, "\$1"],
85 [$type_constant2, "\$1"],
86 [$type_func, "\\\\fB\$1\\\\fP"],
87 [$type_enum, "\\\\fI\$1\\\\fP"],
88 [$type_struct, "\\\\fI\$1\\\\fP"],
89 [$type_typedef, "\\\\fI\$1\\\\fP"],
90 [$type_union, "\\\\fI\$1\\\\fP"],
91 [$type_param, "\\\\fI\$1\\\\fP"],
92 [$type_param_ref, "\\\\fI\$1\$2\\\\fP"],
93 [$type_member, "\\\\fI\$1\$2\$3\\\\fP"],
94 [$type_fallback, "\\\\fI\$1\\\\fP"]
96 my $blankline_man = "";
99 my @highlights_rst = (
100 [$type_constant, "``\$1``"],
101 [$type_constant2, "``\$1``"],
102 # Note: need to escape () to avoid func matching later
103 [$type_member_func, "\\:c\\:type\\:`\$1\$2\$3\\\\(\\\\) <\$1>`"],
104 [$type_member, "\\:c\\:type\\:`\$1\$2\$3 <\$1>`"],
105 [$type_fp_param, "**\$1\\\\(\\\\)**"],
106 [$type_fp_param2, "**\$1\\\\(\\\\)**"],
107 [$type_func, "\$1()"],
108 [$type_enum, "\\:c\\:type\\:`\$1 <\$2>`"],
109 [$type_struct, "\\:c\\:type\\:`\$1 <\$2>`"],
110 [$type_typedef, "\\:c\\:type\\:`\$1 <\$2>`"],
111 [$type_union, "\\:c\\:type\\:`\$1 <\$2>`"],
112 # in rst this can refer to any type
113 [$type_fallback, "\\:c\\:type\\:`\$1`"],
114 [$type_param_ref, "**\$1\$2**"]
116 my $blankline_rst = "\n";
121 -message => "No arguments!\n",
124 -sections => 'SYNOPSIS',
130 my ($sphinx_major, $sphinx_minor, $sphinx_patch);
132 my $dohighlight = "";
136 my $output_mode = "rst";
137 my $output_preformatted = 0;
138 my $no_doc_sections = 0;
139 my $enable_lineno = 0;
140 my @highlights = @highlights_rst;
141 my $blankline = $blankline_rst;
142 my $modulename = "Kernel API";
145 OUTPUT_ALL => 0, # output all symbols and doc sections
146 OUTPUT_INCLUDE => 1, # output only specified symbols
147 OUTPUT_EXPORTED => 2, # output exported symbols
148 OUTPUT_INTERNAL => 3, # output non-exported symbols
150 my $output_selection = OUTPUT_ALL;
151 my $show_not_found = 0; # No longer used
153 my @export_file_list;
156 if (defined($ENV{'KBUILD_BUILD_TIMESTAMP'}) &&
157 (my $seconds = `date -d"${ENV{'KBUILD_BUILD_TIMESTAMP'}}" +%s`) ne '') {
158 @build_time = gmtime($seconds);
160 @build_time = localtime;
163 my $man_date = ('January', 'February', 'March', 'April', 'May', 'June',
164 'July', 'August', 'September', 'October',
165 'November', 'December')[$build_time[4]] .
166 " " . ($build_time[5]+1900);
168 # Essentially these are globals.
169 # They probably want to be tidied up, made more localised or something.
170 # CAVEAT EMPTOR! Some of the others I localised may not want to be, which
171 # could cause "use of undefined value" or other bugs.
172 my ($function, %function_table, %parametertypes, $declaration_purpose);
173 my %nosymbol_table = ();
174 my $declaration_start_line;
175 my ($type, $declaration_name, $return_type);
176 my ($newsection, $newcontents, $prototype, $brcount, %source_map);
178 if (defined($ENV{'KBUILD_VERBOSE'})) {
179 $verbose = "$ENV{'KBUILD_VERBOSE'}";
182 if (defined($ENV{'KCFLAGS'})) {
183 my $kcflags = "$ENV{'KCFLAGS'}";
185 if ($kcflags =~ /Werror/) {
190 if (defined($ENV{'KDOC_WERROR'})) {
191 $Werror = "$ENV{'KDOC_WERROR'}";
194 # Generated docbook code is inserted in a template at a point where
195 # docbook v3.1 requires a non-zero sequence of RefEntry's; see:
196 # https://www.oasis-open.org/docbook/documentation/reference/html/refentry.html
197 # We keep track of number of generated entries and generate a dummy
198 # if needs be to ensure the expanded template can be postprocessed
200 my $section_counter = 0;
206 STATE_NORMAL => 0, # normal code
207 STATE_NAME => 1, # looking for function name
208 STATE_BODY_MAYBE => 2, # body - or maybe more description
209 STATE_BODY => 3, # the body of the comment
210 STATE_BODY_WITH_BLANK_LINE => 4, # the body, which has a blank line
211 STATE_PROTO => 5, # scanning prototype
212 STATE_DOCBLOCK => 6, # documentation block
213 STATE_INLINE => 7, # gathering doc outside main block
219 # Inline documentation state
221 STATE_INLINE_NA => 0, # not applicable ($state != STATE_INLINE)
222 STATE_INLINE_NAME => 1, # looking for member name (@foo:)
223 STATE_INLINE_TEXT => 2, # looking for member documentation
224 STATE_INLINE_END => 3, # done
225 STATE_INLINE_ERROR => 4, # error - Comment without header was found.
226 # Spit a warning as it's not
227 # proper kernel-doc and ignore the rest.
229 my $inline_doc_state;
231 #declaration types: can be
232 # 'function', 'struct', 'union', 'enum', 'typedef'
235 # Name of the kernel-doc identifier for non-DOC markups
238 my $doc_start = '^/\*\*\s*$'; # Allow whitespace at end of comment start.
240 my $doc_com = '\s*\*\s*';
241 my $doc_com_body = '\s*\* ?';
242 my $doc_decl = $doc_com . '(\w+)';
243 # @params and a strictly limited set of supported section names
248 # while trying to not match literal block starts like "example::"
250 my $doc_sect = $doc_com .
251 '\s*(\@[.\w]+|\@\.\.\.|description|context|returns?|notes?|examples?)\s*:([^:].*)?$';
252 my $doc_content = $doc_com_body . '(.*)';
253 my $doc_block = $doc_com . 'DOC:\s*(.*)?';
254 my $doc_inline_start = '^\s*/\*\*\s*$';
255 my $doc_inline_sect = '\s*\*\s*(@\s*[\w][\w\.]*\s*):(.*)';
256 my $doc_inline_end = '^\s*\*/\s*$';
257 my $doc_inline_oneline = '^\s*/\*\*\s*(@[\w\s]+):\s*(.*)\s*\*/\s*$';
258 my $export_symbol = '^\s*EXPORT_SYMBOL(_GPL)?\s*\(\s*(\w+)\s*\)\s*;';
259 my $function_pointer = qr{([^\(]*\(\*)\s*\)\s*\(([^\)]*)\)};
260 my $attribute = qr{__attribute__\s*\(\([a-z0-9,_\*\s\(\)]*\)\)}i;
263 my %parameterdesc_start_lines;
267 my %section_start_lines;
272 my $new_start_line = 0;
274 # the canonical section names. see also $doc_sect above.
275 my $section_default = "Description"; # default section
276 my $section_intro = "Introduction";
277 my $section = $section_default;
278 my $section_context = "Context";
279 my $section_return = "Return";
281 my $undescribed = "-- undescribed --";
285 while ($ARGV[0] =~ m/^--?(.*)/) {
289 $output_mode = "man";
290 @highlights = @highlights_man;
291 $blankline = $blankline_man;
292 } elsif ($cmd eq "rst") {
293 $output_mode = "rst";
294 @highlights = @highlights_rst;
295 $blankline = $blankline_rst;
296 } elsif ($cmd eq "none") {
297 $output_mode = "none";
298 } elsif ($cmd eq "module") { # not needed for XML, inherits from calling document
299 $modulename = shift @ARGV;
300 } elsif ($cmd eq "function") { # to only output specific functions
301 $output_selection = OUTPUT_INCLUDE;
302 $function = shift @ARGV;
303 $function_table{$function} = 1;
304 } elsif ($cmd eq "nosymbol") { # Exclude specific symbols
305 my $symbol = shift @ARGV;
306 $nosymbol_table{$symbol} = 1;
307 } elsif ($cmd eq "export") { # only exported symbols
308 $output_selection = OUTPUT_EXPORTED;
309 %function_table = ();
310 } elsif ($cmd eq "internal") { # only non-exported symbols
311 $output_selection = OUTPUT_INTERNAL;
312 %function_table = ();
313 } elsif ($cmd eq "export-file") {
314 my $file = shift @ARGV;
315 push(@export_file_list, $file);
316 } elsif ($cmd eq "v") {
318 } elsif ($cmd eq "Werror") {
320 } elsif (($cmd eq "h") || ($cmd eq "help")) {
321 pod2usage(-exitval => 0, -verbose => 2);
322 } elsif ($cmd eq 'no-doc-sections') {
323 $no_doc_sections = 1;
324 } elsif ($cmd eq 'enable-lineno') {
326 } elsif ($cmd eq 'show-not-found') {
327 $show_not_found = 1; # A no-op but don't fail
328 } elsif ($cmd eq "sphinx-version") {
329 my $ver_string = shift @ARGV;
330 if ($ver_string =~ m/^(\d+)(\.\d+)?(\.\d+)?/) {
333 $sphinx_minor = substr($2,1);
338 $sphinx_patch = substr($3,1)
343 die "Sphinx version should either major.minor or major.minor.patch format\n";
348 -message => "Argument unknown!\n",
351 -sections => 'SYNOPSIS',
357 -message => "FILE argument missing\n",
360 -sections => 'SYNOPSIS',
366 # continue execution near EOF;
368 # The C domain dialect changed on Sphinx 3. So, we need to check the
369 # version in order to produce the right tags.
372 foreach(split(/:/, $ENV{PATH})) {
373 return "$_/$_[0]" if(-x "$_/$_[0]");
377 sub get_sphinx_version()
381 my $cmd = "sphinx-build";
382 if (!findprog($cmd)) {
383 my $cmd = "sphinx-build3";
384 if (!findprog($cmd)) {
388 printf STDERR "Warning: Sphinx version not found. Using default (Sphinx version %d.%d.%d)\n",
389 $sphinx_major, $sphinx_minor, $sphinx_patch;
394 open IN, "$cmd --version 2>&1 |";
396 if (m/^\s*sphinx-build\s+([\d]+)\.([\d\.]+)(\+\/[\da-f]+)?$/) {
402 # Sphinx 1.2.x uses a different format
403 if (m/^\s*Sphinx.*\s+([\d]+)\.([\d\.]+)$/) {
413 # get kernel version from env
414 sub get_kernel_version() {
415 my $version = 'unknown kernel version';
417 if (defined($ENV{'KERNELVERSION'})) {
418 $version = $ENV{'KERNELVERSION'};
426 if ($enable_lineno && defined($lineno)) {
427 print ".. LINENO " . $lineno . "\n";
432 my $location = shift;
434 print STDERR "$location: warning: $msg";
438 # dumps section contents to arrays/hashes intended for that purpose.
443 my $contents = join "\n", @_;
445 if ($name =~ m/$type_param/) {
447 $parameterdescs{$name} = $contents;
448 $sectcheck = $sectcheck . $name . " ";
449 $parameterdesc_start_lines{$name} = $new_start_line;
451 } elsif ($name eq "@\.\.\.") {
453 $parameterdescs{$name} = $contents;
454 $sectcheck = $sectcheck . $name . " ";
455 $parameterdesc_start_lines{$name} = $new_start_line;
458 if (defined($sections{$name}) && ($sections{$name} ne "")) {
459 # Only warn on user specified duplicate section names.
460 if ($name ne $section_default) {
461 emit_warning("${file}:$.", "duplicate section name '$name'\n");
463 $sections{$name} .= $contents;
465 $sections{$name} = $contents;
466 push @sectionlist, $name;
467 $section_start_lines{$name} = $new_start_line;
474 # dump DOC: section after checking that it should go out
476 sub dump_doc_section {
479 my $contents = join "\n", @_;
481 if ($no_doc_sections) {
485 return if (defined($nosymbol_table{$name}));
487 if (($output_selection == OUTPUT_ALL) ||
488 (($output_selection == OUTPUT_INCLUDE) &&
489 defined($function_table{$name})))
491 dump_section($file, $name, $contents);
492 output_blockhead({'sectionlist' => \@sectionlist,
493 'sections' => \%sections,
494 'module' => $modulename,
495 'content-only' => ($output_selection != OUTPUT_ALL), });
502 # parameterdescs, a hash.
503 # function => "function name"
504 # parameterlist => @list of parameters
505 # parameterdescs => %parameter descriptions
506 # sectionlist => @list of sections
507 # sections => %section descriptions
510 sub output_highlight {
511 my $contents = join "\n",@_;
515 # if (!defined $contents) {
517 # confess "output_highlight got called with no args?\n";
520 # print STDERR "contents b4:$contents\n";
523 # print STDERR "contents af:$contents\n";
525 foreach $line (split "\n", $contents) {
526 if (! $output_preformatted) {
530 if (! $output_preformatted) {
531 print $lineprefix, $blankline;
534 if ($output_mode eq "man" && substr($line, 0, 1) eq ".") {
537 print $lineprefix, $line;
545 # output function in man
546 sub output_function_man(%) {
548 my ($parameter, $section);
551 print ".TH \"$args{'function'}\" 9 \"$args{'function'}\" \"$man_date\" \"Kernel Hacker's Manual\" LINUX\n";
554 print $args{'function'} . " \\- " . $args{'purpose'} . "\n";
556 print ".SH SYNOPSIS\n";
557 if ($args{'functiontype'} ne "") {
558 print ".B \"" . $args{'functiontype'} . "\" " . $args{'function'} . "\n";
560 print ".B \"" . $args{'function'} . "\n";
565 foreach my $parameter (@{$args{'parameterlist'}}) {
566 if ($count == $#{$args{'parameterlist'}}) {
569 $type = $args{'parametertypes'}{$parameter};
570 if ($type =~ m/$function_pointer/) {
571 # pointer-to-function
572 print ".BI \"" . $parenth . $1 . "\" " . " \") (" . $2 . ")" . $post . "\"\n";
574 $type =~ s/([^\*])$/$1 /;
575 print ".BI \"" . $parenth . $type . "\" " . " \"" . $post . "\"\n";
581 print ".SH ARGUMENTS\n";
582 foreach $parameter (@{$args{'parameterlist'}}) {
583 my $parameter_name = $parameter;
584 $parameter_name =~ s/\[.*//;
586 print ".IP \"" . $parameter . "\" 12\n";
587 output_highlight($args{'parameterdescs'}{$parameter_name});
589 foreach $section (@{$args{'sectionlist'}}) {
590 print ".SH \"", uc $section, "\"\n";
591 output_highlight($args{'sections'}{$section});
597 sub output_enum_man(%) {
599 my ($parameter, $section);
602 print ".TH \"$args{'module'}\" 9 \"enum $args{'enum'}\" \"$man_date\" \"API Manual\" LINUX\n";
605 print "enum " . $args{'enum'} . " \\- " . $args{'purpose'} . "\n";
607 print ".SH SYNOPSIS\n";
608 print "enum " . $args{'enum'} . " {\n";
610 foreach my $parameter (@{$args{'parameterlist'}}) {
611 print ".br\n.BI \" $parameter\"\n";
612 if ($count == $#{$args{'parameterlist'}}) {
622 print ".SH Constants\n";
623 foreach $parameter (@{$args{'parameterlist'}}) {
624 my $parameter_name = $parameter;
625 $parameter_name =~ s/\[.*//;
627 print ".IP \"" . $parameter . "\" 12\n";
628 output_highlight($args{'parameterdescs'}{$parameter_name});
630 foreach $section (@{$args{'sectionlist'}}) {
631 print ".SH \"$section\"\n";
632 output_highlight($args{'sections'}{$section});
637 # output struct in man
638 sub output_struct_man(%) {
640 my ($parameter, $section);
642 print ".TH \"$args{'module'}\" 9 \"" . $args{'type'} . " " . $args{'struct'} . "\" \"$man_date\" \"API Manual\" LINUX\n";
645 print $args{'type'} . " " . $args{'struct'} . " \\- " . $args{'purpose'} . "\n";
647 my $declaration = $args{'definition'};
648 $declaration =~ s/\t/ /g;
649 $declaration =~ s/\n/"\n.br\n.BI \"/g;
650 print ".SH SYNOPSIS\n";
651 print $args{'type'} . " " . $args{'struct'} . " {\n.br\n";
652 print ".BI \"$declaration\n};\n.br\n\n";
654 print ".SH Members\n";
655 foreach $parameter (@{$args{'parameterlist'}}) {
656 ($parameter =~ /^#/) && next;
658 my $parameter_name = $parameter;
659 $parameter_name =~ s/\[.*//;
661 ($args{'parameterdescs'}{$parameter_name} ne $undescribed) || next;
662 print ".IP \"" . $parameter . "\" 12\n";
663 output_highlight($args{'parameterdescs'}{$parameter_name});
665 foreach $section (@{$args{'sectionlist'}}) {
666 print ".SH \"$section\"\n";
667 output_highlight($args{'sections'}{$section});
672 # output typedef in man
673 sub output_typedef_man(%) {
675 my ($parameter, $section);
677 print ".TH \"$args{'module'}\" 9 \"$args{'typedef'}\" \"$man_date\" \"API Manual\" LINUX\n";
680 print "typedef " . $args{'typedef'} . " \\- " . $args{'purpose'} . "\n";
682 foreach $section (@{$args{'sectionlist'}}) {
683 print ".SH \"$section\"\n";
684 output_highlight($args{'sections'}{$section});
688 sub output_blockhead_man(%) {
690 my ($parameter, $section);
693 print ".TH \"$args{'module'}\" 9 \"$args{'module'}\" \"$man_date\" \"API Manual\" LINUX\n";
695 foreach $section (@{$args{'sectionlist'}}) {
696 print ".SH \"$section\"\n";
697 output_highlight($args{'sections'}{$section});
702 # output in restructured text
706 # This could use some work; it's used to output the DOC: sections, and
707 # starts by putting out the name of the doc section itself, but that tends
708 # to duplicate a header already in the template file.
710 sub output_blockhead_rst(%) {
712 my ($parameter, $section);
714 foreach $section (@{$args{'sectionlist'}}) {
715 next if (defined($nosymbol_table{$section}));
717 if ($output_selection != OUTPUT_INCLUDE) {
718 print ".. _$section:\n\n";
719 print "**$section**\n\n";
721 print_lineno($section_start_lines{$section});
722 output_highlight_rst($args{'sections'}{$section});
728 # Apply the RST highlights to a sub-block of text.
730 sub highlight_block($) {
731 # The dohighlight kludge requires the text be called $contents
732 my $contents = shift;
739 # Regexes used only here.
741 my $sphinx_literal = '^[^.].*::$';
742 my $sphinx_cblock = '^\.\.\ +code-block::';
744 sub output_highlight_rst {
745 my $input = join "\n",@_;
752 foreach $line (split "\n",$input) {
754 # If we're in a literal block, see if we should drop out
755 # of it. Otherwise pass the line straight through unmunged.
758 if (! ($line =~ /^\s*$/)) {
760 # If this is the first non-blank line in a literal
761 # block we need to figure out what the proper indent is.
763 if ($litprefix eq "") {
765 $litprefix = '^' . $1;
766 $output .= $line . "\n";
767 } elsif (! ($line =~ /$litprefix/)) {
770 $output .= $line . "\n";
773 $output .= $line . "\n";
777 # Not in a literal block (or just dropped out)
780 $block .= $line . "\n";
781 if (($line =~ /$sphinx_literal/) || ($line =~ /$sphinx_cblock/)) {
784 $output .= highlight_block($block);
791 $output .= highlight_block($block);
793 foreach $line (split "\n", $output) {
794 print $lineprefix . $line . "\n";
798 sub output_function_rst(%) {
800 my ($parameter, $section);
801 my $oldprefix = $lineprefix;
805 if ($sphinx_major < 3) {
806 if ($args{'typedef'}) {
807 print ".. c:type:: ". $args{'function'} . "\n\n";
808 print_lineno($declaration_start_line);
809 print " **Typedef**: ";
811 output_highlight_rst($args{'purpose'});
812 $start = "\n\n**Syntax**\n\n ``";
815 print ".. c:function:: ";
818 if ($args{'typedef'} || $args{'functiontype'} eq "") {
820 print ".. c:macro:: ". $args{'function'} . "\n\n";
822 print ".. c:function:: ";
825 if ($args{'typedef'}) {
826 print_lineno($declaration_start_line);
827 print " **Typedef**: ";
829 output_highlight_rst($args{'purpose'});
830 $start = "\n\n**Syntax**\n\n ``";
832 print "``" if ($is_macro);
835 if ($args{'functiontype'} ne "") {
836 $start .= $args{'functiontype'} . " " . $args{'function'} . " (";
838 $start .= $args{'function'} . " (";
843 foreach my $parameter (@{$args{'parameterlist'}}) {
848 $type = $args{'parametertypes'}{$parameter};
850 if ($type =~ m/$function_pointer/) {
851 # pointer-to-function
852 print $1 . $parameter . ") (" . $2 . ")";
862 if (!$args{'typedef'}) {
863 print_lineno($declaration_start_line);
865 output_highlight_rst($args{'purpose'});
870 # Put our descriptive text into a container (thus an HTML <div>) to help
871 # set the function prototypes apart.
873 print ".. container:: kernelindent\n\n";
875 print $lineprefix . "**Parameters**\n\n";
876 foreach $parameter (@{$args{'parameterlist'}}) {
877 my $parameter_name = $parameter;
878 $parameter_name =~ s/\[.*//;
879 $type = $args{'parametertypes'}{$parameter};
882 print $lineprefix . "``$type``\n";
884 print $lineprefix . "``$parameter``\n";
887 print_lineno($parameterdesc_start_lines{$parameter_name});
890 if (defined($args{'parameterdescs'}{$parameter_name}) &&
891 $args{'parameterdescs'}{$parameter_name} ne $undescribed) {
892 output_highlight_rst($args{'parameterdescs'}{$parameter_name});
894 print $lineprefix . "*undescribed*\n";
900 output_section_rst(@_);
901 $lineprefix = $oldprefix;
904 sub output_section_rst(%) {
907 my $oldprefix = $lineprefix;
909 foreach $section (@{$args{'sectionlist'}}) {
910 print $lineprefix . "**$section**\n\n";
911 print_lineno($section_start_lines{$section});
912 output_highlight_rst($args{'sections'}{$section});
918 sub output_enum_rst(%) {
921 my $oldprefix = $lineprefix;
925 if ($sphinx_major < 3) {
926 my $name = "enum " . $args{'enum'};
927 print "\n\n.. c:type:: " . $name . "\n\n";
929 my $name = $args{'enum'};
930 print "\n\n.. c:enum:: " . $name . "\n\n";
932 print_lineno($declaration_start_line);
934 output_highlight_rst($args{'purpose'});
937 print ".. container:: kernelindent\n\n";
938 $outer = $lineprefix . " ";
939 $lineprefix = $outer . " ";
940 print $outer . "**Constants**\n\n";
941 foreach $parameter (@{$args{'parameterlist'}}) {
942 print $outer . "``$parameter``\n";
944 if ($args{'parameterdescs'}{$parameter} ne $undescribed) {
945 output_highlight_rst($args{'parameterdescs'}{$parameter});
947 print $lineprefix . "*undescribed*\n";
952 $lineprefix = $oldprefix;
953 output_section_rst(@_);
956 sub output_typedef_rst(%) {
959 my $oldprefix = $lineprefix;
962 if ($sphinx_major < 3) {
963 $name = "typedef " . $args{'typedef'};
965 $name = $args{'typedef'};
967 print "\n\n.. c:type:: " . $name . "\n\n";
968 print_lineno($declaration_start_line);
970 output_highlight_rst($args{'purpose'});
973 $lineprefix = $oldprefix;
974 output_section_rst(@_);
977 sub output_struct_rst(%) {
980 my $oldprefix = $lineprefix;
982 if ($sphinx_major < 3) {
983 my $name = $args{'type'} . " " . $args{'struct'};
984 print "\n\n.. c:type:: " . $name . "\n\n";
986 my $name = $args{'struct'};
987 if ($args{'type'} eq 'union') {
988 print "\n\n.. c:union:: " . $name . "\n\n";
990 print "\n\n.. c:struct:: " . $name . "\n\n";
993 print_lineno($declaration_start_line);
995 output_highlight_rst($args{'purpose'});
998 print ".. container:: kernelindent\n\n";
999 print $lineprefix . "**Definition**::\n\n";
1000 my $declaration = $args{'definition'};
1001 $lineprefix = $lineprefix . " ";
1002 $declaration =~ s/\t/$lineprefix/g;
1003 print $lineprefix . $args{'type'} . " " . $args{'struct'} . " {\n$declaration" . $lineprefix . "};\n\n";
1006 print $lineprefix . "**Members**\n\n";
1007 foreach $parameter (@{$args{'parameterlist'}}) {
1008 ($parameter =~ /^#/) && next;
1010 my $parameter_name = $parameter;
1011 $parameter_name =~ s/\[.*//;
1013 ($args{'parameterdescs'}{$parameter_name} ne $undescribed) || next;
1014 $type = $args{'parametertypes'}{$parameter};
1015 print_lineno($parameterdesc_start_lines{$parameter_name});
1016 print $lineprefix . "``" . $parameter . "``\n";
1018 output_highlight_rst($args{'parameterdescs'}{$parameter_name});
1024 $lineprefix = $oldprefix;
1025 output_section_rst(@_);
1028 ## none mode output functions
1030 sub output_function_none(%) {
1033 sub output_enum_none(%) {
1036 sub output_typedef_none(%) {
1039 sub output_struct_none(%) {
1042 sub output_blockhead_none(%) {
1046 # generic output function for all types (function, struct/union, typedef, enum);
1047 # calls the generated, variable output_ function name based on
1048 # functype and output_mode
1049 sub output_declaration {
1052 my $functype = shift;
1053 my $func = "output_${functype}_$output_mode";
1055 return if (defined($nosymbol_table{$name}));
1057 if (($output_selection == OUTPUT_ALL) ||
1058 (($output_selection == OUTPUT_INCLUDE ||
1059 $output_selection == OUTPUT_EXPORTED) &&
1060 defined($function_table{$name})) ||
1061 ($output_selection == OUTPUT_INTERNAL &&
1062 !($functype eq "function" && defined($function_table{$name}))))
1070 # generic output function - calls the right one based on current output mode.
1071 sub output_blockhead {
1073 my $func = "output_blockhead_" . $output_mode;
1079 # takes a declaration (struct, union, enum, typedef) and
1080 # invokes the right handler. NOT called for functions.
1081 sub dump_declaration($$) {
1083 my ($prototype, $file) = @_;
1084 my $func = "dump_" . $decl_type;
1088 sub dump_union($$) {
1092 sub dump_struct($$) {
1097 my $type = qr{struct|union};
1098 # For capturing struct/union definition body, i.e. "{members*}qualifiers*"
1099 my $qualifiers = qr{$attribute|__packed|__aligned|____cacheline_aligned_in_smp|____cacheline_aligned};
1100 my $definition_body = qr{\{(.*)\}\s*$qualifiers*};
1101 my $struct_members = qr{($type)([^\{\};]+)\{([^\{\}]*)\}([^\{\}\;]*)\;};
1103 if ($x =~ /($type)\s+(\w+)\s*$definition_body/) {
1105 $declaration_name = $2;
1107 } elsif ($x =~ /typedef\s+($type)\s*$definition_body\s*(\w+)\s*;/) {
1109 $declaration_name = $3;
1114 if ($identifier ne $declaration_name) {
1115 emit_warning("${file}:$.", "expecting prototype for $decl_type $identifier. Prototype was for $decl_type $declaration_name instead\n");
1119 # ignore members marked private:
1120 $members =~ s/\/\*\s*private:.*?\/\*\s*public:.*?\*\///gosi;
1121 $members =~ s/\/\*\s*private:.*//gosi;
1123 $members =~ s/\/\*.*?\*\///gos;
1125 $members =~ s/\s*$attribute/ /gi;
1126 $members =~ s/\s*__aligned\s*\([^;]*\)/ /gos;
1127 $members =~ s/\s*__packed\s*/ /gos;
1128 $members =~ s/\s*CRYPTO_MINALIGN_ATTR/ /gos;
1129 $members =~ s/\s*____cacheline_aligned_in_smp/ /gos;
1130 $members =~ s/\s*____cacheline_aligned/ /gos;
1131 # unwrap struct_group():
1132 # - first eat non-declaration parameters and rewrite for final match
1133 # - then remove macro, outer parens, and trailing semicolon
1134 $members =~ s/\bstruct_group\s*\(([^,]*,)/STRUCT_GROUP(/gos;
1135 $members =~ s/\bstruct_group_(attr|tagged)\s*\(([^,]*,){2}/STRUCT_GROUP(/gos;
1136 $members =~ s/\b__struct_group\s*\(([^,]*,){3}/STRUCT_GROUP(/gos;
1137 $members =~ s/\bSTRUCT_GROUP(\(((?:(?>[^)(]+)|(?1))*)\))[^;]*;/$2/gos;
1139 my $args = qr{([^,)]+)};
1140 # replace DECLARE_BITMAP
1141 $members =~ s/__ETHTOOL_DECLARE_LINK_MODE_MASK\s*\(([^\)]+)\)/DECLARE_BITMAP($1, __ETHTOOL_LINK_MODE_MASK_NBITS)/gos;
1142 $members =~ s/DECLARE_PHY_INTERFACE_MASK\s*\(([^\)]+)\)/DECLARE_BITMAP($1, PHY_INTERFACE_MODE_MAX)/gos;
1143 $members =~ s/DECLARE_BITMAP\s*\($args,\s*$args\)/unsigned long $1\[BITS_TO_LONGS($2)\]/gos;
1144 # replace DECLARE_HASHTABLE
1145 $members =~ s/DECLARE_HASHTABLE\s*\($args,\s*$args\)/unsigned long $1\[1 << (($2) - 1)\]/gos;
1146 # replace DECLARE_KFIFO
1147 $members =~ s/DECLARE_KFIFO\s*\($args,\s*$args,\s*$args\)/$2 \*$1/gos;
1148 # replace DECLARE_KFIFO_PTR
1149 $members =~ s/DECLARE_KFIFO_PTR\s*\($args,\s*$args\)/$2 \*$1/gos;
1150 # replace DECLARE_FLEX_ARRAY
1151 $members =~ s/(?:__)?DECLARE_FLEX_ARRAY\s*\($args,\s*$args\)/$1 $2\[\]/gos;
1152 my $declaration = $members;
1154 # Split nested struct/union elements as newer ones
1155 while ($members =~ m/$struct_members/) {
1160 foreach my $id(split /,/, $ids) {
1161 $newmember .= "$maintype $id; ";
1164 $id =~ s/^\s*\**(\S+)\s*/$1/;
1165 foreach my $arg (split /;/, $content) {
1166 next if ($arg =~ m/^\s*$/);
1167 if ($arg =~ m/^([^\(]+\(\*?\s*)([\w\.]*)(\s*\).*)/) {
1168 # pointer-to-function
1173 if ($id =~ m/^\s*$/) {
1174 # anonymous struct/union
1175 $newmember .= "$type$name$extra; ";
1177 $newmember .= "$type$id.$name$extra; ";
1185 $arg =~ s/:\s*\d+\s*//g;
1187 $arg =~ s/\[.*\]//g;
1188 # The type may have multiple words,
1189 # and multiple IDs can be defined, like:
1190 # const struct foo, *bar, foobar
1191 # So, we remove spaces when parsing the
1192 # names, in order to match just names
1193 # and commas for the names
1194 $arg =~ s/\s*,\s*/,/g;
1195 if ($arg =~ m/(.*)\s+([\S+,]+)/) {
1199 $newmember .= "$arg; ";
1202 foreach my $name (split /,/, $names) {
1203 $name =~ s/^\s*\**(\S+)\s*/$1/;
1204 next if (($name =~ m/^\s*$/));
1205 if ($id =~ m/^\s*$/) {
1206 # anonymous struct/union
1207 $newmember .= "$type $name; ";
1209 $newmember .= "$type $id.$name; ";
1215 $members =~ s/$struct_members/$newmember/;
1218 # Ignore other nested elements, like enums
1219 $members =~ s/(\{[^\{\}]*\})//g;
1221 create_parameterlist($members, ';', $file, $declaration_name);
1222 check_sections($file, $declaration_name, $decl_type, $sectcheck, $struct_actual);
1224 # Adjust declaration for better display
1225 $declaration =~ s/([\{;])/$1\n/g;
1226 $declaration =~ s/\}\s+;/};/g;
1227 # Better handle inlined enums
1228 do {} while ($declaration =~ s/(enum\s+\{[^\}]+),([^\n])/$1,\n$2/);
1230 my @def_args = split /\n/, $declaration;
1233 foreach my $clause (@def_args) {
1234 $clause =~ s/^\s+//;
1235 $clause =~ s/\s+$//;
1236 $clause =~ s/\s+/ /;
1238 $level-- if ($clause =~ m/(\})/ && $level > 1);
1239 if (!($clause =~ m/^\s*#/)) {
1240 $declaration .= "\t" x $level;
1242 $declaration .= "\t" . $clause . "\n";
1243 $level++ if ($clause =~ m/(\{)/ && !($clause =~m/\}/));
1245 output_declaration($declaration_name,
1247 {'struct' => $declaration_name,
1248 'module' => $modulename,
1249 'definition' => $declaration,
1250 'parameterlist' => \@parameterlist,
1251 'parameterdescs' => \%parameterdescs,
1252 'parametertypes' => \%parametertypes,
1253 'sectionlist' => \@sectionlist,
1254 'sections' => \%sections,
1255 'purpose' => $declaration_purpose,
1256 'type' => $decl_type
1260 print STDERR "${file}:$.: error: Cannot parse struct or union!\n";
1266 sub show_warnings($$) {
1267 my $functype = shift;
1270 return 0 if (defined($nosymbol_table{$name}));
1272 return 1 if ($output_selection == OUTPUT_ALL);
1274 if ($output_selection == OUTPUT_EXPORTED) {
1275 if (defined($function_table{$name})) {
1281 if ($output_selection == OUTPUT_INTERNAL) {
1282 if (!($functype eq "function" && defined($function_table{$name}))) {
1288 if ($output_selection == OUTPUT_INCLUDE) {
1289 if (defined($function_table{$name})) {
1295 die("Please add the new output type at show_warnings()");
1304 $x =~ s@/\*.*?\*/@@gos; # strip comments.
1305 # strip #define macros inside enums
1306 $x =~ s@#\s*((define|ifdef)\s+|endif)[^;]*;@@gos;
1308 if ($x =~ /typedef\s+enum\s*\{(.*)\}\s*(\w*)\s*;/) {
1309 $declaration_name = $2;
1311 } elsif ($x =~ /enum\s+(\w*)\s*\{(.*)\}/) {
1312 $declaration_name = $1;
1317 if ($identifier ne $declaration_name) {
1318 if ($identifier eq "") {
1319 emit_warning("${file}:$.", "wrong kernel-doc identifier on line:\n");
1321 emit_warning("${file}:$.", "expecting prototype for enum $identifier. Prototype was for enum $declaration_name instead\n");
1325 $declaration_name = "(anonymous)" if ($declaration_name eq "");
1329 $members =~ s/\s+$//;
1331 foreach my $arg (split ',', $members) {
1332 $arg =~ s/^\s*(\w+).*/$1/;
1333 push @parameterlist, $arg;
1334 if (!$parameterdescs{$arg}) {
1335 $parameterdescs{$arg} = $undescribed;
1336 if (show_warnings("enum", $declaration_name)) {
1337 emit_warning("${file}:$.", "Enum value '$arg' not described in enum '$declaration_name'\n");
1340 $_members{$arg} = 1;
1343 while (my ($k, $v) = each %parameterdescs) {
1344 if (!exists($_members{$k})) {
1345 if (show_warnings("enum", $declaration_name)) {
1346 emit_warning("${file}:$.", "Excess enum value '$k' description in '$declaration_name'\n");
1351 output_declaration($declaration_name,
1353 {'enum' => $declaration_name,
1354 'module' => $modulename,
1355 'parameterlist' => \@parameterlist,
1356 'parameterdescs' => \%parameterdescs,
1357 'sectionlist' => \@sectionlist,
1358 'sections' => \%sections,
1359 'purpose' => $declaration_purpose
1362 print STDERR "${file}:$.: error: Cannot parse enum!\n";
1367 my $typedef_type = qr { ((?:\s+[\w\*]+\b){1,8})\s* }x;
1368 my $typedef_ident = qr { \*?\s*(\w\S+)\s* }x;
1369 my $typedef_args = qr { \s*\((.*)\); }x;
1371 my $typedef1 = qr { typedef$typedef_type\($typedef_ident\)$typedef_args }x;
1372 my $typedef2 = qr { typedef$typedef_type$typedef_ident$typedef_args }x;
1374 sub dump_typedef($$) {
1378 $x =~ s@/\*.*?\*/@@gos; # strip comments.
1380 # Parse function typedef prototypes
1381 if ($x =~ $typedef1 || $x =~ $typedef2) {
1383 $declaration_name = $2;
1385 $return_type =~ s/^\s+//;
1387 if ($identifier ne $declaration_name) {
1388 emit_warning("${file}:$.", "expecting prototype for typedef $identifier. Prototype was for typedef $declaration_name instead\n");
1392 create_parameterlist($args, ',', $file, $declaration_name);
1394 output_declaration($declaration_name,
1396 {'function' => $declaration_name,
1398 'module' => $modulename,
1399 'functiontype' => $return_type,
1400 'parameterlist' => \@parameterlist,
1401 'parameterdescs' => \%parameterdescs,
1402 'parametertypes' => \%parametertypes,
1403 'sectionlist' => \@sectionlist,
1404 'sections' => \%sections,
1405 'purpose' => $declaration_purpose
1410 while (($x =~ /\(*.\)\s*;$/) || ($x =~ /\[*.\]\s*;$/)) {
1411 $x =~ s/\(*.\)\s*;$/;/;
1412 $x =~ s/\[*.\]\s*;$/;/;
1415 if ($x =~ /typedef.*\s+(\w+)\s*;/) {
1416 $declaration_name = $1;
1418 if ($identifier ne $declaration_name) {
1419 emit_warning("${file}:$.", "expecting prototype for typedef $identifier. Prototype was for typedef $declaration_name instead\n");
1423 output_declaration($declaration_name,
1425 {'typedef' => $declaration_name,
1426 'module' => $modulename,
1427 'sectionlist' => \@sectionlist,
1428 'sections' => \%sections,
1429 'purpose' => $declaration_purpose
1433 print STDERR "${file}:$.: error: Cannot parse typedef!\n";
1438 sub save_struct_actual($) {
1441 # strip all spaces from the actual param so that it looks like one string item
1442 $actual =~ s/\s*//g;
1443 $struct_actual = $struct_actual . $actual . " ";
1446 sub create_parameterlist($$$$) {
1448 my $splitter = shift;
1450 my $declaration_name = shift;
1454 # temporarily replace commas inside function pointer definition
1455 my $arg_expr = qr{\([^\),]+};
1456 while ($args =~ /$arg_expr,/) {
1457 $args =~ s/($arg_expr),/$1#/g;
1460 foreach my $arg (split($splitter, $args)) {
1462 $arg =~ s/\/\*.*\*\///;
1463 # strip leading/trailing spaces
1469 # Treat preprocessor directive as a typeless variable just to fill
1470 # corresponding data structures "correctly". Catch it later in
1472 push_parameter($arg, "", "", $file);
1473 } elsif ($arg =~ m/\(.+\)\s*\(/) {
1474 # pointer-to-function
1476 $arg =~ m/[^\(]+\(\*?\s*([\w\[\]\.]*)\s*\)/;
1479 $type =~ s/([^\(]+\(\*?)\s*$param/$1/;
1480 save_struct_actual($param);
1481 push_parameter($param, $type, $arg, $file, $declaration_name);
1483 $arg =~ s/\s*:\s*/:/g;
1484 $arg =~ s/\s*\[/\[/g;
1486 my @args = split('\s*,\s*', $arg);
1487 if ($args[0] =~ m/\*/) {
1488 $args[0] =~ s/(\*+)\s*/ $1/;
1492 if ($args[0] =~ /^(.*\s+)(.*?\[.*\].*)$/) {
1494 push(@first_arg, split('\s+', $1));
1495 push(@first_arg, $2);
1497 @first_arg = split('\s+', shift @args);
1500 unshift(@args, pop @first_arg);
1501 $type = join " ", @first_arg;
1503 foreach $param (@args) {
1504 if ($param =~ m/^(\*+)\s*(.*)/) {
1505 save_struct_actual($2);
1507 push_parameter($2, "$type $1", $arg, $file, $declaration_name);
1509 elsif ($param =~ m/(.*?):(\d+)/) {
1510 if ($type ne "") { # skip unnamed bit-fields
1511 save_struct_actual($1);
1512 push_parameter($1, "$type:$2", $arg, $file, $declaration_name)
1516 save_struct_actual($param);
1517 push_parameter($param, $type, $arg, $file, $declaration_name);
1524 sub push_parameter($$$$$) {
1527 my $org_arg = shift;
1529 my $declaration_name = shift;
1531 if (($anon_struct_union == 1) && ($type eq "") &&
1533 return; # ignore the ending }; from anon. struct/union
1536 $anon_struct_union = 0;
1537 $param =~ s/[\[\)].*//;
1539 if ($type eq "" && $param =~ /\.\.\.$/)
1541 if (!$param =~ /\w\.\.\.$/) {
1542 # handles unnamed variable parameters
1545 elsif ($param =~ /\w\.\.\.$/) {
1546 # for named variable parameters of the form `x...`, remove the dots
1547 $param =~ s/\.\.\.$//;
1549 if (!defined $parameterdescs{$param} || $parameterdescs{$param} eq "") {
1550 $parameterdescs{$param} = "variable arguments";
1553 elsif ($type eq "" && ($param eq "" or $param eq "void"))
1556 $parameterdescs{void} = "no arguments";
1558 elsif ($type eq "" && ($param eq "struct" or $param eq "union"))
1559 # handle unnamed (anonymous) union or struct:
1562 $param = "{unnamed_" . $param . "}";
1563 $parameterdescs{$param} = "anonymous\n";
1564 $anon_struct_union = 1;
1567 # warn if parameter has no description
1568 # (but ignore ones starting with # as these are not parameters
1569 # but inline preprocessor statements);
1570 # Note: It will also ignore void params and unnamed structs/unions
1571 if (!defined $parameterdescs{$param} && $param !~ /^#/) {
1572 $parameterdescs{$param} = $undescribed;
1574 if (show_warnings($type, $declaration_name) && $param !~ /\./) {
1575 emit_warning("${file}:$.", "Function parameter or member '$param' not described in '$declaration_name'\n");
1579 # strip spaces from $param so that it is one continuous string
1580 # on @parameterlist;
1581 # this fixes a problem where check_sections() cannot find
1582 # a parameter like "addr[6 + 2]" because it actually appears
1583 # as "addr[6", "+", "2]" on the parameter list;
1584 # but it's better to maintain the param string unchanged for output,
1585 # so just weaken the string compare in check_sections() to ignore
1586 # "[blah" in a parameter string;
1587 ###$param =~ s/\s*//g;
1588 push @parameterlist, $param;
1589 $org_arg =~ s/\s\s+/ /g;
1590 $parametertypes{$param} = $org_arg;
1593 sub check_sections($$$$$) {
1594 my ($file, $decl_name, $decl_type, $sectcheck, $prmscheck) = @_;
1595 my @sects = split ' ', $sectcheck;
1596 my @prms = split ' ', $prmscheck;
1599 my $prm_clean; # strip trailing "[array size]" and/or beginning "*"
1601 foreach $sx (0 .. $#sects) {
1603 foreach $px (0 .. $#prms) {
1604 $prm_clean = $prms[$px];
1605 $prm_clean =~ s/\[.*\]//;
1606 $prm_clean =~ s/$attribute//i;
1607 # ignore array size in a parameter string;
1608 # however, the original param string may contain
1609 # spaces, e.g.: addr[6 + 2]
1610 # and this appears in @prms as "addr[6" since the
1611 # parameter list is split at spaces;
1612 # hence just ignore "[..." for the sections check;
1613 $prm_clean =~ s/\[.*//;
1615 ##$prm_clean =~ s/^\**//;
1616 if ($prm_clean eq $sects[$sx]) {
1622 if ($decl_type eq "function") {
1623 emit_warning("${file}:$.",
1624 "Excess function parameter " .
1626 "description in '$decl_name'\n");
1633 # Checks the section describing the return value of a function.
1634 sub check_return_section {
1636 my $declaration_name = shift;
1637 my $return_type = shift;
1639 # Ignore an empty return type (It's a macro)
1640 # Ignore functions with a "void" return type. (But don't ignore "void *")
1641 if (($return_type eq "") || ($return_type =~ /void\s*\w*\s*$/)) {
1645 if (!defined($sections{$section_return}) ||
1646 $sections{$section_return} eq "") {
1647 emit_warning("${file}:$.",
1648 "No description found for return value of " .
1649 "'$declaration_name'\n");
1654 # takes a function prototype and the name of the current file being
1655 # processed and spits out all the details stored in the global
1657 sub dump_function($$) {
1658 my $prototype = shift;
1662 print_lineno($new_start_line);
1664 $prototype =~ s/^static +//;
1665 $prototype =~ s/^extern +//;
1666 $prototype =~ s/^asmlinkage +//;
1667 $prototype =~ s/^inline +//;
1668 $prototype =~ s/^__inline__ +//;
1669 $prototype =~ s/^__inline +//;
1670 $prototype =~ s/^__always_inline +//;
1671 $prototype =~ s/^noinline +//;
1672 $prototype =~ s/__init +//;
1673 $prototype =~ s/__init_or_module +//;
1674 $prototype =~ s/__deprecated +//;
1675 $prototype =~ s/__flatten +//;
1676 $prototype =~ s/__meminit +//;
1677 $prototype =~ s/__must_check +//;
1678 $prototype =~ s/__weak +//;
1679 $prototype =~ s/__sched +//;
1680 $prototype =~ s/__printf\s*\(\s*\d*\s*,\s*\d*\s*\) +//;
1681 $prototype =~ s/__alloc_size\s*\(\s*\d+\s*(?:,\s*\d+\s*)?\) +//;
1682 my $define = $prototype =~ s/^#\s*define\s+//; #ak added
1683 $prototype =~ s/__attribute_const__ +//;
1684 $prototype =~ s/__attribute__\s*\(\(
1686 [\w\s]++ # attribute name
1687 (?:\([^)]*+\))? # attribute arguments
1688 \s*+,? # optional comma at the end
1692 # Yes, this truly is vile. We are looking for:
1693 # 1. Return type (may be nothing if we're looking at a macro)
1695 # 3. Function parameters.
1697 # All the while we have to watch out for function pointer parameters
1698 # (which IIRC is what the two sections are for), C types (these
1699 # regexps don't even start to express all the possibilities), and
1702 # If you mess with these regexps, it's a good idea to check that
1703 # the following functions' documentation still comes out right:
1704 # - parport_register_device (function pointer parameters)
1705 # - atomic_set (macro)
1706 # - pci_match_device, __copy_to_user (long return type)
1707 my $name = qr{[a-zA-Z0-9_~:]+};
1708 my $prototype_end1 = qr{[^\(]*};
1709 my $prototype_end2 = qr{[^\{]*};
1710 my $prototype_end = qr{\(($prototype_end1|$prototype_end2)\)};
1711 my $type1 = qr{[\w\s]+};
1712 my $type2 = qr{$type1\*+};
1714 if ($define && $prototype =~ m/^()($name)\s+/) {
1715 # This is an object-like macro, it has no return type and no parameter
1717 # Function-like macros are not allowed to have spaces between
1718 # declaration_name and opening parenthesis (notice the \s+).
1720 $declaration_name = $2;
1722 } elsif ($prototype =~ m/^()($name)\s*$prototype_end/ ||
1723 $prototype =~ m/^($type1)\s+($name)\s*$prototype_end/ ||
1724 $prototype =~ m/^($type2+)\s*($name)\s*$prototype_end/) {
1726 $declaration_name = $2;
1729 create_parameterlist($args, ',', $file, $declaration_name);
1731 emit_warning("${file}:$.", "cannot understand function prototype: '$prototype'\n");
1735 if ($identifier ne $declaration_name) {
1736 emit_warning("${file}:$.", "expecting prototype for $identifier(). Prototype was for $declaration_name() instead\n");
1740 my $prms = join " ", @parameterlist;
1741 check_sections($file, $declaration_name, "function", $sectcheck, $prms);
1743 # This check emits a lot of warnings at the moment, because many
1744 # functions don't have a 'Return' doc section. So until the number
1745 # of warnings goes sufficiently down, the check is only performed in
1747 # TODO: always perform the check.
1748 if ($verbose && !$noret) {
1749 check_return_section($file, $declaration_name, $return_type);
1752 # The function parser can be called with a typedef parameter.
1754 if ($return_type =~ /typedef/) {
1755 output_declaration($declaration_name,
1757 {'function' => $declaration_name,
1759 'module' => $modulename,
1760 'functiontype' => $return_type,
1761 'parameterlist' => \@parameterlist,
1762 'parameterdescs' => \%parameterdescs,
1763 'parametertypes' => \%parametertypes,
1764 'sectionlist' => \@sectionlist,
1765 'sections' => \%sections,
1766 'purpose' => $declaration_purpose
1769 output_declaration($declaration_name,
1771 {'function' => $declaration_name,
1772 'module' => $modulename,
1773 'functiontype' => $return_type,
1774 'parameterlist' => \@parameterlist,
1775 'parameterdescs' => \%parameterdescs,
1776 'parametertypes' => \%parametertypes,
1777 'sectionlist' => \@sectionlist,
1778 'sections' => \%sections,
1779 'purpose' => $declaration_purpose
1786 %parameterdescs = ();
1787 %parametertypes = ();
1788 @parameterlist = ();
1792 $struct_actual = "";
1795 $state = STATE_NORMAL;
1796 $inline_doc_state = STATE_INLINE_NA;
1799 sub tracepoint_munge($) {
1801 my $tracepointname = 0;
1802 my $tracepointargs = 0;
1804 if ($prototype =~ m/TRACE_EVENT\((.*?),/) {
1805 $tracepointname = $1;
1807 if ($prototype =~ m/DEFINE_SINGLE_EVENT\((.*?),/) {
1808 $tracepointname = $1;
1810 if ($prototype =~ m/DEFINE_EVENT\((.*?),(.*?),/) {
1811 $tracepointname = $2;
1813 $tracepointname =~ s/^\s+//; #strip leading whitespace
1814 if ($prototype =~ m/TP_PROTO\((.*?)\)/) {
1815 $tracepointargs = $1;
1817 if (($tracepointname eq 0) || ($tracepointargs eq 0)) {
1818 emit_warning("${file}:$.", "Unrecognized tracepoint format: \n".
1821 $prototype = "static inline void trace_$tracepointname($tracepointargs)";
1822 $identifier = "trace_$identifier";
1826 sub syscall_munge() {
1829 $prototype =~ s@[\r\n]+@ @gos; # strip newlines/CR's
1830 ## if ($prototype =~ m/SYSCALL_DEFINE0\s*\(\s*(a-zA-Z0-9_)*\s*\)/) {
1831 if ($prototype =~ m/SYSCALL_DEFINE0/) {
1833 ## $prototype = "long sys_$1(void)";
1836 $prototype =~ s/SYSCALL_DEFINE.*\(/long sys_/; # fix return type & func name
1837 if ($prototype =~ m/long (sys_.*?),/) {
1838 $prototype =~ s/,/\(/;
1840 $prototype =~ s/\)/\(void\)/;
1843 # now delete all of the odd-number commas in $prototype
1844 # so that arg types & arg names don't have a comma between them
1846 my $len = length($prototype);
1848 $len = 0; # skip the for-loop
1850 for (my $ix = 0; $ix < $len; $ix++) {
1851 if (substr($prototype, $ix, 1) eq ',') {
1853 if ($count % 2 == 1) {
1854 substr($prototype, $ix, 1) = ' ';
1860 sub process_proto_function($$) {
1864 $x =~ s@\/\/.*$@@gos; # strip C99-style comments to end of line
1866 if ($x =~ m#\s*/\*\s+MACDOC\s*#io || ($x =~ /^#/ && $x !~ /^#\s*define/)) {
1869 elsif ($x =~ /([^\{]*)/) {
1873 if (($x =~ /\{/) || ($x =~ /\#\s*define/) || ($x =~ /;/)) {
1874 $prototype =~ s@/\*.*?\*/@@gos; # strip comments.
1875 $prototype =~ s@[\r\n]+@ @gos; # strip newlines/cr's.
1876 $prototype =~ s@^\s+@@gos; # strip leading spaces
1878 # Handle prototypes for function pointers like:
1879 # int (*pcs_config)(struct foo)
1880 $prototype =~ s@^(\S+\s+)\(\s*\*(\S+)\)@$1$2@gos;
1882 if ($prototype =~ /SYSCALL_DEFINE/) {
1885 if ($prototype =~ /TRACE_EVENT/ || $prototype =~ /DEFINE_EVENT/ ||
1886 $prototype =~ /DEFINE_SINGLE_EVENT/)
1888 tracepoint_munge($file);
1890 dump_function($prototype, $file);
1895 sub process_proto_type($$) {
1899 $x =~ s@[\r\n]+@ @gos; # strip newlines/cr's.
1900 $x =~ s@^\s+@@gos; # strip leading spaces
1901 $x =~ s@\s+$@@gos; # strip trailing spaces
1902 $x =~ s@\/\/.*$@@gos; # strip C99-style comments to end of line
1905 # To distinguish preprocessor directive from regular declaration later.
1910 if ( $x =~ /([^\{\};]*)([\{\};])(.*)/ ) {
1911 if( length $prototype ) {
1914 $prototype .= $1 . $2;
1915 ($2 eq '{') && $brcount++;
1916 ($2 eq '}') && $brcount--;
1917 if (($2 eq ';') && ($brcount == 0)) {
1918 dump_declaration($prototype, $file);
1931 sub map_filename($) {
1933 my ($orig_file) = @_;
1935 if (defined($ENV{'SRCTREE'})) {
1936 $file = "$ENV{'SRCTREE'}" . "/" . $orig_file;
1941 if (defined($source_map{$file})) {
1942 $file = $source_map{$file};
1948 sub process_export_file($) {
1949 my ($orig_file) = @_;
1950 my $file = map_filename($orig_file);
1952 if (!open(IN,"<$file")) {
1953 print STDERR "Error: Cannot open file $file\n";
1959 if (/$export_symbol/) {
1960 next if (defined($nosymbol_table{$2}));
1961 $function_table{$2} = 1;
1969 # Parsers for the various processing states.
1971 # STATE_NORMAL: looking for the /** to begin everything.
1973 sub process_normal() {
1974 if (/$doc_start/o) {
1975 $state = STATE_NAME; # next line is always the function name
1977 $declaration_start_line = $. + 1;
1982 # STATE_NAME: Looking for the "name - description" line
1984 sub process_name($$) {
1988 if (/$doc_block/o) {
1989 $state = STATE_DOCBLOCK;
1991 $new_start_line = $.;
1994 $section = $section_intro;
1998 } elsif (/$doc_decl/o) {
2000 my $is_kernel_comment = 0;
2001 my $decl_start = qr{$doc_com};
2002 # test for pointer declaration type, foo * bar() - desc
2003 my $fn_type = qr{\w+\s*\*\s*};
2004 my $parenthesis = qr{\(\w*\)};
2005 my $decl_end = qr{[-:].*};
2006 if (/^$decl_start([\w\s]+?)$parenthesis?\s*$decl_end?$/) {
2009 if ($identifier =~ m/^(struct|union|enum|typedef)\b\s*(\S*)/) {
2012 $is_kernel_comment = 1;
2014 # Look for foo() or static void foo() - description; or misspelt
2016 elsif (/^$decl_start$fn_type?(\w+)\s*$parenthesis?\s*$decl_end?$/ ||
2017 /^$decl_start$fn_type?(\w+.*)$parenthesis?\s*$decl_end$/) {
2019 $decl_type = 'function';
2020 $identifier =~ s/^define\s+//;
2021 $is_kernel_comment = 1;
2023 $identifier =~ s/\s+$//;
2025 $state = STATE_BODY;
2026 # if there's no @param blocks need to set up default section
2029 $section = $section_default;
2030 $new_start_line = $. + 1;
2032 # strip leading/trailing/multiple spaces
2036 $descr =~ s/\s+/ /g;
2037 $declaration_purpose = $descr;
2038 $state = STATE_BODY_MAYBE;
2040 $declaration_purpose = "";
2043 if (!$is_kernel_comment) {
2044 emit_warning("${file}:$.", "This comment starts with '/**', but isn't a kernel-doc comment. Refer Documentation/doc-guide/kernel-doc.rst\n$_");
2045 $state = STATE_NORMAL;
2048 if (($declaration_purpose eq "") && $verbose) {
2049 emit_warning("${file}:$.", "missing initial short description on line:\n$_");
2052 if ($identifier eq "" && $decl_type ne "enum") {
2053 emit_warning("${file}:$.", "wrong kernel-doc identifier on line:\n$_");
2054 $state = STATE_NORMAL;
2058 print STDERR "${file}:$.: info: Scanning doc for $decl_type $identifier\n";
2061 emit_warning("${file}:$.", "Cannot understand $_ on line $. - I thought it was a doc line\n");
2062 $state = STATE_NORMAL;
2068 # STATE_BODY and STATE_BODY_MAYBE: the bulk of a kerneldoc comment.
2070 sub process_body($$) {
2073 # Until all named variable macro parameters are
2074 # documented using the bare name (`x`) rather than with
2075 # dots (`x...`), strip the dots:
2076 if ($section =~ /\w\.\.\.$/) {
2077 $section =~ s/\.\.\.$//;
2080 emit_warning("${file}:$.", "Variable macro arguments should be documented without dots\n");
2084 if ($state == STATE_BODY_WITH_BLANK_LINE && /^\s*\*\s?\S/) {
2085 dump_section($file, $section, $contents);
2086 $section = $section_default;
2087 $new_start_line = $.;
2091 if (/$doc_sect/i) { # case insensitive for supported section names
2095 # map the supported section names to the canonical names
2096 if ($newsection =~ m/^description$/i) {
2097 $newsection = $section_default;
2098 } elsif ($newsection =~ m/^context$/i) {
2099 $newsection = $section_context;
2100 } elsif ($newsection =~ m/^returns?$/i) {
2101 $newsection = $section_return;
2102 } elsif ($newsection =~ m/^\@return$/) {
2103 # special: @return is a section, not a param description
2104 $newsection = $section_return;
2107 if (($contents ne "") && ($contents ne "\n")) {
2108 if (!$in_doc_sect && $verbose) {
2109 emit_warning("${file}:$.", "contents before sections\n");
2111 dump_section($file, $section, $contents);
2112 $section = $section_default;
2116 $state = STATE_BODY;
2117 $contents = $newcontents;
2118 $new_start_line = $.;
2119 while (substr($contents, 0, 1) eq " ") {
2120 $contents = substr($contents, 1);
2122 if ($contents ne "") {
2125 $section = $newsection;
2126 $leading_space = undef;
2127 } elsif (/$doc_end/) {
2128 if (($contents ne "") && ($contents ne "\n")) {
2129 dump_section($file, $section, $contents);
2130 $section = $section_default;
2133 # look for doc_com + <text> + doc_end:
2134 if ($_ =~ m'\s*\*\s*[a-zA-Z_0-9:\.]+\*/') {
2135 emit_warning("${file}:$.", "suspicious ending line: $_");
2139 $state = STATE_PROTO;
2141 $new_start_line = $. + 1;
2142 } elsif (/$doc_content/) {
2144 if ($section eq $section_context) {
2145 dump_section($file, $section, $contents);
2146 $section = $section_default;
2148 $new_start_line = $.;
2149 $state = STATE_BODY;
2151 if ($section ne $section_default) {
2152 $state = STATE_BODY_WITH_BLANK_LINE;
2154 $state = STATE_BODY;
2158 } elsif ($state == STATE_BODY_MAYBE) {
2159 # Continued declaration purpose
2160 chomp($declaration_purpose);
2161 $declaration_purpose .= " " . $1;
2162 $declaration_purpose =~ s/\s+/ /g;
2165 if ($section =~ m/^@/ || $section eq $section_context) {
2166 if (!defined $leading_space) {
2167 if ($cont =~ m/^(\s+)/) {
2168 $leading_space = $1;
2170 $leading_space = "";
2173 $cont =~ s/^$leading_space//;
2175 $contents .= $cont . "\n";
2178 # i dont know - bad line? ignore.
2179 emit_warning("${file}:$.", "bad line: $_");
2185 # STATE_PROTO: reading a function/whatever prototype.
2187 sub process_proto($$) {
2190 if (/$doc_inline_oneline/) {
2193 if ($contents ne "") {
2195 dump_section($file, $section, $contents);
2196 $section = $section_default;
2199 } elsif (/$doc_inline_start/) {
2200 $state = STATE_INLINE;
2201 $inline_doc_state = STATE_INLINE_NAME;
2202 } elsif ($decl_type eq 'function') {
2203 process_proto_function($_, $file);
2205 process_proto_type($_, $file);
2210 # STATE_DOCBLOCK: within a DOC: block.
2212 sub process_docblock($$) {
2216 dump_doc_section($file, $section, $contents);
2217 $section = $section_default;
2220 %parameterdescs = ();
2221 %parametertypes = ();
2222 @parameterlist = ();
2226 $state = STATE_NORMAL;
2227 } elsif (/$doc_content/) {
2229 $contents .= $blankline;
2231 $contents .= $1 . "\n";
2237 # STATE_INLINE: docbook comments within a prototype.
2239 sub process_inline($$) {
2242 # First line (state 1) needs to be a @parameter
2243 if ($inline_doc_state == STATE_INLINE_NAME && /$doc_inline_sect/o) {
2246 $new_start_line = $.;
2247 if ($contents ne "") {
2248 while (substr($contents, 0, 1) eq " ") {
2249 $contents = substr($contents, 1);
2253 $inline_doc_state = STATE_INLINE_TEXT;
2254 # Documentation block end */
2255 } elsif (/$doc_inline_end/) {
2256 if (($contents ne "") && ($contents ne "\n")) {
2257 dump_section($file, $section, $contents);
2258 $section = $section_default;
2261 $state = STATE_PROTO;
2262 $inline_doc_state = STATE_INLINE_NA;
2264 } elsif (/$doc_content/) {
2265 if ($inline_doc_state == STATE_INLINE_TEXT) {
2266 $contents .= $1 . "\n";
2267 # nuke leading blank lines
2268 if ($contents =~ /^\s*$/) {
2271 } elsif ($inline_doc_state == STATE_INLINE_NAME) {
2272 $inline_doc_state = STATE_INLINE_ERROR;
2273 emit_warning("${file}:$.", "Incorrect use of kernel-doc format: $_");
2279 sub process_file($) {
2281 my $initial_section_counter = $section_counter;
2282 my ($orig_file) = @_;
2284 $file = map_filename($orig_file);
2286 if (!open(IN_FILE,"<$file")) {
2287 print STDERR "Error: Cannot open file $file\n";
2294 $section_counter = 0;
2296 while (s/\\\s*$//) {
2299 # Replace tabs by spaces
2300 while ($_ =~ s/\t+/' ' x (length($&) * 8 - length($`) % 8)/e) {};
2301 # Hand this line to the appropriate state handler
2302 if ($state == STATE_NORMAL) {
2304 } elsif ($state == STATE_NAME) {
2305 process_name($file, $_);
2306 } elsif ($state == STATE_BODY || $state == STATE_BODY_MAYBE ||
2307 $state == STATE_BODY_WITH_BLANK_LINE) {
2308 process_body($file, $_);
2309 } elsif ($state == STATE_INLINE) { # scanning for inline parameters
2310 process_inline($file, $_);
2311 } elsif ($state == STATE_PROTO) {
2312 process_proto($file, $_);
2313 } elsif ($state == STATE_DOCBLOCK) {
2314 process_docblock($file, $_);
2318 # Make sure we got something interesting.
2319 if ($initial_section_counter == $section_counter && $
2320 output_mode ne "none") {
2321 if ($output_selection == OUTPUT_INCLUDE) {
2322 emit_warning("${file}:1", "'$_' not found\n")
2323 for keys %function_table;
2326 emit_warning("${file}:1", "no structured comments found\n");
2333 if ($output_mode eq "rst") {
2334 get_sphinx_version() if (!$sphinx_major);
2337 $kernelversion = get_kernel_version();
2339 # generate a sequence of code that will splice in highlighting information
2340 # using the s// operator.
2341 for (my $k = 0; $k < @highlights; $k++) {
2342 my $pattern = $highlights[$k][0];
2343 my $result = $highlights[$k][1];
2344 # print STDERR "scanning pattern:$pattern, highlight:($result)\n";
2345 $dohighlight .= "\$contents =~ s:$pattern:$result:gs;\n";
2348 # Read the file that maps relative names to absolute names for
2349 # separate source and object directories and for shadow trees.
2350 if (open(SOURCE_MAP, "<.tmp_filelist.txt")) {
2351 my ($relname, $absname);
2352 while(<SOURCE_MAP>) {
2354 ($relname, $absname) = (split())[0..1];
2355 $relname =~ s:^/+::;
2356 $source_map{$relname} = $absname;
2361 if ($output_selection == OUTPUT_EXPORTED ||
2362 $output_selection == OUTPUT_INTERNAL) {
2364 push(@export_file_list, @ARGV);
2366 foreach (@export_file_list) {
2368 process_export_file($_);
2376 if ($verbose && $errors) {
2377 print STDERR "$errors errors\n";
2379 if ($verbose && $warnings) {
2380 print STDERR "$warnings warnings\n";
2383 if ($Werror && $warnings) {
2384 print STDERR "$warnings warnings as Errors\n";
2387 exit($output_mode eq "none" ? 0 : $errors)
2394 =head2 Output format selection (mutually exclusive):
2400 Output troff manual page format.
2404 Output reStructuredText format. This is the default.
2408 Do not output documentation, only warnings.
2412 =head2 Output format modifiers
2414 =head3 reStructuredText only
2418 =item -sphinx-version VERSION
2420 Use the ReST C domain dialect compatible with a specific Sphinx Version.
2422 If not specified, kernel-doc will auto-detect using the sphinx-build version
2427 =head2 Output selection (mutually exclusive):
2433 Only output documentation for the symbols that have been exported using
2434 EXPORT_SYMBOL() or EXPORT_SYMBOL_GPL() in any input FILE or -export-file FILE.
2438 Only output documentation for the symbols that have NOT been exported using
2439 EXPORT_SYMBOL() or EXPORT_SYMBOL_GPL() in any input FILE or -export-file FILE.
2441 =item -function NAME
2443 Only output documentation for the given function or DOC: section title.
2444 All other functions and DOC: sections are ignored.
2446 May be specified multiple times.
2448 =item -nosymbol NAME
2450 Exclude the specified symbol from the output documentation.
2452 May be specified multiple times.
2456 =head2 Output selection modifiers:
2460 =item -no-doc-sections
2462 Do not output DOC: sections.
2464 =item -export-file FILE
2466 Specify an additional FILE in which to look for EXPORT_SYMBOL() and
2467 EXPORT_SYMBOL_GPL().
2469 To be used with -export or -internal.
2471 May be specified multiple times.
2475 =head3 reStructuredText only
2479 =item -enable-lineno
2481 Enable output of .. LINENO lines.
2485 =head2 Other parameters:
2495 Verbose output, more warnings and other information.
2499 Treat warnings as errors.