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