]> git.ipfire.org Git - thirdparty/kernel/linux.git/blame - scripts/kernel-doc
docs: kernel-doc: Don't mangle literal code blocks in comments
[thirdparty/kernel/linux.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
af250290
JC
751#
752# Apply the RST highlights to a sub-block of text.
753#
754sub highlight_block($) {
755 # The dohighlight kludge requires the text be called $contents
756 my $contents = shift;
c0d1b6ee
JC
757 eval $dohighlight;
758 die $@ if $@;
af250290
JC
759 return $contents;
760}
c0d1b6ee 761
af250290
JC
762#
763# Regexes used only here.
764#
765my $sphinx_literal = '^[^.].*::$';
766my $sphinx_cblock = '^\.\.\ +code-block::';
767
768sub output_highlight_rst {
769 my $input = join "\n",@_;
770 my $output = "";
771 my $line;
772 my $in_literal = 0;
773 my $litprefix;
774 my $block = "";
775
776 foreach $line (split "\n",$input) {
777 #
778 # If we're in a literal block, see if we should drop out
779 # of it. Otherwise pass the line straight through unmunged.
780 #
781 if ($in_literal) {
782 if (! ($line =~ /^\s*$/)) {
783 #
784 # If this is the first non-blank line in a literal
785 # block we need to figure out what the proper indent is.
786 #
787 if ($litprefix eq "") {
788 $line =~ /^(\s*)/;
789 $litprefix = '^' . $1;
790 $output .= $line . "\n";
791 } elsif (! ($line =~ /$litprefix/)) {
792 $in_literal = 0;
793 } else {
794 $output .= $line . "\n";
795 }
796 } else {
797 $output .= $line . "\n";
798 }
799 }
800 #
801 # Not in a literal block (or just dropped out)
802 #
803 if (! $in_literal) {
804 $block .= $line . "\n";
805 if (($line =~ /$sphinx_literal/) || ($line =~ /$sphinx_cblock/)) {
806 $in_literal = 1;
807 $litprefix = "";
808 $output .= highlight_block($block);
809 $block = ""
810 }
811 }
812 }
813
814 if ($block) {
815 $output .= highlight_block($block);
816 }
817 foreach $line (split "\n", $output) {
830066a7 818 print $lineprefix . $line . "\n";
c0d1b6ee
JC
819 }
820}
821
822sub output_function_rst(%) {
823 my %args = %{$_[0]};
824 my ($parameter, $section);
c099ff69 825 my $oldprefix = $lineprefix;
82801d06
MCC
826 my $start = "";
827
828 if ($args{'typedef'}) {
829 print ".. c:type:: ". $args{'function'} . "\n\n";
830 print_lineno($declaration_start_line);
831 print " **Typedef**: ";
832 $lineprefix = "";
833 output_highlight_rst($args{'purpose'});
834 $start = "\n\n**Syntax**\n\n ``";
835 } else {
836 print ".. c:function:: ";
837 }
c0d1b6ee 838 if ($args{'functiontype'} ne "") {
82801d06 839 $start .= $args{'functiontype'} . " " . $args{'function'} . " (";
c0d1b6ee 840 } else {
82801d06 841 $start .= $args{'function'} . " (";
c0d1b6ee
JC
842 }
843 print $start;
844
845 my $count = 0;
846 foreach my $parameter (@{$args{'parameterlist'}}) {
847 if ($count ne 0) {
848 print ", ";
849 }
850 $count++;
851 $type = $args{'parametertypes'}{$parameter};
a88b1672 852
c0d1b6ee
JC
853 if ($type =~ m/([^\(]*\(\*)\s*\)\s*\(([^\)]*)\)/) {
854 # pointer-to-function
855 print $1 . $parameter . ") (" . $2;
856 } else {
857 print $type . " " . $parameter;
858 }
859 }
82801d06
MCC
860 if ($args{'typedef'}) {
861 print ");``\n\n";
862 } else {
863 print ")\n\n";
864 print_lineno($declaration_start_line);
865 $lineprefix = " ";
866 output_highlight_rst($args{'purpose'});
867 print "\n";
868 }
c0d1b6ee 869
ecbcfba1
JN
870 print "**Parameters**\n\n";
871 $lineprefix = " ";
c0d1b6ee
JC
872 foreach $parameter (@{$args{'parameterlist'}}) {
873 my $parameter_name = $parameter;
ada5f446 874 $parameter_name =~ s/\[.*//;
c0d1b6ee
JC
875 $type = $args{'parametertypes'}{$parameter};
876
877 if ($type ne "") {
ecbcfba1 878 print "``$type $parameter``\n";
c0d1b6ee 879 } else {
ecbcfba1 880 print "``$parameter``\n";
c0d1b6ee 881 }
0b0f5f29
DV
882
883 print_lineno($parameterdesc_start_lines{$parameter_name});
884
5e64fa9c
JN
885 if (defined($args{'parameterdescs'}{$parameter_name}) &&
886 $args{'parameterdescs'}{$parameter_name} ne $undescribed) {
c0d1b6ee 887 output_highlight_rst($args{'parameterdescs'}{$parameter_name});
c0d1b6ee 888 } else {
d4b08e0c 889 print " *undescribed*\n";
c0d1b6ee
JC
890 }
891 print "\n";
892 }
c099ff69
JN
893
894 $lineprefix = $oldprefix;
c0d1b6ee
JC
895 output_section_rst(@_);
896}
897
898sub output_section_rst(%) {
899 my %args = %{$_[0]};
900 my $section;
901 my $oldprefix = $lineprefix;
ecbcfba1 902 $lineprefix = "";
c0d1b6ee
JC
903
904 foreach $section (@{$args{'sectionlist'}}) {
ecbcfba1 905 print "**$section**\n\n";
0b0f5f29 906 print_lineno($section_start_lines{$section});
c0d1b6ee
JC
907 output_highlight_rst($args{'sections'}{$section});
908 print "\n";
909 }
910 print "\n";
911 $lineprefix = $oldprefix;
912}
913
914sub output_enum_rst(%) {
915 my %args = %{$_[0]};
916 my ($parameter);
c099ff69 917 my $oldprefix = $lineprefix;
c0d1b6ee 918 my $count;
c0d1b6ee 919 my $name = "enum " . $args{'enum'};
62850976
JN
920
921 print "\n\n.. c:type:: " . $name . "\n\n";
0b0f5f29 922 print_lineno($declaration_start_line);
c099ff69
JN
923 $lineprefix = " ";
924 output_highlight_rst($args{'purpose'});
925 print "\n";
c0d1b6ee 926
ecbcfba1
JN
927 print "**Constants**\n\n";
928 $lineprefix = " ";
c0d1b6ee 929 foreach $parameter (@{$args{'parameterlist'}}) {
ecbcfba1 930 print "``$parameter``\n";
c0d1b6ee
JC
931 if ($args{'parameterdescs'}{$parameter} ne $undescribed) {
932 output_highlight_rst($args{'parameterdescs'}{$parameter});
933 } else {
d4b08e0c 934 print " *undescribed*\n";
c0d1b6ee
JC
935 }
936 print "\n";
937 }
c099ff69 938
c0d1b6ee
JC
939 $lineprefix = $oldprefix;
940 output_section_rst(@_);
941}
942
943sub output_typedef_rst(%) {
944 my %args = %{$_[0]};
945 my ($parameter);
c099ff69 946 my $oldprefix = $lineprefix;
c0d1b6ee
JC
947 my $name = "typedef " . $args{'typedef'};
948
62850976 949 print "\n\n.. c:type:: " . $name . "\n\n";
0b0f5f29 950 print_lineno($declaration_start_line);
c099ff69
JN
951 $lineprefix = " ";
952 output_highlight_rst($args{'purpose'});
953 print "\n";
c0d1b6ee 954
c099ff69 955 $lineprefix = $oldprefix;
c0d1b6ee
JC
956 output_section_rst(@_);
957}
958
959sub output_struct_rst(%) {
960 my %args = %{$_[0]};
961 my ($parameter);
c099ff69 962 my $oldprefix = $lineprefix;
c0d1b6ee
JC
963 my $name = $args{'type'} . " " . $args{'struct'};
964
62850976 965 print "\n\n.. c:type:: " . $name . "\n\n";
0b0f5f29 966 print_lineno($declaration_start_line);
c099ff69
JN
967 $lineprefix = " ";
968 output_highlight_rst($args{'purpose'});
969 print "\n";
c0d1b6ee 970
ecbcfba1
JN
971 print "**Definition**\n\n";
972 print "::\n\n";
8ad72163
MCC
973 my $declaration = $args{'definition'};
974 $declaration =~ s/\t/ /g;
975 print " " . $args{'type'} . " " . $args{'struct'} . " {\n$declaration };\n\n";
c0d1b6ee 976
ecbcfba1
JN
977 print "**Members**\n\n";
978 $lineprefix = " ";
c0d1b6ee
JC
979 foreach $parameter (@{$args{'parameterlist'}}) {
980 ($parameter =~ /^#/) && next;
981
982 my $parameter_name = $parameter;
983 $parameter_name =~ s/\[.*//;
984
985 ($args{'parameterdescs'}{$parameter_name} ne $undescribed) || next;
986 $type = $args{'parametertypes'}{$parameter};
0b0f5f29 987 print_lineno($parameterdesc_start_lines{$parameter_name});
6d232c80 988 print "``" . $parameter . "``\n";
c0d1b6ee 989 output_highlight_rst($args{'parameterdescs'}{$parameter_name});
c0d1b6ee
JC
990 print "\n";
991 }
992 print "\n";
c099ff69
JN
993
994 $lineprefix = $oldprefix;
c0d1b6ee
JC
995 output_section_rst(@_);
996}
997
3a025e1d
MW
998## none mode output functions
999
1000sub output_function_none(%) {
1001}
1002
1003sub output_enum_none(%) {
1004}
1005
1006sub output_typedef_none(%) {
1007}
1008
1009sub output_struct_none(%) {
1010}
1011
1012sub output_blockhead_none(%) {
1013}
1014
1da177e4 1015##
27205744
RD
1016# generic output function for all types (function, struct/union, typedef, enum);
1017# calls the generated, variable output_ function name based on
1018# functype and output_mode
1da177e4
LT
1019sub output_declaration {
1020 no strict 'refs';
1021 my $name = shift;
1022 my $functype = shift;
1023 my $func = "output_${functype}_$output_mode";
b6c3f456
JN
1024 if (($output_selection == OUTPUT_ALL) ||
1025 (($output_selection == OUTPUT_INCLUDE ||
1026 $output_selection == OUTPUT_EXPORTED) &&
1027 defined($function_table{$name})) ||
1028 (($output_selection == OUTPUT_EXCLUDE ||
1029 $output_selection == OUTPUT_INTERNAL) &&
1030 !($functype eq "function" && defined($function_table{$name}))))
1da177e4 1031 {
3c308798 1032 &$func(@_);
1da177e4
LT
1033 $section_counter++;
1034 }
1035}
1036
1037##
27205744 1038# generic output function - calls the right one based on current output mode.
b112e0f7 1039sub output_blockhead {
1da177e4 1040 no strict 'refs';
b9d97328 1041 my $func = "output_blockhead_" . $output_mode;
1da177e4
LT
1042 &$func(@_);
1043 $section_counter++;
1044}
1045
1046##
3c3b809e 1047# takes a declaration (struct, union, enum, typedef) and
1da177e4
LT
1048# invokes the right handler. NOT called for functions.
1049sub dump_declaration($$) {
1050 no strict 'refs';
1051 my ($prototype, $file) = @_;
b9d97328 1052 my $func = "dump_" . $decl_type;
1da177e4
LT
1053 &$func(@_);
1054}
1055
1056sub dump_union($$) {
1057 dump_struct(@_);
1058}
1059
1060sub dump_struct($$) {
1061 my $x = shift;
1062 my $file = shift;
1063
52dc5aec 1064 if ($x =~ /(struct|union)\s+(\w+)\s*{(.*)}/) {
5cb5c31c 1065 my $decl_type = $1;
3c308798
RD
1066 $declaration_name = $2;
1067 my $members = $3;
1da177e4 1068
aeec46b9 1069 # ignore members marked private:
0d8c39e6
MCC
1070 $members =~ s/\/\*\s*private:.*?\/\*\s*public:.*?\*\///gosi;
1071 $members =~ s/\/\*\s*private:.*//gosi;
aeec46b9
MW
1072 # strip comments:
1073 $members =~ s/\/\*.*?\*\///gos;
ef5da59f 1074 # strip attributes
f0074929 1075 $members =~ s/__attribute__\s*\(\([a-z,_\*\s\(\)]*\)\)//i;
7b990789 1076 $members =~ s/__aligned\s*\([^;]*\)//gos;
f0074929 1077 $members =~ s/\s*CRYPTO_MINALIGN_ATTR//gos;
b22b5a9e 1078 # replace DECLARE_BITMAP
45005b27 1079 $members =~ s/DECLARE_BITMAP\s*\(([^,)]+),\s*([^,)]+)\)/unsigned long $1\[BITS_TO_LONGS($2)\]/gos;
1cb566ba 1080 # replace DECLARE_HASHTABLE
45005b27
MCC
1081 $members =~ s/DECLARE_HASHTABLE\s*\(([^,)]+),\s*([^,)]+)\)/unsigned long $1\[1 << (($2) - 1)\]/gos;
1082 # replace DECLARE_KFIFO
1083 $members =~ s/DECLARE_KFIFO\s*\(([^,)]+),\s*([^,)]+),\s*([^,)]+)\)/$2 \*$1/gos;
1084 # replace DECLARE_KFIFO_PTR
1085 $members =~ s/DECLARE_KFIFO_PTR\s*\(([^,)]+),\s*([^,)]+)\)/$2 \*$1/gos;
aeec46b9 1086
8ad72163
MCC
1087 my $declaration = $members;
1088
1089 # Split nested struct/union elements as newer ones
84ce5b98
MCC
1090 while ($members =~ m/(struct|union)([^\{\};]+)\{([^\{\}]*)\}([^\{\}\;]*)\;/) {
1091 my $newmember;
1092 my $maintype = $1;
1093 my $ids = $4;
1094 my $content = $3;
1095 foreach my $id(split /,/, $ids) {
1096 $newmember .= "$maintype $id; ";
1097
8ad72163 1098 $id =~ s/[:\[].*//;
84ce5b98 1099 $id =~ s/^\s*\**(\S+)\s*/$1/;
8ad72163
MCC
1100 foreach my $arg (split /;/, $content) {
1101 next if ($arg =~ m/^\s*$/);
7c0d7e87
MCC
1102 if ($arg =~ m/^([^\(]+\(\*?\s*)([\w\.]*)(\s*\).*)/) {
1103 # pointer-to-function
1104 my $type = $1;
1105 my $name = $2;
1106 my $extra = $3;
1107 next if (!$name);
1108 if ($id =~ m/^\s*$/) {
1109 # anonymous struct/union
84ce5b98 1110 $newmember .= "$type$name$extra; ";
7c0d7e87 1111 } else {
84ce5b98 1112 $newmember .= "$type$id.$name$extra; ";
7c0d7e87 1113 }
8ad72163 1114 } else {
84ce5b98
MCC
1115 my $type;
1116 my $names;
1117 $arg =~ s/^\s+//;
1118 $arg =~ s/\s+$//;
1119 # Handle bitmaps
1120 $arg =~ s/:\s*\d+\s*//g;
1121 # Handle arrays
1122 $arg =~ s/\[\S+\]//g;
1123 # The type may have multiple words,
1124 # and multiple IDs can be defined, like:
1125 # const struct foo, *bar, foobar
1126 # So, we remove spaces when parsing the
1127 # names, in order to match just names
1128 # and commas for the names
1129 $arg =~ s/\s*,\s*/,/g;
1130 if ($arg =~ m/(.*)\s+([\S+,]+)/) {
1131 $type = $1;
1132 $names = $2;
7c0d7e87 1133 } else {
84ce5b98
MCC
1134 $newmember .= "$arg; ";
1135 next;
1136 }
1137 foreach my $name (split /,/, $names) {
1138 $name =~ s/^\s*\**(\S+)\s*/$1/;
1139 next if (($name =~ m/^\s*$/));
1140 if ($id =~ m/^\s*$/) {
1141 # anonymous struct/union
1142 $newmember .= "$type $name; ";
1143 } else {
1144 $newmember .= "$type $id.$name; ";
1145 }
7c0d7e87 1146 }
8ad72163
MCC
1147 }
1148 }
84ce5b98
MCC
1149 }
1150 $members =~ s/(struct|union)([^\{\};]+)\{([^\{\}]*)}([^\{\}\;]*)\;/$newmember/;
1151 }
8ad72163
MCC
1152
1153 # Ignore other nested elements, like enums
1154 $members =~ s/({[^\{\}]*})//g;
8ad72163 1155
151c468b 1156 create_parameterlist($members, ';', $file, $declaration_name);
1081de2d 1157 check_sections($file, $declaration_name, $decl_type, $sectcheck, $struct_actual);
1da177e4 1158
8ad72163
MCC
1159 # Adjust declaration for better display
1160 $declaration =~ s/([{;])/$1\n/g;
1161 $declaration =~ s/}\s+;/};/g;
1162 # Better handle inlined enums
1163 do {} while ($declaration =~ s/(enum\s+{[^}]+),([^\n])/$1,\n$2/);
1164
1165 my @def_args = split /\n/, $declaration;
1166 my $level = 1;
1167 $declaration = "";
1168 foreach my $clause (@def_args) {
1169 $clause =~ s/^\s+//;
1170 $clause =~ s/\s+$//;
1171 $clause =~ s/\s+/ /;
1172 next if (!$clause);
1173 $level-- if ($clause =~ m/(})/ && $level > 1);
1174 if (!($clause =~ m/^\s*#/)) {
1175 $declaration .= "\t" x $level;
1176 }
1177 $declaration .= "\t" . $clause . "\n";
1178 $level++ if ($clause =~ m/({)/ && !($clause =~m/}/));
1179 }
1da177e4
LT
1180 output_declaration($declaration_name,
1181 'struct',
1182 {'struct' => $declaration_name,
1183 'module' => $modulename,
8ad72163 1184 'definition' => $declaration,
1da177e4
LT
1185 'parameterlist' => \@parameterlist,
1186 'parameterdescs' => \%parameterdescs,
1187 'parametertypes' => \%parametertypes,
1188 'sectionlist' => \@sectionlist,
1189 'sections' => \%sections,
1190 'purpose' => $declaration_purpose,
1191 'type' => $decl_type
1192 });
1193 }
1194 else {
d40e1e65 1195 print STDERR "${file}:$.: error: Cannot parse struct or union!\n";
1da177e4
LT
1196 ++$errors;
1197 }
1198}
1199
85afe608
MCC
1200
1201sub show_warnings($$) {
1202 my $functype = shift;
1203 my $name = shift;
1204
1205 return 1 if ($output_selection == OUTPUT_ALL);
1206
1207 if ($output_selection == OUTPUT_EXPORTED) {
1208 if (defined($function_table{$name})) {
1209 return 1;
1210 } else {
1211 return 0;
1212 }
1213 }
1214 if ($output_selection == OUTPUT_INTERNAL) {
1215 if (!($functype eq "function" && defined($function_table{$name}))) {
1216 return 1;
1217 } else {
1218 return 0;
1219 }
1220 }
1221 if ($output_selection == OUTPUT_INCLUDE) {
1222 if (defined($function_table{$name})) {
1223 return 1;
1224 } else {
1225 return 0;
1226 }
1227 }
1228 if ($output_selection == OUTPUT_EXCLUDE) {
1229 if (!defined($function_table{$name})) {
1230 return 1;
1231 } else {
1232 return 0;
1233 }
1234 }
1235 die("Please add the new output type at show_warnings()");
1236}
1237
1da177e4
LT
1238sub dump_enum($$) {
1239 my $x = shift;
1240 my $file = shift;
1241
aeec46b9 1242 $x =~ s@/\*.*?\*/@@gos; # strip comments.
4468e21e
CN
1243 # strip #define macros inside enums
1244 $x =~ s@#\s*((define|ifdef)\s+|endif)[^;]*;@@gos;
b6d676db 1245
1da177e4 1246 if ($x =~ /enum\s+(\w+)\s*{(.*)}/) {
3c308798
RD
1247 $declaration_name = $1;
1248 my $members = $2;
5cb5c31c
JB
1249 my %_members;
1250
463a0fdc 1251 $members =~ s/\s+$//;
1da177e4
LT
1252
1253 foreach my $arg (split ',', $members) {
1254 $arg =~ s/^\s*(\w+).*/$1/;
1255 push @parameterlist, $arg;
1256 if (!$parameterdescs{$arg}) {
3c308798 1257 $parameterdescs{$arg} = $undescribed;
85afe608 1258 if (show_warnings("enum", $declaration_name)) {
2defb272
MCC
1259 print STDERR "${file}:$.: warning: Enum value '$arg' not described in enum '$declaration_name'\n";
1260 }
1da177e4 1261 }
5cb5c31c 1262 $_members{$arg} = 1;
1da177e4 1263 }
3c3b809e 1264
5cb5c31c
JB
1265 while (my ($k, $v) = each %parameterdescs) {
1266 if (!exists($_members{$k})) {
85afe608 1267 if (show_warnings("enum", $declaration_name)) {
2defb272
MCC
1268 print STDERR "${file}:$.: warning: Excess enum value '$k' description in '$declaration_name'\n";
1269 }
5cb5c31c
JB
1270 }
1271 }
1272
1da177e4
LT
1273 output_declaration($declaration_name,
1274 'enum',
1275 {'enum' => $declaration_name,
1276 'module' => $modulename,
1277 'parameterlist' => \@parameterlist,
1278 'parameterdescs' => \%parameterdescs,
1279 'sectionlist' => \@sectionlist,
1280 'sections' => \%sections,
1281 'purpose' => $declaration_purpose
1282 });
1283 }
1284 else {
d40e1e65 1285 print STDERR "${file}:$.: error: Cannot parse enum!\n";
1da177e4
LT
1286 ++$errors;
1287 }
1288}
1289
1290sub dump_typedef($$) {
1291 my $x = shift;
1292 my $file = shift;
1293
aeec46b9 1294 $x =~ s@/\*.*?\*/@@gos; # strip comments.
1da177e4 1295
83766452 1296 # Parse function prototypes
d37c43ce
MCC
1297 if ($x =~ /typedef\s+(\w+)\s*\(\*\s*(\w\S+)\s*\)\s*\((.*)\);/ ||
1298 $x =~ /typedef\s+(\w+)\s*(\w\S+)\s*\s*\((.*)\);/) {
1299
83766452
MCC
1300 # Function typedefs
1301 $return_type = $1;
1302 $declaration_name = $2;
1303 my $args = $3;
1304
151c468b 1305 create_parameterlist($args, ',', $file, $declaration_name);
1da177e4
LT
1306
1307 output_declaration($declaration_name,
83766452
MCC
1308 'function',
1309 {'function' => $declaration_name,
82801d06 1310 'typedef' => 1,
1da177e4 1311 'module' => $modulename,
83766452
MCC
1312 'functiontype' => $return_type,
1313 'parameterlist' => \@parameterlist,
1314 'parameterdescs' => \%parameterdescs,
1315 'parametertypes' => \%parametertypes,
1da177e4
LT
1316 'sectionlist' => \@sectionlist,
1317 'sections' => \%sections,
1318 'purpose' => $declaration_purpose
1319 });
83766452
MCC
1320 return;
1321 }
1322
1323 while (($x =~ /\(*.\)\s*;$/) || ($x =~ /\[*.\]\s*;$/)) {
1324 $x =~ s/\(*.\)\s*;$/;/;
1325 $x =~ s/\[*.\]\s*;$/;/;
1da177e4 1326 }
83766452
MCC
1327
1328 if ($x =~ /typedef.*\s+(\w+)\s*;/) {
3a80a766
MCC
1329 $declaration_name = $1;
1330
1331 output_declaration($declaration_name,
1332 'typedef',
1333 {'typedef' => $declaration_name,
1334 'module' => $modulename,
1335 'sectionlist' => \@sectionlist,
1336 'sections' => \%sections,
1337 'purpose' => $declaration_purpose
1338 });
1339 }
1da177e4 1340 else {
d40e1e65 1341 print STDERR "${file}:$.: error: Cannot parse typedef!\n";
1da177e4
LT
1342 ++$errors;
1343 }
1344}
1345
a1d94aa5
RD
1346sub save_struct_actual($) {
1347 my $actual = shift;
1348
1349 # strip all spaces from the actual param so that it looks like one string item
1350 $actual =~ s/\s*//g;
1351 $struct_actual = $struct_actual . $actual . " ";
1352}
1353
151c468b 1354sub create_parameterlist($$$$) {
1da177e4
LT
1355 my $args = shift;
1356 my $splitter = shift;
1357 my $file = shift;
151c468b 1358 my $declaration_name = shift;
1da177e4
LT
1359 my $type;
1360 my $param;
1361
a6d3fe77 1362 # temporarily replace commas inside function pointer definition
1da177e4 1363 while ($args =~ /(\([^\),]+),/) {
3c308798 1364 $args =~ s/(\([^\),]+),/$1#/g;
1da177e4 1365 }
3c3b809e 1366
1da177e4
LT
1367 foreach my $arg (split($splitter, $args)) {
1368 # strip comments
1369 $arg =~ s/\/\*.*\*\///;
3c308798
RD
1370 # strip leading/trailing spaces
1371 $arg =~ s/^\s*//;
1da177e4
LT
1372 $arg =~ s/\s*$//;
1373 $arg =~ s/\s+/ /;
1374
1375 if ($arg =~ /^#/) {
1376 # Treat preprocessor directive as a typeless variable just to fill
1377 # corresponding data structures "correctly". Catch it later in
1378 # output_* subs.
1379 push_parameter($arg, "", $file);
00d62961 1380 } elsif ($arg =~ m/\(.+\)\s*\(/) {
1da177e4
LT
1381 # pointer-to-function
1382 $arg =~ tr/#/,/;
7c0d7e87 1383 $arg =~ m/[^\(]+\(\*?\s*([\w\.]*)\s*\)/;
1da177e4
LT
1384 $param = $1;
1385 $type = $arg;
00d62961 1386 $type =~ s/([^\(]+\(\*?)\s*$param/$1/;
a1d94aa5 1387 save_struct_actual($param);
151c468b 1388 push_parameter($param, $type, $file, $declaration_name);
aeec46b9 1389 } elsif ($arg) {
1da177e4
LT
1390 $arg =~ s/\s*:\s*/:/g;
1391 $arg =~ s/\s*\[/\[/g;
1392
1393 my @args = split('\s*,\s*', $arg);
1394 if ($args[0] =~ m/\*/) {
1395 $args[0] =~ s/(\*+)\s*/ $1/;
1396 }
884f2810
BP
1397
1398 my @first_arg;
1399 if ($args[0] =~ /^(.*\s+)(.*?\[.*\].*)$/) {
1400 shift @args;
1401 push(@first_arg, split('\s+', $1));
1402 push(@first_arg, $2);
1403 } else {
1404 @first_arg = split('\s+', shift @args);
1405 }
1406
1da177e4
LT
1407 unshift(@args, pop @first_arg);
1408 $type = join " ", @first_arg;
1409
1410 foreach $param (@args) {
1411 if ($param =~ m/^(\*+)\s*(.*)/) {
a1d94aa5 1412 save_struct_actual($2);
151c468b 1413 push_parameter($2, "$type $1", $file, $declaration_name);
1da177e4
LT
1414 }
1415 elsif ($param =~ m/(.*?):(\d+)/) {
7b97887e 1416 if ($type ne "") { # skip unnamed bit-fields
a1d94aa5 1417 save_struct_actual($1);
151c468b 1418 push_parameter($1, "$type:$2", $file, $declaration_name)
7b97887e 1419 }
1da177e4
LT
1420 }
1421 else {
a1d94aa5 1422 save_struct_actual($param);
151c468b 1423 push_parameter($param, $type, $file, $declaration_name);
1da177e4
LT
1424 }
1425 }
1426 }
1427 }
1428}
1429
151c468b 1430sub push_parameter($$$$) {
1da177e4
LT
1431 my $param = shift;
1432 my $type = shift;
1433 my $file = shift;
151c468b 1434 my $declaration_name = shift;
1da177e4 1435
5f8c7c98
RD
1436 if (($anon_struct_union == 1) && ($type eq "") &&
1437 ($param eq "}")) {
1438 return; # ignore the ending }; from anon. struct/union
1439 }
1440
1441 $anon_struct_union = 0;
f9b5c530 1442 $param =~ s/[\[\)].*//;
1da177e4 1443
a6d3fe77 1444 if ($type eq "" && $param =~ /\.\.\.$/)
1da177e4 1445 {
c950a173
SF
1446 if (!$param =~ /\w\.\.\.$/) {
1447 # handles unnamed variable parameters
1448 $param = "...";
1449 }
ced69090
RD
1450 if (!defined $parameterdescs{$param} || $parameterdescs{$param} eq "") {
1451 $parameterdescs{$param} = "variable arguments";
1452 }
1da177e4
LT
1453 }
1454 elsif ($type eq "" && ($param eq "" or $param eq "void"))
1455 {
1da177e4
LT
1456 $param="void";
1457 $parameterdescs{void} = "no arguments";
1458 }
134fe01b
RD
1459 elsif ($type eq "" && ($param eq "struct" or $param eq "union"))
1460 # handle unnamed (anonymous) union or struct:
1461 {
1462 $type = $param;
5f8c7c98 1463 $param = "{unnamed_" . $param . "}";
134fe01b 1464 $parameterdescs{$param} = "anonymous\n";
5f8c7c98 1465 $anon_struct_union = 1;
134fe01b
RD
1466 }
1467
a6d3fe77 1468 # warn if parameter has no description
134fe01b
RD
1469 # (but ignore ones starting with # as these are not parameters
1470 # but inline preprocessor statements);
151c468b 1471 # Note: It will also ignore void params and unnamed structs/unions
f9b5c530 1472 if (!defined $parameterdescs{$param} && $param !~ /^#/) {
151c468b 1473 $parameterdescs{$param} = $undescribed;
a6d3fe77 1474
85afe608 1475 if (show_warnings($type, $declaration_name)) {
2defb272
MCC
1476 print STDERR
1477 "${file}:$.: warning: Function parameter or member '$param' not described in '$declaration_name'\n";
1478 ++$warnings;
1479 }
3c308798 1480 }
1da177e4 1481
25985edc 1482 # strip spaces from $param so that it is one continuous string
e34e7dbb
RD
1483 # on @parameterlist;
1484 # this fixes a problem where check_sections() cannot find
1485 # a parameter like "addr[6 + 2]" because it actually appears
1486 # as "addr[6", "+", "2]" on the parameter list;
1487 # but it's better to maintain the param string unchanged for output,
1488 # so just weaken the string compare in check_sections() to ignore
1489 # "[blah" in a parameter string;
1490 ###$param =~ s/\s*//g;
1da177e4 1491 push @parameterlist, $param;
02a4f4fe 1492 $type =~ s/\s\s+/ /g;
1da177e4
LT
1493 $parametertypes{$param} = $type;
1494}
1495
1081de2d
MCC
1496sub check_sections($$$$$) {
1497 my ($file, $decl_name, $decl_type, $sectcheck, $prmscheck) = @_;
a1d94aa5
RD
1498 my @sects = split ' ', $sectcheck;
1499 my @prms = split ' ', $prmscheck;
1500 my $err;
1501 my ($px, $sx);
1502 my $prm_clean; # strip trailing "[array size]" and/or beginning "*"
1503
1504 foreach $sx (0 .. $#sects) {
1505 $err = 1;
1506 foreach $px (0 .. $#prms) {
1507 $prm_clean = $prms[$px];
1508 $prm_clean =~ s/\[.*\]//;
1f3a6688 1509 $prm_clean =~ s/__attribute__\s*\(\([a-z,_\*\s\(\)]*\)\)//i;
e34e7dbb
RD
1510 # ignore array size in a parameter string;
1511 # however, the original param string may contain
1512 # spaces, e.g.: addr[6 + 2]
1513 # and this appears in @prms as "addr[6" since the
1514 # parameter list is split at spaces;
1515 # hence just ignore "[..." for the sections check;
1516 $prm_clean =~ s/\[.*//;
1517
a1d94aa5
RD
1518 ##$prm_clean =~ s/^\**//;
1519 if ($prm_clean eq $sects[$sx]) {
1520 $err = 0;
1521 last;
1522 }
1523 }
1524 if ($err) {
1525 if ($decl_type eq "function") {
d40e1e65 1526 print STDERR "${file}:$.: warning: " .
a1d94aa5
RD
1527 "Excess function parameter " .
1528 "'$sects[$sx]' " .
1529 "description in '$decl_name'\n";
1530 ++$warnings;
a1d94aa5
RD
1531 }
1532 }
1533 }
1534}
1535
4092bac7
YB
1536##
1537# Checks the section describing the return value of a function.
1538sub check_return_section {
1539 my $file = shift;
1540 my $declaration_name = shift;
1541 my $return_type = shift;
1542
1543 # Ignore an empty return type (It's a macro)
1544 # Ignore functions with a "void" return type. (But don't ignore "void *")
1545 if (($return_type eq "") || ($return_type =~ /void\s*\w*\s*$/)) {
1546 return;
1547 }
1548
1549 if (!defined($sections{$section_return}) ||
1550 $sections{$section_return} eq "") {
d40e1e65 1551 print STDERR "${file}:$.: warning: " .
4092bac7
YB
1552 "No description found for return value of " .
1553 "'$declaration_name'\n";
1554 ++$warnings;
1555 }
1556}
1557
1da177e4
LT
1558##
1559# takes a function prototype and the name of the current file being
1560# processed and spits out all the details stored in the global
1561# arrays/hashes.
1562sub dump_function($$) {
1563 my $prototype = shift;
1564 my $file = shift;
cbb4d3e6 1565 my $noret = 0;
1da177e4
LT
1566
1567 $prototype =~ s/^static +//;
1568 $prototype =~ s/^extern +//;
4dc3b16b 1569 $prototype =~ s/^asmlinkage +//;
1da177e4
LT
1570 $prototype =~ s/^inline +//;
1571 $prototype =~ s/^__inline__ +//;
32e79401
RD
1572 $prototype =~ s/^__inline +//;
1573 $prototype =~ s/^__always_inline +//;
1574 $prototype =~ s/^noinline +//;
74fc5c65 1575 $prototype =~ s/__init +//;
20072205 1576 $prototype =~ s/__init_or_module +//;
270a0096 1577 $prototype =~ s/__meminit +//;
70c95b00 1578 $prototype =~ s/__must_check +//;
0df7c0e3 1579 $prototype =~ s/__weak +//;
cbb4d3e6 1580 my $define = $prototype =~ s/^#\s*define\s+//; #ak added
b1aaa546
PB
1581 $prototype =~ s/__attribute__\s*\(\(
1582 (?:
1583 [\w\s]++ # attribute name
1584 (?:\([^)]*+\))? # attribute arguments
1585 \s*+,? # optional comma at the end
1586 )+
1587 \)\)\s+//x;
1da177e4
LT
1588
1589 # Yes, this truly is vile. We are looking for:
1590 # 1. Return type (may be nothing if we're looking at a macro)
1591 # 2. Function name
1592 # 3. Function parameters.
1593 #
1594 # All the while we have to watch out for function pointer parameters
1595 # (which IIRC is what the two sections are for), C types (these
1596 # regexps don't even start to express all the possibilities), and
1597 # so on.
1598 #
1599 # If you mess with these regexps, it's a good idea to check that
1600 # the following functions' documentation still comes out right:
1601 # - parport_register_device (function pointer parameters)
1602 # - atomic_set (macro)
9598f91f 1603 # - pci_match_device, __copy_to_user (long return type)
1da177e4 1604
cbb4d3e6
HG
1605 if ($define && $prototype =~ m/^()([a-zA-Z0-9_~:]+)\s+/) {
1606 # This is an object-like macro, it has no return type and no parameter
1607 # list.
1608 # Function-like macros are not allowed to have spaces between
1609 # declaration_name and opening parenthesis (notice the \s+).
1610 $return_type = $1;
1611 $declaration_name = $2;
1612 $noret = 1;
1613 } elsif ($prototype =~ m/^()([a-zA-Z0-9_~:]+)\s*\(([^\(]*)\)/ ||
1da177e4 1614 $prototype =~ m/^(\w+)\s+([a-zA-Z0-9_~:]+)\s*\(([^\(]*)\)/ ||
5a0bc578 1615 $prototype =~ m/^(\w+\s*\*+)\s*([a-zA-Z0-9_~:]+)\s*\(([^\(]*)\)/ ||
1da177e4 1616 $prototype =~ m/^(\w+\s+\w+)\s+([a-zA-Z0-9_~:]+)\s*\(([^\(]*)\)/ ||
94b3e03c 1617 $prototype =~ m/^(\w+\s+\w+\s*\*+)\s*([a-zA-Z0-9_~:]+)\s*\(([^\(]*)\)/ ||
1da177e4 1618 $prototype =~ m/^(\w+\s+\w+\s+\w+)\s+([a-zA-Z0-9_~:]+)\s*\(([^\(]*)\)/ ||
5a0bc578 1619 $prototype =~ m/^(\w+\s+\w+\s+\w+\s*\*+)\s*([a-zA-Z0-9_~:]+)\s*\(([^\(]*)\)/ ||
1da177e4
LT
1620 $prototype =~ m/^()([a-zA-Z0-9_~:]+)\s*\(([^\{]*)\)/ ||
1621 $prototype =~ m/^(\w+)\s+([a-zA-Z0-9_~:]+)\s*\(([^\{]*)\)/ ||
5a0bc578 1622 $prototype =~ m/^(\w+\s*\*+)\s*([a-zA-Z0-9_~:]+)\s*\(([^\{]*)\)/ ||
1da177e4 1623 $prototype =~ m/^(\w+\s+\w+)\s+([a-zA-Z0-9_~:]+)\s*\(([^\{]*)\)/ ||
5a0bc578 1624 $prototype =~ m/^(\w+\s+\w+\s*\*+)\s*([a-zA-Z0-9_~:]+)\s*\(([^\{]*)\)/ ||
1da177e4 1625 $prototype =~ m/^(\w+\s+\w+\s+\w+)\s+([a-zA-Z0-9_~:]+)\s*\(([^\{]*)\)/ ||
5a0bc578 1626 $prototype =~ m/^(\w+\s+\w+\s+\w+\s*\*+)\s*([a-zA-Z0-9_~:]+)\s*\(([^\{]*)\)/ ||
9598f91f 1627 $prototype =~ m/^(\w+\s+\w+\s+\w+\s+\w+)\s+([a-zA-Z0-9_~:]+)\s*\(([^\{]*)\)/ ||
5a0bc578
MW
1628 $prototype =~ m/^(\w+\s+\w+\s+\w+\s+\w+\s*\*+)\s*([a-zA-Z0-9_~:]+)\s*\(([^\{]*)\)/ ||
1629 $prototype =~ m/^(\w+\s+\w+\s*\*+\s*\w+\s*\*+\s*)\s*([a-zA-Z0-9_~:]+)\s*\(([^\{]*)\)/) {
1da177e4
LT
1630 $return_type = $1;
1631 $declaration_name = $2;
1632 my $args = $3;
1633
151c468b 1634 create_parameterlist($args, ',', $file, $declaration_name);
1da177e4 1635 } else {
d40e1e65 1636 print STDERR "${file}:$.: warning: cannot understand function prototype: '$prototype'\n";
1da177e4
LT
1637 return;
1638 }
1639
a1d94aa5 1640 my $prms = join " ", @parameterlist;
1081de2d 1641 check_sections($file, $declaration_name, "function", $sectcheck, $prms);
a1d94aa5 1642
4092bac7
YB
1643 # This check emits a lot of warnings at the moment, because many
1644 # functions don't have a 'Return' doc section. So until the number
1645 # of warnings goes sufficiently down, the check is only performed in
1646 # verbose mode.
1647 # TODO: always perform the check.
cbb4d3e6 1648 if ($verbose && !$noret) {
4092bac7
YB
1649 check_return_section($file, $declaration_name, $return_type);
1650 }
1651
3c3b809e 1652 output_declaration($declaration_name,
1da177e4
LT
1653 'function',
1654 {'function' => $declaration_name,
1655 'module' => $modulename,
1656 'functiontype' => $return_type,
1657 'parameterlist' => \@parameterlist,
1658 'parameterdescs' => \%parameterdescs,
1659 'parametertypes' => \%parametertypes,
1660 'sectionlist' => \@sectionlist,
1661 'sections' => \%sections,
1662 'purpose' => $declaration_purpose
1663 });
1664}
1665
1da177e4
LT
1666sub reset_state {
1667 $function = "";
1da177e4
LT
1668 %parameterdescs = ();
1669 %parametertypes = ();
1670 @parameterlist = ();
1671 %sections = ();
1672 @sectionlist = ();
a1d94aa5
RD
1673 $sectcheck = "";
1674 $struct_actual = "";
1da177e4 1675 $prototype = "";
3c3b809e 1676
48af606a
JN
1677 $state = STATE_NORMAL;
1678 $inline_doc_state = STATE_INLINE_NA;
1da177e4
LT
1679}
1680
56afb0f8
JB
1681sub tracepoint_munge($) {
1682 my $file = shift;
1683 my $tracepointname = 0;
1684 my $tracepointargs = 0;
1685
3a9089fd 1686 if ($prototype =~ m/TRACE_EVENT\((.*?),/) {
56afb0f8
JB
1687 $tracepointname = $1;
1688 }
3a9089fd
JB
1689 if ($prototype =~ m/DEFINE_SINGLE_EVENT\((.*?),/) {
1690 $tracepointname = $1;
1691 }
1692 if ($prototype =~ m/DEFINE_EVENT\((.*?),(.*?),/) {
1693 $tracepointname = $2;
1694 }
1695 $tracepointname =~ s/^\s+//; #strip leading whitespace
1696 if ($prototype =~ m/TP_PROTO\((.*?)\)/) {
56afb0f8
JB
1697 $tracepointargs = $1;
1698 }
1699 if (($tracepointname eq 0) || ($tracepointargs eq 0)) {
d40e1e65 1700 print STDERR "${file}:$.: warning: Unrecognized tracepoint format: \n".
56afb0f8
JB
1701 "$prototype\n";
1702 } else {
1703 $prototype = "static inline void trace_$tracepointname($tracepointargs)";
1704 }
1705}
1706
b4870bc5
RD
1707sub syscall_munge() {
1708 my $void = 0;
1709
7c9aa015 1710 $prototype =~ s@[\r\n]+@ @gos; # strip newlines/CR's
b4870bc5
RD
1711## if ($prototype =~ m/SYSCALL_DEFINE0\s*\(\s*(a-zA-Z0-9_)*\s*\)/) {
1712 if ($prototype =~ m/SYSCALL_DEFINE0/) {
1713 $void = 1;
1714## $prototype = "long sys_$1(void)";
1715 }
1716
1717 $prototype =~ s/SYSCALL_DEFINE.*\(/long sys_/; # fix return type & func name
1718 if ($prototype =~ m/long (sys_.*?),/) {
1719 $prototype =~ s/,/\(/;
1720 } elsif ($void) {
1721 $prototype =~ s/\)/\(void\)/;
1722 }
1723
1724 # now delete all of the odd-number commas in $prototype
1725 # so that arg types & arg names don't have a comma between them
1726 my $count = 0;
1727 my $len = length($prototype);
1728 if ($void) {
1729 $len = 0; # skip the for-loop
1730 }
1731 for (my $ix = 0; $ix < $len; $ix++) {
1732 if (substr($prototype, $ix, 1) eq ',') {
1733 $count++;
1734 if ($count % 2 == 1) {
1735 substr($prototype, $ix, 1) = ' ';
1736 }
1737 }
1738 }
1739}
1740
b7afa92b 1741sub process_proto_function($$) {
1da177e4
LT
1742 my $x = shift;
1743 my $file = shift;
1744
51f5a0c8
RD
1745 $x =~ s@\/\/.*$@@gos; # strip C99-style comments to end of line
1746
890c78c2 1747 if ($x =~ m#\s*/\*\s+MACDOC\s*#io || ($x =~ /^#/ && $x !~ /^#\s*define/)) {
1da177e4
LT
1748 # do nothing
1749 }
1750 elsif ($x =~ /([^\{]*)/) {
3c308798 1751 $prototype .= $1;
1da177e4 1752 }
b4870bc5 1753
890c78c2 1754 if (($x =~ /\{/) || ($x =~ /\#\s*define/) || ($x =~ /;/)) {
3c308798 1755 $prototype =~ s@/\*.*?\*/@@gos; # strip comments.
1da177e4
LT
1756 $prototype =~ s@[\r\n]+@ @gos; # strip newlines/cr's.
1757 $prototype =~ s@^\s+@@gos; # strip leading spaces
b4870bc5
RD
1758 if ($prototype =~ /SYSCALL_DEFINE/) {
1759 syscall_munge();
1760 }
3a9089fd
JB
1761 if ($prototype =~ /TRACE_EVENT/ || $prototype =~ /DEFINE_EVENT/ ||
1762 $prototype =~ /DEFINE_SINGLE_EVENT/)
1763 {
56afb0f8
JB
1764 tracepoint_munge($file);
1765 }
b4870bc5 1766 dump_function($prototype, $file);
1da177e4
LT
1767 reset_state();
1768 }
1769}
1770
b7afa92b 1771sub process_proto_type($$) {
1da177e4
LT
1772 my $x = shift;
1773 my $file = shift;
1774
1da177e4
LT
1775 $x =~ s@[\r\n]+@ @gos; # strip newlines/cr's.
1776 $x =~ s@^\s+@@gos; # strip leading spaces
1777 $x =~ s@\s+$@@gos; # strip trailing spaces
51f5a0c8
RD
1778 $x =~ s@\/\/.*$@@gos; # strip C99-style comments to end of line
1779
1da177e4
LT
1780 if ($x =~ /^#/) {
1781 # To distinguish preprocessor directive from regular declaration later.
1782 $x .= ";";
1783 }
1784
1785 while (1) {
3c308798 1786 if ( $x =~ /([^{};]*)([{};])(.*)/ ) {
463a0fdc
MH
1787 if( length $prototype ) {
1788 $prototype .= " "
1789 }
1da177e4
LT
1790 $prototype .= $1 . $2;
1791 ($2 eq '{') && $brcount++;
1792 ($2 eq '}') && $brcount--;
1793 if (($2 eq ';') && ($brcount == 0)) {
b9d97328 1794 dump_declaration($prototype, $file);
1da177e4 1795 reset_state();
3c308798 1796 last;
1da177e4
LT
1797 }
1798 $x = $3;
3c308798 1799 } else {
1da177e4
LT
1800 $prototype .= $x;
1801 last;
1802 }
1803 }
1804}
1805
6b5b55f6 1806
1ad560e4 1807sub map_filename($) {
2283a117 1808 my $file;
68f86662 1809 my ($orig_file) = @_;
1da177e4 1810
2283a117 1811 if (defined($ENV{'SRCTREE'})) {
68f86662 1812 $file = "$ENV{'SRCTREE'}" . "/" . $orig_file;
1ad560e4 1813 } else {
68f86662 1814 $file = $orig_file;
2283a117 1815 }
1ad560e4 1816
1da177e4
LT
1817 if (defined($source_map{$file})) {
1818 $file = $source_map{$file};
1819 }
1820
1ad560e4
JN
1821 return $file;
1822}
1823
88c2b57d
JN
1824sub process_export_file($) {
1825 my ($orig_file) = @_;
1826 my $file = map_filename($orig_file);
1827
1828 if (!open(IN,"<$file")) {
1829 print STDERR "Error: Cannot open file $file\n";
1830 ++$errors;
1831 return;
1832 }
1833
1834 while (<IN>) {
1835 if (/$export_symbol/) {
1836 $function_table{$2} = 1;
1837 }
1838 }
1839
1840 close(IN);
1841}
1842
07048d13
JC
1843#
1844# Parsers for the various processing states.
1845#
1846# STATE_NORMAL: looking for the /** to begin everything.
1847#
1848sub process_normal() {
1849 if (/$doc_start/o) {
1850 $state = STATE_NAME; # next line is always the function name
1851 $in_doc_sect = 0;
1852 $declaration_start_line = $. + 1;
1853 }
1854}
1855
3cac2bc4
JC
1856#
1857# STATE_NAME: Looking for the "name - description" line
1858#
1859sub process_name($$) {
1860 my $file = shift;
1861 my $identifier;
1862 my $descr;
1863
1864 if (/$doc_block/o) {
1865 $state = STATE_DOCBLOCK;
1866 $contents = "";
1867 $new_start_line = $. + 1;
1868
1869 if ( $1 eq "" ) {
1870 $section = $section_intro;
1871 } else {
1872 $section = $1;
1873 }
1874 }
1875 elsif (/$doc_decl/o) {
1876 $identifier = $1;
1877 if (/\s*([\w\s]+?)\s*-/) {
1878 $identifier = $1;
1879 }
07048d13 1880
3cac2bc4
JC
1881 $state = STATE_BODY;
1882 # if there's no @param blocks need to set up default section
1883 # here
1884 $contents = "";
1885 $section = $section_default;
1886 $new_start_line = $. + 1;
1887 if (/-(.*)/) {
1888 # strip leading/trailing/multiple spaces
1889 $descr= $1;
1890 $descr =~ s/^\s*//;
1891 $descr =~ s/\s*$//;
1892 $descr =~ s/\s+/ /g;
1893 $declaration_purpose = $descr;
1894 $state = STATE_BODY_MAYBE;
1895 } else {
1896 $declaration_purpose = "";
1897 }
1898
1899 if (($declaration_purpose eq "") && $verbose) {
1900 print STDERR "${file}:$.: warning: missing initial short description on line:\n";
1901 print STDERR $_;
1902 ++$warnings;
1903 }
1904
1905 if ($identifier =~ m/^struct/) {
1906 $decl_type = 'struct';
1907 } elsif ($identifier =~ m/^union/) {
1908 $decl_type = 'union';
1909 } elsif ($identifier =~ m/^enum/) {
1910 $decl_type = 'enum';
1911 } elsif ($identifier =~ m/^typedef/) {
1912 $decl_type = 'typedef';
1913 } else {
1914 $decl_type = 'function';
1915 }
1916
1917 if ($verbose) {
1918 print STDERR "${file}:$.: info: Scanning doc for $identifier\n";
1919 }
1920 } else {
1921 print STDERR "${file}:$.: warning: Cannot understand $_ on line $.",
1922 " - I thought it was a doc line\n";
1923 ++$warnings;
1924 $state = STATE_NORMAL;
1925 }
1926}
07048d13 1927
d742f24d
JC
1928
1929#
1930# STATE_BODY and STATE_BODY_MAYBE: the bulk of a kerneldoc comment.
1931#
1932sub process_body($$) {
1933 my $file = shift;
1934
1935 if (/$doc_sect/i) { # case insensitive for supported section names
1936 $newsection = $1;
1937 $newcontents = $2;
1938
1939 # map the supported section names to the canonical names
1940 if ($newsection =~ m/^description$/i) {
1941 $newsection = $section_default;
1942 } elsif ($newsection =~ m/^context$/i) {
1943 $newsection = $section_context;
1944 } elsif ($newsection =~ m/^returns?$/i) {
1945 $newsection = $section_return;
1946 } elsif ($newsection =~ m/^\@return$/) {
1947 # special: @return is a section, not a param description
1948 $newsection = $section_return;
1949 }
1950
1951 if (($contents ne "") && ($contents ne "\n")) {
1952 if (!$in_doc_sect && $verbose) {
1953 print STDERR "${file}:$.: warning: contents before sections\n";
1954 ++$warnings;
1955 }
1956 dump_section($file, $section, $contents);
1957 $section = $section_default;
1958 }
1959
1960 $in_doc_sect = 1;
1961 $state = STATE_BODY;
1962 $contents = $newcontents;
1963 $new_start_line = $.;
1964 while (substr($contents, 0, 1) eq " ") {
1965 $contents = substr($contents, 1);
1966 }
1967 if ($contents ne "") {
1968 $contents .= "\n";
1969 }
1970 $section = $newsection;
1971 $leading_space = undef;
1972 } elsif (/$doc_end/) {
1973 if (($contents ne "") && ($contents ne "\n")) {
1974 dump_section($file, $section, $contents);
1975 $section = $section_default;
1976 $contents = "";
1977 }
1978 # look for doc_com + <text> + doc_end:
1979 if ($_ =~ m'\s*\*\s*[a-zA-Z_0-9:\.]+\*/') {
1980 print STDERR "${file}:$.: warning: suspicious ending line: $_";
1981 ++$warnings;
1982 }
1983
1984 $prototype = "";
1985 $state = STATE_PROTO;
1986 $brcount = 0;
1987 } elsif (/$doc_content/) {
1988 # miguel-style comment kludge, look for blank lines after
1989 # @parameter line to signify start of description
1990 if ($1 eq "") {
1991 if ($section =~ m/^@/ || $section eq $section_context) {
1992 dump_section($file, $section, $contents);
1993 $section = $section_default;
1994 $contents = "";
1995 $new_start_line = $.;
1996 } else {
1997 $contents .= "\n";
1998 }
1999 $state = STATE_BODY;
2000 } elsif ($state == STATE_BODY_MAYBE) {
2001 # Continued declaration purpose
2002 chomp($declaration_purpose);
2003 $declaration_purpose .= " " . $1;
2004 $declaration_purpose =~ s/\s+/ /g;
2005 } else {
2006 my $cont = $1;
2007 if ($section =~ m/^@/ || $section eq $section_context) {
2008 if (!defined $leading_space) {
2009 if ($cont =~ m/^(\s+)/) {
2010 $leading_space = $1;
2011 } else {
2012 $leading_space = "";
2013 }
2014 }
2015 $cont =~ s/^$leading_space//;
2016 }
2017 $contents .= $cont . "\n";
2018 }
2019 } else {
2020 # i dont know - bad line? ignore.
2021 print STDERR "${file}:$.: warning: bad line: $_";
2022 ++$warnings;
2023 }
2024}
2025
2026
cc794812
JC
2027#
2028# STATE_PROTO: reading a function/whatever prototype.
2029#
2030sub process_proto($$) {
2031 my $file = shift;
2032
2033 if (/$doc_inline_oneline/) {
2034 $section = $1;
2035 $contents = $2;
2036 if ($contents ne "") {
2037 $contents .= "\n";
2038 dump_section($file, $section, $contents);
2039 $section = $section_default;
2040 $contents = "";
2041 }
2042 } elsif (/$doc_inline_start/) {
2043 $state = STATE_INLINE;
2044 $inline_doc_state = STATE_INLINE_NAME;
2045 } elsif ($decl_type eq 'function') {
2046 process_proto_function($_, $file);
2047 } else {
2048 process_proto_type($_, $file);
2049 }
2050}
2051
c17add56
JC
2052#
2053# STATE_DOCBLOCK: within a DOC: block.
2054#
2055sub process_docblock($$) {
2056 my $file = shift;
2057
2058 if (/$doc_end/) {
2059 dump_doc_section($file, $section, $contents);
2060 $section = $section_default;
2061 $contents = "";
2062 $function = "";
2063 %parameterdescs = ();
2064 %parametertypes = ();
2065 @parameterlist = ();
2066 %sections = ();
2067 @sectionlist = ();
2068 $prototype = "";
2069 $state = STATE_NORMAL;
2070 } elsif (/$doc_content/) {
2071 if ( $1 eq "" ) {
2072 $contents .= $blankline;
2073 } else {
2074 $contents .= $1 . "\n";
2075 }
2076 }
2077}
2078
2079#
2080# STATE_INLINE: docbook comments within a prototype.
2081#
2082sub process_inline($$) {
2083 my $file = shift;
2084
2085 # First line (state 1) needs to be a @parameter
2086 if ($inline_doc_state == STATE_INLINE_NAME && /$doc_inline_sect/o) {
2087 $section = $1;
2088 $contents = $2;
2089 $new_start_line = $.;
2090 if ($contents ne "") {
2091 while (substr($contents, 0, 1) eq " ") {
2092 $contents = substr($contents, 1);
2093 }
2094 $contents .= "\n";
2095 }
2096 $inline_doc_state = STATE_INLINE_TEXT;
2097 # Documentation block end */
2098 } elsif (/$doc_inline_end/) {
2099 if (($contents ne "") && ($contents ne "\n")) {
2100 dump_section($file, $section, $contents);
2101 $section = $section_default;
2102 $contents = "";
2103 }
2104 $state = STATE_PROTO;
2105 $inline_doc_state = STATE_INLINE_NA;
2106 # Regular text
2107 } elsif (/$doc_content/) {
2108 if ($inline_doc_state == STATE_INLINE_TEXT) {
2109 $contents .= $1 . "\n";
2110 # nuke leading blank lines
2111 if ($contents =~ /^\s*$/) {
2112 $contents = "";
2113 }
2114 } elsif ($inline_doc_state == STATE_INLINE_NAME) {
2115 $inline_doc_state = STATE_INLINE_ERROR;
2116 print STDERR "${file}:$.: warning: ";
2117 print STDERR "Incorrect use of kernel-doc format: $_";
2118 ++$warnings;
2119 }
2120 }
2121}
2122
cc794812 2123
1ad560e4
JN
2124sub process_file($) {
2125 my $file;
1ad560e4
JN
2126 my $initial_section_counter = $section_counter;
2127 my ($orig_file) = @_;
1ad560e4
JN
2128
2129 $file = map_filename($orig_file);
2130
1da177e4
LT
2131 if (!open(IN,"<$file")) {
2132 print STDERR "Error: Cannot open file $file\n";
2133 ++$errors;
2134 return;
2135 }
2136
a9e7314b
ID
2137 $. = 1;
2138
1da177e4
LT
2139 $section_counter = 0;
2140 while (<IN>) {
65478428
DS
2141 while (s/\\\s*$//) {
2142 $_ .= <IN>;
2143 }
7c9aa015
MCC
2144 # Replace tabs by spaces
2145 while ($_ =~ s/\t+/' ' x (length($&) * 8 - length($`) % 8)/e) {};
c17add56
JC
2146
2147 # Hand this line to the appropriate state handler
48af606a 2148 if ($state == STATE_NORMAL) {
07048d13 2149 process_normal();
3cac2bc4
JC
2150 } elsif ($state == STATE_NAME) {
2151 process_name($file, $_);
17b78717 2152 } elsif ($state == STATE_BODY || $state == STATE_BODY_MAYBE) {
d742f24d 2153 process_body($file, $_);
48af606a 2154 } elsif ($state == STATE_INLINE) { # scanning for inline parameters
c17add56 2155 process_inline($file, $_);
cc794812
JC
2156 } elsif ($state == STATE_PROTO) {
2157 process_proto($file, $_);
48af606a 2158 } elsif ($state == STATE_DOCBLOCK) {
c17add56 2159 process_docblock($file, $_);
3c308798 2160 }
1da177e4 2161 }
c17add56
JC
2162
2163 # Make sure we got something interesting.
1da177e4 2164 if ($initial_section_counter == $section_counter) {
3a025e1d
MW
2165 if ($output_mode ne "none") {
2166 print STDERR "${file}:1: warning: no structured comments found\n";
2167 }
b6c3f456 2168 if (($output_selection == OUTPUT_INCLUDE) && ($show_not_found == 1)) {
e946c43a
JB
2169 print STDERR " Was looking for '$_'.\n" for keys %function_table;
2170 }
1da177e4
LT
2171 }
2172}
8484baaa
RD
2173
2174
2175$kernelversion = get_kernel_version();
2176
2177# generate a sequence of code that will splice in highlighting information
2178# using the s// operator.
1ef06233 2179for (my $k = 0; $k < @highlights; $k++) {
4d732701
DCLP
2180 my $pattern = $highlights[$k][0];
2181 my $result = $highlights[$k][1];
2182# print STDERR "scanning pattern:$pattern, highlight:($result)\n";
2183 $dohighlight .= "\$contents =~ s:$pattern:$result:gs;\n";
8484baaa
RD
2184}
2185
2186# Read the file that maps relative names to absolute names for
2187# separate source and object directories and for shadow trees.
2188if (open(SOURCE_MAP, "<.tmp_filelist.txt")) {
2189 my ($relname, $absname);
2190 while(<SOURCE_MAP>) {
2191 chop();
2192 ($relname, $absname) = (split())[0..1];
2193 $relname =~ s:^/+::;
2194 $source_map{$relname} = $absname;
2195 }
2196 close(SOURCE_MAP);
2197}
2198
88c2b57d
JN
2199if ($output_selection == OUTPUT_EXPORTED ||
2200 $output_selection == OUTPUT_INTERNAL) {
c9b2cfb3
JN
2201
2202 push(@export_file_list, @ARGV);
2203
88c2b57d
JN
2204 foreach (@export_file_list) {
2205 chomp;
2206 process_export_file($_);
2207 }
2208}
2209
8484baaa
RD
2210foreach (@ARGV) {
2211 chomp;
2212 process_file($_);
2213}
2214if ($verbose && $errors) {
2215 print STDERR "$errors errors\n";
2216}
2217if ($verbose && $warnings) {
2218 print STDERR "$warnings warnings\n";
2219}
2220
e814bccb 2221exit($output_mode eq "none" ? 0 : $errors);