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 -nofunction NAME Do NOT output documentation for the given function(s);
70 only output documentation for the other functions and
71 DOC: sections. May be specified multiple times.
73 Output selection modifiers:
74 -sphinx-version VER Generate rST syntax for the specified Sphinx version.
75 Only works with reStructuredTextFormat.
76 -no-doc-sections Do not output DOC: sections.
77 -enable-lineno Enable output of #define LINENO lines. Only works with
78 reStructuredText format.
79 -export-file FILE Specify an additional FILE in which to look for
80 EXPORT_SYMBOL() and EXPORT_SYMBOL_GPL(). To be used with
81 -export or -internal. May be specified multiple times.
84 -v Verbose output, more warnings and other information.
94 # In the following table, (...)? signifies optional structure.
95 # (...)* signifies 0 or more structure elements
97 # * function_name(:)? (- short description)?
98 # (* @parameterx: (description of parameter x)?)*
100 # * (Description:)? (Description of function)?
101 # * (section header: (section description)? )*
104 # So .. the trivial example would be:
110 # If the Description: header tag is omitted, then there must be a blank line
111 # after the last parameter specification.
114 # * my_function - does my stuff
115 # * @my_arg: its mine damnit
117 # * Does my stuff explained.
120 # or, could also use:
122 # * my_function - does my stuff
123 # * @my_arg: its mine damnit
124 # * Description: Does my stuff explained.
128 # Besides functions you can also write documentation for structs, unions,
129 # enums and typedefs. Instead of the function name you must write the name
130 # of the declaration; the struct/union/enum/typedef must always precede
131 # the name. Nesting of declarations is not supported.
132 # Use the argument mechanism to document members or constants.
135 # * struct my_struct - short description
137 # * @b: second member
139 # * Longer description
148 # All descriptions can be multiline, except the short function description.
150 # For really longs structs, you can also describe arguments inside the
151 # body of the struct.
154 # * struct my_struct - short description
156 # * @b: second member
158 # * Longer description
164 # * @c: This is longer description of C
166 # * You can use paragraphs to describe arguments
167 # * using this method.
172 # This should be use only for struct/enum members.
174 # You can also add additional sections. When documenting kernel functions you
175 # should document the "Context:" of the function, e.g. whether the functions
176 # can be called form interrupts. Unlike other sections you can end it with an
178 # A non-void function should have a "Return:" section describing the return
180 # Example-sections should contain the string EXAMPLE so that they are marked
181 # appropriately in DocBook.
185 # * user_function - function that can only be called in user context
186 # * @a: some argument
187 # * Context: !in_interrupt()
191 # * user_function(22);
196 # All descriptive text is further processed, scanning for the following special
197 # patterns, which are highlighted appropriately.
199 # 'funcname()' - function
200 # '$ENVVAR' - environmental variable
201 # '&struct_name' - name of a structure (up to two words including 'struct')
202 # '&struct_name.member' - name of a structure member
203 # '@parameter' - name of a parameter
204 # '%CONST' - name of a constant.
205 # '``LITERAL``' - literal string without any spaces on it.
211 my $anon_struct_union = 0;
213 # match expressions used to find embedded type information
214 my $type_constant = '\b``([^\`]+)``\b';
215 my $type_constant2 = '\%([-_\w]+)';
216 my $type_func = '(\w+)\(\)';
217 my $type_param = '\@(\w*((\.\w+)|(->\w+))*(\.\.\.)?)';
218 my $type_fp_param = '\@(\w+)\(\)'; # Special RST handling for 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_member, "\\\\fI\$1\$2\$3\\\\fP"],
242 [$type_fallback, "\\\\fI\$1\\\\fP"]
244 my $blankline_man = "";
247 my @highlights_rst = (
248 [$type_constant, "``\$1``"],
249 [$type_constant2, "``\$1``"],
250 # Note: need to escape () to avoid func matching later
251 [$type_member_func, "\\:c\\:type\\:`\$1\$2\$3\\\\(\\\\) <\$1>`"],
252 [$type_member, "\\:c\\:type\\:`\$1\$2\$3 <\$1>`"],
253 [$type_fp_param, "**\$1\\\\(\\\\)**"],
254 [$type_func, "\$1()"],
255 [$type_enum, "\\:c\\:type\\:`\$1 <\$2>`"],
256 [$type_struct, "\\:c\\:type\\:`\$1 <\$2>`"],
257 [$type_typedef, "\\:c\\:type\\:`\$1 <\$2>`"],
258 [$type_union, "\\:c\\:type\\:`\$1 <\$2>`"],
259 # in rst this can refer to any type
260 [$type_fallback, "\\:c\\:type\\:`\$1`"],
261 [$type_param, "**\$1**"]
263 my $blankline_rst = "\n";
271 my $dohighlight = "";
274 my $output_mode = "rst";
275 my $output_preformatted = 0;
276 my $no_doc_sections = 0;
277 my $enable_lineno = 0;
278 my @highlights = @highlights_rst;
279 my $blankline = $blankline_rst;
280 my $modulename = "Kernel API";
283 OUTPUT_ALL
=> 0, # output all symbols and doc sections
284 OUTPUT_INCLUDE
=> 1, # output only specified symbols
285 OUTPUT_EXCLUDE
=> 2, # output everything except specified symbols
286 OUTPUT_EXPORTED
=> 3, # output exported symbols
287 OUTPUT_INTERNAL
=> 4, # output non-exported symbols
289 my $output_selection = OUTPUT_ALL
;
290 my $show_not_found = 0; # No longer used
291 my $sphinx_version = "0.0"; # if not specified, assume old
293 my @export_file_list;
296 if (defined($ENV{'KBUILD_BUILD_TIMESTAMP'}) &&
297 (my $seconds = `date -d"${ENV{'KBUILD_BUILD_TIMESTAMP'}}" +%s`) ne '') {
298 @build_time = gmtime($seconds);
300 @build_time = localtime;
303 my $man_date = ('January', 'February', 'March', 'April', 'May', 'June',
304 'July', 'August', 'September', 'October',
305 'November', 'December')[$build_time[4]] .
306 " " . ($build_time[5]+1900);
308 # Essentially these are globals.
309 # They probably want to be tidied up, made more localised or something.
310 # CAVEAT EMPTOR! Some of the others I localised may not want to be, which
311 # could cause "use of undefined value" or other bugs.
312 my ($function, %function_table, %parametertypes, $declaration_purpose);
313 my $declaration_start_line;
314 my ($type, $declaration_name, $return_type);
315 my ($newsection, $newcontents, $prototype, $brcount, %source_map);
317 if (defined($ENV{'KBUILD_VERBOSE'})) {
318 $verbose = "$ENV{'KBUILD_VERBOSE'}";
321 # Generated docbook code is inserted in a template at a point where
322 # docbook v3.1 requires a non-zero sequence of RefEntry's; see:
323 # http://www.oasis-open.org/docbook/documentation/reference/html/refentry.html
324 # We keep track of number of generated entries and generate a dummy
325 # if needs be to ensure the expanded template can be postprocessed
327 my $section_counter = 0;
333 STATE_NORMAL
=> 0, # normal code
334 STATE_NAME
=> 1, # looking for function name
335 STATE_BODY_MAYBE
=> 2, # body - or maybe more description
336 STATE_BODY
=> 3, # the body of the comment
337 STATE_PROTO
=> 4, # scanning prototype
338 STATE_DOCBLOCK
=> 5, # documentation block
339 STATE_INLINE
=> 6, # gathering documentation outside main block
345 # Inline documentation state
347 STATE_INLINE_NA
=> 0, # not applicable ($state != STATE_INLINE)
348 STATE_INLINE_NAME
=> 1, # looking for member name (@foo:)
349 STATE_INLINE_TEXT
=> 2, # looking for member documentation
350 STATE_INLINE_END
=> 3, # done
351 STATE_INLINE_ERROR
=> 4, # error - Comment without header was found.
352 # Spit a warning as it's not
353 # proper kernel-doc and ignore the rest.
355 my $inline_doc_state;
357 #declaration types: can be
358 # 'function', 'struct', 'union', 'enum', 'typedef'
361 my $doc_start = '^/\*\*\s*$'; # Allow whitespace at end of comment start.
363 my $doc_com = '\s*\*\s*';
364 my $doc_com_body = '\s*\* ?';
365 my $doc_decl = $doc_com . '(\w+)';
366 # @params and a strictly limited set of supported section names
367 my $doc_sect = $doc_com .
368 '\s*(\@[.\w]+|\@\.\.\.|description|context|returns?|notes?|examples?)\s*:(.*)';
369 my $doc_content = $doc_com_body . '(.*)';
370 my $doc_block = $doc_com . 'DOC:\s*(.*)?';
371 my $doc_inline_start = '^\s*/\*\*\s*$';
372 my $doc_inline_sect = '\s*\*\s*(@\s*[\w][\w\.]*\s*):(.*)';
373 my $doc_inline_end = '^\s*\*/\s*$';
374 my $doc_inline_oneline = '^\s*/\*\*\s*(@[\w\s]+):\s*(.*)\s*\*/\s*$';
375 my $export_symbol = '^\s*EXPORT_SYMBOL(_GPL)?\s*\(\s*(\w+)\s*\)\s*;';
378 my %parameterdesc_start_lines;
382 my %section_start_lines;
387 my $new_start_line = 0;
389 # the canonical section names. see also $doc_sect above.
390 my $section_default = "Description"; # default section
391 my $section_intro = "Introduction";
392 my $section = $section_default;
393 my $section_context = "Context";
394 my $section_return = "Return";
396 my $undescribed = "-- undescribed --";
400 while ($ARGV[0] =~ m/^--?(.*)/) {
404 $output_mode = "man";
405 @highlights = @highlights_man;
406 $blankline = $blankline_man;
407 } elsif ($cmd eq "rst") {
408 $output_mode = "rst";
409 @highlights = @highlights_rst;
410 $blankline = $blankline_rst;
411 } elsif ($cmd eq "none") {
412 $output_mode = "none";
413 } elsif ($cmd eq "module") { # not needed for XML, inherits from calling document
414 $modulename = shift @ARGV;
415 } elsif ($cmd eq "function") { # to only output specific functions
416 $output_selection = OUTPUT_INCLUDE
;
417 $function = shift @ARGV;
418 $function_table{$function} = 1;
419 } elsif ($cmd eq "nofunction") { # output all except specific functions
420 $output_selection = OUTPUT_EXCLUDE
;
421 $function = shift @ARGV;
422 $function_table{$function} = 1;
423 } elsif ($cmd eq "export") { # only exported symbols
424 $output_selection = OUTPUT_EXPORTED
;
425 %function_table = ();
426 } elsif ($cmd eq "internal") { # only non-exported symbols
427 $output_selection = OUTPUT_INTERNAL
;
428 %function_table = ();
429 } elsif ($cmd eq "export-file") {
430 my $file = shift @ARGV;
431 push(@export_file_list, $file);
432 } elsif ($cmd eq "v") {
434 } elsif (($cmd eq "h") || ($cmd eq "help")) {
436 } elsif ($cmd eq 'no-doc-sections') {
437 $no_doc_sections = 1;
438 } elsif ($cmd eq 'enable-lineno') {
440 } elsif ($cmd eq 'show-not-found') {
441 $show_not_found = 1; # A no-op but don't fail
442 } elsif ($cmd eq 'sphinx-version') {
443 $sphinx_version = shift @ARGV;
450 # continue execution near EOF;
452 # get kernel version from env
453 sub get_kernel_version
() {
454 my $version = 'unknown kernel version';
456 if (defined($ENV{'KERNELVERSION'})) {
457 $version = $ENV{'KERNELVERSION'};
465 if ($enable_lineno && defined($lineno)) {
466 print "#define LINENO " . $lineno . "\n";
470 # dumps section contents to arrays/hashes intended for that purpose.
475 my $contents = join "\n", @_;
477 if ($name =~ m/$type_param/) {
479 $parameterdescs{$name} = $contents;
480 $sectcheck = $sectcheck . $name . " ";
481 $parameterdesc_start_lines{$name} = $new_start_line;
483 } elsif ($name eq "@\.\.\.") {
485 $parameterdescs{$name} = $contents;
486 $sectcheck = $sectcheck . $name . " ";
487 $parameterdesc_start_lines{$name} = $new_start_line;
490 if (defined($sections{$name}) && ($sections{$name} ne "")) {
491 # Only warn on user specified duplicate section names.
492 if ($name ne $section_default) {
493 print STDERR
"${file}:$.: warning: duplicate section name '$name'\n";
496 $sections{$name} .= $contents;
498 $sections{$name} = $contents;
499 push @sectionlist, $name;
500 $section_start_lines{$name} = $new_start_line;
507 # dump DOC: section after checking that it should go out
509 sub dump_doc_section
{
512 my $contents = join "\n", @_;
514 if ($no_doc_sections) {
518 if (($output_selection == OUTPUT_ALL
) ||
519 ($output_selection == OUTPUT_INCLUDE
&&
520 defined($function_table{$name})) ||
521 ($output_selection == OUTPUT_EXCLUDE
&&
522 !defined($function_table{$name})))
524 dump_section
($file, $name, $contents);
525 output_blockhead
({'sectionlist' => \
@sectionlist,
526 'sections' => \
%sections,
527 'module' => $modulename,
528 'content-only' => ($output_selection != OUTPUT_ALL
), });
535 # parameterdescs, a hash.
536 # function => "function name"
537 # parameterlist => @list of parameters
538 # parameterdescs => %parameter descriptions
539 # sectionlist => @list of sections
540 # sections => %section descriptions
543 sub output_highlight
{
544 my $contents = join "\n",@_;
548 # if (!defined $contents) {
550 # confess "output_highlight got called with no args?\n";
553 # print STDERR "contents b4:$contents\n";
556 # print STDERR "contents af:$contents\n";
558 foreach $line (split "\n", $contents) {
559 if (! $output_preformatted) {
563 if (! $output_preformatted) {
564 print $lineprefix, $blankline;
567 if ($output_mode eq "man" && substr($line, 0, 1) eq ".") {
570 print $lineprefix, $line;
578 # output function in man
579 sub output_function_man
(%) {
581 my ($parameter, $section);
584 print ".TH \"$args{'function'}\" 9 \"$args{'function'}\" \"$man_date\" \"Kernel Hacker's Manual\" LINUX\n";
587 print $args{'function'} . " \\- " . $args{'purpose'} . "\n";
589 print ".SH SYNOPSIS\n";
590 if ($args{'functiontype'} ne "") {
591 print ".B \"" . $args{'functiontype'} . "\" " . $args{'function'} . "\n";
593 print ".B \"" . $args{'function'} . "\n";
598 foreach my $parameter (@
{$args{'parameterlist'}}) {
599 if ($count == $#{$args{'parameterlist'}}) {
602 $type = $args{'parametertypes'}{$parameter};
603 if ($type =~ m/([^\(]*\(\*)\s*\)\s*\(([^\)]*)\)/) {
604 # pointer-to-function
605 print ".BI \"" . $parenth . $1 . "\" " . $parameter . " \") (" . $2 . ")" . $post . "\"\n";
607 $type =~ s/([^\*])$/$1 /;
608 print ".BI \"" . $parenth . $type . "\" " . $parameter . " \"" . $post . "\"\n";
614 print ".SH ARGUMENTS\n";
615 foreach $parameter (@
{$args{'parameterlist'}}) {
616 my $parameter_name = $parameter;
617 $parameter_name =~ s/\[.*//;
619 print ".IP \"" . $parameter . "\" 12\n";
620 output_highlight
($args{'parameterdescs'}{$parameter_name});
622 foreach $section (@
{$args{'sectionlist'}}) {
623 print ".SH \"", uc $section, "\"\n";
624 output_highlight
($args{'sections'}{$section});
630 sub output_enum_man
(%) {
632 my ($parameter, $section);
635 print ".TH \"$args{'module'}\" 9 \"enum $args{'enum'}\" \"$man_date\" \"API Manual\" LINUX\n";
638 print "enum " . $args{'enum'} . " \\- " . $args{'purpose'} . "\n";
640 print ".SH SYNOPSIS\n";
641 print "enum " . $args{'enum'} . " {\n";
643 foreach my $parameter (@
{$args{'parameterlist'}}) {
644 print ".br\n.BI \" $parameter\"\n";
645 if ($count == $#{$args{'parameterlist'}}) {
655 print ".SH Constants\n";
656 foreach $parameter (@
{$args{'parameterlist'}}) {
657 my $parameter_name = $parameter;
658 $parameter_name =~ s/\[.*//;
660 print ".IP \"" . $parameter . "\" 12\n";
661 output_highlight
($args{'parameterdescs'}{$parameter_name});
663 foreach $section (@
{$args{'sectionlist'}}) {
664 print ".SH \"$section\"\n";
665 output_highlight
($args{'sections'}{$section});
670 # output struct in man
671 sub output_struct_man
(%) {
673 my ($parameter, $section);
675 print ".TH \"$args{'module'}\" 9 \"" . $args{'type'} . " " . $args{'struct'} . "\" \"$man_date\" \"API Manual\" LINUX\n";
678 print $args{'type'} . " " . $args{'struct'} . " \\- " . $args{'purpose'} . "\n";
680 my $declaration = $args{'definition'};
681 $declaration =~ s/\t/ /g;
682 $declaration =~ s/\n/"\n.br\n.BI \"/g;
683 print ".SH SYNOPSIS\n";
684 print $args{'type'} . " " . $args{'struct'} . " {\n.br\n";
685 print ".BI \"$declaration\n};\n.br\n\n";
687 print ".SH Members\n";
688 foreach $parameter (@
{$args{'parameterlist'}}) {
689 ($parameter =~ /^#/) && next;
691 my $parameter_name = $parameter;
692 $parameter_name =~ s/\[.*//;
694 ($args{'parameterdescs'}{$parameter_name} ne $undescribed) || next;
695 print ".IP \"" . $parameter . "\" 12\n";
696 output_highlight
($args{'parameterdescs'}{$parameter_name});
698 foreach $section (@
{$args{'sectionlist'}}) {
699 print ".SH \"$section\"\n";
700 output_highlight
($args{'sections'}{$section});
705 # output typedef in man
706 sub output_typedef_man
(%) {
708 my ($parameter, $section);
710 print ".TH \"$args{'module'}\" 9 \"$args{'typedef'}\" \"$man_date\" \"API Manual\" LINUX\n";
713 print "typedef " . $args{'typedef'} . " \\- " . $args{'purpose'} . "\n";
715 foreach $section (@
{$args{'sectionlist'}}) {
716 print ".SH \"$section\"\n";
717 output_highlight
($args{'sections'}{$section});
721 sub output_blockhead_man
(%) {
723 my ($parameter, $section);
726 print ".TH \"$args{'module'}\" 9 \"$args{'module'}\" \"$man_date\" \"API Manual\" LINUX\n";
728 foreach $section (@
{$args{'sectionlist'}}) {
729 print ".SH \"$section\"\n";
730 output_highlight
($args{'sections'}{$section});
735 # output in restructured text
739 # This could use some work; it's used to output the DOC: sections, and
740 # starts by putting out the name of the doc section itself, but that tends
741 # to duplicate a header already in the template file.
743 sub output_blockhead_rst
(%) {
745 my ($parameter, $section);
747 foreach $section (@
{$args{'sectionlist'}}) {
748 if ($output_selection != OUTPUT_INCLUDE
) {
749 print "**$section**\n\n";
751 print_lineno
($section_start_lines{$section});
752 output_highlight_rst
($args{'sections'}{$section});
758 # Apply the RST highlights to a sub-block of text.
760 sub highlight_block
($) {
761 # The dohighlight kludge requires the text be called $contents
762 my $contents = shift;
769 # Regexes used only here.
771 my $sphinx_literal = '^[^.].*::$';
772 my $sphinx_cblock = '^\.\.\ +code-block::';
774 sub output_highlight_rst
{
775 my $input = join "\n",@_;
782 foreach $line (split "\n",$input) {
784 # If we're in a literal block, see if we should drop out
785 # of it. Otherwise pass the line straight through unmunged.
788 if (! ($line =~ /^\s*$/)) {
790 # If this is the first non-blank line in a literal
791 # block we need to figure out what the proper indent is.
793 if ($litprefix eq "") {
795 $litprefix = '^' . $1;
796 $output .= $line . "\n";
797 } elsif (! ($line =~ /$litprefix/)) {
800 $output .= $line . "\n";
803 $output .= $line . "\n";
807 # Not in a literal block (or just dropped out)
810 $block .= $line . "\n";
811 if (($line =~ /$sphinx_literal/) || ($line =~ /$sphinx_cblock/)) {
814 $output .= highlight_block
($block);
821 $output .= highlight_block
($block);
823 foreach $line (split "\n", $output) {
824 print $lineprefix . $line . "\n";
828 sub output_function_rst
(%) {
830 my ($parameter, $section);
831 my $oldprefix = $lineprefix;
834 if ($args{'typedef'}) {
835 print ".. c:type:: ". $args{'function'} . "\n\n";
836 print_lineno
($declaration_start_line);
837 print " **Typedef**: ";
839 output_highlight_rst
($args{'purpose'});
840 $start = "\n\n**Syntax**\n\n ``";
842 print ".. c:function:: ";
844 if ($args{'functiontype'} ne "") {
845 $start .= $args{'functiontype'} . " " . $args{'function'} . " (";
847 $start .= $args{'function'} . " (";
852 foreach my $parameter (@
{$args{'parameterlist'}}) {
857 $type = $args{'parametertypes'}{$parameter};
859 if ($type =~ m/([^\(]*\(\*)\s*\)\s*\(([^\)]*)\)/) {
860 # pointer-to-function
861 print $1 . $parameter . ") (" . $2 . ")";
863 print $type . " " . $parameter;
866 if ($args{'typedef'}) {
870 print_lineno
($declaration_start_line);
872 output_highlight_rst
($args{'purpose'});
876 print "**Parameters**\n\n";
878 foreach $parameter (@
{$args{'parameterlist'}}) {
879 my $parameter_name = $parameter;
880 $parameter_name =~ s/\[.*//;
881 $type = $args{'parametertypes'}{$parameter};
884 print "``$type $parameter``\n";
886 print "``$parameter``\n";
889 print_lineno
($parameterdesc_start_lines{$parameter_name});
891 if (defined($args{'parameterdescs'}{$parameter_name}) &&
892 $args{'parameterdescs'}{$parameter_name} ne $undescribed) {
893 output_highlight_rst
($args{'parameterdescs'}{$parameter_name});
895 print " *undescribed*\n";
900 $lineprefix = $oldprefix;
901 output_section_rst
(@_);
904 sub output_section_rst
(%) {
907 my $oldprefix = $lineprefix;
910 foreach $section (@
{$args{'sectionlist'}}) {
911 print "**$section**\n\n";
912 print_lineno
($section_start_lines{$section});
913 output_highlight_rst
($args{'sections'}{$section});
917 $lineprefix = $oldprefix;
920 sub output_enum_rst
(%) {
923 my $oldprefix = $lineprefix;
925 my $name = "enum " . $args{'enum'};
927 print "\n\n.. c:type:: " . $name . "\n\n";
928 print_lineno
($declaration_start_line);
930 output_highlight_rst
($args{'purpose'});
933 print "**Constants**\n\n";
935 foreach $parameter (@
{$args{'parameterlist'}}) {
936 print "``$parameter``\n";
937 if ($args{'parameterdescs'}{$parameter} ne $undescribed) {
938 output_highlight_rst
($args{'parameterdescs'}{$parameter});
940 print " *undescribed*\n";
945 $lineprefix = $oldprefix;
946 output_section_rst
(@_);
949 sub output_typedef_rst
(%) {
952 my $oldprefix = $lineprefix;
953 my $name = "typedef " . $args{'typedef'};
955 print "\n\n.. c:type:: " . $name . "\n\n";
956 print_lineno
($declaration_start_line);
958 output_highlight_rst
($args{'purpose'});
961 $lineprefix = $oldprefix;
962 output_section_rst
(@_);
965 sub output_struct_rst
(%) {
968 my $oldprefix = $lineprefix;
969 my $name = $args{'type'} . " " . $args{'struct'};
971 # Sphinx 3.0 and up will emit warnings for "c:type:: struct Foo".
972 # It wants to see "c:struct:: Foo" (and will add the word 'struct' in
973 # the rendered output).
974 if ((split(/\./, $sphinx_version))[0] >= 3) {
976 $sname =~ s/^struct //;
977 print "\n\n.. c:struct:: " . $sname . "\n\n";
979 print "\n\n.. c:type:: " . $name . "\n\n";
981 print_lineno
($declaration_start_line);
983 output_highlight_rst
($args{'purpose'});
986 print "**Definition**\n\n";
988 my $declaration = $args{'definition'};
989 $declaration =~ s/\t/ /g;
990 print " " . $args{'type'} . " " . $args{'struct'} . " {\n$declaration };\n\n";
992 print "**Members**\n\n";
994 foreach $parameter (@
{$args{'parameterlist'}}) {
995 ($parameter =~ /^#/) && next;
997 my $parameter_name = $parameter;
998 $parameter_name =~ s/\[.*//;
1000 ($args{'parameterdescs'}{$parameter_name} ne $undescribed) || next;
1001 $type = $args{'parametertypes'}{$parameter};
1002 print_lineno
($parameterdesc_start_lines{$parameter_name});
1003 print "``" . $parameter . "``\n";
1004 output_highlight_rst
($args{'parameterdescs'}{$parameter_name});
1009 $lineprefix = $oldprefix;
1010 output_section_rst
(@_);
1013 ## none mode output functions
1015 sub output_function_none
(%) {
1018 sub output_enum_none
(%) {
1021 sub output_typedef_none
(%) {
1024 sub output_struct_none
(%) {
1027 sub output_blockhead_none
(%) {
1031 # generic output function for all types (function, struct/union, typedef, enum);
1032 # calls the generated, variable output_ function name based on
1033 # functype and output_mode
1034 sub output_declaration
{
1037 my $functype = shift;
1038 my $func = "output_${functype}_$output_mode";
1039 if (($output_selection == OUTPUT_ALL
) ||
1040 (($output_selection == OUTPUT_INCLUDE
||
1041 $output_selection == OUTPUT_EXPORTED
) &&
1042 defined($function_table{$name})) ||
1043 (($output_selection == OUTPUT_EXCLUDE
||
1044 $output_selection == OUTPUT_INTERNAL
) &&
1045 !($functype eq "function" && defined($function_table{$name}))))
1053 # generic output function - calls the right one based on current output mode.
1054 sub output_blockhead
{
1056 my $func = "output_blockhead_" . $output_mode;
1062 # takes a declaration (struct, union, enum, typedef) and
1063 # invokes the right handler. NOT called for functions.
1064 sub dump_declaration
($$) {
1066 my ($prototype, $file) = @_;
1067 if ($decl_type eq 'type name') {
1068 if ($prototype =~ /^(enum|struct|union)\s+/) {
1075 my $func = "dump_" . $decl_type;
1079 sub dump_union
($$) {
1083 sub dump_struct
($$) {
1087 if ($x =~ /(struct|union)\s+(\w+)\s*\{(.*)\}(\s*(__packed|__aligned|__attribute__\s*\(\([a-z0-9,_\s\(\)]*\)\)))*/) {
1089 $declaration_name = $2;
1092 # ignore members marked private:
1093 $members =~ s/\/\*\s*private:.*?\/\
*\s
*public
:.*?\
*\
///gosi
;
1094 $members =~ s/\/\*\s*private:.*//gosi
;
1096 $members =~ s/\/\*.*?\*\///gos;
1098 $members =~ s/\s*__attribute__\s*\(\([a-z0-9,_\*\s\(\)]*\)\)//gi;
1099 $members =~ s/\s*__aligned\s*\([^;]*\)//gos;
1100 $members =~ s/\s*__packed\s*//gos;
1101 $members =~ s/\s*CRYPTO_MINALIGN_ATTR//gos;
1102 # replace DECLARE_BITMAP
1103 $members =~ s/DECLARE_BITMAP\s*\(([^,)]+),\s*([^,)]+)\)/unsigned long $1\[BITS_TO_LONGS($2)\]/gos;
1104 # replace DECLARE_HASHTABLE
1105 $members =~ s/DECLARE_HASHTABLE\s*\(([^,)]+),\s*([^,)]+)\)/unsigned long $1\[1 << (($2) - 1)\]/gos;
1106 # replace DECLARE_KFIFO
1107 $members =~ s/DECLARE_KFIFO\s*\(([^,)]+),\s*([^,)]+),\s*([^,)]+)\)/$2 \*$1/gos;
1108 # replace DECLARE_KFIFO_PTR
1109 $members =~ s/DECLARE_KFIFO_PTR\s*\(([^,)]+),\s*([^,)]+)\)/$2 \*$1/gos;
1111 my $declaration = $members;
1113 # Split nested struct/union elements as newer ones
1114 while ($members =~ m/(struct|union)([^\{\};]+)\{([^\{\}]*)\}([^\{\}\;]*)\;/) {
1119 foreach my $id(split /,/, $ids) {
1120 $newmember .= "$maintype $id; ";
1123 $id =~ s/^\s*\**(\S+)\s*/$1/;
1124 foreach my $arg (split /;/, $content) {
1125 next if ($arg =~ m/^\s*$/);
1126 if ($arg =~ m/^([^\(]+\(\*?\s*)([\w\.]*)(\s*\).*)/) {
1127 # pointer-to-function
1132 if ($id =~ m/^\s*$/) {
1133 # anonymous struct/union
1134 $newmember .= "$type$name$extra; ";
1136 $newmember .= "$type$id.$name$extra; ";
1144 $arg =~ s/:\s*\d+\s*//g;
1146 $arg =~ s/\[.*\]//g;
1147 # The type may have multiple words,
1148 # and multiple IDs can be defined, like:
1149 # const struct foo, *bar, foobar
1150 # So, we remove spaces when parsing the
1151 # names, in order to match just names
1152 # and commas for the names
1153 $arg =~ s/\s*,\s*/,/g;
1154 if ($arg =~ m/(.*)\s+([\S+,]+)/) {
1158 $newmember .= "$arg; ";
1161 foreach my $name (split /,/, $names) {
1162 $name =~ s/^\s*\**(\S+)\s*/$1/;
1163 next if (($name =~ m/^\s*$/));
1164 if ($id =~ m/^\s*$/) {
1165 # anonymous struct/union
1166 $newmember .= "$type $name; ";
1168 $newmember .= "$type $id.$name; ";
1174 $members =~ s/(struct|union)([^\{\};]+)\{([^\{\}]*)\}([^\{\}\;]*)\;/$newmember/;
1177 # Ignore other nested elements, like enums
1178 $members =~ s/(\{[^\{\}]*\})//g;
1180 create_parameterlist
($members, ';', $file, $declaration_name);
1181 check_sections
($file, $declaration_name, $decl_type, $sectcheck, $struct_actual);
1183 # Adjust declaration for better display
1184 $declaration =~ s/([\{;])/$1\n/g;
1185 $declaration =~ s/\}\s+;/};/g;
1186 # Better handle inlined enums
1187 do {} while ($declaration =~ s/(enum\s+\{[^\}]+),([^\n])/$1,\n$2/);
1189 my @def_args = split /\n/, $declaration;
1192 foreach my $clause (@def_args) {
1193 $clause =~ s/^\s+//;
1194 $clause =~ s/\s+$//;
1195 $clause =~ s/\s+/ /;
1197 $level-- if ($clause =~ m/(\})/ && $level > 1);
1198 if (!($clause =~ m/^\s*#/)) {
1199 $declaration .= "\t" x
$level;
1201 $declaration .= "\t" . $clause . "\n";
1202 $level++ if ($clause =~ m/(\{)/ && !($clause =~m/\}/));
1204 output_declaration
($declaration_name,
1206 {'struct' => $declaration_name,
1207 'module' => $modulename,
1208 'definition' => $declaration,
1209 'parameterlist' => \
@parameterlist,
1210 'parameterdescs' => \
%parameterdescs,
1211 'parametertypes' => \
%parametertypes,
1212 'sectionlist' => \
@sectionlist,
1213 'sections' => \
%sections,
1214 'purpose' => $declaration_purpose,
1215 'type' => $decl_type
1219 print STDERR
"${file}:$.: error: Cannot parse struct or union!\n";
1225 sub show_warnings
($$) {
1226 my $functype = shift;
1229 return 1 if ($output_selection == OUTPUT_ALL
);
1231 if ($output_selection == OUTPUT_EXPORTED
) {
1232 if (defined($function_table{$name})) {
1238 if ($output_selection == OUTPUT_INTERNAL
) {
1239 if (!($functype eq "function" && defined($function_table{$name}))) {
1245 if ($output_selection == OUTPUT_INCLUDE
) {
1246 if (defined($function_table{$name})) {
1252 if ($output_selection == OUTPUT_EXCLUDE
) {
1253 if (!defined($function_table{$name})) {
1259 die("Please add the new output type at show_warnings()");
1266 $x =~ s@
/\*.*?\*/@
@gos; # strip comments.
1267 # strip #define macros inside enums
1268 $x =~ s@
#\s*((define|ifdef)\s+|endif)[^;]*;@@gos;
1270 if ($x =~ /enum\s+(\w*)\s*\{(.*)\}/) {
1271 $declaration_name = $1;
1275 $members =~ s/\s+$//;
1277 foreach my $arg (split ',', $members) {
1278 $arg =~ s/^\s*(\w+).*/$1/;
1279 push @parameterlist, $arg;
1280 if (!$parameterdescs{$arg}) {
1281 $parameterdescs{$arg} = $undescribed;
1282 if (show_warnings
("enum", $declaration_name)) {
1283 print STDERR
"${file}:$.: warning: Enum value '$arg' not described in enum '$declaration_name'\n";
1286 $_members{$arg} = 1;
1289 while (my ($k, $v) = each %parameterdescs) {
1290 if (!exists($_members{$k})) {
1291 if (show_warnings
("enum", $declaration_name)) {
1292 print STDERR
"${file}:$.: warning: Excess enum value '$k' description in '$declaration_name'\n";
1297 output_declaration
($declaration_name,
1299 {'enum' => $declaration_name,
1300 'module' => $modulename,
1301 'parameterlist' => \
@parameterlist,
1302 'parameterdescs' => \
%parameterdescs,
1303 'sectionlist' => \
@sectionlist,
1304 'sections' => \
%sections,
1305 'purpose' => $declaration_purpose
1309 print STDERR
"${file}:$.: error: Cannot parse enum!\n";
1314 sub dump_typedef
($$) {
1318 $x =~ s@
/\*.*?\*/@
@gos; # strip comments.
1320 # Parse function prototypes
1321 if ($x =~ /typedef\s+(\w+)\s*\(\*\s*(\w\S+)\s*\)\s*\((.*)\);/ ||
1322 $x =~ /typedef\s+(\w+)\s*(\w\S+)\s*\s*\((.*)\);/) {
1326 $declaration_name = $2;
1329 create_parameterlist
($args, ',', $file, $declaration_name);
1331 output_declaration
($declaration_name,
1333 {'function' => $declaration_name,
1335 'module' => $modulename,
1336 'functiontype' => $return_type,
1337 'parameterlist' => \
@parameterlist,
1338 'parameterdescs' => \
%parameterdescs,
1339 'parametertypes' => \
%parametertypes,
1340 'sectionlist' => \
@sectionlist,
1341 'sections' => \
%sections,
1342 'purpose' => $declaration_purpose
1347 while (($x =~ /\(*.\)\s*;$/) || ($x =~ /\[*.\]\s*;$/)) {
1348 $x =~ s/\(*.\)\s*;$/;/;
1349 $x =~ s/\[*.\]\s*;$/;/;
1352 if ($x =~ /typedef.*\s+(\w+)\s*;/) {
1353 $declaration_name = $1;
1355 output_declaration
($declaration_name,
1357 {'typedef' => $declaration_name,
1358 'module' => $modulename,
1359 'sectionlist' => \
@sectionlist,
1360 'sections' => \
%sections,
1361 'purpose' => $declaration_purpose
1365 print STDERR
"${file}:$.: error: Cannot parse typedef!\n";
1370 sub save_struct_actual
($) {
1373 # strip all spaces from the actual param so that it looks like one string item
1374 $actual =~ s/\s*//g;
1375 $struct_actual = $struct_actual . $actual . " ";
1378 sub create_parameterlist
($$$$) {
1380 my $splitter = shift;
1382 my $declaration_name = shift;
1386 # temporarily replace commas inside function pointer definition
1387 while ($args =~ /(\([^\),]+),/) {
1388 $args =~ s/(\([^\),]+),/$1#/g;
1391 foreach my $arg (split($splitter, $args)) {
1393 $arg =~ s/\/\*.*\*\///;
1394 # strip leading/trailing spaces
1400 # Treat preprocessor directive as a typeless variable just to fill
1401 # corresponding data structures "correctly". Catch it later in
1403 push_parameter
($arg, "", $file);
1404 } elsif ($arg =~ m/\(.+\)\s*\(/) {
1405 # pointer-to-function
1407 $arg =~ m/[^\(]+\(\*?\s*([\w\.]*)\s*\)/;
1410 $type =~ s/([^\(]+\(\*?)\s*$param/$1/;
1411 save_struct_actual
($param);
1412 push_parameter
($param, $type, $file, $declaration_name);
1414 $arg =~ s/\s*:\s*/:/g;
1415 $arg =~ s/\s*\[/\[/g;
1417 my @args = split('\s*,\s*', $arg);
1418 if ($args[0] =~ m/\*/) {
1419 $args[0] =~ s/(\*+)\s*/ $1/;
1423 if ($args[0] =~ /^(.*\s+)(.*?\[.*\].*)$/) {
1425 push(@first_arg, split('\s+', $1));
1426 push(@first_arg, $2);
1428 @first_arg = split('\s+', shift @args);
1431 unshift(@args, pop @first_arg);
1432 $type = join " ", @first_arg;
1434 foreach $param (@args) {
1435 if ($param =~ m/^(\*+)\s*(.*)/) {
1436 save_struct_actual
($2);
1437 push_parameter
($2, "$type $1", $file, $declaration_name);
1439 elsif ($param =~ m/(.*?):(\d+)/) {
1440 if ($type ne "") { # skip unnamed bit-fields
1441 save_struct_actual
($1);
1442 push_parameter
($1, "$type:$2", $file, $declaration_name)
1446 save_struct_actual
($param);
1447 push_parameter
($param, $type, $file, $declaration_name);
1454 sub push_parameter
($$$$) {
1458 my $declaration_name = shift;
1460 if (($anon_struct_union == 1) && ($type eq "") &&
1462 return; # ignore the ending }; from anon. struct/union
1465 $anon_struct_union = 0;
1466 $param =~ s/[\[\)].*//;
1468 if ($type eq "" && $param =~ /\.\.\.$/)
1470 if (!$param =~ /\w\.\.\.$/) {
1471 # handles unnamed variable parameters
1474 if (!defined $parameterdescs{$param} || $parameterdescs{$param} eq "") {
1475 $parameterdescs{$param} = "variable arguments";
1478 elsif ($type eq "" && ($param eq "" or $param eq "void"))
1481 $parameterdescs{void
} = "no arguments";
1483 elsif ($type eq "" && ($param eq "struct" or $param eq "union"))
1484 # handle unnamed (anonymous) union or struct:
1487 $param = "{unnamed_" . $param . "}";
1488 $parameterdescs{$param} = "anonymous\n";
1489 $anon_struct_union = 1;
1492 # warn if parameter has no description
1493 # (but ignore ones starting with # as these are not parameters
1494 # but inline preprocessor statements);
1495 # Note: It will also ignore void params and unnamed structs/unions
1496 if (!defined $parameterdescs{$param} && $param !~ /^#/) {
1497 $parameterdescs{$param} = $undescribed;
1499 if (show_warnings
($type, $declaration_name) && $param !~ /\./) {
1501 "${file}:$.: warning: Function parameter or member '$param' not described in '$declaration_name'\n";
1506 # strip spaces from $param so that it is one continuous string
1507 # on @parameterlist;
1508 # this fixes a problem where check_sections() cannot find
1509 # a parameter like "addr[6 + 2]" because it actually appears
1510 # as "addr[6", "+", "2]" on the parameter list;
1511 # but it's better to maintain the param string unchanged for output,
1512 # so just weaken the string compare in check_sections() to ignore
1513 # "[blah" in a parameter string;
1514 ###$param =~ s/\s*//g;
1515 push @parameterlist, $param;
1516 $type =~ s/\s\s+/ /g;
1517 $parametertypes{$param} = $type;
1520 sub check_sections
($$$$$) {
1521 my ($file, $decl_name, $decl_type, $sectcheck, $prmscheck) = @_;
1522 my @sects = split ' ', $sectcheck;
1523 my @prms = split ' ', $prmscheck;
1526 my $prm_clean; # strip trailing "[array size]" and/or beginning "*"
1528 foreach $sx (0 .. $#sects) {
1530 foreach $px (0 .. $#prms) {
1531 $prm_clean = $prms[$px];
1532 $prm_clean =~ s/\[.*\]//;
1533 $prm_clean =~ s/__attribute__\s*\(\([a-z,_\*\s\(\)]*\)\)//i;
1534 # ignore array size in a parameter string;
1535 # however, the original param string may contain
1536 # spaces, e.g.: addr[6 + 2]
1537 # and this appears in @prms as "addr[6" since the
1538 # parameter list is split at spaces;
1539 # hence just ignore "[..." for the sections check;
1540 $prm_clean =~ s/\[.*//;
1542 ##$prm_clean =~ s/^\**//;
1543 if ($prm_clean eq $sects[$sx]) {
1549 if ($decl_type eq "function") {
1550 print STDERR
"${file}:$.: warning: " .
1551 "Excess function parameter " .
1553 "description in '$decl_name'\n";
1561 # Checks the section describing the return value of a function.
1562 sub check_return_section
{
1564 my $declaration_name = shift;
1565 my $return_type = shift;
1567 # Ignore an empty return type (It's a macro)
1568 # Ignore functions with a "void" return type. (But don't ignore "void *")
1569 if (($return_type eq "") || ($return_type =~ /void\s*\w*\s*$/)) {
1573 if (!defined($sections{$section_return}) ||
1574 $sections{$section_return} eq "") {
1575 print STDERR
"${file}:$.: warning: " .
1576 "No description found for return value of " .
1577 "'$declaration_name'\n";
1583 # takes a function prototype and the name of the current file being
1584 # processed and spits out all the details stored in the global
1586 sub dump_function
($$) {
1587 my $prototype = shift;
1591 $prototype =~ s/^static +//;
1592 $prototype =~ s/^extern +//;
1593 $prototype =~ s/^asmlinkage +//;
1594 $prototype =~ s/^inline +//;
1595 $prototype =~ s/^__inline__ +//;
1596 $prototype =~ s/^__inline +//;
1597 $prototype =~ s/^__always_inline +//;
1598 $prototype =~ s/^noinline +//;
1599 $prototype =~ s/__init +//;
1600 $prototype =~ s/__init_or_module +//;
1601 $prototype =~ s/__meminit +//;
1602 $prototype =~ s/__must_check +//;
1603 $prototype =~ s/__weak +//;
1604 $prototype =~ s/__sched +//;
1605 $prototype =~ s/__printf\s*\(\s*\d*\s*,\s*\d*\s*\) +//;
1606 my $define = $prototype =~ s/^#\s*define\s+//; #ak added
1607 $prototype =~ s
/__attribute__\s
*\
(\
(
1609 [\w\s
]++ # attribute name
1610 (?
:\
([^)]*+\
))?
# attribute arguments
1611 \s
*+,?
# optional comma at the end
1615 # Yes, this truly is vile. We are looking for:
1616 # 1. Return type (may be nothing if we're looking at a macro)
1618 # 3. Function parameters.
1620 # All the while we have to watch out for function pointer parameters
1621 # (which IIRC is what the two sections are for), C types (these
1622 # regexps don't even start to express all the possibilities), and
1625 # If you mess with these regexps, it's a good idea to check that
1626 # the following functions' documentation still comes out right:
1627 # - parport_register_device (function pointer parameters)
1628 # - atomic_set (macro)
1629 # - pci_match_device, __copy_to_user (long return type)
1631 if ($define && $prototype =~ m/^()([a-zA-Z0-9_~:]+)\s+/) {
1632 # This is an object-like macro, it has no return type and no parameter
1634 # Function-like macros are not allowed to have spaces between
1635 # declaration_name and opening parenthesis (notice the \s+).
1637 $declaration_name = $2;
1639 } elsif ($prototype =~ m/^()([a-zA-Z0-9_~:]+)\s*\(([^\(]*)\)/ ||
1640 $prototype =~ m/^(\w+)\s+([a-zA-Z0-9_~:]+)\s*\(([^\(]*)\)/ ||
1641 $prototype =~ m/^(\w+\s*\*+)\s*([a-zA-Z0-9_~:]+)\s*\(([^\(]*)\)/ ||
1642 $prototype =~ m/^(\w+\s+\w+)\s+([a-zA-Z0-9_~:]+)\s*\(([^\(]*)\)/ ||
1643 $prototype =~ m/^(\w+\s+\w+\s*\*+)\s*([a-zA-Z0-9_~:]+)\s*\(([^\(]*)\)/ ||
1644 $prototype =~ m/^(\w+\s+\w+\s+\w+)\s+([a-zA-Z0-9_~:]+)\s*\(([^\(]*)\)/ ||
1645 $prototype =~ m/^(\w+\s+\w+\s+\w+\s*\*+)\s*([a-zA-Z0-9_~:]+)\s*\(([^\(]*)\)/ ||
1646 $prototype =~ m/^()([a-zA-Z0-9_~:]+)\s*\(([^\{]*)\)/ ||
1647 $prototype =~ m/^(\w+)\s+([a-zA-Z0-9_~:]+)\s*\(([^\{]*)\)/ ||
1648 $prototype =~ m/^(\w+\s*\*+)\s*([a-zA-Z0-9_~:]+)\s*\(([^\{]*)\)/ ||
1649 $prototype =~ m/^(\w+\s+\w+)\s+([a-zA-Z0-9_~:]+)\s*\(([^\{]*)\)/ ||
1650 $prototype =~ m/^(\w+\s+\w+\s*\*+)\s*([a-zA-Z0-9_~:]+)\s*\(([^\{]*)\)/ ||
1651 $prototype =~ m/^(\w+\s+\w+\s+\w+)\s+([a-zA-Z0-9_~:]+)\s*\(([^\{]*)\)/ ||
1652 $prototype =~ m/^(\w+\s+\w+\s+\w+\s*\*+)\s*([a-zA-Z0-9_~:]+)\s*\(([^\{]*)\)/ ||
1653 $prototype =~ m/^(\w+\s+\w+\s+\w+\s+\w+)\s+([a-zA-Z0-9_~:]+)\s*\(([^\{]*)\)/ ||
1654 $prototype =~ m/^(\w+\s+\w+\s+\w+\s+\w+\s*\*+)\s*([a-zA-Z0-9_~:]+)\s*\(([^\{]*)\)/ ||
1655 $prototype =~ m/^(\w+\s+\w+\s*\*+\s*\w+\s*\*+\s*)\s*([a-zA-Z0-9_~:]+)\s*\(([^\{]*)\)/) {
1657 $declaration_name = $2;
1660 create_parameterlist
($args, ',', $file, $declaration_name);
1662 print STDERR
"${file}:$.: warning: cannot understand function prototype: '$prototype'\n";
1666 my $prms = join " ", @parameterlist;
1667 check_sections
($file, $declaration_name, "function", $sectcheck, $prms);
1669 # This check emits a lot of warnings at the moment, because many
1670 # functions don't have a 'Return' doc section. So until the number
1671 # of warnings goes sufficiently down, the check is only performed in
1673 # TODO: always perform the check.
1674 if ($verbose && !$noret) {
1675 check_return_section
($file, $declaration_name, $return_type);
1678 output_declaration
($declaration_name,
1680 {'function' => $declaration_name,
1681 'module' => $modulename,
1682 'functiontype' => $return_type,
1683 'parameterlist' => \
@parameterlist,
1684 'parameterdescs' => \
%parameterdescs,
1685 'parametertypes' => \
%parametertypes,
1686 'sectionlist' => \
@sectionlist,
1687 'sections' => \
%sections,
1688 'purpose' => $declaration_purpose
1694 %parameterdescs = ();
1695 %parametertypes = ();
1696 @parameterlist = ();
1700 $struct_actual = "";
1703 $state = STATE_NORMAL
;
1704 $inline_doc_state = STATE_INLINE_NA
;
1707 sub tracepoint_munge
($) {
1709 my $tracepointname = 0;
1710 my $tracepointargs = 0;
1712 if ($prototype =~ m/TRACE_EVENT\((.*?),/) {
1713 $tracepointname = $1;
1715 if ($prototype =~ m/DEFINE_SINGLE_EVENT\((.*?),/) {
1716 $tracepointname = $1;
1718 if ($prototype =~ m/DEFINE_EVENT\((.*?),(.*?),/) {
1719 $tracepointname = $2;
1721 $tracepointname =~ s/^\s+//; #strip leading whitespace
1722 if ($prototype =~ m/TP_PROTO\((.*?)\)/) {
1723 $tracepointargs = $1;
1725 if (($tracepointname eq 0) || ($tracepointargs eq 0)) {
1726 print STDERR
"${file}:$.: warning: Unrecognized tracepoint format: \n".
1729 $prototype = "static inline void trace_$tracepointname($tracepointargs)";
1733 sub syscall_munge
() {
1736 $prototype =~ s@
[\r\n]+@
@gos; # strip newlines/CR's
1737 ## if ($prototype =~ m/SYSCALL_DEFINE0\s*\(\s*(a-zA-Z0-9_)*\s*\)/) {
1738 if ($prototype =~ m/SYSCALL_DEFINE0/) {
1740 ## $prototype = "long sys_$1(void)";
1743 $prototype =~ s/SYSCALL_DEFINE.*\(/long sys_/; # fix return type & func name
1744 if ($prototype =~ m/long (sys_.*?),/) {
1745 $prototype =~ s/,/\(/;
1747 $prototype =~ s/\)/\(void\)/;
1750 # now delete all of the odd-number commas in $prototype
1751 # so that arg types & arg names don't have a comma between them
1753 my $len = length($prototype);
1755 $len = 0; # skip the for-loop
1757 for (my $ix = 0; $ix < $len; $ix++) {
1758 if (substr($prototype, $ix, 1) eq ',') {
1760 if ($count % 2 == 1) {
1761 substr($prototype, $ix, 1) = ' ';
1767 sub process_proto_function
($$) {
1771 $x =~ s@\
/\/.*$@
@gos; # strip C99-style comments to end of line
1773 if ($x =~ m
#\s*/\*\s+MACDOC\s*#io || ($x =~ /^#/ && $x !~ /^#\s*define/)) {
1776 elsif ($x =~ /([^\{]*)/) {
1780 if (($x =~ /\{/) || ($x =~ /\#\s*define/) || ($x =~ /;/)) {
1781 $prototype =~ s@
/\*.*?\*/@
@gos; # strip comments.
1782 $prototype =~ s@
[\r\n]+@
@gos; # strip newlines/cr's.
1783 $prototype =~ s@
^\s
+@
@gos; # strip leading spaces
1784 if ($prototype =~ /SYSCALL_DEFINE/) {
1787 if ($prototype =~ /TRACE_EVENT/ || $prototype =~ /DEFINE_EVENT/ ||
1788 $prototype =~ /DEFINE_SINGLE_EVENT/)
1790 tracepoint_munge
($file);
1792 dump_function
($prototype, $file);
1797 sub process_proto_type
($$) {
1801 $x =~ s@
[\r\n]+@
@gos; # strip newlines/cr's.
1802 $x =~ s@
^\s
+@
@gos; # strip leading spaces
1803 $x =~ s@\s
+$@
@gos; # strip trailing spaces
1804 $x =~ s@\
/\/.*$@
@gos; # strip C99-style comments to end of line
1807 # To distinguish preprocessor directive from regular declaration later.
1812 if ( $x =~ /([^\{\};]*)([\{\};])(.*)/ ) {
1813 if( length $prototype ) {
1816 $prototype .= $1 . $2;
1817 ($2 eq '{') && $brcount++;
1818 ($2 eq '}') && $brcount--;
1819 if (($2 eq ';') && ($brcount == 0)) {
1820 dump_declaration
($prototype, $file);
1833 sub map_filename
($) {
1835 my ($orig_file) = @_;
1837 if (defined($ENV{'SRCTREE'})) {
1838 $file = "$ENV{'SRCTREE'}" . "/" . $orig_file;
1843 if (defined($source_map{$file})) {
1844 $file = $source_map{$file};
1850 sub process_export_file
($) {
1851 my ($orig_file) = @_;
1852 my $file = map_filename
($orig_file);
1854 if (!open(IN
,"<$file")) {
1855 print STDERR
"Error: Cannot open file $file\n";
1861 if (/$export_symbol/) {
1862 $function_table{$2} = 1;
1870 # Parsers for the various processing states.
1872 # STATE_NORMAL: looking for the /** to begin everything.
1874 sub process_normal
() {
1875 if (/$doc_start/o) {
1876 $state = STATE_NAME
; # next line is always the function name
1878 $declaration_start_line = $. + 1;
1883 # STATE_NAME: Looking for the "name - description" line
1885 sub process_name
($$) {
1890 if (/$doc_block/o) {
1891 $state = STATE_DOCBLOCK
;
1893 $new_start_line = $. + 1;
1896 $section = $section_intro;
1901 elsif (/$doc_decl/o) {
1903 if (/\s*([\w\s]+?)(\s*-|:)/) {
1907 $state = STATE_BODY
;
1908 # if there's no @param blocks need to set up default section
1911 $section = $section_default;
1912 $new_start_line = $. + 1;
1914 # strip leading/trailing/multiple spaces
1918 $descr =~ s/\s+/ /g;
1919 $declaration_purpose = $descr;
1920 $state = STATE_BODY_MAYBE
;
1922 $declaration_purpose = "";
1925 if (($declaration_purpose eq "") && $verbose) {
1926 print STDERR
"${file}:$.: warning: missing initial short description on line:\n";
1931 if ($identifier =~ m/^[A-Z]/) {
1932 $decl_type = 'type name';
1933 } elsif ($identifier =~ m/^struct\b/) {
1934 $decl_type = 'struct';
1935 } elsif ($identifier =~ m/^union\b/) {
1936 $decl_type = 'union';
1937 } elsif ($identifier =~ m/^enum\b/) {
1938 $decl_type = 'enum';
1939 } elsif ($identifier =~ m/^typedef\b/) {
1940 $decl_type = 'typedef';
1942 $decl_type = 'function';
1946 print STDERR
"${file}:$.: info: Scanning doc for $identifier\n";
1949 print STDERR
"${file}:$.: warning: Cannot understand $_ on line $.",
1950 " - I thought it was a doc line\n";
1952 $state = STATE_NORMAL
;
1958 # STATE_BODY and STATE_BODY_MAYBE: the bulk of a kerneldoc comment.
1960 sub process_body
($$) {
1963 if (/$doc_sect/i) { # case insensitive for supported section names
1967 # map the supported section names to the canonical names
1968 if ($newsection =~ m/^description$/i) {
1969 $newsection = $section_default;
1970 } elsif ($newsection =~ m/^context$/i) {
1971 $newsection = $section_context;
1972 } elsif ($newsection =~ m/^returns?$/i) {
1973 $newsection = $section_return;
1974 } elsif ($newsection =~ m/^\@return$/) {
1975 # special: @return is a section, not a param description
1976 $newsection = $section_return;
1979 if (($contents ne "") && ($contents ne "\n")) {
1980 if (!$in_doc_sect && $verbose) {
1981 print STDERR
"${file}:$.: warning: contents before sections\n";
1984 dump_section
($file, $section, $contents);
1985 $section = $section_default;
1989 $state = STATE_BODY
;
1990 $contents = $newcontents;
1991 $new_start_line = $.;
1992 while (substr($contents, 0, 1) eq " ") {
1993 $contents = substr($contents, 1);
1995 if ($contents ne "") {
1998 $section = $newsection;
1999 $leading_space = undef;
2000 } elsif (/$doc_end/) {
2001 if (($contents ne "") && ($contents ne "\n")) {
2002 dump_section
($file, $section, $contents);
2003 $section = $section_default;
2006 # look for doc_com + <text> + doc_end:
2007 if ($_ =~ m
'\s*\*\s*[a-zA-Z_0-9:\.]+\*/') {
2008 print STDERR
"${file}:$.: warning: suspicious ending line: $_";
2013 $state = STATE_PROTO
;
2015 } elsif (/$doc_content/) {
2016 # miguel-style comment kludge, look for blank lines after
2017 # @parameter line to signify start of description
2019 if ($section =~ m/^@/ || $section eq $section_context) {
2020 dump_section
($file, $section, $contents);
2021 $section = $section_default;
2023 $new_start_line = $.;
2027 $state = STATE_BODY
;
2028 } elsif ($state == STATE_BODY_MAYBE
) {
2029 # Continued declaration purpose
2030 chomp($declaration_purpose);
2031 $declaration_purpose .= " " . $1;
2032 $declaration_purpose =~ s/\s+/ /g;
2035 if ($section =~ m/^@/ || $section eq $section_context) {
2036 if (!defined $leading_space) {
2037 if ($cont =~ m/^(\s+)/) {
2038 $leading_space = $1;
2040 $leading_space = "";
2043 $cont =~ s/^$leading_space//;
2045 $contents .= $cont . "\n";
2048 # i dont know - bad line? ignore.
2049 print STDERR
"${file}:$.: warning: bad line: $_";
2056 # STATE_PROTO: reading a function/whatever prototype.
2058 sub process_proto
($$) {
2061 if (/$doc_inline_oneline/) {
2064 if ($contents ne "") {
2066 dump_section
($file, $section, $contents);
2067 $section = $section_default;
2070 } elsif (/$doc_inline_start/) {
2071 $state = STATE_INLINE
;
2072 $inline_doc_state = STATE_INLINE_NAME
;
2073 } elsif ($decl_type eq 'function') {
2074 process_proto_function
($_, $file);
2076 process_proto_type
($_, $file);
2081 # STATE_DOCBLOCK: within a DOC: block.
2083 sub process_docblock
($$) {
2087 dump_doc_section
($file, $section, $contents);
2088 $section = $section_default;
2091 %parameterdescs = ();
2092 %parametertypes = ();
2093 @parameterlist = ();
2097 $state = STATE_NORMAL
;
2098 } elsif (/$doc_content/) {
2100 $contents .= $blankline;
2102 $contents .= $1 . "\n";
2108 # STATE_INLINE: docbook comments within a prototype.
2110 sub process_inline
($$) {
2113 # First line (state 1) needs to be a @parameter
2114 if ($inline_doc_state == STATE_INLINE_NAME
&& /$doc_inline_sect/o) {
2117 $new_start_line = $.;
2118 if ($contents ne "") {
2119 while (substr($contents, 0, 1) eq " ") {
2120 $contents = substr($contents, 1);
2124 $inline_doc_state = STATE_INLINE_TEXT
;
2125 # Documentation block end */
2126 } elsif (/$doc_inline_end/) {
2127 if (($contents ne "") && ($contents ne "\n")) {
2128 dump_section
($file, $section, $contents);
2129 $section = $section_default;
2132 $state = STATE_PROTO
;
2133 $inline_doc_state = STATE_INLINE_NA
;
2135 } elsif (/$doc_content/) {
2136 if ($inline_doc_state == STATE_INLINE_TEXT
) {
2137 $contents .= $1 . "\n";
2138 # nuke leading blank lines
2139 if ($contents =~ /^\s*$/) {
2142 } elsif ($inline_doc_state == STATE_INLINE_NAME
) {
2143 $inline_doc_state = STATE_INLINE_ERROR
;
2144 print STDERR
"${file}:$.: warning: ";
2145 print STDERR
"Incorrect use of kernel-doc format: $_";
2152 sub process_file
($) {
2154 my $initial_section_counter = $section_counter;
2155 my ($orig_file) = @_;
2157 $file = map_filename
($orig_file);
2159 if (!open(IN
,"<$file")) {
2160 print STDERR
"Error: Cannot open file $file\n";
2167 $section_counter = 0;
2169 while (s/\\\s*$//) {
2172 # Replace tabs by spaces
2173 while ($_ =~ s/\t+/' ' x (length($&) * 8 - length($`) % 8)/e) {};
2174 # Hand this line to the appropriate state handler
2175 if ($state == STATE_NORMAL
) {
2177 } elsif ($state == STATE_NAME
) {
2178 process_name
($file, $_);
2179 } elsif ($state == STATE_BODY
|| $state == STATE_BODY_MAYBE
) {
2180 process_body
($file, $_);
2181 } elsif ($state == STATE_INLINE
) { # scanning for inline parameters
2182 process_inline
($file, $_);
2183 } elsif ($state == STATE_PROTO
) {
2184 process_proto
($file, $_);
2185 } elsif ($state == STATE_DOCBLOCK
) {
2186 process_docblock
($file, $_);
2190 # Make sure we got something interesting.
2191 if ($initial_section_counter == $section_counter && $
2192 output_mode
ne "none") {
2193 if ($output_selection == OUTPUT_INCLUDE
) {
2194 print STDERR
"${file}:1: warning: '$_' not found\n"
2195 for keys %function_table;
2198 print STDERR
"${file}:1: warning: no structured comments found\n";
2204 $kernelversion = get_kernel_version
();
2206 # generate a sequence of code that will splice in highlighting information
2207 # using the s// operator.
2208 for (my $k = 0; $k < @highlights; $k++) {
2209 my $pattern = $highlights[$k][0];
2210 my $result = $highlights[$k][1];
2211 # print STDERR "scanning pattern:$pattern, highlight:($result)\n";
2212 $dohighlight .= "\$contents =~ s:$pattern:$result:gs;\n";
2215 # Read the file that maps relative names to absolute names for
2216 # separate source and object directories and for shadow trees.
2217 if (open(SOURCE_MAP
, "<.tmp_filelist.txt")) {
2218 my ($relname, $absname);
2219 while(<SOURCE_MAP
>) {
2221 ($relname, $absname) = (split())[0..1];
2222 $relname =~ s
:^/+::;
2223 $source_map{$relname} = $absname;
2228 if ($output_selection == OUTPUT_EXPORTED
||
2229 $output_selection == OUTPUT_INTERNAL
) {
2231 push(@export_file_list, @ARGV);
2233 foreach (@export_file_list) {
2235 process_export_file
($_);
2243 if ($verbose && $errors) {
2244 print STDERR
"$errors errors\n";
2246 if ($verbose && $warnings) {
2247 print STDERR
"$warnings warnings\n";
2250 exit($output_mode eq "none" ?
0 : $errors);