xref: /openbmc/linux/scripts/kernel-doc (revision 93351d41)
1cb77f0d6SKamil Rytarowski#!/usr/bin/env perl
238476378SJonathan Corbet# SPDX-License-Identifier: GPL-2.0
31da177e4SLinus Torvalds
4cb77f0d6SKamil Rytarowskiuse warnings;
51da177e4SLinus Torvaldsuse strict;
61da177e4SLinus Torvalds
71da177e4SLinus Torvalds## Copyright (c) 1998 Michael Zucchi, All Rights Reserved        ##
81da177e4SLinus Torvalds## Copyright (C) 2000, 1  Tim Waugh <twaugh@redhat.com>          ##
91da177e4SLinus Torvalds## Copyright (C) 2001  Simon Huggins                             ##
1070c95b00SRandy Dunlap## Copyright (C) 2005-2012  Randy Dunlap                         ##
111b40c194SDan Luedtke## Copyright (C) 2012  Dan Luedtke                               ##
121da177e4SLinus Torvalds## 								 ##
131da177e4SLinus Torvalds## #define enhancements by Armin Kuster <akuster@mvista.com>	 ##
141da177e4SLinus Torvalds## Copyright (c) 2000 MontaVista Software, Inc.			 ##
151da177e4SLinus Torvalds## 								 ##
161da177e4SLinus Torvalds## This software falls under the GNU General Public License.     ##
171da177e4SLinus Torvalds## Please read the COPYING file for more information             ##
181da177e4SLinus Torvalds
191da177e4SLinus Torvalds# 18/01/2001 - 	Cleanups
201da177e4SLinus Torvalds# 		Functions prototyped as foo(void) same as foo()
211da177e4SLinus Torvalds# 		Stop eval'ing where we don't need to.
221da177e4SLinus Torvalds# -- huggie@earth.li
231da177e4SLinus Torvalds
241da177e4SLinus Torvalds# 27/06/2001 -  Allowed whitespace after initial "/**" and
251da177e4SLinus Torvalds#               allowed comments before function declarations.
261da177e4SLinus Torvalds# -- Christian Kreibich <ck@whoop.org>
271da177e4SLinus Torvalds
281da177e4SLinus Torvalds# Still to do:
291da177e4SLinus Torvalds# 	- add perldoc documentation
301da177e4SLinus Torvalds# 	- Look more closely at some of the scarier bits :)
311da177e4SLinus Torvalds
321da177e4SLinus Torvalds# 26/05/2001 - 	Support for separate source and object trees.
331da177e4SLinus Torvalds#		Return error code.
341da177e4SLinus Torvalds# 		Keith Owens <kaos@ocs.com.au>
351da177e4SLinus Torvalds
361da177e4SLinus Torvalds# 23/09/2001 - Added support for typedefs, structs, enums and unions
371da177e4SLinus Torvalds#              Support for Context section; can be terminated using empty line
381da177e4SLinus Torvalds#              Small fixes (like spaces vs. \s in regex)
391da177e4SLinus Torvalds# -- Tim Jansen <tim@tjansen.de>
401da177e4SLinus Torvalds
411b40c194SDan Luedtke# 25/07/2012 - Added support for HTML5
421b40c194SDan Luedtke# -- Dan Luedtke <mail@danrl.de>
431da177e4SLinus Torvalds
44fadc0b31SJani Nikulasub usage {
45fadc0b31SJani Nikula    my $message = <<"EOF";
46fadc0b31SJani NikulaUsage: $0 [OPTION ...] FILE ...
471da177e4SLinus Torvalds
48fadc0b31SJani NikulaRead C language source or header FILEs, extract embedded documentation comments,
49fadc0b31SJani Nikulaand print formatted documentation to standard output.
501da177e4SLinus Torvalds
51fadc0b31SJani NikulaThe documentation comments are identified by "/**" opening comment mark. See
52857af3b7SMauro Carvalho ChehabDocumentation/doc-guide/kernel-doc.rst for the documentation comment syntax.
53fadc0b31SJani Nikula
54fadc0b31SJani NikulaOutput format selection (mutually exclusive):
55fadc0b31SJani Nikula  -man			Output troff manual page format. This is the default.
56c0d1b6eeSJonathan Corbet  -rst			Output reStructuredText format.
573a025e1dSMatthew Wilcox  -none			Do not output documentation, only warnings.
58fadc0b31SJani Nikula
5993351d41SMauro Carvalho ChehabOutput format selection modifier (affects only ReST output):
6093351d41SMauro Carvalho Chehab
6193351d41SMauro Carvalho Chehab  -sphinx-version	Use the ReST C domain dialect compatible with an
6293351d41SMauro Carvalho Chehab			specific Sphinx Version.
6393351d41SMauro Carvalho Chehab			If not specified, kernel-doc will auto-detect using
6493351d41SMauro Carvalho Chehab			the sphinx-build version found on PATH.
6593351d41SMauro Carvalho Chehab
66fadc0b31SJani NikulaOutput selection (mutually exclusive):
6786ae2e38SJani Nikula  -export		Only output documentation for symbols that have been
6886ae2e38SJani Nikula			exported using EXPORT_SYMBOL() or EXPORT_SYMBOL_GPL()
69c9b2cfb3SJani Nikula                        in any input FILE or -export-file FILE.
7086ae2e38SJani Nikula  -internal		Only output documentation for symbols that have NOT been
7186ae2e38SJani Nikula			exported using EXPORT_SYMBOL() or EXPORT_SYMBOL_GPL()
72c9b2cfb3SJani Nikula                        in any input FILE or -export-file FILE.
73fadc0b31SJani Nikula  -function NAME	Only output documentation for the given function(s)
74fadc0b31SJani Nikula			or DOC: section title(s). All other functions and DOC:
75fadc0b31SJani Nikula			sections are ignored. May be specified multiple times.
76eab795ddSMauro Carvalho Chehab  -nosymbol NAME	Exclude the specified symbols from the output
77eab795ddSMauro Carvalho Chehab		        documentation. May be specified multiple times.
78fadc0b31SJani Nikula
79fadc0b31SJani NikulaOutput selection modifiers:
80fadc0b31SJani Nikula  -no-doc-sections	Do not output DOC: sections.
810b0f5f29SDaniel Vetter  -enable-lineno        Enable output of #define LINENO lines. Only works with
820b0f5f29SDaniel Vetter                        reStructuredText format.
8388c2b57dSJani Nikula  -export-file FILE     Specify an additional FILE in which to look for
8488c2b57dSJani Nikula                        EXPORT_SYMBOL() and EXPORT_SYMBOL_GPL(). To be used with
8588c2b57dSJani Nikula                        -export or -internal. May be specified multiple times.
86fadc0b31SJani Nikula
87fadc0b31SJani NikulaOther parameters:
88fadc0b31SJani Nikula  -v			Verbose output, more warnings and other information.
89fadc0b31SJani Nikula  -h			Print this help.
902c12c810SPierre-Louis Bossart  -Werror		Treat warnings as errors.
91fadc0b31SJani Nikula
92fadc0b31SJani NikulaEOF
93fadc0b31SJani Nikula    print $message;
94fadc0b31SJani Nikula    exit 1;
95fadc0b31SJani Nikula}
961da177e4SLinus Torvalds
971da177e4SLinus Torvalds#
981da177e4SLinus Torvalds# format of comments.
991da177e4SLinus Torvalds# In the following table, (...)? signifies optional structure.
1001da177e4SLinus Torvalds#                         (...)* signifies 0 or more structure elements
1011da177e4SLinus Torvalds# /**
1021da177e4SLinus Torvalds#  * function_name(:)? (- short description)?
1031da177e4SLinus Torvalds# (* @parameterx: (description of parameter x)?)*
1041da177e4SLinus Torvalds# (* a blank line)?
1051da177e4SLinus Torvalds#  * (Description:)? (Description of function)?
1061da177e4SLinus Torvalds#  * (section header: (section description)? )*
1071da177e4SLinus Torvalds#  (*)?*/
1081da177e4SLinus Torvalds#
1091da177e4SLinus Torvalds# So .. the trivial example would be:
1101da177e4SLinus Torvalds#
1111da177e4SLinus Torvalds# /**
1121da177e4SLinus Torvalds#  * my_function
113b9d97328SRandy Dunlap#  */
1141da177e4SLinus Torvalds#
115891dcd2fSRandy Dunlap# If the Description: header tag is omitted, then there must be a blank line
1161da177e4SLinus Torvalds# after the last parameter specification.
1171da177e4SLinus Torvalds# e.g.
1181da177e4SLinus Torvalds# /**
1191da177e4SLinus Torvalds#  * my_function - does my stuff
1201da177e4SLinus Torvalds#  * @my_arg: its mine damnit
1211da177e4SLinus Torvalds#  *
1221da177e4SLinus Torvalds#  * Does my stuff explained.
1231da177e4SLinus Torvalds#  */
1241da177e4SLinus Torvalds#
1251da177e4SLinus Torvalds#  or, could also use:
1261da177e4SLinus Torvalds# /**
1271da177e4SLinus Torvalds#  * my_function - does my stuff
1281da177e4SLinus Torvalds#  * @my_arg: its mine damnit
1291da177e4SLinus Torvalds#  * Description: Does my stuff explained.
1301da177e4SLinus Torvalds#  */
1311da177e4SLinus Torvalds# etc.
1321da177e4SLinus Torvalds#
133b9d97328SRandy Dunlap# Besides functions you can also write documentation for structs, unions,
1341da177e4SLinus Torvalds# enums and typedefs. Instead of the function name you must write the name
1351da177e4SLinus Torvalds# of the declaration;  the struct/union/enum/typedef must always precede
1361da177e4SLinus Torvalds# the name. Nesting of declarations is not supported.
1371da177e4SLinus Torvalds# Use the argument mechanism to document members or constants.
1381da177e4SLinus Torvalds# e.g.
1391da177e4SLinus Torvalds# /**
1401da177e4SLinus Torvalds#  * struct my_struct - short description
1411da177e4SLinus Torvalds#  * @a: first member
1421da177e4SLinus Torvalds#  * @b: second member
1431da177e4SLinus Torvalds#  *
1441da177e4SLinus Torvalds#  * Longer description
1451da177e4SLinus Torvalds#  */
1461da177e4SLinus Torvalds# struct my_struct {
1471da177e4SLinus Torvalds#     int a;
1481da177e4SLinus Torvalds#     int b;
149aeec46b9SMartin Waitz# /* private: */
150aeec46b9SMartin Waitz#     int c;
1511da177e4SLinus Torvalds# };
1521da177e4SLinus Torvalds#
1531da177e4SLinus Torvalds# All descriptions can be multiline, except the short function description.
1541da177e4SLinus Torvalds#
155a4c6ebedSDanilo Cesar Lemes de Paula# For really longs structs, you can also describe arguments inside the
156a4c6ebedSDanilo Cesar Lemes de Paula# body of the struct.
157a4c6ebedSDanilo Cesar Lemes de Paula# eg.
158a4c6ebedSDanilo Cesar Lemes de Paula# /**
159a4c6ebedSDanilo Cesar Lemes de Paula#  * struct my_struct - short description
160a4c6ebedSDanilo Cesar Lemes de Paula#  * @a: first member
161a4c6ebedSDanilo Cesar Lemes de Paula#  * @b: second member
162a4c6ebedSDanilo Cesar Lemes de Paula#  *
163a4c6ebedSDanilo Cesar Lemes de Paula#  * Longer description
164a4c6ebedSDanilo Cesar Lemes de Paula#  */
165a4c6ebedSDanilo Cesar Lemes de Paula# struct my_struct {
166a4c6ebedSDanilo Cesar Lemes de Paula#     int a;
167a4c6ebedSDanilo Cesar Lemes de Paula#     int b;
168a4c6ebedSDanilo Cesar Lemes de Paula#     /**
169a4c6ebedSDanilo Cesar Lemes de Paula#      * @c: This is longer description of C
170a4c6ebedSDanilo Cesar Lemes de Paula#      *
171a4c6ebedSDanilo Cesar Lemes de Paula#      * You can use paragraphs to describe arguments
172a4c6ebedSDanilo Cesar Lemes de Paula#      * using this method.
173a4c6ebedSDanilo Cesar Lemes de Paula#      */
174a4c6ebedSDanilo Cesar Lemes de Paula#     int c;
175a4c6ebedSDanilo Cesar Lemes de Paula# };
176a4c6ebedSDanilo Cesar Lemes de Paula#
177a4c6ebedSDanilo Cesar Lemes de Paula# This should be use only for struct/enum members.
178a4c6ebedSDanilo Cesar Lemes de Paula#
1791da177e4SLinus Torvalds# You can also add additional sections. When documenting kernel functions you
1801da177e4SLinus Torvalds# should document the "Context:" of the function, e.g. whether the functions
1811da177e4SLinus Torvalds# can be called form interrupts. Unlike other sections you can end it with an
1821da177e4SLinus Torvalds# empty line.
1834092bac7SYacine Belkadi# A non-void function should have a "Return:" section describing the return
1844092bac7SYacine Belkadi# value(s).
1851da177e4SLinus Torvalds# Example-sections should contain the string EXAMPLE so that they are marked
1861da177e4SLinus Torvalds# appropriately in DocBook.
1871da177e4SLinus Torvalds#
1881da177e4SLinus Torvalds# Example:
1891da177e4SLinus Torvalds# /**
1901da177e4SLinus Torvalds#  * user_function - function that can only be called in user context
1911da177e4SLinus Torvalds#  * @a: some argument
1921da177e4SLinus Torvalds#  * Context: !in_interrupt()
1931da177e4SLinus Torvalds#  *
1941da177e4SLinus Torvalds#  * Some description
1951da177e4SLinus Torvalds#  * Example:
1961da177e4SLinus Torvalds#  *    user_function(22);
1971da177e4SLinus Torvalds#  */
1981da177e4SLinus Torvalds# ...
1991da177e4SLinus Torvalds#
2001da177e4SLinus Torvalds#
2011da177e4SLinus Torvalds# All descriptive text is further processed, scanning for the following special
2021da177e4SLinus Torvalds# patterns, which are highlighted appropriately.
2031da177e4SLinus Torvalds#
2041da177e4SLinus Torvalds# 'funcname()' - function
2051da177e4SLinus Torvalds# '$ENVVAR' - environmental variable
2061da177e4SLinus Torvalds# '&struct_name' - name of a structure (up to two words including 'struct')
2075267dd35SPaolo Bonzini# '&struct_name.member' - name of a structure member
2081da177e4SLinus Torvalds# '@parameter' - name of a parameter
2091da177e4SLinus Torvalds# '%CONST' - name of a constant.
210b97f193aSMauro Carvalho Chehab# '``LITERAL``' - literal string without any spaces on it.
2111da177e4SLinus Torvalds
2128484baaaSRandy Dunlap## init lots of data
2138484baaaSRandy Dunlap
2141da177e4SLinus Torvaldsmy $errors = 0;
2151da177e4SLinus Torvaldsmy $warnings = 0;
2165f8c7c98SRandy Dunlapmy $anon_struct_union = 0;
2171da177e4SLinus Torvalds
2181da177e4SLinus Torvalds# match expressions used to find embedded type information
219b97f193aSMauro Carvalho Chehabmy $type_constant = '\b``([^\`]+)``\b';
220b97f193aSMauro Carvalho Chehabmy $type_constant2 = '\%([-_\w]+)';
2211da177e4SLinus Torvaldsmy $type_func = '(\w+)\(\)';
222bfd228c7SMike Rapoportmy $type_param = '\@(\w*((\.\w+)|(->\w+))*(\.\.\.)?)';
223ee2aa759SMauro Carvalho Chehabmy $type_param_ref = '([\!]?)\@(\w*((\.\w+)|(->\w+))*(\.\.\.)?)';
2245219f18aSJonathan Corbetmy $type_fp_param = '\@(\w+)\(\)';  # Special RST handling for func ptr params
225346282dbSMauro Carvalho Chehabmy $type_fp_param2 = '\@(\w+->\S+)\(\)';  # Special RST handling for structs with func ptr params
2261da177e4SLinus Torvaldsmy $type_env = '(\$\w+)';
227df31175bSPaolo Bonzinimy $type_enum = '\&(enum\s*([_\w]+))';
228df31175bSPaolo Bonzinimy $type_struct = '\&(struct\s*([_\w]+))';
229df31175bSPaolo Bonzinimy $type_typedef = '\&(typedef\s*([_\w]+))';
230df31175bSPaolo Bonzinimy $type_union = '\&(union\s*([_\w]+))';
2315267dd35SPaolo Bonzinimy $type_member = '\&([_\w]+)(\.|->)([_\w]+)';
232df31175bSPaolo Bonzinimy $type_fallback = '\&([_\w]+)';
233f3341dcfSJani Nikulamy $type_member_func = $type_member . '\(\)';
2341da177e4SLinus Torvalds
2351da177e4SLinus Torvalds# Output conversion substitutions.
2361da177e4SLinus Torvalds#  One for each output format
2371da177e4SLinus Torvalds
2381da177e4SLinus Torvalds# these are pretty rough
2394d732701SDanilo Cesar Lemes de Paulamy @highlights_man = (
2404d732701SDanilo Cesar Lemes de Paula                      [$type_constant, "\$1"],
241b97f193aSMauro Carvalho Chehab                      [$type_constant2, "\$1"],
2424d732701SDanilo Cesar Lemes de Paula                      [$type_func, "\\\\fB\$1\\\\fP"],
243df31175bSPaolo Bonzini                      [$type_enum, "\\\\fI\$1\\\\fP"],
2444d732701SDanilo Cesar Lemes de Paula                      [$type_struct, "\\\\fI\$1\\\\fP"],
245df31175bSPaolo Bonzini                      [$type_typedef, "\\\\fI\$1\\\\fP"],
246df31175bSPaolo Bonzini                      [$type_union, "\\\\fI\$1\\\\fP"],
2475267dd35SPaolo Bonzini                      [$type_param, "\\\\fI\$1\\\\fP"],
248ee2aa759SMauro Carvalho Chehab                      [$type_param_ref, "\\\\fI\$1\$2\\\\fP"],
249df31175bSPaolo Bonzini                      [$type_member, "\\\\fI\$1\$2\$3\\\\fP"],
250df31175bSPaolo Bonzini                      [$type_fallback, "\\\\fI\$1\\\\fP"]
2514d732701SDanilo Cesar Lemes de Paula		     );
2521da177e4SLinus Torvaldsmy $blankline_man = "";
2531da177e4SLinus Torvalds
254c0d1b6eeSJonathan Corbet# rst-mode
255c0d1b6eeSJonathan Corbetmy @highlights_rst = (
256c0d1b6eeSJonathan Corbet                       [$type_constant, "``\$1``"],
257b97f193aSMauro Carvalho Chehab                       [$type_constant2, "``\$1``"],
258f3341dcfSJani Nikula                       # Note: need to escape () to avoid func matching later
2595267dd35SPaolo Bonzini                       [$type_member_func, "\\:c\\:type\\:`\$1\$2\$3\\\\(\\\\) <\$1>`"],
2605267dd35SPaolo Bonzini                       [$type_member, "\\:c\\:type\\:`\$1\$2\$3 <\$1>`"],
2615219f18aSJonathan Corbet		       [$type_fp_param, "**\$1\\\\(\\\\)**"],
262346282dbSMauro Carvalho Chehab		       [$type_fp_param2, "**\$1\\\\(\\\\)**"],
263344fdb28SJonathan Corbet                       [$type_func, "\$1()"],
264df31175bSPaolo Bonzini                       [$type_enum, "\\:c\\:type\\:`\$1 <\$2>`"],
265df31175bSPaolo Bonzini                       [$type_struct, "\\:c\\:type\\:`\$1 <\$2>`"],
266df31175bSPaolo Bonzini                       [$type_typedef, "\\:c\\:type\\:`\$1 <\$2>`"],
267df31175bSPaolo Bonzini                       [$type_union, "\\:c\\:type\\:`\$1 <\$2>`"],
268a7291e7eSJani Nikula                       # in rst this can refer to any type
269df31175bSPaolo Bonzini                       [$type_fallback, "\\:c\\:type\\:`\$1`"],
270ee2aa759SMauro Carvalho Chehab                       [$type_param_ref, "**\$1\$2**"]
271c0d1b6eeSJonathan Corbet		      );
272c0d1b6eeSJonathan Corbetmy $blankline_rst = "\n";
273c0d1b6eeSJonathan Corbet
2741da177e4SLinus Torvalds# read arguments
2751da177e4SLinus Torvaldsif ($#ARGV == -1) {
2761da177e4SLinus Torvalds    usage();
2771da177e4SLinus Torvalds}
2781da177e4SLinus Torvalds
2798484baaaSRandy Dunlapmy $kernelversion;
28093351d41SMauro Carvalho Chehabmy ($sphinx_major, $sphinx_minor, $sphinx_patch);
281efa44475SMauro Carvalho Chehab
2828484baaaSRandy Dunlapmy $dohighlight = "";
2838484baaaSRandy Dunlap
2841da177e4SLinus Torvaldsmy $verbose = 0;
2852c12c810SPierre-Louis Bossartmy $Werror = 0;
286bdfe2be3SMauro Carvalho Chehabmy $output_mode = "rst";
287e314ba31SDaniel Santosmy $output_preformatted = 0;
2884b44595aSJohannes Bergmy $no_doc_sections = 0;
2890b0f5f29SDaniel Vettermy $enable_lineno = 0;
290bdfe2be3SMauro Carvalho Chehabmy @highlights = @highlights_rst;
291bdfe2be3SMauro Carvalho Chehabmy $blankline = $blankline_rst;
2921da177e4SLinus Torvaldsmy $modulename = "Kernel API";
293b6c3f456SJani Nikula
294b6c3f456SJani Nikulause constant {
295b6c3f456SJani Nikula    OUTPUT_ALL          => 0, # output all symbols and doc sections
296b6c3f456SJani Nikula    OUTPUT_INCLUDE      => 1, # output only specified symbols
297eab795ddSMauro Carvalho Chehab    OUTPUT_EXPORTED     => 2, # output exported symbols
298eab795ddSMauro Carvalho Chehab    OUTPUT_INTERNAL     => 3, # output non-exported symbols
299b6c3f456SJani Nikula};
300b6c3f456SJani Nikulamy $output_selection = OUTPUT_ALL;
301b0d60bfbSJonathan Corbetmy $show_not_found = 0;	# No longer used
302b2c4105bSBen Hutchings
30388c2b57dSJani Nikulamy @export_file_list;
30488c2b57dSJani Nikula
305b2c4105bSBen Hutchingsmy @build_time;
306b2c4105bSBen Hutchingsif (defined($ENV{'KBUILD_BUILD_TIMESTAMP'}) &&
307b2c4105bSBen Hutchings    (my $seconds = `date -d"${ENV{'KBUILD_BUILD_TIMESTAMP'}}" +%s`) ne '') {
308b2c4105bSBen Hutchings    @build_time = gmtime($seconds);
309b2c4105bSBen Hutchings} else {
310b2c4105bSBen Hutchings    @build_time = localtime;
311b2c4105bSBen Hutchings}
312b2c4105bSBen Hutchings
3131da177e4SLinus Torvaldsmy $man_date = ('January', 'February', 'March', 'April', 'May', 'June',
3141da177e4SLinus Torvalds		'July', 'August', 'September', 'October',
315b2c4105bSBen Hutchings		'November', 'December')[$build_time[4]] .
316b2c4105bSBen Hutchings  " " . ($build_time[5]+1900);
3171da177e4SLinus Torvalds
3188484baaaSRandy Dunlap# Essentially these are globals.
319b9d97328SRandy Dunlap# They probably want to be tidied up, made more localised or something.
320b9d97328SRandy Dunlap# CAVEAT EMPTOR!  Some of the others I localised may not want to be, which
3211da177e4SLinus Torvalds# could cause "use of undefined value" or other bugs.
3221da177e4SLinus Torvaldsmy ($function, %function_table, %parametertypes, $declaration_purpose);
323eab795ddSMauro Carvalho Chehabmy %nosymbol_table = ();
3240b0f5f29SDaniel Vettermy $declaration_start_line;
3251da177e4SLinus Torvaldsmy ($type, $declaration_name, $return_type);
3261c32fd0cSIlya Dryomovmy ($newsection, $newcontents, $prototype, $brcount, %source_map);
3271da177e4SLinus Torvalds
328bd0e88e5SRandy Dunlapif (defined($ENV{'KBUILD_VERBOSE'})) {
329bd0e88e5SRandy Dunlap	$verbose = "$ENV{'KBUILD_VERBOSE'}";
330bd0e88e5SRandy Dunlap}
331bd0e88e5SRandy Dunlap
3322c12c810SPierre-Louis Bossartif (defined($ENV{'KDOC_WERROR'})) {
3332c12c810SPierre-Louis Bossart	$Werror = "$ENV{'KDOC_WERROR'}";
3342c12c810SPierre-Louis Bossart}
3352c12c810SPierre-Louis Bossart
3362c12c810SPierre-Louis Bossartif (defined($ENV{'KCFLAGS'})) {
3372c12c810SPierre-Louis Bossart	my $kcflags = "$ENV{'KCFLAGS'}";
3382c12c810SPierre-Louis Bossart
3392c12c810SPierre-Louis Bossart	if ($kcflags =~ /Werror/) {
3402c12c810SPierre-Louis Bossart		$Werror = 1;
3412c12c810SPierre-Louis Bossart	}
3422c12c810SPierre-Louis Bossart}
3432c12c810SPierre-Louis Bossart
3441da177e4SLinus Torvalds# Generated docbook code is inserted in a template at a point where
3451da177e4SLinus Torvalds# docbook v3.1 requires a non-zero sequence of RefEntry's; see:
34693431e06SAlexander A. Klimov# https://www.oasis-open.org/docbook/documentation/reference/html/refentry.html
3471da177e4SLinus Torvalds# We keep track of number of generated entries and generate a dummy
3481da177e4SLinus Torvalds# if needs be to ensure the expanded template can be postprocessed
3491da177e4SLinus Torvalds# into html.
3501da177e4SLinus Torvaldsmy $section_counter = 0;
3511da177e4SLinus Torvalds
3521da177e4SLinus Torvaldsmy $lineprefix="";
3531da177e4SLinus Torvalds
35448af606aSJani Nikula# Parser states
35548af606aSJani Nikulause constant {
35648af606aSJani Nikula    STATE_NORMAL        => 0,        # normal code
35748af606aSJani Nikula    STATE_NAME          => 1,        # looking for function name
35817b78717SJonathan Corbet    STATE_BODY_MAYBE    => 2,        # body - or maybe more description
35917b78717SJonathan Corbet    STATE_BODY          => 3,        # the body of the comment
3600d55d48bSMauro Carvalho Chehab    STATE_BODY_WITH_BLANK_LINE => 4, # the body, which has a blank line
3610d55d48bSMauro Carvalho Chehab    STATE_PROTO         => 5,        # scanning prototype
3620d55d48bSMauro Carvalho Chehab    STATE_DOCBLOCK      => 6,        # documentation block
3630d55d48bSMauro Carvalho Chehab    STATE_INLINE        => 7,        # gathering doc outside main block
36448af606aSJani Nikula};
3651da177e4SLinus Torvaldsmy $state;
366850622dfSRandy Dunlapmy $in_doc_sect;
367d742f24dSJonathan Corbetmy $leading_space;
3681da177e4SLinus Torvalds
36948af606aSJani Nikula# Inline documentation state
37048af606aSJani Nikulause constant {
37148af606aSJani Nikula    STATE_INLINE_NA     => 0, # not applicable ($state != STATE_INLINE)
37248af606aSJani Nikula    STATE_INLINE_NAME   => 1, # looking for member name (@foo:)
37348af606aSJani Nikula    STATE_INLINE_TEXT   => 2, # looking for member documentation
37448af606aSJani Nikula    STATE_INLINE_END    => 3, # done
37548af606aSJani Nikula    STATE_INLINE_ERROR  => 4, # error - Comment without header was found.
37648af606aSJani Nikula                              # Spit a warning as it's not
377a4c6ebedSDanilo Cesar Lemes de Paula                              # proper kernel-doc and ignore the rest.
37848af606aSJani Nikula};
37948af606aSJani Nikulamy $inline_doc_state;
380a4c6ebedSDanilo Cesar Lemes de Paula
3811da177e4SLinus Torvalds#declaration types: can be
3821da177e4SLinus Torvalds# 'function', 'struct', 'union', 'enum', 'typedef'
3831da177e4SLinus Torvaldsmy $decl_type;
3841da177e4SLinus Torvalds
3851da177e4SLinus Torvaldsmy $doc_start = '^/\*\*\s*$'; # Allow whitespace at end of comment start.
3861da177e4SLinus Torvaldsmy $doc_end = '\*/';
3871da177e4SLinus Torvaldsmy $doc_com = '\s*\*\s*';
38812ae6779SDaniel Santosmy $doc_com_body = '\s*\* ?';
3891da177e4SLinus Torvaldsmy $doc_decl = $doc_com . '(\w+)';
390f624adefSJani Nikula# @params and a strictly limited set of supported section names
3918569de68SJonathan Corbetmy $doc_sect = $doc_com .
392ef00028bSJonathan Corbet    '\s*(\@[.\w]+|\@\.\.\.|description|context|returns?|notes?|examples?)\s*:(.*)';
39312ae6779SDaniel Santosmy $doc_content = $doc_com_body . '(.*)';
3941da177e4SLinus Torvaldsmy $doc_block = $doc_com . 'DOC:\s*(.*)?';
39548af606aSJani Nikulamy $doc_inline_start = '^\s*/\*\*\s*$';
396fe7bc493SMauro Carvalho Chehabmy $doc_inline_sect = '\s*\*\s*(@\s*[\w][\w\.]*\s*):(.*)';
39748af606aSJani Nikulamy $doc_inline_end = '^\s*\*/\s*$';
3980c9aa209SJani Nikulamy $doc_inline_oneline = '^\s*/\*\*\s*(@[\w\s]+):\s*(.*)\s*\*/\s*$';
39986ae2e38SJani Nikulamy $export_symbol = '^\s*EXPORT_SYMBOL(_GPL)?\s*\(\s*(\w+)\s*\)\s*;';
4001da177e4SLinus Torvalds
4011da177e4SLinus Torvaldsmy %parameterdescs;
4020b0f5f29SDaniel Vettermy %parameterdesc_start_lines;
4031da177e4SLinus Torvaldsmy @parameterlist;
4041da177e4SLinus Torvaldsmy %sections;
4051da177e4SLinus Torvaldsmy @sectionlist;
4060b0f5f29SDaniel Vettermy %section_start_lines;
407a1d94aa5SRandy Dunlapmy $sectcheck;
408a1d94aa5SRandy Dunlapmy $struct_actual;
4091da177e4SLinus Torvalds
4101da177e4SLinus Torvaldsmy $contents = "";
4110b0f5f29SDaniel Vettermy $new_start_line = 0;
412f624adefSJani Nikula
413f624adefSJani Nikula# the canonical section names. see also $doc_sect above.
4141da177e4SLinus Torvaldsmy $section_default = "Description";	# default section
4151da177e4SLinus Torvaldsmy $section_intro = "Introduction";
4161da177e4SLinus Torvaldsmy $section = $section_default;
4171da177e4SLinus Torvaldsmy $section_context = "Context";
4184092bac7SYacine Belkadimy $section_return = "Return";
4191da177e4SLinus Torvalds
4201da177e4SLinus Torvaldsmy $undescribed = "-- undescribed --";
4211da177e4SLinus Torvalds
4221da177e4SLinus Torvaldsreset_state();
4231da177e4SLinus Torvalds
424b031ac4eSMauro Carvalho Chehabwhile ($ARGV[0] =~ m/^--?(.*)/) {
425b031ac4eSMauro Carvalho Chehab    my $cmd = $1;
426b031ac4eSMauro Carvalho Chehab    shift @ARGV;
427b031ac4eSMauro Carvalho Chehab    if ($cmd eq "man") {
4281da177e4SLinus Torvalds	$output_mode = "man";
4294d732701SDanilo Cesar Lemes de Paula	@highlights = @highlights_man;
4301da177e4SLinus Torvalds	$blankline = $blankline_man;
431b031ac4eSMauro Carvalho Chehab    } elsif ($cmd eq "rst") {
432c0d1b6eeSJonathan Corbet	$output_mode = "rst";
433c0d1b6eeSJonathan Corbet	@highlights = @highlights_rst;
434c0d1b6eeSJonathan Corbet	$blankline = $blankline_rst;
435b031ac4eSMauro Carvalho Chehab    } elsif ($cmd eq "none") {
4363a025e1dSMatthew Wilcox	$output_mode = "none";
437b031ac4eSMauro Carvalho Chehab    } elsif ($cmd eq "module") { # not needed for XML, inherits from calling document
4381da177e4SLinus Torvalds	$modulename = shift @ARGV;
439b031ac4eSMauro Carvalho Chehab    } elsif ($cmd eq "function") { # to only output specific functions
440b6c3f456SJani Nikula	$output_selection = OUTPUT_INCLUDE;
4411da177e4SLinus Torvalds	$function = shift @ARGV;
4421da177e4SLinus Torvalds	$function_table{$function} = 1;
443eab795ddSMauro Carvalho Chehab    } elsif ($cmd eq "nosymbol") { # Exclude specific symbols
444eab795ddSMauro Carvalho Chehab	my $symbol = shift @ARGV;
445eab795ddSMauro Carvalho Chehab	$nosymbol_table{$symbol} = 1;
446b031ac4eSMauro Carvalho Chehab    } elsif ($cmd eq "export") { # only exported symbols
447b6c3f456SJani Nikula	$output_selection = OUTPUT_EXPORTED;
448da9726ecSJani Nikula	%function_table = ();
449b031ac4eSMauro Carvalho Chehab    } elsif ($cmd eq "internal") { # only non-exported symbols
450b6c3f456SJani Nikula	$output_selection = OUTPUT_INTERNAL;
451da9726ecSJani Nikula	%function_table = ();
452b031ac4eSMauro Carvalho Chehab    } elsif ($cmd eq "export-file") {
45388c2b57dSJani Nikula	my $file = shift @ARGV;
45488c2b57dSJani Nikula	push(@export_file_list, $file);
455b031ac4eSMauro Carvalho Chehab    } elsif ($cmd eq "v") {
4561da177e4SLinus Torvalds	$verbose = 1;
4572c12c810SPierre-Louis Bossart    } elsif ($cmd eq "Werror") {
4582c12c810SPierre-Louis Bossart	$Werror = 1;
459b031ac4eSMauro Carvalho Chehab    } elsif (($cmd eq "h") || ($cmd eq "help")) {
4601da177e4SLinus Torvalds	usage();
461b031ac4eSMauro Carvalho Chehab    } elsif ($cmd eq 'no-doc-sections') {
4624b44595aSJohannes Berg	    $no_doc_sections = 1;
463b031ac4eSMauro Carvalho Chehab    } elsif ($cmd eq 'enable-lineno') {
4640b0f5f29SDaniel Vetter	    $enable_lineno = 1;
465b031ac4eSMauro Carvalho Chehab    } elsif ($cmd eq 'show-not-found') {
466b0d60bfbSJonathan Corbet	$show_not_found = 1;  # A no-op but don't fail
46793351d41SMauro Carvalho Chehab    } elsif ($cmd eq "sphinx-version") {
46893351d41SMauro Carvalho Chehab	my $ver_string = shift @ARGV;
46993351d41SMauro Carvalho Chehab	if ($ver_string =~ m/^(\d+)(\.\d+)?(\.\d+)?/) {
47093351d41SMauro Carvalho Chehab	    $sphinx_major = $1;
47193351d41SMauro Carvalho Chehab	    if (defined($2)) {
47293351d41SMauro Carvalho Chehab		$sphinx_minor = substr($2,1);
47393351d41SMauro Carvalho Chehab	    } else {
47493351d41SMauro Carvalho Chehab		$sphinx_minor = 0;
47593351d41SMauro Carvalho Chehab	    }
47693351d41SMauro Carvalho Chehab	    if (defined($3)) {
47793351d41SMauro Carvalho Chehab		$sphinx_patch = substr($3,1)
47893351d41SMauro Carvalho Chehab	    } else {
47993351d41SMauro Carvalho Chehab		$sphinx_patch = 0;
48093351d41SMauro Carvalho Chehab	    }
48193351d41SMauro Carvalho Chehab	} else {
48293351d41SMauro Carvalho Chehab	    die "Sphinx version should either major.minor or major.minor.patch format\n";
48393351d41SMauro Carvalho Chehab	}
484b031ac4eSMauro Carvalho Chehab    } else {
485b031ac4eSMauro Carvalho Chehab	# Unknown argument
486b031ac4eSMauro Carvalho Chehab        usage();
4871da177e4SLinus Torvalds    }
4881da177e4SLinus Torvalds}
4891da177e4SLinus Torvalds
4908484baaaSRandy Dunlap# continue execution near EOF;
4918484baaaSRandy Dunlap
492efa44475SMauro Carvalho Chehab# The C domain dialect changed on Sphinx 3. So, we need to check the
493efa44475SMauro Carvalho Chehab# version in order to produce the right tags.
494efa44475SMauro Carvalho Chehabsub findprog($)
495efa44475SMauro Carvalho Chehab{
496efa44475SMauro Carvalho Chehab	foreach(split(/:/, $ENV{PATH})) {
497efa44475SMauro Carvalho Chehab		return "$_/$_[0]" if(-x "$_/$_[0]");
498efa44475SMauro Carvalho Chehab	}
499efa44475SMauro Carvalho Chehab}
500efa44475SMauro Carvalho Chehab
501efa44475SMauro Carvalho Chehabsub get_sphinx_version()
502efa44475SMauro Carvalho Chehab{
503efa44475SMauro Carvalho Chehab	my $ver;
504efa44475SMauro Carvalho Chehab
505efa44475SMauro Carvalho Chehab	my $cmd = "sphinx-build";
506efa44475SMauro Carvalho Chehab	if (!findprog($cmd)) {
507efa44475SMauro Carvalho Chehab		my $cmd = "sphinx-build3";
50893351d41SMauro Carvalho Chehab		if (!findprog($cmd)) {
50993351d41SMauro Carvalho Chehab			$sphinx_major = 1;
51093351d41SMauro Carvalho Chehab			$sphinx_minor = 2;
51193351d41SMauro Carvalho Chehab			$sphinx_patch = 0;
51293351d41SMauro Carvalho Chehab			printf STDERR "Warning: Sphinx version not found. Using default (Sphinx version %d.%d.%d)\n",
51393351d41SMauro Carvalho Chehab			       $sphinx_major, $sphinx_minor, $sphinx_patch;
51493351d41SMauro Carvalho Chehab			return;
51593351d41SMauro Carvalho Chehab		}
516efa44475SMauro Carvalho Chehab	}
517efa44475SMauro Carvalho Chehab
518efa44475SMauro Carvalho Chehab	open IN, "$cmd --version 2>&1 |";
519efa44475SMauro Carvalho Chehab	while (<IN>) {
520efa44475SMauro Carvalho Chehab		if (m/^\s*sphinx-build\s+([\d]+)\.([\d\.]+)(\+\/[\da-f]+)?$/) {
52193351d41SMauro Carvalho Chehab			$sphinx_major = $1;
52293351d41SMauro Carvalho Chehab			$sphinx_minor = $2;
52393351d41SMauro Carvalho Chehab			$sphinx_patch = $3;
524efa44475SMauro Carvalho Chehab			last;
525efa44475SMauro Carvalho Chehab		}
526efa44475SMauro Carvalho Chehab		# Sphinx 1.2.x uses a different format
527efa44475SMauro Carvalho Chehab		if (m/^\s*Sphinx.*\s+([\d]+)\.([\d\.]+)$/) {
52893351d41SMauro Carvalho Chehab			$sphinx_major = $1;
52993351d41SMauro Carvalho Chehab			$sphinx_minor = $2;
53093351d41SMauro Carvalho Chehab			$sphinx_patch = $3;
531efa44475SMauro Carvalho Chehab			last;
532efa44475SMauro Carvalho Chehab		}
533efa44475SMauro Carvalho Chehab	}
534efa44475SMauro Carvalho Chehab	close IN;
535efa44475SMauro Carvalho Chehab}
536efa44475SMauro Carvalho Chehab
53753f049faSBorislav Petkov# get kernel version from env
53853f049faSBorislav Petkovsub get_kernel_version() {
5391b9bc22dSJohannes Berg    my $version = 'unknown kernel version';
54053f049faSBorislav Petkov
54153f049faSBorislav Petkov    if (defined($ENV{'KERNELVERSION'})) {
54253f049faSBorislav Petkov	$version = $ENV{'KERNELVERSION'};
54353f049faSBorislav Petkov    }
54453f049faSBorislav Petkov    return $version;
54553f049faSBorislav Petkov}
5461da177e4SLinus Torvalds
5470b0f5f29SDaniel Vetter#
5480b0f5f29SDaniel Vettersub print_lineno {
5490b0f5f29SDaniel Vetter    my $lineno = shift;
5500b0f5f29SDaniel Vetter    if ($enable_lineno && defined($lineno)) {
5510b0f5f29SDaniel Vetter        print "#define LINENO " . $lineno . "\n";
5520b0f5f29SDaniel Vetter    }
5530b0f5f29SDaniel Vetter}
5541da177e4SLinus Torvalds##
5551da177e4SLinus Torvalds# dumps section contents to arrays/hashes intended for that purpose.
5561da177e4SLinus Torvalds#
5571da177e4SLinus Torvaldssub dump_section {
55894dc7ad5SRandy Dunlap    my $file = shift;
5591da177e4SLinus Torvalds    my $name = shift;
5601da177e4SLinus Torvalds    my $contents = join "\n", @_;
5611da177e4SLinus Torvalds
56213901ef2SJani Nikula    if ($name =~ m/$type_param/) {
5631da177e4SLinus Torvalds	$name = $1;
5641da177e4SLinus Torvalds	$parameterdescs{$name} = $contents;
565a1d94aa5SRandy Dunlap	$sectcheck = $sectcheck . $name . " ";
5660b0f5f29SDaniel Vetter        $parameterdesc_start_lines{$name} = $new_start_line;
5670b0f5f29SDaniel Vetter        $new_start_line = 0;
568ced69090SRandy Dunlap    } elsif ($name eq "@\.\.\.") {
569ced69090SRandy Dunlap	$name = "...";
570ced69090SRandy Dunlap	$parameterdescs{$name} = $contents;
571a1d94aa5SRandy Dunlap	$sectcheck = $sectcheck . $name . " ";
5720b0f5f29SDaniel Vetter        $parameterdesc_start_lines{$name} = $new_start_line;
5730b0f5f29SDaniel Vetter        $new_start_line = 0;
5741da177e4SLinus Torvalds    } else {
57594dc7ad5SRandy Dunlap	if (defined($sections{$name}) && ($sections{$name} ne "")) {
57695b6be9dSJani Nikula	    # Only warn on user specified duplicate section names.
57795b6be9dSJani Nikula	    if ($name ne $section_default) {
57832217761SJani Nikula		print STDERR "${file}:$.: warning: duplicate section name '$name'\n";
57932217761SJani Nikula		++$warnings;
58095b6be9dSJani Nikula	    }
58132217761SJani Nikula	    $sections{$name} .= $contents;
58232217761SJani Nikula	} else {
5831da177e4SLinus Torvalds	    $sections{$name} = $contents;
5841da177e4SLinus Torvalds	    push @sectionlist, $name;
5850b0f5f29SDaniel Vetter            $section_start_lines{$name} = $new_start_line;
5860b0f5f29SDaniel Vetter            $new_start_line = 0;
5871da177e4SLinus Torvalds	}
5881da177e4SLinus Torvalds    }
58932217761SJani Nikula}
5901da177e4SLinus Torvalds
5911da177e4SLinus Torvalds##
592b112e0f7SJohannes Berg# dump DOC: section after checking that it should go out
593b112e0f7SJohannes Berg#
594b112e0f7SJohannes Bergsub dump_doc_section {
59594dc7ad5SRandy Dunlap    my $file = shift;
596b112e0f7SJohannes Berg    my $name = shift;
597b112e0f7SJohannes Berg    my $contents = join "\n", @_;
598b112e0f7SJohannes Berg
5994b44595aSJohannes Berg    if ($no_doc_sections) {
6004b44595aSJohannes Berg        return;
6014b44595aSJohannes Berg    }
6024b44595aSJohannes Berg
603eab795ddSMauro Carvalho Chehab    return if (defined($nosymbol_table{$name}));
604eab795ddSMauro Carvalho Chehab
605b6c3f456SJani Nikula    if (($output_selection == OUTPUT_ALL) ||
606eab795ddSMauro Carvalho Chehab	(($output_selection == OUTPUT_INCLUDE) &&
607eab795ddSMauro Carvalho Chehab	 defined($function_table{$name})))
608b112e0f7SJohannes Berg    {
60994dc7ad5SRandy Dunlap	dump_section($file, $name, $contents);
610b112e0f7SJohannes Berg	output_blockhead({'sectionlist' => \@sectionlist,
611b112e0f7SJohannes Berg			  'sections' => \%sections,
612b112e0f7SJohannes Berg			  'module' => $modulename,
613b6c3f456SJani Nikula			  'content-only' => ($output_selection != OUTPUT_ALL), });
614b112e0f7SJohannes Berg    }
615b112e0f7SJohannes Berg}
616b112e0f7SJohannes Berg
617b112e0f7SJohannes Berg##
6181da177e4SLinus Torvalds# output function
6191da177e4SLinus Torvalds#
6201da177e4SLinus Torvalds# parameterdescs, a hash.
6211da177e4SLinus Torvalds#  function => "function name"
6221da177e4SLinus Torvalds#  parameterlist => @list of parameters
6231da177e4SLinus Torvalds#  parameterdescs => %parameter descriptions
6241da177e4SLinus Torvalds#  sectionlist => @list of sections
625a21217daSRandy Dunlap#  sections => %section descriptions
6261da177e4SLinus Torvalds#
6271da177e4SLinus Torvalds
6281da177e4SLinus Torvaldssub output_highlight {
6291da177e4SLinus Torvalds    my $contents = join "\n",@_;
6301da177e4SLinus Torvalds    my $line;
6311da177e4SLinus Torvalds
6321da177e4SLinus Torvalds#   DEBUG
6331da177e4SLinus Torvalds#   if (!defined $contents) {
6341da177e4SLinus Torvalds#	use Carp;
6351da177e4SLinus Torvalds#	confess "output_highlight got called with no args?\n";
6361da177e4SLinus Torvalds#   }
6371da177e4SLinus Torvalds
6383eb014a1SRandy Dunlap#   print STDERR "contents b4:$contents\n";
6391da177e4SLinus Torvalds    eval $dohighlight;
6401da177e4SLinus Torvalds    die $@ if $@;
6413eb014a1SRandy Dunlap#   print STDERR "contents af:$contents\n";
6423eb014a1SRandy Dunlap
6431da177e4SLinus Torvalds    foreach $line (split "\n", $contents) {
64412ae6779SDaniel Santos	if (! $output_preformatted) {
64512ae6779SDaniel Santos	    $line =~ s/^\s*//;
64612ae6779SDaniel Santos	}
6471da177e4SLinus Torvalds	if ($line eq ""){
648e314ba31SDaniel Santos	    if (! $output_preformatted) {
6490bba924cSJonathan Corbet		print $lineprefix, $blankline;
650e314ba31SDaniel Santos	    }
6511da177e4SLinus Torvalds	} else {
652cdccb316SRandy Dunlap	    if ($output_mode eq "man" && substr($line, 0, 1) eq ".") {
653cdccb316SRandy Dunlap		print "\\&$line";
654cdccb316SRandy Dunlap	    } else {
6551da177e4SLinus Torvalds		print $lineprefix, $line;
6561da177e4SLinus Torvalds	    }
657cdccb316SRandy Dunlap	}
6581da177e4SLinus Torvalds	print "\n";
6591da177e4SLinus Torvalds    }
6601da177e4SLinus Torvalds}
6611da177e4SLinus Torvalds
6621da177e4SLinus Torvalds##
6631da177e4SLinus Torvalds# output function in man
6641da177e4SLinus Torvaldssub output_function_man(%) {
6651da177e4SLinus Torvalds    my %args = %{$_[0]};
6661da177e4SLinus Torvalds    my ($parameter, $section);
6671da177e4SLinus Torvalds    my $count;
6681da177e4SLinus Torvalds
6691da177e4SLinus Torvalds    print ".TH \"$args{'function'}\" 9 \"$args{'function'}\" \"$man_date\" \"Kernel Hacker's Manual\" LINUX\n";
6701da177e4SLinus Torvalds
6711da177e4SLinus Torvalds    print ".SH NAME\n";
6721da177e4SLinus Torvalds    print $args{'function'} . " \\- " . $args{'purpose'} . "\n";
6731da177e4SLinus Torvalds
6741da177e4SLinus Torvalds    print ".SH SYNOPSIS\n";
675a21217daSRandy Dunlap    if ($args{'functiontype'} ne "") {
6761da177e4SLinus Torvalds	print ".B \"" . $args{'functiontype'} . "\" " . $args{'function'} . "\n";
677a21217daSRandy Dunlap    } else {
678a21217daSRandy Dunlap	print ".B \"" . $args{'function'} . "\n";
679a21217daSRandy Dunlap    }
6801da177e4SLinus Torvalds    $count = 0;
6811da177e4SLinus Torvalds    my $parenth = "(";
6821da177e4SLinus Torvalds    my $post = ",";
6831da177e4SLinus Torvalds    foreach my $parameter (@{$args{'parameterlist'}}) {
6841da177e4SLinus Torvalds	if ($count == $#{$args{'parameterlist'}}) {
6851da177e4SLinus Torvalds	    $post = ");";
6861da177e4SLinus Torvalds	}
6871da177e4SLinus Torvalds	$type = $args{'parametertypes'}{$parameter};
6881da177e4SLinus Torvalds	if ($type =~ m/([^\(]*\(\*)\s*\)\s*\(([^\)]*)\)/) {
6891da177e4SLinus Torvalds	    # pointer-to-function
690ed8348e2SMauro Carvalho Chehab	    print ".BI \"" . $parenth . $1 . "\" " . " \") (" . $2 . ")" . $post . "\"\n";
6911da177e4SLinus Torvalds	} else {
6921da177e4SLinus Torvalds	    $type =~ s/([^\*])$/$1 /;
693ed8348e2SMauro Carvalho Chehab	    print ".BI \"" . $parenth . $type . "\" " . " \"" . $post . "\"\n";
6941da177e4SLinus Torvalds	}
6951da177e4SLinus Torvalds	$count++;
6961da177e4SLinus Torvalds	$parenth = "";
6971da177e4SLinus Torvalds    }
6981da177e4SLinus Torvalds
6991da177e4SLinus Torvalds    print ".SH ARGUMENTS\n";
7001da177e4SLinus Torvalds    foreach $parameter (@{$args{'parameterlist'}}) {
7011da177e4SLinus Torvalds	my $parameter_name = $parameter;
7021da177e4SLinus Torvalds	$parameter_name =~ s/\[.*//;
7031da177e4SLinus Torvalds
7041da177e4SLinus Torvalds	print ".IP \"" . $parameter . "\" 12\n";
7051da177e4SLinus Torvalds	output_highlight($args{'parameterdescs'}{$parameter_name});
7061da177e4SLinus Torvalds    }
7071da177e4SLinus Torvalds    foreach $section (@{$args{'sectionlist'}}) {
7081da177e4SLinus Torvalds	print ".SH \"", uc $section, "\"\n";
7091da177e4SLinus Torvalds	output_highlight($args{'sections'}{$section});
7101da177e4SLinus Torvalds    }
7111da177e4SLinus Torvalds}
7121da177e4SLinus Torvalds
7131da177e4SLinus Torvalds##
7141da177e4SLinus Torvalds# output enum in man
7151da177e4SLinus Torvaldssub output_enum_man(%) {
7161da177e4SLinus Torvalds    my %args = %{$_[0]};
7171da177e4SLinus Torvalds    my ($parameter, $section);
7181da177e4SLinus Torvalds    my $count;
7191da177e4SLinus Torvalds
7201da177e4SLinus Torvalds    print ".TH \"$args{'module'}\" 9 \"enum $args{'enum'}\" \"$man_date\" \"API Manual\" LINUX\n";
7211da177e4SLinus Torvalds
7221da177e4SLinus Torvalds    print ".SH NAME\n";
7231da177e4SLinus Torvalds    print "enum " . $args{'enum'} . " \\- " . $args{'purpose'} . "\n";
7241da177e4SLinus Torvalds
7251da177e4SLinus Torvalds    print ".SH SYNOPSIS\n";
7261da177e4SLinus Torvalds    print "enum " . $args{'enum'} . " {\n";
7271da177e4SLinus Torvalds    $count = 0;
7281da177e4SLinus Torvalds    foreach my $parameter (@{$args{'parameterlist'}}) {
7291da177e4SLinus Torvalds	print ".br\n.BI \"    $parameter\"\n";
7301da177e4SLinus Torvalds	if ($count == $#{$args{'parameterlist'}}) {
7311da177e4SLinus Torvalds	    print "\n};\n";
7321da177e4SLinus Torvalds	    last;
7331da177e4SLinus Torvalds	}
7341da177e4SLinus Torvalds	else {
7351da177e4SLinus Torvalds	    print ", \n.br\n";
7361da177e4SLinus Torvalds	}
7371da177e4SLinus Torvalds	$count++;
7381da177e4SLinus Torvalds    }
7391da177e4SLinus Torvalds
7401da177e4SLinus Torvalds    print ".SH Constants\n";
7411da177e4SLinus Torvalds    foreach $parameter (@{$args{'parameterlist'}}) {
7421da177e4SLinus Torvalds	my $parameter_name = $parameter;
7431da177e4SLinus Torvalds	$parameter_name =~ s/\[.*//;
7441da177e4SLinus Torvalds
7451da177e4SLinus Torvalds	print ".IP \"" . $parameter . "\" 12\n";
7461da177e4SLinus Torvalds	output_highlight($args{'parameterdescs'}{$parameter_name});
7471da177e4SLinus Torvalds    }
7481da177e4SLinus Torvalds    foreach $section (@{$args{'sectionlist'}}) {
7491da177e4SLinus Torvalds	print ".SH \"$section\"\n";
7501da177e4SLinus Torvalds	output_highlight($args{'sections'}{$section});
7511da177e4SLinus Torvalds    }
7521da177e4SLinus Torvalds}
7531da177e4SLinus Torvalds
7541da177e4SLinus Torvalds##
7551da177e4SLinus Torvalds# output struct in man
7561da177e4SLinus Torvaldssub output_struct_man(%) {
7571da177e4SLinus Torvalds    my %args = %{$_[0]};
7581da177e4SLinus Torvalds    my ($parameter, $section);
7591da177e4SLinus Torvalds
7601da177e4SLinus Torvalds    print ".TH \"$args{'module'}\" 9 \"" . $args{'type'} . " " . $args{'struct'} . "\" \"$man_date\" \"API Manual\" LINUX\n";
7611da177e4SLinus Torvalds
7621da177e4SLinus Torvalds    print ".SH NAME\n";
7631da177e4SLinus Torvalds    print $args{'type'} . " " . $args{'struct'} . " \\- " . $args{'purpose'} . "\n";
7641da177e4SLinus Torvalds
7658ad72163SMauro Carvalho Chehab    my $declaration = $args{'definition'};
7668ad72163SMauro Carvalho Chehab    $declaration =~ s/\t/  /g;
7678ad72163SMauro Carvalho Chehab    $declaration =~ s/\n/"\n.br\n.BI \"/g;
7681da177e4SLinus Torvalds    print ".SH SYNOPSIS\n";
7691da177e4SLinus Torvalds    print $args{'type'} . " " . $args{'struct'} . " {\n.br\n";
7708ad72163SMauro Carvalho Chehab    print ".BI \"$declaration\n};\n.br\n\n";
7711da177e4SLinus Torvalds
772c51d3dacSRandy Dunlap    print ".SH Members\n";
7731da177e4SLinus Torvalds    foreach $parameter (@{$args{'parameterlist'}}) {
7741da177e4SLinus Torvalds	($parameter =~ /^#/) && next;
7751da177e4SLinus Torvalds
7761da177e4SLinus Torvalds	my $parameter_name = $parameter;
7771da177e4SLinus Torvalds	$parameter_name =~ s/\[.*//;
7781da177e4SLinus Torvalds
7791da177e4SLinus Torvalds	($args{'parameterdescs'}{$parameter_name} ne $undescribed) || next;
7801da177e4SLinus Torvalds	print ".IP \"" . $parameter . "\" 12\n";
7811da177e4SLinus Torvalds	output_highlight($args{'parameterdescs'}{$parameter_name});
7821da177e4SLinus Torvalds    }
7831da177e4SLinus Torvalds    foreach $section (@{$args{'sectionlist'}}) {
7841da177e4SLinus Torvalds	print ".SH \"$section\"\n";
7851da177e4SLinus Torvalds	output_highlight($args{'sections'}{$section});
7861da177e4SLinus Torvalds    }
7871da177e4SLinus Torvalds}
7881da177e4SLinus Torvalds
7891da177e4SLinus Torvalds##
7901da177e4SLinus Torvalds# output typedef in man
7911da177e4SLinus Torvaldssub output_typedef_man(%) {
7921da177e4SLinus Torvalds    my %args = %{$_[0]};
7931da177e4SLinus Torvalds    my ($parameter, $section);
7941da177e4SLinus Torvalds
7951da177e4SLinus Torvalds    print ".TH \"$args{'module'}\" 9 \"$args{'typedef'}\" \"$man_date\" \"API Manual\" LINUX\n";
7961da177e4SLinus Torvalds
7971da177e4SLinus Torvalds    print ".SH NAME\n";
7981da177e4SLinus Torvalds    print "typedef " . $args{'typedef'} . " \\- " . $args{'purpose'} . "\n";
7991da177e4SLinus Torvalds
8001da177e4SLinus Torvalds    foreach $section (@{$args{'sectionlist'}}) {
8011da177e4SLinus Torvalds	print ".SH \"$section\"\n";
8021da177e4SLinus Torvalds	output_highlight($args{'sections'}{$section});
8031da177e4SLinus Torvalds    }
8041da177e4SLinus Torvalds}
8051da177e4SLinus Torvalds
806b112e0f7SJohannes Bergsub output_blockhead_man(%) {
8071da177e4SLinus Torvalds    my %args = %{$_[0]};
8081da177e4SLinus Torvalds    my ($parameter, $section);
8091da177e4SLinus Torvalds    my $count;
8101da177e4SLinus Torvalds
8111da177e4SLinus Torvalds    print ".TH \"$args{'module'}\" 9 \"$args{'module'}\" \"$man_date\" \"API Manual\" LINUX\n";
8121da177e4SLinus Torvalds
8131da177e4SLinus Torvalds    foreach $section (@{$args{'sectionlist'}}) {
8141da177e4SLinus Torvalds	print ".SH \"$section\"\n";
8151da177e4SLinus Torvalds	output_highlight($args{'sections'}{$section});
8161da177e4SLinus Torvalds    }
8171da177e4SLinus Torvalds}
8181da177e4SLinus Torvalds
8191da177e4SLinus Torvalds##
820c0d1b6eeSJonathan Corbet# output in restructured text
821c0d1b6eeSJonathan Corbet#
822c0d1b6eeSJonathan Corbet
823c0d1b6eeSJonathan Corbet#
824c0d1b6eeSJonathan Corbet# This could use some work; it's used to output the DOC: sections, and
825c0d1b6eeSJonathan Corbet# starts by putting out the name of the doc section itself, but that tends
826c0d1b6eeSJonathan Corbet# to duplicate a header already in the template file.
827c0d1b6eeSJonathan Corbet#
828c0d1b6eeSJonathan Corbetsub output_blockhead_rst(%) {
829c0d1b6eeSJonathan Corbet    my %args = %{$_[0]};
830c0d1b6eeSJonathan Corbet    my ($parameter, $section);
831c0d1b6eeSJonathan Corbet
832c0d1b6eeSJonathan Corbet    foreach $section (@{$args{'sectionlist'}}) {
833eab795ddSMauro Carvalho Chehab	next if (defined($nosymbol_table{$section}));
834eab795ddSMauro Carvalho Chehab
8359e72184bSJani Nikula	if ($output_selection != OUTPUT_INCLUDE) {
836c0d1b6eeSJonathan Corbet	    print "**$section**\n\n";
8379e72184bSJani Nikula	}
8380b0f5f29SDaniel Vetter        print_lineno($section_start_lines{$section});
839c0d1b6eeSJonathan Corbet	output_highlight_rst($args{'sections'}{$section});
840c0d1b6eeSJonathan Corbet	print "\n";
841c0d1b6eeSJonathan Corbet    }
842c0d1b6eeSJonathan Corbet}
843c0d1b6eeSJonathan Corbet
844af250290SJonathan Corbet#
845af250290SJonathan Corbet# Apply the RST highlights to a sub-block of text.
846af250290SJonathan Corbet#
847af250290SJonathan Corbetsub highlight_block($) {
848af250290SJonathan Corbet    # The dohighlight kludge requires the text be called $contents
849af250290SJonathan Corbet    my $contents = shift;
850c0d1b6eeSJonathan Corbet    eval $dohighlight;
851c0d1b6eeSJonathan Corbet    die $@ if $@;
852af250290SJonathan Corbet    return $contents;
853af250290SJonathan Corbet}
854c0d1b6eeSJonathan Corbet
855af250290SJonathan Corbet#
856af250290SJonathan Corbet# Regexes used only here.
857af250290SJonathan Corbet#
858af250290SJonathan Corbetmy $sphinx_literal = '^[^.].*::$';
859af250290SJonathan Corbetmy $sphinx_cblock = '^\.\.\ +code-block::';
860af250290SJonathan Corbet
861af250290SJonathan Corbetsub output_highlight_rst {
862af250290SJonathan Corbet    my $input = join "\n",@_;
863af250290SJonathan Corbet    my $output = "";
864af250290SJonathan Corbet    my $line;
865af250290SJonathan Corbet    my $in_literal = 0;
866af250290SJonathan Corbet    my $litprefix;
867af250290SJonathan Corbet    my $block = "";
868af250290SJonathan Corbet
869af250290SJonathan Corbet    foreach $line (split "\n",$input) {
870af250290SJonathan Corbet	#
871af250290SJonathan Corbet	# If we're in a literal block, see if we should drop out
872af250290SJonathan Corbet	# of it.  Otherwise pass the line straight through unmunged.
873af250290SJonathan Corbet	#
874af250290SJonathan Corbet	if ($in_literal) {
875af250290SJonathan Corbet	    if (! ($line =~ /^\s*$/)) {
876af250290SJonathan Corbet		#
877af250290SJonathan Corbet		# If this is the first non-blank line in a literal
878af250290SJonathan Corbet		# block we need to figure out what the proper indent is.
879af250290SJonathan Corbet		#
880af250290SJonathan Corbet		if ($litprefix eq "") {
881af250290SJonathan Corbet		    $line =~ /^(\s*)/;
882af250290SJonathan Corbet		    $litprefix = '^' . $1;
883af250290SJonathan Corbet		    $output .= $line . "\n";
884af250290SJonathan Corbet		} elsif (! ($line =~ /$litprefix/)) {
885af250290SJonathan Corbet		    $in_literal = 0;
886af250290SJonathan Corbet		} else {
887af250290SJonathan Corbet		    $output .= $line . "\n";
888af250290SJonathan Corbet		}
889af250290SJonathan Corbet	    } else {
890af250290SJonathan Corbet		$output .= $line . "\n";
891af250290SJonathan Corbet	    }
892af250290SJonathan Corbet	}
893af250290SJonathan Corbet	#
894af250290SJonathan Corbet	# Not in a literal block (or just dropped out)
895af250290SJonathan Corbet	#
896af250290SJonathan Corbet	if (! $in_literal) {
897af250290SJonathan Corbet	    $block .= $line . "\n";
898af250290SJonathan Corbet	    if (($line =~ /$sphinx_literal/) || ($line =~ /$sphinx_cblock/)) {
899af250290SJonathan Corbet		$in_literal = 1;
900af250290SJonathan Corbet		$litprefix = "";
901af250290SJonathan Corbet		$output .= highlight_block($block);
902af250290SJonathan Corbet		$block = ""
903af250290SJonathan Corbet	    }
904af250290SJonathan Corbet	}
905af250290SJonathan Corbet    }
906af250290SJonathan Corbet
907af250290SJonathan Corbet    if ($block) {
908af250290SJonathan Corbet	$output .= highlight_block($block);
909af250290SJonathan Corbet    }
910af250290SJonathan Corbet    foreach $line (split "\n", $output) {
911830066a7SJani Nikula	print $lineprefix . $line . "\n";
912c0d1b6eeSJonathan Corbet    }
913c0d1b6eeSJonathan Corbet}
914c0d1b6eeSJonathan Corbet
915c0d1b6eeSJonathan Corbetsub output_function_rst(%) {
916c0d1b6eeSJonathan Corbet    my %args = %{$_[0]};
917c0d1b6eeSJonathan Corbet    my ($parameter, $section);
918c099ff69SJani Nikula    my $oldprefix = $lineprefix;
91982801d06SMauro Carvalho Chehab    my $start = "";
920c0d1b6eeSJonathan Corbet
921efa44475SMauro Carvalho Chehab    if ($sphinx_major < 3) {
922e3ad05feSMauro Carvalho Chehab	if ($args{'typedef'}) {
92382801d06SMauro Carvalho Chehab	    print ".. c:type:: ". $args{'function'} . "\n\n";
92482801d06SMauro Carvalho Chehab	    print_lineno($declaration_start_line);
92582801d06SMauro Carvalho Chehab	    print "   **Typedef**: ";
92682801d06SMauro Carvalho Chehab	    $lineprefix = "";
92782801d06SMauro Carvalho Chehab	    output_highlight_rst($args{'purpose'});
92882801d06SMauro Carvalho Chehab	    $start = "\n\n**Syntax**\n\n  ``";
929c0d1b6eeSJonathan Corbet	} else {
93082801d06SMauro Carvalho Chehab	    print ".. c:function:: ";
93182801d06SMauro Carvalho Chehab	}
932e3ad05feSMauro Carvalho Chehab    } else {
933e3ad05feSMauro Carvalho Chehab	print ".. c:macro:: ". $args{'function'} . "\n\n";
934e3ad05feSMauro Carvalho Chehab
935e3ad05feSMauro Carvalho Chehab	if ($args{'typedef'}) {
936e3ad05feSMauro Carvalho Chehab	    print_lineno($declaration_start_line);
937e3ad05feSMauro Carvalho Chehab	    print "   **Typedef**: ";
938e3ad05feSMauro Carvalho Chehab	    $lineprefix = "";
939e3ad05feSMauro Carvalho Chehab	    output_highlight_rst($args{'purpose'});
940e3ad05feSMauro Carvalho Chehab	    $start = "\n\n**Syntax**\n\n  ``";
941e3ad05feSMauro Carvalho Chehab	} else {
942e3ad05feSMauro Carvalho Chehab	    print "``";
943e3ad05feSMauro Carvalho Chehab	}
944e3ad05feSMauro Carvalho Chehab    }
94582801d06SMauro Carvalho Chehab    if ($args{'functiontype'} ne "") {
94682801d06SMauro Carvalho Chehab	$start .= $args{'functiontype'} . " " . $args{'function'} . " (";
94782801d06SMauro Carvalho Chehab    } else {
94882801d06SMauro Carvalho Chehab	$start .= $args{'function'} . " (";
949c0d1b6eeSJonathan Corbet    }
950c0d1b6eeSJonathan Corbet    print $start;
951c0d1b6eeSJonathan Corbet
952c0d1b6eeSJonathan Corbet    my $count = 0;
953c0d1b6eeSJonathan Corbet    foreach my $parameter (@{$args{'parameterlist'}}) {
954c0d1b6eeSJonathan Corbet	if ($count ne 0) {
955c0d1b6eeSJonathan Corbet	    print ", ";
956c0d1b6eeSJonathan Corbet	}
957c0d1b6eeSJonathan Corbet	$count++;
958c0d1b6eeSJonathan Corbet	$type = $args{'parametertypes'}{$parameter};
959a88b1672SMauro Carvalho Chehab
960c0d1b6eeSJonathan Corbet	if ($type =~ m/([^\(]*\(\*)\s*\)\s*\(([^\)]*)\)/) {
961c0d1b6eeSJonathan Corbet	    # pointer-to-function
962e8f4ba83SPeter Maydell	    print $1 . $parameter . ") (" . $2 . ")";
963c0d1b6eeSJonathan Corbet	} else {
964ed8348e2SMauro Carvalho Chehab	    print $type;
965c0d1b6eeSJonathan Corbet	}
966c0d1b6eeSJonathan Corbet    }
96782801d06SMauro Carvalho Chehab    if ($args{'typedef'}) {
96882801d06SMauro Carvalho Chehab	print ");``\n\n";
96982801d06SMauro Carvalho Chehab    } else {
970e3ad05feSMauro Carvalho Chehab	if ($sphinx_major < 3) {
971c099ff69SJani Nikula	    print ")\n\n";
972e3ad05feSMauro Carvalho Chehab	} else {
973e3ad05feSMauro Carvalho Chehab	    print ")``\n";
974e3ad05feSMauro Carvalho Chehab	}
9750b0f5f29SDaniel Vetter	print_lineno($declaration_start_line);
976c099ff69SJani Nikula	$lineprefix = "   ";
977c099ff69SJani Nikula	output_highlight_rst($args{'purpose'});
978c099ff69SJani Nikula	print "\n";
97982801d06SMauro Carvalho Chehab    }
980c0d1b6eeSJonathan Corbet
981ecbcfba1SJani Nikula    print "**Parameters**\n\n";
982c099ff69SJani Nikula    $lineprefix = "  ";
983c0d1b6eeSJonathan Corbet    foreach $parameter (@{$args{'parameterlist'}}) {
984c0d1b6eeSJonathan Corbet	my $parameter_name = $parameter;
985ada5f446SGabriel Krisman Bertazi	$parameter_name =~ s/\[.*//;
986c0d1b6eeSJonathan Corbet	$type = $args{'parametertypes'}{$parameter};
987c0d1b6eeSJonathan Corbet
988c0d1b6eeSJonathan Corbet	if ($type ne "") {
989ed8348e2SMauro Carvalho Chehab	    print "``$type``\n";
990c0d1b6eeSJonathan Corbet	} else {
991c0d1b6eeSJonathan Corbet	    print "``$parameter``\n";
992c0d1b6eeSJonathan Corbet	}
9930b0f5f29SDaniel Vetter
9940b0f5f29SDaniel Vetter        print_lineno($parameterdesc_start_lines{$parameter_name});
9950b0f5f29SDaniel Vetter
9965e64fa9cSJani Nikula	if (defined($args{'parameterdescs'}{$parameter_name}) &&
9975e64fa9cSJani Nikula	    $args{'parameterdescs'}{$parameter_name} ne $undescribed) {
998c0d1b6eeSJonathan Corbet	    output_highlight_rst($args{'parameterdescs'}{$parameter_name});
999c0d1b6eeSJonathan Corbet	} else {
1000d4b08e0cSJani Nikula	    print "  *undescribed*\n";
1001c0d1b6eeSJonathan Corbet	}
1002c0d1b6eeSJonathan Corbet	print "\n";
1003c0d1b6eeSJonathan Corbet    }
1004c099ff69SJani Nikula
1005c099ff69SJani Nikula    $lineprefix = $oldprefix;
1006c0d1b6eeSJonathan Corbet    output_section_rst(@_);
1007c0d1b6eeSJonathan Corbet}
1008c0d1b6eeSJonathan Corbet
1009c0d1b6eeSJonathan Corbetsub output_section_rst(%) {
1010c0d1b6eeSJonathan Corbet    my %args = %{$_[0]};
1011c0d1b6eeSJonathan Corbet    my $section;
1012c0d1b6eeSJonathan Corbet    my $oldprefix = $lineprefix;
1013c0d1b6eeSJonathan Corbet    $lineprefix = "";
1014c0d1b6eeSJonathan Corbet
1015c0d1b6eeSJonathan Corbet    foreach $section (@{$args{'sectionlist'}}) {
1016ecbcfba1SJani Nikula	print "**$section**\n\n";
10170b0f5f29SDaniel Vetter        print_lineno($section_start_lines{$section});
1018c0d1b6eeSJonathan Corbet	output_highlight_rst($args{'sections'}{$section});
1019c0d1b6eeSJonathan Corbet	print "\n";
1020c0d1b6eeSJonathan Corbet    }
1021c0d1b6eeSJonathan Corbet    print "\n";
1022c0d1b6eeSJonathan Corbet    $lineprefix = $oldprefix;
1023c0d1b6eeSJonathan Corbet}
1024c0d1b6eeSJonathan Corbet
1025c0d1b6eeSJonathan Corbetsub output_enum_rst(%) {
1026c0d1b6eeSJonathan Corbet    my %args = %{$_[0]};
1027c0d1b6eeSJonathan Corbet    my ($parameter);
1028c099ff69SJani Nikula    my $oldprefix = $lineprefix;
1029c0d1b6eeSJonathan Corbet    my $count;
103062850976SJani Nikula
1031efa44475SMauro Carvalho Chehab    if ($sphinx_major < 3) {
1032efa44475SMauro Carvalho Chehab	my $name = "enum " . $args{'enum'};
103362850976SJani Nikula	print "\n\n.. c:type:: " . $name . "\n\n";
1034efa44475SMauro Carvalho Chehab    } else {
1035efa44475SMauro Carvalho Chehab	my $name = $args{'enum'};
1036efa44475SMauro Carvalho Chehab	print "\n\n.. c:enum:: " . $name . "\n\n";
1037efa44475SMauro Carvalho Chehab    }
10380b0f5f29SDaniel Vetter    print_lineno($declaration_start_line);
1039c099ff69SJani Nikula    $lineprefix = "   ";
1040c099ff69SJani Nikula    output_highlight_rst($args{'purpose'});
1041c099ff69SJani Nikula    print "\n";
1042c0d1b6eeSJonathan Corbet
1043ecbcfba1SJani Nikula    print "**Constants**\n\n";
1044c0d1b6eeSJonathan Corbet    $lineprefix = "  ";
1045c0d1b6eeSJonathan Corbet    foreach $parameter (@{$args{'parameterlist'}}) {
1046ecbcfba1SJani Nikula	print "``$parameter``\n";
1047c0d1b6eeSJonathan Corbet	if ($args{'parameterdescs'}{$parameter} ne $undescribed) {
1048c0d1b6eeSJonathan Corbet	    output_highlight_rst($args{'parameterdescs'}{$parameter});
1049c0d1b6eeSJonathan Corbet	} else {
1050d4b08e0cSJani Nikula	    print "  *undescribed*\n";
1051c0d1b6eeSJonathan Corbet	}
1052c0d1b6eeSJonathan Corbet	print "\n";
1053c0d1b6eeSJonathan Corbet    }
1054c099ff69SJani Nikula
1055c0d1b6eeSJonathan Corbet    $lineprefix = $oldprefix;
1056c0d1b6eeSJonathan Corbet    output_section_rst(@_);
1057c0d1b6eeSJonathan Corbet}
1058c0d1b6eeSJonathan Corbet
1059c0d1b6eeSJonathan Corbetsub output_typedef_rst(%) {
1060c0d1b6eeSJonathan Corbet    my %args = %{$_[0]};
1061c0d1b6eeSJonathan Corbet    my ($parameter);
1062c099ff69SJani Nikula    my $oldprefix = $lineprefix;
1063efa44475SMauro Carvalho Chehab    my $name;
1064c0d1b6eeSJonathan Corbet
1065efa44475SMauro Carvalho Chehab    if ($sphinx_major < 3) {
1066efa44475SMauro Carvalho Chehab	$name = "typedef " . $args{'typedef'};
1067efa44475SMauro Carvalho Chehab    } else {
1068efa44475SMauro Carvalho Chehab	$name = $args{'typedef'};
1069efa44475SMauro Carvalho Chehab    }
107062850976SJani Nikula    print "\n\n.. c:type:: " . $name . "\n\n";
10710b0f5f29SDaniel Vetter    print_lineno($declaration_start_line);
1072c099ff69SJani Nikula    $lineprefix = "   ";
1073c099ff69SJani Nikula    output_highlight_rst($args{'purpose'});
1074c099ff69SJani Nikula    print "\n";
1075c0d1b6eeSJonathan Corbet
1076c099ff69SJani Nikula    $lineprefix = $oldprefix;
1077c0d1b6eeSJonathan Corbet    output_section_rst(@_);
1078c0d1b6eeSJonathan Corbet}
1079c0d1b6eeSJonathan Corbet
1080c0d1b6eeSJonathan Corbetsub output_struct_rst(%) {
1081c0d1b6eeSJonathan Corbet    my %args = %{$_[0]};
1082c0d1b6eeSJonathan Corbet    my ($parameter);
1083c099ff69SJani Nikula    my $oldprefix = $lineprefix;
1084c0d1b6eeSJonathan Corbet
1085efa44475SMauro Carvalho Chehab    if ($sphinx_major < 3) {
1086efa44475SMauro Carvalho Chehab	my $name = $args{'type'} . " " . $args{'struct'};
108762850976SJani Nikula	print "\n\n.. c:type:: " . $name . "\n\n";
1088efa44475SMauro Carvalho Chehab    } else {
1089efa44475SMauro Carvalho Chehab	my $name = $args{'struct'};
1090efa44475SMauro Carvalho Chehab	print "\n\n.. c:struct:: " . $name . "\n\n";
1091efa44475SMauro Carvalho Chehab    }
10920b0f5f29SDaniel Vetter    print_lineno($declaration_start_line);
1093c099ff69SJani Nikula    $lineprefix = "   ";
1094c099ff69SJani Nikula    output_highlight_rst($args{'purpose'});
1095c099ff69SJani Nikula    print "\n";
1096c0d1b6eeSJonathan Corbet
1097ecbcfba1SJani Nikula    print "**Definition**\n\n";
1098c0d1b6eeSJonathan Corbet    print "::\n\n";
10998ad72163SMauro Carvalho Chehab    my $declaration = $args{'definition'};
11008ad72163SMauro Carvalho Chehab    $declaration =~ s/\t/  /g;
11018ad72163SMauro Carvalho Chehab    print "  " . $args{'type'} . " " . $args{'struct'} . " {\n$declaration  };\n\n";
1102c0d1b6eeSJonathan Corbet
1103ecbcfba1SJani Nikula    print "**Members**\n\n";
1104c099ff69SJani Nikula    $lineprefix = "  ";
1105c0d1b6eeSJonathan Corbet    foreach $parameter (@{$args{'parameterlist'}}) {
1106c0d1b6eeSJonathan Corbet	($parameter =~ /^#/) && next;
1107c0d1b6eeSJonathan Corbet
1108c0d1b6eeSJonathan Corbet	my $parameter_name = $parameter;
1109c0d1b6eeSJonathan Corbet	$parameter_name =~ s/\[.*//;
1110c0d1b6eeSJonathan Corbet
1111c0d1b6eeSJonathan Corbet	($args{'parameterdescs'}{$parameter_name} ne $undescribed) || next;
1112c0d1b6eeSJonathan Corbet	$type = $args{'parametertypes'}{$parameter};
11130b0f5f29SDaniel Vetter        print_lineno($parameterdesc_start_lines{$parameter_name});
11146d232c80SMauro Carvalho Chehab	print "``" . $parameter . "``\n";
1115c0d1b6eeSJonathan Corbet	output_highlight_rst($args{'parameterdescs'}{$parameter_name});
1116c0d1b6eeSJonathan Corbet	print "\n";
1117c0d1b6eeSJonathan Corbet    }
1118c0d1b6eeSJonathan Corbet    print "\n";
1119c099ff69SJani Nikula
1120c099ff69SJani Nikula    $lineprefix = $oldprefix;
1121c0d1b6eeSJonathan Corbet    output_section_rst(@_);
1122c0d1b6eeSJonathan Corbet}
1123c0d1b6eeSJonathan Corbet
11243a025e1dSMatthew Wilcox## none mode output functions
11253a025e1dSMatthew Wilcox
11263a025e1dSMatthew Wilcoxsub output_function_none(%) {
11273a025e1dSMatthew Wilcox}
11283a025e1dSMatthew Wilcox
11293a025e1dSMatthew Wilcoxsub output_enum_none(%) {
11303a025e1dSMatthew Wilcox}
11313a025e1dSMatthew Wilcox
11323a025e1dSMatthew Wilcoxsub output_typedef_none(%) {
11333a025e1dSMatthew Wilcox}
11343a025e1dSMatthew Wilcox
11353a025e1dSMatthew Wilcoxsub output_struct_none(%) {
11363a025e1dSMatthew Wilcox}
11373a025e1dSMatthew Wilcox
11383a025e1dSMatthew Wilcoxsub output_blockhead_none(%) {
11393a025e1dSMatthew Wilcox}
11403a025e1dSMatthew Wilcox
11411da177e4SLinus Torvalds##
114227205744SRandy Dunlap# generic output function for all types (function, struct/union, typedef, enum);
114327205744SRandy Dunlap# calls the generated, variable output_ function name based on
114427205744SRandy Dunlap# functype and output_mode
11451da177e4SLinus Torvaldssub output_declaration {
11461da177e4SLinus Torvalds    no strict 'refs';
11471da177e4SLinus Torvalds    my $name = shift;
11481da177e4SLinus Torvalds    my $functype = shift;
11491da177e4SLinus Torvalds    my $func = "output_${functype}_$output_mode";
1150eab795ddSMauro Carvalho Chehab
1151eab795ddSMauro Carvalho Chehab    return if (defined($nosymbol_table{$name}));
1152eab795ddSMauro Carvalho Chehab
1153b6c3f456SJani Nikula    if (($output_selection == OUTPUT_ALL) ||
1154b6c3f456SJani Nikula	(($output_selection == OUTPUT_INCLUDE ||
1155b6c3f456SJani Nikula	  $output_selection == OUTPUT_EXPORTED) &&
115686ae2e38SJani Nikula	 defined($function_table{$name})) ||
1157eab795ddSMauro Carvalho Chehab	($output_selection == OUTPUT_INTERNAL &&
115886ae2e38SJani Nikula	 !($functype eq "function" && defined($function_table{$name}))))
11591da177e4SLinus Torvalds    {
11601da177e4SLinus Torvalds	&$func(@_);
11611da177e4SLinus Torvalds	$section_counter++;
11621da177e4SLinus Torvalds    }
11631da177e4SLinus Torvalds}
11641da177e4SLinus Torvalds
11651da177e4SLinus Torvalds##
116627205744SRandy Dunlap# generic output function - calls the right one based on current output mode.
1167b112e0f7SJohannes Bergsub output_blockhead {
11681da177e4SLinus Torvalds    no strict 'refs';
1169b112e0f7SJohannes Berg    my $func = "output_blockhead_" . $output_mode;
11701da177e4SLinus Torvalds    &$func(@_);
11711da177e4SLinus Torvalds    $section_counter++;
11721da177e4SLinus Torvalds}
11731da177e4SLinus Torvalds
11741da177e4SLinus Torvalds##
11751da177e4SLinus Torvalds# takes a declaration (struct, union, enum, typedef) and
11761da177e4SLinus Torvalds# invokes the right handler. NOT called for functions.
11771da177e4SLinus Torvaldssub dump_declaration($$) {
11781da177e4SLinus Torvalds    no strict 'refs';
11791da177e4SLinus Torvalds    my ($prototype, $file) = @_;
11801da177e4SLinus Torvalds    my $func = "dump_" . $decl_type;
11811da177e4SLinus Torvalds    &$func(@_);
11821da177e4SLinus Torvalds}
11831da177e4SLinus Torvalds
11841da177e4SLinus Torvaldssub dump_union($$) {
11851da177e4SLinus Torvalds    dump_struct(@_);
11861da177e4SLinus Torvalds}
11871da177e4SLinus Torvalds
11881da177e4SLinus Torvaldssub dump_struct($$) {
11891da177e4SLinus Torvalds    my $x = shift;
11901da177e4SLinus Torvalds    my $file = shift;
11911da177e4SLinus Torvalds
1192a070991fSJonathan Cameron    if ($x =~ /(struct|union)\s+(\w+)\s*\{(.*)\}(\s*(__packed|__aligned|____cacheline_aligned_in_smp|____cacheline_aligned|__attribute__\s*\(\([a-z0-9,_\s\(\)]*\)\)))*/) {
11935cb5c31cSJohannes Berg	my $decl_type = $1;
11941da177e4SLinus Torvalds	$declaration_name = $2;
11951da177e4SLinus Torvalds	my $members = $3;
11961da177e4SLinus Torvalds
1197aeec46b9SMartin Waitz	# ignore members marked private:
11980d8c39e6SMauro Carvalho Chehab	$members =~ s/\/\*\s*private:.*?\/\*\s*public:.*?\*\///gosi;
11990d8c39e6SMauro Carvalho Chehab	$members =~ s/\/\*\s*private:.*//gosi;
1200aeec46b9SMartin Waitz	# strip comments:
1201aeec46b9SMartin Waitz	$members =~ s/\/\*.*?\*\///gos;
1202ef5da59fSRandy Dunlap	# strip attributes
12033d9bfb19SSakari Ailus	$members =~ s/\s*__attribute__\s*\(\([a-z0-9,_\*\s\(\)]*\)\)/ /gi;
12043d9bfb19SSakari Ailus	$members =~ s/\s*__aligned\s*\([^;]*\)/ /gos;
12053d9bfb19SSakari Ailus	$members =~ s/\s*__packed\s*/ /gos;
1206f0074929SJonathan Corbet	$members =~ s/\s*CRYPTO_MINALIGN_ATTR/ /gos;
1207f861537dSAndré Almeida	$members =~ s/\s*____cacheline_aligned_in_smp/ /gos;
1208a070991fSJonathan Cameron	$members =~ s/\s*____cacheline_aligned/ /gos;
12093556108eSMauro Carvalho Chehab
1210b22b5a9eSConchúr Navid	# replace DECLARE_BITMAP
12113556108eSMauro Carvalho Chehab	$members =~ s/__ETHTOOL_DECLARE_LINK_MODE_MASK\s*\(([^\)]+)\)/DECLARE_BITMAP($1, __ETHTOOL_LINK_MODE_MASK_NBITS)/gos;
121245005b27SMauro Carvalho Chehab	$members =~ s/DECLARE_BITMAP\s*\(([^,)]+),\s*([^,)]+)\)/unsigned long $1\[BITS_TO_LONGS($2)\]/gos;
12131cb566baSJakub Kicinski	# replace DECLARE_HASHTABLE
121445005b27SMauro Carvalho Chehab	$members =~ s/DECLARE_HASHTABLE\s*\(([^,)]+),\s*([^,)]+)\)/unsigned long $1\[1 << (($2) - 1)\]/gos;
121545005b27SMauro Carvalho Chehab	# replace DECLARE_KFIFO
121645005b27SMauro Carvalho Chehab	$members =~ s/DECLARE_KFIFO\s*\(([^,)]+),\s*([^,)]+),\s*([^,)]+)\)/$2 \*$1/gos;
121745005b27SMauro Carvalho Chehab	# replace DECLARE_KFIFO_PTR
121845005b27SMauro Carvalho Chehab	$members =~ s/DECLARE_KFIFO_PTR\s*\(([^,)]+),\s*([^,)]+)\)/$2 \*$1/gos;
1219aeec46b9SMartin Waitz
12208ad72163SMauro Carvalho Chehab	my $declaration = $members;
12218ad72163SMauro Carvalho Chehab
12228ad72163SMauro Carvalho Chehab	# Split nested struct/union elements as newer ones
122384ce5b98SMauro Carvalho Chehab	while ($members =~ m/(struct|union)([^\{\};]+)\{([^\{\}]*)\}([^\{\}\;]*)\;/) {
122484ce5b98SMauro Carvalho Chehab		my $newmember;
122584ce5b98SMauro Carvalho Chehab		my $maintype = $1;
122684ce5b98SMauro Carvalho Chehab		my $ids = $4;
12278ad72163SMauro Carvalho Chehab		my $content = $3;
122884ce5b98SMauro Carvalho Chehab		foreach my $id(split /,/, $ids) {
122984ce5b98SMauro Carvalho Chehab			$newmember .= "$maintype $id; ";
123084ce5b98SMauro Carvalho Chehab
12318ad72163SMauro Carvalho Chehab			$id =~ s/[:\[].*//;
123284ce5b98SMauro Carvalho Chehab			$id =~ s/^\s*\**(\S+)\s*/$1/;
12338ad72163SMauro Carvalho Chehab			foreach my $arg (split /;/, $content) {
12348ad72163SMauro Carvalho Chehab				next if ($arg =~ m/^\s*$/);
12357c0d7e87SMauro Carvalho Chehab				if ($arg =~ m/^([^\(]+\(\*?\s*)([\w\.]*)(\s*\).*)/) {
12367c0d7e87SMauro Carvalho Chehab					# pointer-to-function
12377c0d7e87SMauro Carvalho Chehab					my $type = $1;
12387c0d7e87SMauro Carvalho Chehab					my $name = $2;
12397c0d7e87SMauro Carvalho Chehab					my $extra = $3;
12407c0d7e87SMauro Carvalho Chehab					next if (!$name);
12417c0d7e87SMauro Carvalho Chehab					if ($id =~ m/^\s*$/) {
12427c0d7e87SMauro Carvalho Chehab						# anonymous struct/union
12437c0d7e87SMauro Carvalho Chehab						$newmember .= "$type$name$extra; ";
12447c0d7e87SMauro Carvalho Chehab					} else {
12457c0d7e87SMauro Carvalho Chehab						$newmember .= "$type$id.$name$extra; ";
12467c0d7e87SMauro Carvalho Chehab					}
12477c0d7e87SMauro Carvalho Chehab				} else {
124884ce5b98SMauro Carvalho Chehab					my $type;
124984ce5b98SMauro Carvalho Chehab					my $names;
125084ce5b98SMauro Carvalho Chehab					$arg =~ s/^\s+//;
125184ce5b98SMauro Carvalho Chehab					$arg =~ s/\s+$//;
125284ce5b98SMauro Carvalho Chehab					# Handle bitmaps
125384ce5b98SMauro Carvalho Chehab					$arg =~ s/:\s*\d+\s*//g;
125484ce5b98SMauro Carvalho Chehab					# Handle arrays
1255d404d579SMauro Carvalho Chehab					$arg =~ s/\[.*\]//g;
125684ce5b98SMauro Carvalho Chehab					# The type may have multiple words,
125784ce5b98SMauro Carvalho Chehab					# and multiple IDs can be defined, like:
125884ce5b98SMauro Carvalho Chehab					#	const struct foo, *bar, foobar
125984ce5b98SMauro Carvalho Chehab					# So, we remove spaces when parsing the
126084ce5b98SMauro Carvalho Chehab					# names, in order to match just names
126184ce5b98SMauro Carvalho Chehab					# and commas for the names
126284ce5b98SMauro Carvalho Chehab					$arg =~ s/\s*,\s*/,/g;
126384ce5b98SMauro Carvalho Chehab					if ($arg =~ m/(.*)\s+([\S+,]+)/) {
126484ce5b98SMauro Carvalho Chehab						$type = $1;
126584ce5b98SMauro Carvalho Chehab						$names = $2;
126684ce5b98SMauro Carvalho Chehab					} else {
126784ce5b98SMauro Carvalho Chehab						$newmember .= "$arg; ";
126884ce5b98SMauro Carvalho Chehab						next;
126984ce5b98SMauro Carvalho Chehab					}
127084ce5b98SMauro Carvalho Chehab					foreach my $name (split /,/, $names) {
127184ce5b98SMauro Carvalho Chehab						$name =~ s/^\s*\**(\S+)\s*/$1/;
12728ad72163SMauro Carvalho Chehab						next if (($name =~ m/^\s*$/));
12738ad72163SMauro Carvalho Chehab						if ($id =~ m/^\s*$/) {
12748ad72163SMauro Carvalho Chehab							# anonymous struct/union
12758ad72163SMauro Carvalho Chehab							$newmember .= "$type $name; ";
12768ad72163SMauro Carvalho Chehab						} else {
12778ad72163SMauro Carvalho Chehab							$newmember .= "$type $id.$name; ";
12788ad72163SMauro Carvalho Chehab						}
12798ad72163SMauro Carvalho Chehab					}
12807c0d7e87SMauro Carvalho Chehab				}
128184ce5b98SMauro Carvalho Chehab			}
128284ce5b98SMauro Carvalho Chehab		}
1283673bb2dfSBen Hutchings		$members =~ s/(struct|union)([^\{\};]+)\{([^\{\}]*)\}([^\{\}\;]*)\;/$newmember/;
128484ce5b98SMauro Carvalho Chehab	}
12858ad72163SMauro Carvalho Chehab
12868ad72163SMauro Carvalho Chehab	# Ignore other nested elements, like enums
1287673bb2dfSBen Hutchings	$members =~ s/(\{[^\{\}]*\})//g;
12888ad72163SMauro Carvalho Chehab
1289151c468bSMauro Carvalho Chehab	create_parameterlist($members, ';', $file, $declaration_name);
12901081de2dSMauro Carvalho Chehab	check_sections($file, $declaration_name, $decl_type, $sectcheck, $struct_actual);
12911da177e4SLinus Torvalds
12928ad72163SMauro Carvalho Chehab	# Adjust declaration for better display
1293673bb2dfSBen Hutchings	$declaration =~ s/([\{;])/$1\n/g;
1294673bb2dfSBen Hutchings	$declaration =~ s/\}\s+;/};/g;
12958ad72163SMauro Carvalho Chehab	# Better handle inlined enums
1296673bb2dfSBen Hutchings	do {} while ($declaration =~ s/(enum\s+\{[^\}]+),([^\n])/$1,\n$2/);
12978ad72163SMauro Carvalho Chehab
12988ad72163SMauro Carvalho Chehab	my @def_args = split /\n/, $declaration;
12998ad72163SMauro Carvalho Chehab	my $level = 1;
13008ad72163SMauro Carvalho Chehab	$declaration = "";
13018ad72163SMauro Carvalho Chehab	foreach my $clause (@def_args) {
13028ad72163SMauro Carvalho Chehab		$clause =~ s/^\s+//;
13038ad72163SMauro Carvalho Chehab		$clause =~ s/\s+$//;
13048ad72163SMauro Carvalho Chehab		$clause =~ s/\s+/ /;
13058ad72163SMauro Carvalho Chehab		next if (!$clause);
1306673bb2dfSBen Hutchings		$level-- if ($clause =~ m/(\})/ && $level > 1);
13078ad72163SMauro Carvalho Chehab		if (!($clause =~ m/^\s*#/)) {
13088ad72163SMauro Carvalho Chehab			$declaration .= "\t" x $level;
13098ad72163SMauro Carvalho Chehab		}
13108ad72163SMauro Carvalho Chehab		$declaration .= "\t" . $clause . "\n";
1311673bb2dfSBen Hutchings		$level++ if ($clause =~ m/(\{)/ && !($clause =~m/\}/));
13128ad72163SMauro Carvalho Chehab	}
13131da177e4SLinus Torvalds	output_declaration($declaration_name,
13141da177e4SLinus Torvalds			   'struct',
13151da177e4SLinus Torvalds			   {'struct' => $declaration_name,
13161da177e4SLinus Torvalds			    'module' => $modulename,
13178ad72163SMauro Carvalho Chehab			    'definition' => $declaration,
13181da177e4SLinus Torvalds			    'parameterlist' => \@parameterlist,
13191da177e4SLinus Torvalds			    'parameterdescs' => \%parameterdescs,
13201da177e4SLinus Torvalds			    'parametertypes' => \%parametertypes,
13211da177e4SLinus Torvalds			    'sectionlist' => \@sectionlist,
13221da177e4SLinus Torvalds			    'sections' => \%sections,
13231da177e4SLinus Torvalds			    'purpose' => $declaration_purpose,
13241da177e4SLinus Torvalds			    'type' => $decl_type
13251da177e4SLinus Torvalds			   });
13261da177e4SLinus Torvalds    }
13271da177e4SLinus Torvalds    else {
1328d40e1e65SBart Van Assche	print STDERR "${file}:$.: error: Cannot parse struct or union!\n";
13291da177e4SLinus Torvalds	++$errors;
13301da177e4SLinus Torvalds    }
13311da177e4SLinus Torvalds}
13321da177e4SLinus Torvalds
133385afe608SMauro Carvalho Chehab
133485afe608SMauro Carvalho Chehabsub show_warnings($$) {
133585afe608SMauro Carvalho Chehab	my $functype = shift;
133685afe608SMauro Carvalho Chehab	my $name = shift;
133785afe608SMauro Carvalho Chehab
1338eab795ddSMauro Carvalho Chehab	return 0 if (defined($nosymbol_table{$name}));
1339eab795ddSMauro Carvalho Chehab
134085afe608SMauro Carvalho Chehab	return 1 if ($output_selection == OUTPUT_ALL);
134185afe608SMauro Carvalho Chehab
134285afe608SMauro Carvalho Chehab	if ($output_selection == OUTPUT_EXPORTED) {
134385afe608SMauro Carvalho Chehab		if (defined($function_table{$name})) {
134485afe608SMauro Carvalho Chehab			return 1;
134585afe608SMauro Carvalho Chehab		} else {
134685afe608SMauro Carvalho Chehab			return 0;
134785afe608SMauro Carvalho Chehab		}
134885afe608SMauro Carvalho Chehab	}
134985afe608SMauro Carvalho Chehab        if ($output_selection == OUTPUT_INTERNAL) {
135085afe608SMauro Carvalho Chehab		if (!($functype eq "function" && defined($function_table{$name}))) {
135185afe608SMauro Carvalho Chehab			return 1;
135285afe608SMauro Carvalho Chehab		} else {
135385afe608SMauro Carvalho Chehab			return 0;
135485afe608SMauro Carvalho Chehab		}
135585afe608SMauro Carvalho Chehab	}
135685afe608SMauro Carvalho Chehab	if ($output_selection == OUTPUT_INCLUDE) {
135785afe608SMauro Carvalho Chehab		if (defined($function_table{$name})) {
135885afe608SMauro Carvalho Chehab			return 1;
135985afe608SMauro Carvalho Chehab		} else {
136085afe608SMauro Carvalho Chehab			return 0;
136185afe608SMauro Carvalho Chehab		}
136285afe608SMauro Carvalho Chehab	}
136385afe608SMauro Carvalho Chehab	die("Please add the new output type at show_warnings()");
136485afe608SMauro Carvalho Chehab}
136585afe608SMauro Carvalho Chehab
13661da177e4SLinus Torvaldssub dump_enum($$) {
13671da177e4SLinus Torvalds    my $x = shift;
13681da177e4SLinus Torvalds    my $file = shift;
1369d38c8cfbSMauro Carvalho Chehab    my $members;
1370d38c8cfbSMauro Carvalho Chehab
13711da177e4SLinus Torvalds
1372aeec46b9SMartin Waitz    $x =~ s@/\*.*?\*/@@gos;	# strip comments.
13734468e21eSConchúr Navid    # strip #define macros inside enums
13744468e21eSConchúr Navid    $x =~ s@#\s*((define|ifdef)\s+|endif)[^;]*;@@gos;
1375b6d676dbSRandy Dunlap
1376d38c8cfbSMauro Carvalho Chehab    if ($x =~ /typedef\s+enum\s*\{(.*)\}\s*(\w*)\s*;/) {
1377d38c8cfbSMauro Carvalho Chehab	$declaration_name = $2;
1378d38c8cfbSMauro Carvalho Chehab	$members = $1;
1379d38c8cfbSMauro Carvalho Chehab    } elsif ($x =~ /enum\s+(\w*)\s*\{(.*)\}/) {
13801da177e4SLinus Torvalds	$declaration_name = $1;
1381d38c8cfbSMauro Carvalho Chehab	$members = $2;
1382d38c8cfbSMauro Carvalho Chehab    }
1383d38c8cfbSMauro Carvalho Chehab
1384d38c8cfbSMauro Carvalho Chehab    if ($declaration_name) {
13855cb5c31cSJohannes Berg	my %_members;
13865cb5c31cSJohannes Berg
1387463a0fdcSMarkus Heiser	$members =~ s/\s+$//;
13881da177e4SLinus Torvalds
13891da177e4SLinus Torvalds	foreach my $arg (split ',', $members) {
13901da177e4SLinus Torvalds	    $arg =~ s/^\s*(\w+).*/$1/;
13911da177e4SLinus Torvalds	    push @parameterlist, $arg;
13921da177e4SLinus Torvalds	    if (!$parameterdescs{$arg}) {
13931da177e4SLinus Torvalds		$parameterdescs{$arg} = $undescribed;
139485afe608SMauro Carvalho Chehab	        if (show_warnings("enum", $declaration_name)) {
13952defb272SMauro Carvalho Chehab			print STDERR "${file}:$.: warning: Enum value '$arg' not described in enum '$declaration_name'\n";
13962defb272SMauro Carvalho Chehab		}
13971da177e4SLinus Torvalds	    }
13985cb5c31cSJohannes Berg	    $_members{$arg} = 1;
13995cb5c31cSJohannes Berg	}
14001da177e4SLinus Torvalds
14015cb5c31cSJohannes Berg	while (my ($k, $v) = each %parameterdescs) {
14025cb5c31cSJohannes Berg	    if (!exists($_members{$k})) {
140385afe608SMauro Carvalho Chehab	        if (show_warnings("enum", $declaration_name)) {
14042defb272SMauro Carvalho Chehab		     print STDERR "${file}:$.: warning: Excess enum value '$k' description in '$declaration_name'\n";
14052defb272SMauro Carvalho Chehab		}
14065cb5c31cSJohannes Berg	    }
14071da177e4SLinus Torvalds        }
14081da177e4SLinus Torvalds
14091da177e4SLinus Torvalds	output_declaration($declaration_name,
14101da177e4SLinus Torvalds			   'enum',
14111da177e4SLinus Torvalds			   {'enum' => $declaration_name,
14121da177e4SLinus Torvalds			    'module' => $modulename,
14131da177e4SLinus Torvalds			    'parameterlist' => \@parameterlist,
14141da177e4SLinus Torvalds			    'parameterdescs' => \%parameterdescs,
14151da177e4SLinus Torvalds			    'sectionlist' => \@sectionlist,
14161da177e4SLinus Torvalds			    'sections' => \%sections,
14171da177e4SLinus Torvalds			    'purpose' => $declaration_purpose
14181da177e4SLinus Torvalds			   });
1419d38c8cfbSMauro Carvalho Chehab    } else {
1420d40e1e65SBart Van Assche	print STDERR "${file}:$.: error: Cannot parse enum!\n";
14211da177e4SLinus Torvalds	++$errors;
14221da177e4SLinus Torvalds    }
14231da177e4SLinus Torvalds}
14241da177e4SLinus Torvalds
14251da177e4SLinus Torvaldssub dump_typedef($$) {
14261da177e4SLinus Torvalds    my $x = shift;
14271da177e4SLinus Torvalds    my $file = shift;
14281da177e4SLinus Torvalds
1429aeec46b9SMartin Waitz    $x =~ s@/\*.*?\*/@@gos;	# strip comments.
143083766452SMauro Carvalho Chehab
143183766452SMauro Carvalho Chehab    # Parse function prototypes
1432d37c43ceSMauro Carvalho Chehab    if ($x =~ /typedef\s+(\w+)\s*\(\*\s*(\w\S+)\s*\)\s*\((.*)\);/ ||
1433d37c43ceSMauro Carvalho Chehab	$x =~ /typedef\s+(\w+)\s*(\w\S+)\s*\s*\((.*)\);/) {
1434d37c43ceSMauro Carvalho Chehab
143583766452SMauro Carvalho Chehab	# Function typedefs
143683766452SMauro Carvalho Chehab	$return_type = $1;
143783766452SMauro Carvalho Chehab	$declaration_name = $2;
143883766452SMauro Carvalho Chehab	my $args = $3;
143983766452SMauro Carvalho Chehab
1440151c468bSMauro Carvalho Chehab	create_parameterlist($args, ',', $file, $declaration_name);
144183766452SMauro Carvalho Chehab
144283766452SMauro Carvalho Chehab	output_declaration($declaration_name,
144383766452SMauro Carvalho Chehab			   'function',
144483766452SMauro Carvalho Chehab			   {'function' => $declaration_name,
144582801d06SMauro Carvalho Chehab			    'typedef' => 1,
144683766452SMauro Carvalho Chehab			    'module' => $modulename,
144783766452SMauro Carvalho Chehab			    'functiontype' => $return_type,
144883766452SMauro Carvalho Chehab			    'parameterlist' => \@parameterlist,
144983766452SMauro Carvalho Chehab			    'parameterdescs' => \%parameterdescs,
145083766452SMauro Carvalho Chehab			    'parametertypes' => \%parametertypes,
145183766452SMauro Carvalho Chehab			    'sectionlist' => \@sectionlist,
145283766452SMauro Carvalho Chehab			    'sections' => \%sections,
145383766452SMauro Carvalho Chehab			    'purpose' => $declaration_purpose
145483766452SMauro Carvalho Chehab			   });
145583766452SMauro Carvalho Chehab	return;
145683766452SMauro Carvalho Chehab    }
145783766452SMauro Carvalho Chehab
14581da177e4SLinus Torvalds    while (($x =~ /\(*.\)\s*;$/) || ($x =~ /\[*.\]\s*;$/)) {
14591da177e4SLinus Torvalds	$x =~ s/\(*.\)\s*;$/;/;
14601da177e4SLinus Torvalds	$x =~ s/\[*.\]\s*;$/;/;
14611da177e4SLinus Torvalds    }
14621da177e4SLinus Torvalds
14631da177e4SLinus Torvalds    if ($x =~ /typedef.*\s+(\w+)\s*;/) {
14641da177e4SLinus Torvalds	$declaration_name = $1;
14651da177e4SLinus Torvalds
14661da177e4SLinus Torvalds	output_declaration($declaration_name,
14671da177e4SLinus Torvalds			   'typedef',
14681da177e4SLinus Torvalds			   {'typedef' => $declaration_name,
14691da177e4SLinus Torvalds			    'module' => $modulename,
14701da177e4SLinus Torvalds			    'sectionlist' => \@sectionlist,
14711da177e4SLinus Torvalds			    'sections' => \%sections,
14721da177e4SLinus Torvalds			    'purpose' => $declaration_purpose
14731da177e4SLinus Torvalds			   });
14741da177e4SLinus Torvalds    }
14751da177e4SLinus Torvalds    else {
1476d40e1e65SBart Van Assche	print STDERR "${file}:$.: error: Cannot parse typedef!\n";
14771da177e4SLinus Torvalds	++$errors;
14781da177e4SLinus Torvalds    }
14791da177e4SLinus Torvalds}
14801da177e4SLinus Torvalds
1481a1d94aa5SRandy Dunlapsub save_struct_actual($) {
1482a1d94aa5SRandy Dunlap    my $actual = shift;
1483a1d94aa5SRandy Dunlap
1484a1d94aa5SRandy Dunlap    # strip all spaces from the actual param so that it looks like one string item
1485a1d94aa5SRandy Dunlap    $actual =~ s/\s*//g;
1486a1d94aa5SRandy Dunlap    $struct_actual = $struct_actual . $actual . " ";
1487a1d94aa5SRandy Dunlap}
1488a1d94aa5SRandy Dunlap
1489151c468bSMauro Carvalho Chehabsub create_parameterlist($$$$) {
14901da177e4SLinus Torvalds    my $args = shift;
14911da177e4SLinus Torvalds    my $splitter = shift;
14921da177e4SLinus Torvalds    my $file = shift;
1493151c468bSMauro Carvalho Chehab    my $declaration_name = shift;
14941da177e4SLinus Torvalds    my $type;
14951da177e4SLinus Torvalds    my $param;
14961da177e4SLinus Torvalds
1497a6d3fe77SMartin Waitz    # temporarily replace commas inside function pointer definition
14981da177e4SLinus Torvalds    while ($args =~ /(\([^\),]+),/) {
14991da177e4SLinus Torvalds	$args =~ s/(\([^\),]+),/$1#/g;
15001da177e4SLinus Torvalds    }
15011da177e4SLinus Torvalds
15021da177e4SLinus Torvalds    foreach my $arg (split($splitter, $args)) {
15031da177e4SLinus Torvalds	# strip comments
15041da177e4SLinus Torvalds	$arg =~ s/\/\*.*\*\///;
15051da177e4SLinus Torvalds	# strip leading/trailing spaces
15061da177e4SLinus Torvalds	$arg =~ s/^\s*//;
15071da177e4SLinus Torvalds	$arg =~ s/\s*$//;
15081da177e4SLinus Torvalds	$arg =~ s/\s+/ /;
15091da177e4SLinus Torvalds
15101da177e4SLinus Torvalds	if ($arg =~ /^#/) {
15111da177e4SLinus Torvalds	    # Treat preprocessor directive as a typeless variable just to fill
15121da177e4SLinus Torvalds	    # corresponding data structures "correctly". Catch it later in
15131da177e4SLinus Torvalds	    # output_* subs.
1514ed8348e2SMauro Carvalho Chehab	    push_parameter($arg, "", "", $file);
151500d62961SRichard Kennedy	} elsif ($arg =~ m/\(.+\)\s*\(/) {
15161da177e4SLinus Torvalds	    # pointer-to-function
15171da177e4SLinus Torvalds	    $arg =~ tr/#/,/;
15187c0d7e87SMauro Carvalho Chehab	    $arg =~ m/[^\(]+\(\*?\s*([\w\.]*)\s*\)/;
15191da177e4SLinus Torvalds	    $param = $1;
15201da177e4SLinus Torvalds	    $type = $arg;
152100d62961SRichard Kennedy	    $type =~ s/([^\(]+\(\*?)\s*$param/$1/;
1522a1d94aa5SRandy Dunlap	    save_struct_actual($param);
1523ed8348e2SMauro Carvalho Chehab	    push_parameter($param, $type, $arg, $file, $declaration_name);
1524aeec46b9SMartin Waitz	} elsif ($arg) {
15251da177e4SLinus Torvalds	    $arg =~ s/\s*:\s*/:/g;
15261da177e4SLinus Torvalds	    $arg =~ s/\s*\[/\[/g;
15271da177e4SLinus Torvalds
15281da177e4SLinus Torvalds	    my @args = split('\s*,\s*', $arg);
15291da177e4SLinus Torvalds	    if ($args[0] =~ m/\*/) {
15301da177e4SLinus Torvalds		$args[0] =~ s/(\*+)\s*/ $1/;
15311da177e4SLinus Torvalds	    }
1532884f2810SBorislav Petkov
1533884f2810SBorislav Petkov	    my @first_arg;
1534884f2810SBorislav Petkov	    if ($args[0] =~ /^(.*\s+)(.*?\[.*\].*)$/) {
1535884f2810SBorislav Petkov		    shift @args;
1536884f2810SBorislav Petkov		    push(@first_arg, split('\s+', $1));
1537884f2810SBorislav Petkov		    push(@first_arg, $2);
1538884f2810SBorislav Petkov	    } else {
1539884f2810SBorislav Petkov		    @first_arg = split('\s+', shift @args);
1540884f2810SBorislav Petkov	    }
1541884f2810SBorislav Petkov
15421da177e4SLinus Torvalds	    unshift(@args, pop @first_arg);
15431da177e4SLinus Torvalds	    $type = join " ", @first_arg;
15441da177e4SLinus Torvalds
15451da177e4SLinus Torvalds	    foreach $param (@args) {
15461da177e4SLinus Torvalds		if ($param =~ m/^(\*+)\s*(.*)/) {
1547a1d94aa5SRandy Dunlap		    save_struct_actual($2);
1548ed8348e2SMauro Carvalho Chehab
1549ed8348e2SMauro Carvalho Chehab		    push_parameter($2, "$type $1", $arg, $file, $declaration_name);
15501da177e4SLinus Torvalds		}
15511da177e4SLinus Torvalds		elsif ($param =~ m/(.*?):(\d+)/) {
15527b97887eSRandy Dunlap		    if ($type ne "") { # skip unnamed bit-fields
1553a1d94aa5SRandy Dunlap			save_struct_actual($1);
1554ed8348e2SMauro Carvalho Chehab			push_parameter($1, "$type:$2", $arg, $file, $declaration_name)
15551da177e4SLinus Torvalds		    }
15567b97887eSRandy Dunlap		}
15571da177e4SLinus Torvalds		else {
1558a1d94aa5SRandy Dunlap		    save_struct_actual($param);
1559ed8348e2SMauro Carvalho Chehab		    push_parameter($param, $type, $arg, $file, $declaration_name);
15601da177e4SLinus Torvalds		}
15611da177e4SLinus Torvalds	    }
15621da177e4SLinus Torvalds	}
15631da177e4SLinus Torvalds    }
15641da177e4SLinus Torvalds}
15651da177e4SLinus Torvalds
1566ed8348e2SMauro Carvalho Chehabsub push_parameter($$$$$) {
15671da177e4SLinus Torvalds	my $param = shift;
15681da177e4SLinus Torvalds	my $type = shift;
1569ed8348e2SMauro Carvalho Chehab	my $org_arg = shift;
15701da177e4SLinus Torvalds	my $file = shift;
1571151c468bSMauro Carvalho Chehab	my $declaration_name = shift;
15721da177e4SLinus Torvalds
15735f8c7c98SRandy Dunlap	if (($anon_struct_union == 1) && ($type eq "") &&
15745f8c7c98SRandy Dunlap	    ($param eq "}")) {
15755f8c7c98SRandy Dunlap		return;		# ignore the ending }; from anon. struct/union
15765f8c7c98SRandy Dunlap	}
15775f8c7c98SRandy Dunlap
15785f8c7c98SRandy Dunlap	$anon_struct_union = 0;
1579f9b5c530SMauro Carvalho Chehab	$param =~ s/[\[\)].*//;
15801da177e4SLinus Torvalds
1581a6d3fe77SMartin Waitz	if ($type eq "" && $param =~ /\.\.\.$/)
15821da177e4SLinus Torvalds	{
1583c950a173SSilvio Fricke	    if (!$param =~ /\w\.\.\.$/) {
1584c950a173SSilvio Fricke	      # handles unnamed variable parameters
1585ef00028bSJonathan Corbet	      $param = "...";
1586c950a173SSilvio Fricke	    }
158743756e34SJonathan Neuschäfer	    elsif ($param =~ /\w\.\.\.$/) {
158843756e34SJonathan Neuschäfer	      # for named variable parameters of the form `x...`, remove the dots
158943756e34SJonathan Neuschäfer	      $param =~ s/\.\.\.$//;
159043756e34SJonathan Neuschäfer	    }
1591ced69090SRandy Dunlap	    if (!defined $parameterdescs{$param} || $parameterdescs{$param} eq "") {
1592a6d3fe77SMartin Waitz		$parameterdescs{$param} = "variable arguments";
15931da177e4SLinus Torvalds	    }
1594ced69090SRandy Dunlap	}
15951da177e4SLinus Torvalds	elsif ($type eq "" && ($param eq "" or $param eq "void"))
15961da177e4SLinus Torvalds	{
15971da177e4SLinus Torvalds	    $param="void";
15981da177e4SLinus Torvalds	    $parameterdescs{void} = "no arguments";
15991da177e4SLinus Torvalds	}
1600134fe01bSRandy Dunlap	elsif ($type eq "" && ($param eq "struct" or $param eq "union"))
1601134fe01bSRandy Dunlap	# handle unnamed (anonymous) union or struct:
1602134fe01bSRandy Dunlap	{
1603134fe01bSRandy Dunlap		$type = $param;
1604134fe01bSRandy Dunlap		$param = "{unnamed_" . $param . "}";
1605134fe01bSRandy Dunlap		$parameterdescs{$param} = "anonymous\n";
16065f8c7c98SRandy Dunlap		$anon_struct_union = 1;
1607134fe01bSRandy Dunlap	}
1608134fe01bSRandy Dunlap
1609a6d3fe77SMartin Waitz	# warn if parameter has no description
1610134fe01bSRandy Dunlap	# (but ignore ones starting with # as these are not parameters
1611134fe01bSRandy Dunlap	# but inline preprocessor statements);
1612151c468bSMauro Carvalho Chehab	# Note: It will also ignore void params and unnamed structs/unions
1613f9b5c530SMauro Carvalho Chehab	if (!defined $parameterdescs{$param} && $param !~ /^#/) {
1614f9b5c530SMauro Carvalho Chehab		$parameterdescs{$param} = $undescribed;
16151da177e4SLinus Torvalds
1616be5cd20cSJonathan Corbet	        if (show_warnings($type, $declaration_name) && $param !~ /\./) {
1617151c468bSMauro Carvalho Chehab			print STDERR
1618151c468bSMauro Carvalho Chehab			      "${file}:$.: warning: Function parameter or member '$param' not described in '$declaration_name'\n";
16191da177e4SLinus Torvalds			++$warnings;
16201da177e4SLinus Torvalds		}
16212defb272SMauro Carvalho Chehab	}
16221da177e4SLinus Torvalds
162325985edcSLucas De Marchi	# strip spaces from $param so that it is one continuous string
1624e34e7dbbSRandy Dunlap	# on @parameterlist;
1625e34e7dbbSRandy Dunlap	# this fixes a problem where check_sections() cannot find
1626e34e7dbbSRandy Dunlap	# a parameter like "addr[6 + 2]" because it actually appears
1627e34e7dbbSRandy Dunlap	# as "addr[6", "+", "2]" on the parameter list;
1628e34e7dbbSRandy Dunlap	# but it's better to maintain the param string unchanged for output,
1629e34e7dbbSRandy Dunlap	# so just weaken the string compare in check_sections() to ignore
1630e34e7dbbSRandy Dunlap	# "[blah" in a parameter string;
1631e34e7dbbSRandy Dunlap	###$param =~ s/\s*//g;
16321da177e4SLinus Torvalds	push @parameterlist, $param;
1633ed8348e2SMauro Carvalho Chehab	$org_arg =~ s/\s\s+/ /g;
1634ed8348e2SMauro Carvalho Chehab	$parametertypes{$param} = $org_arg;
16351da177e4SLinus Torvalds}
16361da177e4SLinus Torvalds
16371081de2dSMauro Carvalho Chehabsub check_sections($$$$$) {
16381081de2dSMauro Carvalho Chehab	my ($file, $decl_name, $decl_type, $sectcheck, $prmscheck) = @_;
1639a1d94aa5SRandy Dunlap	my @sects = split ' ', $sectcheck;
1640a1d94aa5SRandy Dunlap	my @prms = split ' ', $prmscheck;
1641a1d94aa5SRandy Dunlap	my $err;
1642a1d94aa5SRandy Dunlap	my ($px, $sx);
1643a1d94aa5SRandy Dunlap	my $prm_clean;		# strip trailing "[array size]" and/or beginning "*"
1644a1d94aa5SRandy Dunlap
1645a1d94aa5SRandy Dunlap	foreach $sx (0 .. $#sects) {
1646a1d94aa5SRandy Dunlap		$err = 1;
1647a1d94aa5SRandy Dunlap		foreach $px (0 .. $#prms) {
1648a1d94aa5SRandy Dunlap			$prm_clean = $prms[$px];
1649a1d94aa5SRandy Dunlap			$prm_clean =~ s/\[.*\]//;
16501f3a6688SJohannes Berg			$prm_clean =~ s/__attribute__\s*\(\([a-z,_\*\s\(\)]*\)\)//i;
1651e34e7dbbSRandy Dunlap			# ignore array size in a parameter string;
1652e34e7dbbSRandy Dunlap			# however, the original param string may contain
1653e34e7dbbSRandy Dunlap			# spaces, e.g.:  addr[6 + 2]
1654e34e7dbbSRandy Dunlap			# and this appears in @prms as "addr[6" since the
1655e34e7dbbSRandy Dunlap			# parameter list is split at spaces;
1656e34e7dbbSRandy Dunlap			# hence just ignore "[..." for the sections check;
1657e34e7dbbSRandy Dunlap			$prm_clean =~ s/\[.*//;
1658e34e7dbbSRandy Dunlap
1659a1d94aa5SRandy Dunlap			##$prm_clean =~ s/^\**//;
1660a1d94aa5SRandy Dunlap			if ($prm_clean eq $sects[$sx]) {
1661a1d94aa5SRandy Dunlap				$err = 0;
1662a1d94aa5SRandy Dunlap				last;
1663a1d94aa5SRandy Dunlap			}
1664a1d94aa5SRandy Dunlap		}
1665a1d94aa5SRandy Dunlap		if ($err) {
1666a1d94aa5SRandy Dunlap			if ($decl_type eq "function") {
1667d40e1e65SBart Van Assche				print STDERR "${file}:$.: warning: " .
1668a1d94aa5SRandy Dunlap					"Excess function parameter " .
1669a1d94aa5SRandy Dunlap					"'$sects[$sx]' " .
1670a1d94aa5SRandy Dunlap					"description in '$decl_name'\n";
1671a1d94aa5SRandy Dunlap				++$warnings;
1672a1d94aa5SRandy Dunlap			}
1673a1d94aa5SRandy Dunlap		}
1674a1d94aa5SRandy Dunlap	}
1675a1d94aa5SRandy Dunlap}
1676a1d94aa5SRandy Dunlap
16771da177e4SLinus Torvalds##
16784092bac7SYacine Belkadi# Checks the section describing the return value of a function.
16794092bac7SYacine Belkadisub check_return_section {
16804092bac7SYacine Belkadi        my $file = shift;
16814092bac7SYacine Belkadi        my $declaration_name = shift;
16824092bac7SYacine Belkadi        my $return_type = shift;
16834092bac7SYacine Belkadi
16844092bac7SYacine Belkadi        # Ignore an empty return type (It's a macro)
16854092bac7SYacine Belkadi        # Ignore functions with a "void" return type. (But don't ignore "void *")
16864092bac7SYacine Belkadi        if (($return_type eq "") || ($return_type =~ /void\s*\w*\s*$/)) {
16874092bac7SYacine Belkadi                return;
16884092bac7SYacine Belkadi        }
16894092bac7SYacine Belkadi
16904092bac7SYacine Belkadi        if (!defined($sections{$section_return}) ||
16914092bac7SYacine Belkadi            $sections{$section_return} eq "") {
1692d40e1e65SBart Van Assche                print STDERR "${file}:$.: warning: " .
16934092bac7SYacine Belkadi                        "No description found for return value of " .
16944092bac7SYacine Belkadi                        "'$declaration_name'\n";
16954092bac7SYacine Belkadi                ++$warnings;
16964092bac7SYacine Belkadi        }
16974092bac7SYacine Belkadi}
16984092bac7SYacine Belkadi
16994092bac7SYacine Belkadi##
17001da177e4SLinus Torvalds# takes a function prototype and the name of the current file being
17011da177e4SLinus Torvalds# processed and spits out all the details stored in the global
17021da177e4SLinus Torvalds# arrays/hashes.
17031da177e4SLinus Torvaldssub dump_function($$) {
17041da177e4SLinus Torvalds    my $prototype = shift;
17051da177e4SLinus Torvalds    my $file = shift;
1706cbb4d3e6SHoria Geanta    my $noret = 0;
17071da177e4SLinus Torvalds
17085eb6b4b3SMauro Carvalho Chehab    print_lineno($.);
17095eb6b4b3SMauro Carvalho Chehab
17101da177e4SLinus Torvalds    $prototype =~ s/^static +//;
17111da177e4SLinus Torvalds    $prototype =~ s/^extern +//;
17124dc3b16bSPavel Pisa    $prototype =~ s/^asmlinkage +//;
17131da177e4SLinus Torvalds    $prototype =~ s/^inline +//;
17141da177e4SLinus Torvalds    $prototype =~ s/^__inline__ +//;
171532e79401SRandy Dunlap    $prototype =~ s/^__inline +//;
171632e79401SRandy Dunlap    $prototype =~ s/^__always_inline +//;
171732e79401SRandy Dunlap    $prototype =~ s/^noinline +//;
171874fc5c65SRandy Dunlap    $prototype =~ s/__init +//;
171920072205SRandy Dunlap    $prototype =~ s/__init_or_module +//;
1720270a0096SRandy Dunlap    $prototype =~ s/__meminit +//;
172170c95b00SRandy Dunlap    $prototype =~ s/__must_check +//;
17220df7c0e3SRandy Dunlap    $prototype =~ s/__weak +//;
17230891f959SMatthew Wilcox    $prototype =~ s/__sched +//;
172495e760cbSRandy Dunlap    $prototype =~ s/__printf\s*\(\s*\d*\s*,\s*\d*\s*\) +//;
1725cbb4d3e6SHoria Geanta    my $define = $prototype =~ s/^#\s*define\s+//; #ak added
1726b1aaa546SPaolo Bonzini    $prototype =~ s/__attribute__\s*\(\(
1727b1aaa546SPaolo Bonzini            (?:
1728b1aaa546SPaolo Bonzini                 [\w\s]++          # attribute name
1729b1aaa546SPaolo Bonzini                 (?:\([^)]*+\))?   # attribute arguments
1730b1aaa546SPaolo Bonzini                 \s*+,?            # optional comma at the end
1731b1aaa546SPaolo Bonzini            )+
1732b1aaa546SPaolo Bonzini          \)\)\s+//x;
17331da177e4SLinus Torvalds
17341da177e4SLinus Torvalds    # Yes, this truly is vile.  We are looking for:
17351da177e4SLinus Torvalds    # 1. Return type (may be nothing if we're looking at a macro)
17361da177e4SLinus Torvalds    # 2. Function name
17371da177e4SLinus Torvalds    # 3. Function parameters.
17381da177e4SLinus Torvalds    #
17391da177e4SLinus Torvalds    # All the while we have to watch out for function pointer parameters
17401da177e4SLinus Torvalds    # (which IIRC is what the two sections are for), C types (these
17411da177e4SLinus Torvalds    # regexps don't even start to express all the possibilities), and
17421da177e4SLinus Torvalds    # so on.
17431da177e4SLinus Torvalds    #
17441da177e4SLinus Torvalds    # If you mess with these regexps, it's a good idea to check that
17451da177e4SLinus Torvalds    # the following functions' documentation still comes out right:
17461da177e4SLinus Torvalds    # - parport_register_device (function pointer parameters)
17471da177e4SLinus Torvalds    # - atomic_set (macro)
17489598f91fSMartin Waitz    # - pci_match_device, __copy_to_user (long return type)
17491da177e4SLinus Torvalds
1750cbb4d3e6SHoria Geanta    if ($define && $prototype =~ m/^()([a-zA-Z0-9_~:]+)\s+/) {
1751cbb4d3e6SHoria Geanta        # This is an object-like macro, it has no return type and no parameter
1752cbb4d3e6SHoria Geanta        # list.
1753cbb4d3e6SHoria Geanta        # Function-like macros are not allowed to have spaces between
1754cbb4d3e6SHoria Geanta        # declaration_name and opening parenthesis (notice the \s+).
1755cbb4d3e6SHoria Geanta        $return_type = $1;
1756cbb4d3e6SHoria Geanta        $declaration_name = $2;
1757cbb4d3e6SHoria Geanta        $noret = 1;
1758cbb4d3e6SHoria Geanta    } elsif ($prototype =~ m/^()([a-zA-Z0-9_~:]+)\s*\(([^\(]*)\)/ ||
17591da177e4SLinus Torvalds	$prototype =~ m/^(\w+)\s+([a-zA-Z0-9_~:]+)\s*\(([^\(]*)\)/ ||
17605a0bc578SMatthew Wilcox	$prototype =~ m/^(\w+\s*\*+)\s*([a-zA-Z0-9_~:]+)\s*\(([^\(]*)\)/ ||
17611da177e4SLinus Torvalds	$prototype =~ m/^(\w+\s+\w+)\s+([a-zA-Z0-9_~:]+)\s*\(([^\(]*)\)/ ||
176294b3e03cSRandy Dunlap	$prototype =~ m/^(\w+\s+\w+\s*\*+)\s*([a-zA-Z0-9_~:]+)\s*\(([^\(]*)\)/ ||
17631da177e4SLinus Torvalds	$prototype =~ m/^(\w+\s+\w+\s+\w+)\s+([a-zA-Z0-9_~:]+)\s*\(([^\(]*)\)/ ||
17645a0bc578SMatthew Wilcox	$prototype =~ m/^(\w+\s+\w+\s+\w+\s*\*+)\s*([a-zA-Z0-9_~:]+)\s*\(([^\(]*)\)/ ||
17651da177e4SLinus Torvalds	$prototype =~ m/^()([a-zA-Z0-9_~:]+)\s*\(([^\{]*)\)/ ||
17661da177e4SLinus Torvalds	$prototype =~ m/^(\w+)\s+([a-zA-Z0-9_~:]+)\s*\(([^\{]*)\)/ ||
17675a0bc578SMatthew Wilcox	$prototype =~ m/^(\w+\s*\*+)\s*([a-zA-Z0-9_~:]+)\s*\(([^\{]*)\)/ ||
17681da177e4SLinus Torvalds	$prototype =~ m/^(\w+\s+\w+)\s+([a-zA-Z0-9_~:]+)\s*\(([^\{]*)\)/ ||
17695a0bc578SMatthew Wilcox	$prototype =~ m/^(\w+\s+\w+\s*\*+)\s*([a-zA-Z0-9_~:]+)\s*\(([^\{]*)\)/ ||
17701da177e4SLinus Torvalds	$prototype =~ m/^(\w+\s+\w+\s+\w+)\s+([a-zA-Z0-9_~:]+)\s*\(([^\{]*)\)/ ||
17715a0bc578SMatthew Wilcox	$prototype =~ m/^(\w+\s+\w+\s+\w+\s*\*+)\s*([a-zA-Z0-9_~:]+)\s*\(([^\{]*)\)/ ||
17729598f91fSMartin Waitz	$prototype =~ m/^(\w+\s+\w+\s+\w+\s+\w+)\s+([a-zA-Z0-9_~:]+)\s*\(([^\{]*)\)/ ||
17735a0bc578SMatthew Wilcox	$prototype =~ m/^(\w+\s+\w+\s+\w+\s+\w+\s*\*+)\s*([a-zA-Z0-9_~:]+)\s*\(([^\{]*)\)/ ||
17745a0bc578SMatthew Wilcox	$prototype =~ m/^(\w+\s+\w+\s*\*+\s*\w+\s*\*+\s*)\s*([a-zA-Z0-9_~:]+)\s*\(([^\{]*)\)/)  {
17751da177e4SLinus Torvalds	$return_type = $1;
17761da177e4SLinus Torvalds	$declaration_name = $2;
17771da177e4SLinus Torvalds	my $args = $3;
17781da177e4SLinus Torvalds
1779151c468bSMauro Carvalho Chehab	create_parameterlist($args, ',', $file, $declaration_name);
17801da177e4SLinus Torvalds    } else {
1781d40e1e65SBart Van Assche	print STDERR "${file}:$.: warning: cannot understand function prototype: '$prototype'\n";
17821da177e4SLinus Torvalds	return;
17831da177e4SLinus Torvalds    }
17841da177e4SLinus Torvalds
1785a1d94aa5SRandy Dunlap    my $prms = join " ", @parameterlist;
17861081de2dSMauro Carvalho Chehab    check_sections($file, $declaration_name, "function", $sectcheck, $prms);
1787a1d94aa5SRandy Dunlap
17884092bac7SYacine Belkadi    # This check emits a lot of warnings at the moment, because many
17894092bac7SYacine Belkadi    # functions don't have a 'Return' doc section. So until the number
17904092bac7SYacine Belkadi    # of warnings goes sufficiently down, the check is only performed in
17914092bac7SYacine Belkadi    # verbose mode.
17924092bac7SYacine Belkadi    # TODO: always perform the check.
1793cbb4d3e6SHoria Geanta    if ($verbose && !$noret) {
17944092bac7SYacine Belkadi	    check_return_section($file, $declaration_name, $return_type);
17954092bac7SYacine Belkadi    }
17964092bac7SYacine Belkadi
179747bcacfdSMauro Carvalho Chehab    # The function parser can be called with a typedef parameter.
179847bcacfdSMauro Carvalho Chehab    # Handle it.
179947bcacfdSMauro Carvalho Chehab    if ($return_type =~ /typedef/) {
180047bcacfdSMauro Carvalho Chehab	output_declaration($declaration_name,
180147bcacfdSMauro Carvalho Chehab			   'function',
180247bcacfdSMauro Carvalho Chehab			   {'function' => $declaration_name,
180347bcacfdSMauro Carvalho Chehab			    'typedef' => 1,
180447bcacfdSMauro Carvalho Chehab			    'module' => $modulename,
180547bcacfdSMauro Carvalho Chehab			    'functiontype' => $return_type,
180647bcacfdSMauro Carvalho Chehab			    'parameterlist' => \@parameterlist,
180747bcacfdSMauro Carvalho Chehab			    'parameterdescs' => \%parameterdescs,
180847bcacfdSMauro Carvalho Chehab			    'parametertypes' => \%parametertypes,
180947bcacfdSMauro Carvalho Chehab			    'sectionlist' => \@sectionlist,
181047bcacfdSMauro Carvalho Chehab			    'sections' => \%sections,
181147bcacfdSMauro Carvalho Chehab			    'purpose' => $declaration_purpose
181247bcacfdSMauro Carvalho Chehab			   });
181347bcacfdSMauro Carvalho Chehab    } else {
18141da177e4SLinus Torvalds	output_declaration($declaration_name,
18151da177e4SLinus Torvalds			   'function',
18161da177e4SLinus Torvalds			   {'function' => $declaration_name,
18171da177e4SLinus Torvalds			    'module' => $modulename,
18181da177e4SLinus Torvalds			    'functiontype' => $return_type,
18191da177e4SLinus Torvalds			    'parameterlist' => \@parameterlist,
18201da177e4SLinus Torvalds			    'parameterdescs' => \%parameterdescs,
18211da177e4SLinus Torvalds			    'parametertypes' => \%parametertypes,
18221da177e4SLinus Torvalds			    'sectionlist' => \@sectionlist,
18231da177e4SLinus Torvalds			    'sections' => \%sections,
18241da177e4SLinus Torvalds			    'purpose' => $declaration_purpose
18251da177e4SLinus Torvalds			   });
18261da177e4SLinus Torvalds    }
182747bcacfdSMauro Carvalho Chehab}
18281da177e4SLinus Torvalds
18291da177e4SLinus Torvaldssub reset_state {
18301da177e4SLinus Torvalds    $function = "";
18311da177e4SLinus Torvalds    %parameterdescs = ();
18321da177e4SLinus Torvalds    %parametertypes = ();
18331da177e4SLinus Torvalds    @parameterlist = ();
18341da177e4SLinus Torvalds    %sections = ();
18351da177e4SLinus Torvalds    @sectionlist = ();
1836a1d94aa5SRandy Dunlap    $sectcheck = "";
1837a1d94aa5SRandy Dunlap    $struct_actual = "";
18381da177e4SLinus Torvalds    $prototype = "";
18391da177e4SLinus Torvalds
184048af606aSJani Nikula    $state = STATE_NORMAL;
184148af606aSJani Nikula    $inline_doc_state = STATE_INLINE_NA;
18421da177e4SLinus Torvalds}
18431da177e4SLinus Torvalds
184456afb0f8SJason Baronsub tracepoint_munge($) {
184556afb0f8SJason Baron	my $file = shift;
184656afb0f8SJason Baron	my $tracepointname = 0;
184756afb0f8SJason Baron	my $tracepointargs = 0;
184856afb0f8SJason Baron
184956afb0f8SJason Baron	if ($prototype =~ m/TRACE_EVENT\((.*?),/) {
185056afb0f8SJason Baron		$tracepointname = $1;
185156afb0f8SJason Baron	}
18523a9089fdSJason Baron	if ($prototype =~ m/DEFINE_SINGLE_EVENT\((.*?),/) {
18533a9089fdSJason Baron		$tracepointname = $1;
18543a9089fdSJason Baron	}
18553a9089fdSJason Baron	if ($prototype =~ m/DEFINE_EVENT\((.*?),(.*?),/) {
18563a9089fdSJason Baron		$tracepointname = $2;
18573a9089fdSJason Baron	}
18583a9089fdSJason Baron	$tracepointname =~ s/^\s+//; #strip leading whitespace
185956afb0f8SJason Baron	if ($prototype =~ m/TP_PROTO\((.*?)\)/) {
186056afb0f8SJason Baron		$tracepointargs = $1;
186156afb0f8SJason Baron	}
186256afb0f8SJason Baron	if (($tracepointname eq 0) || ($tracepointargs eq 0)) {
1863d40e1e65SBart Van Assche		print STDERR "${file}:$.: warning: Unrecognized tracepoint format: \n".
186456afb0f8SJason Baron			     "$prototype\n";
186556afb0f8SJason Baron	} else {
186656afb0f8SJason Baron		$prototype = "static inline void trace_$tracepointname($tracepointargs)";
186756afb0f8SJason Baron	}
186856afb0f8SJason Baron}
186956afb0f8SJason Baron
1870b4870bc5SRandy Dunlapsub syscall_munge() {
1871b4870bc5SRandy Dunlap	my $void = 0;
1872b4870bc5SRandy Dunlap
18737c9aa015SMauro Carvalho Chehab	$prototype =~ s@[\r\n]+@ @gos; # strip newlines/CR's
1874b4870bc5SRandy Dunlap##	if ($prototype =~ m/SYSCALL_DEFINE0\s*\(\s*(a-zA-Z0-9_)*\s*\)/) {
1875b4870bc5SRandy Dunlap	if ($prototype =~ m/SYSCALL_DEFINE0/) {
1876b4870bc5SRandy Dunlap		$void = 1;
1877b4870bc5SRandy Dunlap##		$prototype = "long sys_$1(void)";
1878b4870bc5SRandy Dunlap	}
1879b4870bc5SRandy Dunlap
1880b4870bc5SRandy Dunlap	$prototype =~ s/SYSCALL_DEFINE.*\(/long sys_/; # fix return type & func name
1881b4870bc5SRandy Dunlap	if ($prototype =~ m/long (sys_.*?),/) {
1882b4870bc5SRandy Dunlap		$prototype =~ s/,/\(/;
1883b4870bc5SRandy Dunlap	} elsif ($void) {
1884b4870bc5SRandy Dunlap		$prototype =~ s/\)/\(void\)/;
1885b4870bc5SRandy Dunlap	}
1886b4870bc5SRandy Dunlap
1887b4870bc5SRandy Dunlap	# now delete all of the odd-number commas in $prototype
1888b4870bc5SRandy Dunlap	# so that arg types & arg names don't have a comma between them
1889b4870bc5SRandy Dunlap	my $count = 0;
1890b4870bc5SRandy Dunlap	my $len = length($prototype);
1891b4870bc5SRandy Dunlap	if ($void) {
1892b4870bc5SRandy Dunlap		$len = 0;	# skip the for-loop
1893b4870bc5SRandy Dunlap	}
1894b4870bc5SRandy Dunlap	for (my $ix = 0; $ix < $len; $ix++) {
1895b4870bc5SRandy Dunlap		if (substr($prototype, $ix, 1) eq ',') {
1896b4870bc5SRandy Dunlap			$count++;
1897b4870bc5SRandy Dunlap			if ($count % 2 == 1) {
1898b4870bc5SRandy Dunlap				substr($prototype, $ix, 1) = ' ';
1899b4870bc5SRandy Dunlap			}
1900b4870bc5SRandy Dunlap		}
1901b4870bc5SRandy Dunlap	}
1902b4870bc5SRandy Dunlap}
1903b4870bc5SRandy Dunlap
1904b7afa92bSDaniel Vettersub process_proto_function($$) {
19051da177e4SLinus Torvalds    my $x = shift;
19061da177e4SLinus Torvalds    my $file = shift;
19071da177e4SLinus Torvalds
190851f5a0c8SRandy Dunlap    $x =~ s@\/\/.*$@@gos; # strip C99-style comments to end of line
190951f5a0c8SRandy Dunlap
1910890c78c2SRandy Dunlap    if ($x =~ m#\s*/\*\s+MACDOC\s*#io || ($x =~ /^#/ && $x !~ /^#\s*define/)) {
19111da177e4SLinus Torvalds	# do nothing
19121da177e4SLinus Torvalds    }
19131da177e4SLinus Torvalds    elsif ($x =~ /([^\{]*)/) {
19141da177e4SLinus Torvalds	$prototype .= $1;
19151da177e4SLinus Torvalds    }
1916b4870bc5SRandy Dunlap
1917890c78c2SRandy Dunlap    if (($x =~ /\{/) || ($x =~ /\#\s*define/) || ($x =~ /;/)) {
19181da177e4SLinus Torvalds	$prototype =~ s@/\*.*?\*/@@gos;	# strip comments.
19191da177e4SLinus Torvalds	$prototype =~ s@[\r\n]+@ @gos; # strip newlines/cr's.
19201da177e4SLinus Torvalds	$prototype =~ s@^\s+@@gos; # strip leading spaces
19217ae281b0SMauro Carvalho Chehab
19227ae281b0SMauro Carvalho Chehab	 # Handle prototypes for function pointers like:
19237ae281b0SMauro Carvalho Chehab	 # int (*pcs_config)(struct foo)
19247ae281b0SMauro Carvalho Chehab	$prototype =~ s@^(\S+\s+)\(\s*\*(\S+)\)@$1$2@gos;
19257ae281b0SMauro Carvalho Chehab
1926b4870bc5SRandy Dunlap	if ($prototype =~ /SYSCALL_DEFINE/) {
1927b4870bc5SRandy Dunlap		syscall_munge();
1928b4870bc5SRandy Dunlap	}
19293a9089fdSJason Baron	if ($prototype =~ /TRACE_EVENT/ || $prototype =~ /DEFINE_EVENT/ ||
19303a9089fdSJason Baron	    $prototype =~ /DEFINE_SINGLE_EVENT/)
19313a9089fdSJason Baron	{
193256afb0f8SJason Baron		tracepoint_munge($file);
193356afb0f8SJason Baron	}
19341da177e4SLinus Torvalds	dump_function($prototype, $file);
19351da177e4SLinus Torvalds	reset_state();
19361da177e4SLinus Torvalds    }
19371da177e4SLinus Torvalds}
19381da177e4SLinus Torvalds
1939b7afa92bSDaniel Vettersub process_proto_type($$) {
19401da177e4SLinus Torvalds    my $x = shift;
19411da177e4SLinus Torvalds    my $file = shift;
19421da177e4SLinus Torvalds
19431da177e4SLinus Torvalds    $x =~ s@[\r\n]+@ @gos; # strip newlines/cr's.
19441da177e4SLinus Torvalds    $x =~ s@^\s+@@gos; # strip leading spaces
19451da177e4SLinus Torvalds    $x =~ s@\s+$@@gos; # strip trailing spaces
194651f5a0c8SRandy Dunlap    $x =~ s@\/\/.*$@@gos; # strip C99-style comments to end of line
194751f5a0c8SRandy Dunlap
19481da177e4SLinus Torvalds    if ($x =~ /^#/) {
19491da177e4SLinus Torvalds	# To distinguish preprocessor directive from regular declaration later.
19501da177e4SLinus Torvalds	$x .= ";";
19511da177e4SLinus Torvalds    }
19521da177e4SLinus Torvalds
19531da177e4SLinus Torvalds    while (1) {
1954673bb2dfSBen Hutchings	if ( $x =~ /([^\{\};]*)([\{\};])(.*)/ ) {
1955463a0fdcSMarkus Heiser            if( length $prototype ) {
1956463a0fdcSMarkus Heiser                $prototype .= " "
1957463a0fdcSMarkus Heiser            }
19581da177e4SLinus Torvalds	    $prototype .= $1 . $2;
19591da177e4SLinus Torvalds	    ($2 eq '{') && $brcount++;
19601da177e4SLinus Torvalds	    ($2 eq '}') && $brcount--;
19611da177e4SLinus Torvalds	    if (($2 eq ';') && ($brcount == 0)) {
19621da177e4SLinus Torvalds		dump_declaration($prototype, $file);
19631da177e4SLinus Torvalds		reset_state();
19641da177e4SLinus Torvalds		last;
19651da177e4SLinus Torvalds	    }
19661da177e4SLinus Torvalds	    $x = $3;
19671da177e4SLinus Torvalds	} else {
19681da177e4SLinus Torvalds	    $prototype .= $x;
19691da177e4SLinus Torvalds	    last;
19701da177e4SLinus Torvalds	}
19711da177e4SLinus Torvalds    }
19721da177e4SLinus Torvalds}
19731da177e4SLinus Torvalds
19746b5b55f6SRandy Dunlap
19751ad560e4SJani Nikulasub map_filename($) {
19761ad560e4SJani Nikula    my $file;
19771ad560e4SJani Nikula    my ($orig_file) = @_;
19781ad560e4SJani Nikula
19791ad560e4SJani Nikula    if (defined($ENV{'SRCTREE'})) {
19801ad560e4SJani Nikula	$file = "$ENV{'SRCTREE'}" . "/" . $orig_file;
19811ad560e4SJani Nikula    } else {
19821ad560e4SJani Nikula	$file = $orig_file;
19831ad560e4SJani Nikula    }
19841ad560e4SJani Nikula
19851ad560e4SJani Nikula    if (defined($source_map{$file})) {
19861ad560e4SJani Nikula	$file = $source_map{$file};
19871ad560e4SJani Nikula    }
19881ad560e4SJani Nikula
19891ad560e4SJani Nikula    return $file;
19901ad560e4SJani Nikula}
19911ad560e4SJani Nikula
199288c2b57dSJani Nikulasub process_export_file($) {
199388c2b57dSJani Nikula    my ($orig_file) = @_;
199488c2b57dSJani Nikula    my $file = map_filename($orig_file);
199588c2b57dSJani Nikula
199688c2b57dSJani Nikula    if (!open(IN,"<$file")) {
199788c2b57dSJani Nikula	print STDERR "Error: Cannot open file $file\n";
199888c2b57dSJani Nikula	++$errors;
199988c2b57dSJani Nikula	return;
200088c2b57dSJani Nikula    }
200188c2b57dSJani Nikula
200288c2b57dSJani Nikula    while (<IN>) {
200388c2b57dSJani Nikula	if (/$export_symbol/) {
2004eab795ddSMauro Carvalho Chehab	    next if (defined($nosymbol_table{$2}));
200588c2b57dSJani Nikula	    $function_table{$2} = 1;
200688c2b57dSJani Nikula	}
200788c2b57dSJani Nikula    }
200888c2b57dSJani Nikula
200988c2b57dSJani Nikula    close(IN);
201088c2b57dSJani Nikula}
201188c2b57dSJani Nikula
201207048d13SJonathan Corbet#
201307048d13SJonathan Corbet# Parsers for the various processing states.
201407048d13SJonathan Corbet#
201507048d13SJonathan Corbet# STATE_NORMAL: looking for the /** to begin everything.
201607048d13SJonathan Corbet#
201707048d13SJonathan Corbetsub process_normal() {
20181da177e4SLinus Torvalds    if (/$doc_start/o) {
201948af606aSJani Nikula	$state = STATE_NAME;	# next line is always the function name
2020850622dfSRandy Dunlap	$in_doc_sect = 0;
20210b0f5f29SDaniel Vetter	$declaration_start_line = $. + 1;
20221da177e4SLinus Torvalds    }
202307048d13SJonathan Corbet}
202407048d13SJonathan Corbet
20253cac2bc4SJonathan Corbet#
20263cac2bc4SJonathan Corbet# STATE_NAME: Looking for the "name - description" line
20273cac2bc4SJonathan Corbet#
20283cac2bc4SJonathan Corbetsub process_name($$) {
20293cac2bc4SJonathan Corbet    my $file = shift;
20301da177e4SLinus Torvalds    my $identifier;
20311da177e4SLinus Torvalds    my $descr;
20321da177e4SLinus Torvalds
20331da177e4SLinus Torvalds    if (/$doc_block/o) {
203448af606aSJani Nikula	$state = STATE_DOCBLOCK;
20351da177e4SLinus Torvalds	$contents = "";
20360b0f5f29SDaniel Vetter	$new_start_line = $. + 1;
20370b0f5f29SDaniel Vetter
20381da177e4SLinus Torvalds	if ( $1 eq "" ) {
20391da177e4SLinus Torvalds	    $section = $section_intro;
20401da177e4SLinus Torvalds	} else {
20411da177e4SLinus Torvalds	    $section = $1;
20421da177e4SLinus Torvalds	}
20431da177e4SLinus Torvalds    }
20441da177e4SLinus Torvalds    elsif (/$doc_decl/o) {
20451da177e4SLinus Torvalds	$identifier = $1;
2046a8dae20bSMike Rapoport	if (/\s*([\w\s]+?)(\(\))?\s*-/) {
20471da177e4SLinus Torvalds	    $identifier = $1;
20481da177e4SLinus Torvalds	}
20491da177e4SLinus Torvalds
205017b78717SJonathan Corbet	$state = STATE_BODY;
20510b0f5f29SDaniel Vetter	# if there's no @param blocks need to set up default section
20520b0f5f29SDaniel Vetter	# here
20532f4ad40aSJani Nikula	$contents = "";
20542f4ad40aSJani Nikula	$section = $section_default;
20550b0f5f29SDaniel Vetter	$new_start_line = $. + 1;
20561da177e4SLinus Torvalds	if (/-(.*)/) {
205751f5a0c8SRandy Dunlap	    # strip leading/trailing/multiple spaces
2058a21217daSRandy Dunlap	    $descr= $1;
2059a21217daSRandy Dunlap	    $descr =~ s/^\s*//;
2060a21217daSRandy Dunlap	    $descr =~ s/\s*$//;
206112ae6779SDaniel Santos	    $descr =~ s/\s+/ /g;
20620bba924cSJonathan Corbet	    $declaration_purpose = $descr;
206317b78717SJonathan Corbet	    $state = STATE_BODY_MAYBE;
20641da177e4SLinus Torvalds	} else {
20651da177e4SLinus Torvalds	    $declaration_purpose = "";
20661da177e4SLinus Torvalds	}
206777cc23b8SRandy Dunlap
206877cc23b8SRandy Dunlap	if (($declaration_purpose eq "") && $verbose) {
2069d40e1e65SBart Van Assche	    print STDERR "${file}:$.: warning: missing initial short description on line:\n";
207077cc23b8SRandy Dunlap	    print STDERR $_;
207177cc23b8SRandy Dunlap	    ++$warnings;
207277cc23b8SRandy Dunlap	}
207377cc23b8SRandy Dunlap
2074cf419d54SRandy Dunlap	if ($identifier =~ m/^struct\b/) {
20751da177e4SLinus Torvalds	    $decl_type = 'struct';
2076cf419d54SRandy Dunlap	} elsif ($identifier =~ m/^union\b/) {
20771da177e4SLinus Torvalds	    $decl_type = 'union';
2078cf419d54SRandy Dunlap	} elsif ($identifier =~ m/^enum\b/) {
20791da177e4SLinus Torvalds	    $decl_type = 'enum';
2080cf419d54SRandy Dunlap	} elsif ($identifier =~ m/^typedef\b/) {
20811da177e4SLinus Torvalds	    $decl_type = 'typedef';
20821da177e4SLinus Torvalds	} else {
20831da177e4SLinus Torvalds	    $decl_type = 'function';
20841da177e4SLinus Torvalds	}
20851da177e4SLinus Torvalds
20861da177e4SLinus Torvalds	if ($verbose) {
2087d40e1e65SBart Van Assche	    print STDERR "${file}:$.: info: Scanning doc for $identifier\n";
20881da177e4SLinus Torvalds	}
20891da177e4SLinus Torvalds    } else {
2090d40e1e65SBart Van Assche	print STDERR "${file}:$.: warning: Cannot understand $_ on line $.",
20911da177e4SLinus Torvalds	    " - I thought it was a doc line\n";
20921da177e4SLinus Torvalds	++$warnings;
209348af606aSJani Nikula	$state = STATE_NORMAL;
20941da177e4SLinus Torvalds    }
20953cac2bc4SJonathan Corbet}
20963cac2bc4SJonathan Corbet
20973cac2bc4SJonathan Corbet
2098d742f24dSJonathan Corbet#
2099d742f24dSJonathan Corbet# STATE_BODY and STATE_BODY_MAYBE: the bulk of a kerneldoc comment.
2100d742f24dSJonathan Corbet#
2101d742f24dSJonathan Corbetsub process_body($$) {
2102d742f24dSJonathan Corbet    my $file = shift;
21033cac2bc4SJonathan Corbet
210443756e34SJonathan Neuschäfer    # Until all named variable macro parameters are
210543756e34SJonathan Neuschäfer    # documented using the bare name (`x`) rather than with
210643756e34SJonathan Neuschäfer    # dots (`x...`), strip the dots:
210743756e34SJonathan Neuschäfer    if ($section =~ /\w\.\.\.$/) {
210843756e34SJonathan Neuschäfer	$section =~ s/\.\.\.$//;
210943756e34SJonathan Neuschäfer
211043756e34SJonathan Neuschäfer	if ($verbose) {
211143756e34SJonathan Neuschäfer	    print STDERR "${file}:$.: warning: Variable macro arguments should be documented without dots\n";
211243756e34SJonathan Neuschäfer	    ++$warnings;
211343756e34SJonathan Neuschäfer	}
211443756e34SJonathan Neuschäfer    }
211543756e34SJonathan Neuschäfer
21160d55d48bSMauro Carvalho Chehab    if ($state == STATE_BODY_WITH_BLANK_LINE && /^\s*\*\s?\S/) {
21170d55d48bSMauro Carvalho Chehab	dump_section($file, $section, $contents);
21180d55d48bSMauro Carvalho Chehab	$section = $section_default;
21190d55d48bSMauro Carvalho Chehab	$contents = "";
21200d55d48bSMauro Carvalho Chehab    }
21210d55d48bSMauro Carvalho Chehab
2122f624adefSJani Nikula    if (/$doc_sect/i) { # case insensitive for supported section names
21231da177e4SLinus Torvalds	$newsection = $1;
21241da177e4SLinus Torvalds	$newcontents = $2;
21251da177e4SLinus Torvalds
2126f624adefSJani Nikula	# map the supported section names to the canonical names
2127f624adefSJani Nikula	if ($newsection =~ m/^description$/i) {
2128f624adefSJani Nikula	    $newsection = $section_default;
2129f624adefSJani Nikula	} elsif ($newsection =~ m/^context$/i) {
2130f624adefSJani Nikula	    $newsection = $section_context;
2131f624adefSJani Nikula	} elsif ($newsection =~ m/^returns?$/i) {
2132f624adefSJani Nikula	    $newsection = $section_return;
2133f624adefSJani Nikula	} elsif ($newsection =~ m/^\@return$/) {
2134f624adefSJani Nikula	    # special: @return is a section, not a param description
2135f624adefSJani Nikula	    $newsection = $section_return;
2136f624adefSJani Nikula	}
2137f624adefSJani Nikula
2138792aa2f2SRandy Dunlap	if (($contents ne "") && ($contents ne "\n")) {
2139850622dfSRandy Dunlap	    if (!$in_doc_sect && $verbose) {
2140d40e1e65SBart Van Assche		print STDERR "${file}:$.: warning: contents before sections\n";
2141850622dfSRandy Dunlap		++$warnings;
2142850622dfSRandy Dunlap	    }
21430bba924cSJonathan Corbet	    dump_section($file, $section, $contents);
21441da177e4SLinus Torvalds	    $section = $section_default;
21451da177e4SLinus Torvalds	}
21461da177e4SLinus Torvalds
2147850622dfSRandy Dunlap	$in_doc_sect = 1;
214817b78717SJonathan Corbet	$state = STATE_BODY;
21491da177e4SLinus Torvalds	$contents = $newcontents;
21500b0f5f29SDaniel Vetter	$new_start_line = $.;
21517c9aa015SMauro Carvalho Chehab	while (substr($contents, 0, 1) eq " ") {
215205189497SRandy Dunlap	    $contents = substr($contents, 1);
215305189497SRandy Dunlap	}
21540a726301SJani Nikula	if ($contents ne "") {
21551da177e4SLinus Torvalds	    $contents .= "\n";
21561da177e4SLinus Torvalds	}
21571da177e4SLinus Torvalds	$section = $newsection;
2158b7886de4SJani Nikula	$leading_space = undef;
21591da177e4SLinus Torvalds    } elsif (/$doc_end/) {
21604c98ecafSRandy Dunlap	if (($contents ne "") && ($contents ne "\n")) {
21610bba924cSJonathan Corbet	    dump_section($file, $section, $contents);
21621da177e4SLinus Torvalds	    $section = $section_default;
21631da177e4SLinus Torvalds	    $contents = "";
21641da177e4SLinus Torvalds	}
216546b958ebSRandy Dunlap	# look for doc_com + <text> + doc_end:
216646b958ebSRandy Dunlap	if ($_ =~ m'\s*\*\s*[a-zA-Z_0-9:\.]+\*/') {
2167d40e1e65SBart Van Assche	    print STDERR "${file}:$.: warning: suspicious ending line: $_";
216846b958ebSRandy Dunlap	    ++$warnings;
216946b958ebSRandy Dunlap	}
21701da177e4SLinus Torvalds
21711da177e4SLinus Torvalds	$prototype = "";
217248af606aSJani Nikula	$state = STATE_PROTO;
21731da177e4SLinus Torvalds	$brcount = 0;
21741da177e4SLinus Torvalds    } elsif (/$doc_content/) {
21756423133bSJohannes Weiner	if ($1 eq "") {
21760d55d48bSMauro Carvalho Chehab	    if ($section eq $section_context) {
21770bba924cSJonathan Corbet		dump_section($file, $section, $contents);
21781da177e4SLinus Torvalds		$section = $section_default;
21791da177e4SLinus Torvalds		$contents = "";
21800b0f5f29SDaniel Vetter		$new_start_line = $.;
21810d55d48bSMauro Carvalho Chehab		$state = STATE_BODY;
21821da177e4SLinus Torvalds	    } else {
21830d55d48bSMauro Carvalho Chehab		if ($section ne $section_default) {
21840d55d48bSMauro Carvalho Chehab		    $state = STATE_BODY_WITH_BLANK_LINE;
21850d55d48bSMauro Carvalho Chehab		} else {
21860d55d48bSMauro Carvalho Chehab		    $state = STATE_BODY;
21870d55d48bSMauro Carvalho Chehab		}
21886423133bSJohannes Weiner		$contents .= "\n";
21896423133bSJohannes Weiner	    }
219017b78717SJonathan Corbet	} elsif ($state == STATE_BODY_MAYBE) {
21916423133bSJohannes Weiner	    # Continued declaration purpose
21926423133bSJohannes Weiner	    chomp($declaration_purpose);
21930bba924cSJonathan Corbet	    $declaration_purpose .= " " . $1;
219412ae6779SDaniel Santos	    $declaration_purpose =~ s/\s+/ /g;
21956423133bSJohannes Weiner	} else {
2196b7886de4SJani Nikula	    my $cont = $1;
2197b7886de4SJani Nikula	    if ($section =~ m/^@/ || $section eq $section_context) {
2198b7886de4SJani Nikula		if (!defined $leading_space) {
2199b7886de4SJani Nikula		    if ($cont =~ m/^(\s+)/) {
2200b7886de4SJani Nikula			$leading_space = $1;
2201b7886de4SJani Nikula		    } else {
2202b7886de4SJani Nikula			$leading_space = "";
2203b7886de4SJani Nikula		    }
2204b7886de4SJani Nikula		}
2205b7886de4SJani Nikula		$cont =~ s/^$leading_space//;
2206b7886de4SJani Nikula	    }
2207b7886de4SJani Nikula	    $contents .= $cont . "\n";
22081da177e4SLinus Torvalds	}
22091da177e4SLinus Torvalds    } else {
22101da177e4SLinus Torvalds	# i dont know - bad line?  ignore.
2211d40e1e65SBart Van Assche	print STDERR "${file}:$.: warning: bad line: $_";
22121da177e4SLinus Torvalds	++$warnings;
22131da177e4SLinus Torvalds    }
2214d742f24dSJonathan Corbet}
2215d742f24dSJonathan Corbet
2216d742f24dSJonathan Corbet
2217cc794812SJonathan Corbet#
2218cc794812SJonathan Corbet# STATE_PROTO: reading a function/whatever prototype.
2219cc794812SJonathan Corbet#
2220cc794812SJonathan Corbetsub process_proto($$) {
2221cc794812SJonathan Corbet    my $file = shift;
2222cc794812SJonathan Corbet
2223cc794812SJonathan Corbet    if (/$doc_inline_oneline/) {
2224cc794812SJonathan Corbet	$section = $1;
2225cc794812SJonathan Corbet	$contents = $2;
2226cc794812SJonathan Corbet	if ($contents ne "") {
2227cc794812SJonathan Corbet	    $contents .= "\n";
2228cc794812SJonathan Corbet	    dump_section($file, $section, $contents);
2229cc794812SJonathan Corbet	    $section = $section_default;
2230cc794812SJonathan Corbet	    $contents = "";
2231cc794812SJonathan Corbet	}
2232cc794812SJonathan Corbet    } elsif (/$doc_inline_start/) {
2233cc794812SJonathan Corbet	$state = STATE_INLINE;
2234cc794812SJonathan Corbet	$inline_doc_state = STATE_INLINE_NAME;
2235cc794812SJonathan Corbet    } elsif ($decl_type eq 'function') {
2236cc794812SJonathan Corbet	process_proto_function($_, $file);
2237cc794812SJonathan Corbet    } else {
2238cc794812SJonathan Corbet	process_proto_type($_, $file);
2239cc794812SJonathan Corbet    }
2240cc794812SJonathan Corbet}
2241cc794812SJonathan Corbet
2242c17add56SJonathan Corbet#
2243c17add56SJonathan Corbet# STATE_DOCBLOCK: within a DOC: block.
2244c17add56SJonathan Corbet#
2245c17add56SJonathan Corbetsub process_docblock($$) {
2246c17add56SJonathan Corbet    my $file = shift;
2247cc794812SJonathan Corbet
2248c17add56SJonathan Corbet    if (/$doc_end/) {
2249c17add56SJonathan Corbet	dump_doc_section($file, $section, $contents);
2250c17add56SJonathan Corbet	$section = $section_default;
2251c17add56SJonathan Corbet	$contents = "";
2252c17add56SJonathan Corbet	$function = "";
2253c17add56SJonathan Corbet	%parameterdescs = ();
2254c17add56SJonathan Corbet	%parametertypes = ();
2255c17add56SJonathan Corbet	@parameterlist = ();
2256c17add56SJonathan Corbet	%sections = ();
2257c17add56SJonathan Corbet	@sectionlist = ();
2258c17add56SJonathan Corbet	$prototype = "";
2259c17add56SJonathan Corbet	$state = STATE_NORMAL;
2260c17add56SJonathan Corbet    } elsif (/$doc_content/) {
2261c17add56SJonathan Corbet	if ( $1 eq "" )	{
2262c17add56SJonathan Corbet	    $contents .= $blankline;
2263c17add56SJonathan Corbet	} else {
2264c17add56SJonathan Corbet	    $contents .= $1 . "\n";
2265c17add56SJonathan Corbet	}
2266c17add56SJonathan Corbet    }
2267d742f24dSJonathan Corbet}
2268d742f24dSJonathan Corbet
2269c17add56SJonathan Corbet#
2270c17add56SJonathan Corbet# STATE_INLINE: docbook comments within a prototype.
2271c17add56SJonathan Corbet#
2272c17add56SJonathan Corbetsub process_inline($$) {
2273c17add56SJonathan Corbet    my $file = shift;
2274d742f24dSJonathan Corbet
2275a4c6ebedSDanilo Cesar Lemes de Paula    # First line (state 1) needs to be a @parameter
227648af606aSJani Nikula    if ($inline_doc_state == STATE_INLINE_NAME && /$doc_inline_sect/o) {
2277a4c6ebedSDanilo Cesar Lemes de Paula	$section = $1;
2278a4c6ebedSDanilo Cesar Lemes de Paula	$contents = $2;
22790b0f5f29SDaniel Vetter	$new_start_line = $.;
2280a4c6ebedSDanilo Cesar Lemes de Paula	if ($contents ne "") {
22817c9aa015SMauro Carvalho Chehab	    while (substr($contents, 0, 1) eq " ") {
2282a4c6ebedSDanilo Cesar Lemes de Paula		$contents = substr($contents, 1);
2283a4c6ebedSDanilo Cesar Lemes de Paula	    }
2284a4c6ebedSDanilo Cesar Lemes de Paula	    $contents .= "\n";
2285a4c6ebedSDanilo Cesar Lemes de Paula	}
228648af606aSJani Nikula	$inline_doc_state = STATE_INLINE_TEXT;
2287a4c6ebedSDanilo Cesar Lemes de Paula	# Documentation block end */
228848af606aSJani Nikula    } elsif (/$doc_inline_end/) {
2289a4c6ebedSDanilo Cesar Lemes de Paula	if (($contents ne "") && ($contents ne "\n")) {
22900bba924cSJonathan Corbet	    dump_section($file, $section, $contents);
2291a4c6ebedSDanilo Cesar Lemes de Paula	    $section = $section_default;
2292a4c6ebedSDanilo Cesar Lemes de Paula	    $contents = "";
2293a4c6ebedSDanilo Cesar Lemes de Paula	}
229448af606aSJani Nikula	$state = STATE_PROTO;
229548af606aSJani Nikula	$inline_doc_state = STATE_INLINE_NA;
2296a4c6ebedSDanilo Cesar Lemes de Paula	# Regular text
2297a4c6ebedSDanilo Cesar Lemes de Paula    } elsif (/$doc_content/) {
229848af606aSJani Nikula	if ($inline_doc_state == STATE_INLINE_TEXT) {
2299a4c6ebedSDanilo Cesar Lemes de Paula	    $contents .= $1 . "\n";
23006450c895SJani Nikula	    # nuke leading blank lines
23016450c895SJani Nikula	    if ($contents =~ /^\s*$/) {
23026450c895SJani Nikula		$contents = "";
23036450c895SJani Nikula	    }
230448af606aSJani Nikula	} elsif ($inline_doc_state == STATE_INLINE_NAME) {
230548af606aSJani Nikula	    $inline_doc_state = STATE_INLINE_ERROR;
2306e7ca311eSDaniel Vetter	    print STDERR "${file}:$.: warning: ";
2307a4c6ebedSDanilo Cesar Lemes de Paula	    print STDERR "Incorrect use of kernel-doc format: $_";
2308a4c6ebedSDanilo Cesar Lemes de Paula	    ++$warnings;
2309a4c6ebedSDanilo Cesar Lemes de Paula	}
2310a4c6ebedSDanilo Cesar Lemes de Paula    }
23110c9aa209SJani Nikula}
2312c17add56SJonathan Corbet
2313c17add56SJonathan Corbet
2314c17add56SJonathan Corbetsub process_file($) {
2315c17add56SJonathan Corbet    my $file;
2316c17add56SJonathan Corbet    my $initial_section_counter = $section_counter;
2317c17add56SJonathan Corbet    my ($orig_file) = @_;
2318c17add56SJonathan Corbet
2319c17add56SJonathan Corbet    $file = map_filename($orig_file);
2320c17add56SJonathan Corbet
2321dbe8ba00SMauro Carvalho Chehab    if (!open(IN_FILE,"<$file")) {
2322c17add56SJonathan Corbet	print STDERR "Error: Cannot open file $file\n";
2323c17add56SJonathan Corbet	++$errors;
2324c17add56SJonathan Corbet	return;
23251da177e4SLinus Torvalds    }
2326c17add56SJonathan Corbet
2327c17add56SJonathan Corbet    $. = 1;
2328c17add56SJonathan Corbet
2329c17add56SJonathan Corbet    $section_counter = 0;
2330dbe8ba00SMauro Carvalho Chehab    while (<IN_FILE>) {
2331c17add56SJonathan Corbet	while (s/\\\s*$//) {
2332dbe8ba00SMauro Carvalho Chehab	    $_ .= <IN_FILE>;
2333c17add56SJonathan Corbet	}
2334c17add56SJonathan Corbet	# Replace tabs by spaces
2335c17add56SJonathan Corbet        while ($_ =~ s/\t+/' ' x (length($&) * 8 - length($`) % 8)/e) {};
2336c17add56SJonathan Corbet	# Hand this line to the appropriate state handler
2337c17add56SJonathan Corbet	if ($state == STATE_NORMAL) {
2338c17add56SJonathan Corbet	    process_normal();
2339c17add56SJonathan Corbet	} elsif ($state == STATE_NAME) {
2340c17add56SJonathan Corbet	    process_name($file, $_);
23410d55d48bSMauro Carvalho Chehab	} elsif ($state == STATE_BODY || $state == STATE_BODY_MAYBE ||
23420d55d48bSMauro Carvalho Chehab		 $state == STATE_BODY_WITH_BLANK_LINE) {
2343c17add56SJonathan Corbet	    process_body($file, $_);
2344c17add56SJonathan Corbet	} elsif ($state == STATE_INLINE) { # scanning for inline parameters
2345c17add56SJonathan Corbet	    process_inline($file, $_);
2346cc794812SJonathan Corbet	} elsif ($state == STATE_PROTO) {
2347cc794812SJonathan Corbet	    process_proto($file, $_);
234848af606aSJani Nikula	} elsif ($state == STATE_DOCBLOCK) {
2349c17add56SJonathan Corbet	    process_docblock($file, $_);
23501da177e4SLinus Torvalds	}
23511da177e4SLinus Torvalds    }
2352c17add56SJonathan Corbet
2353c17add56SJonathan Corbet    # Make sure we got something interesting.
2354b0d60bfbSJonathan Corbet    if ($initial_section_counter == $section_counter && $
2355b0d60bfbSJonathan Corbet	output_mode ne "none") {
2356b0d60bfbSJonathan Corbet	if ($output_selection == OUTPUT_INCLUDE) {
2357b0d60bfbSJonathan Corbet	    print STDERR "${file}:1: warning: '$_' not found\n"
2358b0d60bfbSJonathan Corbet		for keys %function_table;
23593a025e1dSMatthew Wilcox	}
2360b0d60bfbSJonathan Corbet	else {
2361b0d60bfbSJonathan Corbet	    print STDERR "${file}:1: warning: no structured comments found\n";
2362e946c43aSJohannes Berg	}
23631da177e4SLinus Torvalds    }
2364dbe8ba00SMauro Carvalho Chehab    close IN_FILE;
23651da177e4SLinus Torvalds}
23668484baaaSRandy Dunlap
23678484baaaSRandy Dunlap
236893351d41SMauro Carvalho Chehabif ($output_mode eq "rst") {
236993351d41SMauro Carvalho Chehab	get_sphinx_version() if (!$sphinx_major);
237093351d41SMauro Carvalho Chehab}
237193351d41SMauro Carvalho Chehab
23728484baaaSRandy Dunlap$kernelversion = get_kernel_version();
23738484baaaSRandy Dunlap
23748484baaaSRandy Dunlap# generate a sequence of code that will splice in highlighting information
23758484baaaSRandy Dunlap# using the s// operator.
23761ef06233SMauro Carvalho Chehabfor (my $k = 0; $k < @highlights; $k++) {
23774d732701SDanilo Cesar Lemes de Paula    my $pattern = $highlights[$k][0];
23784d732701SDanilo Cesar Lemes de Paula    my $result = $highlights[$k][1];
23794d732701SDanilo Cesar Lemes de Paula#   print STDERR "scanning pattern:$pattern, highlight:($result)\n";
23804d732701SDanilo Cesar Lemes de Paula    $dohighlight .=  "\$contents =~ s:$pattern:$result:gs;\n";
23818484baaaSRandy Dunlap}
23828484baaaSRandy Dunlap
23838484baaaSRandy Dunlap# Read the file that maps relative names to absolute names for
23848484baaaSRandy Dunlap# separate source and object directories and for shadow trees.
23858484baaaSRandy Dunlapif (open(SOURCE_MAP, "<.tmp_filelist.txt")) {
23868484baaaSRandy Dunlap	my ($relname, $absname);
23878484baaaSRandy Dunlap	while(<SOURCE_MAP>) {
23888484baaaSRandy Dunlap		chop();
23898484baaaSRandy Dunlap		($relname, $absname) = (split())[0..1];
23908484baaaSRandy Dunlap		$relname =~ s:^/+::;
23918484baaaSRandy Dunlap		$source_map{$relname} = $absname;
23928484baaaSRandy Dunlap	}
23938484baaaSRandy Dunlap	close(SOURCE_MAP);
23948484baaaSRandy Dunlap}
23958484baaaSRandy Dunlap
239688c2b57dSJani Nikulaif ($output_selection == OUTPUT_EXPORTED ||
239788c2b57dSJani Nikula    $output_selection == OUTPUT_INTERNAL) {
2398c9b2cfb3SJani Nikula
2399c9b2cfb3SJani Nikula    push(@export_file_list, @ARGV);
2400c9b2cfb3SJani Nikula
240188c2b57dSJani Nikula    foreach (@export_file_list) {
240288c2b57dSJani Nikula	chomp;
240388c2b57dSJani Nikula	process_export_file($_);
240488c2b57dSJani Nikula    }
240588c2b57dSJani Nikula}
240688c2b57dSJani Nikula
24078484baaaSRandy Dunlapforeach (@ARGV) {
24088484baaaSRandy Dunlap    chomp;
24098484baaaSRandy Dunlap    process_file($_);
24108484baaaSRandy Dunlap}
24118484baaaSRandy Dunlapif ($verbose && $errors) {
24128484baaaSRandy Dunlap  print STDERR "$errors errors\n";
24138484baaaSRandy Dunlap}
24148484baaaSRandy Dunlapif ($verbose && $warnings) {
24158484baaaSRandy Dunlap  print STDERR "$warnings warnings\n";
24168484baaaSRandy Dunlap}
24178484baaaSRandy Dunlap
24182c12c810SPierre-Louis Bossartif ($Werror && $warnings) {
24192c12c810SPierre-Louis Bossart    print STDERR "$warnings warnings as Errors\n";
24202c12c810SPierre-Louis Bossart    exit($warnings);
24212c12c810SPierre-Louis Bossart} else {
24222c12c810SPierre-Louis Bossart    exit($output_mode eq "none" ? 0 : $errors)
24232c12c810SPierre-Louis Bossart}
2424