6 ## Copyright (c) 1998 Michael Zucchi, All Rights Reserved ##
7 ## Copyright (C) 2000, 1 Tim Waugh <twaugh@redhat.com> ##
8 ## Copyright (C) 2001 Simon Huggins ##
9 ## Copyright (C) 2005-2012 Randy Dunlap ##
10 ## Copyright (C) 2012 Dan Luedtke ##
12 ## #define enhancements by Armin Kuster <akuster@mvista.com> ##
13 ## Copyright (c) 2000 MontaVista Software, Inc. ##
15 ## This software falls under the GNU General Public License. ##
16 ## Please read the COPYING file for more information ##
18 # 18/01/2001 - Cleanups
19 # Functions prototyped as foo(void) same as foo()
20 # Stop eval'ing where we don't need to.
23 # 27/06/2001 - Allowed whitespace after initial "/**" and
24 # allowed comments before function declarations.
25 # -- Christian Kreibich <ck@whoop.org>
28 # - add perldoc documentation
29 # - Look more closely at some of the scarier bits :)
31 # 26/05/2001 - Support for separate source and object trees.
33 # Keith Owens <kaos@ocs.com.au>
35 # 23/09/2001 - Added support for typedefs, structs, enums and unions
36 # Support for Context section; can be terminated using empty line
37 # Small fixes (like spaces vs. \s in regex)
38 # -- Tim Jansen <tim@tjansen.de>
40 # 25/07/2012 - Added support for HTML5
41 # -- Dan Luedtke <mail@danrl.de>
44 my $message = <<"EOF";
45 Usage: $0 [OPTION ...] FILE ...
47 Read C language source or header FILEs, extract embedded documentation comments,
48 and print formatted documentation to standard output.
50 The documentation comments are identified by "/**" opening comment mark. See
51 Documentation/doc-guide/kernel-doc.rst for the documentation comment syntax.
53 Output format selection (mutually exclusive):
54 -man Output troff manual page format. This is the default.
55 -rst Output reStructuredText format.
56 -none Do not output documentation, only warnings.
58 Output selection (mutually exclusive):
59 -export Only output documentation for symbols that have been
60 exported using EXPORT_SYMBOL() or EXPORT_SYMBOL_GPL()
61 in any input FILE or -export-file FILE.
62 -internal Only output documentation for symbols that have NOT been
63 exported using EXPORT_SYMBOL() or EXPORT_SYMBOL_GPL()
64 in any input FILE or -export-file FILE.
65 -function NAME Only output documentation for the given function(s)
66 or DOC: section title(s). All other functions and DOC:
67 sections are ignored. May be specified multiple times.
68 -nofunction NAME Do NOT output documentation for the given function(s);
69 only output documentation for the other functions and
70 DOC: sections. 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.
91 # In the following table, (...)? signifies optional structure.
92 # (...)* signifies 0 or more structure elements
94 # * function_name(:)? (- short description)?
95 # (* @parameterx: (description of parameter x)?)*
97 # * (Description:)? (Description of function)?
98 # * (section header: (section description)? )*
101 # So .. the trivial example would be:
107 # If the Description: header tag is omitted, then there must be a blank line
108 # after the last parameter specification.
111 # * my_function - does my stuff
112 # * @my_arg: its mine damnit
114 # * Does my stuff explained.
117 # or, could also use:
119 # * my_function - does my stuff
120 # * @my_arg: its mine damnit
121 # * Description: Does my stuff explained.
125 # Besides functions you can also write documentation for structs, unions,
126 # enums and typedefs. Instead of the function name you must write the name
127 # of the declaration; the struct/union/enum/typedef must always precede
128 # the name. Nesting of declarations is not supported.
129 # Use the argument mechanism to document members or constants.
132 # * struct my_struct - short description
134 # * @b: second member
136 # * Longer description
145 # All descriptions can be multiline, except the short function description.
147 # For really longs structs, you can also describe arguments inside the
148 # body of the struct.
151 # * struct my_struct - short description
153 # * @b: second member
155 # * Longer description
161 # * @c: This is longer description of C
163 # * You can use paragraphs to describe arguments
164 # * using this method.
169 # This should be use only for struct/enum members.
171 # You can also add additional sections. When documenting kernel functions you
172 # should document the "Context:" of the function, e.g. whether the functions
173 # can be called form interrupts. Unlike other sections you can end it with an
175 # A non-void function should have a "Return:" section describing the return
177 # Example-sections should contain the string EXAMPLE so that they are marked
178 # appropriately in DocBook.
182 # * user_function - function that can only be called in user context
183 # * @a: some argument
184 # * Context: !in_interrupt()
188 # * user_function(22);
193 # All descriptive text is further processed, scanning for the following special
194 # patterns, which are highlighted appropriately.
196 # 'funcname()' - function
197 # '$ENVVAR' - environmental variable
198 # '&struct_name' - name of a structure (up to two words including 'struct')
199 # '&struct_name.member' - name of a structure member
200 # '@parameter' - name of a parameter
201 # '%CONST' - name of a constant.
202 # '``LITERAL``' - literal string without any spaces on it.
208 my $anon_struct_union = 0;
210 # match expressions used to find embedded type information
211 my $type_constant = '\b``([^\`]+)``\b';
212 my $type_constant2 = '\%([-_\w]+)';
213 my $type_func = '(\w+)\(\)';
214 my $type_param = '\@(\w*(\.\w+)*(\.\.\.)?)';
215 my $type_fp_param = '\@(\w+)\(\)'; # Special RST handling for func ptr params
216 my $type_env = '(\$\w+)';
217 my $type_enum = '\&(enum\s*([_\w]+))';
218 my $type_struct = '\&(struct\s*([_\w]+))';
219 my $type_typedef = '\&(typedef\s*([_\w]+))';
220 my $type_union = '\&(union\s*([_\w]+))';
221 my $type_member = '\&([_\w]+)(\.|->)([_\w]+)';
222 my $type_fallback = '\&([_\w]+)';
223 my $type_member_func = $type_member . '\(\)';
225 # Output conversion substitutions.
226 # One for each output format
228 # these are pretty rough
229 my @highlights_man = (
230 [$type_constant, "\$1"],
231 [$type_constant2, "\$1"],
232 [$type_func, "\\\\fB\$1\\\\fP"],
233 [$type_enum, "\\\\fI\$1\\\\fP"],
234 [$type_struct, "\\\\fI\$1\\\\fP"],
235 [$type_typedef, "\\\\fI\$1\\\\fP"],
236 [$type_union, "\\\\fI\$1\\\\fP"],
237 [$type_param, "\\\\fI\$1\\\\fP"],
238 [$type_member, "\\\\fI\$1\$2\$3\\\\fP"],
239 [$type_fallback, "\\\\fI\$1\\\\fP"]
241 my $blankline_man = "";
244 my @highlights_rst = (
245 [$type_constant, "``\$1``"],
246 [$type_constant2, "``\$1``"],
247 # Note: need to escape () to avoid func matching later
248 [$type_member_func, "\\:c\\:type\\:`\$1\$2\$3\\\\(\\\\) <\$1>`"],
249 [$type_member, "\\:c\\:type\\:`\$1\$2\$3 <\$1>`"],
250 [$type_fp_param, "**\$1\\\\(\\\\)**"],
251 [$type_func, "\\:c\\:func\\:`\$1()`"],
252 [$type_enum, "\\:c\\:type\\:`\$1 <\$2>`"],
253 [$type_struct, "\\:c\\:type\\:`\$1 <\$2>`"],
254 [$type_typedef, "\\:c\\:type\\:`\$1 <\$2>`"],
255 [$type_union, "\\:c\\:type\\:`\$1 <\$2>`"],
256 # in rst this can refer to any type
257 [$type_fallback, "\\:c\\:type\\:`\$1`"],
258 [$type_param, "**\$1**"]
260 my $blankline_rst = "\n";
268 my $dohighlight = "";
271 my $output_mode = "rst";
272 my $output_preformatted = 0;
273 my $no_doc_sections = 0;
274 my $enable_lineno = 0;
275 my @highlights = @highlights_rst;
276 my $blankline = $blankline_rst;
277 my $modulename = "Kernel API";
280 OUTPUT_ALL => 0, # output all symbols and doc sections
281 OUTPUT_INCLUDE => 1, # output only specified symbols
282 OUTPUT_EXCLUDE => 2, # output everything except specified symbols
283 OUTPUT_EXPORTED => 3, # output exported symbols
284 OUTPUT_INTERNAL => 4, # output non-exported symbols
286 my $output_selection = OUTPUT_ALL;
287 my $show_not_found = 0;
289 my @export_file_list;
292 if (defined($ENV{'KBUILD_BUILD_TIMESTAMP'}) &&
293 (my $seconds = `date -d"${ENV{'KBUILD_BUILD_TIMESTAMP'}}" +%s`) ne '') {
294 @build_time = gmtime($seconds);
296 @build_time = localtime;
299 my $man_date = ('January', 'February', 'March', 'April', 'May', 'June',
300 'July', 'August', 'September', 'October',
301 'November', 'December')[$build_time[4]] .
302 " " . ($build_time[5]+1900);
304 # Essentially these are globals.
305 # They probably want to be tidied up, made more localised or something.
306 # CAVEAT EMPTOR! Some of the others I localised may not want to be, which
307 # could cause "use of undefined value" or other bugs.
308 my ($function, %function_table, %parametertypes, $declaration_purpose);
309 my $declaration_start_line;
310 my ($type, $declaration_name, $return_type);
311 my ($newsection, $newcontents, $prototype, $brcount, %source_map);
313 if (defined($ENV{'KBUILD_VERBOSE'})) {
314 $verbose = "$ENV{'KBUILD_VERBOSE'}";
317 # Generated docbook code is inserted in a template at a point where
318 # docbook v3.1 requires a non-zero sequence of RefEntry's; see:
319 # http://www.oasis-open.org/docbook/documentation/reference/html/refentry.html
320 # We keep track of number of generated entries and generate a dummy
321 # if needs be to ensure the expanded template can be postprocessed
323 my $section_counter = 0;
329 STATE_NORMAL => 0, # normal code
330 STATE_NAME => 1, # looking for function name
331 STATE_BODY_MAYBE => 2, # body - or maybe more description
332 STATE_BODY => 3, # the body of the comment
333 STATE_PROTO => 4, # scanning prototype
334 STATE_DOCBLOCK => 5, # documentation block
335 STATE_INLINE => 6, # gathering documentation outside main block
341 # Inline documentation state
343 STATE_INLINE_NA => 0, # not applicable ($state != STATE_INLINE)
344 STATE_INLINE_NAME => 1, # looking for member name (@foo:)
345 STATE_INLINE_TEXT => 2, # looking for member documentation
346 STATE_INLINE_END => 3, # done
347 STATE_INLINE_ERROR => 4, # error - Comment without header was found.
348 # Spit a warning as it's not
349 # proper kernel-doc and ignore the rest.
351 my $inline_doc_state;
353 #declaration types: can be
354 # 'function', 'struct', 'union', 'enum', 'typedef'
357 my $doc_start = '^/\*\*\s*$'; # Allow whitespace at end of comment start.
359 my $doc_com = '\s*\*\s*';
360 my $doc_com_body = '\s*\* ?';
361 my $doc_decl = $doc_com . '(\w+)';
362 # @params and a strictly limited set of supported section names
363 my $doc_sect = $doc_com .
364 '\s*(\@[.\w]+|\@\.\.\.|description|context|returns?|notes?|examples?)\s*:(.*)';
365 my $doc_content = $doc_com_body . '(.*)';
366 my $doc_block = $doc_com . 'DOC:\s*(.*)?';
367 my $doc_inline_start = '^\s*/\*\*\s*$';
368 my $doc_inline_sect = '\s*\*\s*(@[\w\s]+):(.*)';
369 my $doc_inline_end = '^\s*\*/\s*$';
370 my $doc_inline_oneline = '^\s*/\*\*\s*(@[\w\s]+):\s*(.*)\s*\*/\s*$';
371 my $export_symbol = '^\s*EXPORT_SYMBOL(_GPL)?\s*\(\s*(\w+)\s*\)\s*;';
374 my %parameterdesc_start_lines;
378 my %section_start_lines;
383 my $new_start_line = 0;
385 # the canonical section names. see also $doc_sect above.
386 my $section_default = "Description"; # default section
387 my $section_intro = "Introduction";
388 my $section = $section_default;
389 my $section_context = "Context";
390 my $section_return = "Return";
392 my $undescribed = "-- undescribed --";
396 while ($ARGV[0] =~ m/^--?(.*)/) {
400 $output_mode = "man";
401 @highlights = @highlights_man;
402 $blankline = $blankline_man;
403 } elsif ($cmd eq "rst") {
404 $output_mode = "rst";
405 @highlights = @highlights_rst;
406 $blankline = $blankline_rst;
407 } elsif ($cmd eq "none") {
408 $output_mode = "none";
409 } elsif ($cmd eq "module") { # not needed for XML, inherits from calling document
410 $modulename = shift @ARGV;
411 } elsif ($cmd eq "function") { # to only output specific functions
412 $output_selection = OUTPUT_INCLUDE;
413 $function = shift @ARGV;
414 $function_table{$function} = 1;
415 } elsif ($cmd eq "nofunction") { # output all except specific functions
416 $output_selection = OUTPUT_EXCLUDE;
417 $function = shift @ARGV;
418 $function_table{$function} = 1;
419 } elsif ($cmd eq "export") { # only exported symbols
420 $output_selection = OUTPUT_EXPORTED;
421 %function_table = ();
422 } elsif ($cmd eq "internal") { # only non-exported symbols
423 $output_selection = OUTPUT_INTERNAL;
424 %function_table = ();
425 } elsif ($cmd eq "export-file") {
426 my $file = shift @ARGV;
427 push(@export_file_list, $file);
428 } elsif ($cmd eq "v") {
430 } elsif (($cmd eq "h") || ($cmd eq "help")) {
432 } elsif ($cmd eq 'no-doc-sections') {
433 $no_doc_sections = 1;
434 } elsif ($cmd eq 'enable-lineno') {
436 } elsif ($cmd eq 'show-not-found') {
444 # continue execution near EOF;
446 # get kernel version from env
447 sub get_kernel_version() {
448 my $version = 'unknown kernel version';
450 if (defined($ENV{'KERNELVERSION'})) {
451 $version = $ENV{'KERNELVERSION'};
459 if ($enable_lineno && defined($lineno)) {
460 print "#define LINENO " . $lineno . "\n";
464 # dumps section contents to arrays/hashes intended for that purpose.
469 my $contents = join "\n", @_;
471 if ($name =~ m/$type_param/) {
473 $parameterdescs{$name} = $contents;
474 $sectcheck = $sectcheck . $name . " ";
475 $parameterdesc_start_lines{$name} = $new_start_line;
477 } elsif ($name eq "@\.\.\.") {
479 $parameterdescs{$name} = $contents;
480 $sectcheck = $sectcheck . $name . " ";
481 $parameterdesc_start_lines{$name} = $new_start_line;
484 if (defined($sections{$name}) && ($sections{$name} ne "")) {
485 # Only warn on user specified duplicate section names.
486 if ($name ne $section_default) {
487 print STDERR "${file}:$.: warning: duplicate section name '$name'\n";
490 $sections{$name} .= $contents;
492 $sections{$name} = $contents;
493 push @sectionlist, $name;
494 $section_start_lines{$name} = $new_start_line;
501 # dump DOC: section after checking that it should go out
503 sub dump_doc_section {
506 my $contents = join "\n", @_;
508 if ($no_doc_sections) {
512 if (($output_selection == OUTPUT_ALL) ||
513 ($output_selection == OUTPUT_INCLUDE &&
514 defined($function_table{$name})) ||
515 ($output_selection == OUTPUT_EXCLUDE &&
516 !defined($function_table{$name})))
518 dump_section($file, $name, $contents);
519 output_blockhead({'sectionlist' => \@sectionlist,
520 'sections' => \%sections,
521 'module' => $modulename,
522 'content-only' => ($output_selection != OUTPUT_ALL), });
529 # parameterdescs, a hash.
530 # function => "function name"
531 # parameterlist => @list of parameters
532 # parameterdescs => %parameter descriptions
533 # sectionlist => @list of sections
534 # sections => %section descriptions
537 sub output_highlight {
538 my $contents = join "\n",@_;
542 # if (!defined $contents) {
544 # confess "output_highlight got called with no args?\n";
547 # print STDERR "contents b4:$contents\n";
550 # print STDERR "contents af:$contents\n";
552 foreach $line (split "\n", $contents) {
553 if (! $output_preformatted) {
557 if (! $output_preformatted) {
558 print $lineprefix, $blankline;
561 if ($output_mode eq "man" && substr($line, 0, 1) eq ".") {
564 print $lineprefix, $line;
572 # output function in man
573 sub output_function_man(%) {
575 my ($parameter, $section);
578 print ".TH \"$args{'function'}\" 9 \"$args{'function'}\" \"$man_date\" \"Kernel Hacker's Manual\" LINUX\n";
581 print $args{'function'} . " \\- " . $args{'purpose'} . "\n";
583 print ".SH SYNOPSIS\n";
584 if ($args{'functiontype'} ne "") {
585 print ".B \"" . $args{'functiontype'} . "\" " . $args{'function'} . "\n";
587 print ".B \"" . $args{'function'} . "\n";
592 foreach my $parameter (@{$args{'parameterlist'}}) {
593 if ($count == $#{$args{'parameterlist'}}) {
596 $type = $args{'parametertypes'}{$parameter};
597 if ($type =~ m/([^\(]*\(\*)\s*\)\s*\(([^\)]*)\)/) {
598 # pointer-to-function
599 print ".BI \"" . $parenth . $1 . "\" " . $parameter . " \") (" . $2 . ")" . $post . "\"\n";
601 $type =~ s/([^\*])$/$1 /;
602 print ".BI \"" . $parenth . $type . "\" " . $parameter . " \"" . $post . "\"\n";
608 print ".SH ARGUMENTS\n";
609 foreach $parameter (@{$args{'parameterlist'}}) {
610 my $parameter_name = $parameter;
611 $parameter_name =~ s/\[.*//;
613 print ".IP \"" . $parameter . "\" 12\n";
614 output_highlight($args{'parameterdescs'}{$parameter_name});
616 foreach $section (@{$args{'sectionlist'}}) {
617 print ".SH \"", uc $section, "\"\n";
618 output_highlight($args{'sections'}{$section});
624 sub output_enum_man(%) {
626 my ($parameter, $section);
629 print ".TH \"$args{'module'}\" 9 \"enum $args{'enum'}\" \"$man_date\" \"API Manual\" LINUX\n";
632 print "enum " . $args{'enum'} . " \\- " . $args{'purpose'} . "\n";
634 print ".SH SYNOPSIS\n";
635 print "enum " . $args{'enum'} . " {\n";
637 foreach my $parameter (@{$args{'parameterlist'}}) {
638 print ".br\n.BI \" $parameter\"\n";
639 if ($count == $#{$args{'parameterlist'}}) {
649 print ".SH Constants\n";
650 foreach $parameter (@{$args{'parameterlist'}}) {
651 my $parameter_name = $parameter;
652 $parameter_name =~ s/\[.*//;
654 print ".IP \"" . $parameter . "\" 12\n";
655 output_highlight($args{'parameterdescs'}{$parameter_name});
657 foreach $section (@{$args{'sectionlist'}}) {
658 print ".SH \"$section\"\n";
659 output_highlight($args{'sections'}{$section});
664 # output struct in man
665 sub output_struct_man(%) {
667 my ($parameter, $section);
669 print ".TH \"$args{'module'}\" 9 \"" . $args{'type'} . " " . $args{'struct'} . "\" \"$man_date\" \"API Manual\" LINUX\n";
672 print $args{'type'} . " " . $args{'struct'} . " \\- " . $args{'purpose'} . "\n";
674 my $declaration = $args{'definition'};
675 $declaration =~ s/\t/ /g;
676 $declaration =~ s/\n/"\n.br\n.BI \"/g;
677 print ".SH SYNOPSIS\n";
678 print $args{'type'} . " " . $args{'struct'} . " {\n.br\n";
679 print ".BI \"$declaration\n};\n.br\n\n";
681 print ".SH Members\n";
682 foreach $parameter (@{$args{'parameterlist'}}) {
683 ($parameter =~ /^#/) && next;
685 my $parameter_name = $parameter;
686 $parameter_name =~ s/\[.*//;
688 ($args{'parameterdescs'}{$parameter_name} ne $undescribed) || next;
689 print ".IP \"" . $parameter . "\" 12\n";
690 output_highlight($args{'parameterdescs'}{$parameter_name});
692 foreach $section (@{$args{'sectionlist'}}) {
693 print ".SH \"$section\"\n";
694 output_highlight($args{'sections'}{$section});
699 # output typedef in man
700 sub output_typedef_man(%) {
702 my ($parameter, $section);
704 print ".TH \"$args{'module'}\" 9 \"$args{'typedef'}\" \"$man_date\" \"API Manual\" LINUX\n";
707 print "typedef " . $args{'typedef'} . " \\- " . $args{'purpose'} . "\n";
709 foreach $section (@{$args{'sectionlist'}}) {
710 print ".SH \"$section\"\n";
711 output_highlight($args{'sections'}{$section});
715 sub output_blockhead_man(%) {
717 my ($parameter, $section);
720 print ".TH \"$args{'module'}\" 9 \"$args{'module'}\" \"$man_date\" \"API Manual\" LINUX\n";
722 foreach $section (@{$args{'sectionlist'}}) {
723 print ".SH \"$section\"\n";
724 output_highlight($args{'sections'}{$section});
729 # output in restructured text
733 # This could use some work; it's used to output the DOC: sections, and
734 # starts by putting out the name of the doc section itself, but that tends
735 # to duplicate a header already in the template file.
737 sub output_blockhead_rst(%) {
739 my ($parameter, $section);
741 foreach $section (@{$args{'sectionlist'}}) {
742 if ($output_selection != OUTPUT_INCLUDE) {
743 print "**$section**\n\n";
745 print_lineno($section_start_lines{$section});
746 output_highlight_rst($args{'sections'}{$section});
752 # Apply the RST highlights to a sub-block of text.
754 sub highlight_block($) {
755 # The dohighlight kludge requires the text be called $contents
756 my $contents = shift;
763 # Regexes used only here.
765 my $sphinx_literal = '^[^.].*::$';
766 my $sphinx_cblock = '^\.\.\ +code-block::';
768 sub output_highlight_rst {
769 my $input = join "\n",@_;
776 foreach $line (split "\n",$input) {
778 # If we're in a literal block, see if we should drop out
779 # of it. Otherwise pass the line straight through unmunged.
782 if (! ($line =~ /^\s*$/)) {
784 # If this is the first non-blank line in a literal
785 # block we need to figure out what the proper indent is.
787 if ($litprefix eq "") {
789 $litprefix = '^' . $1;
790 $output .= $line . "\n";
791 } elsif (! ($line =~ /$litprefix/)) {
794 $output .= $line . "\n";
797 $output .= $line . "\n";
801 # Not in a literal block (or just dropped out)
804 $block .= $line . "\n";
805 if (($line =~ /$sphinx_literal/) || ($line =~ /$sphinx_cblock/)) {
808 $output .= highlight_block($block);
815 $output .= highlight_block($block);
817 foreach $line (split "\n", $output) {
818 print $lineprefix . $line . "\n";
822 sub output_function_rst(%) {
824 my ($parameter, $section);
825 my $oldprefix = $lineprefix;
828 if ($args{'typedef'}) {
829 print ".. c:type:: ". $args{'function'} . "\n\n";
830 print_lineno($declaration_start_line);
831 print " **Typedef**: ";
833 output_highlight_rst($args{'purpose'});
834 $start = "\n\n**Syntax**\n\n ``";
836 print ".. c:function:: ";
838 if ($args{'functiontype'} ne "") {
839 $start .= $args{'functiontype'} . " " . $args{'function'} . " (";
841 $start .= $args{'function'} . " (";
846 foreach my $parameter (@{$args{'parameterlist'}}) {
851 $type = $args{'parametertypes'}{$parameter};
853 if ($type =~ m/([^\(]*\(\*)\s*\)\s*\(([^\)]*)\)/) {
854 # pointer-to-function
855 print $1 . $parameter . ") (" . $2;
857 print $type . " " . $parameter;
860 if ($args{'typedef'}) {
864 print_lineno($declaration_start_line);
866 output_highlight_rst($args{'purpose'});
870 print "**Parameters**\n\n";
872 foreach $parameter (@{$args{'parameterlist'}}) {
873 my $parameter_name = $parameter;
874 $parameter_name =~ s/\[.*//;
875 $type = $args{'parametertypes'}{$parameter};
878 print "``$type $parameter``\n";
880 print "``$parameter``\n";
883 print_lineno($parameterdesc_start_lines{$parameter_name});
885 if (defined($args{'parameterdescs'}{$parameter_name}) &&
886 $args{'parameterdescs'}{$parameter_name} ne $undescribed) {
887 output_highlight_rst($args{'parameterdescs'}{$parameter_name});
889 print " *undescribed*\n";
894 $lineprefix = $oldprefix;
895 output_section_rst(@_);
898 sub output_section_rst(%) {
901 my $oldprefix = $lineprefix;
904 foreach $section (@{$args{'sectionlist'}}) {
905 print "**$section**\n\n";
906 print_lineno($section_start_lines{$section});
907 output_highlight_rst($args{'sections'}{$section});
911 $lineprefix = $oldprefix;
914 sub output_enum_rst(%) {
917 my $oldprefix = $lineprefix;
919 my $name = "enum " . $args{'enum'};
921 print "\n\n.. c:type:: " . $name . "\n\n";
922 print_lineno($declaration_start_line);
924 output_highlight_rst($args{'purpose'});
927 print "**Constants**\n\n";
929 foreach $parameter (@{$args{'parameterlist'}}) {
930 print "``$parameter``\n";
931 if ($args{'parameterdescs'}{$parameter} ne $undescribed) {
932 output_highlight_rst($args{'parameterdescs'}{$parameter});
934 print " *undescribed*\n";
939 $lineprefix = $oldprefix;
940 output_section_rst(@_);
943 sub output_typedef_rst(%) {
946 my $oldprefix = $lineprefix;
947 my $name = "typedef " . $args{'typedef'};
949 print "\n\n.. c:type:: " . $name . "\n\n";
950 print_lineno($declaration_start_line);
952 output_highlight_rst($args{'purpose'});
955 $lineprefix = $oldprefix;
956 output_section_rst(@_);
959 sub output_struct_rst(%) {
962 my $oldprefix = $lineprefix;
963 my $name = $args{'type'} . " " . $args{'struct'};
965 print "\n\n.. c:type:: " . $name . "\n\n";
966 print_lineno($declaration_start_line);
968 output_highlight_rst($args{'purpose'});
971 print "**Definition**\n\n";
973 my $declaration = $args{'definition'};
974 $declaration =~ s/\t/ /g;
975 print " " . $args{'type'} . " " . $args{'struct'} . " {\n$declaration };\n\n";
977 print "**Members**\n\n";
979 foreach $parameter (@{$args{'parameterlist'}}) {
980 ($parameter =~ /^#/) && next;
982 my $parameter_name = $parameter;
983 $parameter_name =~ s/\[.*//;
985 ($args{'parameterdescs'}{$parameter_name} ne $undescribed) || next;
986 $type = $args{'parametertypes'}{$parameter};
987 print_lineno($parameterdesc_start_lines{$parameter_name});
988 print "``" . $parameter . "``\n";
989 output_highlight_rst($args{'parameterdescs'}{$parameter_name});
994 $lineprefix = $oldprefix;
995 output_section_rst(@_);
998 ## none mode output functions
1000 sub output_function_none(%) {
1003 sub output_enum_none(%) {
1006 sub output_typedef_none(%) {
1009 sub output_struct_none(%) {
1012 sub output_blockhead_none(%) {
1016 # generic output function for all types (function, struct/union, typedef, enum);
1017 # calls the generated, variable output_ function name based on
1018 # functype and output_mode
1019 sub output_declaration {
1022 my $functype = shift;
1023 my $func = "output_${functype}_$output_mode";
1024 if (($output_selection == OUTPUT_ALL) ||
1025 (($output_selection == OUTPUT_INCLUDE ||
1026 $output_selection == OUTPUT_EXPORTED) &&
1027 defined($function_table{$name})) ||
1028 (($output_selection == OUTPUT_EXCLUDE ||
1029 $output_selection == OUTPUT_INTERNAL) &&
1030 !($functype eq "function" && defined($function_table{$name}))))
1038 # generic output function - calls the right one based on current output mode.
1039 sub output_blockhead {
1041 my $func = "output_blockhead_" . $output_mode;
1047 # takes a declaration (struct, union, enum, typedef) and
1048 # invokes the right handler. NOT called for functions.
1049 sub dump_declaration($$) {
1051 my ($prototype, $file) = @_;
1052 my $func = "dump_" . $decl_type;
1056 sub dump_union($$) {
1060 sub dump_struct($$) {
1064 if ($x =~ /(struct|union)\s+(\w+)\s*{(.*)}/) {
1066 $declaration_name = $2;
1069 # ignore members marked private:
1070 $members =~ s/\/\*\s*private:.*?\/\*\s*public:.*?\*\///gosi;
1071 $members =~ s/\/\*\s*private:.*//gosi;
1073 $members =~ s/\/\*.*?\*\///gos;
1075 $members =~ s/__attribute__\s*\(\([a-z,_\*\s\(\)]*\)\)//i;
1076 $members =~ s/__aligned\s*\([^;]*\)//gos;
1077 $members =~ s/\s*CRYPTO_MINALIGN_ATTR//gos;
1078 # replace DECLARE_BITMAP
1079 $members =~ s/DECLARE_BITMAP\s*\(([^,)]+),\s*([^,)]+)\)/unsigned long $1\[BITS_TO_LONGS($2)\]/gos;
1080 # replace DECLARE_HASHTABLE
1081 $members =~ s/DECLARE_HASHTABLE\s*\(([^,)]+),\s*([^,)]+)\)/unsigned long $1\[1 << (($2) - 1)\]/gos;
1082 # replace DECLARE_KFIFO
1083 $members =~ s/DECLARE_KFIFO\s*\(([^,)]+),\s*([^,)]+),\s*([^,)]+)\)/$2 \*$1/gos;
1084 # replace DECLARE_KFIFO_PTR
1085 $members =~ s/DECLARE_KFIFO_PTR\s*\(([^,)]+),\s*([^,)]+)\)/$2 \*$1/gos;
1087 my $declaration = $members;
1089 # Split nested struct/union elements as newer ones
1090 while ($members =~ m/(struct|union)([^\{\};]+)\{([^\{\}]*)\}([^\{\}\;]*)\;/) {
1095 foreach my $id(split /,/, $ids) {
1096 $newmember .= "$maintype $id; ";
1099 $id =~ s/^\s*\**(\S+)\s*/$1/;
1100 foreach my $arg (split /;/, $content) {
1101 next if ($arg =~ m/^\s*$/);
1102 if ($arg =~ m/^([^\(]+\(\*?\s*)([\w\.]*)(\s*\).*)/) {
1103 # pointer-to-function
1108 if ($id =~ m/^\s*$/) {
1109 # anonymous struct/union
1110 $newmember .= "$type$name$extra; ";
1112 $newmember .= "$type$id.$name$extra; ";
1120 $arg =~ s/:\s*\d+\s*//g;
1122 $arg =~ s/\[\S+\]//g;
1123 # The type may have multiple words,
1124 # and multiple IDs can be defined, like:
1125 # const struct foo, *bar, foobar
1126 # So, we remove spaces when parsing the
1127 # names, in order to match just names
1128 # and commas for the names
1129 $arg =~ s/\s*,\s*/,/g;
1130 if ($arg =~ m/(.*)\s+([\S+,]+)/) {
1134 $newmember .= "$arg; ";
1137 foreach my $name (split /,/, $names) {
1138 $name =~ s/^\s*\**(\S+)\s*/$1/;
1139 next if (($name =~ m/^\s*$/));
1140 if ($id =~ m/^\s*$/) {
1141 # anonymous struct/union
1142 $newmember .= "$type $name; ";
1144 $newmember .= "$type $id.$name; ";
1150 $members =~ s/(struct|union)([^\{\};]+)\{([^\{\}]*)}([^\{\}\;]*)\;/$newmember/;
1153 # Ignore other nested elements, like enums
1154 $members =~ s/({[^\{\}]*})//g;
1156 create_parameterlist($members, ';', $file, $declaration_name);
1157 check_sections($file, $declaration_name, $decl_type, $sectcheck, $struct_actual);
1159 # Adjust declaration for better display
1160 $declaration =~ s/([{;])/$1\n/g;
1161 $declaration =~ s/}\s+;/};/g;
1162 # Better handle inlined enums
1163 do {} while ($declaration =~ s/(enum\s+{[^}]+),([^\n])/$1,\n$2/);
1165 my @def_args = split /\n/, $declaration;
1168 foreach my $clause (@def_args) {
1169 $clause =~ s/^\s+//;
1170 $clause =~ s/\s+$//;
1171 $clause =~ s/\s+/ /;
1173 $level-- if ($clause =~ m/(})/ && $level > 1);
1174 if (!($clause =~ m/^\s*#/)) {
1175 $declaration .= "\t" x $level;
1177 $declaration .= "\t" . $clause . "\n";
1178 $level++ if ($clause =~ m/({)/ && !($clause =~m/}/));
1180 output_declaration($declaration_name,
1182 {'struct' => $declaration_name,
1183 'module' => $modulename,
1184 'definition' => $declaration,
1185 'parameterlist' => \@parameterlist,
1186 'parameterdescs' => \%parameterdescs,
1187 'parametertypes' => \%parametertypes,
1188 'sectionlist' => \@sectionlist,
1189 'sections' => \%sections,
1190 'purpose' => $declaration_purpose,
1191 'type' => $decl_type
1195 print STDERR "${file}:$.: error: Cannot parse struct or union!\n";
1201 sub show_warnings($$) {
1202 my $functype = shift;
1205 return 1 if ($output_selection == OUTPUT_ALL);
1207 if ($output_selection == OUTPUT_EXPORTED) {
1208 if (defined($function_table{$name})) {
1214 if ($output_selection == OUTPUT_INTERNAL) {
1215 if (!($functype eq "function" && defined($function_table{$name}))) {
1221 if ($output_selection == OUTPUT_INCLUDE) {
1222 if (defined($function_table{$name})) {
1228 if ($output_selection == OUTPUT_EXCLUDE) {
1229 if (!defined($function_table{$name})) {
1235 die("Please add the new output type at show_warnings()");
1242 $x =~ s@/\*.*?\*/@@gos; # strip comments.
1243 # strip #define macros inside enums
1244 $x =~ s@#\s*((define|ifdef)\s+|endif)[^;]*;@@gos;
1246 if ($x =~ /enum\s+(\w+)\s*{(.*)}/) {
1247 $declaration_name = $1;
1251 $members =~ s/\s+$//;
1253 foreach my $arg (split ',', $members) {
1254 $arg =~ s/^\s*(\w+).*/$1/;
1255 push @parameterlist, $arg;
1256 if (!$parameterdescs{$arg}) {
1257 $parameterdescs{$arg} = $undescribed;
1258 if (show_warnings("enum", $declaration_name)) {
1259 print STDERR "${file}:$.: warning: Enum value '$arg' not described in enum '$declaration_name'\n";
1262 $_members{$arg} = 1;
1265 while (my ($k, $v) = each %parameterdescs) {
1266 if (!exists($_members{$k})) {
1267 if (show_warnings("enum", $declaration_name)) {
1268 print STDERR "${file}:$.: warning: Excess enum value '$k' description in '$declaration_name'\n";
1273 output_declaration($declaration_name,
1275 {'enum' => $declaration_name,
1276 'module' => $modulename,
1277 'parameterlist' => \@parameterlist,
1278 'parameterdescs' => \%parameterdescs,
1279 'sectionlist' => \@sectionlist,
1280 'sections' => \%sections,
1281 'purpose' => $declaration_purpose
1285 print STDERR "${file}:$.: error: Cannot parse enum!\n";
1290 sub dump_typedef($$) {
1294 $x =~ s@/\*.*?\*/@@gos; # strip comments.
1296 # Parse function prototypes
1297 if ($x =~ /typedef\s+(\w+)\s*\(\*\s*(\w\S+)\s*\)\s*\((.*)\);/ ||
1298 $x =~ /typedef\s+(\w+)\s*(\w\S+)\s*\s*\((.*)\);/) {
1302 $declaration_name = $2;
1305 create_parameterlist($args, ',', $file, $declaration_name);
1307 output_declaration($declaration_name,
1309 {'function' => $declaration_name,
1311 'module' => $modulename,
1312 'functiontype' => $return_type,
1313 'parameterlist' => \@parameterlist,
1314 'parameterdescs' => \%parameterdescs,
1315 'parametertypes' => \%parametertypes,
1316 'sectionlist' => \@sectionlist,
1317 'sections' => \%sections,
1318 'purpose' => $declaration_purpose
1323 while (($x =~ /\(*.\)\s*;$/) || ($x =~ /\[*.\]\s*;$/)) {
1324 $x =~ s/\(*.\)\s*;$/;/;
1325 $x =~ s/\[*.\]\s*;$/;/;
1328 if ($x =~ /typedef.*\s+(\w+)\s*;/) {
1329 $declaration_name = $1;
1331 output_declaration($declaration_name,
1333 {'typedef' => $declaration_name,
1334 'module' => $modulename,
1335 'sectionlist' => \@sectionlist,
1336 'sections' => \%sections,
1337 'purpose' => $declaration_purpose
1341 print STDERR "${file}:$.: error: Cannot parse typedef!\n";
1346 sub save_struct_actual($) {
1349 # strip all spaces from the actual param so that it looks like one string item
1350 $actual =~ s/\s*//g;
1351 $struct_actual = $struct_actual . $actual . " ";
1354 sub create_parameterlist($$$$) {
1356 my $splitter = shift;
1358 my $declaration_name = shift;
1362 # temporarily replace commas inside function pointer definition
1363 while ($args =~ /(\([^\),]+),/) {
1364 $args =~ s/(\([^\),]+),/$1#/g;
1367 foreach my $arg (split($splitter, $args)) {
1369 $arg =~ s/\/\*.*\*\///;
1370 # strip leading/trailing spaces
1376 # Treat preprocessor directive as a typeless variable just to fill
1377 # corresponding data structures "correctly". Catch it later in
1379 push_parameter($arg, "", $file);
1380 } elsif ($arg =~ m/\(.+\)\s*\(/) {
1381 # pointer-to-function
1383 $arg =~ m/[^\(]+\(\*?\s*([\w\.]*)\s*\)/;
1386 $type =~ s/([^\(]+\(\*?)\s*$param/$1/;
1387 save_struct_actual($param);
1388 push_parameter($param, $type, $file, $declaration_name);
1390 $arg =~ s/\s*:\s*/:/g;
1391 $arg =~ s/\s*\[/\[/g;
1393 my @args = split('\s*,\s*', $arg);
1394 if ($args[0] =~ m/\*/) {
1395 $args[0] =~ s/(\*+)\s*/ $1/;
1399 if ($args[0] =~ /^(.*\s+)(.*?\[.*\].*)$/) {
1401 push(@first_arg, split('\s+', $1));
1402 push(@first_arg, $2);
1404 @first_arg = split('\s+', shift @args);
1407 unshift(@args, pop @first_arg);
1408 $type = join " ", @first_arg;
1410 foreach $param (@args) {
1411 if ($param =~ m/^(\*+)\s*(.*)/) {
1412 save_struct_actual($2);
1413 push_parameter($2, "$type $1", $file, $declaration_name);
1415 elsif ($param =~ m/(.*?):(\d+)/) {
1416 if ($type ne "") { # skip unnamed bit-fields
1417 save_struct_actual($1);
1418 push_parameter($1, "$type:$2", $file, $declaration_name)
1422 save_struct_actual($param);
1423 push_parameter($param, $type, $file, $declaration_name);
1430 sub push_parameter($$$$) {
1434 my $declaration_name = shift;
1436 if (($anon_struct_union == 1) && ($type eq "") &&
1438 return; # ignore the ending }; from anon. struct/union
1441 $anon_struct_union = 0;
1442 $param =~ s/[\[\)].*//;
1444 if ($type eq "" && $param =~ /\.\.\.$/)
1446 if (!$param =~ /\w\.\.\.$/) {
1447 # handles unnamed variable parameters
1450 if (!defined $parameterdescs{$param} || $parameterdescs{$param} eq "") {
1451 $parameterdescs{$param} = "variable arguments";
1454 elsif ($type eq "" && ($param eq "" or $param eq "void"))
1457 $parameterdescs{void} = "no arguments";
1459 elsif ($type eq "" && ($param eq "struct" or $param eq "union"))
1460 # handle unnamed (anonymous) union or struct:
1463 $param = "{unnamed_" . $param . "}";
1464 $parameterdescs{$param} = "anonymous\n";
1465 $anon_struct_union = 1;
1468 # warn if parameter has no description
1469 # (but ignore ones starting with # as these are not parameters
1470 # but inline preprocessor statements);
1471 # Note: It will also ignore void params and unnamed structs/unions
1472 if (!defined $parameterdescs{$param} && $param !~ /^#/) {
1473 $parameterdescs{$param} = $undescribed;
1475 if (show_warnings($type, $declaration_name)) {
1477 "${file}:$.: warning: Function parameter or member '$param' not described in '$declaration_name'\n";
1482 # strip spaces from $param so that it is one continuous string
1483 # on @parameterlist;
1484 # this fixes a problem where check_sections() cannot find
1485 # a parameter like "addr[6 + 2]" because it actually appears
1486 # as "addr[6", "+", "2]" on the parameter list;
1487 # but it's better to maintain the param string unchanged for output,
1488 # so just weaken the string compare in check_sections() to ignore
1489 # "[blah" in a parameter string;
1490 ###$param =~ s/\s*//g;
1491 push @parameterlist, $param;
1492 $type =~ s/\s\s+/ /g;
1493 $parametertypes{$param} = $type;
1496 sub check_sections($$$$$) {
1497 my ($file, $decl_name, $decl_type, $sectcheck, $prmscheck) = @_;
1498 my @sects = split ' ', $sectcheck;
1499 my @prms = split ' ', $prmscheck;
1502 my $prm_clean; # strip trailing "[array size]" and/or beginning "*"
1504 foreach $sx (0 .. $#sects) {
1506 foreach $px (0 .. $#prms) {
1507 $prm_clean = $prms[$px];
1508 $prm_clean =~ s/\[.*\]//;
1509 $prm_clean =~ s/__attribute__\s*\(\([a-z,_\*\s\(\)]*\)\)//i;
1510 # ignore array size in a parameter string;
1511 # however, the original param string may contain
1512 # spaces, e.g.: addr[6 + 2]
1513 # and this appears in @prms as "addr[6" since the
1514 # parameter list is split at spaces;
1515 # hence just ignore "[..." for the sections check;
1516 $prm_clean =~ s/\[.*//;
1518 ##$prm_clean =~ s/^\**//;
1519 if ($prm_clean eq $sects[$sx]) {
1525 if ($decl_type eq "function") {
1526 print STDERR "${file}:$.: warning: " .
1527 "Excess function parameter " .
1529 "description in '$decl_name'\n";
1537 # Checks the section describing the return value of a function.
1538 sub check_return_section {
1540 my $declaration_name = shift;
1541 my $return_type = shift;
1543 # Ignore an empty return type (It's a macro)
1544 # Ignore functions with a "void" return type. (But don't ignore "void *")
1545 if (($return_type eq "") || ($return_type =~ /void\s*\w*\s*$/)) {
1549 if (!defined($sections{$section_return}) ||
1550 $sections{$section_return} eq "") {
1551 print STDERR "${file}:$.: warning: " .
1552 "No description found for return value of " .
1553 "'$declaration_name'\n";
1559 # takes a function prototype and the name of the current file being
1560 # processed and spits out all the details stored in the global
1562 sub dump_function($$) {
1563 my $prototype = shift;
1567 $prototype =~ s/^static +//;
1568 $prototype =~ s/^extern +//;
1569 $prototype =~ s/^asmlinkage +//;
1570 $prototype =~ s/^inline +//;
1571 $prototype =~ s/^__inline__ +//;
1572 $prototype =~ s/^__inline +//;
1573 $prototype =~ s/^__always_inline +//;
1574 $prototype =~ s/^noinline +//;
1575 $prototype =~ s/__init +//;
1576 $prototype =~ s/__init_or_module +//;
1577 $prototype =~ s/__meminit +//;
1578 $prototype =~ s/__must_check +//;
1579 $prototype =~ s/__weak +//;
1580 my $define = $prototype =~ s/^#\s*define\s+//; #ak added
1581 $prototype =~ s/__attribute__\s*\(\(
1583 [\w\s]++ # attribute name
1584 (?:\([^)]*+\))? # attribute arguments
1585 \s*+,? # optional comma at the end
1589 # Yes, this truly is vile. We are looking for:
1590 # 1. Return type (may be nothing if we're looking at a macro)
1592 # 3. Function parameters.
1594 # All the while we have to watch out for function pointer parameters
1595 # (which IIRC is what the two sections are for), C types (these
1596 # regexps don't even start to express all the possibilities), and
1599 # If you mess with these regexps, it's a good idea to check that
1600 # the following functions' documentation still comes out right:
1601 # - parport_register_device (function pointer parameters)
1602 # - atomic_set (macro)
1603 # - pci_match_device, __copy_to_user (long return type)
1605 if ($define && $prototype =~ m/^()([a-zA-Z0-9_~:]+)\s+/) {
1606 # This is an object-like macro, it has no return type and no parameter
1608 # Function-like macros are not allowed to have spaces between
1609 # declaration_name and opening parenthesis (notice the \s+).
1611 $declaration_name = $2;
1613 } elsif ($prototype =~ m/^()([a-zA-Z0-9_~:]+)\s*\(([^\(]*)\)/ ||
1614 $prototype =~ m/^(\w+)\s+([a-zA-Z0-9_~:]+)\s*\(([^\(]*)\)/ ||
1615 $prototype =~ m/^(\w+\s*\*+)\s*([a-zA-Z0-9_~:]+)\s*\(([^\(]*)\)/ ||
1616 $prototype =~ m/^(\w+\s+\w+)\s+([a-zA-Z0-9_~:]+)\s*\(([^\(]*)\)/ ||
1617 $prototype =~ m/^(\w+\s+\w+\s*\*+)\s*([a-zA-Z0-9_~:]+)\s*\(([^\(]*)\)/ ||
1618 $prototype =~ m/^(\w+\s+\w+\s+\w+)\s+([a-zA-Z0-9_~:]+)\s*\(([^\(]*)\)/ ||
1619 $prototype =~ m/^(\w+\s+\w+\s+\w+\s*\*+)\s*([a-zA-Z0-9_~:]+)\s*\(([^\(]*)\)/ ||
1620 $prototype =~ m/^()([a-zA-Z0-9_~:]+)\s*\(([^\{]*)\)/ ||
1621 $prototype =~ m/^(\w+)\s+([a-zA-Z0-9_~:]+)\s*\(([^\{]*)\)/ ||
1622 $prototype =~ m/^(\w+\s*\*+)\s*([a-zA-Z0-9_~:]+)\s*\(([^\{]*)\)/ ||
1623 $prototype =~ m/^(\w+\s+\w+)\s+([a-zA-Z0-9_~:]+)\s*\(([^\{]*)\)/ ||
1624 $prototype =~ m/^(\w+\s+\w+\s*\*+)\s*([a-zA-Z0-9_~:]+)\s*\(([^\{]*)\)/ ||
1625 $prototype =~ m/^(\w+\s+\w+\s+\w+)\s+([a-zA-Z0-9_~:]+)\s*\(([^\{]*)\)/ ||
1626 $prototype =~ m/^(\w+\s+\w+\s+\w+\s*\*+)\s*([a-zA-Z0-9_~:]+)\s*\(([^\{]*)\)/ ||
1627 $prototype =~ m/^(\w+\s+\w+\s+\w+\s+\w+)\s+([a-zA-Z0-9_~:]+)\s*\(([^\{]*)\)/ ||
1628 $prototype =~ m/^(\w+\s+\w+\s+\w+\s+\w+\s*\*+)\s*([a-zA-Z0-9_~:]+)\s*\(([^\{]*)\)/ ||
1629 $prototype =~ m/^(\w+\s+\w+\s*\*+\s*\w+\s*\*+\s*)\s*([a-zA-Z0-9_~:]+)\s*\(([^\{]*)\)/) {
1631 $declaration_name = $2;
1634 create_parameterlist($args, ',', $file, $declaration_name);
1636 print STDERR "${file}:$.: warning: cannot understand function prototype: '$prototype'\n";
1640 my $prms = join " ", @parameterlist;
1641 check_sections($file, $declaration_name, "function", $sectcheck, $prms);
1643 # This check emits a lot of warnings at the moment, because many
1644 # functions don't have a 'Return' doc section. So until the number
1645 # of warnings goes sufficiently down, the check is only performed in
1647 # TODO: always perform the check.
1648 if ($verbose && !$noret) {
1649 check_return_section($file, $declaration_name, $return_type);
1652 output_declaration($declaration_name,
1654 {'function' => $declaration_name,
1655 'module' => $modulename,
1656 'functiontype' => $return_type,
1657 'parameterlist' => \@parameterlist,
1658 'parameterdescs' => \%parameterdescs,
1659 'parametertypes' => \%parametertypes,
1660 'sectionlist' => \@sectionlist,
1661 'sections' => \%sections,
1662 'purpose' => $declaration_purpose
1668 %parameterdescs = ();
1669 %parametertypes = ();
1670 @parameterlist = ();
1674 $struct_actual = "";
1677 $state = STATE_NORMAL;
1678 $inline_doc_state = STATE_INLINE_NA;
1681 sub tracepoint_munge($) {
1683 my $tracepointname = 0;
1684 my $tracepointargs = 0;
1686 if ($prototype =~ m/TRACE_EVENT\((.*?),/) {
1687 $tracepointname = $1;
1689 if ($prototype =~ m/DEFINE_SINGLE_EVENT\((.*?),/) {
1690 $tracepointname = $1;
1692 if ($prototype =~ m/DEFINE_EVENT\((.*?),(.*?),/) {
1693 $tracepointname = $2;
1695 $tracepointname =~ s/^\s+//; #strip leading whitespace
1696 if ($prototype =~ m/TP_PROTO\((.*?)\)/) {
1697 $tracepointargs = $1;
1699 if (($tracepointname eq 0) || ($tracepointargs eq 0)) {
1700 print STDERR "${file}:$.: warning: Unrecognized tracepoint format: \n".
1703 $prototype = "static inline void trace_$tracepointname($tracepointargs)";
1707 sub syscall_munge() {
1710 $prototype =~ s@[\r\n]+@ @gos; # strip newlines/CR's
1711 ## if ($prototype =~ m/SYSCALL_DEFINE0\s*\(\s*(a-zA-Z0-9_)*\s*\)/) {
1712 if ($prototype =~ m/SYSCALL_DEFINE0/) {
1714 ## $prototype = "long sys_$1(void)";
1717 $prototype =~ s/SYSCALL_DEFINE.*\(/long sys_/; # fix return type & func name
1718 if ($prototype =~ m/long (sys_.*?),/) {
1719 $prototype =~ s/,/\(/;
1721 $prototype =~ s/\)/\(void\)/;
1724 # now delete all of the odd-number commas in $prototype
1725 # so that arg types & arg names don't have a comma between them
1727 my $len = length($prototype);
1729 $len = 0; # skip the for-loop
1731 for (my $ix = 0; $ix < $len; $ix++) {
1732 if (substr($prototype, $ix, 1) eq ',') {
1734 if ($count % 2 == 1) {
1735 substr($prototype, $ix, 1) = ' ';
1741 sub process_proto_function($$) {
1745 $x =~ s@\/\/.*$@@gos; # strip C99-style comments to end of line
1747 if ($x =~ m#\s*/\*\s+MACDOC\s*#io || ($x =~ /^#/ && $x !~ /^#\s*define/)) {
1750 elsif ($x =~ /([^\{]*)/) {
1754 if (($x =~ /\{/) || ($x =~ /\#\s*define/) || ($x =~ /;/)) {
1755 $prototype =~ s@/\*.*?\*/@@gos; # strip comments.
1756 $prototype =~ s@[\r\n]+@ @gos; # strip newlines/cr's.
1757 $prototype =~ s@^\s+@@gos; # strip leading spaces
1758 if ($prototype =~ /SYSCALL_DEFINE/) {
1761 if ($prototype =~ /TRACE_EVENT/ || $prototype =~ /DEFINE_EVENT/ ||
1762 $prototype =~ /DEFINE_SINGLE_EVENT/)
1764 tracepoint_munge($file);
1766 dump_function($prototype, $file);
1771 sub process_proto_type($$) {
1775 $x =~ s@[\r\n]+@ @gos; # strip newlines/cr's.
1776 $x =~ s@^\s+@@gos; # strip leading spaces
1777 $x =~ s@\s+$@@gos; # strip trailing spaces
1778 $x =~ s@\/\/.*$@@gos; # strip C99-style comments to end of line
1781 # To distinguish preprocessor directive from regular declaration later.
1786 if ( $x =~ /([^{};]*)([{};])(.*)/ ) {
1787 if( length $prototype ) {
1790 $prototype .= $1 . $2;
1791 ($2 eq '{') && $brcount++;
1792 ($2 eq '}') && $brcount--;
1793 if (($2 eq ';') && ($brcount == 0)) {
1794 dump_declaration($prototype, $file);
1807 sub map_filename($) {
1809 my ($orig_file) = @_;
1811 if (defined($ENV{'SRCTREE'})) {
1812 $file = "$ENV{'SRCTREE'}" . "/" . $orig_file;
1817 if (defined($source_map{$file})) {
1818 $file = $source_map{$file};
1824 sub process_export_file($) {
1825 my ($orig_file) = @_;
1826 my $file = map_filename($orig_file);
1828 if (!open(IN,"<$file")) {
1829 print STDERR "Error: Cannot open file $file\n";
1835 if (/$export_symbol/) {
1836 $function_table{$2} = 1;
1844 # Parsers for the various processing states.
1846 # STATE_NORMAL: looking for the /** to begin everything.
1848 sub process_normal() {
1849 if (/$doc_start/o) {
1850 $state = STATE_NAME; # next line is always the function name
1852 $declaration_start_line = $. + 1;
1857 # STATE_NAME: Looking for the "name - description" line
1859 sub process_name($$) {
1864 if (/$doc_block/o) {
1865 $state = STATE_DOCBLOCK;
1867 $new_start_line = $. + 1;
1870 $section = $section_intro;
1875 elsif (/$doc_decl/o) {
1877 if (/\s*([\w\s]+?)\s*-/) {
1881 $state = STATE_BODY;
1882 # if there's no @param blocks need to set up default section
1885 $section = $section_default;
1886 $new_start_line = $. + 1;
1888 # strip leading/trailing/multiple spaces
1892 $descr =~ s/\s+/ /g;
1893 $declaration_purpose = $descr;
1894 $state = STATE_BODY_MAYBE;
1896 $declaration_purpose = "";
1899 if (($declaration_purpose eq "") && $verbose) {
1900 print STDERR "${file}:$.: warning: missing initial short description on line:\n";
1905 if ($identifier =~ m/^struct/) {
1906 $decl_type = 'struct';
1907 } elsif ($identifier =~ m/^union/) {
1908 $decl_type = 'union';
1909 } elsif ($identifier =~ m/^enum/) {
1910 $decl_type = 'enum';
1911 } elsif ($identifier =~ m/^typedef/) {
1912 $decl_type = 'typedef';
1914 $decl_type = 'function';
1918 print STDERR "${file}:$.: info: Scanning doc for $identifier\n";
1921 print STDERR "${file}:$.: warning: Cannot understand $_ on line $.",
1922 " - I thought it was a doc line\n";
1924 $state = STATE_NORMAL;
1930 # STATE_BODY and STATE_BODY_MAYBE: the bulk of a kerneldoc comment.
1932 sub process_body($$) {
1935 if (/$doc_sect/i) { # case insensitive for supported section names
1939 # map the supported section names to the canonical names
1940 if ($newsection =~ m/^description$/i) {
1941 $newsection = $section_default;
1942 } elsif ($newsection =~ m/^context$/i) {
1943 $newsection = $section_context;
1944 } elsif ($newsection =~ m/^returns?$/i) {
1945 $newsection = $section_return;
1946 } elsif ($newsection =~ m/^\@return$/) {
1947 # special: @return is a section, not a param description
1948 $newsection = $section_return;
1951 if (($contents ne "") && ($contents ne "\n")) {
1952 if (!$in_doc_sect && $verbose) {
1953 print STDERR "${file}:$.: warning: contents before sections\n";
1956 dump_section($file, $section, $contents);
1957 $section = $section_default;
1961 $state = STATE_BODY;
1962 $contents = $newcontents;
1963 $new_start_line = $.;
1964 while (substr($contents, 0, 1) eq " ") {
1965 $contents = substr($contents, 1);
1967 if ($contents ne "") {
1970 $section = $newsection;
1971 $leading_space = undef;
1972 } elsif (/$doc_end/) {
1973 if (($contents ne "") && ($contents ne "\n")) {
1974 dump_section($file, $section, $contents);
1975 $section = $section_default;
1978 # look for doc_com + <text> + doc_end:
1979 if ($_ =~ m'\s*\*\s*[a-zA-Z_0-9:\.]+\*/') {
1980 print STDERR "${file}:$.: warning: suspicious ending line: $_";
1985 $state = STATE_PROTO;
1987 } elsif (/$doc_content/) {
1988 # miguel-style comment kludge, look for blank lines after
1989 # @parameter line to signify start of description
1991 if ($section =~ m/^@/ || $section eq $section_context) {
1992 dump_section($file, $section, $contents);
1993 $section = $section_default;
1995 $new_start_line = $.;
1999 $state = STATE_BODY;
2000 } elsif ($state == STATE_BODY_MAYBE) {
2001 # Continued declaration purpose
2002 chomp($declaration_purpose);
2003 $declaration_purpose .= " " . $1;
2004 $declaration_purpose =~ s/\s+/ /g;
2007 if ($section =~ m/^@/ || $section eq $section_context) {
2008 if (!defined $leading_space) {
2009 if ($cont =~ m/^(\s+)/) {
2010 $leading_space = $1;
2012 $leading_space = "";
2015 $cont =~ s/^$leading_space//;
2017 $contents .= $cont . "\n";
2020 # i dont know - bad line? ignore.
2021 print STDERR "${file}:$.: warning: bad line: $_";
2028 # STATE_PROTO: reading a function/whatever prototype.
2030 sub process_proto($$) {
2033 if (/$doc_inline_oneline/) {
2036 if ($contents ne "") {
2038 dump_section($file, $section, $contents);
2039 $section = $section_default;
2042 } elsif (/$doc_inline_start/) {
2043 $state = STATE_INLINE;
2044 $inline_doc_state = STATE_INLINE_NAME;
2045 } elsif ($decl_type eq 'function') {
2046 process_proto_function($_, $file);
2048 process_proto_type($_, $file);
2053 # STATE_DOCBLOCK: within a DOC: block.
2055 sub process_docblock($$) {
2059 dump_doc_section($file, $section, $contents);
2060 $section = $section_default;
2063 %parameterdescs = ();
2064 %parametertypes = ();
2065 @parameterlist = ();
2069 $state = STATE_NORMAL;
2070 } elsif (/$doc_content/) {
2072 $contents .= $blankline;
2074 $contents .= $1 . "\n";
2080 # STATE_INLINE: docbook comments within a prototype.
2082 sub process_inline($$) {
2085 # First line (state 1) needs to be a @parameter
2086 if ($inline_doc_state == STATE_INLINE_NAME && /$doc_inline_sect/o) {
2089 $new_start_line = $.;
2090 if ($contents ne "") {
2091 while (substr($contents, 0, 1) eq " ") {
2092 $contents = substr($contents, 1);
2096 $inline_doc_state = STATE_INLINE_TEXT;
2097 # Documentation block end */
2098 } elsif (/$doc_inline_end/) {
2099 if (($contents ne "") && ($contents ne "\n")) {
2100 dump_section($file, $section, $contents);
2101 $section = $section_default;
2104 $state = STATE_PROTO;
2105 $inline_doc_state = STATE_INLINE_NA;
2107 } elsif (/$doc_content/) {
2108 if ($inline_doc_state == STATE_INLINE_TEXT) {
2109 $contents .= $1 . "\n";
2110 # nuke leading blank lines
2111 if ($contents =~ /^\s*$/) {
2114 } elsif ($inline_doc_state == STATE_INLINE_NAME) {
2115 $inline_doc_state = STATE_INLINE_ERROR;
2116 print STDERR "${file}:$.: warning: ";
2117 print STDERR "Incorrect use of kernel-doc format: $_";
2124 sub process_file($) {
2126 my $initial_section_counter = $section_counter;
2127 my ($orig_file) = @_;
2129 $file = map_filename($orig_file);
2131 if (!open(IN,"<$file")) {
2132 print STDERR "Error: Cannot open file $file\n";
2139 $section_counter = 0;
2141 while (s/\\\s*$//) {
2144 # Replace tabs by spaces
2145 while ($_ =~ s/\t+/' ' x (length($&) * 8 - length($`) % 8)/e) {};
2147 # Hand this line to the appropriate state handler
2148 if ($state == STATE_NORMAL) {
2150 } elsif ($state == STATE_NAME) {
2151 process_name($file, $_);
2152 } elsif ($state == STATE_BODY || $state == STATE_BODY_MAYBE) {
2153 process_body($file, $_);
2154 } elsif ($state == STATE_INLINE) { # scanning for inline parameters
2155 process_inline($file, $_);
2156 } elsif ($state == STATE_PROTO) {
2157 process_proto($file, $_);
2158 } elsif ($state == STATE_DOCBLOCK) {
2159 process_docblock($file, $_);
2163 # Make sure we got something interesting.
2164 if ($initial_section_counter == $section_counter) {
2165 if ($output_mode ne "none") {
2166 print STDERR "${file}:1: warning: no structured comments found\n";
2168 if (($output_selection == OUTPUT_INCLUDE) && ($show_not_found == 1)) {
2169 print STDERR " Was looking for '$_'.\n" for keys %function_table;
2175 $kernelversion = get_kernel_version();
2177 # generate a sequence of code that will splice in highlighting information
2178 # using the s// operator.
2179 for (my $k = 0; $k < @highlights; $k++) {
2180 my $pattern = $highlights[$k][0];
2181 my $result = $highlights[$k][1];
2182 # print STDERR "scanning pattern:$pattern, highlight:($result)\n";
2183 $dohighlight .= "\$contents =~ s:$pattern:$result:gs;\n";
2186 # Read the file that maps relative names to absolute names for
2187 # separate source and object directories and for shadow trees.
2188 if (open(SOURCE_MAP, "<.tmp_filelist.txt")) {
2189 my ($relname, $absname);
2190 while(<SOURCE_MAP>) {
2192 ($relname, $absname) = (split())[0..1];
2193 $relname =~ s:^/+::;
2194 $source_map{$relname} = $absname;
2199 if ($output_selection == OUTPUT_EXPORTED ||
2200 $output_selection == OUTPUT_INTERNAL) {
2202 push(@export_file_list, @ARGV);
2204 foreach (@export_file_list) {
2206 process_export_file($_);
2214 if ($verbose && $errors) {
2215 print STDERR "$errors errors\n";
2217 if ($verbose && $warnings) {
2218 print STDERR "$warnings warnings\n";
2221 exit($output_mode eq "none" ? 0 : $errors);