docs: kernel-doc: Finish moving STATE_* code out of process_file()
[linux-2.6-block.git] / scripts / kernel-doc
CommitLineData
cb77f0d6 1#!/usr/bin/env perl
1da177e4 2
cb77f0d6 3use warnings;
1da177e4
LT
4use strict;
5
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 ##
70c95b00 9## Copyright (C) 2005-2012 Randy Dunlap ##
1b40c194 10## Copyright (C) 2012 Dan Luedtke ##
1da177e4
LT
11## ##
12## #define enhancements by Armin Kuster <akuster@mvista.com> ##
13## Copyright (c) 2000 MontaVista Software, Inc. ##
14## ##
15## This software falls under the GNU General Public License. ##
16## Please read the COPYING file for more information ##
17
1da177e4
LT
18# 18/01/2001 - Cleanups
19# Functions prototyped as foo(void) same as foo()
20# Stop eval'ing where we don't need to.
21# -- huggie@earth.li
22
23# 27/06/2001 - Allowed whitespace after initial "/**" and
24# allowed comments before function declarations.
25# -- Christian Kreibich <ck@whoop.org>
26
27# Still to do:
28# - add perldoc documentation
29# - Look more closely at some of the scarier bits :)
30
31# 26/05/2001 - Support for separate source and object trees.
32# Return error code.
33# Keith Owens <kaos@ocs.com.au>
34
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>
39
1b40c194
DL
40# 25/07/2012 - Added support for HTML5
41# -- Dan Luedtke <mail@danrl.de>
1da177e4 42
fadc0b31
JN
43sub usage {
44 my $message = <<"EOF";
45Usage: $0 [OPTION ...] FILE ...
46
47Read C language source or header FILEs, extract embedded documentation comments,
48and print formatted documentation to standard output.
49
50The documentation comments are identified by "/**" opening comment mark. See
857af3b7 51Documentation/doc-guide/kernel-doc.rst for the documentation comment syntax.
fadc0b31
JN
52
53Output format selection (mutually exclusive):
fadc0b31 54 -man Output troff manual page format. This is the default.
c0d1b6ee 55 -rst Output reStructuredText format.
3a025e1d 56 -none Do not output documentation, only warnings.
fadc0b31
JN
57
58Output selection (mutually exclusive):
86ae2e38
JN
59 -export Only output documentation for symbols that have been
60 exported using EXPORT_SYMBOL() or EXPORT_SYMBOL_GPL()
c9b2cfb3 61 in any input FILE or -export-file FILE.
86ae2e38
JN
62 -internal Only output documentation for symbols that have NOT been
63 exported using EXPORT_SYMBOL() or EXPORT_SYMBOL_GPL()
c9b2cfb3 64 in any input FILE or -export-file FILE.
fadc0b31
JN
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.
71
72Output selection modifiers:
73 -no-doc-sections Do not output DOC: sections.
0b0f5f29
DV
74 -enable-lineno Enable output of #define LINENO lines. Only works with
75 reStructuredText format.
88c2b57d
JN
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.
fadc0b31
JN
79
80Other parameters:
81 -v Verbose output, more warnings and other information.
82 -h Print this help.
83
84EOF
85 print $message;
86 exit 1;
87}
1da177e4
LT
88
89#
90# format of comments.
91# In the following table, (...)? signifies optional structure.
92# (...)* signifies 0 or more structure elements
93# /**
94# * function_name(:)? (- short description)?
95# (* @parameterx: (description of parameter x)?)*
96# (* a blank line)?
97# * (Description:)? (Description of function)?
98# * (section header: (section description)? )*
99# (*)?*/
100#
101# So .. the trivial example would be:
102#
103# /**
104# * my_function
b9d97328 105# */
1da177e4 106#
891dcd2f 107# If the Description: header tag is omitted, then there must be a blank line
1da177e4
LT
108# after the last parameter specification.
109# e.g.
110# /**
111# * my_function - does my stuff
112# * @my_arg: its mine damnit
113# *
3c3b809e 114# * Does my stuff explained.
1da177e4
LT
115# */
116#
117# or, could also use:
118# /**
119# * my_function - does my stuff
120# * @my_arg: its mine damnit
3c3b809e 121# * Description: Does my stuff explained.
1da177e4
LT
122# */
123# etc.
124#
b9d97328 125# Besides functions you can also write documentation for structs, unions,
3c3b809e
RD
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.
1da177e4
LT
129# Use the argument mechanism to document members or constants.
130# e.g.
131# /**
132# * struct my_struct - short description
133# * @a: first member
134# * @b: second member
3c3b809e 135# *
1da177e4
LT
136# * Longer description
137# */
138# struct my_struct {
139# int a;
140# int b;
aeec46b9
MW
141# /* private: */
142# int c;
1da177e4
LT
143# };
144#
145# All descriptions can be multiline, except the short function description.
3c3b809e 146#
a4c6ebed
DCLP
147# For really longs structs, you can also describe arguments inside the
148# body of the struct.
149# eg.
150# /**
151# * struct my_struct - short description
152# * @a: first member
153# * @b: second member
154# *
155# * Longer description
156# */
157# struct my_struct {
158# int a;
159# int b;
160# /**
161# * @c: This is longer description of C
162# *
163# * You can use paragraphs to describe arguments
164# * using this method.
165# */
166# int c;
167# };
168#
169# This should be use only for struct/enum members.
170#
3c3b809e
RD
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
1da177e4 173# can be called form interrupts. Unlike other sections you can end it with an
3c3b809e 174# empty line.
4092bac7
YB
175# A non-void function should have a "Return:" section describing the return
176# value(s).
3c3b809e 177# Example-sections should contain the string EXAMPLE so that they are marked
1da177e4
LT
178# appropriately in DocBook.
179#
180# Example:
181# /**
182# * user_function - function that can only be called in user context
183# * @a: some argument
184# * Context: !in_interrupt()
3c3b809e 185# *
1da177e4
LT
186# * Some description
187# * Example:
188# * user_function(22);
189# */
190# ...
191#
192#
193# All descriptive text is further processed, scanning for the following special
194# patterns, which are highlighted appropriately.
195#
196# 'funcname()' - function
197# '$ENVVAR' - environmental variable
198# '&struct_name' - name of a structure (up to two words including 'struct')
5267dd35 199# '&struct_name.member' - name of a structure member
1da177e4
LT
200# '@parameter' - name of a parameter
201# '%CONST' - name of a constant.
b97f193a 202# '``LITERAL``' - literal string without any spaces on it.
1da177e4 203
8484baaa
RD
204## init lots of data
205
1da177e4
LT
206my $errors = 0;
207my $warnings = 0;
5f8c7c98 208my $anon_struct_union = 0;
1da177e4
LT
209
210# match expressions used to find embedded type information
b97f193a
MCC
211my $type_constant = '\b``([^\`]+)``\b';
212my $type_constant2 = '\%([-_\w]+)';
1da177e4 213my $type_func = '(\w+)\(\)';
8ad72163 214my $type_param = '\@(\w*(\.\w+)*(\.\.\.)?)';
5219f18a 215my $type_fp_param = '\@(\w+)\(\)'; # Special RST handling for func ptr params
1da177e4 216my $type_env = '(\$\w+)';
df31175b
PB
217my $type_enum = '\&(enum\s*([_\w]+))';
218my $type_struct = '\&(struct\s*([_\w]+))';
219my $type_typedef = '\&(typedef\s*([_\w]+))';
220my $type_union = '\&(union\s*([_\w]+))';
5267dd35 221my $type_member = '\&([_\w]+)(\.|->)([_\w]+)';
df31175b 222my $type_fallback = '\&([_\w]+)';
f3341dcf 223my $type_member_func = $type_member . '\(\)';
1da177e4
LT
224
225# Output conversion substitutions.
226# One for each output format
227
1da177e4 228# these are pretty rough
4d732701
DCLP
229my @highlights_man = (
230 [$type_constant, "\$1"],
b97f193a 231 [$type_constant2, "\$1"],
4d732701 232 [$type_func, "\\\\fB\$1\\\\fP"],
df31175b 233 [$type_enum, "\\\\fI\$1\\\\fP"],
4d732701 234 [$type_struct, "\\\\fI\$1\\\\fP"],
df31175b
PB
235 [$type_typedef, "\\\\fI\$1\\\\fP"],
236 [$type_union, "\\\\fI\$1\\\\fP"],
5267dd35 237 [$type_param, "\\\\fI\$1\\\\fP"],
df31175b
PB
238 [$type_member, "\\\\fI\$1\$2\$3\\\\fP"],
239 [$type_fallback, "\\\\fI\$1\\\\fP"]
4d732701 240 );
1da177e4
LT
241my $blankline_man = "";
242
c0d1b6ee
JC
243# rst-mode
244my @highlights_rst = (
245 [$type_constant, "``\$1``"],
b97f193a 246 [$type_constant2, "``\$1``"],
f3341dcf 247 # Note: need to escape () to avoid func matching later
5267dd35
PB
248 [$type_member_func, "\\:c\\:type\\:`\$1\$2\$3\\\\(\\\\) <\$1>`"],
249 [$type_member, "\\:c\\:type\\:`\$1\$2\$3 <\$1>`"],
5219f18a 250 [$type_fp_param, "**\$1\\\\(\\\\)**"],
a19bce64 251 [$type_func, "\\:c\\:func\\:`\$1()`"],
df31175b
PB
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>`"],
a7291e7e 256 # in rst this can refer to any type
df31175b 257 [$type_fallback, "\\:c\\:type\\:`\$1`"],
c0d1b6ee
JC
258 [$type_param, "**\$1**"]
259 );
260my $blankline_rst = "\n";
261
1da177e4 262# read arguments
b9d97328 263if ($#ARGV == -1) {
1da177e4
LT
264 usage();
265}
266
8484baaa
RD
267my $kernelversion;
268my $dohighlight = "";
269
1da177e4 270my $verbose = 0;
bdfe2be3 271my $output_mode = "rst";
e314ba31 272my $output_preformatted = 0;
4b44595a 273my $no_doc_sections = 0;
0b0f5f29 274my $enable_lineno = 0;
bdfe2be3
MCC
275my @highlights = @highlights_rst;
276my $blankline = $blankline_rst;
1da177e4 277my $modulename = "Kernel API";
b6c3f456
JN
278
279use constant {
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
285};
286my $output_selection = OUTPUT_ALL;
b2c4105b
BH
287my $show_not_found = 0;
288
88c2b57d
JN
289my @export_file_list;
290
b2c4105b
BH
291my @build_time;
292if (defined($ENV{'KBUILD_BUILD_TIMESTAMP'}) &&
293 (my $seconds = `date -d"${ENV{'KBUILD_BUILD_TIMESTAMP'}}" +%s`) ne '') {
294 @build_time = gmtime($seconds);
295} else {
296 @build_time = localtime;
297}
298
3c3b809e
RD
299my $man_date = ('January', 'February', 'March', 'April', 'May', 'June',
300 'July', 'August', 'September', 'October',
b2c4105b
BH
301 'November', 'December')[$build_time[4]] .
302 " " . ($build_time[5]+1900);
1da177e4 303
8484baaa 304# Essentially these are globals.
b9d97328
RD
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
1da177e4 307# could cause "use of undefined value" or other bugs.
b9d97328 308my ($function, %function_table, %parametertypes, $declaration_purpose);
0b0f5f29 309my $declaration_start_line;
b9d97328 310my ($type, $declaration_name, $return_type);
1c32fd0c 311my ($newsection, $newcontents, $prototype, $brcount, %source_map);
1da177e4 312
bd0e88e5
RD
313if (defined($ENV{'KBUILD_VERBOSE'})) {
314 $verbose = "$ENV{'KBUILD_VERBOSE'}";
315}
316
3c3b809e 317# Generated docbook code is inserted in a template at a point where
1da177e4
LT
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
322# into html.
323my $section_counter = 0;
324
325my $lineprefix="";
326
48af606a
JN
327# Parser states
328use constant {
329 STATE_NORMAL => 0, # normal code
330 STATE_NAME => 1, # looking for function name
17b78717
JC
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
48af606a 336};
1da177e4 337my $state;
850622df 338my $in_doc_sect;
d742f24d 339my $leading_space;
1da177e4 340
48af606a
JN
341# Inline documentation state
342use constant {
343 STATE_INLINE_NA => 0, # not applicable ($state != STATE_INLINE)
344 STATE_INLINE_NAME => 1, # looking for member name (@foo:)
345 STATE_INLINE_TEXT => 2, # looking for member documentation
346 STATE_INLINE_END => 3, # done
347 STATE_INLINE_ERROR => 4, # error - Comment without header was found.
348 # Spit a warning as it's not
349 # proper kernel-doc and ignore the rest.
350};
351my $inline_doc_state;
a4c6ebed 352
1da177e4
LT
353#declaration types: can be
354# 'function', 'struct', 'union', 'enum', 'typedef'
355my $decl_type;
356
1da177e4
LT
357my $doc_start = '^/\*\*\s*$'; # Allow whitespace at end of comment start.
358my $doc_end = '\*/';
359my $doc_com = '\s*\*\s*';
12ae6779 360my $doc_com_body = '\s*\* ?';
b9d97328 361my $doc_decl = $doc_com . '(\w+)';
f624adef 362# @params and a strictly limited set of supported section names
8569de68 363my $doc_sect = $doc_com .
ef00028b 364 '\s*(\@[.\w]+|\@\.\.\.|description|context|returns?|notes?|examples?)\s*:(.*)';
12ae6779 365my $doc_content = $doc_com_body . '(.*)';
b9d97328 366my $doc_block = $doc_com . 'DOC:\s*(.*)?';
48af606a
JN
367my $doc_inline_start = '^\s*/\*\*\s*$';
368my $doc_inline_sect = '\s*\*\s*(@[\w\s]+):(.*)';
369my $doc_inline_end = '^\s*\*/\s*$';
0c9aa209 370my $doc_inline_oneline = '^\s*/\*\*\s*(@[\w\s]+):\s*(.*)\s*\*/\s*$';
86ae2e38 371my $export_symbol = '^\s*EXPORT_SYMBOL(_GPL)?\s*\(\s*(\w+)\s*\)\s*;';
1da177e4 372
1da177e4 373my %parameterdescs;
0b0f5f29 374my %parameterdesc_start_lines;
1da177e4
LT
375my @parameterlist;
376my %sections;
377my @sectionlist;
0b0f5f29 378my %section_start_lines;
a1d94aa5
RD
379my $sectcheck;
380my $struct_actual;
1da177e4
LT
381
382my $contents = "";
0b0f5f29 383my $new_start_line = 0;
f624adef
JN
384
385# the canonical section names. see also $doc_sect above.
1da177e4
LT
386my $section_default = "Description"; # default section
387my $section_intro = "Introduction";
388my $section = $section_default;
389my $section_context = "Context";
4092bac7 390my $section_return = "Return";
1da177e4
LT
391
392my $undescribed = "-- undescribed --";
393
394reset_state();
395
b031ac4e
MCC
396while ($ARGV[0] =~ m/^--?(.*)/) {
397 my $cmd = $1;
398 shift @ARGV;
399 if ($cmd eq "man") {
1da177e4 400 $output_mode = "man";
4d732701 401 @highlights = @highlights_man;
1da177e4 402 $blankline = $blankline_man;
b031ac4e 403 } elsif ($cmd eq "rst") {
c0d1b6ee
JC
404 $output_mode = "rst";
405 @highlights = @highlights_rst;
406 $blankline = $blankline_rst;
b031ac4e 407 } elsif ($cmd eq "none") {
3a025e1d 408 $output_mode = "none";
b031ac4e 409 } elsif ($cmd eq "module") { # not needed for XML, inherits from calling document
1da177e4 410 $modulename = shift @ARGV;
b031ac4e 411 } elsif ($cmd eq "function") { # to only output specific functions
b6c3f456 412 $output_selection = OUTPUT_INCLUDE;
1da177e4
LT
413 $function = shift @ARGV;
414 $function_table{$function} = 1;
b031ac4e 415 } elsif ($cmd eq "nofunction") { # output all except specific functions
b6c3f456 416 $output_selection = OUTPUT_EXCLUDE;
1da177e4
LT
417 $function = shift @ARGV;
418 $function_table{$function} = 1;
b031ac4e 419 } elsif ($cmd eq "export") { # only exported symbols
b6c3f456 420 $output_selection = OUTPUT_EXPORTED;
da9726ec 421 %function_table = ();
b031ac4e 422 } elsif ($cmd eq "internal") { # only non-exported symbols
b6c3f456 423 $output_selection = OUTPUT_INTERNAL;
da9726ec 424 %function_table = ();
b031ac4e 425 } elsif ($cmd eq "export-file") {
88c2b57d
JN
426 my $file = shift @ARGV;
427 push(@export_file_list, $file);
b031ac4e 428 } elsif ($cmd eq "v") {
1da177e4 429 $verbose = 1;
b031ac4e 430 } elsif (($cmd eq "h") || ($cmd eq "help")) {
1da177e4 431 usage();
b031ac4e 432 } elsif ($cmd eq 'no-doc-sections') {
4b44595a 433 $no_doc_sections = 1;
b031ac4e 434 } elsif ($cmd eq 'enable-lineno') {
0b0f5f29 435 $enable_lineno = 1;
b031ac4e 436 } elsif ($cmd eq 'show-not-found') {
e946c43a 437 $show_not_found = 1;
b031ac4e
MCC
438 } else {
439 # Unknown argument
440 usage();
1da177e4
LT
441 }
442}
443
8484baaa
RD
444# continue execution near EOF;
445
53f049fa
BP
446# get kernel version from env
447sub get_kernel_version() {
1b9bc22d 448 my $version = 'unknown kernel version';
53f049fa
BP
449
450 if (defined($ENV{'KERNELVERSION'})) {
451 $version = $ENV{'KERNELVERSION'};
452 }
453 return $version;
454}
1da177e4 455
0b0f5f29
DV
456#
457sub print_lineno {
458 my $lineno = shift;
459 if ($enable_lineno && defined($lineno)) {
460 print "#define LINENO " . $lineno . "\n";
461 }
462}
1da177e4
LT
463##
464# dumps section contents to arrays/hashes intended for that purpose.
465#
466sub dump_section {
94dc7ad5 467 my $file = shift;
1da177e4
LT
468 my $name = shift;
469 my $contents = join "\n", @_;
470
13901ef2 471 if ($name =~ m/$type_param/) {
1da177e4
LT
472 $name = $1;
473 $parameterdescs{$name} = $contents;
a1d94aa5 474 $sectcheck = $sectcheck . $name . " ";
0b0f5f29
DV
475 $parameterdesc_start_lines{$name} = $new_start_line;
476 $new_start_line = 0;
ced69090 477 } elsif ($name eq "@\.\.\.") {
ced69090
RD
478 $name = "...";
479 $parameterdescs{$name} = $contents;
a1d94aa5 480 $sectcheck = $sectcheck . $name . " ";
0b0f5f29
DV
481 $parameterdesc_start_lines{$name} = $new_start_line;
482 $new_start_line = 0;
1da177e4 483 } else {
94dc7ad5 484 if (defined($sections{$name}) && ($sections{$name} ne "")) {
95b6be9d
JN
485 # Only warn on user specified duplicate section names.
486 if ($name ne $section_default) {
487 print STDERR "${file}:$.: warning: duplicate section name '$name'\n";
488 ++$warnings;
489 }
32217761
JN
490 $sections{$name} .= $contents;
491 } else {
492 $sections{$name} = $contents;
493 push @sectionlist, $name;
0b0f5f29
DV
494 $section_start_lines{$name} = $new_start_line;
495 $new_start_line = 0;
94dc7ad5 496 }
1da177e4
LT
497 }
498}
499
b112e0f7
JB
500##
501# dump DOC: section after checking that it should go out
502#
503sub dump_doc_section {
94dc7ad5 504 my $file = shift;
b112e0f7
JB
505 my $name = shift;
506 my $contents = join "\n", @_;
507
4b44595a
JB
508 if ($no_doc_sections) {
509 return;
510 }
511
b6c3f456
JN
512 if (($output_selection == OUTPUT_ALL) ||
513 ($output_selection == OUTPUT_INCLUDE &&
514 defined($function_table{$name})) ||
515 ($output_selection == OUTPUT_EXCLUDE &&
516 !defined($function_table{$name})))
b112e0f7 517 {
94dc7ad5 518 dump_section($file, $name, $contents);
b112e0f7
JB
519 output_blockhead({'sectionlist' => \@sectionlist,
520 'sections' => \%sections,
521 'module' => $modulename,
b6c3f456 522 'content-only' => ($output_selection != OUTPUT_ALL), });
b112e0f7
JB
523 }
524}
525
1da177e4
LT
526##
527# output function
528#
529# parameterdescs, a hash.
530# function => "function name"
531# parameterlist => @list of parameters
532# parameterdescs => %parameter descriptions
533# sectionlist => @list of sections
a21217da 534# sections => %section descriptions
3c3b809e 535#
1da177e4
LT
536
537sub output_highlight {
538 my $contents = join "\n",@_;
539 my $line;
540
541# DEBUG
542# if (!defined $contents) {
543# use Carp;
544# confess "output_highlight got called with no args?\n";
545# }
546
3eb014a1 547# print STDERR "contents b4:$contents\n";
1da177e4
LT
548 eval $dohighlight;
549 die $@ if $@;
3eb014a1
RD
550# print STDERR "contents af:$contents\n";
551
1da177e4 552 foreach $line (split "\n", $contents) {
12ae6779
DS
553 if (! $output_preformatted) {
554 $line =~ s/^\s*//;
555 }
3c308798 556 if ($line eq ""){
e314ba31 557 if (! $output_preformatted) {
0bba924c 558 print $lineprefix, $blankline;
e314ba31 559 }
1da177e4 560 } else {
cdccb316
RD
561 if ($output_mode eq "man" && substr($line, 0, 1) eq ".") {
562 print "\\&$line";
563 } else {
564 print $lineprefix, $line;
565 }
1da177e4
LT
566 }
567 print "\n";
568 }
569}
570
1da177e4
LT
571##
572# output function in man
573sub output_function_man(%) {
574 my %args = %{$_[0]};
575 my ($parameter, $section);
576 my $count;
577
578 print ".TH \"$args{'function'}\" 9 \"$args{'function'}\" \"$man_date\" \"Kernel Hacker's Manual\" LINUX\n";
579
580 print ".SH NAME\n";
b9d97328 581 print $args{'function'} . " \\- " . $args{'purpose'} . "\n";
1da177e4
LT
582
583 print ".SH SYNOPSIS\n";
a21217da 584 if ($args{'functiontype'} ne "") {
b9d97328 585 print ".B \"" . $args{'functiontype'} . "\" " . $args{'function'} . "\n";
a21217da 586 } else {
b9d97328 587 print ".B \"" . $args{'function'} . "\n";
a21217da 588 }
1da177e4
LT
589 $count = 0;
590 my $parenth = "(";
591 my $post = ",";
592 foreach my $parameter (@{$args{'parameterlist'}}) {
593 if ($count == $#{$args{'parameterlist'}}) {
594 $post = ");";
595 }
596 $type = $args{'parametertypes'}{$parameter};
597 if ($type =~ m/([^\(]*\(\*)\s*\)\s*\(([^\)]*)\)/) {
598 # pointer-to-function
b9d97328 599 print ".BI \"" . $parenth . $1 . "\" " . $parameter . " \") (" . $2 . ")" . $post . "\"\n";
1da177e4
LT
600 } else {
601 $type =~ s/([^\*])$/$1 /;
b9d97328 602 print ".BI \"" . $parenth . $type . "\" " . $parameter . " \"" . $post . "\"\n";
1da177e4
LT
603 }
604 $count++;
605 $parenth = "";
606 }
607
608 print ".SH ARGUMENTS\n";
609 foreach $parameter (@{$args{'parameterlist'}}) {
610 my $parameter_name = $parameter;
611 $parameter_name =~ s/\[.*//;
612
b9d97328 613 print ".IP \"" . $parameter . "\" 12\n";
1da177e4
LT
614 output_highlight($args{'parameterdescs'}{$parameter_name});
615 }
616 foreach $section (@{$args{'sectionlist'}}) {
617 print ".SH \"", uc $section, "\"\n";
618 output_highlight($args{'sections'}{$section});
619 }
620}
621
622##
623# output enum in man
624sub output_enum_man(%) {
625 my %args = %{$_[0]};
626 my ($parameter, $section);
627 my $count;
628
629 print ".TH \"$args{'module'}\" 9 \"enum $args{'enum'}\" \"$man_date\" \"API Manual\" LINUX\n";
630
631 print ".SH NAME\n";
b9d97328 632 print "enum " . $args{'enum'} . " \\- " . $args{'purpose'} . "\n";
1da177e4
LT
633
634 print ".SH SYNOPSIS\n";
b9d97328 635 print "enum " . $args{'enum'} . " {\n";
1da177e4
LT
636 $count = 0;
637 foreach my $parameter (@{$args{'parameterlist'}}) {
3c308798 638 print ".br\n.BI \" $parameter\"\n";
1da177e4
LT
639 if ($count == $#{$args{'parameterlist'}}) {
640 print "\n};\n";
641 last;
642 }
643 else {
644 print ", \n.br\n";
645 }
646 $count++;
647 }
648
649 print ".SH Constants\n";
650 foreach $parameter (@{$args{'parameterlist'}}) {
651 my $parameter_name = $parameter;
652 $parameter_name =~ s/\[.*//;
653
b9d97328 654 print ".IP \"" . $parameter . "\" 12\n";
1da177e4
LT
655 output_highlight($args{'parameterdescs'}{$parameter_name});
656 }
657 foreach $section (@{$args{'sectionlist'}}) {
658 print ".SH \"$section\"\n";
659 output_highlight($args{'sections'}{$section});
660 }
661}
662
663##
664# output struct in man
665sub output_struct_man(%) {
666 my %args = %{$_[0]};
667 my ($parameter, $section);
668
b9d97328 669 print ".TH \"$args{'module'}\" 9 \"" . $args{'type'} . " " . $args{'struct'} . "\" \"$man_date\" \"API Manual\" LINUX\n";
1da177e4
LT
670
671 print ".SH NAME\n";
b9d97328 672 print $args{'type'} . " " . $args{'struct'} . " \\- " . $args{'purpose'} . "\n";
1da177e4 673
8ad72163
MCC
674 my $declaration = $args{'definition'};
675 $declaration =~ s/\t/ /g;
676 $declaration =~ s/\n/"\n.br\n.BI \"/g;
1da177e4 677 print ".SH SYNOPSIS\n";
b9d97328 678 print $args{'type'} . " " . $args{'struct'} . " {\n.br\n";
8ad72163 679 print ".BI \"$declaration\n};\n.br\n\n";
1da177e4 680
c51d3dac 681 print ".SH Members\n";
1da177e4
LT
682 foreach $parameter (@{$args{'parameterlist'}}) {
683 ($parameter =~ /^#/) && next;
684
685 my $parameter_name = $parameter;
686 $parameter_name =~ s/\[.*//;
687
3c308798 688 ($args{'parameterdescs'}{$parameter_name} ne $undescribed) || next;
b9d97328 689 print ".IP \"" . $parameter . "\" 12\n";
1da177e4
LT
690 output_highlight($args{'parameterdescs'}{$parameter_name});
691 }
692 foreach $section (@{$args{'sectionlist'}}) {
693 print ".SH \"$section\"\n";
694 output_highlight($args{'sections'}{$section});
695 }
696}
697
698##
699# output typedef in man
700sub output_typedef_man(%) {
701 my %args = %{$_[0]};
702 my ($parameter, $section);
703
704 print ".TH \"$args{'module'}\" 9 \"$args{'typedef'}\" \"$man_date\" \"API Manual\" LINUX\n";
705
706 print ".SH NAME\n";
b9d97328 707 print "typedef " . $args{'typedef'} . " \\- " . $args{'purpose'} . "\n";
1da177e4
LT
708
709 foreach $section (@{$args{'sectionlist'}}) {
710 print ".SH \"$section\"\n";
711 output_highlight($args{'sections'}{$section});
712 }
713}
714
b112e0f7 715sub output_blockhead_man(%) {
1da177e4
LT
716 my %args = %{$_[0]};
717 my ($parameter, $section);
718 my $count;
719
720 print ".TH \"$args{'module'}\" 9 \"$args{'module'}\" \"$man_date\" \"API Manual\" LINUX\n";
721
722 foreach $section (@{$args{'sectionlist'}}) {
723 print ".SH \"$section\"\n";
724 output_highlight($args{'sections'}{$section});
725 }
726}
727
c0d1b6ee
JC
728##
729# output in restructured text
730#
731
732#
733# This could use some work; it's used to output the DOC: sections, and
734# starts by putting out the name of the doc section itself, but that tends
735# to duplicate a header already in the template file.
736#
737sub output_blockhead_rst(%) {
738 my %args = %{$_[0]};
739 my ($parameter, $section);
740
741 foreach $section (@{$args{'sectionlist'}}) {
9e72184b
JN
742 if ($output_selection != OUTPUT_INCLUDE) {
743 print "**$section**\n\n";
744 }
0b0f5f29 745 print_lineno($section_start_lines{$section});
c0d1b6ee
JC
746 output_highlight_rst($args{'sections'}{$section});
747 print "\n";
748 }
749}
750
751sub output_highlight_rst {
752 my $contents = join "\n",@_;
753 my $line;
754
c0d1b6ee
JC
755 eval $dohighlight;
756 die $@ if $@;
757
758 foreach $line (split "\n", $contents) {
830066a7 759 print $lineprefix . $line . "\n";
c0d1b6ee
JC
760 }
761}
762
763sub output_function_rst(%) {
764 my %args = %{$_[0]};
765 my ($parameter, $section);
c099ff69 766 my $oldprefix = $lineprefix;
82801d06
MCC
767 my $start = "";
768
769 if ($args{'typedef'}) {
770 print ".. c:type:: ". $args{'function'} . "\n\n";
771 print_lineno($declaration_start_line);
772 print " **Typedef**: ";
773 $lineprefix = "";
774 output_highlight_rst($args{'purpose'});
775 $start = "\n\n**Syntax**\n\n ``";
776 } else {
777 print ".. c:function:: ";
778 }
c0d1b6ee 779 if ($args{'functiontype'} ne "") {
82801d06 780 $start .= $args{'functiontype'} . " " . $args{'function'} . " (";
c0d1b6ee 781 } else {
82801d06 782 $start .= $args{'function'} . " (";
c0d1b6ee
JC
783 }
784 print $start;
785
786 my $count = 0;
787 foreach my $parameter (@{$args{'parameterlist'}}) {
788 if ($count ne 0) {
789 print ", ";
790 }
791 $count++;
792 $type = $args{'parametertypes'}{$parameter};
a88b1672 793
c0d1b6ee
JC
794 if ($type =~ m/([^\(]*\(\*)\s*\)\s*\(([^\)]*)\)/) {
795 # pointer-to-function
796 print $1 . $parameter . ") (" . $2;
797 } else {
798 print $type . " " . $parameter;
799 }
800 }
82801d06
MCC
801 if ($args{'typedef'}) {
802 print ");``\n\n";
803 } else {
804 print ")\n\n";
805 print_lineno($declaration_start_line);
806 $lineprefix = " ";
807 output_highlight_rst($args{'purpose'});
808 print "\n";
809 }
c0d1b6ee 810
ecbcfba1
JN
811 print "**Parameters**\n\n";
812 $lineprefix = " ";
c0d1b6ee
JC
813 foreach $parameter (@{$args{'parameterlist'}}) {
814 my $parameter_name = $parameter;
ada5f446 815 $parameter_name =~ s/\[.*//;
c0d1b6ee
JC
816 $type = $args{'parametertypes'}{$parameter};
817
818 if ($type ne "") {
ecbcfba1 819 print "``$type $parameter``\n";
c0d1b6ee 820 } else {
ecbcfba1 821 print "``$parameter``\n";
c0d1b6ee 822 }
0b0f5f29
DV
823
824 print_lineno($parameterdesc_start_lines{$parameter_name});
825
5e64fa9c
JN
826 if (defined($args{'parameterdescs'}{$parameter_name}) &&
827 $args{'parameterdescs'}{$parameter_name} ne $undescribed) {
c0d1b6ee 828 output_highlight_rst($args{'parameterdescs'}{$parameter_name});
c0d1b6ee 829 } else {
d4b08e0c 830 print " *undescribed*\n";
c0d1b6ee
JC
831 }
832 print "\n";
833 }
c099ff69
JN
834
835 $lineprefix = $oldprefix;
c0d1b6ee
JC
836 output_section_rst(@_);
837}
838
839sub output_section_rst(%) {
840 my %args = %{$_[0]};
841 my $section;
842 my $oldprefix = $lineprefix;
ecbcfba1 843 $lineprefix = "";
c0d1b6ee
JC
844
845 foreach $section (@{$args{'sectionlist'}}) {
ecbcfba1 846 print "**$section**\n\n";
0b0f5f29 847 print_lineno($section_start_lines{$section});
c0d1b6ee
JC
848 output_highlight_rst($args{'sections'}{$section});
849 print "\n";
850 }
851 print "\n";
852 $lineprefix = $oldprefix;
853}
854
855sub output_enum_rst(%) {
856 my %args = %{$_[0]};
857 my ($parameter);
c099ff69 858 my $oldprefix = $lineprefix;
c0d1b6ee 859 my $count;
c0d1b6ee 860 my $name = "enum " . $args{'enum'};
62850976
JN
861
862 print "\n\n.. c:type:: " . $name . "\n\n";
0b0f5f29 863 print_lineno($declaration_start_line);
c099ff69
JN
864 $lineprefix = " ";
865 output_highlight_rst($args{'purpose'});
866 print "\n";
c0d1b6ee 867
ecbcfba1
JN
868 print "**Constants**\n\n";
869 $lineprefix = " ";
c0d1b6ee 870 foreach $parameter (@{$args{'parameterlist'}}) {
ecbcfba1 871 print "``$parameter``\n";
c0d1b6ee
JC
872 if ($args{'parameterdescs'}{$parameter} ne $undescribed) {
873 output_highlight_rst($args{'parameterdescs'}{$parameter});
874 } else {
d4b08e0c 875 print " *undescribed*\n";
c0d1b6ee
JC
876 }
877 print "\n";
878 }
c099ff69 879
c0d1b6ee
JC
880 $lineprefix = $oldprefix;
881 output_section_rst(@_);
882}
883
884sub output_typedef_rst(%) {
885 my %args = %{$_[0]};
886 my ($parameter);
c099ff69 887 my $oldprefix = $lineprefix;
c0d1b6ee
JC
888 my $name = "typedef " . $args{'typedef'};
889
62850976 890 print "\n\n.. c:type:: " . $name . "\n\n";
0b0f5f29 891 print_lineno($declaration_start_line);
c099ff69
JN
892 $lineprefix = " ";
893 output_highlight_rst($args{'purpose'});
894 print "\n";
c0d1b6ee 895
c099ff69 896 $lineprefix = $oldprefix;
c0d1b6ee
JC
897 output_section_rst(@_);
898}
899
900sub output_struct_rst(%) {
901 my %args = %{$_[0]};
902 my ($parameter);
c099ff69 903 my $oldprefix = $lineprefix;
c0d1b6ee
JC
904 my $name = $args{'type'} . " " . $args{'struct'};
905
62850976 906 print "\n\n.. c:type:: " . $name . "\n\n";
0b0f5f29 907 print_lineno($declaration_start_line);
c099ff69
JN
908 $lineprefix = " ";
909 output_highlight_rst($args{'purpose'});
910 print "\n";
c0d1b6ee 911
ecbcfba1
JN
912 print "**Definition**\n\n";
913 print "::\n\n";
8ad72163
MCC
914 my $declaration = $args{'definition'};
915 $declaration =~ s/\t/ /g;
916 print " " . $args{'type'} . " " . $args{'struct'} . " {\n$declaration };\n\n";
c0d1b6ee 917
ecbcfba1
JN
918 print "**Members**\n\n";
919 $lineprefix = " ";
c0d1b6ee
JC
920 foreach $parameter (@{$args{'parameterlist'}}) {
921 ($parameter =~ /^#/) && next;
922
923 my $parameter_name = $parameter;
924 $parameter_name =~ s/\[.*//;
925
926 ($args{'parameterdescs'}{$parameter_name} ne $undescribed) || next;
927 $type = $args{'parametertypes'}{$parameter};
0b0f5f29 928 print_lineno($parameterdesc_start_lines{$parameter_name});
6d232c80 929 print "``" . $parameter . "``\n";
c0d1b6ee 930 output_highlight_rst($args{'parameterdescs'}{$parameter_name});
c0d1b6ee
JC
931 print "\n";
932 }
933 print "\n";
c099ff69
JN
934
935 $lineprefix = $oldprefix;
c0d1b6ee
JC
936 output_section_rst(@_);
937}
938
3a025e1d
MW
939## none mode output functions
940
941sub output_function_none(%) {
942}
943
944sub output_enum_none(%) {
945}
946
947sub output_typedef_none(%) {
948}
949
950sub output_struct_none(%) {
951}
952
953sub output_blockhead_none(%) {
954}
955
1da177e4 956##
27205744
RD
957# generic output function for all types (function, struct/union, typedef, enum);
958# calls the generated, variable output_ function name based on
959# functype and output_mode
1da177e4
LT
960sub output_declaration {
961 no strict 'refs';
962 my $name = shift;
963 my $functype = shift;
964 my $func = "output_${functype}_$output_mode";
b6c3f456
JN
965 if (($output_selection == OUTPUT_ALL) ||
966 (($output_selection == OUTPUT_INCLUDE ||
967 $output_selection == OUTPUT_EXPORTED) &&
968 defined($function_table{$name})) ||
969 (($output_selection == OUTPUT_EXCLUDE ||
970 $output_selection == OUTPUT_INTERNAL) &&
971 !($functype eq "function" && defined($function_table{$name}))))
1da177e4 972 {
3c308798 973 &$func(@_);
1da177e4
LT
974 $section_counter++;
975 }
976}
977
978##
27205744 979# generic output function - calls the right one based on current output mode.
b112e0f7 980sub output_blockhead {
1da177e4 981 no strict 'refs';
b9d97328 982 my $func = "output_blockhead_" . $output_mode;
1da177e4
LT
983 &$func(@_);
984 $section_counter++;
985}
986
987##
3c3b809e 988# takes a declaration (struct, union, enum, typedef) and
1da177e4
LT
989# invokes the right handler. NOT called for functions.
990sub dump_declaration($$) {
991 no strict 'refs';
992 my ($prototype, $file) = @_;
b9d97328 993 my $func = "dump_" . $decl_type;
1da177e4
LT
994 &$func(@_);
995}
996
997sub dump_union($$) {
998 dump_struct(@_);
999}
1000
1001sub dump_struct($$) {
1002 my $x = shift;
1003 my $file = shift;
1004
52dc5aec 1005 if ($x =~ /(struct|union)\s+(\w+)\s*{(.*)}/) {
5cb5c31c 1006 my $decl_type = $1;
3c308798
RD
1007 $declaration_name = $2;
1008 my $members = $3;
1da177e4 1009
aeec46b9 1010 # ignore members marked private:
0d8c39e6
MCC
1011 $members =~ s/\/\*\s*private:.*?\/\*\s*public:.*?\*\///gosi;
1012 $members =~ s/\/\*\s*private:.*//gosi;
aeec46b9
MW
1013 # strip comments:
1014 $members =~ s/\/\*.*?\*\///gos;
ef5da59f 1015 # strip attributes
f0074929 1016 $members =~ s/__attribute__\s*\(\([a-z,_\*\s\(\)]*\)\)//i;
7b990789 1017 $members =~ s/__aligned\s*\([^;]*\)//gos;
f0074929 1018 $members =~ s/\s*CRYPTO_MINALIGN_ATTR//gos;
b22b5a9e 1019 # replace DECLARE_BITMAP
45005b27 1020 $members =~ s/DECLARE_BITMAP\s*\(([^,)]+),\s*([^,)]+)\)/unsigned long $1\[BITS_TO_LONGS($2)\]/gos;
1cb566ba 1021 # replace DECLARE_HASHTABLE
45005b27
MCC
1022 $members =~ s/DECLARE_HASHTABLE\s*\(([^,)]+),\s*([^,)]+)\)/unsigned long $1\[1 << (($2) - 1)\]/gos;
1023 # replace DECLARE_KFIFO
1024 $members =~ s/DECLARE_KFIFO\s*\(([^,)]+),\s*([^,)]+),\s*([^,)]+)\)/$2 \*$1/gos;
1025 # replace DECLARE_KFIFO_PTR
1026 $members =~ s/DECLARE_KFIFO_PTR\s*\(([^,)]+),\s*([^,)]+)\)/$2 \*$1/gos;
aeec46b9 1027
8ad72163
MCC
1028 my $declaration = $members;
1029
1030 # Split nested struct/union elements as newer ones
84ce5b98
MCC
1031 while ($members =~ m/(struct|union)([^\{\};]+)\{([^\{\}]*)\}([^\{\}\;]*)\;/) {
1032 my $newmember;
1033 my $maintype = $1;
1034 my $ids = $4;
1035 my $content = $3;
1036 foreach my $id(split /,/, $ids) {
1037 $newmember .= "$maintype $id; ";
1038
8ad72163 1039 $id =~ s/[:\[].*//;
84ce5b98 1040 $id =~ s/^\s*\**(\S+)\s*/$1/;
8ad72163
MCC
1041 foreach my $arg (split /;/, $content) {
1042 next if ($arg =~ m/^\s*$/);
7c0d7e87
MCC
1043 if ($arg =~ m/^([^\(]+\(\*?\s*)([\w\.]*)(\s*\).*)/) {
1044 # pointer-to-function
1045 my $type = $1;
1046 my $name = $2;
1047 my $extra = $3;
1048 next if (!$name);
1049 if ($id =~ m/^\s*$/) {
1050 # anonymous struct/union
84ce5b98 1051 $newmember .= "$type$name$extra; ";
7c0d7e87 1052 } else {
84ce5b98 1053 $newmember .= "$type$id.$name$extra; ";
7c0d7e87 1054 }
8ad72163 1055 } else {
84ce5b98
MCC
1056 my $type;
1057 my $names;
1058 $arg =~ s/^\s+//;
1059 $arg =~ s/\s+$//;
1060 # Handle bitmaps
1061 $arg =~ s/:\s*\d+\s*//g;
1062 # Handle arrays
1063 $arg =~ s/\[\S+\]//g;
1064 # The type may have multiple words,
1065 # and multiple IDs can be defined, like:
1066 # const struct foo, *bar, foobar
1067 # So, we remove spaces when parsing the
1068 # names, in order to match just names
1069 # and commas for the names
1070 $arg =~ s/\s*,\s*/,/g;
1071 if ($arg =~ m/(.*)\s+([\S+,]+)/) {
1072 $type = $1;
1073 $names = $2;
7c0d7e87 1074 } else {
84ce5b98
MCC
1075 $newmember .= "$arg; ";
1076 next;
1077 }
1078 foreach my $name (split /,/, $names) {
1079 $name =~ s/^\s*\**(\S+)\s*/$1/;
1080 next if (($name =~ m/^\s*$/));
1081 if ($id =~ m/^\s*$/) {
1082 # anonymous struct/union
1083 $newmember .= "$type $name; ";
1084 } else {
1085 $newmember .= "$type $id.$name; ";
1086 }
7c0d7e87 1087 }
8ad72163
MCC
1088 }
1089 }
84ce5b98
MCC
1090 }
1091 $members =~ s/(struct|union)([^\{\};]+)\{([^\{\}]*)}([^\{\}\;]*)\;/$newmember/;
1092 }
8ad72163
MCC
1093
1094 # Ignore other nested elements, like enums
1095 $members =~ s/({[^\{\}]*})//g;
8ad72163 1096
151c468b 1097 create_parameterlist($members, ';', $file, $declaration_name);
1081de2d 1098 check_sections($file, $declaration_name, $decl_type, $sectcheck, $struct_actual);
1da177e4 1099
8ad72163
MCC
1100 # Adjust declaration for better display
1101 $declaration =~ s/([{;])/$1\n/g;
1102 $declaration =~ s/}\s+;/};/g;
1103 # Better handle inlined enums
1104 do {} while ($declaration =~ s/(enum\s+{[^}]+),([^\n])/$1,\n$2/);
1105
1106 my @def_args = split /\n/, $declaration;
1107 my $level = 1;
1108 $declaration = "";
1109 foreach my $clause (@def_args) {
1110 $clause =~ s/^\s+//;
1111 $clause =~ s/\s+$//;
1112 $clause =~ s/\s+/ /;
1113 next if (!$clause);
1114 $level-- if ($clause =~ m/(})/ && $level > 1);
1115 if (!($clause =~ m/^\s*#/)) {
1116 $declaration .= "\t" x $level;
1117 }
1118 $declaration .= "\t" . $clause . "\n";
1119 $level++ if ($clause =~ m/({)/ && !($clause =~m/}/));
1120 }
1da177e4
LT
1121 output_declaration($declaration_name,
1122 'struct',
1123 {'struct' => $declaration_name,
1124 'module' => $modulename,
8ad72163 1125 'definition' => $declaration,
1da177e4
LT
1126 'parameterlist' => \@parameterlist,
1127 'parameterdescs' => \%parameterdescs,
1128 'parametertypes' => \%parametertypes,
1129 'sectionlist' => \@sectionlist,
1130 'sections' => \%sections,
1131 'purpose' => $declaration_purpose,
1132 'type' => $decl_type
1133 });
1134 }
1135 else {
d40e1e65 1136 print STDERR "${file}:$.: error: Cannot parse struct or union!\n";
1da177e4
LT
1137 ++$errors;
1138 }
1139}
1140
85afe608
MCC
1141
1142sub show_warnings($$) {
1143 my $functype = shift;
1144 my $name = shift;
1145
1146 return 1 if ($output_selection == OUTPUT_ALL);
1147
1148 if ($output_selection == OUTPUT_EXPORTED) {
1149 if (defined($function_table{$name})) {
1150 return 1;
1151 } else {
1152 return 0;
1153 }
1154 }
1155 if ($output_selection == OUTPUT_INTERNAL) {
1156 if (!($functype eq "function" && defined($function_table{$name}))) {
1157 return 1;
1158 } else {
1159 return 0;
1160 }
1161 }
1162 if ($output_selection == OUTPUT_INCLUDE) {
1163 if (defined($function_table{$name})) {
1164 return 1;
1165 } else {
1166 return 0;
1167 }
1168 }
1169 if ($output_selection == OUTPUT_EXCLUDE) {
1170 if (!defined($function_table{$name})) {
1171 return 1;
1172 } else {
1173 return 0;
1174 }
1175 }
1176 die("Please add the new output type at show_warnings()");
1177}
1178
1da177e4
LT
1179sub dump_enum($$) {
1180 my $x = shift;
1181 my $file = shift;
1182
aeec46b9 1183 $x =~ s@/\*.*?\*/@@gos; # strip comments.
4468e21e
CN
1184 # strip #define macros inside enums
1185 $x =~ s@#\s*((define|ifdef)\s+|endif)[^;]*;@@gos;
b6d676db 1186
1da177e4 1187 if ($x =~ /enum\s+(\w+)\s*{(.*)}/) {
3c308798
RD
1188 $declaration_name = $1;
1189 my $members = $2;
5cb5c31c
JB
1190 my %_members;
1191
463a0fdc 1192 $members =~ s/\s+$//;
1da177e4
LT
1193
1194 foreach my $arg (split ',', $members) {
1195 $arg =~ s/^\s*(\w+).*/$1/;
1196 push @parameterlist, $arg;
1197 if (!$parameterdescs{$arg}) {
3c308798 1198 $parameterdescs{$arg} = $undescribed;
85afe608 1199 if (show_warnings("enum", $declaration_name)) {
2defb272
MCC
1200 print STDERR "${file}:$.: warning: Enum value '$arg' not described in enum '$declaration_name'\n";
1201 }
1da177e4 1202 }
5cb5c31c 1203 $_members{$arg} = 1;
1da177e4 1204 }
3c3b809e 1205
5cb5c31c
JB
1206 while (my ($k, $v) = each %parameterdescs) {
1207 if (!exists($_members{$k})) {
85afe608 1208 if (show_warnings("enum", $declaration_name)) {
2defb272
MCC
1209 print STDERR "${file}:$.: warning: Excess enum value '$k' description in '$declaration_name'\n";
1210 }
5cb5c31c
JB
1211 }
1212 }
1213
1da177e4
LT
1214 output_declaration($declaration_name,
1215 'enum',
1216 {'enum' => $declaration_name,
1217 'module' => $modulename,
1218 'parameterlist' => \@parameterlist,
1219 'parameterdescs' => \%parameterdescs,
1220 'sectionlist' => \@sectionlist,
1221 'sections' => \%sections,
1222 'purpose' => $declaration_purpose
1223 });
1224 }
1225 else {
d40e1e65 1226 print STDERR "${file}:$.: error: Cannot parse enum!\n";
1da177e4
LT
1227 ++$errors;
1228 }
1229}
1230
1231sub dump_typedef($$) {
1232 my $x = shift;
1233 my $file = shift;
1234
aeec46b9 1235 $x =~ s@/\*.*?\*/@@gos; # strip comments.
1da177e4 1236
83766452 1237 # Parse function prototypes
d37c43ce
MCC
1238 if ($x =~ /typedef\s+(\w+)\s*\(\*\s*(\w\S+)\s*\)\s*\((.*)\);/ ||
1239 $x =~ /typedef\s+(\w+)\s*(\w\S+)\s*\s*\((.*)\);/) {
1240
83766452
MCC
1241 # Function typedefs
1242 $return_type = $1;
1243 $declaration_name = $2;
1244 my $args = $3;
1245
151c468b 1246 create_parameterlist($args, ',', $file, $declaration_name);
1da177e4
LT
1247
1248 output_declaration($declaration_name,
83766452
MCC
1249 'function',
1250 {'function' => $declaration_name,
82801d06 1251 'typedef' => 1,
1da177e4 1252 'module' => $modulename,
83766452
MCC
1253 'functiontype' => $return_type,
1254 'parameterlist' => \@parameterlist,
1255 'parameterdescs' => \%parameterdescs,
1256 'parametertypes' => \%parametertypes,
1da177e4
LT
1257 'sectionlist' => \@sectionlist,
1258 'sections' => \%sections,
1259 'purpose' => $declaration_purpose
1260 });
83766452
MCC
1261 return;
1262 }
1263
1264 while (($x =~ /\(*.\)\s*;$/) || ($x =~ /\[*.\]\s*;$/)) {
1265 $x =~ s/\(*.\)\s*;$/;/;
1266 $x =~ s/\[*.\]\s*;$/;/;
1da177e4 1267 }
83766452
MCC
1268
1269 if ($x =~ /typedef.*\s+(\w+)\s*;/) {
3a80a766
MCC
1270 $declaration_name = $1;
1271
1272 output_declaration($declaration_name,
1273 'typedef',
1274 {'typedef' => $declaration_name,
1275 'module' => $modulename,
1276 'sectionlist' => \@sectionlist,
1277 'sections' => \%sections,
1278 'purpose' => $declaration_purpose
1279 });
1280 }
1da177e4 1281 else {
d40e1e65 1282 print STDERR "${file}:$.: error: Cannot parse typedef!\n";
1da177e4
LT
1283 ++$errors;
1284 }
1285}
1286
a1d94aa5
RD
1287sub save_struct_actual($) {
1288 my $actual = shift;
1289
1290 # strip all spaces from the actual param so that it looks like one string item
1291 $actual =~ s/\s*//g;
1292 $struct_actual = $struct_actual . $actual . " ";
1293}
1294
151c468b 1295sub create_parameterlist($$$$) {
1da177e4
LT
1296 my $args = shift;
1297 my $splitter = shift;
1298 my $file = shift;
151c468b 1299 my $declaration_name = shift;
1da177e4
LT
1300 my $type;
1301 my $param;
1302
a6d3fe77 1303 # temporarily replace commas inside function pointer definition
1da177e4 1304 while ($args =~ /(\([^\),]+),/) {
3c308798 1305 $args =~ s/(\([^\),]+),/$1#/g;
1da177e4 1306 }
3c3b809e 1307
1da177e4
LT
1308 foreach my $arg (split($splitter, $args)) {
1309 # strip comments
1310 $arg =~ s/\/\*.*\*\///;
3c308798
RD
1311 # strip leading/trailing spaces
1312 $arg =~ s/^\s*//;
1da177e4
LT
1313 $arg =~ s/\s*$//;
1314 $arg =~ s/\s+/ /;
1315
1316 if ($arg =~ /^#/) {
1317 # Treat preprocessor directive as a typeless variable just to fill
1318 # corresponding data structures "correctly". Catch it later in
1319 # output_* subs.
1320 push_parameter($arg, "", $file);
00d62961 1321 } elsif ($arg =~ m/\(.+\)\s*\(/) {
1da177e4
LT
1322 # pointer-to-function
1323 $arg =~ tr/#/,/;
7c0d7e87 1324 $arg =~ m/[^\(]+\(\*?\s*([\w\.]*)\s*\)/;
1da177e4
LT
1325 $param = $1;
1326 $type = $arg;
00d62961 1327 $type =~ s/([^\(]+\(\*?)\s*$param/$1/;
a1d94aa5 1328 save_struct_actual($param);
151c468b 1329 push_parameter($param, $type, $file, $declaration_name);
aeec46b9 1330 } elsif ($arg) {
1da177e4
LT
1331 $arg =~ s/\s*:\s*/:/g;
1332 $arg =~ s/\s*\[/\[/g;
1333
1334 my @args = split('\s*,\s*', $arg);
1335 if ($args[0] =~ m/\*/) {
1336 $args[0] =~ s/(\*+)\s*/ $1/;
1337 }
884f2810
BP
1338
1339 my @first_arg;
1340 if ($args[0] =~ /^(.*\s+)(.*?\[.*\].*)$/) {
1341 shift @args;
1342 push(@first_arg, split('\s+', $1));
1343 push(@first_arg, $2);
1344 } else {
1345 @first_arg = split('\s+', shift @args);
1346 }
1347
1da177e4
LT
1348 unshift(@args, pop @first_arg);
1349 $type = join " ", @first_arg;
1350
1351 foreach $param (@args) {
1352 if ($param =~ m/^(\*+)\s*(.*)/) {
a1d94aa5 1353 save_struct_actual($2);
151c468b 1354 push_parameter($2, "$type $1", $file, $declaration_name);
1da177e4
LT
1355 }
1356 elsif ($param =~ m/(.*?):(\d+)/) {
7b97887e 1357 if ($type ne "") { # skip unnamed bit-fields
a1d94aa5 1358 save_struct_actual($1);
151c468b 1359 push_parameter($1, "$type:$2", $file, $declaration_name)
7b97887e 1360 }
1da177e4
LT
1361 }
1362 else {
a1d94aa5 1363 save_struct_actual($param);
151c468b 1364 push_parameter($param, $type, $file, $declaration_name);
1da177e4
LT
1365 }
1366 }
1367 }
1368 }
1369}
1370
151c468b 1371sub push_parameter($$$$) {
1da177e4
LT
1372 my $param = shift;
1373 my $type = shift;
1374 my $file = shift;
151c468b 1375 my $declaration_name = shift;
1da177e4 1376
5f8c7c98
RD
1377 if (($anon_struct_union == 1) && ($type eq "") &&
1378 ($param eq "}")) {
1379 return; # ignore the ending }; from anon. struct/union
1380 }
1381
1382 $anon_struct_union = 0;
f9b5c530 1383 $param =~ s/[\[\)].*//;
1da177e4 1384
a6d3fe77 1385 if ($type eq "" && $param =~ /\.\.\.$/)
1da177e4 1386 {
c950a173
SF
1387 if (!$param =~ /\w\.\.\.$/) {
1388 # handles unnamed variable parameters
1389 $param = "...";
1390 }
ced69090
RD
1391 if (!defined $parameterdescs{$param} || $parameterdescs{$param} eq "") {
1392 $parameterdescs{$param} = "variable arguments";
1393 }
1da177e4
LT
1394 }
1395 elsif ($type eq "" && ($param eq "" or $param eq "void"))
1396 {
1da177e4
LT
1397 $param="void";
1398 $parameterdescs{void} = "no arguments";
1399 }
134fe01b
RD
1400 elsif ($type eq "" && ($param eq "struct" or $param eq "union"))
1401 # handle unnamed (anonymous) union or struct:
1402 {
1403 $type = $param;
5f8c7c98 1404 $param = "{unnamed_" . $param . "}";
134fe01b 1405 $parameterdescs{$param} = "anonymous\n";
5f8c7c98 1406 $anon_struct_union = 1;
134fe01b
RD
1407 }
1408
a6d3fe77 1409 # warn if parameter has no description
134fe01b
RD
1410 # (but ignore ones starting with # as these are not parameters
1411 # but inline preprocessor statements);
151c468b 1412 # Note: It will also ignore void params and unnamed structs/unions
f9b5c530 1413 if (!defined $parameterdescs{$param} && $param !~ /^#/) {
151c468b 1414 $parameterdescs{$param} = $undescribed;
a6d3fe77 1415
85afe608 1416 if (show_warnings($type, $declaration_name)) {
2defb272
MCC
1417 print STDERR
1418 "${file}:$.: warning: Function parameter or member '$param' not described in '$declaration_name'\n";
1419 ++$warnings;
1420 }
3c308798 1421 }
1da177e4 1422
25985edc 1423 # strip spaces from $param so that it is one continuous string
e34e7dbb
RD
1424 # on @parameterlist;
1425 # this fixes a problem where check_sections() cannot find
1426 # a parameter like "addr[6 + 2]" because it actually appears
1427 # as "addr[6", "+", "2]" on the parameter list;
1428 # but it's better to maintain the param string unchanged for output,
1429 # so just weaken the string compare in check_sections() to ignore
1430 # "[blah" in a parameter string;
1431 ###$param =~ s/\s*//g;
1da177e4 1432 push @parameterlist, $param;
02a4f4fe 1433 $type =~ s/\s\s+/ /g;
1da177e4
LT
1434 $parametertypes{$param} = $type;
1435}
1436
1081de2d
MCC
1437sub check_sections($$$$$) {
1438 my ($file, $decl_name, $decl_type, $sectcheck, $prmscheck) = @_;
a1d94aa5
RD
1439 my @sects = split ' ', $sectcheck;
1440 my @prms = split ' ', $prmscheck;
1441 my $err;
1442 my ($px, $sx);
1443 my $prm_clean; # strip trailing "[array size]" and/or beginning "*"
1444
1445 foreach $sx (0 .. $#sects) {
1446 $err = 1;
1447 foreach $px (0 .. $#prms) {
1448 $prm_clean = $prms[$px];
1449 $prm_clean =~ s/\[.*\]//;
1f3a6688 1450 $prm_clean =~ s/__attribute__\s*\(\([a-z,_\*\s\(\)]*\)\)//i;
e34e7dbb
RD
1451 # ignore array size in a parameter string;
1452 # however, the original param string may contain
1453 # spaces, e.g.: addr[6 + 2]
1454 # and this appears in @prms as "addr[6" since the
1455 # parameter list is split at spaces;
1456 # hence just ignore "[..." for the sections check;
1457 $prm_clean =~ s/\[.*//;
1458
a1d94aa5
RD
1459 ##$prm_clean =~ s/^\**//;
1460 if ($prm_clean eq $sects[$sx]) {
1461 $err = 0;
1462 last;
1463 }
1464 }
1465 if ($err) {
1466 if ($decl_type eq "function") {
d40e1e65 1467 print STDERR "${file}:$.: warning: " .
a1d94aa5
RD
1468 "Excess function parameter " .
1469 "'$sects[$sx]' " .
1470 "description in '$decl_name'\n";
1471 ++$warnings;
a1d94aa5
RD
1472 }
1473 }
1474 }
1475}
1476
4092bac7
YB
1477##
1478# Checks the section describing the return value of a function.
1479sub check_return_section {
1480 my $file = shift;
1481 my $declaration_name = shift;
1482 my $return_type = shift;
1483
1484 # Ignore an empty return type (It's a macro)
1485 # Ignore functions with a "void" return type. (But don't ignore "void *")
1486 if (($return_type eq "") || ($return_type =~ /void\s*\w*\s*$/)) {
1487 return;
1488 }
1489
1490 if (!defined($sections{$section_return}) ||
1491 $sections{$section_return} eq "") {
d40e1e65 1492 print STDERR "${file}:$.: warning: " .
4092bac7
YB
1493 "No description found for return value of " .
1494 "'$declaration_name'\n";
1495 ++$warnings;
1496 }
1497}
1498
1da177e4
LT
1499##
1500# takes a function prototype and the name of the current file being
1501# processed and spits out all the details stored in the global
1502# arrays/hashes.
1503sub dump_function($$) {
1504 my $prototype = shift;
1505 my $file = shift;
cbb4d3e6 1506 my $noret = 0;
1da177e4
LT
1507
1508 $prototype =~ s/^static +//;
1509 $prototype =~ s/^extern +//;
4dc3b16b 1510 $prototype =~ s/^asmlinkage +//;
1da177e4
LT
1511 $prototype =~ s/^inline +//;
1512 $prototype =~ s/^__inline__ +//;
32e79401
RD
1513 $prototype =~ s/^__inline +//;
1514 $prototype =~ s/^__always_inline +//;
1515 $prototype =~ s/^noinline +//;
74fc5c65 1516 $prototype =~ s/__init +//;
20072205 1517 $prototype =~ s/__init_or_module +//;
270a0096 1518 $prototype =~ s/__meminit +//;
70c95b00 1519 $prototype =~ s/__must_check +//;
0df7c0e3 1520 $prototype =~ s/__weak +//;
cbb4d3e6 1521 my $define = $prototype =~ s/^#\s*define\s+//; #ak added
b1aaa546
PB
1522 $prototype =~ s/__attribute__\s*\(\(
1523 (?:
1524 [\w\s]++ # attribute name
1525 (?:\([^)]*+\))? # attribute arguments
1526 \s*+,? # optional comma at the end
1527 )+
1528 \)\)\s+//x;
1da177e4
LT
1529
1530 # Yes, this truly is vile. We are looking for:
1531 # 1. Return type (may be nothing if we're looking at a macro)
1532 # 2. Function name
1533 # 3. Function parameters.
1534 #
1535 # All the while we have to watch out for function pointer parameters
1536 # (which IIRC is what the two sections are for), C types (these
1537 # regexps don't even start to express all the possibilities), and
1538 # so on.
1539 #
1540 # If you mess with these regexps, it's a good idea to check that
1541 # the following functions' documentation still comes out right:
1542 # - parport_register_device (function pointer parameters)
1543 # - atomic_set (macro)
9598f91f 1544 # - pci_match_device, __copy_to_user (long return type)
1da177e4 1545
cbb4d3e6
HG
1546 if ($define && $prototype =~ m/^()([a-zA-Z0-9_~:]+)\s+/) {
1547 # This is an object-like macro, it has no return type and no parameter
1548 # list.
1549 # Function-like macros are not allowed to have spaces between
1550 # declaration_name and opening parenthesis (notice the \s+).
1551 $return_type = $1;
1552 $declaration_name = $2;
1553 $noret = 1;
1554 } elsif ($prototype =~ m/^()([a-zA-Z0-9_~:]+)\s*\(([^\(]*)\)/ ||
1da177e4 1555 $prototype =~ m/^(\w+)\s+([a-zA-Z0-9_~:]+)\s*\(([^\(]*)\)/ ||
5a0bc578 1556 $prototype =~ m/^(\w+\s*\*+)\s*([a-zA-Z0-9_~:]+)\s*\(([^\(]*)\)/ ||
1da177e4 1557 $prototype =~ m/^(\w+\s+\w+)\s+([a-zA-Z0-9_~:]+)\s*\(([^\(]*)\)/ ||
94b3e03c 1558 $prototype =~ m/^(\w+\s+\w+\s*\*+)\s*([a-zA-Z0-9_~:]+)\s*\(([^\(]*)\)/ ||
1da177e4 1559 $prototype =~ m/^(\w+\s+\w+\s+\w+)\s+([a-zA-Z0-9_~:]+)\s*\(([^\(]*)\)/ ||
5a0bc578 1560 $prototype =~ m/^(\w+\s+\w+\s+\w+\s*\*+)\s*([a-zA-Z0-9_~:]+)\s*\(([^\(]*)\)/ ||
1da177e4
LT
1561 $prototype =~ m/^()([a-zA-Z0-9_~:]+)\s*\(([^\{]*)\)/ ||
1562 $prototype =~ m/^(\w+)\s+([a-zA-Z0-9_~:]+)\s*\(([^\{]*)\)/ ||
5a0bc578 1563 $prototype =~ m/^(\w+\s*\*+)\s*([a-zA-Z0-9_~:]+)\s*\(([^\{]*)\)/ ||
1da177e4 1564 $prototype =~ m/^(\w+\s+\w+)\s+([a-zA-Z0-9_~:]+)\s*\(([^\{]*)\)/ ||
5a0bc578 1565 $prototype =~ m/^(\w+\s+\w+\s*\*+)\s*([a-zA-Z0-9_~:]+)\s*\(([^\{]*)\)/ ||
1da177e4 1566 $prototype =~ m/^(\w+\s+\w+\s+\w+)\s+([a-zA-Z0-9_~:]+)\s*\(([^\{]*)\)/ ||
5a0bc578 1567 $prototype =~ m/^(\w+\s+\w+\s+\w+\s*\*+)\s*([a-zA-Z0-9_~:]+)\s*\(([^\{]*)\)/ ||
9598f91f 1568 $prototype =~ m/^(\w+\s+\w+\s+\w+\s+\w+)\s+([a-zA-Z0-9_~:]+)\s*\(([^\{]*)\)/ ||
5a0bc578
MW
1569 $prototype =~ m/^(\w+\s+\w+\s+\w+\s+\w+\s*\*+)\s*([a-zA-Z0-9_~:]+)\s*\(([^\{]*)\)/ ||
1570 $prototype =~ m/^(\w+\s+\w+\s*\*+\s*\w+\s*\*+\s*)\s*([a-zA-Z0-9_~:]+)\s*\(([^\{]*)\)/) {
1da177e4
LT
1571 $return_type = $1;
1572 $declaration_name = $2;
1573 my $args = $3;
1574
151c468b 1575 create_parameterlist($args, ',', $file, $declaration_name);
1da177e4 1576 } else {
d40e1e65 1577 print STDERR "${file}:$.: warning: cannot understand function prototype: '$prototype'\n";
1da177e4
LT
1578 return;
1579 }
1580
a1d94aa5 1581 my $prms = join " ", @parameterlist;
1081de2d 1582 check_sections($file, $declaration_name, "function", $sectcheck, $prms);
a1d94aa5 1583
4092bac7
YB
1584 # This check emits a lot of warnings at the moment, because many
1585 # functions don't have a 'Return' doc section. So until the number
1586 # of warnings goes sufficiently down, the check is only performed in
1587 # verbose mode.
1588 # TODO: always perform the check.
cbb4d3e6 1589 if ($verbose && !$noret) {
4092bac7
YB
1590 check_return_section($file, $declaration_name, $return_type);
1591 }
1592
3c3b809e 1593 output_declaration($declaration_name,
1da177e4
LT
1594 'function',
1595 {'function' => $declaration_name,
1596 'module' => $modulename,
1597 'functiontype' => $return_type,
1598 'parameterlist' => \@parameterlist,
1599 'parameterdescs' => \%parameterdescs,
1600 'parametertypes' => \%parametertypes,
1601 'sectionlist' => \@sectionlist,
1602 'sections' => \%sections,
1603 'purpose' => $declaration_purpose
1604 });
1605}
1606
1da177e4
LT
1607sub reset_state {
1608 $function = "";
1da177e4
LT
1609 %parameterdescs = ();
1610 %parametertypes = ();
1611 @parameterlist = ();
1612 %sections = ();
1613 @sectionlist = ();
a1d94aa5
RD
1614 $sectcheck = "";
1615 $struct_actual = "";
1da177e4 1616 $prototype = "";
3c3b809e 1617
48af606a
JN
1618 $state = STATE_NORMAL;
1619 $inline_doc_state = STATE_INLINE_NA;
1da177e4
LT
1620}
1621
56afb0f8
JB
1622sub tracepoint_munge($) {
1623 my $file = shift;
1624 my $tracepointname = 0;
1625 my $tracepointargs = 0;
1626
3a9089fd 1627 if ($prototype =~ m/TRACE_EVENT\((.*?),/) {
56afb0f8
JB
1628 $tracepointname = $1;
1629 }
3a9089fd
JB
1630 if ($prototype =~ m/DEFINE_SINGLE_EVENT\((.*?),/) {
1631 $tracepointname = $1;
1632 }
1633 if ($prototype =~ m/DEFINE_EVENT\((.*?),(.*?),/) {
1634 $tracepointname = $2;
1635 }
1636 $tracepointname =~ s/^\s+//; #strip leading whitespace
1637 if ($prototype =~ m/TP_PROTO\((.*?)\)/) {
56afb0f8
JB
1638 $tracepointargs = $1;
1639 }
1640 if (($tracepointname eq 0) || ($tracepointargs eq 0)) {
d40e1e65 1641 print STDERR "${file}:$.: warning: Unrecognized tracepoint format: \n".
56afb0f8
JB
1642 "$prototype\n";
1643 } else {
1644 $prototype = "static inline void trace_$tracepointname($tracepointargs)";
1645 }
1646}
1647
b4870bc5
RD
1648sub syscall_munge() {
1649 my $void = 0;
1650
7c9aa015 1651 $prototype =~ s@[\r\n]+@ @gos; # strip newlines/CR's
b4870bc5
RD
1652## if ($prototype =~ m/SYSCALL_DEFINE0\s*\(\s*(a-zA-Z0-9_)*\s*\)/) {
1653 if ($prototype =~ m/SYSCALL_DEFINE0/) {
1654 $void = 1;
1655## $prototype = "long sys_$1(void)";
1656 }
1657
1658 $prototype =~ s/SYSCALL_DEFINE.*\(/long sys_/; # fix return type & func name
1659 if ($prototype =~ m/long (sys_.*?),/) {
1660 $prototype =~ s/,/\(/;
1661 } elsif ($void) {
1662 $prototype =~ s/\)/\(void\)/;
1663 }
1664
1665 # now delete all of the odd-number commas in $prototype
1666 # so that arg types & arg names don't have a comma between them
1667 my $count = 0;
1668 my $len = length($prototype);
1669 if ($void) {
1670 $len = 0; # skip the for-loop
1671 }
1672 for (my $ix = 0; $ix < $len; $ix++) {
1673 if (substr($prototype, $ix, 1) eq ',') {
1674 $count++;
1675 if ($count % 2 == 1) {
1676 substr($prototype, $ix, 1) = ' ';
1677 }
1678 }
1679 }
1680}
1681
b7afa92b 1682sub process_proto_function($$) {
1da177e4
LT
1683 my $x = shift;
1684 my $file = shift;
1685
51f5a0c8
RD
1686 $x =~ s@\/\/.*$@@gos; # strip C99-style comments to end of line
1687
890c78c2 1688 if ($x =~ m#\s*/\*\s+MACDOC\s*#io || ($x =~ /^#/ && $x !~ /^#\s*define/)) {
1da177e4
LT
1689 # do nothing
1690 }
1691 elsif ($x =~ /([^\{]*)/) {
3c308798 1692 $prototype .= $1;
1da177e4 1693 }
b4870bc5 1694
890c78c2 1695 if (($x =~ /\{/) || ($x =~ /\#\s*define/) || ($x =~ /;/)) {
3c308798 1696 $prototype =~ s@/\*.*?\*/@@gos; # strip comments.
1da177e4
LT
1697 $prototype =~ s@[\r\n]+@ @gos; # strip newlines/cr's.
1698 $prototype =~ s@^\s+@@gos; # strip leading spaces
b4870bc5
RD
1699 if ($prototype =~ /SYSCALL_DEFINE/) {
1700 syscall_munge();
1701 }
3a9089fd
JB
1702 if ($prototype =~ /TRACE_EVENT/ || $prototype =~ /DEFINE_EVENT/ ||
1703 $prototype =~ /DEFINE_SINGLE_EVENT/)
1704 {
56afb0f8
JB
1705 tracepoint_munge($file);
1706 }
b4870bc5 1707 dump_function($prototype, $file);
1da177e4
LT
1708 reset_state();
1709 }
1710}
1711
b7afa92b 1712sub process_proto_type($$) {
1da177e4
LT
1713 my $x = shift;
1714 my $file = shift;
1715
1da177e4
LT
1716 $x =~ s@[\r\n]+@ @gos; # strip newlines/cr's.
1717 $x =~ s@^\s+@@gos; # strip leading spaces
1718 $x =~ s@\s+$@@gos; # strip trailing spaces
51f5a0c8
RD
1719 $x =~ s@\/\/.*$@@gos; # strip C99-style comments to end of line
1720
1da177e4
LT
1721 if ($x =~ /^#/) {
1722 # To distinguish preprocessor directive from regular declaration later.
1723 $x .= ";";
1724 }
1725
1726 while (1) {
3c308798 1727 if ( $x =~ /([^{};]*)([{};])(.*)/ ) {
463a0fdc
MH
1728 if( length $prototype ) {
1729 $prototype .= " "
1730 }
1da177e4
LT
1731 $prototype .= $1 . $2;
1732 ($2 eq '{') && $brcount++;
1733 ($2 eq '}') && $brcount--;
1734 if (($2 eq ';') && ($brcount == 0)) {
b9d97328 1735 dump_declaration($prototype, $file);
1da177e4 1736 reset_state();
3c308798 1737 last;
1da177e4
LT
1738 }
1739 $x = $3;
3c308798 1740 } else {
1da177e4
LT
1741 $prototype .= $x;
1742 last;
1743 }
1744 }
1745}
1746
6b5b55f6 1747
1ad560e4 1748sub map_filename($) {
2283a117 1749 my $file;
68f86662 1750 my ($orig_file) = @_;
1da177e4 1751
2283a117 1752 if (defined($ENV{'SRCTREE'})) {
68f86662 1753 $file = "$ENV{'SRCTREE'}" . "/" . $orig_file;
1ad560e4 1754 } else {
68f86662 1755 $file = $orig_file;
2283a117 1756 }
1ad560e4 1757
1da177e4
LT
1758 if (defined($source_map{$file})) {
1759 $file = $source_map{$file};
1760 }
1761
1ad560e4
JN
1762 return $file;
1763}
1764
88c2b57d
JN
1765sub process_export_file($) {
1766 my ($orig_file) = @_;
1767 my $file = map_filename($orig_file);
1768
1769 if (!open(IN,"<$file")) {
1770 print STDERR "Error: Cannot open file $file\n";
1771 ++$errors;
1772 return;
1773 }
1774
1775 while (<IN>) {
1776 if (/$export_symbol/) {
1777 $function_table{$2} = 1;
1778 }
1779 }
1780
1781 close(IN);
1782}
1783
07048d13
JC
1784#
1785# Parsers for the various processing states.
1786#
1787# STATE_NORMAL: looking for the /** to begin everything.
1788#
1789sub process_normal() {
1790 if (/$doc_start/o) {
1791 $state = STATE_NAME; # next line is always the function name
1792 $in_doc_sect = 0;
1793 $declaration_start_line = $. + 1;
1794 }
1795}
1796
3cac2bc4
JC
1797#
1798# STATE_NAME: Looking for the "name - description" line
1799#
1800sub process_name($$) {
1801 my $file = shift;
1802 my $identifier;
1803 my $descr;
1804
1805 if (/$doc_block/o) {
1806 $state = STATE_DOCBLOCK;
1807 $contents = "";
1808 $new_start_line = $. + 1;
1809
1810 if ( $1 eq "" ) {
1811 $section = $section_intro;
1812 } else {
1813 $section = $1;
1814 }
1815 }
1816 elsif (/$doc_decl/o) {
1817 $identifier = $1;
1818 if (/\s*([\w\s]+?)\s*-/) {
1819 $identifier = $1;
1820 }
07048d13 1821
3cac2bc4
JC
1822 $state = STATE_BODY;
1823 # if there's no @param blocks need to set up default section
1824 # here
1825 $contents = "";
1826 $section = $section_default;
1827 $new_start_line = $. + 1;
1828 if (/-(.*)/) {
1829 # strip leading/trailing/multiple spaces
1830 $descr= $1;
1831 $descr =~ s/^\s*//;
1832 $descr =~ s/\s*$//;
1833 $descr =~ s/\s+/ /g;
1834 $declaration_purpose = $descr;
1835 $state = STATE_BODY_MAYBE;
1836 } else {
1837 $declaration_purpose = "";
1838 }
1839
1840 if (($declaration_purpose eq "") && $verbose) {
1841 print STDERR "${file}:$.: warning: missing initial short description on line:\n";
1842 print STDERR $_;
1843 ++$warnings;
1844 }
1845
1846 if ($identifier =~ m/^struct/) {
1847 $decl_type = 'struct';
1848 } elsif ($identifier =~ m/^union/) {
1849 $decl_type = 'union';
1850 } elsif ($identifier =~ m/^enum/) {
1851 $decl_type = 'enum';
1852 } elsif ($identifier =~ m/^typedef/) {
1853 $decl_type = 'typedef';
1854 } else {
1855 $decl_type = 'function';
1856 }
1857
1858 if ($verbose) {
1859 print STDERR "${file}:$.: info: Scanning doc for $identifier\n";
1860 }
1861 } else {
1862 print STDERR "${file}:$.: warning: Cannot understand $_ on line $.",
1863 " - I thought it was a doc line\n";
1864 ++$warnings;
1865 $state = STATE_NORMAL;
1866 }
1867}
07048d13 1868
d742f24d
JC
1869
1870#
1871# STATE_BODY and STATE_BODY_MAYBE: the bulk of a kerneldoc comment.
1872#
1873sub process_body($$) {
1874 my $file = shift;
1875
1876 if (/$doc_sect/i) { # case insensitive for supported section names
1877 $newsection = $1;
1878 $newcontents = $2;
1879
1880 # map the supported section names to the canonical names
1881 if ($newsection =~ m/^description$/i) {
1882 $newsection = $section_default;
1883 } elsif ($newsection =~ m/^context$/i) {
1884 $newsection = $section_context;
1885 } elsif ($newsection =~ m/^returns?$/i) {
1886 $newsection = $section_return;
1887 } elsif ($newsection =~ m/^\@return$/) {
1888 # special: @return is a section, not a param description
1889 $newsection = $section_return;
1890 }
1891
1892 if (($contents ne "") && ($contents ne "\n")) {
1893 if (!$in_doc_sect && $verbose) {
1894 print STDERR "${file}:$.: warning: contents before sections\n";
1895 ++$warnings;
1896 }
1897 dump_section($file, $section, $contents);
1898 $section = $section_default;
1899 }
1900
1901 $in_doc_sect = 1;
1902 $state = STATE_BODY;
1903 $contents = $newcontents;
1904 $new_start_line = $.;
1905 while (substr($contents, 0, 1) eq " ") {
1906 $contents = substr($contents, 1);
1907 }
1908 if ($contents ne "") {
1909 $contents .= "\n";
1910 }
1911 $section = $newsection;
1912 $leading_space = undef;
1913 } elsif (/$doc_end/) {
1914 if (($contents ne "") && ($contents ne "\n")) {
1915 dump_section($file, $section, $contents);
1916 $section = $section_default;
1917 $contents = "";
1918 }
1919 # look for doc_com + <text> + doc_end:
1920 if ($_ =~ m'\s*\*\s*[a-zA-Z_0-9:\.]+\*/') {
1921 print STDERR "${file}:$.: warning: suspicious ending line: $_";
1922 ++$warnings;
1923 }
1924
1925 $prototype = "";
1926 $state = STATE_PROTO;
1927 $brcount = 0;
1928 } elsif (/$doc_content/) {
1929 # miguel-style comment kludge, look for blank lines after
1930 # @parameter line to signify start of description
1931 if ($1 eq "") {
1932 if ($section =~ m/^@/ || $section eq $section_context) {
1933 dump_section($file, $section, $contents);
1934 $section = $section_default;
1935 $contents = "";
1936 $new_start_line = $.;
1937 } else {
1938 $contents .= "\n";
1939 }
1940 $state = STATE_BODY;
1941 } elsif ($state == STATE_BODY_MAYBE) {
1942 # Continued declaration purpose
1943 chomp($declaration_purpose);
1944 $declaration_purpose .= " " . $1;
1945 $declaration_purpose =~ s/\s+/ /g;
1946 } else {
1947 my $cont = $1;
1948 if ($section =~ m/^@/ || $section eq $section_context) {
1949 if (!defined $leading_space) {
1950 if ($cont =~ m/^(\s+)/) {
1951 $leading_space = $1;
1952 } else {
1953 $leading_space = "";
1954 }
1955 }
1956 $cont =~ s/^$leading_space//;
1957 }
1958 $contents .= $cont . "\n";
1959 }
1960 } else {
1961 # i dont know - bad line? ignore.
1962 print STDERR "${file}:$.: warning: bad line: $_";
1963 ++$warnings;
1964 }
1965}
1966
1967
cc794812
JC
1968#
1969# STATE_PROTO: reading a function/whatever prototype.
1970#
1971sub process_proto($$) {
1972 my $file = shift;
1973
1974 if (/$doc_inline_oneline/) {
1975 $section = $1;
1976 $contents = $2;
1977 if ($contents ne "") {
1978 $contents .= "\n";
1979 dump_section($file, $section, $contents);
1980 $section = $section_default;
1981 $contents = "";
1982 }
1983 } elsif (/$doc_inline_start/) {
1984 $state = STATE_INLINE;
1985 $inline_doc_state = STATE_INLINE_NAME;
1986 } elsif ($decl_type eq 'function') {
1987 process_proto_function($_, $file);
1988 } else {
1989 process_proto_type($_, $file);
1990 }
1991}
1992
c17add56
JC
1993#
1994# STATE_DOCBLOCK: within a DOC: block.
1995#
1996sub process_docblock($$) {
1997 my $file = shift;
1998
1999 if (/$doc_end/) {
2000 dump_doc_section($file, $section, $contents);
2001 $section = $section_default;
2002 $contents = "";
2003 $function = "";
2004 %parameterdescs = ();
2005 %parametertypes = ();
2006 @parameterlist = ();
2007 %sections = ();
2008 @sectionlist = ();
2009 $prototype = "";
2010 $state = STATE_NORMAL;
2011 } elsif (/$doc_content/) {
2012 if ( $1 eq "" ) {
2013 $contents .= $blankline;
2014 } else {
2015 $contents .= $1 . "\n";
2016 }
2017 }
2018}
2019
2020#
2021# STATE_INLINE: docbook comments within a prototype.
2022#
2023sub process_inline($$) {
2024 my $file = shift;
2025
2026 # First line (state 1) needs to be a @parameter
2027 if ($inline_doc_state == STATE_INLINE_NAME && /$doc_inline_sect/o) {
2028 $section = $1;
2029 $contents = $2;
2030 $new_start_line = $.;
2031 if ($contents ne "") {
2032 while (substr($contents, 0, 1) eq " ") {
2033 $contents = substr($contents, 1);
2034 }
2035 $contents .= "\n";
2036 }
2037 $inline_doc_state = STATE_INLINE_TEXT;
2038 # Documentation block end */
2039 } elsif (/$doc_inline_end/) {
2040 if (($contents ne "") && ($contents ne "\n")) {
2041 dump_section($file, $section, $contents);
2042 $section = $section_default;
2043 $contents = "";
2044 }
2045 $state = STATE_PROTO;
2046 $inline_doc_state = STATE_INLINE_NA;
2047 # Regular text
2048 } elsif (/$doc_content/) {
2049 if ($inline_doc_state == STATE_INLINE_TEXT) {
2050 $contents .= $1 . "\n";
2051 # nuke leading blank lines
2052 if ($contents =~ /^\s*$/) {
2053 $contents = "";
2054 }
2055 } elsif ($inline_doc_state == STATE_INLINE_NAME) {
2056 $inline_doc_state = STATE_INLINE_ERROR;
2057 print STDERR "${file}:$.: warning: ";
2058 print STDERR "Incorrect use of kernel-doc format: $_";
2059 ++$warnings;
2060 }
2061 }
2062}
2063
cc794812 2064
1ad560e4
JN
2065sub process_file($) {
2066 my $file;
1ad560e4
JN
2067 my $initial_section_counter = $section_counter;
2068 my ($orig_file) = @_;
1ad560e4
JN
2069
2070 $file = map_filename($orig_file);
2071
1da177e4
LT
2072 if (!open(IN,"<$file")) {
2073 print STDERR "Error: Cannot open file $file\n";
2074 ++$errors;
2075 return;
2076 }
2077
a9e7314b
ID
2078 $. = 1;
2079
1da177e4
LT
2080 $section_counter = 0;
2081 while (<IN>) {
65478428
DS
2082 while (s/\\\s*$//) {
2083 $_ .= <IN>;
2084 }
7c9aa015
MCC
2085 # Replace tabs by spaces
2086 while ($_ =~ s/\t+/' ' x (length($&) * 8 - length($`) % 8)/e) {};
c17add56
JC
2087
2088 # Hand this line to the appropriate state handler
48af606a 2089 if ($state == STATE_NORMAL) {
07048d13 2090 process_normal();
3cac2bc4
JC
2091 } elsif ($state == STATE_NAME) {
2092 process_name($file, $_);
17b78717 2093 } elsif ($state == STATE_BODY || $state == STATE_BODY_MAYBE) {
d742f24d 2094 process_body($file, $_);
48af606a 2095 } elsif ($state == STATE_INLINE) { # scanning for inline parameters
c17add56 2096 process_inline($file, $_);
cc794812
JC
2097 } elsif ($state == STATE_PROTO) {
2098 process_proto($file, $_);
48af606a 2099 } elsif ($state == STATE_DOCBLOCK) {
c17add56 2100 process_docblock($file, $_);
3c308798 2101 }
1da177e4 2102 }
c17add56
JC
2103
2104 # Make sure we got something interesting.
1da177e4 2105 if ($initial_section_counter == $section_counter) {
3a025e1d
MW
2106 if ($output_mode ne "none") {
2107 print STDERR "${file}:1: warning: no structured comments found\n";
2108 }
b6c3f456 2109 if (($output_selection == OUTPUT_INCLUDE) && ($show_not_found == 1)) {
e946c43a
JB
2110 print STDERR " Was looking for '$_'.\n" for keys %function_table;
2111 }
1da177e4
LT
2112 }
2113}
8484baaa
RD
2114
2115
2116$kernelversion = get_kernel_version();
2117
2118# generate a sequence of code that will splice in highlighting information
2119# using the s// operator.
1ef06233 2120for (my $k = 0; $k < @highlights; $k++) {
4d732701
DCLP
2121 my $pattern = $highlights[$k][0];
2122 my $result = $highlights[$k][1];
2123# print STDERR "scanning pattern:$pattern, highlight:($result)\n";
2124 $dohighlight .= "\$contents =~ s:$pattern:$result:gs;\n";
8484baaa
RD
2125}
2126
2127# Read the file that maps relative names to absolute names for
2128# separate source and object directories and for shadow trees.
2129if (open(SOURCE_MAP, "<.tmp_filelist.txt")) {
2130 my ($relname, $absname);
2131 while(<SOURCE_MAP>) {
2132 chop();
2133 ($relname, $absname) = (split())[0..1];
2134 $relname =~ s:^/+::;
2135 $source_map{$relname} = $absname;
2136 }
2137 close(SOURCE_MAP);
2138}
2139
88c2b57d
JN
2140if ($output_selection == OUTPUT_EXPORTED ||
2141 $output_selection == OUTPUT_INTERNAL) {
c9b2cfb3
JN
2142
2143 push(@export_file_list, @ARGV);
2144
88c2b57d
JN
2145 foreach (@export_file_list) {
2146 chomp;
2147 process_export_file($_);
2148 }
2149}
2150
8484baaa
RD
2151foreach (@ARGV) {
2152 chomp;
2153 process_file($_);
2154}
2155if ($verbose && $errors) {
2156 print STDERR "$errors errors\n";
2157}
2158if ($verbose && $warnings) {
2159 print STDERR "$warnings warnings\n";
2160}
2161
e814bccb 2162exit($output_mode eq "none" ? 0 : $errors);