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
340 # Inline documentation state
342 STATE_INLINE_NA => 0, # not applicable ($state != STATE_INLINE)
343 STATE_INLINE_NAME => 1, # looking for member name (@foo:)
344 STATE_INLINE_TEXT => 2, # looking for member documentation
345 STATE_INLINE_END => 3, # done
346 STATE_INLINE_ERROR => 4, # error - Comment without header was found.
347 # Spit a warning as it's not
348 # proper kernel-doc and ignore the rest.
350 my $inline_doc_state;
352 #declaration types: can be
353 # 'function', 'struct', 'union', 'enum', 'typedef'
356 my $doc_start = '^/\*\*\s*$'; # Allow whitespace at end of comment start.
358 my $doc_com = '\s*\*\s*';
359 my $doc_com_body = '\s*\* ?';
360 my $doc_decl = $doc_com . '(\w+)';
361 # @params and a strictly limited set of supported section names
362 my $doc_sect = $doc_com .
363 '\s*(\@[.\w]+|\@\.\.\.|description|context|returns?|notes?|examples?)\s*:(.*)';
364 my $doc_content = $doc_com_body . '(.*)';
365 my $doc_block = $doc_com . 'DOC:\s*(.*)?';
366 my $doc_inline_start = '^\s*/\*\*\s*$';
367 my $doc_inline_sect = '\s*\*\s*(@[\w\s]+):(.*)';
368 my $doc_inline_end = '^\s*\*/\s*$';
369 my $doc_inline_oneline = '^\s*/\*\*\s*(@[\w\s]+):\s*(.*)\s*\*/\s*$';
370 my $export_symbol = '^\s*EXPORT_SYMBOL(_GPL)?\s*\(\s*(\w+)\s*\)\s*;';
373 my %parameterdesc_start_lines;
377 my %section_start_lines;
382 my $new_start_line = 0;
384 # the canonical section names. see also $doc_sect above.
385 my $section_default = "Description"; # default section
386 my $section_intro = "Introduction";
387 my $section = $section_default;
388 my $section_context = "Context";
389 my $section_return = "Return";
391 my $undescribed = "-- undescribed --";
395 while ($ARGV[0] =~ m/^--?(.*)/) {
399 $output_mode = "man";
400 @highlights = @highlights_man;
401 $blankline = $blankline_man;
402 } elsif ($cmd eq "rst") {
403 $output_mode = "rst";
404 @highlights = @highlights_rst;
405 $blankline = $blankline_rst;
406 } elsif ($cmd eq "none") {
407 $output_mode = "none";
408 } elsif ($cmd eq "module") { # not needed for XML, inherits from calling document
409 $modulename = shift @ARGV;
410 } elsif ($cmd eq "function") { # to only output specific functions
411 $output_selection = OUTPUT_INCLUDE;
412 $function = shift @ARGV;
413 $function_table{$function} = 1;
414 } elsif ($cmd eq "nofunction") { # output all except specific functions
415 $output_selection = OUTPUT_EXCLUDE;
416 $function = shift @ARGV;
417 $function_table{$function} = 1;
418 } elsif ($cmd eq "export") { # only exported symbols
419 $output_selection = OUTPUT_EXPORTED;
420 %function_table = ();
421 } elsif ($cmd eq "internal") { # only non-exported symbols
422 $output_selection = OUTPUT_INTERNAL;
423 %function_table = ();
424 } elsif ($cmd eq "export-file") {
425 my $file = shift @ARGV;
426 push(@export_file_list, $file);
427 } elsif ($cmd eq "v") {
429 } elsif (($cmd eq "h") || ($cmd eq "help")) {
431 } elsif ($cmd eq 'no-doc-sections') {
432 $no_doc_sections = 1;
433 } elsif ($cmd eq 'enable-lineno') {
435 } elsif ($cmd eq 'show-not-found') {
443 # continue execution near EOF;
445 # get kernel version from env
446 sub get_kernel_version() {
447 my $version = 'unknown kernel version';
449 if (defined($ENV{'KERNELVERSION'})) {
450 $version = $ENV{'KERNELVERSION'};
458 if ($enable_lineno && defined($lineno)) {
459 print "#define LINENO " . $lineno . "\n";
463 # dumps section contents to arrays/hashes intended for that purpose.
468 my $contents = join "\n", @_;
470 if ($name =~ m/$type_param/) {
472 $parameterdescs{$name} = $contents;
473 $sectcheck = $sectcheck . $name . " ";
474 $parameterdesc_start_lines{$name} = $new_start_line;
476 } elsif ($name eq "@\.\.\.") {
478 $parameterdescs{$name} = $contents;
479 $sectcheck = $sectcheck . $name . " ";
480 $parameterdesc_start_lines{$name} = $new_start_line;
483 if (defined($sections{$name}) && ($sections{$name} ne "")) {
484 # Only warn on user specified duplicate section names.
485 if ($name ne $section_default) {
486 print STDERR "${file}:$.: warning: duplicate section name '$name'\n";
489 $sections{$name} .= $contents;
491 $sections{$name} = $contents;
492 push @sectionlist, $name;
493 $section_start_lines{$name} = $new_start_line;
500 # dump DOC: section after checking that it should go out
502 sub dump_doc_section {
505 my $contents = join "\n", @_;
507 if ($no_doc_sections) {
511 if (($output_selection == OUTPUT_ALL) ||
512 ($output_selection == OUTPUT_INCLUDE &&
513 defined($function_table{$name})) ||
514 ($output_selection == OUTPUT_EXCLUDE &&
515 !defined($function_table{$name})))
517 dump_section($file, $name, $contents);
518 output_blockhead({'sectionlist' => \@sectionlist,
519 'sections' => \%sections,
520 'module' => $modulename,
521 'content-only' => ($output_selection != OUTPUT_ALL), });
528 # parameterdescs, a hash.
529 # function => "function name"
530 # parameterlist => @list of parameters
531 # parameterdescs => %parameter descriptions
532 # sectionlist => @list of sections
533 # sections => %section descriptions
536 sub output_highlight {
537 my $contents = join "\n",@_;
541 # if (!defined $contents) {
543 # confess "output_highlight got called with no args?\n";
546 # print STDERR "contents b4:$contents\n";
549 # print STDERR "contents af:$contents\n";
551 foreach $line (split "\n", $contents) {
552 if (! $output_preformatted) {
556 if (! $output_preformatted) {
557 print $lineprefix, $blankline;
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",@_;
757 foreach $line (split "\n", $contents) {
758 print $lineprefix . $line . "\n";
762 sub output_function_rst(%) {
764 my ($parameter, $section);
765 my $oldprefix = $lineprefix;
768 if ($args{'typedef'}) {
769 print ".. c:type:: ". $args{'function'} . "\n\n";
770 print_lineno($declaration_start_line);
771 print " **Typedef**: ";
773 output_highlight_rst($args{'purpose'});
774 $start = "\n\n**Syntax**\n\n ``";
776 print ".. c:function:: ";
778 if ($args{'functiontype'} ne "") {
779 $start .= $args{'functiontype'} . " " . $args{'function'} . " (";
781 $start .= $args{'function'} . " (";
786 foreach my $parameter (@{$args{'parameterlist'}}) {
791 $type = $args{'parametertypes'}{$parameter};
793 if ($type =~ m/([^\(]*\(\*)\s*\)\s*\(([^\)]*)\)/) {
794 # pointer-to-function
795 print $1 . $parameter . ") (" . $2;
797 print $type . " " . $parameter;
800 if ($args{'typedef'}) {
804 print_lineno($declaration_start_line);
806 output_highlight_rst($args{'purpose'});
810 print "**Parameters**\n\n";
812 foreach $parameter (@{$args{'parameterlist'}}) {
813 my $parameter_name = $parameter;
814 $parameter_name =~ s/\[.*//;
815 $type = $args{'parametertypes'}{$parameter};
818 print "``$type $parameter``\n";
820 print "``$parameter``\n";
823 print_lineno($parameterdesc_start_lines{$parameter_name});
825 if (defined($args{'parameterdescs'}{$parameter_name}) &&
826 $args{'parameterdescs'}{$parameter_name} ne $undescribed) {
827 output_highlight_rst($args{'parameterdescs'}{$parameter_name});
829 print " *undescribed*\n";
834 $lineprefix = $oldprefix;
835 output_section_rst(@_);
838 sub output_section_rst(%) {
841 my $oldprefix = $lineprefix;
844 foreach $section (@{$args{'sectionlist'}}) {
845 print "**$section**\n\n";
846 print_lineno($section_start_lines{$section});
847 output_highlight_rst($args{'sections'}{$section});
851 $lineprefix = $oldprefix;
854 sub output_enum_rst(%) {
857 my $oldprefix = $lineprefix;
859 my $name = "enum " . $args{'enum'};
861 print "\n\n.. c:type:: " . $name . "\n\n";
862 print_lineno($declaration_start_line);
864 output_highlight_rst($args{'purpose'});
867 print "**Constants**\n\n";
869 foreach $parameter (@{$args{'parameterlist'}}) {
870 print "``$parameter``\n";
871 if ($args{'parameterdescs'}{$parameter} ne $undescribed) {
872 output_highlight_rst($args{'parameterdescs'}{$parameter});
874 print " *undescribed*\n";
879 $lineprefix = $oldprefix;
880 output_section_rst(@_);
883 sub output_typedef_rst(%) {
886 my $oldprefix = $lineprefix;
887 my $name = "typedef " . $args{'typedef'};
889 print "\n\n.. c:type:: " . $name . "\n\n";
890 print_lineno($declaration_start_line);
892 output_highlight_rst($args{'purpose'});
895 $lineprefix = $oldprefix;
896 output_section_rst(@_);
899 sub output_struct_rst(%) {
902 my $oldprefix = $lineprefix;
903 my $name = $args{'type'} . " " . $args{'struct'};
905 print "\n\n.. c:type:: " . $name . "\n\n";
906 print_lineno($declaration_start_line);
908 output_highlight_rst($args{'purpose'});
911 print "**Definition**\n\n";
913 my $declaration = $args{'definition'};
914 $declaration =~ s/\t/ /g;
915 print " " . $args{'type'} . " " . $args{'struct'} . " {\n$declaration };\n\n";
917 print "**Members**\n\n";
919 foreach $parameter (@{$args{'parameterlist'}}) {
920 ($parameter =~ /^#/) && next;
922 my $parameter_name = $parameter;
923 $parameter_name =~ s/\[.*//;
925 ($args{'parameterdescs'}{$parameter_name} ne $undescribed) || next;
926 $type = $args{'parametertypes'}{$parameter};
927 print_lineno($parameterdesc_start_lines{$parameter_name});
928 print "``" . $parameter . "``\n";
929 output_highlight_rst($args{'parameterdescs'}{$parameter_name});
934 $lineprefix = $oldprefix;
935 output_section_rst(@_);
938 ## none mode output functions
940 sub output_function_none(%) {
943 sub output_enum_none(%) {
946 sub output_typedef_none(%) {
949 sub output_struct_none(%) {
952 sub output_blockhead_none(%) {
956 # generic output function for all types (function, struct/union, typedef, enum);
957 # calls the generated, variable output_ function name based on
958 # functype and output_mode
959 sub output_declaration {
962 my $functype = shift;
963 my $func = "output_${functype}_$output_mode";
964 if (($output_selection == OUTPUT_ALL) ||
965 (($output_selection == OUTPUT_INCLUDE ||
966 $output_selection == OUTPUT_EXPORTED) &&
967 defined($function_table{$name})) ||
968 (($output_selection == OUTPUT_EXCLUDE ||
969 $output_selection == OUTPUT_INTERNAL) &&
970 !($functype eq "function" && defined($function_table{$name}))))
978 # generic output function - calls the right one based on current output mode.
979 sub output_blockhead {
981 my $func = "output_blockhead_" . $output_mode;
987 # takes a declaration (struct, union, enum, typedef) and
988 # invokes the right handler. NOT called for functions.
989 sub dump_declaration($$) {
991 my ($prototype, $file) = @_;
992 my $func = "dump_" . $decl_type;
1000 sub dump_struct($$) {
1004 if ($x =~ /(struct|union)\s+(\w+)\s*{(.*)}/) {
1006 $declaration_name = $2;
1009 # ignore members marked private:
1010 $members =~ s/\/\*\s*private:.*?\/\*\s*public:.*?\*\///gosi;
1011 $members =~ s/\/\*\s*private:.*//gosi;
1013 $members =~ s/\/\*.*?\*\///gos;
1015 $members =~ s/__attribute__\s*\(\([a-z,_\*\s\(\)]*\)\)//i;
1016 $members =~ s/__aligned\s*\([^;]*\)//gos;
1017 $members =~ s/\s*CRYPTO_MINALIGN_ATTR//gos;
1018 # replace DECLARE_BITMAP
1019 $members =~ s/DECLARE_BITMAP\s*\(([^,)]+),\s*([^,)]+)\)/unsigned long $1\[BITS_TO_LONGS($2)\]/gos;
1020 # replace DECLARE_HASHTABLE
1021 $members =~ s/DECLARE_HASHTABLE\s*\(([^,)]+),\s*([^,)]+)\)/unsigned long $1\[1 << (($2) - 1)\]/gos;
1022 # replace DECLARE_KFIFO
1023 $members =~ s/DECLARE_KFIFO\s*\(([^,)]+),\s*([^,)]+),\s*([^,)]+)\)/$2 \*$1/gos;
1024 # replace DECLARE_KFIFO_PTR
1025 $members =~ s/DECLARE_KFIFO_PTR\s*\(([^,)]+),\s*([^,)]+)\)/$2 \*$1/gos;
1027 my $declaration = $members;
1029 # Split nested struct/union elements as newer ones
1030 while ($members =~ m/(struct|union)([^\{\};]+)\{([^\{\}]*)\}([^\{\}\;]*)\;/) {
1035 foreach my $id(split /,/, $ids) {
1036 $newmember .= "$maintype $id; ";
1039 $id =~ s/^\s*\**(\S+)\s*/$1/;
1040 foreach my $arg (split /;/, $content) {
1041 next if ($arg =~ m/^\s*$/);
1042 if ($arg =~ m/^([^\(]+\(\*?\s*)([\w\.]*)(\s*\).*)/) {
1043 # pointer-to-function
1048 if ($id =~ m/^\s*$/) {
1049 # anonymous struct/union
1050 $newmember .= "$type$name$extra; ";
1052 $newmember .= "$type$id.$name$extra; ";
1060 $arg =~ s/:\s*\d+\s*//g;
1062 $arg =~ s/\[\S+\]//g;
1063 # The type may have multiple words,
1064 # and multiple IDs can be defined, like:
1065 # const struct foo, *bar, foobar
1066 # So, we remove spaces when parsing the
1067 # names, in order to match just names
1068 # and commas for the names
1069 $arg =~ s/\s*,\s*/,/g;
1070 if ($arg =~ m/(.*)\s+([\S+,]+)/) {
1074 $newmember .= "$arg; ";
1077 foreach my $name (split /,/, $names) {
1078 $name =~ s/^\s*\**(\S+)\s*/$1/;
1079 next if (($name =~ m/^\s*$/));
1080 if ($id =~ m/^\s*$/) {
1081 # anonymous struct/union
1082 $newmember .= "$type $name; ";
1084 $newmember .= "$type $id.$name; ";
1090 $members =~ s/(struct|union)([^\{\};]+)\{([^\{\}]*)}([^\{\}\;]*)\;/$newmember/;
1093 # Ignore other nested elements, like enums
1094 $members =~ s/({[^\{\}]*})//g;
1096 create_parameterlist($members, ';', $file, $declaration_name);
1097 check_sections($file, $declaration_name, $decl_type, $sectcheck, $struct_actual);
1099 # Adjust declaration for better display
1100 $declaration =~ s/([{;])/$1\n/g;
1101 $declaration =~ s/}\s+;/};/g;
1102 # Better handle inlined enums
1103 do {} while ($declaration =~ s/(enum\s+{[^}]+),([^\n])/$1,\n$2/);
1105 my @def_args = split /\n/, $declaration;
1108 foreach my $clause (@def_args) {
1109 $clause =~ s/^\s+//;
1110 $clause =~ s/\s+$//;
1111 $clause =~ s/\s+/ /;
1113 $level-- if ($clause =~ m/(})/ && $level > 1);
1114 if (!($clause =~ m/^\s*#/)) {
1115 $declaration .= "\t" x $level;
1117 $declaration .= "\t" . $clause . "\n";
1118 $level++ if ($clause =~ m/({)/ && !($clause =~m/}/));
1120 output_declaration($declaration_name,
1122 {'struct' => $declaration_name,
1123 'module' => $modulename,
1124 'definition' => $declaration,
1125 'parameterlist' => \@parameterlist,
1126 'parameterdescs' => \%parameterdescs,
1127 'parametertypes' => \%parametertypes,
1128 'sectionlist' => \@sectionlist,
1129 'sections' => \%sections,
1130 'purpose' => $declaration_purpose,
1131 'type' => $decl_type
1135 print STDERR "${file}:$.: error: Cannot parse struct or union!\n";
1141 sub show_warnings($$) {
1142 my $functype = shift;
1145 return 1 if ($output_selection == OUTPUT_ALL);
1147 if ($output_selection == OUTPUT_EXPORTED) {
1148 if (defined($function_table{$name})) {
1154 if ($output_selection == OUTPUT_INTERNAL) {
1155 if (!($functype eq "function" && defined($function_table{$name}))) {
1161 if ($output_selection == OUTPUT_INCLUDE) {
1162 if (defined($function_table{$name})) {
1168 if ($output_selection == OUTPUT_EXCLUDE) {
1169 if (!defined($function_table{$name})) {
1175 die("Please add the new output type at show_warnings()");
1182 $x =~ s@/\*.*?\*/@@gos; # strip comments.
1183 # strip #define macros inside enums
1184 $x =~ s@#\s*((define|ifdef)\s+|endif)[^;]*;@@gos;
1186 if ($x =~ /enum\s+(\w+)\s*{(.*)}/) {
1187 $declaration_name = $1;
1191 $members =~ s/\s+$//;
1193 foreach my $arg (split ',', $members) {
1194 $arg =~ s/^\s*(\w+).*/$1/;
1195 push @parameterlist, $arg;
1196 if (!$parameterdescs{$arg}) {
1197 $parameterdescs{$arg} = $undescribed;
1198 if (show_warnings("enum", $declaration_name)) {
1199 print STDERR "${file}:$.: warning: Enum value '$arg' not described in enum '$declaration_name'\n";
1202 $_members{$arg} = 1;
1205 while (my ($k, $v) = each %parameterdescs) {
1206 if (!exists($_members{$k})) {
1207 if (show_warnings("enum", $declaration_name)) {
1208 print STDERR "${file}:$.: warning: Excess enum value '$k' description in '$declaration_name'\n";
1213 output_declaration($declaration_name,
1215 {'enum' => $declaration_name,
1216 'module' => $modulename,
1217 'parameterlist' => \@parameterlist,
1218 'parameterdescs' => \%parameterdescs,
1219 'sectionlist' => \@sectionlist,
1220 'sections' => \%sections,
1221 'purpose' => $declaration_purpose
1225 print STDERR "${file}:$.: error: Cannot parse enum!\n";
1230 sub dump_typedef($$) {
1234 $x =~ s@/\*.*?\*/@@gos; # strip comments.
1236 # Parse function prototypes
1237 if ($x =~ /typedef\s+(\w+)\s*\(\*\s*(\w\S+)\s*\)\s*\((.*)\);/ ||
1238 $x =~ /typedef\s+(\w+)\s*(\w\S+)\s*\s*\((.*)\);/) {
1242 $declaration_name = $2;
1245 create_parameterlist($args, ',', $file, $declaration_name);
1247 output_declaration($declaration_name,
1249 {'function' => $declaration_name,
1251 'module' => $modulename,
1252 'functiontype' => $return_type,
1253 'parameterlist' => \@parameterlist,
1254 'parameterdescs' => \%parameterdescs,
1255 'parametertypes' => \%parametertypes,
1256 'sectionlist' => \@sectionlist,
1257 'sections' => \%sections,
1258 'purpose' => $declaration_purpose
1263 while (($x =~ /\(*.\)\s*;$/) || ($x =~ /\[*.\]\s*;$/)) {
1264 $x =~ s/\(*.\)\s*;$/;/;
1265 $x =~ s/\[*.\]\s*;$/;/;
1268 if ($x =~ /typedef.*\s+(\w+)\s*;/) {
1269 $declaration_name = $1;
1271 output_declaration($declaration_name,
1273 {'typedef' => $declaration_name,
1274 'module' => $modulename,
1275 'sectionlist' => \@sectionlist,
1276 'sections' => \%sections,
1277 'purpose' => $declaration_purpose
1281 print STDERR "${file}:$.: error: Cannot parse typedef!\n";
1286 sub save_struct_actual($) {
1289 # strip all spaces from the actual param so that it looks like one string item
1290 $actual =~ s/\s*//g;
1291 $struct_actual = $struct_actual . $actual . " ";
1294 sub create_parameterlist($$$$) {
1296 my $splitter = shift;
1298 my $declaration_name = shift;
1302 # temporarily replace commas inside function pointer definition
1303 while ($args =~ /(\([^\),]+),/) {
1304 $args =~ s/(\([^\),]+),/$1#/g;
1307 foreach my $arg (split($splitter, $args)) {
1309 $arg =~ s/\/\*.*\*\///;
1310 # strip leading/trailing spaces
1316 # Treat preprocessor directive as a typeless variable just to fill
1317 # corresponding data structures "correctly". Catch it later in
1319 push_parameter($arg, "", $file);
1320 } elsif ($arg =~ m/\(.+\)\s*\(/) {
1321 # pointer-to-function
1323 $arg =~ m/[^\(]+\(\*?\s*([\w\.]*)\s*\)/;
1326 $type =~ s/([^\(]+\(\*?)\s*$param/$1/;
1327 save_struct_actual($param);
1328 push_parameter($param, $type, $file, $declaration_name);
1330 $arg =~ s/\s*:\s*/:/g;
1331 $arg =~ s/\s*\[/\[/g;
1333 my @args = split('\s*,\s*', $arg);
1334 if ($args[0] =~ m/\*/) {
1335 $args[0] =~ s/(\*+)\s*/ $1/;
1339 if ($args[0] =~ /^(.*\s+)(.*?\[.*\].*)$/) {
1341 push(@first_arg, split('\s+', $1));
1342 push(@first_arg, $2);
1344 @first_arg = split('\s+', shift @args);
1347 unshift(@args, pop @first_arg);
1348 $type = join " ", @first_arg;
1350 foreach $param (@args) {
1351 if ($param =~ m/^(\*+)\s*(.*)/) {
1352 save_struct_actual($2);
1353 push_parameter($2, "$type $1", $file, $declaration_name);
1355 elsif ($param =~ m/(.*?):(\d+)/) {
1356 if ($type ne "") { # skip unnamed bit-fields
1357 save_struct_actual($1);
1358 push_parameter($1, "$type:$2", $file, $declaration_name)
1362 save_struct_actual($param);
1363 push_parameter($param, $type, $file, $declaration_name);
1370 sub push_parameter($$$$) {
1374 my $declaration_name = shift;
1376 if (($anon_struct_union == 1) && ($type eq "") &&
1378 return; # ignore the ending }; from anon. struct/union
1381 $anon_struct_union = 0;
1382 $param =~ s/[\[\)].*//;
1384 if ($type eq "" && $param =~ /\.\.\.$/)
1386 if (!$param =~ /\w\.\.\.$/) {
1387 # handles unnamed variable parameters
1390 if (!defined $parameterdescs{$param} || $parameterdescs{$param} eq "") {
1391 $parameterdescs{$param} = "variable arguments";
1394 elsif ($type eq "" && ($param eq "" or $param eq "void"))
1397 $parameterdescs{void} = "no arguments";
1399 elsif ($type eq "" && ($param eq "struct" or $param eq "union"))
1400 # handle unnamed (anonymous) union or struct:
1403 $param = "{unnamed_" . $param . "}";
1404 $parameterdescs{$param} = "anonymous\n";
1405 $anon_struct_union = 1;
1408 # warn if parameter has no description
1409 # (but ignore ones starting with # as these are not parameters
1410 # but inline preprocessor statements);
1411 # Note: It will also ignore void params and unnamed structs/unions
1412 if (!defined $parameterdescs{$param} && $param !~ /^#/) {
1413 $parameterdescs{$param} = $undescribed;
1415 if (show_warnings($type, $declaration_name)) {
1417 "${file}:$.: warning: Function parameter or member '$param' not described in '$declaration_name'\n";
1422 # strip spaces from $param so that it is one continuous string
1423 # on @parameterlist;
1424 # this fixes a problem where check_sections() cannot find
1425 # a parameter like "addr[6 + 2]" because it actually appears
1426 # as "addr[6", "+", "2]" on the parameter list;
1427 # but it's better to maintain the param string unchanged for output,
1428 # so just weaken the string compare in check_sections() to ignore
1429 # "[blah" in a parameter string;
1430 ###$param =~ s/\s*//g;
1431 push @parameterlist, $param;
1432 $type =~ s/\s\s+/ /g;
1433 $parametertypes{$param} = $type;
1436 sub check_sections($$$$$) {
1437 my ($file, $decl_name, $decl_type, $sectcheck, $prmscheck) = @_;
1438 my @sects = split ' ', $sectcheck;
1439 my @prms = split ' ', $prmscheck;
1442 my $prm_clean; # strip trailing "[array size]" and/or beginning "*"
1444 foreach $sx (0 .. $#sects) {
1446 foreach $px (0 .. $#prms) {
1447 $prm_clean = $prms[$px];
1448 $prm_clean =~ s/\[.*\]//;
1449 $prm_clean =~ s/__attribute__\s*\(\([a-z,_\*\s\(\)]*\)\)//i;
1450 # ignore array size in a parameter string;
1451 # however, the original param string may contain
1452 # spaces, e.g.: addr[6 + 2]
1453 # and this appears in @prms as "addr[6" since the
1454 # parameter list is split at spaces;
1455 # hence just ignore "[..." for the sections check;
1456 $prm_clean =~ s/\[.*//;
1458 ##$prm_clean =~ s/^\**//;
1459 if ($prm_clean eq $sects[$sx]) {
1465 if ($decl_type eq "function") {
1466 print STDERR "${file}:$.: warning: " .
1467 "Excess function parameter " .
1469 "description in '$decl_name'\n";
1477 # Checks the section describing the return value of a function.
1478 sub check_return_section {
1480 my $declaration_name = shift;
1481 my $return_type = shift;
1483 # Ignore an empty return type (It's a macro)
1484 # Ignore functions with a "void" return type. (But don't ignore "void *")
1485 if (($return_type eq "") || ($return_type =~ /void\s*\w*\s*$/)) {
1489 if (!defined($sections{$section_return}) ||
1490 $sections{$section_return} eq "") {
1491 print STDERR "${file}:$.: warning: " .
1492 "No description found for return value of " .
1493 "'$declaration_name'\n";
1499 # takes a function prototype and the name of the current file being
1500 # processed and spits out all the details stored in the global
1502 sub dump_function($$) {
1503 my $prototype = shift;
1507 $prototype =~ s/^static +//;
1508 $prototype =~ s/^extern +//;
1509 $prototype =~ s/^asmlinkage +//;
1510 $prototype =~ s/^inline +//;
1511 $prototype =~ s/^__inline__ +//;
1512 $prototype =~ s/^__inline +//;
1513 $prototype =~ s/^__always_inline +//;
1514 $prototype =~ s/^noinline +//;
1515 $prototype =~ s/__init +//;
1516 $prototype =~ s/__init_or_module +//;
1517 $prototype =~ s/__meminit +//;
1518 $prototype =~ s/__must_check +//;
1519 $prototype =~ s/__weak +//;
1520 my $define = $prototype =~ s/^#\s*define\s+//; #ak added
1521 $prototype =~ s/__attribute__\s*\(\(
1523 [\w\s]++ # attribute name
1524 (?:\([^)]*+\))? # attribute arguments
1525 \s*+,? # optional comma at the end
1529 # Yes, this truly is vile. We are looking for:
1530 # 1. Return type (may be nothing if we're looking at a macro)
1532 # 3. Function parameters.
1534 # All the while we have to watch out for function pointer parameters
1535 # (which IIRC is what the two sections are for), C types (these
1536 # regexps don't even start to express all the possibilities), and
1539 # If you mess with these regexps, it's a good idea to check that
1540 # the following functions' documentation still comes out right:
1541 # - parport_register_device (function pointer parameters)
1542 # - atomic_set (macro)
1543 # - pci_match_device, __copy_to_user (long return type)
1545 if ($define && $prototype =~ m/^()([a-zA-Z0-9_~:]+)\s+/) {
1546 # This is an object-like macro, it has no return type and no parameter
1548 # Function-like macros are not allowed to have spaces between
1549 # declaration_name and opening parenthesis (notice the \s+).
1551 $declaration_name = $2;
1553 } elsif ($prototype =~ m/^()([a-zA-Z0-9_~:]+)\s*\(([^\(]*)\)/ ||
1554 $prototype =~ m/^(\w+)\s+([a-zA-Z0-9_~:]+)\s*\(([^\(]*)\)/ ||
1555 $prototype =~ m/^(\w+\s*\*+)\s*([a-zA-Z0-9_~:]+)\s*\(([^\(]*)\)/ ||
1556 $prototype =~ m/^(\w+\s+\w+)\s+([a-zA-Z0-9_~:]+)\s*\(([^\(]*)\)/ ||
1557 $prototype =~ m/^(\w+\s+\w+\s*\*+)\s*([a-zA-Z0-9_~:]+)\s*\(([^\(]*)\)/ ||
1558 $prototype =~ m/^(\w+\s+\w+\s+\w+)\s+([a-zA-Z0-9_~:]+)\s*\(([^\(]*)\)/ ||
1559 $prototype =~ m/^(\w+\s+\w+\s+\w+\s*\*+)\s*([a-zA-Z0-9_~:]+)\s*\(([^\(]*)\)/ ||
1560 $prototype =~ m/^()([a-zA-Z0-9_~:]+)\s*\(([^\{]*)\)/ ||
1561 $prototype =~ m/^(\w+)\s+([a-zA-Z0-9_~:]+)\s*\(([^\{]*)\)/ ||
1562 $prototype =~ m/^(\w+\s*\*+)\s*([a-zA-Z0-9_~:]+)\s*\(([^\{]*)\)/ ||
1563 $prototype =~ m/^(\w+\s+\w+)\s+([a-zA-Z0-9_~:]+)\s*\(([^\{]*)\)/ ||
1564 $prototype =~ m/^(\w+\s+\w+\s*\*+)\s*([a-zA-Z0-9_~:]+)\s*\(([^\{]*)\)/ ||
1565 $prototype =~ m/^(\w+\s+\w+\s+\w+)\s+([a-zA-Z0-9_~:]+)\s*\(([^\{]*)\)/ ||
1566 $prototype =~ m/^(\w+\s+\w+\s+\w+\s*\*+)\s*([a-zA-Z0-9_~:]+)\s*\(([^\{]*)\)/ ||
1567 $prototype =~ m/^(\w+\s+\w+\s+\w+\s+\w+)\s+([a-zA-Z0-9_~:]+)\s*\(([^\{]*)\)/ ||
1568 $prototype =~ m/^(\w+\s+\w+\s+\w+\s+\w+\s*\*+)\s*([a-zA-Z0-9_~:]+)\s*\(([^\{]*)\)/ ||
1569 $prototype =~ m/^(\w+\s+\w+\s*\*+\s*\w+\s*\*+\s*)\s*([a-zA-Z0-9_~:]+)\s*\(([^\{]*)\)/) {
1571 $declaration_name = $2;
1574 create_parameterlist($args, ',', $file, $declaration_name);
1576 print STDERR "${file}:$.: warning: cannot understand function prototype: '$prototype'\n";
1580 my $prms = join " ", @parameterlist;
1581 check_sections($file, $declaration_name, "function", $sectcheck, $prms);
1583 # This check emits a lot of warnings at the moment, because many
1584 # functions don't have a 'Return' doc section. So until the number
1585 # of warnings goes sufficiently down, the check is only performed in
1587 # TODO: always perform the check.
1588 if ($verbose && !$noret) {
1589 check_return_section($file, $declaration_name, $return_type);
1592 output_declaration($declaration_name,
1594 {'function' => $declaration_name,
1595 'module' => $modulename,
1596 'functiontype' => $return_type,
1597 'parameterlist' => \@parameterlist,
1598 'parameterdescs' => \%parameterdescs,
1599 'parametertypes' => \%parametertypes,
1600 'sectionlist' => \@sectionlist,
1601 'sections' => \%sections,
1602 'purpose' => $declaration_purpose
1608 %parameterdescs = ();
1609 %parametertypes = ();
1610 @parameterlist = ();
1614 $struct_actual = "";
1617 $state = STATE_NORMAL;
1618 $inline_doc_state = STATE_INLINE_NA;
1621 sub tracepoint_munge($) {
1623 my $tracepointname = 0;
1624 my $tracepointargs = 0;
1626 if ($prototype =~ m/TRACE_EVENT\((.*?),/) {
1627 $tracepointname = $1;
1629 if ($prototype =~ m/DEFINE_SINGLE_EVENT\((.*?),/) {
1630 $tracepointname = $1;
1632 if ($prototype =~ m/DEFINE_EVENT\((.*?),(.*?),/) {
1633 $tracepointname = $2;
1635 $tracepointname =~ s/^\s+//; #strip leading whitespace
1636 if ($prototype =~ m/TP_PROTO\((.*?)\)/) {
1637 $tracepointargs = $1;
1639 if (($tracepointname eq 0) || ($tracepointargs eq 0)) {
1640 print STDERR "${file}:$.: warning: Unrecognized tracepoint format: \n".
1643 $prototype = "static inline void trace_$tracepointname($tracepointargs)";
1647 sub syscall_munge() {
1650 $prototype =~ s@[\r\n]+@ @gos; # strip newlines/CR's
1651 ## if ($prototype =~ m/SYSCALL_DEFINE0\s*\(\s*(a-zA-Z0-9_)*\s*\)/) {
1652 if ($prototype =~ m/SYSCALL_DEFINE0/) {
1654 ## $prototype = "long sys_$1(void)";
1657 $prototype =~ s/SYSCALL_DEFINE.*\(/long sys_/; # fix return type & func name
1658 if ($prototype =~ m/long (sys_.*?),/) {
1659 $prototype =~ s/,/\(/;
1661 $prototype =~ s/\)/\(void\)/;
1664 # now delete all of the odd-number commas in $prototype
1665 # so that arg types & arg names don't have a comma between them
1667 my $len = length($prototype);
1669 $len = 0; # skip the for-loop
1671 for (my $ix = 0; $ix < $len; $ix++) {
1672 if (substr($prototype, $ix, 1) eq ',') {
1674 if ($count % 2 == 1) {
1675 substr($prototype, $ix, 1) = ' ';
1681 sub process_proto_function($$) {
1685 $x =~ s@\/\/.*$@@gos; # strip C99-style comments to end of line
1687 if ($x =~ m#\s*/\*\s+MACDOC\s*#io || ($x =~ /^#/ && $x !~ /^#\s*define/)) {
1690 elsif ($x =~ /([^\{]*)/) {
1694 if (($x =~ /\{/) || ($x =~ /\#\s*define/) || ($x =~ /;/)) {
1695 $prototype =~ s@/\*.*?\*/@@gos; # strip comments.
1696 $prototype =~ s@[\r\n]+@ @gos; # strip newlines/cr's.
1697 $prototype =~ s@^\s+@@gos; # strip leading spaces
1698 if ($prototype =~ /SYSCALL_DEFINE/) {
1701 if ($prototype =~ /TRACE_EVENT/ || $prototype =~ /DEFINE_EVENT/ ||
1702 $prototype =~ /DEFINE_SINGLE_EVENT/)
1704 tracepoint_munge($file);
1706 dump_function($prototype, $file);
1711 sub process_proto_type($$) {
1715 $x =~ s@[\r\n]+@ @gos; # strip newlines/cr's.
1716 $x =~ s@^\s+@@gos; # strip leading spaces
1717 $x =~ s@\s+$@@gos; # strip trailing spaces
1718 $x =~ s@\/\/.*$@@gos; # strip C99-style comments to end of line
1721 # To distinguish preprocessor directive from regular declaration later.
1726 if ( $x =~ /([^{};]*)([{};])(.*)/ ) {
1727 if( length $prototype ) {
1730 $prototype .= $1 . $2;
1731 ($2 eq '{') && $brcount++;
1732 ($2 eq '}') && $brcount--;
1733 if (($2 eq ';') && ($brcount == 0)) {
1734 dump_declaration($prototype, $file);
1747 sub map_filename($) {
1749 my ($orig_file) = @_;
1751 if (defined($ENV{'SRCTREE'})) {
1752 $file = "$ENV{'SRCTREE'}" . "/" . $orig_file;
1757 if (defined($source_map{$file})) {
1758 $file = $source_map{$file};
1764 sub process_export_file($) {
1765 my ($orig_file) = @_;
1766 my $file = map_filename($orig_file);
1768 if (!open(IN,"<$file")) {
1769 print STDERR "Error: Cannot open file $file\n";
1775 if (/$export_symbol/) {
1776 $function_table{$2} = 1;
1784 # Parsers for the various processing states.
1786 # STATE_NORMAL: looking for the /** to begin everything.
1788 sub process_normal() {
1789 if (/$doc_start/o) {
1790 $state = STATE_NAME; # next line is always the function name
1792 $declaration_start_line = $. + 1;
1798 sub process_file($) {
1803 my $initial_section_counter = $section_counter;
1804 my ($orig_file) = @_;
1807 $file = map_filename($orig_file);
1809 if (!open(IN,"<$file")) {
1810 print STDERR "Error: Cannot open file $file\n";
1817 $section_counter = 0;
1819 while (s/\\\s*$//) {
1822 # Replace tabs by spaces
1823 while ($_ =~ s/\t+/' ' x (length($&) * 8 - length($`) % 8)/e) {};
1824 if ($state == STATE_NORMAL) {
1826 } elsif ($state == STATE_NAME) {# this line is the function name (always)
1827 if (/$doc_block/o) {
1828 $state = STATE_DOCBLOCK;
1830 $new_start_line = $. + 1;
1833 $section = $section_intro;
1838 elsif (/$doc_decl/o) {
1840 if (/\s*([\w\s]+?)\s*-/) {
1844 $state = STATE_BODY;
1845 # if there's no @param blocks need to set up default section
1848 $section = $section_default;
1849 $new_start_line = $. + 1;
1851 # strip leading/trailing/multiple spaces
1855 $descr =~ s/\s+/ /g;
1856 $declaration_purpose = $descr;
1857 $state = STATE_BODY_MAYBE;
1859 $declaration_purpose = "";
1862 if (($declaration_purpose eq "") && $verbose) {
1863 print STDERR "${file}:$.: warning: missing initial short description on line:\n";
1868 if ($identifier =~ m/^struct/) {
1869 $decl_type = 'struct';
1870 } elsif ($identifier =~ m/^union/) {
1871 $decl_type = 'union';
1872 } elsif ($identifier =~ m/^enum/) {
1873 $decl_type = 'enum';
1874 } elsif ($identifier =~ m/^typedef/) {
1875 $decl_type = 'typedef';
1877 $decl_type = 'function';
1881 print STDERR "${file}:$.: info: Scanning doc for $identifier\n";
1884 print STDERR "${file}:$.: warning: Cannot understand $_ on line $.",
1885 " - I thought it was a doc line\n";
1887 $state = STATE_NORMAL;
1889 } elsif ($state == STATE_BODY || $state == STATE_BODY_MAYBE) {
1890 if (/$doc_sect/i) { # case insensitive for supported section names
1894 # map the supported section names to the canonical names
1895 if ($newsection =~ m/^description$/i) {
1896 $newsection = $section_default;
1897 } elsif ($newsection =~ m/^context$/i) {
1898 $newsection = $section_context;
1899 } elsif ($newsection =~ m/^returns?$/i) {
1900 $newsection = $section_return;
1901 } elsif ($newsection =~ m/^\@return$/) {
1902 # special: @return is a section, not a param description
1903 $newsection = $section_return;
1906 if (($contents ne "") && ($contents ne "\n")) {
1907 if (!$in_doc_sect && $verbose) {
1908 print STDERR "${file}:$.: warning: contents before sections\n";
1911 dump_section($file, $section, $contents);
1912 $section = $section_default;
1916 $state = STATE_BODY;
1917 $contents = $newcontents;
1918 $new_start_line = $.;
1919 while (substr($contents, 0, 1) eq " ") {
1920 $contents = substr($contents, 1);
1922 if ($contents ne "") {
1925 $section = $newsection;
1926 $leading_space = undef;
1927 } elsif (/$doc_end/) {
1928 if (($contents ne "") && ($contents ne "\n")) {
1929 dump_section($file, $section, $contents);
1930 $section = $section_default;
1933 # look for doc_com + <text> + doc_end:
1934 if ($_ =~ m'\s*\*\s*[a-zA-Z_0-9:\.]+\*/') {
1935 print STDERR "${file}:$.: warning: suspicious ending line: $_";
1940 $state = STATE_PROTO;
1942 # print STDERR "end of doc comment, looking for prototype\n";
1943 } elsif (/$doc_content/) {
1944 # miguel-style comment kludge, look for blank lines after
1945 # @parameter line to signify start of description
1947 if ($section =~ m/^@/ || $section eq $section_context) {
1948 dump_section($file, $section, $contents);
1949 $section = $section_default;
1951 $new_start_line = $.;
1955 $state = STATE_BODY;
1956 } elsif ($state == STATE_BODY_MAYBE) {
1957 # Continued declaration purpose
1958 chomp($declaration_purpose);
1959 $declaration_purpose .= " " . $1;
1960 $declaration_purpose =~ s/\s+/ /g;
1963 if ($section =~ m/^@/ || $section eq $section_context) {
1964 if (!defined $leading_space) {
1965 if ($cont =~ m/^(\s+)/) {
1966 $leading_space = $1;
1968 $leading_space = "";
1972 $cont =~ s/^$leading_space//;
1974 $contents .= $cont . "\n";
1977 # i dont know - bad line? ignore.
1978 print STDERR "${file}:$.: warning: bad line: $_";
1981 } elsif ($state == STATE_INLINE) { # scanning for inline parameters
1982 # First line (state 1) needs to be a @parameter
1983 if ($inline_doc_state == STATE_INLINE_NAME && /$doc_inline_sect/o) {
1986 $new_start_line = $.;
1987 if ($contents ne "") {
1988 while (substr($contents, 0, 1) eq " ") {
1989 $contents = substr($contents, 1);
1993 $inline_doc_state = STATE_INLINE_TEXT;
1994 # Documentation block end */
1995 } elsif (/$doc_inline_end/) {
1996 if (($contents ne "") && ($contents ne "\n")) {
1997 dump_section($file, $section, $contents);
1998 $section = $section_default;
2001 $state = STATE_PROTO;
2002 $inline_doc_state = STATE_INLINE_NA;
2004 } elsif (/$doc_content/) {
2005 if ($inline_doc_state == STATE_INLINE_TEXT) {
2006 $contents .= $1 . "\n";
2007 # nuke leading blank lines
2008 if ($contents =~ /^\s*$/) {
2011 } elsif ($inline_doc_state == STATE_INLINE_NAME) {
2012 $inline_doc_state = STATE_INLINE_ERROR;
2013 print STDERR "${file}:$.: warning: ";
2014 print STDERR "Incorrect use of kernel-doc format: $_";
2018 } elsif ($state == STATE_PROTO) { # scanning for function '{' (end of prototype)
2019 if (/$doc_inline_oneline/) {
2022 if ($contents ne "") {
2024 dump_section($file, $section, $contents);
2025 $section = $section_default;
2028 } elsif (/$doc_inline_start/) {
2029 $state = STATE_INLINE;
2030 $inline_doc_state = STATE_INLINE_NAME;
2031 } elsif ($decl_type eq 'function') {
2032 process_proto_function($_, $file);
2034 process_proto_type($_, $file);
2036 } elsif ($state == STATE_DOCBLOCK) {
2039 dump_doc_section($file, $section, $contents);
2040 $section = $section_default;
2043 %parameterdescs = ();
2044 %parametertypes = ();
2045 @parameterlist = ();
2049 $state = STATE_NORMAL;
2051 elsif (/$doc_content/)
2055 $contents .= $blankline;
2059 $contents .= $1 . "\n";
2064 if ($initial_section_counter == $section_counter) {
2065 if ($output_mode ne "none") {
2066 print STDERR "${file}:1: warning: no structured comments found\n";
2068 if (($output_selection == OUTPUT_INCLUDE) && ($show_not_found == 1)) {
2069 print STDERR " Was looking for '$_'.\n" for keys %function_table;
2075 $kernelversion = get_kernel_version();
2077 # generate a sequence of code that will splice in highlighting information
2078 # using the s// operator.
2079 for (my $k = 0; $k < @highlights; $k++) {
2080 my $pattern = $highlights[$k][0];
2081 my $result = $highlights[$k][1];
2082 # print STDERR "scanning pattern:$pattern, highlight:($result)\n";
2083 $dohighlight .= "\$contents =~ s:$pattern:$result:gs;\n";
2086 # Read the file that maps relative names to absolute names for
2087 # separate source and object directories and for shadow trees.
2088 if (open(SOURCE_MAP, "<.tmp_filelist.txt")) {
2089 my ($relname, $absname);
2090 while(<SOURCE_MAP>) {
2092 ($relname, $absname) = (split())[0..1];
2093 $relname =~ s:^/+::;
2094 $source_map{$relname} = $absname;
2099 if ($output_selection == OUTPUT_EXPORTED ||
2100 $output_selection == OUTPUT_INTERNAL) {
2102 push(@export_file_list, @ARGV);
2104 foreach (@export_file_list) {
2106 process_export_file($_);
2114 if ($verbose && $errors) {
2115 print STDERR "$errors errors\n";
2117 if ($verbose && $warnings) {
2118 print STDERR "$warnings warnings\n";
2121 exit($output_mode eq "none" ? 0 : $errors);