scripts: kernel-doc: don't mangle with parameter list
[qemu.git] / scripts / kernel-doc
blob478037f736e29c329507065308f75c08de77e001
1 #!/usr/bin/env perl
2 # SPDX-License-Identifier: GPL-2.0
4 use warnings;
5 use strict;
7 ## Copyright (c) 1998 Michael Zucchi, All Rights Reserved ##
8 ## Copyright (C) 2000, 1 Tim Waugh <twaugh@redhat.com> ##
9 ## Copyright (C) 2001 Simon Huggins ##
10 ## Copyright (C) 2005-2012 Randy Dunlap ##
11 ## Copyright (C) 2012 Dan Luedtke ##
12 ## ##
13 ## #define enhancements by Armin Kuster <akuster@mvista.com> ##
14 ## Copyright (c) 2000 MontaVista Software, Inc. ##
15 ## ##
16 ## This software falls under the GNU General Public License. ##
17 ## Please read the COPYING file for more information ##
19 # 18/01/2001 - Cleanups
20 # Functions prototyped as foo(void) same as foo()
21 # Stop eval'ing where we don't need to.
22 # -- huggie@earth.li
24 # 27/06/2001 - Allowed whitespace after initial "/**" and
25 # allowed comments before function declarations.
26 # -- Christian Kreibich <ck@whoop.org>
28 # Still to do:
29 # - add perldoc documentation
30 # - Look more closely at some of the scarier bits :)
32 # 26/05/2001 - Support for separate source and object trees.
33 # Return error code.
34 # Keith Owens <kaos@ocs.com.au>
36 # 23/09/2001 - Added support for typedefs, structs, enums and unions
37 # Support for Context section; can be terminated using empty line
38 # Small fixes (like spaces vs. \s in regex)
39 # -- Tim Jansen <tim@tjansen.de>
41 # 25/07/2012 - Added support for HTML5
42 # -- Dan Luedtke <mail@danrl.de>
44 sub usage {
45 my $message = <<"EOF";
46 Usage: $0 [OPTION ...] FILE ...
48 Read C language source or header FILEs, extract embedded documentation comments,
49 and print formatted documentation to standard output.
51 The documentation comments are identified by "/**" opening comment mark. See
52 Documentation/doc-guide/kernel-doc.rst for the documentation comment syntax.
54 Output format selection (mutually exclusive):
55 -man Output troff manual page format. This is the default.
56 -rst Output reStructuredText format.
57 -none Do not output documentation, only warnings.
59 Output selection (mutually exclusive):
60 -export Only output documentation for symbols that have been
61 exported using EXPORT_SYMBOL() or EXPORT_SYMBOL_GPL()
62 in any input FILE or -export-file FILE.
63 -internal Only output documentation for symbols that have NOT been
64 exported using EXPORT_SYMBOL() or EXPORT_SYMBOL_GPL()
65 in any input FILE or -export-file FILE.
66 -function NAME Only output documentation for the given function(s)
67 or DOC: section title(s). All other functions and DOC:
68 sections are ignored. May be specified multiple times.
69 -nosymbol NAME Exclude the specified symbols from the output
70 documentation. May be specified multiple times.
72 Output selection modifiers:
73 -no-doc-sections Do not output DOC: sections.
74 -enable-lineno Enable output of #define LINENO lines. Only works with
75 reStructuredText format.
76 -export-file FILE Specify an additional FILE in which to look for
77 EXPORT_SYMBOL() and EXPORT_SYMBOL_GPL(). To be used with
78 -export or -internal. May be specified multiple times.
80 Other parameters:
81 -v Verbose output, more warnings and other information.
82 -h Print this help.
83 -Werror Treat warnings as errors.
85 EOF
86 print $message;
87 exit 1;
91 # format of comments.
92 # In the following table, (...)? signifies optional structure.
93 # (...)* signifies 0 or more structure elements
94 # /**
95 # * function_name(:)? (- short description)?
96 # (* @parameterx: (description of parameter x)?)*
97 # (* a blank line)?
98 # * (Description:)? (Description of function)?
99 # * (section header: (section description)? )*
100 # (*)?*/
102 # So .. the trivial example would be:
104 # /**
105 # * my_function
106 # */
108 # If the Description: header tag is omitted, then there must be a blank line
109 # after the last parameter specification.
110 # e.g.
111 # /**
112 # * my_function - does my stuff
113 # * @my_arg: its mine damnit
115 # * Does my stuff explained.
116 # */
118 # or, could also use:
119 # /**
120 # * my_function - does my stuff
121 # * @my_arg: its mine damnit
122 # * Description: Does my stuff explained.
123 # */
124 # etc.
126 # Besides functions you can also write documentation for structs, unions,
127 # enums and typedefs. Instead of the function name you must write the name
128 # of the declaration; the struct/union/enum/typedef must always precede
129 # the name. Nesting of declarations is not supported.
130 # Use the argument mechanism to document members or constants.
131 # e.g.
132 # /**
133 # * struct my_struct - short description
134 # * @a: first member
135 # * @b: second member
137 # * Longer description
138 # */
139 # struct my_struct {
140 # int a;
141 # int b;
142 # /* private: */
143 # int c;
144 # };
146 # All descriptions can be multiline, except the short function description.
148 # For really longs structs, you can also describe arguments inside the
149 # body of the struct.
150 # eg.
151 # /**
152 # * struct my_struct - short description
153 # * @a: first member
154 # * @b: second member
156 # * Longer description
157 # */
158 # struct my_struct {
159 # int a;
160 # int b;
161 # /**
162 # * @c: This is longer description of C
164 # * You can use paragraphs to describe arguments
165 # * using this method.
166 # */
167 # int c;
168 # };
170 # This should be use only for struct/enum members.
172 # You can also add additional sections. When documenting kernel functions you
173 # should document the "Context:" of the function, e.g. whether the functions
174 # can be called form interrupts. Unlike other sections you can end it with an
175 # empty line.
176 # A non-void function should have a "Return:" section describing the return
177 # value(s).
178 # Example-sections should contain the string EXAMPLE so that they are marked
179 # appropriately in DocBook.
181 # Example:
182 # /**
183 # * user_function - function that can only be called in user context
184 # * @a: some argument
185 # * Context: !in_interrupt()
187 # * Some description
188 # * Example:
189 # * user_function(22);
190 # */
191 # ...
194 # All descriptive text is further processed, scanning for the following special
195 # patterns, which are highlighted appropriately.
197 # 'funcname()' - function
198 # '$ENVVAR' - environmental variable
199 # '&struct_name' - name of a structure (up to two words including 'struct')
200 # '&struct_name.member' - name of a structure member
201 # '@parameter' - name of a parameter
202 # '%CONST' - name of a constant.
203 # '``LITERAL``' - literal string without any spaces on it.
205 ## init lots of data
207 my $errors = 0;
208 my $warnings = 0;
209 my $anon_struct_union = 0;
211 # match expressions used to find embedded type information
212 my $type_constant = '\b``([^\`]+)``\b';
213 my $type_constant2 = '\%([-_\w]+)';
214 my $type_func = '(\w+)\(\)';
215 my $type_param = '\@(\w*((\.\w+)|(->\w+))*(\.\.\.)?)';
216 my $type_param_ref = '([\!]?)\@(\w*((\.\w+)|(->\w+))*(\.\.\.)?)';
217 my $type_fp_param = '\@(\w+)\(\)'; # Special RST handling for func ptr params
218 my $type_fp_param2 = '\@(\w+->\S+)\(\)'; # Special RST handling for structs with func ptr params
219 my $type_env = '(\$\w+)';
220 my $type_enum = '#(enum\s*([_\w]+))';
221 my $type_struct = '#(struct\s*([_\w]+))';
222 my $type_typedef = '#(([A-Z][_\w]*))';
223 my $type_union = '#(union\s*([_\w]+))';
224 my $type_member = '#([_\w]+)(\.|->)([_\w]+)';
225 my $type_fallback = '(?!)'; # this never matches
226 my $type_member_func = $type_member . '\(\)';
228 # Output conversion substitutions.
229 # One for each output format
231 # these are pretty rough
232 my @highlights_man = (
233 [$type_constant, "\$1"],
234 [$type_constant2, "\$1"],
235 [$type_func, "\\\\fB\$1\\\\fP"],
236 [$type_enum, "\\\\fI\$1\\\\fP"],
237 [$type_struct, "\\\\fI\$1\\\\fP"],
238 [$type_typedef, "\\\\fI\$1\\\\fP"],
239 [$type_union, "\\\\fI\$1\\\\fP"],
240 [$type_param, "\\\\fI\$1\\\\fP"],
241 [$type_param_ref, "\\\\fI\$1\$2\\\\fP"],
242 [$type_member, "\\\\fI\$1\$2\$3\\\\fP"],
243 [$type_fallback, "\\\\fI\$1\\\\fP"]
245 my $blankline_man = "";
247 # rst-mode
248 my @highlights_rst = (
249 [$type_constant, "``\$1``"],
250 [$type_constant2, "``\$1``"],
251 # Note: need to escape () to avoid func matching later
252 [$type_member_func, "\\:c\\:type\\:`\$1\$2\$3\\\\(\\\\) <\$1>`"],
253 [$type_member, "\\:c\\:type\\:`\$1\$2\$3 <\$1>`"],
254 [$type_fp_param, "**\$1\\\\(\\\\)**"],
255 [$type_fp_param2, "**\$1\\\\(\\\\)**"],
256 [$type_func, "\$1()"],
257 [$type_enum, "\\:c\\:type\\:`\$1 <\$2>`"],
258 [$type_struct, "\\:c\\:type\\:`\$1 <\$2>`"],
259 [$type_typedef, "\\:c\\:type\\:`\$1 <\$2>`"],
260 [$type_union, "\\:c\\:type\\:`\$1 <\$2>`"],
261 # in rst this can refer to any type
262 [$type_fallback, "\\:c\\:type\\:`\$1`"],
263 [$type_param_ref, "**\$1\$2**"]
265 my $blankline_rst = "\n";
267 # read arguments
268 if ($#ARGV == -1) {
269 usage();
272 my $kernelversion;
273 my $sphinx_major;
275 my $dohighlight = "";
277 my $verbose = 0;
278 my $Werror = 0;
279 my $output_mode = "rst";
280 my $output_preformatted = 0;
281 my $no_doc_sections = 0;
282 my $enable_lineno = 0;
283 my @highlights = @highlights_rst;
284 my $blankline = $blankline_rst;
285 my $modulename = "Kernel API";
287 use constant {
288 OUTPUT_ALL => 0, # output all symbols and doc sections
289 OUTPUT_INCLUDE => 1, # output only specified symbols
290 OUTPUT_EXPORTED => 2, # output exported symbols
291 OUTPUT_INTERNAL => 3, # output non-exported symbols
293 my $output_selection = OUTPUT_ALL;
294 my $show_not_found = 0; # No longer used
296 my @export_file_list;
298 my @build_time;
299 if (defined($ENV{'KBUILD_BUILD_TIMESTAMP'}) &&
300 (my $seconds = `date -d"${ENV{'KBUILD_BUILD_TIMESTAMP'}}" +%s`) ne '') {
301 @build_time = gmtime($seconds);
302 } else {
303 @build_time = localtime;
306 my $man_date = ('January', 'February', 'March', 'April', 'May', 'June',
307 'July', 'August', 'September', 'October',
308 'November', 'December')[$build_time[4]] .
309 " " . ($build_time[5]+1900);
311 # Essentially these are globals.
312 # They probably want to be tidied up, made more localised or something.
313 # CAVEAT EMPTOR! Some of the others I localised may not want to be, which
314 # could cause "use of undefined value" or other bugs.
315 my ($function, %function_table, %parametertypes, $declaration_purpose);
316 my %nosymbol_table = ();
317 my $declaration_start_line;
318 my ($type, $declaration_name, $return_type);
319 my ($newsection, $newcontents, $prototype, $brcount, %source_map);
321 if (defined($ENV{'KBUILD_VERBOSE'})) {
322 $verbose = "$ENV{'KBUILD_VERBOSE'}";
325 if (defined($ENV{'KDOC_WERROR'})) {
326 $Werror = "$ENV{'KDOC_WERROR'}";
329 if (defined($ENV{'KCFLAGS'})) {
330 my $kcflags = "$ENV{'KCFLAGS'}";
332 if ($kcflags =~ /Werror/) {
333 $Werror = 1;
337 # Generated docbook code is inserted in a template at a point where
338 # docbook v3.1 requires a non-zero sequence of RefEntry's; see:
339 # https://www.oasis-open.org/docbook/documentation/reference/html/refentry.html
340 # We keep track of number of generated entries and generate a dummy
341 # if needs be to ensure the expanded template can be postprocessed
342 # into html.
343 my $section_counter = 0;
345 my $lineprefix="";
347 # Parser states
348 use constant {
349 STATE_NORMAL => 0, # normal code
350 STATE_NAME => 1, # looking for function name
351 STATE_BODY_MAYBE => 2, # body - or maybe more description
352 STATE_BODY => 3, # the body of the comment
353 STATE_BODY_WITH_BLANK_LINE => 4, # the body, which has a blank line
354 STATE_PROTO => 5, # scanning prototype
355 STATE_DOCBLOCK => 6, # documentation block
356 STATE_INLINE => 7, # gathering doc outside main block
358 my $state;
359 my $in_doc_sect;
360 my $leading_space;
362 # Inline documentation state
363 use constant {
364 STATE_INLINE_NA => 0, # not applicable ($state != STATE_INLINE)
365 STATE_INLINE_NAME => 1, # looking for member name (@foo:)
366 STATE_INLINE_TEXT => 2, # looking for member documentation
367 STATE_INLINE_END => 3, # done
368 STATE_INLINE_ERROR => 4, # error - Comment without header was found.
369 # Spit a warning as it's not
370 # proper kernel-doc and ignore the rest.
372 my $inline_doc_state;
374 #declaration types: can be
375 # 'function', 'struct', 'union', 'enum', 'typedef'
376 my $decl_type;
378 my $doc_start = '^/\*\*\s*$'; # Allow whitespace at end of comment start.
379 my $doc_end = '\*/';
380 my $doc_com = '\s*\*\s*';
381 my $doc_com_body = '\s*\* ?';
382 my $doc_decl = $doc_com . '(\w+)';
383 # @params and a strictly limited set of supported section names
384 my $doc_sect = $doc_com .
385 '\s*(\@[.\w]+|\@\.\.\.|description|context|returns?|notes?|examples?)\s*:(.*)';
386 my $doc_content = $doc_com_body . '(.*)';
387 my $doc_block = $doc_com . 'DOC:\s*(.*)?';
388 my $doc_inline_start = '^\s*/\*\*\s*$';
389 my $doc_inline_sect = '\s*\*\s*(@\s*[\w][\w\.]*\s*):(.*)';
390 my $doc_inline_end = '^\s*\*/\s*$';
391 my $doc_inline_oneline = '^\s*/\*\*\s*(@[\w\s]+):\s*(.*)\s*\*/\s*$';
392 my $export_symbol = '^\s*EXPORT_SYMBOL(_GPL)?\s*\(\s*(\w+)\s*\)\s*;';
394 my %parameterdescs;
395 my %parameterdesc_start_lines;
396 my @parameterlist;
397 my %sections;
398 my @sectionlist;
399 my %section_start_lines;
400 my $sectcheck;
401 my $struct_actual;
403 my $contents = "";
404 my $new_start_line = 0;
406 # the canonical section names. see also $doc_sect above.
407 my $section_default = "Description"; # default section
408 my $section_intro = "Introduction";
409 my $section = $section_default;
410 my $section_context = "Context";
411 my $section_return = "Return";
413 my $undescribed = "-- undescribed --";
415 reset_state();
417 while ($ARGV[0] =~ m/^--?(.*)/) {
418 my $cmd = $1;
419 shift @ARGV;
420 if ($cmd eq "man") {
421 $output_mode = "man";
422 @highlights = @highlights_man;
423 $blankline = $blankline_man;
424 } elsif ($cmd eq "rst") {
425 $output_mode = "rst";
426 @highlights = @highlights_rst;
427 $blankline = $blankline_rst;
428 } elsif ($cmd eq "none") {
429 $output_mode = "none";
430 } elsif ($cmd eq "module") { # not needed for XML, inherits from calling document
431 $modulename = shift @ARGV;
432 } elsif ($cmd eq "function") { # to only output specific functions
433 $output_selection = OUTPUT_INCLUDE;
434 $function = shift @ARGV;
435 $function_table{$function} = 1;
436 } elsif ($cmd eq "nosymbol") { # Exclude specific symbols
437 my $symbol = shift @ARGV;
438 $nosymbol_table{$symbol} = 1;
439 } elsif ($cmd eq "export") { # only exported symbols
440 $output_selection = OUTPUT_EXPORTED;
441 %function_table = ();
442 } elsif ($cmd eq "internal") { # only non-exported symbols
443 $output_selection = OUTPUT_INTERNAL;
444 %function_table = ();
445 } elsif ($cmd eq "export-file") {
446 my $file = shift @ARGV;
447 push(@export_file_list, $file);
448 } elsif ($cmd eq "v") {
449 $verbose = 1;
450 } elsif ($cmd eq "Werror") {
451 $Werror = 1;
452 } elsif (($cmd eq "h") || ($cmd eq "help")) {
453 usage();
454 } elsif ($cmd eq 'no-doc-sections') {
455 $no_doc_sections = 1;
456 } elsif ($cmd eq 'enable-lineno') {
457 $enable_lineno = 1;
458 } elsif ($cmd eq 'show-not-found') {
459 $show_not_found = 1; # A no-op but don't fail
460 } else {
461 # Unknown argument
462 usage();
466 # continue execution near EOF;
468 # The C domain dialect changed on Sphinx 3. So, we need to check the
469 # version in order to produce the right tags.
470 sub findprog($)
472 foreach(split(/:/, $ENV{PATH})) {
473 return "$_/$_[0]" if(-x "$_/$_[0]");
477 sub get_sphinx_version()
479 my $ver;
480 my $major = 1;
482 my $cmd = "sphinx-build";
483 if (!findprog($cmd)) {
484 my $cmd = "sphinx-build3";
485 return $major if (!findprog($cmd));
488 open IN, "$cmd --version 2>&1 |";
489 while (<IN>) {
490 if (m/^\s*sphinx-build\s+([\d]+)\.([\d\.]+)(\+\/[\da-f]+)?$/) {
491 $major=$1;
492 last;
494 # Sphinx 1.2.x uses a different format
495 if (m/^\s*Sphinx.*\s+([\d]+)\.([\d\.]+)$/) {
496 $major=$1;
497 last;
500 close IN;
502 return $major;
505 # get kernel version from env
506 sub get_kernel_version() {
507 my $version = 'unknown kernel version';
509 if (defined($ENV{'KERNELVERSION'})) {
510 $version = $ENV{'KERNELVERSION'};
512 return $version;
516 sub print_lineno {
517 my $lineno = shift;
518 if ($enable_lineno && defined($lineno)) {
519 print "#define LINENO " . $lineno . "\n";
523 # dumps section contents to arrays/hashes intended for that purpose.
525 sub dump_section {
526 my $file = shift;
527 my $name = shift;
528 my $contents = join "\n", @_;
530 if ($name =~ m/$type_param/) {
531 $name = $1;
532 $parameterdescs{$name} = $contents;
533 $sectcheck = $sectcheck . $name . " ";
534 $parameterdesc_start_lines{$name} = $new_start_line;
535 $new_start_line = 0;
536 } elsif ($name eq "@\.\.\.") {
537 $name = "...";
538 $parameterdescs{$name} = $contents;
539 $sectcheck = $sectcheck . $name . " ";
540 $parameterdesc_start_lines{$name} = $new_start_line;
541 $new_start_line = 0;
542 } else {
543 if (defined($sections{$name}) && ($sections{$name} ne "")) {
544 # Only warn on user specified duplicate section names.
545 if ($name ne $section_default) {
546 print STDERR "${file}:$.: warning: duplicate section name '$name'\n";
547 ++$warnings;
549 $sections{$name} .= $contents;
550 } else {
551 $sections{$name} = $contents;
552 push @sectionlist, $name;
553 $section_start_lines{$name} = $new_start_line;
554 $new_start_line = 0;
560 # dump DOC: section after checking that it should go out
562 sub dump_doc_section {
563 my $file = shift;
564 my $name = shift;
565 my $contents = join "\n", @_;
567 if ($no_doc_sections) {
568 return;
571 return if (defined($nosymbol_table{$name}));
573 if (($output_selection == OUTPUT_ALL) ||
574 (($output_selection == OUTPUT_INCLUDE) &&
575 defined($function_table{$name})))
577 dump_section($file, $name, $contents);
578 output_blockhead({'sectionlist' => \@sectionlist,
579 'sections' => \%sections,
580 'module' => $modulename,
581 'content-only' => ($output_selection != OUTPUT_ALL), });
586 # output function
588 # parameterdescs, a hash.
589 # function => "function name"
590 # parameterlist => @list of parameters
591 # parameterdescs => %parameter descriptions
592 # sectionlist => @list of sections
593 # sections => %section descriptions
596 sub output_highlight {
597 my $contents = join "\n",@_;
598 my $line;
600 # DEBUG
601 # if (!defined $contents) {
602 # use Carp;
603 # confess "output_highlight got called with no args?\n";
606 # print STDERR "contents b4:$contents\n";
607 eval $dohighlight;
608 die $@ if $@;
609 # print STDERR "contents af:$contents\n";
611 foreach $line (split "\n", $contents) {
612 if (! $output_preformatted) {
613 $line =~ s/^\s*//;
615 if ($line eq ""){
616 if (! $output_preformatted) {
617 print $lineprefix, $blankline;
619 } else {
620 if ($output_mode eq "man" && substr($line, 0, 1) eq ".") {
621 print "\\&$line";
622 } else {
623 print $lineprefix, $line;
626 print "\n";
631 # output function in man
632 sub output_function_man(%) {
633 my %args = %{$_[0]};
634 my ($parameter, $section);
635 my $count;
637 print ".TH \"$args{'function'}\" 9 \"$args{'function'}\" \"$man_date\" \"Kernel Hacker's Manual\" LINUX\n";
639 print ".SH NAME\n";
640 print $args{'function'} . " \\- " . $args{'purpose'} . "\n";
642 print ".SH SYNOPSIS\n";
643 if ($args{'functiontype'} ne "") {
644 print ".B \"" . $args{'functiontype'} . "\" " . $args{'function'} . "\n";
645 } else {
646 print ".B \"" . $args{'function'} . "\n";
648 $count = 0;
649 my $parenth = "(";
650 my $post = ",";
651 foreach my $parameter (@{$args{'parameterlist'}}) {
652 if ($count == $#{$args{'parameterlist'}}) {
653 $post = ");";
655 $type = $args{'parametertypes'}{$parameter};
656 if ($type =~ m/([^\(]*\(\*)\s*\)\s*\(([^\)]*)\)/) {
657 # pointer-to-function
658 print ".BI \"" . $parenth . $1 . "\" " . " \") (" . $2 . ")" . $post . "\"\n";
659 } else {
660 $type =~ s/([^\*])$/$1 /;
661 print ".BI \"" . $parenth . $type . "\" " . " \"" . $post . "\"\n";
663 $count++;
664 $parenth = "";
667 print ".SH ARGUMENTS\n";
668 foreach $parameter (@{$args{'parameterlist'}}) {
669 my $parameter_name = $parameter;
670 $parameter_name =~ s/\[.*//;
672 print ".IP \"" . $parameter . "\" 12\n";
673 output_highlight($args{'parameterdescs'}{$parameter_name});
675 foreach $section (@{$args{'sectionlist'}}) {
676 print ".SH \"", uc $section, "\"\n";
677 output_highlight($args{'sections'}{$section});
682 # output enum in man
683 sub output_enum_man(%) {
684 my %args = %{$_[0]};
685 my ($parameter, $section);
686 my $count;
688 print ".TH \"$args{'module'}\" 9 \"enum $args{'enum'}\" \"$man_date\" \"API Manual\" LINUX\n";
690 print ".SH NAME\n";
691 print "enum " . $args{'enum'} . " \\- " . $args{'purpose'} . "\n";
693 print ".SH SYNOPSIS\n";
694 print "enum " . $args{'enum'} . " {\n";
695 $count = 0;
696 foreach my $parameter (@{$args{'parameterlist'}}) {
697 print ".br\n.BI \" $parameter\"\n";
698 if ($count == $#{$args{'parameterlist'}}) {
699 print "\n};\n";
700 last;
702 else {
703 print ", \n.br\n";
705 $count++;
708 print ".SH Constants\n";
709 foreach $parameter (@{$args{'parameterlist'}}) {
710 my $parameter_name = $parameter;
711 $parameter_name =~ s/\[.*//;
713 print ".IP \"" . $parameter . "\" 12\n";
714 output_highlight($args{'parameterdescs'}{$parameter_name});
716 foreach $section (@{$args{'sectionlist'}}) {
717 print ".SH \"$section\"\n";
718 output_highlight($args{'sections'}{$section});
723 # output struct in man
724 sub output_struct_man(%) {
725 my %args = %{$_[0]};
726 my ($parameter, $section);
728 print ".TH \"$args{'module'}\" 9 \"" . $args{'type'} . " " . $args{'struct'} . "\" \"$man_date\" \"API Manual\" LINUX\n";
730 print ".SH NAME\n";
731 print $args{'type'} . " " . $args{'struct'} . " \\- " . $args{'purpose'} . "\n";
733 my $declaration = $args{'definition'};
734 $declaration =~ s/\t/ /g;
735 $declaration =~ s/\n/"\n.br\n.BI \"/g;
736 print ".SH SYNOPSIS\n";
737 print $args{'type'} . " " . $args{'struct'} . " {\n.br\n";
738 print ".BI \"$declaration\n};\n.br\n\n";
740 print ".SH Members\n";
741 foreach $parameter (@{$args{'parameterlist'}}) {
742 ($parameter =~ /^#/) && next;
744 my $parameter_name = $parameter;
745 $parameter_name =~ s/\[.*//;
747 ($args{'parameterdescs'}{$parameter_name} ne $undescribed) || next;
748 print ".IP \"" . $parameter . "\" 12\n";
749 output_highlight($args{'parameterdescs'}{$parameter_name});
751 foreach $section (@{$args{'sectionlist'}}) {
752 print ".SH \"$section\"\n";
753 output_highlight($args{'sections'}{$section});
758 # output typedef in man
759 sub output_typedef_man(%) {
760 my %args = %{$_[0]};
761 my ($parameter, $section);
763 print ".TH \"$args{'module'}\" 9 \"$args{'typedef'}\" \"$man_date\" \"API Manual\" LINUX\n";
765 print ".SH NAME\n";
766 print "typedef " . $args{'typedef'} . " \\- " . $args{'purpose'} . "\n";
768 foreach $section (@{$args{'sectionlist'}}) {
769 print ".SH \"$section\"\n";
770 output_highlight($args{'sections'}{$section});
774 sub output_blockhead_man(%) {
775 my %args = %{$_[0]};
776 my ($parameter, $section);
777 my $count;
779 print ".TH \"$args{'module'}\" 9 \"$args{'module'}\" \"$man_date\" \"API Manual\" LINUX\n";
781 foreach $section (@{$args{'sectionlist'}}) {
782 print ".SH \"$section\"\n";
783 output_highlight($args{'sections'}{$section});
788 # output in restructured text
792 # This could use some work; it's used to output the DOC: sections, and
793 # starts by putting out the name of the doc section itself, but that tends
794 # to duplicate a header already in the template file.
796 sub output_blockhead_rst(%) {
797 my %args = %{$_[0]};
798 my ($parameter, $section);
800 foreach $section (@{$args{'sectionlist'}}) {
801 next if (defined($nosymbol_table{$section}));
803 if ($output_selection != OUTPUT_INCLUDE) {
804 print "**$section**\n\n";
806 print_lineno($section_start_lines{$section});
807 output_highlight_rst($args{'sections'}{$section});
808 print "\n";
813 # Apply the RST highlights to a sub-block of text.
815 sub highlight_block($) {
816 # The dohighlight kludge requires the text be called $contents
817 my $contents = shift;
818 eval $dohighlight;
819 die $@ if $@;
820 return $contents;
824 # Regexes used only here.
826 my $sphinx_literal = '^[^.].*::$';
827 my $sphinx_cblock = '^\.\.\ +code-block::';
829 sub output_highlight_rst {
830 my $input = join "\n",@_;
831 my $output = "";
832 my $line;
833 my $in_literal = 0;
834 my $litprefix;
835 my $block = "";
837 foreach $line (split "\n",$input) {
839 # If we're in a literal block, see if we should drop out
840 # of it. Otherwise pass the line straight through unmunged.
842 if ($in_literal) {
843 if (! ($line =~ /^\s*$/)) {
845 # If this is the first non-blank line in a literal
846 # block we need to figure out what the proper indent is.
848 if ($litprefix eq "") {
849 $line =~ /^(\s*)/;
850 $litprefix = '^' . $1;
851 $output .= $line . "\n";
852 } elsif (! ($line =~ /$litprefix/)) {
853 $in_literal = 0;
854 } else {
855 $output .= $line . "\n";
857 } else {
858 $output .= $line . "\n";
862 # Not in a literal block (or just dropped out)
864 if (! $in_literal) {
865 $block .= $line . "\n";
866 if (($line =~ /$sphinx_literal/) || ($line =~ /$sphinx_cblock/)) {
867 $in_literal = 1;
868 $litprefix = "";
869 $output .= highlight_block($block);
870 $block = ""
875 if ($block) {
876 $output .= highlight_block($block);
878 foreach $line (split "\n", $output) {
879 print $lineprefix . $line . "\n";
883 sub output_function_rst(%) {
884 my %args = %{$_[0]};
885 my ($parameter, $section);
886 my $oldprefix = $lineprefix;
887 my $start = "";
889 if ($sphinx_major < 3) {
890 if ($args{'typedef'}) {
891 print ".. c:type:: ". $args{'function'} . "\n\n";
892 print_lineno($declaration_start_line);
893 print " **Typedef**: ";
894 $lineprefix = "";
895 output_highlight_rst($args{'purpose'});
896 $start = "\n\n**Syntax**\n\n ``";
897 } else {
898 print ".. c:function:: ";
900 } else {
901 print ".. c:macro:: ". $args{'function'} . "\n\n";
903 if ($args{'typedef'}) {
904 print_lineno($declaration_start_line);
905 print " **Typedef**: ";
906 $lineprefix = "";
907 output_highlight_rst($args{'purpose'});
908 $start = "\n\n**Syntax**\n\n ``";
909 } else {
910 print "``";
913 if ($args{'functiontype'} ne "") {
914 $start .= $args{'functiontype'} . " " . $args{'function'} . " (";
915 } else {
916 $start .= $args{'function'} . " (";
918 print $start;
920 my $count = 0;
921 foreach my $parameter (@{$args{'parameterlist'}}) {
922 if ($count ne 0) {
923 print ", ";
925 $count++;
926 $type = $args{'parametertypes'}{$parameter};
928 if ($type =~ m/([^\(]*\(\*)\s*\)\s*\(([^\)]*)\)/) {
929 # pointer-to-function
930 print $1 . $parameter . ") (" . $2 . ")";
931 } else {
932 print $type;
935 if ($args{'typedef'}) {
936 print ");``\n\n";
937 } else {
938 if ($sphinx_major < 3) {
939 print ")\n\n";
940 } else {
941 print ")``\n";
943 print_lineno($declaration_start_line);
944 $lineprefix = " ";
945 output_highlight_rst($args{'purpose'});
946 print "\n";
949 print "**Parameters**\n\n";
950 $lineprefix = " ";
951 foreach $parameter (@{$args{'parameterlist'}}) {
952 my $parameter_name = $parameter;
953 $parameter_name =~ s/\[.*//;
954 $type = $args{'parametertypes'}{$parameter};
956 if ($type ne "") {
957 print "``$type``\n";
958 } else {
959 print "``$parameter``\n";
962 print_lineno($parameterdesc_start_lines{$parameter_name});
964 if (defined($args{'parameterdescs'}{$parameter_name}) &&
965 $args{'parameterdescs'}{$parameter_name} ne $undescribed) {
966 output_highlight_rst($args{'parameterdescs'}{$parameter_name});
967 } else {
968 print " *undescribed*\n";
970 print "\n";
973 $lineprefix = $oldprefix;
974 output_section_rst(@_);
977 sub output_section_rst(%) {
978 my %args = %{$_[0]};
979 my $section;
980 my $oldprefix = $lineprefix;
981 $lineprefix = "";
983 foreach $section (@{$args{'sectionlist'}}) {
984 print "**$section**\n\n";
985 print_lineno($section_start_lines{$section});
986 output_highlight_rst($args{'sections'}{$section});
987 print "\n";
989 print "\n";
990 $lineprefix = $oldprefix;
993 sub output_enum_rst(%) {
994 my %args = %{$_[0]};
995 my ($parameter);
996 my $oldprefix = $lineprefix;
997 my $count;
999 if ($sphinx_major < 3) {
1000 my $name = "enum " . $args{'enum'};
1001 print "\n\n.. c:type:: " . $name . "\n\n";
1002 } else {
1003 my $name = $args{'enum'};
1004 print "\n\n.. c:enum:: " . $name . "\n\n";
1006 print_lineno($declaration_start_line);
1007 $lineprefix = " ";
1008 output_highlight_rst($args{'purpose'});
1009 print "\n";
1011 print "**Constants**\n\n";
1012 $lineprefix = " ";
1013 foreach $parameter (@{$args{'parameterlist'}}) {
1014 print "``$parameter``\n";
1015 if ($args{'parameterdescs'}{$parameter} ne $undescribed) {
1016 output_highlight_rst($args{'parameterdescs'}{$parameter});
1017 } else {
1018 print " *undescribed*\n";
1020 print "\n";
1023 $lineprefix = $oldprefix;
1024 output_section_rst(@_);
1027 sub output_typedef_rst(%) {
1028 my %args = %{$_[0]};
1029 my ($parameter);
1030 my $oldprefix = $lineprefix;
1031 my $name;
1033 if ($sphinx_major < 3) {
1034 $name = "typedef " . $args{'typedef'};
1035 } else {
1036 $name = $args{'typedef'};
1038 print "\n\n.. c:type:: " . $name . "\n\n";
1039 print_lineno($declaration_start_line);
1040 $lineprefix = " ";
1041 output_highlight_rst($args{'purpose'});
1042 print "\n";
1044 $lineprefix = $oldprefix;
1045 output_section_rst(@_);
1048 sub output_struct_rst(%) {
1049 my %args = %{$_[0]};
1050 my ($parameter);
1051 my $oldprefix = $lineprefix;
1053 if ($sphinx_major < 3) {
1054 my $name = $args{'type'} . " " . $args{'struct'};
1055 print "\n\n.. c:type:: " . $name . "\n\n";
1056 } else {
1057 my $name = $args{'struct'};
1058 print "\n\n.. c:struct:: " . $name . "\n\n";
1060 print_lineno($declaration_start_line);
1061 $lineprefix = " ";
1062 output_highlight_rst($args{'purpose'});
1063 print "\n";
1065 print "**Definition**\n\n";
1066 print "::\n\n";
1067 my $declaration = $args{'definition'};
1068 $declaration =~ s/\t/ /g;
1069 print " " . $args{'type'} . " " . $args{'struct'} . " {\n$declaration };\n\n";
1071 print "**Members**\n\n";
1072 $lineprefix = " ";
1073 foreach $parameter (@{$args{'parameterlist'}}) {
1074 ($parameter =~ /^#/) && next;
1076 my $parameter_name = $parameter;
1077 $parameter_name =~ s/\[.*//;
1079 ($args{'parameterdescs'}{$parameter_name} ne $undescribed) || next;
1080 $type = $args{'parametertypes'}{$parameter};
1081 print_lineno($parameterdesc_start_lines{$parameter_name});
1082 print "``" . $parameter . "``\n";
1083 output_highlight_rst($args{'parameterdescs'}{$parameter_name});
1084 print "\n";
1086 print "\n";
1088 $lineprefix = $oldprefix;
1089 output_section_rst(@_);
1092 ## none mode output functions
1094 sub output_function_none(%) {
1097 sub output_enum_none(%) {
1100 sub output_typedef_none(%) {
1103 sub output_struct_none(%) {
1106 sub output_blockhead_none(%) {
1110 # generic output function for all types (function, struct/union, typedef, enum);
1111 # calls the generated, variable output_ function name based on
1112 # functype and output_mode
1113 sub output_declaration {
1114 no strict 'refs';
1115 my $name = shift;
1116 my $functype = shift;
1117 my $func = "output_${functype}_$output_mode";
1119 return if (defined($nosymbol_table{$name}));
1121 if (($output_selection == OUTPUT_ALL) ||
1122 (($output_selection == OUTPUT_INCLUDE ||
1123 $output_selection == OUTPUT_EXPORTED) &&
1124 defined($function_table{$name})) ||
1125 ($output_selection == OUTPUT_INTERNAL &&
1126 !($functype eq "function" && defined($function_table{$name}))))
1128 &$func(@_);
1129 $section_counter++;
1134 # generic output function - calls the right one based on current output mode.
1135 sub output_blockhead {
1136 no strict 'refs';
1137 my $func = "output_blockhead_" . $output_mode;
1138 &$func(@_);
1139 $section_counter++;
1143 # takes a declaration (struct, union, enum, typedef) and
1144 # invokes the right handler. NOT called for functions.
1145 sub dump_declaration($$) {
1146 no strict 'refs';
1147 my ($prototype, $file) = @_;
1148 my $func = "dump_" . $decl_type;
1149 &$func(@_);
1152 sub dump_union($$) {
1153 dump_struct(@_);
1156 sub dump_struct($$) {
1157 my $x = shift;
1158 my $file = shift;
1160 if ($x =~ /(struct|union)\s+(\w+)\s*\{(.*)\}(\s*(__packed|__aligned|____cacheline_aligned_in_smp|____cacheline_aligned|__attribute__\s*\(\([a-z0-9,_\s\(\)]*\)\)))*/) {
1161 my $decl_type = $1;
1162 $declaration_name = $2;
1163 my $members = $3;
1165 # ignore members marked private:
1166 $members =~ s/\/\*\s*private:.*?\/\*\s*public:.*?\*\///gosi;
1167 $members =~ s/\/\*\s*private:.*//gosi;
1168 # strip comments:
1169 $members =~ s/\/\*.*?\*\///gos;
1170 # strip attributes
1171 $members =~ s/\s*__attribute__\s*\(\([a-z0-9,_\*\s\(\)]*\)\)/ /gi;
1172 $members =~ s/\s*__aligned\s*\([^;]*\)/ /gos;
1173 $members =~ s/\s*__packed\s*/ /gos;
1174 $members =~ s/\s*CRYPTO_MINALIGN_ATTR/ /gos;
1175 $members =~ s/\s*____cacheline_aligned_in_smp/ /gos;
1176 $members =~ s/\s*____cacheline_aligned/ /gos;
1178 # replace DECLARE_BITMAP
1179 $members =~ s/__ETHTOOL_DECLARE_LINK_MODE_MASK\s*\(([^\)]+)\)/DECLARE_BITMAP($1, __ETHTOOL_LINK_MODE_MASK_NBITS)/gos;
1180 $members =~ s/DECLARE_BITMAP\s*\(([^,)]+),\s*([^,)]+)\)/unsigned long $1\[BITS_TO_LONGS($2)\]/gos;
1181 # replace DECLARE_HASHTABLE
1182 $members =~ s/DECLARE_HASHTABLE\s*\(([^,)]+),\s*([^,)]+)\)/unsigned long $1\[1 << (($2) - 1)\]/gos;
1183 # replace DECLARE_KFIFO
1184 $members =~ s/DECLARE_KFIFO\s*\(([^,)]+),\s*([^,)]+),\s*([^,)]+)\)/$2 \*$1/gos;
1185 # replace DECLARE_KFIFO_PTR
1186 $members =~ s/DECLARE_KFIFO_PTR\s*\(([^,)]+),\s*([^,)]+)\)/$2 \*$1/gos;
1188 my $declaration = $members;
1190 # Split nested struct/union elements as newer ones
1191 while ($members =~ m/(struct|union)([^\{\};]+)\{([^\{\}]*)\}([^\{\}\;]*)\;/) {
1192 my $newmember;
1193 my $maintype = $1;
1194 my $ids = $4;
1195 my $content = $3;
1196 foreach my $id(split /,/, $ids) {
1197 $newmember .= "$maintype $id; ";
1199 $id =~ s/[:\[].*//;
1200 $id =~ s/^\s*\**(\S+)\s*/$1/;
1201 foreach my $arg (split /;/, $content) {
1202 next if ($arg =~ m/^\s*$/);
1203 if ($arg =~ m/^([^\(]+\(\*?\s*)([\w\.]*)(\s*\).*)/) {
1204 # pointer-to-function
1205 my $type = $1;
1206 my $name = $2;
1207 my $extra = $3;
1208 next if (!$name);
1209 if ($id =~ m/^\s*$/) {
1210 # anonymous struct/union
1211 $newmember .= "$type$name$extra; ";
1212 } else {
1213 $newmember .= "$type$id.$name$extra; ";
1215 } else {
1216 my $type;
1217 my $names;
1218 $arg =~ s/^\s+//;
1219 $arg =~ s/\s+$//;
1220 # Handle bitmaps
1221 $arg =~ s/:\s*\d+\s*//g;
1222 # Handle arrays
1223 $arg =~ s/\[.*\]//g;
1224 # The type may have multiple words,
1225 # and multiple IDs can be defined, like:
1226 # const struct foo, *bar, foobar
1227 # So, we remove spaces when parsing the
1228 # names, in order to match just names
1229 # and commas for the names
1230 $arg =~ s/\s*,\s*/,/g;
1231 if ($arg =~ m/(.*)\s+([\S+,]+)/) {
1232 $type = $1;
1233 $names = $2;
1234 } else {
1235 $newmember .= "$arg; ";
1236 next;
1238 foreach my $name (split /,/, $names) {
1239 $name =~ s/^\s*\**(\S+)\s*/$1/;
1240 next if (($name =~ m/^\s*$/));
1241 if ($id =~ m/^\s*$/) {
1242 # anonymous struct/union
1243 $newmember .= "$type $name; ";
1244 } else {
1245 $newmember .= "$type $id.$name; ";
1251 $members =~ s/(struct|union)([^\{\};]+)\{([^\{\}]*)\}([^\{\}\;]*)\;/$newmember/;
1254 # Ignore other nested elements, like enums
1255 $members =~ s/(\{[^\{\}]*\})//g;
1257 create_parameterlist($members, ';', $file, $declaration_name);
1258 check_sections($file, $declaration_name, $decl_type, $sectcheck, $struct_actual);
1260 # Adjust declaration for better display
1261 $declaration =~ s/([\{;])/$1\n/g;
1262 $declaration =~ s/\}\s+;/};/g;
1263 # Better handle inlined enums
1264 do {} while ($declaration =~ s/(enum\s+\{[^\}]+),([^\n])/$1,\n$2/);
1266 my @def_args = split /\n/, $declaration;
1267 my $level = 1;
1268 $declaration = "";
1269 foreach my $clause (@def_args) {
1270 $clause =~ s/^\s+//;
1271 $clause =~ s/\s+$//;
1272 $clause =~ s/\s+/ /;
1273 next if (!$clause);
1274 $level-- if ($clause =~ m/(\})/ && $level > 1);
1275 if (!($clause =~ m/^\s*#/)) {
1276 $declaration .= "\t" x $level;
1278 $declaration .= "\t" . $clause . "\n";
1279 $level++ if ($clause =~ m/(\{)/ && !($clause =~m/\}/));
1281 output_declaration($declaration_name,
1282 'struct',
1283 {'struct' => $declaration_name,
1284 'module' => $modulename,
1285 'definition' => $declaration,
1286 'parameterlist' => \@parameterlist,
1287 'parameterdescs' => \%parameterdescs,
1288 'parametertypes' => \%parametertypes,
1289 'sectionlist' => \@sectionlist,
1290 'sections' => \%sections,
1291 'purpose' => $declaration_purpose,
1292 'type' => $decl_type
1295 else {
1296 print STDERR "${file}:$.: error: Cannot parse struct or union!\n";
1297 ++$errors;
1302 sub show_warnings($$) {
1303 my $functype = shift;
1304 my $name = shift;
1306 return 0 if (defined($nosymbol_table{$name}));
1308 return 1 if ($output_selection == OUTPUT_ALL);
1310 if ($output_selection == OUTPUT_EXPORTED) {
1311 if (defined($function_table{$name})) {
1312 return 1;
1313 } else {
1314 return 0;
1317 if ($output_selection == OUTPUT_INTERNAL) {
1318 if (!($functype eq "function" && defined($function_table{$name}))) {
1319 return 1;
1320 } else {
1321 return 0;
1324 if ($output_selection == OUTPUT_INCLUDE) {
1325 if (defined($function_table{$name})) {
1326 return 1;
1327 } else {
1328 return 0;
1331 die("Please add the new output type at show_warnings()");
1334 sub dump_enum($$) {
1335 my $x = shift;
1336 my $file = shift;
1337 my $members;
1340 $x =~ s@/\*.*?\*/@@gos; # strip comments.
1341 # strip #define macros inside enums
1342 $x =~ s@#\s*((define|ifdef)\s+|endif)[^;]*;@@gos;
1344 if ($x =~ /typedef\s+enum\s*\{(.*)\}\s*(\w*)\s*;/) {
1345 $declaration_name = $2;
1346 $members = $1;
1347 } elsif ($x =~ /enum\s+(\w*)\s*\{(.*)\}/) {
1348 $declaration_name = $1;
1349 $members = $2;
1352 if ($declaration_name) {
1353 my %_members;
1355 $members =~ s/\s+$//;
1357 foreach my $arg (split ',', $members) {
1358 $arg =~ s/^\s*(\w+).*/$1/;
1359 push @parameterlist, $arg;
1360 if (!$parameterdescs{$arg}) {
1361 $parameterdescs{$arg} = $undescribed;
1362 if (show_warnings("enum", $declaration_name)) {
1363 print STDERR "${file}:$.: warning: Enum value '$arg' not described in enum '$declaration_name'\n";
1366 $_members{$arg} = 1;
1369 while (my ($k, $v) = each %parameterdescs) {
1370 if (!exists($_members{$k})) {
1371 if (show_warnings("enum", $declaration_name)) {
1372 print STDERR "${file}:$.: warning: Excess enum value '$k' description in '$declaration_name'\n";
1377 output_declaration($declaration_name,
1378 'enum',
1379 {'enum' => $declaration_name,
1380 'module' => $modulename,
1381 'parameterlist' => \@parameterlist,
1382 'parameterdescs' => \%parameterdescs,
1383 'sectionlist' => \@sectionlist,
1384 'sections' => \%sections,
1385 'purpose' => $declaration_purpose
1387 } else {
1388 print STDERR "${file}:$.: error: Cannot parse enum!\n";
1389 ++$errors;
1393 sub dump_typedef($$) {
1394 my $x = shift;
1395 my $file = shift;
1397 $x =~ s@/\*.*?\*/@@gos; # strip comments.
1399 # Parse function prototypes
1400 if ($x =~ /typedef\s+(\w+\s*\**)\s*\(\*?\s*(\w\S+)\s*\)\s*\((.*)\);/ ||
1401 $x =~ /typedef\s+(\w+\s*\**)\s*(\w\S+)\s*\s*\((.*)\);/) {
1403 # Function typedefs
1404 $return_type = $1;
1405 $declaration_name = $2;
1406 my $args = $3;
1408 create_parameterlist($args, ',', $file, $declaration_name);
1410 output_declaration($declaration_name,
1411 'function',
1412 {'function' => $declaration_name,
1413 'typedef' => 1,
1414 'module' => $modulename,
1415 'functiontype' => $return_type,
1416 'parameterlist' => \@parameterlist,
1417 'parameterdescs' => \%parameterdescs,
1418 'parametertypes' => \%parametertypes,
1419 'sectionlist' => \@sectionlist,
1420 'sections' => \%sections,
1421 'purpose' => $declaration_purpose
1423 return;
1426 while (($x =~ /\(*.\)\s*;$/) || ($x =~ /\[*.\]\s*;$/)) {
1427 $x =~ s/\(*.\)\s*;$/;/;
1428 $x =~ s/\[*.\]\s*;$/;/;
1431 if ($x =~ /typedef.*\s+(\w+)\s*;/) {
1432 $declaration_name = $1;
1434 output_declaration($declaration_name,
1435 'typedef',
1436 {'typedef' => $declaration_name,
1437 'module' => $modulename,
1438 'sectionlist' => \@sectionlist,
1439 'sections' => \%sections,
1440 'purpose' => $declaration_purpose
1443 else {
1444 print STDERR "${file}:$.: error: Cannot parse typedef!\n";
1445 ++$errors;
1449 sub save_struct_actual($) {
1450 my $actual = shift;
1452 # strip all spaces from the actual param so that it looks like one string item
1453 $actual =~ s/\s*//g;
1454 $struct_actual = $struct_actual . $actual . " ";
1457 sub create_parameterlist($$$$) {
1458 my $args = shift;
1459 my $splitter = shift;
1460 my $file = shift;
1461 my $declaration_name = shift;
1462 my $type;
1463 my $param;
1465 # temporarily replace commas inside function pointer definition
1466 while ($args =~ /(\([^\),]+),/) {
1467 $args =~ s/(\([^\),]+),/$1#/g;
1470 foreach my $arg (split($splitter, $args)) {
1471 # strip comments
1472 $arg =~ s/\/\*.*\*\///;
1473 # strip leading/trailing spaces
1474 $arg =~ s/^\s*//;
1475 $arg =~ s/\s*$//;
1476 $arg =~ s/\s+/ /;
1478 if ($arg =~ /^#/) {
1479 # Treat preprocessor directive as a typeless variable just to fill
1480 # corresponding data structures "correctly". Catch it later in
1481 # output_* subs.
1482 push_parameter($arg, "", "", $file);
1483 } elsif ($arg =~ m/\(.+\)\s*\(/) {
1484 # pointer-to-function
1485 $arg =~ tr/#/,/;
1486 $arg =~ m/[^\(]+\(\*?\s*([\w\.]*)\s*\)/;
1487 $param = $1;
1488 $type = $arg;
1489 $type =~ s/([^\(]+\(\*?)\s*$param/$1/;
1490 save_struct_actual($param);
1491 push_parameter($param, $type, $arg, $file, $declaration_name);
1492 } elsif ($arg) {
1493 $arg =~ s/\s*:\s*/:/g;
1494 $arg =~ s/\s*\[/\[/g;
1496 my @args = split('\s*,\s*', $arg);
1497 if ($args[0] =~ m/\*/) {
1498 $args[0] =~ s/(\*+)\s*/ $1/;
1501 my @first_arg;
1502 if ($args[0] =~ /^(.*\s+)(.*?\[.*\].*)$/) {
1503 shift @args;
1504 push(@first_arg, split('\s+', $1));
1505 push(@first_arg, $2);
1506 } else {
1507 @first_arg = split('\s+', shift @args);
1510 unshift(@args, pop @first_arg);
1511 $type = join " ", @first_arg;
1513 foreach $param (@args) {
1514 if ($param =~ m/^(\*+)\s*(.*)/) {
1515 save_struct_actual($2);
1517 push_parameter($2, "$type $1", $arg, $file, $declaration_name);
1519 elsif ($param =~ m/(.*?):(\d+)/) {
1520 if ($type ne "") { # skip unnamed bit-fields
1521 save_struct_actual($1);
1522 push_parameter($1, "$type:$2", $arg, $file, $declaration_name)
1525 else {
1526 save_struct_actual($param);
1527 push_parameter($param, $type, $arg, $file, $declaration_name);
1534 sub push_parameter($$$$$) {
1535 my $param = shift;
1536 my $type = shift;
1537 my $org_arg = shift;
1538 my $file = shift;
1539 my $declaration_name = shift;
1541 if (($anon_struct_union == 1) && ($type eq "") &&
1542 ($param eq "}")) {
1543 return; # ignore the ending }; from anon. struct/union
1546 $anon_struct_union = 0;
1547 $param =~ s/[\[\)].*//;
1549 if ($type eq "" && $param =~ /\.\.\.$/)
1551 if (!$param =~ /\w\.\.\.$/) {
1552 # handles unnamed variable parameters
1553 $param = "...";
1555 elsif ($param =~ /\w\.\.\.$/) {
1556 # for named variable parameters of the form `x...`, remove the dots
1557 $param =~ s/\.\.\.$//;
1559 if (!defined $parameterdescs{$param} || $parameterdescs{$param} eq "") {
1560 $parameterdescs{$param} = "variable arguments";
1563 elsif ($type eq "" && ($param eq "" or $param eq "void"))
1565 $param="void";
1566 $parameterdescs{void} = "no arguments";
1568 elsif ($type eq "" && ($param eq "struct" or $param eq "union"))
1569 # handle unnamed (anonymous) union or struct:
1571 $type = $param;
1572 $param = "{unnamed_" . $param . "}";
1573 $parameterdescs{$param} = "anonymous\n";
1574 $anon_struct_union = 1;
1577 # warn if parameter has no description
1578 # (but ignore ones starting with # as these are not parameters
1579 # but inline preprocessor statements);
1580 # Note: It will also ignore void params and unnamed structs/unions
1581 if (!defined $parameterdescs{$param} && $param !~ /^#/) {
1582 $parameterdescs{$param} = $undescribed;
1584 if (show_warnings($type, $declaration_name) && $param !~ /\./) {
1585 print STDERR
1586 "${file}:$.: warning: Function parameter or member '$param' not described in '$declaration_name'\n";
1587 ++$warnings;
1591 # strip spaces from $param so that it is one continuous string
1592 # on @parameterlist;
1593 # this fixes a problem where check_sections() cannot find
1594 # a parameter like "addr[6 + 2]" because it actually appears
1595 # as "addr[6", "+", "2]" on the parameter list;
1596 # but it's better to maintain the param string unchanged for output,
1597 # so just weaken the string compare in check_sections() to ignore
1598 # "[blah" in a parameter string;
1599 ###$param =~ s/\s*//g;
1600 push @parameterlist, $param;
1601 $org_arg =~ s/\s\s+/ /g;
1602 $parametertypes{$param} = $org_arg;
1605 sub check_sections($$$$$) {
1606 my ($file, $decl_name, $decl_type, $sectcheck, $prmscheck) = @_;
1607 my @sects = split ' ', $sectcheck;
1608 my @prms = split ' ', $prmscheck;
1609 my $err;
1610 my ($px, $sx);
1611 my $prm_clean; # strip trailing "[array size]" and/or beginning "*"
1613 foreach $sx (0 .. $#sects) {
1614 $err = 1;
1615 foreach $px (0 .. $#prms) {
1616 $prm_clean = $prms[$px];
1617 $prm_clean =~ s/\[.*\]//;
1618 $prm_clean =~ s/__attribute__\s*\(\([a-z,_\*\s\(\)]*\)\)//i;
1619 # ignore array size in a parameter string;
1620 # however, the original param string may contain
1621 # spaces, e.g.: addr[6 + 2]
1622 # and this appears in @prms as "addr[6" since the
1623 # parameter list is split at spaces;
1624 # hence just ignore "[..." for the sections check;
1625 $prm_clean =~ s/\[.*//;
1627 ##$prm_clean =~ s/^\**//;
1628 if ($prm_clean eq $sects[$sx]) {
1629 $err = 0;
1630 last;
1633 if ($err) {
1634 if ($decl_type eq "function") {
1635 print STDERR "${file}:$.: warning: " .
1636 "Excess function parameter " .
1637 "'$sects[$sx]' " .
1638 "description in '$decl_name'\n";
1639 ++$warnings;
1646 # Checks the section describing the return value of a function.
1647 sub check_return_section {
1648 my $file = shift;
1649 my $declaration_name = shift;
1650 my $return_type = shift;
1652 # Ignore an empty return type (It's a macro)
1653 # Ignore functions with a "void" return type. (But don't ignore "void *")
1654 if (($return_type eq "") || ($return_type =~ /void\s*\w*\s*$/)) {
1655 return;
1658 if (!defined($sections{$section_return}) ||
1659 $sections{$section_return} eq "") {
1660 print STDERR "${file}:$.: warning: " .
1661 "No description found for return value of " .
1662 "'$declaration_name'\n";
1663 ++$warnings;
1668 # takes a function prototype and the name of the current file being
1669 # processed and spits out all the details stored in the global
1670 # arrays/hashes.
1671 sub dump_function($$) {
1672 my $prototype = shift;
1673 my $file = shift;
1674 my $noret = 0;
1676 print_lineno($.);
1678 $prototype =~ s/^static +//;
1679 $prototype =~ s/^extern +//;
1680 $prototype =~ s/^asmlinkage +//;
1681 $prototype =~ s/^inline +//;
1682 $prototype =~ s/^__inline__ +//;
1683 $prototype =~ s/^__inline +//;
1684 $prototype =~ s/^__always_inline +//;
1685 $prototype =~ s/^noinline +//;
1686 $prototype =~ s/__init +//;
1687 $prototype =~ s/__init_or_module +//;
1688 $prototype =~ s/__meminit +//;
1689 $prototype =~ s/__must_check +//;
1690 $prototype =~ s/__weak +//;
1691 $prototype =~ s/__sched +//;
1692 $prototype =~ s/__printf\s*\(\s*\d*\s*,\s*\d*\s*\) +//;
1693 my $define = $prototype =~ s/^#\s*define\s+//; #ak added
1694 $prototype =~ s/__attribute__\s*\(\(
1696 [\w\s]++ # attribute name
1697 (?:\([^)]*+\))? # attribute arguments
1698 \s*+,? # optional comma at the end
1700 \)\)\s+//x;
1702 # Yes, this truly is vile. We are looking for:
1703 # 1. Return type (may be nothing if we're looking at a macro)
1704 # 2. Function name
1705 # 3. Function parameters.
1707 # All the while we have to watch out for function pointer parameters
1708 # (which IIRC is what the two sections are for), C types (these
1709 # regexps don't even start to express all the possibilities), and
1710 # so on.
1712 # If you mess with these regexps, it's a good idea to check that
1713 # the following functions' documentation still comes out right:
1714 # - parport_register_device (function pointer parameters)
1715 # - qatomic_set (macro)
1716 # - pci_match_device, __copy_to_user (long return type)
1718 if ($define && $prototype =~ m/^()([a-zA-Z0-9_~:]+)\s+/) {
1719 # This is an object-like macro, it has no return type and no parameter
1720 # list.
1721 # Function-like macros are not allowed to have spaces between
1722 # declaration_name and opening parenthesis (notice the \s+).
1723 $return_type = $1;
1724 $declaration_name = $2;
1725 $noret = 1;
1726 } elsif ($prototype =~ m/^()([a-zA-Z0-9_~:]+)\s*\(([^\(]*)\)/ ||
1727 $prototype =~ m/^(\w+)\s+([a-zA-Z0-9_~:]+)\s*\(([^\(]*)\)/ ||
1728 $prototype =~ m/^(\w+\s*\*+)\s*([a-zA-Z0-9_~:]+)\s*\(([^\(]*)\)/ ||
1729 $prototype =~ m/^(\w+\s+\w+)\s+([a-zA-Z0-9_~:]+)\s*\(([^\(]*)\)/ ||
1730 $prototype =~ m/^(\w+\s+\w+\s*\*+)\s*([a-zA-Z0-9_~:]+)\s*\(([^\(]*)\)/ ||
1731 $prototype =~ m/^(\w+\s+\w+\s+\w+)\s+([a-zA-Z0-9_~:]+)\s*\(([^\(]*)\)/ ||
1732 $prototype =~ m/^(\w+\s+\w+\s+\w+\s*\*+)\s*([a-zA-Z0-9_~:]+)\s*\(([^\(]*)\)/ ||
1733 $prototype =~ m/^()([a-zA-Z0-9_~:]+)\s*\(([^\{]*)\)/ ||
1734 $prototype =~ m/^(\w+)\s+([a-zA-Z0-9_~:]+)\s*\(([^\{]*)\)/ ||
1735 $prototype =~ m/^(\w+\s*\*+)\s*([a-zA-Z0-9_~:]+)\s*\(([^\{]*)\)/ ||
1736 $prototype =~ m/^(\w+\s+\w+)\s+([a-zA-Z0-9_~:]+)\s*\(([^\{]*)\)/ ||
1737 $prototype =~ m/^(\w+\s+\w+\s*\*+)\s*([a-zA-Z0-9_~:]+)\s*\(([^\{]*)\)/ ||
1738 $prototype =~ m/^(\w+\s+\w+\s+\w+)\s+([a-zA-Z0-9_~:]+)\s*\(([^\{]*)\)/ ||
1739 $prototype =~ m/^(\w+\s+\w+\s+\w+\s*\*+)\s*([a-zA-Z0-9_~:]+)\s*\(([^\{]*)\)/ ||
1740 $prototype =~ m/^(\w+\s+\w+\s+\w+\s+\w+)\s+([a-zA-Z0-9_~:]+)\s*\(([^\{]*)\)/ ||
1741 $prototype =~ m/^(\w+\s+\w+\s+\w+\s+\w+\s*\*+)\s*([a-zA-Z0-9_~:]+)\s*\(([^\{]*)\)/ ||
1742 $prototype =~ m/^(\w+\s+\w+\s*\*+\s*\w+\s*\*+\s*)\s*([a-zA-Z0-9_~:]+)\s*\(([^\{]*)\)/) {
1743 $return_type = $1;
1744 $declaration_name = $2;
1745 my $args = $3;
1747 create_parameterlist($args, ',', $file, $declaration_name);
1748 } else {
1749 print STDERR "${file}:$.: warning: cannot understand function prototype: '$prototype'\n";
1750 return;
1753 my $prms = join " ", @parameterlist;
1754 check_sections($file, $declaration_name, "function", $sectcheck, $prms);
1756 # This check emits a lot of warnings at the moment, because many
1757 # functions don't have a 'Return' doc section. So until the number
1758 # of warnings goes sufficiently down, the check is only performed in
1759 # verbose mode.
1760 # TODO: always perform the check.
1761 if ($verbose && !$noret) {
1762 check_return_section($file, $declaration_name, $return_type);
1765 # The function parser can be called with a typedef parameter.
1766 # Handle it.
1767 if ($return_type =~ /typedef/) {
1768 output_declaration($declaration_name,
1769 'function',
1770 {'function' => $declaration_name,
1771 'typedef' => 1,
1772 'module' => $modulename,
1773 'functiontype' => $return_type,
1774 'parameterlist' => \@parameterlist,
1775 'parameterdescs' => \%parameterdescs,
1776 'parametertypes' => \%parametertypes,
1777 'sectionlist' => \@sectionlist,
1778 'sections' => \%sections,
1779 'purpose' => $declaration_purpose
1781 } else {
1782 output_declaration($declaration_name,
1783 'function',
1784 {'function' => $declaration_name,
1785 'module' => $modulename,
1786 'functiontype' => $return_type,
1787 'parameterlist' => \@parameterlist,
1788 'parameterdescs' => \%parameterdescs,
1789 'parametertypes' => \%parametertypes,
1790 'sectionlist' => \@sectionlist,
1791 'sections' => \%sections,
1792 'purpose' => $declaration_purpose
1797 sub reset_state {
1798 $function = "";
1799 %parameterdescs = ();
1800 %parametertypes = ();
1801 @parameterlist = ();
1802 %sections = ();
1803 @sectionlist = ();
1804 $sectcheck = "";
1805 $struct_actual = "";
1806 $prototype = "";
1808 $state = STATE_NORMAL;
1809 $inline_doc_state = STATE_INLINE_NA;
1812 sub tracepoint_munge($) {
1813 my $file = shift;
1814 my $tracepointname = 0;
1815 my $tracepointargs = 0;
1817 if ($prototype =~ m/TRACE_EVENT\((.*?),/) {
1818 $tracepointname = $1;
1820 if ($prototype =~ m/DEFINE_SINGLE_EVENT\((.*?),/) {
1821 $tracepointname = $1;
1823 if ($prototype =~ m/DEFINE_EVENT\((.*?),(.*?),/) {
1824 $tracepointname = $2;
1826 $tracepointname =~ s/^\s+//; #strip leading whitespace
1827 if ($prototype =~ m/TP_PROTO\((.*?)\)/) {
1828 $tracepointargs = $1;
1830 if (($tracepointname eq 0) || ($tracepointargs eq 0)) {
1831 print STDERR "${file}:$.: warning: Unrecognized tracepoint format: \n".
1832 "$prototype\n";
1833 } else {
1834 $prototype = "static inline void trace_$tracepointname($tracepointargs)";
1838 sub syscall_munge() {
1839 my $void = 0;
1841 $prototype =~ s@[\r\n]+@ @gos; # strip newlines/CR's
1842 ## if ($prototype =~ m/SYSCALL_DEFINE0\s*\(\s*(a-zA-Z0-9_)*\s*\)/) {
1843 if ($prototype =~ m/SYSCALL_DEFINE0/) {
1844 $void = 1;
1845 ## $prototype = "long sys_$1(void)";
1848 $prototype =~ s/SYSCALL_DEFINE.*\(/long sys_/; # fix return type & func name
1849 if ($prototype =~ m/long (sys_.*?),/) {
1850 $prototype =~ s/,/\(/;
1851 } elsif ($void) {
1852 $prototype =~ s/\)/\(void\)/;
1855 # now delete all of the odd-number commas in $prototype
1856 # so that arg types & arg names don't have a comma between them
1857 my $count = 0;
1858 my $len = length($prototype);
1859 if ($void) {
1860 $len = 0; # skip the for-loop
1862 for (my $ix = 0; $ix < $len; $ix++) {
1863 if (substr($prototype, $ix, 1) eq ',') {
1864 $count++;
1865 if ($count % 2 == 1) {
1866 substr($prototype, $ix, 1) = ' ';
1872 sub process_proto_function($$) {
1873 my $x = shift;
1874 my $file = shift;
1876 $x =~ s@\/\/.*$@@gos; # strip C99-style comments to end of line
1878 if ($x =~ m#\s*/\*\s+MACDOC\s*#io || ($x =~ /^#/ && $x !~ /^#\s*define/)) {
1879 # do nothing
1881 elsif ($x =~ /([^\{]*)/) {
1882 $prototype .= $1;
1885 if (($x =~ /\{/) || ($x =~ /\#\s*define/) || ($x =~ /;/)) {
1886 $prototype =~ s@/\*.*?\*/@@gos; # strip comments.
1887 $prototype =~ s@[\r\n]+@ @gos; # strip newlines/cr's.
1888 $prototype =~ s@^\s+@@gos; # strip leading spaces
1890 # Handle prototypes for function pointers like:
1891 # int (*pcs_config)(struct foo)
1892 $prototype =~ s@^(\S+\s+)\(\s*\*(\S+)\)@$1$2@gos;
1894 if ($prototype =~ /SYSCALL_DEFINE/) {
1895 syscall_munge();
1897 if ($prototype =~ /TRACE_EVENT/ || $prototype =~ /DEFINE_EVENT/ ||
1898 $prototype =~ /DEFINE_SINGLE_EVENT/)
1900 tracepoint_munge($file);
1902 dump_function($prototype, $file);
1903 reset_state();
1907 sub process_proto_type($$) {
1908 my $x = shift;
1909 my $file = shift;
1911 $x =~ s@[\r\n]+@ @gos; # strip newlines/cr's.
1912 $x =~ s@^\s+@@gos; # strip leading spaces
1913 $x =~ s@\s+$@@gos; # strip trailing spaces
1914 $x =~ s@\/\/.*$@@gos; # strip C99-style comments to end of line
1916 if ($x =~ /^#/) {
1917 # To distinguish preprocessor directive from regular declaration later.
1918 $x .= ";";
1921 while (1) {
1922 if ( $x =~ /([^\{\};]*)([\{\};])(.*)/ ) {
1923 if( length $prototype ) {
1924 $prototype .= " "
1926 $prototype .= $1 . $2;
1927 ($2 eq '{') && $brcount++;
1928 ($2 eq '}') && $brcount--;
1929 if (($2 eq ';') && ($brcount == 0)) {
1930 dump_declaration($prototype, $file);
1931 reset_state();
1932 last;
1934 $x = $3;
1935 } else {
1936 $prototype .= $x;
1937 last;
1943 sub map_filename($) {
1944 my $file;
1945 my ($orig_file) = @_;
1947 if (defined($ENV{'SRCTREE'})) {
1948 $file = "$ENV{'SRCTREE'}" . "/" . $orig_file;
1949 } else {
1950 $file = $orig_file;
1953 if (defined($source_map{$file})) {
1954 $file = $source_map{$file};
1957 return $file;
1960 sub process_export_file($) {
1961 my ($orig_file) = @_;
1962 my $file = map_filename($orig_file);
1964 if (!open(IN,"<$file")) {
1965 print STDERR "Error: Cannot open file $file\n";
1966 ++$errors;
1967 return;
1970 while (<IN>) {
1971 if (/$export_symbol/) {
1972 next if (defined($nosymbol_table{$2}));
1973 $function_table{$2} = 1;
1977 close(IN);
1981 # Parsers for the various processing states.
1983 # STATE_NORMAL: looking for the /** to begin everything.
1985 sub process_normal() {
1986 if (/$doc_start/o) {
1987 $state = STATE_NAME; # next line is always the function name
1988 $in_doc_sect = 0;
1989 $declaration_start_line = $. + 1;
1994 # STATE_NAME: Looking for the "name - description" line
1996 sub process_name($$) {
1997 my $file = shift;
1998 my $identifier;
1999 my $descr;
2001 if (/$doc_block/o) {
2002 $state = STATE_DOCBLOCK;
2003 $contents = "";
2004 $new_start_line = $. + 1;
2006 if ( $1 eq "" ) {
2007 $section = $section_intro;
2008 } else {
2009 $section = $1;
2012 elsif (/$doc_decl/o) {
2013 $identifier = $1;
2014 if (/\s*([\w\s]+?)(\s*-|:)/) {
2015 $identifier = $1;
2018 $state = STATE_BODY;
2019 # if there's no @param blocks need to set up default section
2020 # here
2021 $contents = "";
2022 $section = $section_default;
2023 $new_start_line = $. + 1;
2024 if (/[-:](.*)/) {
2025 # strip leading/trailing/multiple spaces
2026 $descr= $1;
2027 $descr =~ s/^\s*//;
2028 $descr =~ s/\s*$//;
2029 $descr =~ s/\s+/ /g;
2030 $declaration_purpose = $descr;
2031 $state = STATE_BODY_MAYBE;
2032 } else {
2033 $declaration_purpose = "";
2036 if (($declaration_purpose eq "") && $verbose) {
2037 print STDERR "${file}:$.: warning: missing initial short description on line:\n";
2038 print STDERR $_;
2039 ++$warnings;
2042 if ($identifier =~ m/^struct\b/) {
2043 $decl_type = 'struct';
2044 } elsif ($identifier =~ m/^union\b/) {
2045 $decl_type = 'union';
2046 } elsif ($identifier =~ m/^enum\b/) {
2047 $decl_type = 'enum';
2048 } elsif ($identifier =~ m/^typedef\b/) {
2049 $decl_type = 'typedef';
2050 } else {
2051 $decl_type = 'function';
2054 if ($verbose) {
2055 print STDERR "${file}:$.: info: Scanning doc for $identifier\n";
2057 } else {
2058 print STDERR "${file}:$.: warning: Cannot understand $_ on line $.",
2059 " - I thought it was a doc line\n";
2060 ++$warnings;
2061 $state = STATE_NORMAL;
2067 # STATE_BODY and STATE_BODY_MAYBE: the bulk of a kerneldoc comment.
2069 sub process_body($$) {
2070 my $file = shift;
2072 # Until all named variable macro parameters are
2073 # documented using the bare name (`x`) rather than with
2074 # dots (`x...`), strip the dots:
2075 if ($section =~ /\w\.\.\.$/) {
2076 $section =~ s/\.\.\.$//;
2078 if ($verbose) {
2079 print STDERR "${file}:$.: warning: Variable macro arguments should be documented without dots\n";
2080 ++$warnings;
2084 if ($state == STATE_BODY_WITH_BLANK_LINE && /^\s*\*\s?\S/) {
2085 dump_section($file, $section, $contents);
2086 $section = $section_default;
2087 $contents = "";
2090 if (/$doc_sect/i) { # case insensitive for supported section names
2091 $newsection = $1;
2092 $newcontents = $2;
2094 # map the supported section names to the canonical names
2095 if ($newsection =~ m/^description$/i) {
2096 $newsection = $section_default;
2097 } elsif ($newsection =~ m/^context$/i) {
2098 $newsection = $section_context;
2099 } elsif ($newsection =~ m/^returns?$/i) {
2100 $newsection = $section_return;
2101 } elsif ($newsection =~ m/^\@return$/) {
2102 # special: @return is a section, not a param description
2103 $newsection = $section_return;
2106 if (($contents ne "") && ($contents ne "\n")) {
2107 if (!$in_doc_sect && $verbose) {
2108 print STDERR "${file}:$.: warning: contents before sections\n";
2109 ++$warnings;
2111 dump_section($file, $section, $contents);
2112 $section = $section_default;
2115 $in_doc_sect = 1;
2116 $state = STATE_BODY;
2117 $contents = $newcontents;
2118 $new_start_line = $.;
2119 while (substr($contents, 0, 1) eq " ") {
2120 $contents = substr($contents, 1);
2122 if ($contents ne "") {
2123 $contents .= "\n";
2125 $section = $newsection;
2126 $leading_space = undef;
2127 } elsif (/$doc_end/) {
2128 if (($contents ne "") && ($contents ne "\n")) {
2129 dump_section($file, $section, $contents);
2130 $section = $section_default;
2131 $contents = "";
2133 # look for doc_com + <text> + doc_end:
2134 if ($_ =~ m'\s*\*\s*[a-zA-Z_0-9:\.]+\*/') {
2135 print STDERR "${file}:$.: warning: suspicious ending line: $_";
2136 ++$warnings;
2139 $prototype = "";
2140 $state = STATE_PROTO;
2141 $brcount = 0;
2142 } elsif (/$doc_content/) {
2143 if ($1 eq "") {
2144 if ($section eq $section_context) {
2145 dump_section($file, $section, $contents);
2146 $section = $section_default;
2147 $contents = "";
2148 $new_start_line = $.;
2149 $state = STATE_BODY;
2150 } else {
2151 if ($section ne $section_default) {
2152 $state = STATE_BODY_WITH_BLANK_LINE;
2153 } else {
2154 $state = STATE_BODY;
2156 $contents .= "\n";
2158 } elsif ($state == STATE_BODY_MAYBE) {
2159 # Continued declaration purpose
2160 chomp($declaration_purpose);
2161 $declaration_purpose .= " " . $1;
2162 $declaration_purpose =~ s/\s+/ /g;
2163 } else {
2164 my $cont = $1;
2165 if ($section =~ m/^@/ || $section eq $section_context) {
2166 if (!defined $leading_space) {
2167 if ($cont =~ m/^(\s+)/) {
2168 $leading_space = $1;
2169 } else {
2170 $leading_space = "";
2173 $cont =~ s/^$leading_space//;
2175 $contents .= $cont . "\n";
2177 } else {
2178 # i dont know - bad line? ignore.
2179 print STDERR "${file}:$.: warning: bad line: $_";
2180 ++$warnings;
2186 # STATE_PROTO: reading a function/whatever prototype.
2188 sub process_proto($$) {
2189 my $file = shift;
2191 if (/$doc_inline_oneline/) {
2192 $section = $1;
2193 $contents = $2;
2194 if ($contents ne "") {
2195 $contents .= "\n";
2196 dump_section($file, $section, $contents);
2197 $section = $section_default;
2198 $contents = "";
2200 } elsif (/$doc_inline_start/) {
2201 $state = STATE_INLINE;
2202 $inline_doc_state = STATE_INLINE_NAME;
2203 } elsif ($decl_type eq 'function') {
2204 process_proto_function($_, $file);
2205 } else {
2206 process_proto_type($_, $file);
2211 # STATE_DOCBLOCK: within a DOC: block.
2213 sub process_docblock($$) {
2214 my $file = shift;
2216 if (/$doc_end/) {
2217 dump_doc_section($file, $section, $contents);
2218 $section = $section_default;
2219 $contents = "";
2220 $function = "";
2221 %parameterdescs = ();
2222 %parametertypes = ();
2223 @parameterlist = ();
2224 %sections = ();
2225 @sectionlist = ();
2226 $prototype = "";
2227 $state = STATE_NORMAL;
2228 } elsif (/$doc_content/) {
2229 if ( $1 eq "" ) {
2230 $contents .= $blankline;
2231 } else {
2232 $contents .= $1 . "\n";
2238 # STATE_INLINE: docbook comments within a prototype.
2240 sub process_inline($$) {
2241 my $file = shift;
2243 # First line (state 1) needs to be a @parameter
2244 if ($inline_doc_state == STATE_INLINE_NAME && /$doc_inline_sect/o) {
2245 $section = $1;
2246 $contents = $2;
2247 $new_start_line = $.;
2248 if ($contents ne "") {
2249 while (substr($contents, 0, 1) eq " ") {
2250 $contents = substr($contents, 1);
2252 $contents .= "\n";
2254 $inline_doc_state = STATE_INLINE_TEXT;
2255 # Documentation block end */
2256 } elsif (/$doc_inline_end/) {
2257 if (($contents ne "") && ($contents ne "\n")) {
2258 dump_section($file, $section, $contents);
2259 $section = $section_default;
2260 $contents = "";
2262 $state = STATE_PROTO;
2263 $inline_doc_state = STATE_INLINE_NA;
2264 # Regular text
2265 } elsif (/$doc_content/) {
2266 if ($inline_doc_state == STATE_INLINE_TEXT) {
2267 $contents .= $1 . "\n";
2268 # nuke leading blank lines
2269 if ($contents =~ /^\s*$/) {
2270 $contents = "";
2272 } elsif ($inline_doc_state == STATE_INLINE_NAME) {
2273 $inline_doc_state = STATE_INLINE_ERROR;
2274 print STDERR "${file}:$.: warning: ";
2275 print STDERR "Incorrect use of kernel-doc format: $_";
2276 ++$warnings;
2282 sub process_file($) {
2283 my $file;
2284 my $initial_section_counter = $section_counter;
2285 my ($orig_file) = @_;
2287 $file = map_filename($orig_file);
2289 if (!open(IN_FILE,"<$file")) {
2290 print STDERR "Error: Cannot open file $file\n";
2291 ++$errors;
2292 return;
2295 $. = 1;
2297 $section_counter = 0;
2298 while (<IN_FILE>) {
2299 while (s/\\\s*$//) {
2300 $_ .= <IN_FILE>;
2302 # Replace tabs by spaces
2303 while ($_ =~ s/\t+/' ' x (length($&) * 8 - length($`) % 8)/e) {};
2304 # Hand this line to the appropriate state handler
2305 if ($state == STATE_NORMAL) {
2306 process_normal();
2307 } elsif ($state == STATE_NAME) {
2308 process_name($file, $_);
2309 } elsif ($state == STATE_BODY || $state == STATE_BODY_MAYBE ||
2310 $state == STATE_BODY_WITH_BLANK_LINE) {
2311 process_body($file, $_);
2312 } elsif ($state == STATE_INLINE) { # scanning for inline parameters
2313 process_inline($file, $_);
2314 } elsif ($state == STATE_PROTO) {
2315 process_proto($file, $_);
2316 } elsif ($state == STATE_DOCBLOCK) {
2317 process_docblock($file, $_);
2321 # Make sure we got something interesting.
2322 if ($initial_section_counter == $section_counter && $
2323 output_mode ne "none") {
2324 if ($output_selection == OUTPUT_INCLUDE) {
2325 print STDERR "${file}:1: warning: '$_' not found\n"
2326 for keys %function_table;
2328 else {
2329 print STDERR "${file}:1: warning: no structured comments found\n";
2332 close IN_FILE;
2336 $sphinx_major = get_sphinx_version();
2337 $kernelversion = get_kernel_version();
2339 # generate a sequence of code that will splice in highlighting information
2340 # using the s// operator.
2341 for (my $k = 0; $k < @highlights; $k++) {
2342 my $pattern = $highlights[$k][0];
2343 my $result = $highlights[$k][1];
2344 # print STDERR "scanning pattern:$pattern, highlight:($result)\n";
2345 $dohighlight .= "\$contents =~ s:$pattern:$result:gs;\n";
2348 # Read the file that maps relative names to absolute names for
2349 # separate source and object directories and for shadow trees.
2350 if (open(SOURCE_MAP, "<.tmp_filelist.txt")) {
2351 my ($relname, $absname);
2352 while(<SOURCE_MAP>) {
2353 chop();
2354 ($relname, $absname) = (split())[0..1];
2355 $relname =~ s:^/+::;
2356 $source_map{$relname} = $absname;
2358 close(SOURCE_MAP);
2361 if ($output_selection == OUTPUT_EXPORTED ||
2362 $output_selection == OUTPUT_INTERNAL) {
2364 push(@export_file_list, @ARGV);
2366 foreach (@export_file_list) {
2367 chomp;
2368 process_export_file($_);
2372 foreach (@ARGV) {
2373 chomp;
2374 process_file($_);
2376 if ($verbose && $errors) {
2377 print STDERR "$errors errors\n";
2379 if ($verbose && $warnings) {
2380 print STDERR "$warnings warnings\n";
2383 if ($Werror && $warnings) {
2384 print STDERR "$warnings warnings as Errors\n";
2385 exit($warnings);
2386 } else {
2387 exit($output_mode eq "none" ? 0 : $errors)