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_FIELD => 2, # scanning field start
332 STATE_PROTO => 3, # scanning prototype
333 STATE_DOCBLOCK => 4, # documentation block
334 STATE_INLINE => 5, # gathering documentation outside main block
339 # Inline documentation state
341 STATE_INLINE_NA => 0, # not applicable ($state != STATE_INLINE)
342 STATE_INLINE_NAME => 1, # looking for member name (@foo:)
343 STATE_INLINE_TEXT => 2, # looking for member documentation
344 STATE_INLINE_END => 3, # done
345 STATE_INLINE_ERROR => 4, # error - Comment without header was found.
346 # Spit a warning as it's not
347 # proper kernel-doc and ignore the rest.
349 my $inline_doc_state;
351 #declaration types: can be
352 # 'function', 'struct', 'union', 'enum', 'typedef'
355 my $doc_start = '^/\*\*\s*$'; # Allow whitespace at end of comment start.
357 my $doc_com = '\s*\*\s*';
358 my $doc_com_body = '\s*\* ?';
359 my $doc_decl = $doc_com . '(\w+)';
360 # @params and a strictly limited set of supported section names
361 my $doc_sect = $doc_com .
362 '\s*(\@[.\w]+|\@\.\.\.|description|context|returns?|notes?|examples?)\s*:(.*)';
363 my $doc_content = $doc_com_body . '(.*)';
364 my $doc_block = $doc_com . 'DOC:\s*(.*)?';
365 my $doc_inline_start = '^\s*/\*\*\s*$';
366 my $doc_inline_sect = '\s*\*\s*(@[\w\s]+):(.*)';
367 my $doc_inline_end = '^\s*\*/\s*$';
368 my $doc_inline_oneline = '^\s*/\*\*\s*(@[\w\s]+):\s*(.*)\s*\*/\s*$';
369 my $export_symbol = '^\s*EXPORT_SYMBOL(_GPL)?\s*\(\s*(\w+)\s*\)\s*;';
372 my %parameterdesc_start_lines;
376 my %section_start_lines;
381 my $new_start_line = 0;
383 # the canonical section names. see also $doc_sect above.
384 my $section_default = "Description"; # default section
385 my $section_intro = "Introduction";
386 my $section = $section_default;
387 my $section_context = "Context";
388 my $section_return = "Return";
390 my $undescribed = "-- undescribed --";
394 while ($ARGV[0] =~ m/^--?(.*)/) {
398 $output_mode = "man";
399 @highlights = @highlights_man;
400 $blankline = $blankline_man;
401 } elsif ($cmd eq "rst") {
402 $output_mode = "rst";
403 @highlights = @highlights_rst;
404 $blankline = $blankline_rst;
405 } elsif ($cmd eq "none") {
406 $output_mode = "none";
407 } elsif ($cmd eq "module") { # not needed for XML, inherits from calling document
408 $modulename = shift @ARGV;
409 } elsif ($cmd eq "function") { # to only output specific functions
410 $output_selection = OUTPUT_INCLUDE;
411 $function = shift @ARGV;
412 $function_table{$function} = 1;
413 } elsif ($cmd eq "nofunction") { # output all except specific functions
414 $output_selection = OUTPUT_EXCLUDE;
415 $function = shift @ARGV;
416 $function_table{$function} = 1;
417 } elsif ($cmd eq "export") { # only exported symbols
418 $output_selection = OUTPUT_EXPORTED;
419 %function_table = ();
420 } elsif ($cmd eq "internal") { # only non-exported symbols
421 $output_selection = OUTPUT_INTERNAL;
422 %function_table = ();
423 } elsif ($cmd eq "export-file") {
424 my $file = shift @ARGV;
425 push(@export_file_list, $file);
426 } elsif ($cmd eq "v") {
428 } elsif (($cmd eq "h") || ($cmd eq "help")) {
430 } elsif ($cmd eq 'no-doc-sections') {
431 $no_doc_sections = 1;
432 } elsif ($cmd eq 'enable-lineno') {
434 } elsif ($cmd eq 'show-not-found') {
442 # continue execution near EOF;
444 # get kernel version from env
445 sub get_kernel_version() {
446 my $version = 'unknown kernel version';
448 if (defined($ENV{'KERNELVERSION'})) {
449 $version = $ENV{'KERNELVERSION'};
457 if ($enable_lineno && defined($lineno)) {
458 print "#define LINENO " . $lineno . "\n";
462 # dumps section contents to arrays/hashes intended for that purpose.
467 my $contents = join "\n", @_;
469 if ($name =~ m/$type_param/) {
471 $parameterdescs{$name} = $contents;
472 $sectcheck = $sectcheck . $name . " ";
473 $parameterdesc_start_lines{$name} = $new_start_line;
475 } elsif ($name eq "@\.\.\.") {
477 $parameterdescs{$name} = $contents;
478 $sectcheck = $sectcheck . $name . " ";
479 $parameterdesc_start_lines{$name} = $new_start_line;
482 if (defined($sections{$name}) && ($sections{$name} ne "")) {
483 # Only warn on user specified duplicate section names.
484 if ($name ne $section_default) {
485 print STDERR "${file}:$.: warning: duplicate section name '$name'\n";
488 $sections{$name} .= $contents;
490 $sections{$name} = $contents;
491 push @sectionlist, $name;
492 $section_start_lines{$name} = $new_start_line;
499 # dump DOC: section after checking that it should go out
501 sub dump_doc_section {
504 my $contents = join "\n", @_;
506 if ($no_doc_sections) {
510 if (($output_selection == OUTPUT_ALL) ||
511 ($output_selection == OUTPUT_INCLUDE &&
512 defined($function_table{$name})) ||
513 ($output_selection == OUTPUT_EXCLUDE &&
514 !defined($function_table{$name})))
516 dump_section($file, $name, $contents);
517 output_blockhead({'sectionlist' => \@sectionlist,
518 'sections' => \%sections,
519 'module' => $modulename,
520 'content-only' => ($output_selection != OUTPUT_ALL), });
527 # parameterdescs, a hash.
528 # function => "function name"
529 # parameterlist => @list of parameters
530 # parameterdescs => %parameter descriptions
531 # sectionlist => @list of sections
532 # sections => %section descriptions
535 sub output_highlight {
536 my $contents = join "\n",@_;
540 # if (!defined $contents) {
542 # confess "output_highlight got called with no args?\n";
545 # print STDERR "contents b4:$contents\n";
548 # print STDERR "contents af:$contents\n";
550 foreach $line (split "\n", $contents) {
551 if (! $output_preformatted) {
555 if (! $output_preformatted) {
556 print $lineprefix, local_unescape($blankline);
559 $line =~ s/\\\\\\/\&/g;
560 if ($output_mode eq "man" && substr($line, 0, 1) eq ".") {
563 print $lineprefix, $line;
571 # output function in man
572 sub output_function_man(%) {
574 my ($parameter, $section);
577 print ".TH \"$args{'function'}\" 9 \"$args{'function'}\" \"$man_date\" \"Kernel Hacker's Manual\" LINUX\n";
580 print $args{'function'} . " \\- " . $args{'purpose'} . "\n";
582 print ".SH SYNOPSIS\n";
583 if ($args{'functiontype'} ne "") {
584 print ".B \"" . $args{'functiontype'} . "\" " . $args{'function'} . "\n";
586 print ".B \"" . $args{'function'} . "\n";
591 foreach my $parameter (@{$args{'parameterlist'}}) {
592 if ($count == $#{$args{'parameterlist'}}) {
595 $type = $args{'parametertypes'}{$parameter};
596 if ($type =~ m/([^\(]*\(\*)\s*\)\s*\(([^\)]*)\)/) {
597 # pointer-to-function
598 print ".BI \"" . $parenth . $1 . "\" " . $parameter . " \") (" . $2 . ")" . $post . "\"\n";
600 $type =~ s/([^\*])$/$1 /;
601 print ".BI \"" . $parenth . $type . "\" " . $parameter . " \"" . $post . "\"\n";
607 print ".SH ARGUMENTS\n";
608 foreach $parameter (@{$args{'parameterlist'}}) {
609 my $parameter_name = $parameter;
610 $parameter_name =~ s/\[.*//;
612 print ".IP \"" . $parameter . "\" 12\n";
613 output_highlight($args{'parameterdescs'}{$parameter_name});
615 foreach $section (@{$args{'sectionlist'}}) {
616 print ".SH \"", uc $section, "\"\n";
617 output_highlight($args{'sections'}{$section});
623 sub output_enum_man(%) {
625 my ($parameter, $section);
628 print ".TH \"$args{'module'}\" 9 \"enum $args{'enum'}\" \"$man_date\" \"API Manual\" LINUX\n";
631 print "enum " . $args{'enum'} . " \\- " . $args{'purpose'} . "\n";
633 print ".SH SYNOPSIS\n";
634 print "enum " . $args{'enum'} . " {\n";
636 foreach my $parameter (@{$args{'parameterlist'}}) {
637 print ".br\n.BI \" $parameter\"\n";
638 if ($count == $#{$args{'parameterlist'}}) {
648 print ".SH Constants\n";
649 foreach $parameter (@{$args{'parameterlist'}}) {
650 my $parameter_name = $parameter;
651 $parameter_name =~ s/\[.*//;
653 print ".IP \"" . $parameter . "\" 12\n";
654 output_highlight($args{'parameterdescs'}{$parameter_name});
656 foreach $section (@{$args{'sectionlist'}}) {
657 print ".SH \"$section\"\n";
658 output_highlight($args{'sections'}{$section});
663 # output struct in man
664 sub output_struct_man(%) {
666 my ($parameter, $section);
668 print ".TH \"$args{'module'}\" 9 \"" . $args{'type'} . " " . $args{'struct'} . "\" \"$man_date\" \"API Manual\" LINUX\n";
671 print $args{'type'} . " " . $args{'struct'} . " \\- " . $args{'purpose'} . "\n";
673 my $declaration = $args{'definition'};
674 $declaration =~ s/\t/ /g;
675 $declaration =~ s/\n/"\n.br\n.BI \"/g;
676 print ".SH SYNOPSIS\n";
677 print $args{'type'} . " " . $args{'struct'} . " {\n.br\n";
678 print ".BI \"$declaration\n};\n.br\n\n";
680 print ".SH Members\n";
681 foreach $parameter (@{$args{'parameterlist'}}) {
682 ($parameter =~ /^#/) && next;
684 my $parameter_name = $parameter;
685 $parameter_name =~ s/\[.*//;
687 ($args{'parameterdescs'}{$parameter_name} ne $undescribed) || next;
688 print ".IP \"" . $parameter . "\" 12\n";
689 output_highlight($args{'parameterdescs'}{$parameter_name});
691 foreach $section (@{$args{'sectionlist'}}) {
692 print ".SH \"$section\"\n";
693 output_highlight($args{'sections'}{$section});
698 # output typedef in man
699 sub output_typedef_man(%) {
701 my ($parameter, $section);
703 print ".TH \"$args{'module'}\" 9 \"$args{'typedef'}\" \"$man_date\" \"API Manual\" LINUX\n";
706 print "typedef " . $args{'typedef'} . " \\- " . $args{'purpose'} . "\n";
708 foreach $section (@{$args{'sectionlist'}}) {
709 print ".SH \"$section\"\n";
710 output_highlight($args{'sections'}{$section});
714 sub output_blockhead_man(%) {
716 my ($parameter, $section);
719 print ".TH \"$args{'module'}\" 9 \"$args{'module'}\" \"$man_date\" \"API Manual\" LINUX\n";
721 foreach $section (@{$args{'sectionlist'}}) {
722 print ".SH \"$section\"\n";
723 output_highlight($args{'sections'}{$section});
728 # output in restructured text
732 # This could use some work; it's used to output the DOC: sections, and
733 # starts by putting out the name of the doc section itself, but that tends
734 # to duplicate a header already in the template file.
736 sub output_blockhead_rst(%) {
738 my ($parameter, $section);
740 foreach $section (@{$args{'sectionlist'}}) {
741 if ($output_selection != OUTPUT_INCLUDE) {
742 print "**$section**\n\n";
744 print_lineno($section_start_lines{$section});
745 output_highlight_rst($args{'sections'}{$section});
750 sub output_highlight_rst {
751 my $contents = join "\n",@_;
754 # undo the evil effects of xml_escape() earlier
755 $contents = xml_unescape($contents);
760 foreach $line (split "\n", $contents) {
761 print $lineprefix . $line . "\n";
765 sub output_function_rst(%) {
767 my ($parameter, $section);
768 my $oldprefix = $lineprefix;
771 if ($args{'typedef'}) {
772 print ".. c:type:: ". $args{'function'} . "\n\n";
773 print_lineno($declaration_start_line);
774 print " **Typedef**: ";
776 output_highlight_rst($args{'purpose'});
777 $start = "\n\n**Syntax**\n\n ``";
779 print ".. c:function:: ";
781 if ($args{'functiontype'} ne "") {
782 $start .= $args{'functiontype'} . " " . $args{'function'} . " (";
784 $start .= $args{'function'} . " (";
789 foreach my $parameter (@{$args{'parameterlist'}}) {
794 $type = $args{'parametertypes'}{$parameter};
796 if ($type =~ m/([^\(]*\(\*)\s*\)\s*\(([^\)]*)\)/) {
797 # pointer-to-function
798 print $1 . $parameter . ") (" . $2;
800 print $type . " " . $parameter;
803 if ($args{'typedef'}) {
807 print_lineno($declaration_start_line);
809 output_highlight_rst($args{'purpose'});
813 print "**Parameters**\n\n";
815 foreach $parameter (@{$args{'parameterlist'}}) {
816 my $parameter_name = $parameter;
817 $parameter_name =~ s/\[.*//;
818 $type = $args{'parametertypes'}{$parameter};
821 print "``$type $parameter``\n";
823 print "``$parameter``\n";
826 print_lineno($parameterdesc_start_lines{$parameter_name});
828 if (defined($args{'parameterdescs'}{$parameter_name}) &&
829 $args{'parameterdescs'}{$parameter_name} ne $undescribed) {
830 output_highlight_rst($args{'parameterdescs'}{$parameter_name});
832 print " *undescribed*\n";
837 $lineprefix = $oldprefix;
838 output_section_rst(@_);
841 sub output_section_rst(%) {
844 my $oldprefix = $lineprefix;
847 foreach $section (@{$args{'sectionlist'}}) {
848 print "**$section**\n\n";
849 print_lineno($section_start_lines{$section});
850 output_highlight_rst($args{'sections'}{$section});
854 $lineprefix = $oldprefix;
857 sub output_enum_rst(%) {
860 my $oldprefix = $lineprefix;
862 my $name = "enum " . $args{'enum'};
864 print "\n\n.. c:type:: " . $name . "\n\n";
865 print_lineno($declaration_start_line);
867 output_highlight_rst($args{'purpose'});
870 print "**Constants**\n\n";
872 foreach $parameter (@{$args{'parameterlist'}}) {
873 print "``$parameter``\n";
874 if ($args{'parameterdescs'}{$parameter} ne $undescribed) {
875 output_highlight_rst($args{'parameterdescs'}{$parameter});
877 print " *undescribed*\n";
882 $lineprefix = $oldprefix;
883 output_section_rst(@_);
886 sub output_typedef_rst(%) {
889 my $oldprefix = $lineprefix;
890 my $name = "typedef " . $args{'typedef'};
892 print "\n\n.. c:type:: " . $name . "\n\n";
893 print_lineno($declaration_start_line);
895 output_highlight_rst($args{'purpose'});
898 $lineprefix = $oldprefix;
899 output_section_rst(@_);
902 sub output_struct_rst(%) {
905 my $oldprefix = $lineprefix;
906 my $name = $args{'type'} . " " . $args{'struct'};
908 print "\n\n.. c:type:: " . $name . "\n\n";
909 print_lineno($declaration_start_line);
911 output_highlight_rst($args{'purpose'});
914 print "**Definition**\n\n";
916 my $declaration = $args{'definition'};
917 $declaration =~ s/\t/ /g;
918 print " " . $args{'type'} . " " . $args{'struct'} . " {\n$declaration };\n\n";
920 print "**Members**\n\n";
922 foreach $parameter (@{$args{'parameterlist'}}) {
923 ($parameter =~ /^#/) && next;
925 my $parameter_name = $parameter;
926 $parameter_name =~ s/\[.*//;
928 ($args{'parameterdescs'}{$parameter_name} ne $undescribed) || next;
929 $type = $args{'parametertypes'}{$parameter};
930 print_lineno($parameterdesc_start_lines{$parameter_name});
931 print "``" . $parameter . "``\n";
932 output_highlight_rst($args{'parameterdescs'}{$parameter_name});
937 $lineprefix = $oldprefix;
938 output_section_rst(@_);
941 ## none mode output functions
943 sub output_function_none(%) {
946 sub output_enum_none(%) {
949 sub output_typedef_none(%) {
952 sub output_struct_none(%) {
955 sub output_blockhead_none(%) {
959 # generic output function for all types (function, struct/union, typedef, enum);
960 # calls the generated, variable output_ function name based on
961 # functype and output_mode
962 sub output_declaration {
965 my $functype = shift;
966 my $func = "output_${functype}_$output_mode";
967 if (($output_selection == OUTPUT_ALL) ||
968 (($output_selection == OUTPUT_INCLUDE ||
969 $output_selection == OUTPUT_EXPORTED) &&
970 defined($function_table{$name})) ||
971 (($output_selection == OUTPUT_EXCLUDE ||
972 $output_selection == OUTPUT_INTERNAL) &&
973 !($functype eq "function" && defined($function_table{$name}))))
981 # generic output function - calls the right one based on current output mode.
982 sub output_blockhead {
984 my $func = "output_blockhead_" . $output_mode;
990 # takes a declaration (struct, union, enum, typedef) and
991 # invokes the right handler. NOT called for functions.
992 sub dump_declaration($$) {
994 my ($prototype, $file) = @_;
995 my $func = "dump_" . $decl_type;
1003 sub dump_struct($$) {
1008 if ($x =~ /(struct|union)\s+(\w+)\s*{(.*)}/) {
1010 $declaration_name = $2;
1013 # ignore members marked private:
1014 $members =~ s/\/\*\s*private:.*?\/\*\s*public:.*?\*\///gosi;
1015 $members =~ s/\/\*\s*private:.*//gosi;
1017 $members =~ s/\/\*.*?\*\///gos;
1019 $members =~ s/__attribute__\s*\(\([a-z,_\*\s\(\)]*\)\)//i;
1020 $members =~ s/__aligned\s*\([^;]*\)//gos;
1021 $members =~ s/\s*CRYPTO_MINALIGN_ATTR//gos;
1022 # replace DECLARE_BITMAP
1023 $members =~ s/DECLARE_BITMAP\s*\(([^,)]+),\s*([^,)]+)\)/unsigned long $1\[BITS_TO_LONGS($2)\]/gos;
1024 # replace DECLARE_HASHTABLE
1025 $members =~ s/DECLARE_HASHTABLE\s*\(([^,)]+),\s*([^,)]+)\)/unsigned long $1\[1 << (($2) - 1)\]/gos;
1026 # replace DECLARE_KFIFO
1027 $members =~ s/DECLARE_KFIFO\s*\(([^,)]+),\s*([^,)]+),\s*([^,)]+)\)/$2 \*$1/gos;
1028 # replace DECLARE_KFIFO_PTR
1029 $members =~ s/DECLARE_KFIFO_PTR\s*\(([^,)]+),\s*([^,)]+)\)/$2 \*$1/gos;
1031 my $declaration = $members;
1033 # Split nested struct/union elements as newer ones
1037 while ($members =~ m/(struct|union)([^{};]+){([^{}]*)}([^{}\;]*)\;/) {
1038 my $newmember = "$1 $4;";
1043 foreach my $arg (split /;/, $content) {
1044 next if ($arg =~ m/^\s*$/);
1047 $type =~ s/\s\S+$//;
1049 $name =~ s/[:\[].*//;
1051 next if (($name =~ m/^\s*$/));
1052 if ($id =~ m/^\s*$/) {
1053 # anonymous struct/union
1054 $newmember .= "$type $name;";
1056 $newmember .= "$type $id.$name;";
1059 $members =~ s/(struct|union)([^{};]+){([^{}]*)}([^{}\;]*)\;/$newmember/;
1064 # Ignore other nested elements, like enums
1065 $members =~ s/({[^\{\}]*})//g;
1066 $nested = $decl_type;
1067 $nested =~ s/\/\*.*?\*\///gos;
1069 create_parameterlist($members, ';', $file);
1070 check_sections($file, $declaration_name, $decl_type, $sectcheck, $struct_actual, $nested);
1072 # Adjust declaration for better display
1073 $declaration =~ s/([{;])/$1\n/g;
1074 $declaration =~ s/}\s+;/};/g;
1075 # Better handle inlined enums
1076 do {} while ($declaration =~ s/(enum\s+{[^}]+),([^\n])/$1,\n$2/);
1078 my @def_args = split /\n/, $declaration;
1081 foreach my $clause (@def_args) {
1082 $clause =~ s/^\s+//;
1083 $clause =~ s/\s+$//;
1084 $clause =~ s/\s+/ /;
1086 $level-- if ($clause =~ m/(})/ && $level > 1);
1087 if (!($clause =~ m/^\s*#/)) {
1088 $declaration .= "\t" x $level;
1090 $declaration .= "\t" . $clause . "\n";
1091 $level++ if ($clause =~ m/({)/ && !($clause =~m/}/));
1093 output_declaration($declaration_name,
1095 {'struct' => $declaration_name,
1096 'module' => $modulename,
1097 'definition' => $declaration,
1098 'parameterlist' => \@parameterlist,
1099 'parameterdescs' => \%parameterdescs,
1100 'parametertypes' => \%parametertypes,
1101 'sectionlist' => \@sectionlist,
1102 'sections' => \%sections,
1103 'purpose' => $declaration_purpose,
1104 'type' => $decl_type
1108 print STDERR "${file}:$.: error: Cannot parse struct or union!\n";
1117 $x =~ s@/\*.*?\*/@@gos; # strip comments.
1118 # strip #define macros inside enums
1119 $x =~ s@#\s*((define|ifdef)\s+|endif)[^;]*;@@gos;
1121 if ($x =~ /enum\s+(\w+)\s*{(.*)}/) {
1122 $declaration_name = $1;
1126 $members =~ s/\s+$//;
1128 foreach my $arg (split ',', $members) {
1129 $arg =~ s/^\s*(\w+).*/$1/;
1130 push @parameterlist, $arg;
1131 if (!$parameterdescs{$arg}) {
1132 $parameterdescs{$arg} = $undescribed;
1133 print STDERR "${file}:$.: warning: Enum value '$arg' ".
1134 "not described in enum '$declaration_name'\n";
1136 $_members{$arg} = 1;
1139 while (my ($k, $v) = each %parameterdescs) {
1140 if (!exists($_members{$k})) {
1141 print STDERR "${file}:$.: warning: Excess enum value " .
1142 "'$k' description in '$declaration_name'\n";
1146 output_declaration($declaration_name,
1148 {'enum' => $declaration_name,
1149 'module' => $modulename,
1150 'parameterlist' => \@parameterlist,
1151 'parameterdescs' => \%parameterdescs,
1152 'sectionlist' => \@sectionlist,
1153 'sections' => \%sections,
1154 'purpose' => $declaration_purpose
1158 print STDERR "${file}:$.: error: Cannot parse enum!\n";
1163 sub dump_typedef($$) {
1167 $x =~ s@/\*.*?\*/@@gos; # strip comments.
1169 # Parse function prototypes
1170 if ($x =~ /typedef\s+(\w+)\s*\(\*\s*(\w\S+)\s*\)\s*\((.*)\);/ ||
1171 $x =~ /typedef\s+(\w+)\s*(\w\S+)\s*\s*\((.*)\);/) {
1175 $declaration_name = $2;
1178 create_parameterlist($args, ',', $file);
1180 output_declaration($declaration_name,
1182 {'function' => $declaration_name,
1184 'module' => $modulename,
1185 'functiontype' => $return_type,
1186 'parameterlist' => \@parameterlist,
1187 'parameterdescs' => \%parameterdescs,
1188 'parametertypes' => \%parametertypes,
1189 'sectionlist' => \@sectionlist,
1190 'sections' => \%sections,
1191 'purpose' => $declaration_purpose
1196 while (($x =~ /\(*.\)\s*;$/) || ($x =~ /\[*.\]\s*;$/)) {
1197 $x =~ s/\(*.\)\s*;$/;/;
1198 $x =~ s/\[*.\]\s*;$/;/;
1201 if ($x =~ /typedef.*\s+(\w+)\s*;/) {
1202 $declaration_name = $1;
1204 output_declaration($declaration_name,
1206 {'typedef' => $declaration_name,
1207 'module' => $modulename,
1208 'sectionlist' => \@sectionlist,
1209 'sections' => \%sections,
1210 'purpose' => $declaration_purpose
1214 print STDERR "${file}:$.: error: Cannot parse typedef!\n";
1219 sub save_struct_actual($) {
1222 # strip all spaces from the actual param so that it looks like one string item
1223 $actual =~ s/\s*//g;
1224 $struct_actual = $struct_actual . $actual . " ";
1227 sub create_parameterlist($$$) {
1229 my $splitter = shift;
1234 # temporarily replace commas inside function pointer definition
1235 while ($args =~ /(\([^\),]+),/) {
1236 $args =~ s/(\([^\),]+),/$1#/g;
1239 foreach my $arg (split($splitter, $args)) {
1241 $arg =~ s/\/\*.*\*\///;
1242 # strip leading/trailing spaces
1248 # Treat preprocessor directive as a typeless variable just to fill
1249 # corresponding data structures "correctly". Catch it later in
1251 push_parameter($arg, "", $file);
1252 } elsif ($arg =~ m/\(.+\)\s*\(/) {
1253 # pointer-to-function
1255 $arg =~ m/[^\(]+\(\*?\s*(\w*)\s*\)/;
1258 $type =~ s/([^\(]+\(\*?)\s*$param/$1/;
1259 save_struct_actual($param);
1260 push_parameter($param, $type, $file);
1262 $arg =~ s/\s*:\s*/:/g;
1263 $arg =~ s/\s*\[/\[/g;
1265 my @args = split('\s*,\s*', $arg);
1266 if ($args[0] =~ m/\*/) {
1267 $args[0] =~ s/(\*+)\s*/ $1/;
1271 if ($args[0] =~ /^(.*\s+)(.*?\[.*\].*)$/) {
1273 push(@first_arg, split('\s+', $1));
1274 push(@first_arg, $2);
1276 @first_arg = split('\s+', shift @args);
1279 unshift(@args, pop @first_arg);
1280 $type = join " ", @first_arg;
1282 foreach $param (@args) {
1283 if ($param =~ m/^(\*+)\s*(.*)/) {
1284 save_struct_actual($2);
1285 push_parameter($2, "$type $1", $file);
1287 elsif ($param =~ m/(.*?):(\d+)/) {
1288 if ($type ne "") { # skip unnamed bit-fields
1289 save_struct_actual($1);
1290 push_parameter($1, "$type:$2", $file)
1294 save_struct_actual($param);
1295 push_parameter($param, $type, $file);
1302 sub push_parameter($$$) {
1307 if (($anon_struct_union == 1) && ($type eq "") &&
1309 return; # ignore the ending }; from anon. struct/union
1312 $anon_struct_union = 0;
1313 $param =~ s/[\[\)].*//;
1315 if ($type eq "" && $param =~ /\.\.\.$/)
1317 if (!$param =~ /\w\.\.\.$/) {
1318 # handles unnamed variable parameters
1321 if (!defined $parameterdescs{$param} || $parameterdescs{$param} eq "") {
1322 $parameterdescs{$param} = "variable arguments";
1325 elsif ($type eq "" && ($param eq "" or $param eq "void"))
1328 $parameterdescs{void} = "no arguments";
1330 elsif ($type eq "" && ($param eq "struct" or $param eq "union"))
1331 # handle unnamed (anonymous) union or struct:
1334 $param = "{unnamed_" . $param . "}";
1335 $parameterdescs{$param} = "anonymous\n";
1336 $anon_struct_union = 1;
1339 # warn if parameter has no description
1340 # (but ignore ones starting with # as these are not parameters
1341 # but inline preprocessor statements);
1342 # also ignore unnamed structs/unions;
1343 if (!$anon_struct_union) {
1344 if (!defined $parameterdescs{$param} && $param !~ /^#/) {
1346 $parameterdescs{$param} = $undescribed;
1348 if (($type eq 'function') || ($type eq 'enum')) {
1349 print STDERR "${file}:$.: warning: Function parameter ".
1350 "or member '$param' not " .
1351 "described in '$declaration_name'\n";
1353 print STDERR "${file}:$.: warning:" .
1354 " No description found for parameter '$param'\n";
1359 $param = xml_escape($param);
1361 # strip spaces from $param so that it is one continuous string
1362 # on @parameterlist;
1363 # this fixes a problem where check_sections() cannot find
1364 # a parameter like "addr[6 + 2]" because it actually appears
1365 # as "addr[6", "+", "2]" on the parameter list;
1366 # but it's better to maintain the param string unchanged for output,
1367 # so just weaken the string compare in check_sections() to ignore
1368 # "[blah" in a parameter string;
1369 ###$param =~ s/\s*//g;
1370 push @parameterlist, $param;
1371 $type =~ s/\s\s+/ /g;
1372 $parametertypes{$param} = $type;
1375 sub check_sections($$$$$$) {
1376 my ($file, $decl_name, $decl_type, $sectcheck, $prmscheck, $nested) = @_;
1377 my @sects = split ' ', $sectcheck;
1378 my @prms = split ' ', $prmscheck;
1381 my $prm_clean; # strip trailing "[array size]" and/or beginning "*"
1383 foreach $sx (0 .. $#sects) {
1385 foreach $px (0 .. $#prms) {
1386 $prm_clean = $prms[$px];
1387 $prm_clean =~ s/\[.*\]//;
1388 $prm_clean =~ s/__attribute__\s*\(\([a-z,_\*\s\(\)]*\)\)//i;
1389 # ignore array size in a parameter string;
1390 # however, the original param string may contain
1391 # spaces, e.g.: addr[6 + 2]
1392 # and this appears in @prms as "addr[6" since the
1393 # parameter list is split at spaces;
1394 # hence just ignore "[..." for the sections check;
1395 $prm_clean =~ s/\[.*//;
1397 ##$prm_clean =~ s/^\**//;
1398 if ($prm_clean eq $sects[$sx]) {
1404 if ($decl_type eq "function") {
1405 print STDERR "${file}:$.: warning: " .
1406 "Excess function parameter " .
1408 "description in '$decl_name'\n";
1411 if ($nested !~ m/\Q$sects[$sx]\E/) {
1412 print STDERR "${file}:$.: warning: " .
1413 "Excess $decl_type member " .
1415 "description in '$decl_name'\n";
1424 # Checks the section describing the return value of a function.
1425 sub check_return_section {
1427 my $declaration_name = shift;
1428 my $return_type = shift;
1430 # Ignore an empty return type (It's a macro)
1431 # Ignore functions with a "void" return type. (But don't ignore "void *")
1432 if (($return_type eq "") || ($return_type =~ /void\s*\w*\s*$/)) {
1436 if (!defined($sections{$section_return}) ||
1437 $sections{$section_return} eq "") {
1438 print STDERR "${file}:$.: warning: " .
1439 "No description found for return value of " .
1440 "'$declaration_name'\n";
1446 # takes a function prototype and the name of the current file being
1447 # processed and spits out all the details stored in the global
1449 sub dump_function($$) {
1450 my $prototype = shift;
1454 $prototype =~ s/^static +//;
1455 $prototype =~ s/^extern +//;
1456 $prototype =~ s/^asmlinkage +//;
1457 $prototype =~ s/^inline +//;
1458 $prototype =~ s/^__inline__ +//;
1459 $prototype =~ s/^__inline +//;
1460 $prototype =~ s/^__always_inline +//;
1461 $prototype =~ s/^noinline +//;
1462 $prototype =~ s/__init +//;
1463 $prototype =~ s/__init_or_module +//;
1464 $prototype =~ s/__meminit +//;
1465 $prototype =~ s/__must_check +//;
1466 $prototype =~ s/__weak +//;
1467 my $define = $prototype =~ s/^#\s*define\s+//; #ak added
1468 $prototype =~ s/__attribute__\s*\(\(
1470 [\w\s]++ # attribute name
1471 (?:\([^)]*+\))? # attribute arguments
1472 \s*+,? # optional comma at the end
1476 # Yes, this truly is vile. We are looking for:
1477 # 1. Return type (may be nothing if we're looking at a macro)
1479 # 3. Function parameters.
1481 # All the while we have to watch out for function pointer parameters
1482 # (which IIRC is what the two sections are for), C types (these
1483 # regexps don't even start to express all the possibilities), and
1486 # If you mess with these regexps, it's a good idea to check that
1487 # the following functions' documentation still comes out right:
1488 # - parport_register_device (function pointer parameters)
1489 # - atomic_set (macro)
1490 # - pci_match_device, __copy_to_user (long return type)
1492 if ($define && $prototype =~ m/^()([a-zA-Z0-9_~:]+)\s+/) {
1493 # This is an object-like macro, it has no return type and no parameter
1495 # Function-like macros are not allowed to have spaces between
1496 # declaration_name and opening parenthesis (notice the \s+).
1498 $declaration_name = $2;
1500 } elsif ($prototype =~ m/^()([a-zA-Z0-9_~:]+)\s*\(([^\(]*)\)/ ||
1501 $prototype =~ m/^(\w+)\s+([a-zA-Z0-9_~:]+)\s*\(([^\(]*)\)/ ||
1502 $prototype =~ m/^(\w+\s*\*+)\s*([a-zA-Z0-9_~:]+)\s*\(([^\(]*)\)/ ||
1503 $prototype =~ m/^(\w+\s+\w+)\s+([a-zA-Z0-9_~:]+)\s*\(([^\(]*)\)/ ||
1504 $prototype =~ m/^(\w+\s+\w+\s*\*+)\s*([a-zA-Z0-9_~:]+)\s*\(([^\(]*)\)/ ||
1505 $prototype =~ m/^(\w+\s+\w+\s+\w+)\s+([a-zA-Z0-9_~:]+)\s*\(([^\(]*)\)/ ||
1506 $prototype =~ m/^(\w+\s+\w+\s+\w+\s*\*+)\s*([a-zA-Z0-9_~:]+)\s*\(([^\(]*)\)/ ||
1507 $prototype =~ m/^()([a-zA-Z0-9_~:]+)\s*\(([^\{]*)\)/ ||
1508 $prototype =~ m/^(\w+)\s+([a-zA-Z0-9_~:]+)\s*\(([^\{]*)\)/ ||
1509 $prototype =~ m/^(\w+\s*\*+)\s*([a-zA-Z0-9_~:]+)\s*\(([^\{]*)\)/ ||
1510 $prototype =~ m/^(\w+\s+\w+)\s+([a-zA-Z0-9_~:]+)\s*\(([^\{]*)\)/ ||
1511 $prototype =~ m/^(\w+\s+\w+\s*\*+)\s*([a-zA-Z0-9_~:]+)\s*\(([^\{]*)\)/ ||
1512 $prototype =~ m/^(\w+\s+\w+\s+\w+)\s+([a-zA-Z0-9_~:]+)\s*\(([^\{]*)\)/ ||
1513 $prototype =~ m/^(\w+\s+\w+\s+\w+\s*\*+)\s*([a-zA-Z0-9_~:]+)\s*\(([^\{]*)\)/ ||
1514 $prototype =~ m/^(\w+\s+\w+\s+\w+\s+\w+)\s+([a-zA-Z0-9_~:]+)\s*\(([^\{]*)\)/ ||
1515 $prototype =~ m/^(\w+\s+\w+\s+\w+\s+\w+\s*\*+)\s*([a-zA-Z0-9_~:]+)\s*\(([^\{]*)\)/ ||
1516 $prototype =~ m/^(\w+\s+\w+\s*\*+\s*\w+\s*\*+\s*)\s*([a-zA-Z0-9_~:]+)\s*\(([^\{]*)\)/) {
1518 $declaration_name = $2;
1521 create_parameterlist($args, ',', $file);
1523 print STDERR "${file}:$.: warning: cannot understand function prototype: '$prototype'\n";
1527 my $prms = join " ", @parameterlist;
1528 check_sections($file, $declaration_name, "function", $sectcheck, $prms, "");
1530 # This check emits a lot of warnings at the moment, because many
1531 # functions don't have a 'Return' doc section. So until the number
1532 # of warnings goes sufficiently down, the check is only performed in
1534 # TODO: always perform the check.
1535 if ($verbose && !$noret) {
1536 check_return_section($file, $declaration_name, $return_type);
1539 output_declaration($declaration_name,
1541 {'function' => $declaration_name,
1542 'module' => $modulename,
1543 'functiontype' => $return_type,
1544 'parameterlist' => \@parameterlist,
1545 'parameterdescs' => \%parameterdescs,
1546 'parametertypes' => \%parametertypes,
1547 'sectionlist' => \@sectionlist,
1548 'sections' => \%sections,
1549 'purpose' => $declaration_purpose
1555 %parameterdescs = ();
1556 %parametertypes = ();
1557 @parameterlist = ();
1561 $struct_actual = "";
1564 $state = STATE_NORMAL;
1565 $inline_doc_state = STATE_INLINE_NA;
1568 sub tracepoint_munge($) {
1570 my $tracepointname = 0;
1571 my $tracepointargs = 0;
1573 if ($prototype =~ m/TRACE_EVENT\((.*?),/) {
1574 $tracepointname = $1;
1576 if ($prototype =~ m/DEFINE_SINGLE_EVENT\((.*?),/) {
1577 $tracepointname = $1;
1579 if ($prototype =~ m/DEFINE_EVENT\((.*?),(.*?),/) {
1580 $tracepointname = $2;
1582 $tracepointname =~ s/^\s+//; #strip leading whitespace
1583 if ($prototype =~ m/TP_PROTO\((.*?)\)/) {
1584 $tracepointargs = $1;
1586 if (($tracepointname eq 0) || ($tracepointargs eq 0)) {
1587 print STDERR "${file}:$.: warning: Unrecognized tracepoint format: \n".
1590 $prototype = "static inline void trace_$tracepointname($tracepointargs)";
1594 sub syscall_munge() {
1597 $prototype =~ s@[\r\n]+@ @gos; # strip newlines/CR's
1598 ## if ($prototype =~ m/SYSCALL_DEFINE0\s*\(\s*(a-zA-Z0-9_)*\s*\)/) {
1599 if ($prototype =~ m/SYSCALL_DEFINE0/) {
1601 ## $prototype = "long sys_$1(void)";
1604 $prototype =~ s/SYSCALL_DEFINE.*\(/long sys_/; # fix return type & func name
1605 if ($prototype =~ m/long (sys_.*?),/) {
1606 $prototype =~ s/,/\(/;
1608 $prototype =~ s/\)/\(void\)/;
1611 # now delete all of the odd-number commas in $prototype
1612 # so that arg types & arg names don't have a comma between them
1614 my $len = length($prototype);
1616 $len = 0; # skip the for-loop
1618 for (my $ix = 0; $ix < $len; $ix++) {
1619 if (substr($prototype, $ix, 1) eq ',') {
1621 if ($count % 2 == 1) {
1622 substr($prototype, $ix, 1) = ' ';
1628 sub process_proto_function($$) {
1632 $x =~ s@\/\/.*$@@gos; # strip C99-style comments to end of line
1634 if ($x =~ m#\s*/\*\s+MACDOC\s*#io || ($x =~ /^#/ && $x !~ /^#\s*define/)) {
1637 elsif ($x =~ /([^\{]*)/) {
1641 if (($x =~ /\{/) || ($x =~ /\#\s*define/) || ($x =~ /;/)) {
1642 $prototype =~ s@/\*.*?\*/@@gos; # strip comments.
1643 $prototype =~ s@[\r\n]+@ @gos; # strip newlines/cr's.
1644 $prototype =~ s@^\s+@@gos; # strip leading spaces
1645 if ($prototype =~ /SYSCALL_DEFINE/) {
1648 if ($prototype =~ /TRACE_EVENT/ || $prototype =~ /DEFINE_EVENT/ ||
1649 $prototype =~ /DEFINE_SINGLE_EVENT/)
1651 tracepoint_munge($file);
1653 dump_function($prototype, $file);
1658 sub process_proto_type($$) {
1662 $x =~ s@[\r\n]+@ @gos; # strip newlines/cr's.
1663 $x =~ s@^\s+@@gos; # strip leading spaces
1664 $x =~ s@\s+$@@gos; # strip trailing spaces
1665 $x =~ s@\/\/.*$@@gos; # strip C99-style comments to end of line
1668 # To distinguish preprocessor directive from regular declaration later.
1673 if ( $x =~ /([^{};]*)([{};])(.*)/ ) {
1674 if( length $prototype ) {
1677 $prototype .= $1 . $2;
1678 ($2 eq '{') && $brcount++;
1679 ($2 eq '}') && $brcount--;
1680 if (($2 eq ';') && ($brcount == 0)) {
1681 dump_declaration($prototype, $file);
1693 # xml_escape: replace <, >, and & in the text stream;
1695 # however, formatting controls that are generated internally/locally in the
1696 # kernel-doc script are not escaped here; instead, they begin life like
1697 # $blankline_html (4 of '\' followed by a mnemonic + ':'), then these strings
1698 # are converted to their mnemonic-expected output, without the 4 * '\' & ':',
1699 # just before actual output; (this is done by local_unescape())
1702 if ($output_mode eq "man") {
1705 $text =~ s/\&/\\\\\\amp;/g;
1706 $text =~ s/\</\\\\\\lt;/g;
1707 $text =~ s/\>/\\\\\\gt;/g;
1711 # xml_unescape: reverse the effects of xml_escape
1712 sub xml_unescape($) {
1714 if ($output_mode eq "man") {
1717 $text =~ s/\\\\\\amp;/\&/g;
1718 $text =~ s/\\\\\\lt;/</g;
1719 $text =~ s/\\\\\\gt;/>/g;
1723 # convert local escape strings to html
1724 # local escape strings look like: '\\\\menmonic:' (that's 4 backslashes)
1725 sub local_unescape($) {
1727 if ($output_mode eq "man") {
1730 $text =~ s/\\\\\\\\lt:/</g;
1731 $text =~ s/\\\\\\\\gt:/>/g;
1735 sub map_filename($) {
1737 my ($orig_file) = @_;
1739 if (defined($ENV{'SRCTREE'})) {
1740 $file = "$ENV{'SRCTREE'}" . "/" . $orig_file;
1745 if (defined($source_map{$file})) {
1746 $file = $source_map{$file};
1752 sub process_export_file($) {
1753 my ($orig_file) = @_;
1754 my $file = map_filename($orig_file);
1756 if (!open(IN,"<$file")) {
1757 print STDERR "Error: Cannot open file $file\n";
1763 if (/$export_symbol/) {
1764 $function_table{$2} = 1;
1771 sub process_file($) {
1777 my $initial_section_counter = $section_counter;
1778 my ($orig_file) = @_;
1781 $file = map_filename($orig_file);
1783 if (!open(IN,"<$file")) {
1784 print STDERR "Error: Cannot open file $file\n";
1791 $section_counter = 0;
1793 while (s/\\\s*$//) {
1796 # Replace tabs by spaces
1797 while ($_ =~ s/\t+/' ' x (length($&) * 8 - length($`) % 8)/e) {};
1798 if ($state == STATE_NORMAL) {
1799 if (/$doc_start/o) {
1800 $state = STATE_NAME; # next line is always the function name
1802 $declaration_start_line = $. + 1;
1804 } elsif ($state == STATE_NAME) {# this line is the function name (always)
1805 if (/$doc_block/o) {
1806 $state = STATE_DOCBLOCK;
1808 $new_start_line = $. + 1;
1811 $section = $section_intro;
1816 elsif (/$doc_decl/o) {
1818 if (/\s*([\w\s]+?)\s*-/) {
1822 $state = STATE_FIELD;
1823 # if there's no @param blocks need to set up default section
1826 $section = $section_default;
1827 $new_start_line = $. + 1;
1829 # strip leading/trailing/multiple spaces
1833 $descr =~ s/\s+/ /g;
1834 $declaration_purpose = xml_escape($descr);
1837 $declaration_purpose = "";
1840 if (($declaration_purpose eq "") && $verbose) {
1841 print STDERR "${file}:$.: warning: missing initial short description on line:\n";
1846 if ($identifier =~ m/^struct/) {
1847 $decl_type = 'struct';
1848 } elsif ($identifier =~ m/^union/) {
1849 $decl_type = 'union';
1850 } elsif ($identifier =~ m/^enum/) {
1851 $decl_type = 'enum';
1852 } elsif ($identifier =~ m/^typedef/) {
1853 $decl_type = 'typedef';
1855 $decl_type = 'function';
1859 print STDERR "${file}:$.: info: Scanning doc for $identifier\n";
1862 print STDERR "${file}:$.: warning: Cannot understand $_ on line $.",
1863 " - I thought it was a doc line\n";
1865 $state = STATE_NORMAL;
1867 } elsif ($state == STATE_FIELD) { # look for head: lines, and include content
1868 if (/$doc_sect/i) { # case insensitive for supported section names
1872 # map the supported section names to the canonical names
1873 if ($newsection =~ m/^description$/i) {
1874 $newsection = $section_default;
1875 } elsif ($newsection =~ m/^context$/i) {
1876 $newsection = $section_context;
1877 } elsif ($newsection =~ m/^returns?$/i) {
1878 $newsection = $section_return;
1879 } elsif ($newsection =~ m/^\@return$/) {
1880 # special: @return is a section, not a param description
1881 $newsection = $section_return;
1884 if (($contents ne "") && ($contents ne "\n")) {
1885 if (!$in_doc_sect && $verbose) {
1886 print STDERR "${file}:$.: warning: contents before sections\n";
1889 dump_section($file, $section, xml_escape($contents));
1890 $section = $section_default;
1895 $contents = $newcontents;
1896 $new_start_line = $.;
1897 while (substr($contents, 0, 1) eq " ") {
1898 $contents = substr($contents, 1);
1900 if ($contents ne "") {
1903 $section = $newsection;
1904 $leading_space = undef;
1905 } elsif (/$doc_end/) {
1906 if (($contents ne "") && ($contents ne "\n")) {
1907 dump_section($file, $section, xml_escape($contents));
1908 $section = $section_default;
1911 # look for doc_com + <text> + doc_end:
1912 if ($_ =~ m'\s*\*\s*[a-zA-Z_0-9:\.]+\*/') {
1913 print STDERR "${file}:$.: warning: suspicious ending line: $_";
1918 $state = STATE_PROTO;
1920 # print STDERR "end of doc comment, looking for prototype\n";
1921 } elsif (/$doc_content/) {
1922 # miguel-style comment kludge, look for blank lines after
1923 # @parameter line to signify start of description
1925 if ($section =~ m/^@/ || $section eq $section_context) {
1926 dump_section($file, $section, xml_escape($contents));
1927 $section = $section_default;
1929 $new_start_line = $.;
1934 } elsif ($in_purpose == 1) {
1935 # Continued declaration purpose
1936 chomp($declaration_purpose);
1937 $declaration_purpose .= " " . xml_escape($1);
1938 $declaration_purpose =~ s/\s+/ /g;
1941 if ($section =~ m/^@/ || $section eq $section_context) {
1942 if (!defined $leading_space) {
1943 if ($cont =~ m/^(\s+)/) {
1944 $leading_space = $1;
1946 $leading_space = "";
1950 $cont =~ s/^$leading_space//;
1952 $contents .= $cont . "\n";
1955 # i dont know - bad line? ignore.
1956 print STDERR "${file}:$.: warning: bad line: $_";
1959 } elsif ($state == STATE_INLINE) { # scanning for inline parameters
1960 # First line (state 1) needs to be a @parameter
1961 if ($inline_doc_state == STATE_INLINE_NAME && /$doc_inline_sect/o) {
1964 $new_start_line = $.;
1965 if ($contents ne "") {
1966 while (substr($contents, 0, 1) eq " ") {
1967 $contents = substr($contents, 1);
1971 $inline_doc_state = STATE_INLINE_TEXT;
1972 # Documentation block end */
1973 } elsif (/$doc_inline_end/) {
1974 if (($contents ne "") && ($contents ne "\n")) {
1975 dump_section($file, $section, xml_escape($contents));
1976 $section = $section_default;
1979 $state = STATE_PROTO;
1980 $inline_doc_state = STATE_INLINE_NA;
1982 } elsif (/$doc_content/) {
1983 if ($inline_doc_state == STATE_INLINE_TEXT) {
1984 $contents .= $1 . "\n";
1985 # nuke leading blank lines
1986 if ($contents =~ /^\s*$/) {
1989 } elsif ($inline_doc_state == STATE_INLINE_NAME) {
1990 $inline_doc_state = STATE_INLINE_ERROR;
1991 print STDERR "${file}:$.: warning: ";
1992 print STDERR "Incorrect use of kernel-doc format: $_";
1996 } elsif ($state == STATE_PROTO) { # scanning for function '{' (end of prototype)
1997 if (/$doc_inline_oneline/) {
2000 if ($contents ne "") {
2002 dump_section($file, $section, xml_escape($contents));
2003 $section = $section_default;
2006 } elsif (/$doc_inline_start/) {
2007 $state = STATE_INLINE;
2008 $inline_doc_state = STATE_INLINE_NAME;
2009 } elsif ($decl_type eq 'function') {
2010 process_proto_function($_, $file);
2012 process_proto_type($_, $file);
2014 } elsif ($state == STATE_DOCBLOCK) {
2017 dump_doc_section($file, $section, xml_escape($contents));
2018 $section = $section_default;
2021 %parameterdescs = ();
2022 %parametertypes = ();
2023 @parameterlist = ();
2027 $state = STATE_NORMAL;
2029 elsif (/$doc_content/)
2033 $contents .= $blankline;
2037 $contents .= $1 . "\n";
2042 if ($initial_section_counter == $section_counter) {
2043 if ($output_mode ne "none") {
2044 print STDERR "${file}:1: warning: no structured comments found\n";
2046 if (($output_selection == OUTPUT_INCLUDE) && ($show_not_found == 1)) {
2047 print STDERR " Was looking for '$_'.\n" for keys %function_table;
2053 $kernelversion = get_kernel_version();
2055 # generate a sequence of code that will splice in highlighting information
2056 # using the s// operator.
2057 for (my $k = 0; $k < @highlights; $k++) {
2058 my $pattern = $highlights[$k][0];
2059 my $result = $highlights[$k][1];
2060 # print STDERR "scanning pattern:$pattern, highlight:($result)\n";
2061 $dohighlight .= "\$contents =~ s:$pattern:$result:gs;\n";
2064 # Read the file that maps relative names to absolute names for
2065 # separate source and object directories and for shadow trees.
2066 if (open(SOURCE_MAP, "<.tmp_filelist.txt")) {
2067 my ($relname, $absname);
2068 while(<SOURCE_MAP>) {
2070 ($relname, $absname) = (split())[0..1];
2071 $relname =~ s:^/+::;
2072 $source_map{$relname} = $absname;
2077 if ($output_selection == OUTPUT_EXPORTED ||
2078 $output_selection == OUTPUT_INTERNAL) {
2080 push(@export_file_list, @ARGV);
2082 foreach (@export_file_list) {
2084 process_export_file($_);
2092 if ($verbose && $errors) {
2093 print STDERR "$errors errors\n";
2095 if ($verbose && $warnings) {
2096 print STDERR "$warnings warnings\n";
2099 exit($output_mode eq "none" ? 0 : $errors);