2 # SPDX-License-Identifier: GPL-2.0
7 ## Copyright (c) 1998 Michael Zucchi, All Rights Reserved ##
8 ## Copyright (C) 2000, 1 Tim Waugh <twaugh@redhat.com> ##
9 ## Copyright (C) 2001 Simon Huggins ##
10 ## Copyright (C) 2005-2012 Randy Dunlap ##
11 ## Copyright (C) 2012 Dan Luedtke ##
13 ## #define enhancements by Armin Kuster <akuster@mvista.com> ##
14 ## Copyright (c) 2000 MontaVista Software, Inc. ##
16 ## This software falls under the GNU General Public License. ##
17 ## Please read the COPYING file for more information ##
19 # 18/01/2001 - Cleanups
20 # Functions prototyped as foo(void) same as foo()
21 # Stop eval'ing where we don't need to.
24 # 27/06/2001 - Allowed whitespace after initial "/**" and
25 # allowed comments before function declarations.
26 # -- Christian Kreibich <ck@whoop.org>
29 # - add perldoc documentation
30 # - Look more closely at some of the scarier bits :)
32 # 26/05/2001 - Support for separate source and object trees.
34 # Keith Owens <kaos@ocs.com.au>
36 # 23/09/2001 - Added support for typedefs, structs, enums and unions
37 # Support for Context section; can be terminated using empty line
38 # Small fixes (like spaces vs. \s in regex)
39 # -- Tim Jansen <tim@tjansen.de>
41 # 25/07/2012 - Added support for HTML5
42 # -- Dan Luedtke <mail@danrl.de>
45 my $message = <<"EOF";
46 Usage: $0 [OPTION ...] FILE ...
48 Read C language source or header FILEs, extract embedded documentation comments,
49 and print formatted documentation to standard output.
51 The documentation comments are identified by "/**" opening comment mark. See
52 Documentation/doc-guide/kernel-doc.rst for the documentation comment syntax.
54 Output format selection (mutually exclusive):
55 -man Output troff manual page format. This is the default.
56 -rst Output reStructuredText format.
57 -none Do not output documentation, only warnings.
59 Output selection (mutually exclusive):
60 -export Only output documentation for symbols that have been
61 exported using EXPORT_SYMBOL() or EXPORT_SYMBOL_GPL()
62 in any input FILE or -export-file FILE.
63 -internal Only output documentation for symbols that have NOT been
64 exported using EXPORT_SYMBOL() or EXPORT_SYMBOL_GPL()
65 in any input FILE or -export-file FILE.
66 -function NAME Only output documentation for the given function(s)
67 or DOC: section title(s). All other functions and DOC:
68 sections are ignored. May be specified multiple times.
69 -nosymbol NAME Exclude the specified symbols from the output
70 documentation. May be specified multiple times.
72 Output selection modifiers:
73 -no-doc-sections Do not output DOC: sections.
74 -enable-lineno Enable output of #define LINENO lines. Only works with
75 reStructuredText format.
76 -export-file FILE Specify an additional FILE in which to look for
77 EXPORT_SYMBOL() and EXPORT_SYMBOL_GPL(). To be used with
78 -export or -internal. May be specified multiple times.
81 -v Verbose output, more warnings and other information.
83 -Werror Treat warnings as errors.
92 # In the following table, (...)? signifies optional structure.
93 # (...)* signifies 0 or more structure elements
95 # * function_name(:)? (- short description)?
96 # (* @parameterx: (description of parameter x)?)*
98 # * (Description:)? (Description of function)?
99 # * (section header: (section description)? )*
102 # So .. the trivial example would be:
108 # If the Description: header tag is omitted, then there must be a blank line
109 # after the last parameter specification.
112 # * my_function - does my stuff
113 # * @my_arg: its mine damnit
115 # * Does my stuff explained.
118 # or, could also use:
120 # * my_function - does my stuff
121 # * @my_arg: its mine damnit
122 # * Description: Does my stuff explained.
126 # Besides functions you can also write documentation for structs, unions,
127 # enums and typedefs. Instead of the function name you must write the name
128 # of the declaration; the struct/union/enum/typedef must always precede
129 # the name. Nesting of declarations is not supported.
130 # Use the argument mechanism to document members or constants.
133 # * struct my_struct - short description
135 # * @b: second member
137 # * Longer description
146 # All descriptions can be multiline, except the short function description.
148 # For really longs structs, you can also describe arguments inside the
149 # body of the struct.
152 # * struct my_struct - short description
154 # * @b: second member
156 # * Longer description
162 # * @c: This is longer description of C
164 # * You can use paragraphs to describe arguments
165 # * using this method.
170 # This should be use only for struct/enum members.
172 # You can also add additional sections. When documenting kernel functions you
173 # should document the "Context:" of the function, e.g. whether the functions
174 # can be called form interrupts. Unlike other sections you can end it with an
176 # A non-void function should have a "Return:" section describing the return
178 # Example-sections should contain the string EXAMPLE so that they are marked
179 # appropriately in DocBook.
183 # * user_function - function that can only be called in user context
184 # * @a: some argument
185 # * Context: !in_interrupt()
189 # * user_function(22);
194 # All descriptive text is further processed, scanning for the following special
195 # patterns, which are highlighted appropriately.
197 # 'funcname()' - function
198 # '$ENVVAR' - environmental variable
199 # '&struct_name' - name of a structure (up to two words including 'struct')
200 # '&struct_name.member' - name of a structure member
201 # '@parameter' - name of a parameter
202 # '%CONST' - name of a constant.
203 # '``LITERAL``' - literal string without any spaces on it.
209 my $anon_struct_union = 0;
211 # match expressions used to find embedded type information
212 my $type_constant = '\b``([^\`]+)``\b';
213 my $type_constant2 = '\%([-_\w]+)';
214 my $type_func = '(\w+)\(\)';
215 my $type_param = '\@(\w*((\.\w+)|(->\w+))*(\.\.\.)?)';
216 my $type_param_ref = '([\!]?)\@(\w*((\.\w+)|(->\w+))*(\.\.\.)?)';
217 my $type_fp_param = '\@(\w+)\(\)'; # Special RST handling for func ptr params
218 my $type_fp_param2 = '\@(\w+->\S+)\(\)'; # Special RST handling for structs with func ptr params
219 my $type_env = '(\$\w+)';
220 my $type_enum = '#(enum\s*([_\w]+))';
221 my $type_struct = '#(struct\s*([_\w]+))';
222 my $type_typedef = '#(([A-Z][_\w]*))';
223 my $type_union = '#(union\s*([_\w]+))';
224 my $type_member = '#([_\w]+)(\.|->)([_\w]+)';
225 my $type_fallback = '(?!)'; # this never matches
226 my $type_member_func = $type_member . '\(\)';
228 # Output conversion substitutions.
229 # One for each output format
231 # these are pretty rough
232 my @highlights_man = (
233 [$type_constant, "\$1"],
234 [$type_constant2, "\$1"],
235 [$type_func, "\\\\fB\$1\\\\fP"],
236 [$type_enum, "\\\\fI\$1\\\\fP"],
237 [$type_struct, "\\\\fI\$1\\\\fP"],
238 [$type_typedef, "\\\\fI\$1\\\\fP"],
239 [$type_union, "\\\\fI\$1\\\\fP"],
240 [$type_param, "\\\\fI\$1\\\\fP"],
241 [$type_param_ref, "\\\\fI\$1\$2\\\\fP"],
242 [$type_member, "\\\\fI\$1\$2\$3\\\\fP"],
243 [$type_fallback, "\\\\fI\$1\\\\fP"]
245 my $blankline_man = "";
248 my @highlights_rst = (
249 [$type_constant, "``\$1``"],
250 [$type_constant2, "``\$1``"],
251 # Note: need to escape () to avoid func matching later
252 [$type_member_func, "\\:c\\:type\\:`\$1\$2\$3\\\\(\\\\) <\$1>`"],
253 [$type_member, "\\:c\\:type\\:`\$1\$2\$3 <\$1>`"],
254 [$type_fp_param, "**\$1\\\\(\\\\)**"],
255 [$type_fp_param2, "**\$1\\\\(\\\\)**"],
256 [$type_func, "\$1()"],
257 [$type_enum, "\\:c\\:type\\:`\$1 <\$2>`"],
258 [$type_struct, "\\:c\\:type\\:`\$1 <\$2>`"],
259 [$type_typedef, "\\:c\\:type\\:`\$1 <\$2>`"],
260 [$type_union, "\\:c\\:type\\:`\$1 <\$2>`"],
261 # in rst this can refer to any type
262 [$type_fallback, "\\:c\\:type\\:`\$1`"],
263 [$type_param_ref, "**\$1\$2**"]
265 my $blankline_rst = "\n";
275 my $dohighlight = "";
279 my $output_mode = "rst";
280 my $output_preformatted = 0;
281 my $no_doc_sections = 0;
282 my $enable_lineno = 0;
283 my @highlights = @highlights_rst;
284 my $blankline = $blankline_rst;
285 my $modulename = "Kernel API";
288 OUTPUT_ALL
=> 0, # output all symbols and doc sections
289 OUTPUT_INCLUDE
=> 1, # output only specified symbols
290 OUTPUT_EXPORTED
=> 2, # output exported symbols
291 OUTPUT_INTERNAL
=> 3, # output non-exported symbols
293 my $output_selection = OUTPUT_ALL
;
294 my $show_not_found = 0; # No longer used
296 my @export_file_list;
299 if (defined($ENV{'KBUILD_BUILD_TIMESTAMP'}) &&
300 (my $seconds = `date -d"${ENV{'KBUILD_BUILD_TIMESTAMP'}}" +%s`) ne '') {
301 @build_time = gmtime($seconds);
303 @build_time = localtime;
306 my $man_date = ('January', 'February', 'March', 'April', 'May', 'June',
307 'July', 'August', 'September', 'October',
308 'November', 'December')[$build_time[4]] .
309 " " . ($build_time[5]+1900);
311 # Essentially these are globals.
312 # They probably want to be tidied up, made more localised or something.
313 # CAVEAT EMPTOR! Some of the others I localised may not want to be, which
314 # could cause "use of undefined value" or other bugs.
315 my ($function, %function_table, %parametertypes, $declaration_purpose);
316 my %nosymbol_table = ();
317 my $declaration_start_line;
318 my ($type, $declaration_name, $return_type);
319 my ($newsection, $newcontents, $prototype, $brcount, %source_map);
321 if (defined($ENV{'KBUILD_VERBOSE'})) {
322 $verbose = "$ENV{'KBUILD_VERBOSE'}";
325 if (defined($ENV{'KDOC_WERROR'})) {
326 $Werror = "$ENV{'KDOC_WERROR'}";
329 if (defined($ENV{'KCFLAGS'})) {
330 my $kcflags = "$ENV{'KCFLAGS'}";
332 if ($kcflags =~ /Werror/) {
337 # Generated docbook code is inserted in a template at a point where
338 # docbook v3.1 requires a non-zero sequence of RefEntry's; see:
339 # https://www.oasis-open.org/docbook/documentation/reference/html/refentry.html
340 # We keep track of number of generated entries and generate a dummy
341 # if needs be to ensure the expanded template can be postprocessed
343 my $section_counter = 0;
349 STATE_NORMAL
=> 0, # normal code
350 STATE_NAME
=> 1, # looking for function name
351 STATE_BODY_MAYBE
=> 2, # body - or maybe more description
352 STATE_BODY
=> 3, # the body of the comment
353 STATE_BODY_WITH_BLANK_LINE
=> 4, # the body, which has a blank line
354 STATE_PROTO
=> 5, # scanning prototype
355 STATE_DOCBLOCK
=> 6, # documentation block
356 STATE_INLINE
=> 7, # gathering doc outside main block
362 # Inline documentation state
364 STATE_INLINE_NA
=> 0, # not applicable ($state != STATE_INLINE)
365 STATE_INLINE_NAME
=> 1, # looking for member name (@foo:)
366 STATE_INLINE_TEXT
=> 2, # looking for member documentation
367 STATE_INLINE_END
=> 3, # done
368 STATE_INLINE_ERROR
=> 4, # error - Comment without header was found.
369 # Spit a warning as it's not
370 # proper kernel-doc and ignore the rest.
372 my $inline_doc_state;
374 #declaration types: can be
375 # 'function', 'struct', 'union', 'enum', 'typedef'
378 my $doc_start = '^/\*\*\s*$'; # Allow whitespace at end of comment start.
380 my $doc_com = '\s*\*\s*';
381 my $doc_com_body = '\s*\* ?';
382 my $doc_decl = $doc_com . '(\w+)';
383 # @params and a strictly limited set of supported section names
384 my $doc_sect = $doc_com .
385 '\s*(\@[.\w]+|\@\.\.\.|description|context|returns?|notes?|examples?)\s*:(.*)';
386 my $doc_content = $doc_com_body . '(.*)';
387 my $doc_block = $doc_com . 'DOC:\s*(.*)?';
388 my $doc_inline_start = '^\s*/\*\*\s*$';
389 my $doc_inline_sect = '\s*\*\s*(@\s*[\w][\w\.]*\s*):(.*)';
390 my $doc_inline_end = '^\s*\*/\s*$';
391 my $doc_inline_oneline = '^\s*/\*\*\s*(@[\w\s]+):\s*(.*)\s*\*/\s*$';
392 my $export_symbol = '^\s*EXPORT_SYMBOL(_GPL)?\s*\(\s*(\w+)\s*\)\s*;';
395 my %parameterdesc_start_lines;
399 my %section_start_lines;
404 my $new_start_line = 0;
406 # the canonical section names. see also $doc_sect above.
407 my $section_default = "Description"; # default section
408 my $section_intro = "Introduction";
409 my $section = $section_default;
410 my $section_context = "Context";
411 my $section_return = "Return";
413 my $undescribed = "-- undescribed --";
417 while ($ARGV[0] =~ m/^--?(.*)/) {
421 $output_mode = "man";
422 @highlights = @highlights_man;
423 $blankline = $blankline_man;
424 } elsif ($cmd eq "rst") {
425 $output_mode = "rst";
426 @highlights = @highlights_rst;
427 $blankline = $blankline_rst;
428 } elsif ($cmd eq "none") {
429 $output_mode = "none";
430 } elsif ($cmd eq "module") { # not needed for XML, inherits from calling document
431 $modulename = shift @ARGV;
432 } elsif ($cmd eq "function") { # to only output specific functions
433 $output_selection = OUTPUT_INCLUDE
;
434 $function = shift @ARGV;
435 $function_table{$function} = 1;
436 } elsif ($cmd eq "nosymbol") { # Exclude specific symbols
437 my $symbol = shift @ARGV;
438 $nosymbol_table{$symbol} = 1;
439 } elsif ($cmd eq "export") { # only exported symbols
440 $output_selection = OUTPUT_EXPORTED
;
441 %function_table = ();
442 } elsif ($cmd eq "internal") { # only non-exported symbols
443 $output_selection = OUTPUT_INTERNAL
;
444 %function_table = ();
445 } elsif ($cmd eq "export-file") {
446 my $file = shift @ARGV;
447 push(@export_file_list, $file);
448 } elsif ($cmd eq "v") {
450 } elsif ($cmd eq "Werror") {
452 } elsif (($cmd eq "h") || ($cmd eq "help")) {
454 } elsif ($cmd eq 'no-doc-sections') {
455 $no_doc_sections = 1;
456 } elsif ($cmd eq 'enable-lineno') {
458 } elsif ($cmd eq 'show-not-found') {
459 $show_not_found = 1; # A no-op but don't fail
466 # continue execution near EOF;
468 # The C domain dialect changed on Sphinx 3. So, we need to check the
469 # version in order to produce the right tags.
472 foreach(split(/:/, $ENV{PATH
})) {
473 return "$_/$_[0]" if(-x
"$_/$_[0]");
477 sub get_sphinx_version
()
482 my $cmd = "sphinx-build";
483 if (!findprog
($cmd)) {
484 my $cmd = "sphinx-build3";
485 return $major if (!findprog
($cmd));
488 open IN
, "$cmd --version 2>&1 |";
490 if (m/^\s*sphinx-build\s+([\d]+)\.([\d\.]+)(\+\/[\da
-f
]+)?
$/) {
494 # Sphinx 1.2.x uses a different format
495 if (m/^\s*Sphinx.*\s+([\d]+)\.([\d\.]+)$/) {
505 # get kernel version from env
506 sub get_kernel_version
() {
507 my $version = 'unknown kernel version';
509 if (defined($ENV{'KERNELVERSION'})) {
510 $version = $ENV{'KERNELVERSION'};
518 if ($enable_lineno && defined($lineno)) {
519 print "#define LINENO " . $lineno . "\n";
523 # dumps section contents to arrays/hashes intended for that purpose.
528 my $contents = join "\n", @_;
530 if ($name =~ m/$type_param/) {
532 $parameterdescs{$name} = $contents;
533 $sectcheck = $sectcheck . $name . " ";
534 $parameterdesc_start_lines{$name} = $new_start_line;
536 } elsif ($name eq "@\.\.\.") {
538 $parameterdescs{$name} = $contents;
539 $sectcheck = $sectcheck . $name . " ";
540 $parameterdesc_start_lines{$name} = $new_start_line;
543 if (defined($sections{$name}) && ($sections{$name} ne "")) {
544 # Only warn on user specified duplicate section names.
545 if ($name ne $section_default) {
546 print STDERR
"${file}:$.: warning: duplicate section name '$name'\n";
549 $sections{$name} .= $contents;
551 $sections{$name} = $contents;
552 push @sectionlist, $name;
553 $section_start_lines{$name} = $new_start_line;
560 # dump DOC: section after checking that it should go out
562 sub dump_doc_section
{
565 my $contents = join "\n", @_;
567 if ($no_doc_sections) {
571 return if (defined($nosymbol_table{$name}));
573 if (($output_selection == OUTPUT_ALL
) ||
574 (($output_selection == OUTPUT_INCLUDE
) &&
575 defined($function_table{$name})))
577 dump_section
($file, $name, $contents);
578 output_blockhead
({'sectionlist' => \
@sectionlist,
579 'sections' => \
%sections,
580 'module' => $modulename,
581 'content-only' => ($output_selection != OUTPUT_ALL
), });
588 # parameterdescs, a hash.
589 # function => "function name"
590 # parameterlist => @list of parameters
591 # parameterdescs => %parameter descriptions
592 # sectionlist => @list of sections
593 # sections => %section descriptions
596 sub output_highlight
{
597 my $contents = join "\n",@_;
601 # if (!defined $contents) {
603 # confess "output_highlight got called with no args?\n";
606 # print STDERR "contents b4:$contents\n";
609 # print STDERR "contents af:$contents\n";
611 foreach $line (split "\n", $contents) {
612 if (! $output_preformatted) {
616 if (! $output_preformatted) {
617 print $lineprefix, $blankline;
620 if ($output_mode eq "man" && substr($line, 0, 1) eq ".") {
623 print $lineprefix, $line;
631 # output function in man
632 sub output_function_man
(%) {
634 my ($parameter, $section);
637 print ".TH \"$args{'function'}\" 9 \"$args{'function'}\" \"$man_date\" \"Kernel Hacker's Manual\" LINUX\n";
640 print $args{'function'} . " \\- " . $args{'purpose'} . "\n";
642 print ".SH SYNOPSIS\n";
643 if ($args{'functiontype'} ne "") {
644 print ".B \"" . $args{'functiontype'} . "\" " . $args{'function'} . "\n";
646 print ".B \"" . $args{'function'} . "\n";
651 foreach my $parameter (@
{$args{'parameterlist'}}) {
652 if ($count == $#{$args{'parameterlist'}}) {
655 $type = $args{'parametertypes'}{$parameter};
656 if ($type =~ m/([^\(]*\(\*)\s*\)\s*\(([^\)]*)\)/) {
657 # pointer-to-function
658 print ".BI \"" . $parenth . $1 . "\" " . " \") (" . $2 . ")" . $post . "\"\n";
660 $type =~ s/([^\*])$/$1 /;
661 print ".BI \"" . $parenth . $type . "\" " . " \"" . $post . "\"\n";
667 print ".SH ARGUMENTS\n";
668 foreach $parameter (@
{$args{'parameterlist'}}) {
669 my $parameter_name = $parameter;
670 $parameter_name =~ s/\[.*//;
672 print ".IP \"" . $parameter . "\" 12\n";
673 output_highlight
($args{'parameterdescs'}{$parameter_name});
675 foreach $section (@
{$args{'sectionlist'}}) {
676 print ".SH \"", uc $section, "\"\n";
677 output_highlight
($args{'sections'}{$section});
683 sub output_enum_man
(%) {
685 my ($parameter, $section);
688 print ".TH \"$args{'module'}\" 9 \"enum $args{'enum'}\" \"$man_date\" \"API Manual\" LINUX\n";
691 print "enum " . $args{'enum'} . " \\- " . $args{'purpose'} . "\n";
693 print ".SH SYNOPSIS\n";
694 print "enum " . $args{'enum'} . " {\n";
696 foreach my $parameter (@
{$args{'parameterlist'}}) {
697 print ".br\n.BI \" $parameter\"\n";
698 if ($count == $#{$args{'parameterlist'}}) {
708 print ".SH Constants\n";
709 foreach $parameter (@
{$args{'parameterlist'}}) {
710 my $parameter_name = $parameter;
711 $parameter_name =~ s/\[.*//;
713 print ".IP \"" . $parameter . "\" 12\n";
714 output_highlight
($args{'parameterdescs'}{$parameter_name});
716 foreach $section (@
{$args{'sectionlist'}}) {
717 print ".SH \"$section\"\n";
718 output_highlight
($args{'sections'}{$section});
723 # output struct in man
724 sub output_struct_man
(%) {
726 my ($parameter, $section);
728 print ".TH \"$args{'module'}\" 9 \"" . $args{'type'} . " " . $args{'struct'} . "\" \"$man_date\" \"API Manual\" LINUX\n";
731 print $args{'type'} . " " . $args{'struct'} . " \\- " . $args{'purpose'} . "\n";
733 my $declaration = $args{'definition'};
734 $declaration =~ s/\t/ /g;
735 $declaration =~ s/\n/"\n.br\n.BI \"/g;
736 print ".SH SYNOPSIS\n";
737 print $args{'type'} . " " . $args{'struct'} . " {\n.br\n";
738 print ".BI \"$declaration\n};\n.br\n\n";
740 print ".SH Members\n";
741 foreach $parameter (@
{$args{'parameterlist'}}) {
742 ($parameter =~ /^#/) && next;
744 my $parameter_name = $parameter;
745 $parameter_name =~ s/\[.*//;
747 ($args{'parameterdescs'}{$parameter_name} ne $undescribed) || next;
748 print ".IP \"" . $parameter . "\" 12\n";
749 output_highlight
($args{'parameterdescs'}{$parameter_name});
751 foreach $section (@
{$args{'sectionlist'}}) {
752 print ".SH \"$section\"\n";
753 output_highlight
($args{'sections'}{$section});
758 # output typedef in man
759 sub output_typedef_man
(%) {
761 my ($parameter, $section);
763 print ".TH \"$args{'module'}\" 9 \"$args{'typedef'}\" \"$man_date\" \"API Manual\" LINUX\n";
766 print "typedef " . $args{'typedef'} . " \\- " . $args{'purpose'} . "\n";
768 foreach $section (@
{$args{'sectionlist'}}) {
769 print ".SH \"$section\"\n";
770 output_highlight
($args{'sections'}{$section});
774 sub output_blockhead_man
(%) {
776 my ($parameter, $section);
779 print ".TH \"$args{'module'}\" 9 \"$args{'module'}\" \"$man_date\" \"API Manual\" LINUX\n";
781 foreach $section (@
{$args{'sectionlist'}}) {
782 print ".SH \"$section\"\n";
783 output_highlight
($args{'sections'}{$section});
788 # output in restructured text
792 # This could use some work; it's used to output the DOC: sections, and
793 # starts by putting out the name of the doc section itself, but that tends
794 # to duplicate a header already in the template file.
796 sub output_blockhead_rst
(%) {
798 my ($parameter, $section);
800 foreach $section (@
{$args{'sectionlist'}}) {
801 next if (defined($nosymbol_table{$section}));
803 if ($output_selection != OUTPUT_INCLUDE
) {
804 print "**$section**\n\n";
806 print_lineno
($section_start_lines{$section});
807 output_highlight_rst
($args{'sections'}{$section});
813 # Apply the RST highlights to a sub-block of text.
815 sub highlight_block
($) {
816 # The dohighlight kludge requires the text be called $contents
817 my $contents = shift;
824 # Regexes used only here.
826 my $sphinx_literal = '^[^.].*::$';
827 my $sphinx_cblock = '^\.\.\ +code-block::';
829 sub output_highlight_rst
{
830 my $input = join "\n",@_;
837 foreach $line (split "\n",$input) {
839 # If we're in a literal block, see if we should drop out
840 # of it. Otherwise pass the line straight through unmunged.
843 if (! ($line =~ /^\s*$/)) {
845 # If this is the first non-blank line in a literal
846 # block we need to figure out what the proper indent is.
848 if ($litprefix eq "") {
850 $litprefix = '^' . $1;
851 $output .= $line . "\n";
852 } elsif (! ($line =~ /$litprefix/)) {
855 $output .= $line . "\n";
858 $output .= $line . "\n";
862 # Not in a literal block (or just dropped out)
865 $block .= $line . "\n";
866 if (($line =~ /$sphinx_literal/) || ($line =~ /$sphinx_cblock/)) {
869 $output .= highlight_block
($block);
876 $output .= highlight_block
($block);
878 foreach $line (split "\n", $output) {
879 print $lineprefix . $line . "\n";
883 sub output_function_rst
(%) {
885 my ($parameter, $section);
886 my $oldprefix = $lineprefix;
889 if ($sphinx_major < 3) {
890 if ($args{'typedef'}) {
891 print ".. c:type:: ". $args{'function'} . "\n\n";
892 print_lineno
($declaration_start_line);
893 print " **Typedef**: ";
895 output_highlight_rst
($args{'purpose'});
896 $start = "\n\n**Syntax**\n\n ``";
898 print ".. c:function:: ";
901 print ".. c:macro:: ". $args{'function'} . "\n\n";
903 if ($args{'typedef'}) {
904 print_lineno
($declaration_start_line);
905 print " **Typedef**: ";
907 output_highlight_rst
($args{'purpose'});
908 $start = "\n\n**Syntax**\n\n ``";
913 if ($args{'functiontype'} ne "") {
914 $start .= $args{'functiontype'} . " " . $args{'function'} . " (";
916 $start .= $args{'function'} . " (";
921 foreach my $parameter (@
{$args{'parameterlist'}}) {
926 $type = $args{'parametertypes'}{$parameter};
928 if ($type =~ m/([^\(]*\(\*)\s*\)\s*\(([^\)]*)\)/) {
929 # pointer-to-function
930 print $1 . $parameter . ") (" . $2 . ")";
935 if ($args{'typedef'}) {
938 if ($sphinx_major < 3) {
943 print_lineno
($declaration_start_line);
945 output_highlight_rst
($args{'purpose'});
949 print "**Parameters**\n\n";
951 foreach $parameter (@
{$args{'parameterlist'}}) {
952 my $parameter_name = $parameter;
953 $parameter_name =~ s/\[.*//;
954 $type = $args{'parametertypes'}{$parameter};
959 print "``$parameter``\n";
962 print_lineno
($parameterdesc_start_lines{$parameter_name});
964 if (defined($args{'parameterdescs'}{$parameter_name}) &&
965 $args{'parameterdescs'}{$parameter_name} ne $undescribed) {
966 output_highlight_rst
($args{'parameterdescs'}{$parameter_name});
968 print " *undescribed*\n";
973 $lineprefix = $oldprefix;
974 output_section_rst
(@_);
977 sub output_section_rst
(%) {
980 my $oldprefix = $lineprefix;
983 foreach $section (@
{$args{'sectionlist'}}) {
984 print "**$section**\n\n";
985 print_lineno
($section_start_lines{$section});
986 output_highlight_rst
($args{'sections'}{$section});
990 $lineprefix = $oldprefix;
993 sub output_enum_rst
(%) {
996 my $oldprefix = $lineprefix;
999 if ($sphinx_major < 3) {
1000 my $name = "enum " . $args{'enum'};
1001 print "\n\n.. c:type:: " . $name . "\n\n";
1003 my $name = $args{'enum'};
1004 print "\n\n.. c:enum:: " . $name . "\n\n";
1006 print_lineno
($declaration_start_line);
1008 output_highlight_rst
($args{'purpose'});
1011 print "**Constants**\n\n";
1013 foreach $parameter (@
{$args{'parameterlist'}}) {
1014 print "``$parameter``\n";
1015 if ($args{'parameterdescs'}{$parameter} ne $undescribed) {
1016 output_highlight_rst
($args{'parameterdescs'}{$parameter});
1018 print " *undescribed*\n";
1023 $lineprefix = $oldprefix;
1024 output_section_rst
(@_);
1027 sub output_typedef_rst
(%) {
1028 my %args = %{$_[0]};
1030 my $oldprefix = $lineprefix;
1033 if ($sphinx_major < 3) {
1034 $name = "typedef " . $args{'typedef'};
1036 $name = $args{'typedef'};
1038 print "\n\n.. c:type:: " . $name . "\n\n";
1039 print_lineno
($declaration_start_line);
1041 output_highlight_rst
($args{'purpose'});
1044 $lineprefix = $oldprefix;
1045 output_section_rst
(@_);
1048 sub output_struct_rst
(%) {
1049 my %args = %{$_[0]};
1051 my $oldprefix = $lineprefix;
1053 if ($sphinx_major < 3) {
1054 my $name = $args{'type'} . " " . $args{'struct'};
1055 print "\n\n.. c:type:: " . $name . "\n\n";
1057 my $name = $args{'struct'};
1058 print "\n\n.. c:struct:: " . $name . "\n\n";
1060 print_lineno
($declaration_start_line);
1062 output_highlight_rst
($args{'purpose'});
1065 print "**Definition**\n\n";
1067 my $declaration = $args{'definition'};
1068 $declaration =~ s/\t/ /g;
1069 print " " . $args{'type'} . " " . $args{'struct'} . " {\n$declaration };\n\n";
1071 print "**Members**\n\n";
1073 foreach $parameter (@
{$args{'parameterlist'}}) {
1074 ($parameter =~ /^#/) && next;
1076 my $parameter_name = $parameter;
1077 $parameter_name =~ s/\[.*//;
1079 ($args{'parameterdescs'}{$parameter_name} ne $undescribed) || next;
1080 $type = $args{'parametertypes'}{$parameter};
1081 print_lineno
($parameterdesc_start_lines{$parameter_name});
1082 print "``" . $parameter . "``\n";
1083 output_highlight_rst
($args{'parameterdescs'}{$parameter_name});
1088 $lineprefix = $oldprefix;
1089 output_section_rst
(@_);
1092 ## none mode output functions
1094 sub output_function_none
(%) {
1097 sub output_enum_none
(%) {
1100 sub output_typedef_none
(%) {
1103 sub output_struct_none
(%) {
1106 sub output_blockhead_none
(%) {
1110 # generic output function for all types (function, struct/union, typedef, enum);
1111 # calls the generated, variable output_ function name based on
1112 # functype and output_mode
1113 sub output_declaration
{
1116 my $functype = shift;
1117 my $func = "output_${functype}_$output_mode";
1119 return if (defined($nosymbol_table{$name}));
1121 if (($output_selection == OUTPUT_ALL
) ||
1122 (($output_selection == OUTPUT_INCLUDE
||
1123 $output_selection == OUTPUT_EXPORTED
) &&
1124 defined($function_table{$name})) ||
1125 ($output_selection == OUTPUT_INTERNAL
&&
1126 !($functype eq "function" && defined($function_table{$name}))))
1134 # generic output function - calls the right one based on current output mode.
1135 sub output_blockhead
{
1137 my $func = "output_blockhead_" . $output_mode;
1143 # takes a declaration (struct, union, enum, typedef) and
1144 # invokes the right handler. NOT called for functions.
1145 sub dump_declaration
($$) {
1147 my ($prototype, $file) = @_;
1148 my $func = "dump_" . $decl_type;
1152 sub dump_union
($$) {
1156 sub dump_struct
($$) {
1160 if ($x =~ /(struct|union)\s+(\w+)\s*\{(.*)\}(\s*(__packed|__aligned|____cacheline_aligned_in_smp|____cacheline_aligned|__attribute__\s*\(\([a-z0-9,_\s\(\)]*\)\)))*/) {
1162 $declaration_name = $2;
1165 # ignore members marked private:
1166 $members =~ s/\/\*\s*private:.*?\/\
*\s
*public
:.*?\
*\
///gosi
;
1167 $members =~ s/\/\*\s*private:.*//gosi
;
1169 $members =~ s/\/\*.*?\*\///gos;
1171 $members =~ s/\s*__attribute__\s*\(\([a-z0-9,_\*\s\(\)]*\)\)/ /gi;
1172 $members =~ s/\s*__aligned\s*\([^;]*\)/ /gos;
1173 $members =~ s/\s*__packed\s*/ /gos;
1174 $members =~ s/\s*CRYPTO_MINALIGN_ATTR/ /gos;
1175 $members =~ s/\s*____cacheline_aligned_in_smp/ /gos;
1176 $members =~ s/\s*____cacheline_aligned/ /gos;
1178 # replace DECLARE_BITMAP
1179 $members =~ s/__ETHTOOL_DECLARE_LINK_MODE_MASK\s*\(([^\)]+)\)/DECLARE_BITMAP($1, __ETHTOOL_LINK_MODE_MASK_NBITS)/gos;
1180 $members =~ s/DECLARE_BITMAP\s*\(([^,)]+),\s*([^,)]+)\)/unsigned long $1\[BITS_TO_LONGS($2)\]/gos;
1181 # replace DECLARE_HASHTABLE
1182 $members =~ s/DECLARE_HASHTABLE\s*\(([^,)]+),\s*([^,)]+)\)/unsigned long $1\[1 << (($2) - 1)\]/gos;
1183 # replace DECLARE_KFIFO
1184 $members =~ s/DECLARE_KFIFO\s*\(([^,)]+),\s*([^,)]+),\s*([^,)]+)\)/$2 \*$1/gos;
1185 # replace DECLARE_KFIFO_PTR
1186 $members =~ s/DECLARE_KFIFO_PTR\s*\(([^,)]+),\s*([^,)]+)\)/$2 \*$1/gos;
1188 my $declaration = $members;
1190 # Split nested struct/union elements as newer ones
1191 while ($members =~ m/(struct|union)([^\{\};]+)\{([^\{\}]*)\}([^\{\}\;]*)\;/) {
1196 foreach my $id(split /,/, $ids) {
1197 $newmember .= "$maintype $id; ";
1200 $id =~ s/^\s*\**(\S+)\s*/$1/;
1201 foreach my $arg (split /;/, $content) {
1202 next if ($arg =~ m/^\s*$/);
1203 if ($arg =~ m/^([^\(]+\(\*?\s*)([\w\.]*)(\s*\).*)/) {
1204 # pointer-to-function
1209 if ($id =~ m/^\s*$/) {
1210 # anonymous struct/union
1211 $newmember .= "$type$name$extra; ";
1213 $newmember .= "$type$id.$name$extra; ";
1221 $arg =~ s/:\s*\d+\s*//g;
1223 $arg =~ s/\[.*\]//g;
1224 # The type may have multiple words,
1225 # and multiple IDs can be defined, like:
1226 # const struct foo, *bar, foobar
1227 # So, we remove spaces when parsing the
1228 # names, in order to match just names
1229 # and commas for the names
1230 $arg =~ s/\s*,\s*/,/g;
1231 if ($arg =~ m/(.*)\s+([\S+,]+)/) {
1235 $newmember .= "$arg; ";
1238 foreach my $name (split /,/, $names) {
1239 $name =~ s/^\s*\**(\S+)\s*/$1/;
1240 next if (($name =~ m/^\s*$/));
1241 if ($id =~ m/^\s*$/) {
1242 # anonymous struct/union
1243 $newmember .= "$type $name; ";
1245 $newmember .= "$type $id.$name; ";
1251 $members =~ s/(struct|union)([^\{\};]+)\{([^\{\}]*)\}([^\{\}\;]*)\;/$newmember/;
1254 # Ignore other nested elements, like enums
1255 $members =~ s/(\{[^\{\}]*\})//g;
1257 create_parameterlist
($members, ';', $file, $declaration_name);
1258 check_sections
($file, $declaration_name, $decl_type, $sectcheck, $struct_actual);
1260 # Adjust declaration for better display
1261 $declaration =~ s/([\{;])/$1\n/g;
1262 $declaration =~ s/\}\s+;/};/g;
1263 # Better handle inlined enums
1264 do {} while ($declaration =~ s/(enum\s+\{[^\}]+),([^\n])/$1,\n$2/);
1266 my @def_args = split /\n/, $declaration;
1269 foreach my $clause (@def_args) {
1270 $clause =~ s/^\s+//;
1271 $clause =~ s/\s+$//;
1272 $clause =~ s/\s+/ /;
1274 $level-- if ($clause =~ m/(\})/ && $level > 1);
1275 if (!($clause =~ m/^\s*#/)) {
1276 $declaration .= "\t" x
$level;
1278 $declaration .= "\t" . $clause . "\n";
1279 $level++ if ($clause =~ m/(\{)/ && !($clause =~m/\}/));
1281 output_declaration
($declaration_name,
1283 {'struct' => $declaration_name,
1284 'module' => $modulename,
1285 'definition' => $declaration,
1286 'parameterlist' => \
@parameterlist,
1287 'parameterdescs' => \
%parameterdescs,
1288 'parametertypes' => \
%parametertypes,
1289 'sectionlist' => \
@sectionlist,
1290 'sections' => \
%sections,
1291 'purpose' => $declaration_purpose,
1292 'type' => $decl_type
1296 print STDERR
"${file}:$.: error: Cannot parse struct or union!\n";
1302 sub show_warnings
($$) {
1303 my $functype = shift;
1306 return 0 if (defined($nosymbol_table{$name}));
1308 return 1 if ($output_selection == OUTPUT_ALL
);
1310 if ($output_selection == OUTPUT_EXPORTED
) {
1311 if (defined($function_table{$name})) {
1317 if ($output_selection == OUTPUT_INTERNAL
) {
1318 if (!($functype eq "function" && defined($function_table{$name}))) {
1324 if ($output_selection == OUTPUT_INCLUDE
) {
1325 if (defined($function_table{$name})) {
1331 die("Please add the new output type at show_warnings()");
1340 $x =~ s@
/\*.*?\*/@
@gos; # strip comments.
1341 # strip #define macros inside enums
1342 $x =~ s@
#\s*((define|ifdef)\s+|endif)[^;]*;@@gos;
1344 if ($x =~ /typedef\s+enum\s*\{(.*)\}\s*(\w*)\s*;/) {
1345 $declaration_name = $2;
1347 } elsif ($x =~ /enum\s+(\w*)\s*\{(.*)\}/) {
1348 $declaration_name = $1;
1352 if ($declaration_name) {
1355 $members =~ s/\s+$//;
1357 foreach my $arg (split ',', $members) {
1358 $arg =~ s/^\s*(\w+).*/$1/;
1359 push @parameterlist, $arg;
1360 if (!$parameterdescs{$arg}) {
1361 $parameterdescs{$arg} = $undescribed;
1362 if (show_warnings
("enum", $declaration_name)) {
1363 print STDERR
"${file}:$.: warning: Enum value '$arg' not described in enum '$declaration_name'\n";
1366 $_members{$arg} = 1;
1369 while (my ($k, $v) = each %parameterdescs) {
1370 if (!exists($_members{$k})) {
1371 if (show_warnings
("enum", $declaration_name)) {
1372 print STDERR
"${file}:$.: warning: Excess enum value '$k' description in '$declaration_name'\n";
1377 output_declaration
($declaration_name,
1379 {'enum' => $declaration_name,
1380 'module' => $modulename,
1381 'parameterlist' => \
@parameterlist,
1382 'parameterdescs' => \
%parameterdescs,
1383 'sectionlist' => \
@sectionlist,
1384 'sections' => \
%sections,
1385 'purpose' => $declaration_purpose
1388 print STDERR
"${file}:$.: error: Cannot parse enum!\n";
1393 sub dump_typedef
($$) {
1397 $x =~ s@
/\*.*?\*/@
@gos; # strip comments.
1399 # Parse function prototypes
1400 if ($x =~ /typedef\s+(\w+\s*\**)\s*\(\*?\s*(\w\S+)\s*\)\s*\((.*)\);/ ||
1401 $x =~ /typedef\s+(\w+\s*\**)\s*(\w\S+)\s*\s*\((.*)\);/) {
1405 $declaration_name = $2;
1408 create_parameterlist
($args, ',', $file, $declaration_name);
1410 output_declaration
($declaration_name,
1412 {'function' => $declaration_name,
1414 'module' => $modulename,
1415 'functiontype' => $return_type,
1416 'parameterlist' => \
@parameterlist,
1417 'parameterdescs' => \
%parameterdescs,
1418 'parametertypes' => \
%parametertypes,
1419 'sectionlist' => \
@sectionlist,
1420 'sections' => \
%sections,
1421 'purpose' => $declaration_purpose
1426 while (($x =~ /\(*.\)\s*;$/) || ($x =~ /\[*.\]\s*;$/)) {
1427 $x =~ s/\(*.\)\s*;$/;/;
1428 $x =~ s/\[*.\]\s*;$/;/;
1431 if ($x =~ /typedef.*\s+(\w+)\s*;/) {
1432 $declaration_name = $1;
1434 output_declaration
($declaration_name,
1436 {'typedef' => $declaration_name,
1437 'module' => $modulename,
1438 'sectionlist' => \
@sectionlist,
1439 'sections' => \
%sections,
1440 'purpose' => $declaration_purpose
1444 print STDERR
"${file}:$.: error: Cannot parse typedef!\n";
1449 sub save_struct_actual
($) {
1452 # strip all spaces from the actual param so that it looks like one string item
1453 $actual =~ s/\s*//g;
1454 $struct_actual = $struct_actual . $actual . " ";
1457 sub create_parameterlist
($$$$) {
1459 my $splitter = shift;
1461 my $declaration_name = shift;
1465 # temporarily replace commas inside function pointer definition
1466 while ($args =~ /(\([^\),]+),/) {
1467 $args =~ s/(\([^\),]+),/$1#/g;
1470 foreach my $arg (split($splitter, $args)) {
1472 $arg =~ s/\/\*.*\*\///;
1473 # strip leading/trailing spaces
1479 # Treat preprocessor directive as a typeless variable just to fill
1480 # corresponding data structures "correctly". Catch it later in
1482 push_parameter
($arg, "", "", $file);
1483 } elsif ($arg =~ m/\(.+\)\s*\(/) {
1484 # pointer-to-function
1486 $arg =~ m/[^\(]+\(\*?\s*([\w\.]*)\s*\)/;
1489 $type =~ s/([^\(]+\(\*?)\s*$param/$1/;
1490 save_struct_actual
($param);
1491 push_parameter
($param, $type, $arg, $file, $declaration_name);
1493 $arg =~ s/\s*:\s*/:/g;
1494 $arg =~ s/\s*\[/\[/g;
1496 my @args = split('\s*,\s*', $arg);
1497 if ($args[0] =~ m/\*/) {
1498 $args[0] =~ s/(\*+)\s*/ $1/;
1502 if ($args[0] =~ /^(.*\s+)(.*?\[.*\].*)$/) {
1504 push(@first_arg, split('\s+', $1));
1505 push(@first_arg, $2);
1507 @first_arg = split('\s+', shift @args);
1510 unshift(@args, pop @first_arg);
1511 $type = join " ", @first_arg;
1513 foreach $param (@args) {
1514 if ($param =~ m/^(\*+)\s*(.*)/) {
1515 save_struct_actual
($2);
1517 push_parameter
($2, "$type $1", $arg, $file, $declaration_name);
1519 elsif ($param =~ m/(.*?):(\d+)/) {
1520 if ($type ne "") { # skip unnamed bit-fields
1521 save_struct_actual
($1);
1522 push_parameter
($1, "$type:$2", $arg, $file, $declaration_name)
1526 save_struct_actual
($param);
1527 push_parameter
($param, $type, $arg, $file, $declaration_name);
1534 sub push_parameter
($$$$$) {
1537 my $org_arg = shift;
1539 my $declaration_name = shift;
1541 if (($anon_struct_union == 1) && ($type eq "") &&
1543 return; # ignore the ending }; from anon. struct/union
1546 $anon_struct_union = 0;
1547 $param =~ s/[\[\)].*//;
1549 if ($type eq "" && $param =~ /\.\.\.$/)
1551 if (!$param =~ /\w\.\.\.$/) {
1552 # handles unnamed variable parameters
1555 elsif ($param =~ /\w\.\.\.$/) {
1556 # for named variable parameters of the form `x...`, remove the dots
1557 $param =~ s/\.\.\.$//;
1559 if (!defined $parameterdescs{$param} || $parameterdescs{$param} eq "") {
1560 $parameterdescs{$param} = "variable arguments";
1563 elsif ($type eq "" && ($param eq "" or $param eq "void"))
1566 $parameterdescs{void
} = "no arguments";
1568 elsif ($type eq "" && ($param eq "struct" or $param eq "union"))
1569 # handle unnamed (anonymous) union or struct:
1572 $param = "{unnamed_" . $param . "}";
1573 $parameterdescs{$param} = "anonymous\n";
1574 $anon_struct_union = 1;
1577 # warn if parameter has no description
1578 # (but ignore ones starting with # as these are not parameters
1579 # but inline preprocessor statements);
1580 # Note: It will also ignore void params and unnamed structs/unions
1581 if (!defined $parameterdescs{$param} && $param !~ /^#/) {
1582 $parameterdescs{$param} = $undescribed;
1584 if (show_warnings
($type, $declaration_name) && $param !~ /\./) {
1586 "${file}:$.: warning: Function parameter or member '$param' not described in '$declaration_name'\n";
1591 # strip spaces from $param so that it is one continuous string
1592 # on @parameterlist;
1593 # this fixes a problem where check_sections() cannot find
1594 # a parameter like "addr[6 + 2]" because it actually appears
1595 # as "addr[6", "+", "2]" on the parameter list;
1596 # but it's better to maintain the param string unchanged for output,
1597 # so just weaken the string compare in check_sections() to ignore
1598 # "[blah" in a parameter string;
1599 ###$param =~ s/\s*//g;
1600 push @parameterlist, $param;
1601 $org_arg =~ s/\s\s+/ /g;
1602 $parametertypes{$param} = $org_arg;
1605 sub check_sections
($$$$$) {
1606 my ($file, $decl_name, $decl_type, $sectcheck, $prmscheck) = @_;
1607 my @sects = split ' ', $sectcheck;
1608 my @prms = split ' ', $prmscheck;
1611 my $prm_clean; # strip trailing "[array size]" and/or beginning "*"
1613 foreach $sx (0 .. $#sects) {
1615 foreach $px (0 .. $#prms) {
1616 $prm_clean = $prms[$px];
1617 $prm_clean =~ s/\[.*\]//;
1618 $prm_clean =~ s/__attribute__\s*\(\([a-z,_\*\s\(\)]*\)\)//i;
1619 # ignore array size in a parameter string;
1620 # however, the original param string may contain
1621 # spaces, e.g.: addr[6 + 2]
1622 # and this appears in @prms as "addr[6" since the
1623 # parameter list is split at spaces;
1624 # hence just ignore "[..." for the sections check;
1625 $prm_clean =~ s/\[.*//;
1627 ##$prm_clean =~ s/^\**//;
1628 if ($prm_clean eq $sects[$sx]) {
1634 if ($decl_type eq "function") {
1635 print STDERR
"${file}:$.: warning: " .
1636 "Excess function parameter " .
1638 "description in '$decl_name'\n";
1646 # Checks the section describing the return value of a function.
1647 sub check_return_section
{
1649 my $declaration_name = shift;
1650 my $return_type = shift;
1652 # Ignore an empty return type (It's a macro)
1653 # Ignore functions with a "void" return type. (But don't ignore "void *")
1654 if (($return_type eq "") || ($return_type =~ /void\s*\w*\s*$/)) {
1658 if (!defined($sections{$section_return}) ||
1659 $sections{$section_return} eq "") {
1660 print STDERR
"${file}:$.: warning: " .
1661 "No description found for return value of " .
1662 "'$declaration_name'\n";
1668 # takes a function prototype and the name of the current file being
1669 # processed and spits out all the details stored in the global
1671 sub dump_function
($$) {
1672 my $prototype = shift;
1678 $prototype =~ s/^static +//;
1679 $prototype =~ s/^extern +//;
1680 $prototype =~ s/^asmlinkage +//;
1681 $prototype =~ s/^inline +//;
1682 $prototype =~ s/^__inline__ +//;
1683 $prototype =~ s/^__inline +//;
1684 $prototype =~ s/^__always_inline +//;
1685 $prototype =~ s/^noinline +//;
1686 $prototype =~ s/__init +//;
1687 $prototype =~ s/__init_or_module +//;
1688 $prototype =~ s/__meminit +//;
1689 $prototype =~ s/__must_check +//;
1690 $prototype =~ s/__weak +//;
1691 $prototype =~ s/__sched +//;
1692 $prototype =~ s/__printf\s*\(\s*\d*\s*,\s*\d*\s*\) +//;
1693 my $define = $prototype =~ s/^#\s*define\s+//; #ak added
1694 $prototype =~ s
/__attribute__\s
*\
(\
(
1696 [\w\s
]++ # attribute name
1697 (?
:\
([^)]*+\
))?
# attribute arguments
1698 \s
*+,?
# optional comma at the end
1702 # Yes, this truly is vile. We are looking for:
1703 # 1. Return type (may be nothing if we're looking at a macro)
1705 # 3. Function parameters.
1707 # All the while we have to watch out for function pointer parameters
1708 # (which IIRC is what the two sections are for), C types (these
1709 # regexps don't even start to express all the possibilities), and
1712 # If you mess with these regexps, it's a good idea to check that
1713 # the following functions' documentation still comes out right:
1714 # - parport_register_device (function pointer parameters)
1715 # - qatomic_set (macro)
1716 # - pci_match_device, __copy_to_user (long return type)
1718 if ($define && $prototype =~ m/^()([a-zA-Z0-9_~:]+)\s+/) {
1719 # This is an object-like macro, it has no return type and no parameter
1721 # Function-like macros are not allowed to have spaces between
1722 # declaration_name and opening parenthesis (notice the \s+).
1724 $declaration_name = $2;
1726 } elsif ($prototype =~ m/^()([a-zA-Z0-9_~:]+)\s*\(([^\(]*)\)/ ||
1727 $prototype =~ m/^(\w+)\s+([a-zA-Z0-9_~:]+)\s*\(([^\(]*)\)/ ||
1728 $prototype =~ m/^(\w+\s*\*+)\s*([a-zA-Z0-9_~:]+)\s*\(([^\(]*)\)/ ||
1729 $prototype =~ m/^(\w+\s+\w+)\s+([a-zA-Z0-9_~:]+)\s*\(([^\(]*)\)/ ||
1730 $prototype =~ m/^(\w+\s+\w+\s*\*+)\s*([a-zA-Z0-9_~:]+)\s*\(([^\(]*)\)/ ||
1731 $prototype =~ m/^(\w+\s+\w+\s+\w+)\s+([a-zA-Z0-9_~:]+)\s*\(([^\(]*)\)/ ||
1732 $prototype =~ m/^(\w+\s+\w+\s+\w+\s*\*+)\s*([a-zA-Z0-9_~:]+)\s*\(([^\(]*)\)/ ||
1733 $prototype =~ m/^()([a-zA-Z0-9_~:]+)\s*\(([^\{]*)\)/ ||
1734 $prototype =~ m/^(\w+)\s+([a-zA-Z0-9_~:]+)\s*\(([^\{]*)\)/ ||
1735 $prototype =~ m/^(\w+\s*\*+)\s*([a-zA-Z0-9_~:]+)\s*\(([^\{]*)\)/ ||
1736 $prototype =~ m/^(\w+\s+\w+)\s+([a-zA-Z0-9_~:]+)\s*\(([^\{]*)\)/ ||
1737 $prototype =~ m/^(\w+\s+\w+\s*\*+)\s*([a-zA-Z0-9_~:]+)\s*\(([^\{]*)\)/ ||
1738 $prototype =~ m/^(\w+\s+\w+\s+\w+)\s+([a-zA-Z0-9_~:]+)\s*\(([^\{]*)\)/ ||
1739 $prototype =~ m/^(\w+\s+\w+\s+\w+\s*\*+)\s*([a-zA-Z0-9_~:]+)\s*\(([^\{]*)\)/ ||
1740 $prototype =~ m/^(\w+\s+\w+\s+\w+\s+\w+)\s+([a-zA-Z0-9_~:]+)\s*\(([^\{]*)\)/ ||
1741 $prototype =~ m/^(\w+\s+\w+\s+\w+\s+\w+\s*\*+)\s*([a-zA-Z0-9_~:]+)\s*\(([^\{]*)\)/ ||
1742 $prototype =~ m/^(\w+\s+\w+\s*\*+\s*\w+\s*\*+\s*)\s*([a-zA-Z0-9_~:]+)\s*\(([^\{]*)\)/) {
1744 $declaration_name = $2;
1747 create_parameterlist
($args, ',', $file, $declaration_name);
1749 print STDERR
"${file}:$.: warning: cannot understand function prototype: '$prototype'\n";
1753 my $prms = join " ", @parameterlist;
1754 check_sections
($file, $declaration_name, "function", $sectcheck, $prms);
1756 # This check emits a lot of warnings at the moment, because many
1757 # functions don't have a 'Return' doc section. So until the number
1758 # of warnings goes sufficiently down, the check is only performed in
1760 # TODO: always perform the check.
1761 if ($verbose && !$noret) {
1762 check_return_section
($file, $declaration_name, $return_type);
1765 # The function parser can be called with a typedef parameter.
1767 if ($return_type =~ /typedef/) {
1768 output_declaration
($declaration_name,
1770 {'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
1782 output_declaration
($declaration_name,
1784 {'function' => $declaration_name,
1785 'module' => $modulename,
1786 'functiontype' => $return_type,
1787 'parameterlist' => \
@parameterlist,
1788 'parameterdescs' => \
%parameterdescs,
1789 'parametertypes' => \
%parametertypes,
1790 'sectionlist' => \
@sectionlist,
1791 'sections' => \
%sections,
1792 'purpose' => $declaration_purpose
1799 %parameterdescs = ();
1800 %parametertypes = ();
1801 @parameterlist = ();
1805 $struct_actual = "";
1808 $state = STATE_NORMAL
;
1809 $inline_doc_state = STATE_INLINE_NA
;
1812 sub tracepoint_munge
($) {
1814 my $tracepointname = 0;
1815 my $tracepointargs = 0;
1817 if ($prototype =~ m/TRACE_EVENT\((.*?),/) {
1818 $tracepointname = $1;
1820 if ($prototype =~ m/DEFINE_SINGLE_EVENT\((.*?),/) {
1821 $tracepointname = $1;
1823 if ($prototype =~ m/DEFINE_EVENT\((.*?),(.*?),/) {
1824 $tracepointname = $2;
1826 $tracepointname =~ s/^\s+//; #strip leading whitespace
1827 if ($prototype =~ m/TP_PROTO\((.*?)\)/) {
1828 $tracepointargs = $1;
1830 if (($tracepointname eq 0) || ($tracepointargs eq 0)) {
1831 print STDERR
"${file}:$.: warning: Unrecognized tracepoint format: \n".
1834 $prototype = "static inline void trace_$tracepointname($tracepointargs)";
1838 sub syscall_munge
() {
1841 $prototype =~ s@
[\r\n]+@
@gos; # strip newlines/CR's
1842 ## if ($prototype =~ m/SYSCALL_DEFINE0\s*\(\s*(a-zA-Z0-9_)*\s*\)/) {
1843 if ($prototype =~ m/SYSCALL_DEFINE0/) {
1845 ## $prototype = "long sys_$1(void)";
1848 $prototype =~ s/SYSCALL_DEFINE.*\(/long sys_/; # fix return type & func name
1849 if ($prototype =~ m/long (sys_.*?),/) {
1850 $prototype =~ s/,/\(/;
1852 $prototype =~ s/\)/\(void\)/;
1855 # now delete all of the odd-number commas in $prototype
1856 # so that arg types & arg names don't have a comma between them
1858 my $len = length($prototype);
1860 $len = 0; # skip the for-loop
1862 for (my $ix = 0; $ix < $len; $ix++) {
1863 if (substr($prototype, $ix, 1) eq ',') {
1865 if ($count % 2 == 1) {
1866 substr($prototype, $ix, 1) = ' ';
1872 sub process_proto_function
($$) {
1876 $x =~ s@\
/\/.*$@
@gos; # strip C99-style comments to end of line
1878 if ($x =~ m
#\s*/\*\s+MACDOC\s*#io || ($x =~ /^#/ && $x !~ /^#\s*define/)) {
1881 elsif ($x =~ /([^\{]*)/) {
1885 if (($x =~ /\{/) || ($x =~ /\#\s*define/) || ($x =~ /;/)) {
1886 $prototype =~ s@
/\*.*?\*/@
@gos; # strip comments.
1887 $prototype =~ s@
[\r\n]+@
@gos; # strip newlines/cr's.
1888 $prototype =~ s@
^\s
+@
@gos; # strip leading spaces
1890 # Handle prototypes for function pointers like:
1891 # int (*pcs_config)(struct foo)
1892 $prototype =~ s@
^(\S
+\s
+)\
(\s
*\
*(\S
+)\
)@
$1$2@gos;
1894 if ($prototype =~ /SYSCALL_DEFINE/) {
1897 if ($prototype =~ /TRACE_EVENT/ || $prototype =~ /DEFINE_EVENT/ ||
1898 $prototype =~ /DEFINE_SINGLE_EVENT/)
1900 tracepoint_munge
($file);
1902 dump_function
($prototype, $file);
1907 sub process_proto_type
($$) {
1911 $x =~ s@
[\r\n]+@
@gos; # strip newlines/cr's.
1912 $x =~ s@
^\s
+@
@gos; # strip leading spaces
1913 $x =~ s@\s
+$@
@gos; # strip trailing spaces
1914 $x =~ s@\
/\/.*$@
@gos; # strip C99-style comments to end of line
1917 # To distinguish preprocessor directive from regular declaration later.
1922 if ( $x =~ /([^\{\};]*)([\{\};])(.*)/ ) {
1923 if( length $prototype ) {
1926 $prototype .= $1 . $2;
1927 ($2 eq '{') && $brcount++;
1928 ($2 eq '}') && $brcount--;
1929 if (($2 eq ';') && ($brcount == 0)) {
1930 dump_declaration
($prototype, $file);
1943 sub map_filename
($) {
1945 my ($orig_file) = @_;
1947 if (defined($ENV{'SRCTREE'})) {
1948 $file = "$ENV{'SRCTREE'}" . "/" . $orig_file;
1953 if (defined($source_map{$file})) {
1954 $file = $source_map{$file};
1960 sub process_export_file
($) {
1961 my ($orig_file) = @_;
1962 my $file = map_filename
($orig_file);
1964 if (!open(IN
,"<$file")) {
1965 print STDERR
"Error: Cannot open file $file\n";
1971 if (/$export_symbol/) {
1972 next if (defined($nosymbol_table{$2}));
1973 $function_table{$2} = 1;
1981 # Parsers for the various processing states.
1983 # STATE_NORMAL: looking for the /** to begin everything.
1985 sub process_normal
() {
1986 if (/$doc_start/o) {
1987 $state = STATE_NAME
; # next line is always the function name
1989 $declaration_start_line = $. + 1;
1994 # STATE_NAME: Looking for the "name - description" line
1996 sub process_name
($$) {
2001 if (/$doc_block/o) {
2002 $state = STATE_DOCBLOCK
;
2004 $new_start_line = $. + 1;
2007 $section = $section_intro;
2012 elsif (/$doc_decl/o) {
2014 if (/\s*([\w\s]+?)(\s*-|:)/) {
2018 $state = STATE_BODY
;
2019 # if there's no @param blocks need to set up default section
2022 $section = $section_default;
2023 $new_start_line = $. + 1;
2025 # strip leading/trailing/multiple spaces
2029 $descr =~ s/\s+/ /g;
2030 $declaration_purpose = $descr;
2031 $state = STATE_BODY_MAYBE
;
2033 $declaration_purpose = "";
2036 if (($declaration_purpose eq "") && $verbose) {
2037 print STDERR
"${file}:$.: warning: missing initial short description on line:\n";
2042 if ($identifier =~ m/^struct\b/) {
2043 $decl_type = 'struct';
2044 } elsif ($identifier =~ m/^union\b/) {
2045 $decl_type = 'union';
2046 } elsif ($identifier =~ m/^enum\b/) {
2047 $decl_type = 'enum';
2048 } elsif ($identifier =~ m/^typedef\b/) {
2049 $decl_type = 'typedef';
2051 $decl_type = 'function';
2055 print STDERR
"${file}:$.: info: Scanning doc for $identifier\n";
2058 print STDERR
"${file}:$.: warning: Cannot understand $_ on line $.",
2059 " - I thought it was a doc line\n";
2061 $state = STATE_NORMAL
;
2067 # STATE_BODY and STATE_BODY_MAYBE: the bulk of a kerneldoc comment.
2069 sub process_body
($$) {
2072 # Until all named variable macro parameters are
2073 # documented using the bare name (`x`) rather than with
2074 # dots (`x...`), strip the dots:
2075 if ($section =~ /\w\.\.\.$/) {
2076 $section =~ s/\.\.\.$//;
2079 print STDERR
"${file}:$.: warning: 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;
2090 if (/$doc_sect/i) { # case insensitive for supported section names
2094 # map the supported section names to the canonical names
2095 if ($newsection =~ m/^description$/i) {
2096 $newsection = $section_default;
2097 } elsif ($newsection =~ m/^context$/i) {
2098 $newsection = $section_context;
2099 } elsif ($newsection =~ m/^returns?$/i) {
2100 $newsection = $section_return;
2101 } elsif ($newsection =~ m/^\@return$/) {
2102 # special: @return is a section, not a param description
2103 $newsection = $section_return;
2106 if (($contents ne "") && ($contents ne "\n")) {
2107 if (!$in_doc_sect && $verbose) {
2108 print STDERR
"${file}:$.: warning: 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 print STDERR
"${file}:$.: warning: suspicious ending line: $_";
2140 $state = STATE_PROTO
;
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 print STDERR
"${file}:$.: warning: bad line: $_";
2186 # STATE_PROTO: reading a function/whatever prototype.
2188 sub process_proto
($$) {
2191 if (/$doc_inline_oneline/) {
2194 if ($contents ne "") {
2196 dump_section
($file, $section, $contents);
2197 $section = $section_default;
2200 } elsif (/$doc_inline_start/) {
2201 $state = STATE_INLINE
;
2202 $inline_doc_state = STATE_INLINE_NAME
;
2203 } elsif ($decl_type eq 'function') {
2204 process_proto_function
($_, $file);
2206 process_proto_type
($_, $file);
2211 # STATE_DOCBLOCK: within a DOC: block.
2213 sub process_docblock
($$) {
2217 dump_doc_section
($file, $section, $contents);
2218 $section = $section_default;
2221 %parameterdescs = ();
2222 %parametertypes = ();
2223 @parameterlist = ();
2227 $state = STATE_NORMAL
;
2228 } elsif (/$doc_content/) {
2230 $contents .= $blankline;
2232 $contents .= $1 . "\n";
2238 # STATE_INLINE: docbook comments within a prototype.
2240 sub process_inline
($$) {
2243 # First line (state 1) needs to be a @parameter
2244 if ($inline_doc_state == STATE_INLINE_NAME
&& /$doc_inline_sect/o) {
2247 $new_start_line = $.;
2248 if ($contents ne "") {
2249 while (substr($contents, 0, 1) eq " ") {
2250 $contents = substr($contents, 1);
2254 $inline_doc_state = STATE_INLINE_TEXT
;
2255 # Documentation block end */
2256 } elsif (/$doc_inline_end/) {
2257 if (($contents ne "") && ($contents ne "\n")) {
2258 dump_section
($file, $section, $contents);
2259 $section = $section_default;
2262 $state = STATE_PROTO
;
2263 $inline_doc_state = STATE_INLINE_NA
;
2265 } elsif (/$doc_content/) {
2266 if ($inline_doc_state == STATE_INLINE_TEXT
) {
2267 $contents .= $1 . "\n";
2268 # nuke leading blank lines
2269 if ($contents =~ /^\s*$/) {
2272 } elsif ($inline_doc_state == STATE_INLINE_NAME
) {
2273 $inline_doc_state = STATE_INLINE_ERROR
;
2274 print STDERR
"${file}:$.: warning: ";
2275 print STDERR
"Incorrect use of kernel-doc format: $_";
2282 sub process_file
($) {
2284 my $initial_section_counter = $section_counter;
2285 my ($orig_file) = @_;
2287 $file = map_filename
($orig_file);
2289 if (!open(IN_FILE
,"<$file")) {
2290 print STDERR
"Error: Cannot open file $file\n";
2297 $section_counter = 0;
2299 while (s/\\\s*$//) {
2302 # Replace tabs by spaces
2303 while ($_ =~ s/\t+/' ' x (length($&) * 8 - length($`) % 8)/e) {};
2304 # Hand this line to the appropriate state handler
2305 if ($state == STATE_NORMAL
) {
2307 } elsif ($state == STATE_NAME
) {
2308 process_name
($file, $_);
2309 } elsif ($state == STATE_BODY
|| $state == STATE_BODY_MAYBE
||
2310 $state == STATE_BODY_WITH_BLANK_LINE
) {
2311 process_body
($file, $_);
2312 } elsif ($state == STATE_INLINE
) { # scanning for inline parameters
2313 process_inline
($file, $_);
2314 } elsif ($state == STATE_PROTO
) {
2315 process_proto
($file, $_);
2316 } elsif ($state == STATE_DOCBLOCK
) {
2317 process_docblock
($file, $_);
2321 # Make sure we got something interesting.
2322 if ($initial_section_counter == $section_counter && $
2323 output_mode
ne "none") {
2324 if ($output_selection == OUTPUT_INCLUDE
) {
2325 print STDERR
"${file}:1: warning: '$_' not found\n"
2326 for keys %function_table;
2329 print STDERR
"${file}:1: warning: no structured comments found\n";
2336 $sphinx_major = get_sphinx_version
();
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)