xref: /openbmc/linux/scripts/kernel-doc (revision a40a8a11)
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{'KCFLAGS'})) {
3332c12c810SPierre-Louis Bossart	my $kcflags = "$ENV{'KCFLAGS'}";
3342c12c810SPierre-Louis Bossart
3352c12c810SPierre-Louis Bossart	if ($kcflags =~ /Werror/) {
3362c12c810SPierre-Louis Bossart		$Werror = 1;
3372c12c810SPierre-Louis Bossart	}
3382c12c810SPierre-Louis Bossart}
3392c12c810SPierre-Louis Bossart
340bed4ed30SLaurent Pinchartif (defined($ENV{'KDOC_WERROR'})) {
341bed4ed30SLaurent Pinchart	$Werror = "$ENV{'KDOC_WERROR'}";
342bed4ed30SLaurent Pinchart}
343bed4ed30SLaurent Pinchart
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
38552042e2dSMauro Carvalho Chehab# Name of the kernel-doc identifier for non-DOC markups
38652042e2dSMauro Carvalho Chehabmy $identifier;
38752042e2dSMauro Carvalho Chehab
3881da177e4SLinus Torvaldsmy $doc_start = '^/\*\*\s*$'; # Allow whitespace at end of comment start.
3891da177e4SLinus Torvaldsmy $doc_end = '\*/';
3901da177e4SLinus Torvaldsmy $doc_com = '\s*\*\s*';
39112ae6779SDaniel Santosmy $doc_com_body = '\s*\* ?';
3921da177e4SLinus Torvaldsmy $doc_decl = $doc_com . '(\w+)';
393f624adefSJani Nikula# @params and a strictly limited set of supported section names
394212209cfSJonathan Corbet# Specifically:
395212209cfSJonathan Corbet#   Match @word:
396212209cfSJonathan Corbet#	  @...:
397212209cfSJonathan Corbet#         @{section-name}:
398212209cfSJonathan Corbet# while trying to not match literal block starts like "example::"
399212209cfSJonathan Corbet#
4008569de68SJonathan Corbetmy $doc_sect = $doc_com .
401212209cfSJonathan Corbet    '\s*(\@[.\w]+|\@\.\.\.|description|context|returns?|notes?|examples?)\s*:([^:].*)?$';
40212ae6779SDaniel Santosmy $doc_content = $doc_com_body . '(.*)';
4031da177e4SLinus Torvaldsmy $doc_block = $doc_com . 'DOC:\s*(.*)?';
40448af606aSJani Nikulamy $doc_inline_start = '^\s*/\*\*\s*$';
405fe7bc493SMauro Carvalho Chehabmy $doc_inline_sect = '\s*\*\s*(@\s*[\w][\w\.]*\s*):(.*)';
40648af606aSJani Nikulamy $doc_inline_end = '^\s*\*/\s*$';
4070c9aa209SJani Nikulamy $doc_inline_oneline = '^\s*/\*\*\s*(@[\w\s]+):\s*(.*)\s*\*/\s*$';
40886ae2e38SJani Nikulamy $export_symbol = '^\s*EXPORT_SYMBOL(_GPL)?\s*\(\s*(\w+)\s*\)\s*;';
409e86bdb24SAditya Srivastavamy $function_pointer = qr{([^\(]*\(\*)\s*\)\s*\(([^\)]*)\)};
410e86bdb24SAditya Srivastavamy $attribute = qr{__attribute__\s*\(\([a-z0-9,_\*\s\(\)]*\)\)}i;
4111da177e4SLinus Torvalds
4121da177e4SLinus Torvaldsmy %parameterdescs;
4130b0f5f29SDaniel Vettermy %parameterdesc_start_lines;
4141da177e4SLinus Torvaldsmy @parameterlist;
4151da177e4SLinus Torvaldsmy %sections;
4161da177e4SLinus Torvaldsmy @sectionlist;
4170b0f5f29SDaniel Vettermy %section_start_lines;
418a1d94aa5SRandy Dunlapmy $sectcheck;
419a1d94aa5SRandy Dunlapmy $struct_actual;
4201da177e4SLinus Torvalds
4211da177e4SLinus Torvaldsmy $contents = "";
4220b0f5f29SDaniel Vettermy $new_start_line = 0;
423f624adefSJani Nikula
424f624adefSJani Nikula# the canonical section names. see also $doc_sect above.
4251da177e4SLinus Torvaldsmy $section_default = "Description";	# default section
4261da177e4SLinus Torvaldsmy $section_intro = "Introduction";
4271da177e4SLinus Torvaldsmy $section = $section_default;
4281da177e4SLinus Torvaldsmy $section_context = "Context";
4294092bac7SYacine Belkadimy $section_return = "Return";
4301da177e4SLinus Torvalds
4311da177e4SLinus Torvaldsmy $undescribed = "-- undescribed --";
4321da177e4SLinus Torvalds
4331da177e4SLinus Torvaldsreset_state();
4341da177e4SLinus Torvalds
435b031ac4eSMauro Carvalho Chehabwhile ($ARGV[0] =~ m/^--?(.*)/) {
436b031ac4eSMauro Carvalho Chehab    my $cmd = $1;
437b031ac4eSMauro Carvalho Chehab    shift @ARGV;
438b031ac4eSMauro Carvalho Chehab    if ($cmd eq "man") {
4391da177e4SLinus Torvalds	$output_mode = "man";
4404d732701SDanilo Cesar Lemes de Paula	@highlights = @highlights_man;
4411da177e4SLinus Torvalds	$blankline = $blankline_man;
442b031ac4eSMauro Carvalho Chehab    } elsif ($cmd eq "rst") {
443c0d1b6eeSJonathan Corbet	$output_mode = "rst";
444c0d1b6eeSJonathan Corbet	@highlights = @highlights_rst;
445c0d1b6eeSJonathan Corbet	$blankline = $blankline_rst;
446b031ac4eSMauro Carvalho Chehab    } elsif ($cmd eq "none") {
4473a025e1dSMatthew Wilcox	$output_mode = "none";
448b031ac4eSMauro Carvalho Chehab    } elsif ($cmd eq "module") { # not needed for XML, inherits from calling document
4491da177e4SLinus Torvalds	$modulename = shift @ARGV;
450b031ac4eSMauro Carvalho Chehab    } elsif ($cmd eq "function") { # to only output specific functions
451b6c3f456SJani Nikula	$output_selection = OUTPUT_INCLUDE;
4521da177e4SLinus Torvalds	$function = shift @ARGV;
4531da177e4SLinus Torvalds	$function_table{$function} = 1;
454eab795ddSMauro Carvalho Chehab    } elsif ($cmd eq "nosymbol") { # Exclude specific symbols
455eab795ddSMauro Carvalho Chehab	my $symbol = shift @ARGV;
456eab795ddSMauro Carvalho Chehab	$nosymbol_table{$symbol} = 1;
457b031ac4eSMauro Carvalho Chehab    } elsif ($cmd eq "export") { # only exported symbols
458b6c3f456SJani Nikula	$output_selection = OUTPUT_EXPORTED;
459da9726ecSJani Nikula	%function_table = ();
460b031ac4eSMauro Carvalho Chehab    } elsif ($cmd eq "internal") { # only non-exported symbols
461b6c3f456SJani Nikula	$output_selection = OUTPUT_INTERNAL;
462da9726ecSJani Nikula	%function_table = ();
463b031ac4eSMauro Carvalho Chehab    } elsif ($cmd eq "export-file") {
46488c2b57dSJani Nikula	my $file = shift @ARGV;
46588c2b57dSJani Nikula	push(@export_file_list, $file);
466b031ac4eSMauro Carvalho Chehab    } elsif ($cmd eq "v") {
4671da177e4SLinus Torvalds	$verbose = 1;
4682c12c810SPierre-Louis Bossart    } elsif ($cmd eq "Werror") {
4692c12c810SPierre-Louis Bossart	$Werror = 1;
470b031ac4eSMauro Carvalho Chehab    } elsif (($cmd eq "h") || ($cmd eq "help")) {
4711da177e4SLinus Torvalds	usage();
472b031ac4eSMauro Carvalho Chehab    } elsif ($cmd eq 'no-doc-sections') {
4734b44595aSJohannes Berg	    $no_doc_sections = 1;
474b031ac4eSMauro Carvalho Chehab    } elsif ($cmd eq 'enable-lineno') {
4750b0f5f29SDaniel Vetter	    $enable_lineno = 1;
476b031ac4eSMauro Carvalho Chehab    } elsif ($cmd eq 'show-not-found') {
477b0d60bfbSJonathan Corbet	$show_not_found = 1;  # A no-op but don't fail
47893351d41SMauro Carvalho Chehab    } elsif ($cmd eq "sphinx-version") {
47993351d41SMauro Carvalho Chehab	my $ver_string = shift @ARGV;
48093351d41SMauro Carvalho Chehab	if ($ver_string =~ m/^(\d+)(\.\d+)?(\.\d+)?/) {
48193351d41SMauro Carvalho Chehab	    $sphinx_major = $1;
48293351d41SMauro Carvalho Chehab	    if (defined($2)) {
48393351d41SMauro Carvalho Chehab		$sphinx_minor = substr($2,1);
48493351d41SMauro Carvalho Chehab	    } else {
48593351d41SMauro Carvalho Chehab		$sphinx_minor = 0;
48693351d41SMauro Carvalho Chehab	    }
48793351d41SMauro Carvalho Chehab	    if (defined($3)) {
48893351d41SMauro Carvalho Chehab		$sphinx_patch = substr($3,1)
48993351d41SMauro Carvalho Chehab	    } else {
49093351d41SMauro Carvalho Chehab		$sphinx_patch = 0;
49193351d41SMauro Carvalho Chehab	    }
49293351d41SMauro Carvalho Chehab	} else {
49393351d41SMauro Carvalho Chehab	    die "Sphinx version should either major.minor or major.minor.patch format\n";
49493351d41SMauro Carvalho Chehab	}
495b031ac4eSMauro Carvalho Chehab    } else {
496b031ac4eSMauro Carvalho Chehab	# Unknown argument
497b031ac4eSMauro Carvalho Chehab        usage();
4981da177e4SLinus Torvalds    }
4991da177e4SLinus Torvalds}
5001da177e4SLinus Torvalds
5018484baaaSRandy Dunlap# continue execution near EOF;
5028484baaaSRandy Dunlap
503efa44475SMauro Carvalho Chehab# The C domain dialect changed on Sphinx 3. So, we need to check the
504efa44475SMauro Carvalho Chehab# version in order to produce the right tags.
505efa44475SMauro Carvalho Chehabsub findprog($)
506efa44475SMauro Carvalho Chehab{
507efa44475SMauro Carvalho Chehab	foreach(split(/:/, $ENV{PATH})) {
508efa44475SMauro Carvalho Chehab		return "$_/$_[0]" if(-x "$_/$_[0]");
509efa44475SMauro Carvalho Chehab	}
510efa44475SMauro Carvalho Chehab}
511efa44475SMauro Carvalho Chehab
512efa44475SMauro Carvalho Chehabsub get_sphinx_version()
513efa44475SMauro Carvalho Chehab{
514efa44475SMauro Carvalho Chehab	my $ver;
515efa44475SMauro Carvalho Chehab
516efa44475SMauro Carvalho Chehab	my $cmd = "sphinx-build";
517efa44475SMauro Carvalho Chehab	if (!findprog($cmd)) {
518efa44475SMauro Carvalho Chehab		my $cmd = "sphinx-build3";
51993351d41SMauro Carvalho Chehab		if (!findprog($cmd)) {
52093351d41SMauro Carvalho Chehab			$sphinx_major = 1;
52193351d41SMauro Carvalho Chehab			$sphinx_minor = 2;
52293351d41SMauro Carvalho Chehab			$sphinx_patch = 0;
52393351d41SMauro Carvalho Chehab			printf STDERR "Warning: Sphinx version not found. Using default (Sphinx version %d.%d.%d)\n",
52493351d41SMauro Carvalho Chehab			       $sphinx_major, $sphinx_minor, $sphinx_patch;
52593351d41SMauro Carvalho Chehab			return;
52693351d41SMauro Carvalho Chehab		}
527efa44475SMauro Carvalho Chehab	}
528efa44475SMauro Carvalho Chehab
529efa44475SMauro Carvalho Chehab	open IN, "$cmd --version 2>&1 |";
530efa44475SMauro Carvalho Chehab	while (<IN>) {
531efa44475SMauro Carvalho Chehab		if (m/^\s*sphinx-build\s+([\d]+)\.([\d\.]+)(\+\/[\da-f]+)?$/) {
53293351d41SMauro Carvalho Chehab			$sphinx_major = $1;
53393351d41SMauro Carvalho Chehab			$sphinx_minor = $2;
53493351d41SMauro Carvalho Chehab			$sphinx_patch = $3;
535efa44475SMauro Carvalho Chehab			last;
536efa44475SMauro Carvalho Chehab		}
537efa44475SMauro Carvalho Chehab		# Sphinx 1.2.x uses a different format
538efa44475SMauro Carvalho Chehab		if (m/^\s*Sphinx.*\s+([\d]+)\.([\d\.]+)$/) {
53993351d41SMauro Carvalho Chehab			$sphinx_major = $1;
54093351d41SMauro Carvalho Chehab			$sphinx_minor = $2;
54193351d41SMauro Carvalho Chehab			$sphinx_patch = $3;
542efa44475SMauro Carvalho Chehab			last;
543efa44475SMauro Carvalho Chehab		}
544efa44475SMauro Carvalho Chehab	}
545efa44475SMauro Carvalho Chehab	close IN;
546efa44475SMauro Carvalho Chehab}
547efa44475SMauro Carvalho Chehab
54853f049faSBorislav Petkov# get kernel version from env
54953f049faSBorislav Petkovsub get_kernel_version() {
5501b9bc22dSJohannes Berg    my $version = 'unknown kernel version';
55153f049faSBorislav Petkov
55253f049faSBorislav Petkov    if (defined($ENV{'KERNELVERSION'})) {
55353f049faSBorislav Petkov	$version = $ENV{'KERNELVERSION'};
55453f049faSBorislav Petkov    }
55553f049faSBorislav Petkov    return $version;
55653f049faSBorislav Petkov}
5571da177e4SLinus Torvalds
5580b0f5f29SDaniel Vetter#
5590b0f5f29SDaniel Vettersub print_lineno {
5600b0f5f29SDaniel Vetter    my $lineno = shift;
5610b0f5f29SDaniel Vetter    if ($enable_lineno && defined($lineno)) {
5620b0f5f29SDaniel Vetter        print "#define LINENO " . $lineno . "\n";
5630b0f5f29SDaniel Vetter    }
5640b0f5f29SDaniel Vetter}
5651da177e4SLinus Torvalds##
5661da177e4SLinus Torvalds# dumps section contents to arrays/hashes intended for that purpose.
5671da177e4SLinus Torvalds#
5681da177e4SLinus Torvaldssub dump_section {
56994dc7ad5SRandy Dunlap    my $file = shift;
5701da177e4SLinus Torvalds    my $name = shift;
5711da177e4SLinus Torvalds    my $contents = join "\n", @_;
5721da177e4SLinus Torvalds
57313901ef2SJani Nikula    if ($name =~ m/$type_param/) {
5741da177e4SLinus Torvalds	$name = $1;
5751da177e4SLinus Torvalds	$parameterdescs{$name} = $contents;
576a1d94aa5SRandy Dunlap	$sectcheck = $sectcheck . $name . " ";
5770b0f5f29SDaniel Vetter        $parameterdesc_start_lines{$name} = $new_start_line;
5780b0f5f29SDaniel Vetter        $new_start_line = 0;
579ced69090SRandy Dunlap    } elsif ($name eq "@\.\.\.") {
580ced69090SRandy Dunlap	$name = "...";
581ced69090SRandy Dunlap	$parameterdescs{$name} = $contents;
582a1d94aa5SRandy Dunlap	$sectcheck = $sectcheck . $name . " ";
5830b0f5f29SDaniel Vetter        $parameterdesc_start_lines{$name} = $new_start_line;
5840b0f5f29SDaniel Vetter        $new_start_line = 0;
5851da177e4SLinus Torvalds    } else {
58694dc7ad5SRandy Dunlap	if (defined($sections{$name}) && ($sections{$name} ne "")) {
58795b6be9dSJani Nikula	    # Only warn on user specified duplicate section names.
58895b6be9dSJani Nikula	    if ($name ne $section_default) {
58932217761SJani Nikula		print STDERR "${file}:$.: warning: duplicate section name '$name'\n";
59032217761SJani Nikula		++$warnings;
59195b6be9dSJani Nikula	    }
59232217761SJani Nikula	    $sections{$name} .= $contents;
59332217761SJani Nikula	} else {
5941da177e4SLinus Torvalds	    $sections{$name} = $contents;
5951da177e4SLinus Torvalds	    push @sectionlist, $name;
5960b0f5f29SDaniel Vetter            $section_start_lines{$name} = $new_start_line;
5970b0f5f29SDaniel Vetter            $new_start_line = 0;
5981da177e4SLinus Torvalds	}
5991da177e4SLinus Torvalds    }
60032217761SJani Nikula}
6011da177e4SLinus Torvalds
6021da177e4SLinus Torvalds##
603b112e0f7SJohannes Berg# dump DOC: section after checking that it should go out
604b112e0f7SJohannes Berg#
605b112e0f7SJohannes Bergsub dump_doc_section {
60694dc7ad5SRandy Dunlap    my $file = shift;
607b112e0f7SJohannes Berg    my $name = shift;
608b112e0f7SJohannes Berg    my $contents = join "\n", @_;
609b112e0f7SJohannes Berg
6104b44595aSJohannes Berg    if ($no_doc_sections) {
6114b44595aSJohannes Berg        return;
6124b44595aSJohannes Berg    }
6134b44595aSJohannes Berg
614eab795ddSMauro Carvalho Chehab    return if (defined($nosymbol_table{$name}));
615eab795ddSMauro Carvalho Chehab
616b6c3f456SJani Nikula    if (($output_selection == OUTPUT_ALL) ||
617eab795ddSMauro Carvalho Chehab	(($output_selection == OUTPUT_INCLUDE) &&
618eab795ddSMauro Carvalho Chehab	 defined($function_table{$name})))
619b112e0f7SJohannes Berg    {
62094dc7ad5SRandy Dunlap	dump_section($file, $name, $contents);
621b112e0f7SJohannes Berg	output_blockhead({'sectionlist' => \@sectionlist,
622b112e0f7SJohannes Berg			  'sections' => \%sections,
623b112e0f7SJohannes Berg			  'module' => $modulename,
624b6c3f456SJani Nikula			  'content-only' => ($output_selection != OUTPUT_ALL), });
625b112e0f7SJohannes Berg    }
626b112e0f7SJohannes Berg}
627b112e0f7SJohannes Berg
628b112e0f7SJohannes Berg##
6291da177e4SLinus Torvalds# output function
6301da177e4SLinus Torvalds#
6311da177e4SLinus Torvalds# parameterdescs, a hash.
6321da177e4SLinus Torvalds#  function => "function name"
6331da177e4SLinus Torvalds#  parameterlist => @list of parameters
6341da177e4SLinus Torvalds#  parameterdescs => %parameter descriptions
6351da177e4SLinus Torvalds#  sectionlist => @list of sections
636a21217daSRandy Dunlap#  sections => %section descriptions
6371da177e4SLinus Torvalds#
6381da177e4SLinus Torvalds
6391da177e4SLinus Torvaldssub output_highlight {
6401da177e4SLinus Torvalds    my $contents = join "\n",@_;
6411da177e4SLinus Torvalds    my $line;
6421da177e4SLinus Torvalds
6431da177e4SLinus Torvalds#   DEBUG
6441da177e4SLinus Torvalds#   if (!defined $contents) {
6451da177e4SLinus Torvalds#	use Carp;
6461da177e4SLinus Torvalds#	confess "output_highlight got called with no args?\n";
6471da177e4SLinus Torvalds#   }
6481da177e4SLinus Torvalds
6493eb014a1SRandy Dunlap#   print STDERR "contents b4:$contents\n";
6501da177e4SLinus Torvalds    eval $dohighlight;
6511da177e4SLinus Torvalds    die $@ if $@;
6523eb014a1SRandy Dunlap#   print STDERR "contents af:$contents\n";
6533eb014a1SRandy Dunlap
6541da177e4SLinus Torvalds    foreach $line (split "\n", $contents) {
65512ae6779SDaniel Santos	if (! $output_preformatted) {
65612ae6779SDaniel Santos	    $line =~ s/^\s*//;
65712ae6779SDaniel Santos	}
6581da177e4SLinus Torvalds	if ($line eq ""){
659e314ba31SDaniel Santos	    if (! $output_preformatted) {
6600bba924cSJonathan Corbet		print $lineprefix, $blankline;
661e314ba31SDaniel Santos	    }
6621da177e4SLinus Torvalds	} else {
663cdccb316SRandy Dunlap	    if ($output_mode eq "man" && substr($line, 0, 1) eq ".") {
664cdccb316SRandy Dunlap		print "\\&$line";
665cdccb316SRandy Dunlap	    } else {
6661da177e4SLinus Torvalds		print $lineprefix, $line;
6671da177e4SLinus Torvalds	    }
668cdccb316SRandy Dunlap	}
6691da177e4SLinus Torvalds	print "\n";
6701da177e4SLinus Torvalds    }
6711da177e4SLinus Torvalds}
6721da177e4SLinus Torvalds
6731da177e4SLinus Torvalds##
6741da177e4SLinus Torvalds# output function in man
6751da177e4SLinus Torvaldssub output_function_man(%) {
6761da177e4SLinus Torvalds    my %args = %{$_[0]};
6771da177e4SLinus Torvalds    my ($parameter, $section);
6781da177e4SLinus Torvalds    my $count;
6791da177e4SLinus Torvalds
6801da177e4SLinus Torvalds    print ".TH \"$args{'function'}\" 9 \"$args{'function'}\" \"$man_date\" \"Kernel Hacker's Manual\" LINUX\n";
6811da177e4SLinus Torvalds
6821da177e4SLinus Torvalds    print ".SH NAME\n";
6831da177e4SLinus Torvalds    print $args{'function'} . " \\- " . $args{'purpose'} . "\n";
6841da177e4SLinus Torvalds
6851da177e4SLinus Torvalds    print ".SH SYNOPSIS\n";
686a21217daSRandy Dunlap    if ($args{'functiontype'} ne "") {
6871da177e4SLinus Torvalds	print ".B \"" . $args{'functiontype'} . "\" " . $args{'function'} . "\n";
688a21217daSRandy Dunlap    } else {
689a21217daSRandy Dunlap	print ".B \"" . $args{'function'} . "\n";
690a21217daSRandy Dunlap    }
6911da177e4SLinus Torvalds    $count = 0;
6921da177e4SLinus Torvalds    my $parenth = "(";
6931da177e4SLinus Torvalds    my $post = ",";
6941da177e4SLinus Torvalds    foreach my $parameter (@{$args{'parameterlist'}}) {
6951da177e4SLinus Torvalds	if ($count == $#{$args{'parameterlist'}}) {
6961da177e4SLinus Torvalds	    $post = ");";
6971da177e4SLinus Torvalds	}
6981da177e4SLinus Torvalds	$type = $args{'parametertypes'}{$parameter};
699e86bdb24SAditya Srivastava	if ($type =~ m/$function_pointer/) {
7001da177e4SLinus Torvalds	    # pointer-to-function
701ed8348e2SMauro Carvalho Chehab	    print ".BI \"" . $parenth . $1 . "\" " . " \") (" . $2 . ")" . $post . "\"\n";
7021da177e4SLinus Torvalds	} else {
7031da177e4SLinus Torvalds	    $type =~ s/([^\*])$/$1 /;
704ed8348e2SMauro Carvalho Chehab	    print ".BI \"" . $parenth . $type . "\" " . " \"" . $post . "\"\n";
7051da177e4SLinus Torvalds	}
7061da177e4SLinus Torvalds	$count++;
7071da177e4SLinus Torvalds	$parenth = "";
7081da177e4SLinus Torvalds    }
7091da177e4SLinus Torvalds
7101da177e4SLinus Torvalds    print ".SH ARGUMENTS\n";
7111da177e4SLinus Torvalds    foreach $parameter (@{$args{'parameterlist'}}) {
7121da177e4SLinus Torvalds	my $parameter_name = $parameter;
7131da177e4SLinus Torvalds	$parameter_name =~ s/\[.*//;
7141da177e4SLinus Torvalds
7151da177e4SLinus Torvalds	print ".IP \"" . $parameter . "\" 12\n";
7161da177e4SLinus Torvalds	output_highlight($args{'parameterdescs'}{$parameter_name});
7171da177e4SLinus Torvalds    }
7181da177e4SLinus Torvalds    foreach $section (@{$args{'sectionlist'}}) {
7191da177e4SLinus Torvalds	print ".SH \"", uc $section, "\"\n";
7201da177e4SLinus Torvalds	output_highlight($args{'sections'}{$section});
7211da177e4SLinus Torvalds    }
7221da177e4SLinus Torvalds}
7231da177e4SLinus Torvalds
7241da177e4SLinus Torvalds##
7251da177e4SLinus Torvalds# output enum in man
7261da177e4SLinus Torvaldssub output_enum_man(%) {
7271da177e4SLinus Torvalds    my %args = %{$_[0]};
7281da177e4SLinus Torvalds    my ($parameter, $section);
7291da177e4SLinus Torvalds    my $count;
7301da177e4SLinus Torvalds
7311da177e4SLinus Torvalds    print ".TH \"$args{'module'}\" 9 \"enum $args{'enum'}\" \"$man_date\" \"API Manual\" LINUX\n";
7321da177e4SLinus Torvalds
7331da177e4SLinus Torvalds    print ".SH NAME\n";
7341da177e4SLinus Torvalds    print "enum " . $args{'enum'} . " \\- " . $args{'purpose'} . "\n";
7351da177e4SLinus Torvalds
7361da177e4SLinus Torvalds    print ".SH SYNOPSIS\n";
7371da177e4SLinus Torvalds    print "enum " . $args{'enum'} . " {\n";
7381da177e4SLinus Torvalds    $count = 0;
7391da177e4SLinus Torvalds    foreach my $parameter (@{$args{'parameterlist'}}) {
7401da177e4SLinus Torvalds	print ".br\n.BI \"    $parameter\"\n";
7411da177e4SLinus Torvalds	if ($count == $#{$args{'parameterlist'}}) {
7421da177e4SLinus Torvalds	    print "\n};\n";
7431da177e4SLinus Torvalds	    last;
7441da177e4SLinus Torvalds	}
7451da177e4SLinus Torvalds	else {
7461da177e4SLinus Torvalds	    print ", \n.br\n";
7471da177e4SLinus Torvalds	}
7481da177e4SLinus Torvalds	$count++;
7491da177e4SLinus Torvalds    }
7501da177e4SLinus Torvalds
7511da177e4SLinus Torvalds    print ".SH Constants\n";
7521da177e4SLinus Torvalds    foreach $parameter (@{$args{'parameterlist'}}) {
7531da177e4SLinus Torvalds	my $parameter_name = $parameter;
7541da177e4SLinus Torvalds	$parameter_name =~ s/\[.*//;
7551da177e4SLinus Torvalds
7561da177e4SLinus Torvalds	print ".IP \"" . $parameter . "\" 12\n";
7571da177e4SLinus Torvalds	output_highlight($args{'parameterdescs'}{$parameter_name});
7581da177e4SLinus Torvalds    }
7591da177e4SLinus Torvalds    foreach $section (@{$args{'sectionlist'}}) {
7601da177e4SLinus Torvalds	print ".SH \"$section\"\n";
7611da177e4SLinus Torvalds	output_highlight($args{'sections'}{$section});
7621da177e4SLinus Torvalds    }
7631da177e4SLinus Torvalds}
7641da177e4SLinus Torvalds
7651da177e4SLinus Torvalds##
7661da177e4SLinus Torvalds# output struct in man
7671da177e4SLinus Torvaldssub output_struct_man(%) {
7681da177e4SLinus Torvalds    my %args = %{$_[0]};
7691da177e4SLinus Torvalds    my ($parameter, $section);
7701da177e4SLinus Torvalds
7711da177e4SLinus Torvalds    print ".TH \"$args{'module'}\" 9 \"" . $args{'type'} . " " . $args{'struct'} . "\" \"$man_date\" \"API Manual\" LINUX\n";
7721da177e4SLinus Torvalds
7731da177e4SLinus Torvalds    print ".SH NAME\n";
7741da177e4SLinus Torvalds    print $args{'type'} . " " . $args{'struct'} . " \\- " . $args{'purpose'} . "\n";
7751da177e4SLinus Torvalds
7768ad72163SMauro Carvalho Chehab    my $declaration = $args{'definition'};
7778ad72163SMauro Carvalho Chehab    $declaration =~ s/\t/  /g;
7788ad72163SMauro Carvalho Chehab    $declaration =~ s/\n/"\n.br\n.BI \"/g;
7791da177e4SLinus Torvalds    print ".SH SYNOPSIS\n";
7801da177e4SLinus Torvalds    print $args{'type'} . " " . $args{'struct'} . " {\n.br\n";
7818ad72163SMauro Carvalho Chehab    print ".BI \"$declaration\n};\n.br\n\n";
7821da177e4SLinus Torvalds
783c51d3dacSRandy Dunlap    print ".SH Members\n";
7841da177e4SLinus Torvalds    foreach $parameter (@{$args{'parameterlist'}}) {
7851da177e4SLinus Torvalds	($parameter =~ /^#/) && next;
7861da177e4SLinus Torvalds
7871da177e4SLinus Torvalds	my $parameter_name = $parameter;
7881da177e4SLinus Torvalds	$parameter_name =~ s/\[.*//;
7891da177e4SLinus Torvalds
7901da177e4SLinus Torvalds	($args{'parameterdescs'}{$parameter_name} ne $undescribed) || next;
7911da177e4SLinus Torvalds	print ".IP \"" . $parameter . "\" 12\n";
7921da177e4SLinus Torvalds	output_highlight($args{'parameterdescs'}{$parameter_name});
7931da177e4SLinus Torvalds    }
7941da177e4SLinus Torvalds    foreach $section (@{$args{'sectionlist'}}) {
7951da177e4SLinus Torvalds	print ".SH \"$section\"\n";
7961da177e4SLinus Torvalds	output_highlight($args{'sections'}{$section});
7971da177e4SLinus Torvalds    }
7981da177e4SLinus Torvalds}
7991da177e4SLinus Torvalds
8001da177e4SLinus Torvalds##
8011da177e4SLinus Torvalds# output typedef in man
8021da177e4SLinus Torvaldssub output_typedef_man(%) {
8031da177e4SLinus Torvalds    my %args = %{$_[0]};
8041da177e4SLinus Torvalds    my ($parameter, $section);
8051da177e4SLinus Torvalds
8061da177e4SLinus Torvalds    print ".TH \"$args{'module'}\" 9 \"$args{'typedef'}\" \"$man_date\" \"API Manual\" LINUX\n";
8071da177e4SLinus Torvalds
8081da177e4SLinus Torvalds    print ".SH NAME\n";
8091da177e4SLinus Torvalds    print "typedef " . $args{'typedef'} . " \\- " . $args{'purpose'} . "\n";
8101da177e4SLinus Torvalds
8111da177e4SLinus Torvalds    foreach $section (@{$args{'sectionlist'}}) {
8121da177e4SLinus Torvalds	print ".SH \"$section\"\n";
8131da177e4SLinus Torvalds	output_highlight($args{'sections'}{$section});
8141da177e4SLinus Torvalds    }
8151da177e4SLinus Torvalds}
8161da177e4SLinus Torvalds
817b112e0f7SJohannes Bergsub output_blockhead_man(%) {
8181da177e4SLinus Torvalds    my %args = %{$_[0]};
8191da177e4SLinus Torvalds    my ($parameter, $section);
8201da177e4SLinus Torvalds    my $count;
8211da177e4SLinus Torvalds
8221da177e4SLinus Torvalds    print ".TH \"$args{'module'}\" 9 \"$args{'module'}\" \"$man_date\" \"API Manual\" LINUX\n";
8231da177e4SLinus Torvalds
8241da177e4SLinus Torvalds    foreach $section (@{$args{'sectionlist'}}) {
8251da177e4SLinus Torvalds	print ".SH \"$section\"\n";
8261da177e4SLinus Torvalds	output_highlight($args{'sections'}{$section});
8271da177e4SLinus Torvalds    }
8281da177e4SLinus Torvalds}
8291da177e4SLinus Torvalds
8301da177e4SLinus Torvalds##
831c0d1b6eeSJonathan Corbet# output in restructured text
832c0d1b6eeSJonathan Corbet#
833c0d1b6eeSJonathan Corbet
834c0d1b6eeSJonathan Corbet#
835c0d1b6eeSJonathan Corbet# This could use some work; it's used to output the DOC: sections, and
836c0d1b6eeSJonathan Corbet# starts by putting out the name of the doc section itself, but that tends
837c0d1b6eeSJonathan Corbet# to duplicate a header already in the template file.
838c0d1b6eeSJonathan Corbet#
839c0d1b6eeSJonathan Corbetsub output_blockhead_rst(%) {
840c0d1b6eeSJonathan Corbet    my %args = %{$_[0]};
841c0d1b6eeSJonathan Corbet    my ($parameter, $section);
842c0d1b6eeSJonathan Corbet
843c0d1b6eeSJonathan Corbet    foreach $section (@{$args{'sectionlist'}}) {
844eab795ddSMauro Carvalho Chehab	next if (defined($nosymbol_table{$section}));
845eab795ddSMauro Carvalho Chehab
8469e72184bSJani Nikula	if ($output_selection != OUTPUT_INCLUDE) {
84706a755d6SMichal Wajdeczko	    print ".. _$section:\n\n";
848c0d1b6eeSJonathan Corbet	    print "**$section**\n\n";
8499e72184bSJani Nikula	}
8500b0f5f29SDaniel Vetter        print_lineno($section_start_lines{$section});
851c0d1b6eeSJonathan Corbet	output_highlight_rst($args{'sections'}{$section});
852c0d1b6eeSJonathan Corbet	print "\n";
853c0d1b6eeSJonathan Corbet    }
854c0d1b6eeSJonathan Corbet}
855c0d1b6eeSJonathan Corbet
856af250290SJonathan Corbet#
857af250290SJonathan Corbet# Apply the RST highlights to a sub-block of text.
858af250290SJonathan Corbet#
859af250290SJonathan Corbetsub highlight_block($) {
860af250290SJonathan Corbet    # The dohighlight kludge requires the text be called $contents
861af250290SJonathan Corbet    my $contents = shift;
862c0d1b6eeSJonathan Corbet    eval $dohighlight;
863c0d1b6eeSJonathan Corbet    die $@ if $@;
864af250290SJonathan Corbet    return $contents;
865af250290SJonathan Corbet}
866c0d1b6eeSJonathan Corbet
867af250290SJonathan Corbet#
868af250290SJonathan Corbet# Regexes used only here.
869af250290SJonathan Corbet#
870af250290SJonathan Corbetmy $sphinx_literal = '^[^.].*::$';
871af250290SJonathan Corbetmy $sphinx_cblock = '^\.\.\ +code-block::';
872af250290SJonathan Corbet
873af250290SJonathan Corbetsub output_highlight_rst {
874af250290SJonathan Corbet    my $input = join "\n",@_;
875af250290SJonathan Corbet    my $output = "";
876af250290SJonathan Corbet    my $line;
877af250290SJonathan Corbet    my $in_literal = 0;
878af250290SJonathan Corbet    my $litprefix;
879af250290SJonathan Corbet    my $block = "";
880af250290SJonathan Corbet
881af250290SJonathan Corbet    foreach $line (split "\n",$input) {
882af250290SJonathan Corbet	#
883af250290SJonathan Corbet	# If we're in a literal block, see if we should drop out
884af250290SJonathan Corbet	# of it.  Otherwise pass the line straight through unmunged.
885af250290SJonathan Corbet	#
886af250290SJonathan Corbet	if ($in_literal) {
887af250290SJonathan Corbet	    if (! ($line =~ /^\s*$/)) {
888af250290SJonathan Corbet		#
889af250290SJonathan Corbet		# If this is the first non-blank line in a literal
890af250290SJonathan Corbet		# block we need to figure out what the proper indent is.
891af250290SJonathan Corbet		#
892af250290SJonathan Corbet		if ($litprefix eq "") {
893af250290SJonathan Corbet		    $line =~ /^(\s*)/;
894af250290SJonathan Corbet		    $litprefix = '^' . $1;
895af250290SJonathan Corbet		    $output .= $line . "\n";
896af250290SJonathan Corbet		} elsif (! ($line =~ /$litprefix/)) {
897af250290SJonathan Corbet		    $in_literal = 0;
898af250290SJonathan Corbet		} else {
899af250290SJonathan Corbet		    $output .= $line . "\n";
900af250290SJonathan Corbet		}
901af250290SJonathan Corbet	    } else {
902af250290SJonathan Corbet		$output .= $line . "\n";
903af250290SJonathan Corbet	    }
904af250290SJonathan Corbet	}
905af250290SJonathan Corbet	#
906af250290SJonathan Corbet	# Not in a literal block (or just dropped out)
907af250290SJonathan Corbet	#
908af250290SJonathan Corbet	if (! $in_literal) {
909af250290SJonathan Corbet	    $block .= $line . "\n";
910af250290SJonathan Corbet	    if (($line =~ /$sphinx_literal/) || ($line =~ /$sphinx_cblock/)) {
911af250290SJonathan Corbet		$in_literal = 1;
912af250290SJonathan Corbet		$litprefix = "";
913af250290SJonathan Corbet		$output .= highlight_block($block);
914af250290SJonathan Corbet		$block = ""
915af250290SJonathan Corbet	    }
916af250290SJonathan Corbet	}
917af250290SJonathan Corbet    }
918af250290SJonathan Corbet
919af250290SJonathan Corbet    if ($block) {
920af250290SJonathan Corbet	$output .= highlight_block($block);
921af250290SJonathan Corbet    }
922af250290SJonathan Corbet    foreach $line (split "\n", $output) {
923830066a7SJani Nikula	print $lineprefix . $line . "\n";
924c0d1b6eeSJonathan Corbet    }
925c0d1b6eeSJonathan Corbet}
926c0d1b6eeSJonathan Corbet
927c0d1b6eeSJonathan Corbetsub output_function_rst(%) {
928c0d1b6eeSJonathan Corbet    my %args = %{$_[0]};
929c0d1b6eeSJonathan Corbet    my ($parameter, $section);
930c099ff69SJani Nikula    my $oldprefix = $lineprefix;
93182801d06SMauro Carvalho Chehab    my $start = "";
9326e9e4158SMauro Carvalho Chehab    my $is_macro = 0;
933c0d1b6eeSJonathan Corbet
934efa44475SMauro Carvalho Chehab    if ($sphinx_major < 3) {
935e3ad05feSMauro Carvalho Chehab	if ($args{'typedef'}) {
93682801d06SMauro Carvalho Chehab	    print ".. c:type:: ". $args{'function'} . "\n\n";
93782801d06SMauro Carvalho Chehab	    print_lineno($declaration_start_line);
93882801d06SMauro Carvalho Chehab	    print "   **Typedef**: ";
93982801d06SMauro Carvalho Chehab	    $lineprefix = "";
94082801d06SMauro Carvalho Chehab	    output_highlight_rst($args{'purpose'});
94182801d06SMauro Carvalho Chehab	    $start = "\n\n**Syntax**\n\n  ``";
9426e9e4158SMauro Carvalho Chehab	    $is_macro = 1;
943c0d1b6eeSJonathan Corbet	} else {
94482801d06SMauro Carvalho Chehab	    print ".. c:function:: ";
94582801d06SMauro Carvalho Chehab	}
946e3ad05feSMauro Carvalho Chehab    } else {
9476e9e4158SMauro Carvalho Chehab	if ($args{'typedef'} || $args{'functiontype'} eq "") {
9486e9e4158SMauro Carvalho Chehab	    $is_macro = 1;
949e3ad05feSMauro Carvalho Chehab	    print ".. c:macro:: ". $args{'function'} . "\n\n";
9506e9e4158SMauro Carvalho Chehab	} else {
9516e9e4158SMauro Carvalho Chehab	    print ".. c:function:: ";
9526e9e4158SMauro Carvalho Chehab	}
953e3ad05feSMauro Carvalho Chehab
954e3ad05feSMauro Carvalho Chehab	if ($args{'typedef'}) {
955e3ad05feSMauro Carvalho Chehab	    print_lineno($declaration_start_line);
956e3ad05feSMauro Carvalho Chehab	    print "   **Typedef**: ";
957e3ad05feSMauro Carvalho Chehab	    $lineprefix = "";
958e3ad05feSMauro Carvalho Chehab	    output_highlight_rst($args{'purpose'});
959e3ad05feSMauro Carvalho Chehab	    $start = "\n\n**Syntax**\n\n  ``";
960e3ad05feSMauro Carvalho Chehab	} else {
9616e9e4158SMauro Carvalho Chehab	    print "``" if ($is_macro);
962e3ad05feSMauro Carvalho Chehab	}
963e3ad05feSMauro Carvalho Chehab    }
96482801d06SMauro Carvalho Chehab    if ($args{'functiontype'} ne "") {
96582801d06SMauro Carvalho Chehab	$start .= $args{'functiontype'} . " " . $args{'function'} . " (";
96682801d06SMauro Carvalho Chehab    } else {
96782801d06SMauro Carvalho Chehab	$start .= $args{'function'} . " (";
968c0d1b6eeSJonathan Corbet    }
969c0d1b6eeSJonathan Corbet    print $start;
970c0d1b6eeSJonathan Corbet
971c0d1b6eeSJonathan Corbet    my $count = 0;
972c0d1b6eeSJonathan Corbet    foreach my $parameter (@{$args{'parameterlist'}}) {
973c0d1b6eeSJonathan Corbet	if ($count ne 0) {
974c0d1b6eeSJonathan Corbet	    print ", ";
975c0d1b6eeSJonathan Corbet	}
976c0d1b6eeSJonathan Corbet	$count++;
977c0d1b6eeSJonathan Corbet	$type = $args{'parametertypes'}{$parameter};
978a88b1672SMauro Carvalho Chehab
979e86bdb24SAditya Srivastava	if ($type =~ m/$function_pointer/) {
980c0d1b6eeSJonathan Corbet	    # pointer-to-function
981e8f4ba83SPeter Maydell	    print $1 . $parameter . ") (" . $2 . ")";
982c0d1b6eeSJonathan Corbet	} else {
983ed8348e2SMauro Carvalho Chehab	    print $type;
984c0d1b6eeSJonathan Corbet	}
985c0d1b6eeSJonathan Corbet    }
9866e9e4158SMauro Carvalho Chehab    if ($is_macro) {
9876e9e4158SMauro Carvalho Chehab	print ")``\n\n";
98882801d06SMauro Carvalho Chehab    } else {
989c099ff69SJani Nikula	print ")\n\n";
990e3ad05feSMauro Carvalho Chehab    }
9916e9e4158SMauro Carvalho Chehab    if (!$args{'typedef'}) {
9920b0f5f29SDaniel Vetter	print_lineno($declaration_start_line);
993c099ff69SJani Nikula	$lineprefix = "   ";
994c099ff69SJani Nikula	output_highlight_rst($args{'purpose'});
995c099ff69SJani Nikula	print "\n";
99682801d06SMauro Carvalho Chehab    }
997c0d1b6eeSJonathan Corbet
998ecbcfba1SJani Nikula    print "**Parameters**\n\n";
999c099ff69SJani Nikula    $lineprefix = "  ";
1000c0d1b6eeSJonathan Corbet    foreach $parameter (@{$args{'parameterlist'}}) {
1001c0d1b6eeSJonathan Corbet	my $parameter_name = $parameter;
1002ada5f446SGabriel Krisman Bertazi	$parameter_name =~ s/\[.*//;
1003c0d1b6eeSJonathan Corbet	$type = $args{'parametertypes'}{$parameter};
1004c0d1b6eeSJonathan Corbet
1005c0d1b6eeSJonathan Corbet	if ($type ne "") {
1006ed8348e2SMauro Carvalho Chehab	    print "``$type``\n";
1007c0d1b6eeSJonathan Corbet	} else {
1008c0d1b6eeSJonathan Corbet	    print "``$parameter``\n";
1009c0d1b6eeSJonathan Corbet	}
10100b0f5f29SDaniel Vetter
10110b0f5f29SDaniel Vetter        print_lineno($parameterdesc_start_lines{$parameter_name});
10120b0f5f29SDaniel Vetter
10135e64fa9cSJani Nikula	if (defined($args{'parameterdescs'}{$parameter_name}) &&
10145e64fa9cSJani Nikula	    $args{'parameterdescs'}{$parameter_name} ne $undescribed) {
1015c0d1b6eeSJonathan Corbet	    output_highlight_rst($args{'parameterdescs'}{$parameter_name});
1016c0d1b6eeSJonathan Corbet	} else {
1017d4b08e0cSJani Nikula	    print "  *undescribed*\n";
1018c0d1b6eeSJonathan Corbet	}
1019c0d1b6eeSJonathan Corbet	print "\n";
1020c0d1b6eeSJonathan Corbet    }
1021c099ff69SJani Nikula
1022c099ff69SJani Nikula    $lineprefix = $oldprefix;
1023c0d1b6eeSJonathan Corbet    output_section_rst(@_);
1024c0d1b6eeSJonathan Corbet}
1025c0d1b6eeSJonathan Corbet
1026c0d1b6eeSJonathan Corbetsub output_section_rst(%) {
1027c0d1b6eeSJonathan Corbet    my %args = %{$_[0]};
1028c0d1b6eeSJonathan Corbet    my $section;
1029c0d1b6eeSJonathan Corbet    my $oldprefix = $lineprefix;
1030c0d1b6eeSJonathan Corbet    $lineprefix = "";
1031c0d1b6eeSJonathan Corbet
1032c0d1b6eeSJonathan Corbet    foreach $section (@{$args{'sectionlist'}}) {
1033ecbcfba1SJani Nikula	print "**$section**\n\n";
10340b0f5f29SDaniel Vetter        print_lineno($section_start_lines{$section});
1035c0d1b6eeSJonathan Corbet	output_highlight_rst($args{'sections'}{$section});
1036c0d1b6eeSJonathan Corbet	print "\n";
1037c0d1b6eeSJonathan Corbet    }
1038c0d1b6eeSJonathan Corbet    print "\n";
1039c0d1b6eeSJonathan Corbet    $lineprefix = $oldprefix;
1040c0d1b6eeSJonathan Corbet}
1041c0d1b6eeSJonathan Corbet
1042c0d1b6eeSJonathan Corbetsub output_enum_rst(%) {
1043c0d1b6eeSJonathan Corbet    my %args = %{$_[0]};
1044c0d1b6eeSJonathan Corbet    my ($parameter);
1045c099ff69SJani Nikula    my $oldprefix = $lineprefix;
1046c0d1b6eeSJonathan Corbet    my $count;
104762850976SJani Nikula
1048efa44475SMauro Carvalho Chehab    if ($sphinx_major < 3) {
1049efa44475SMauro Carvalho Chehab	my $name = "enum " . $args{'enum'};
105062850976SJani Nikula	print "\n\n.. c:type:: " . $name . "\n\n";
1051efa44475SMauro Carvalho Chehab    } else {
1052efa44475SMauro Carvalho Chehab	my $name = $args{'enum'};
1053efa44475SMauro Carvalho Chehab	print "\n\n.. c:enum:: " . $name . "\n\n";
1054efa44475SMauro Carvalho Chehab    }
10550b0f5f29SDaniel Vetter    print_lineno($declaration_start_line);
1056c099ff69SJani Nikula    $lineprefix = "   ";
1057c099ff69SJani Nikula    output_highlight_rst($args{'purpose'});
1058c099ff69SJani Nikula    print "\n";
1059c0d1b6eeSJonathan Corbet
1060ecbcfba1SJani Nikula    print "**Constants**\n\n";
1061c0d1b6eeSJonathan Corbet    $lineprefix = "  ";
1062c0d1b6eeSJonathan Corbet    foreach $parameter (@{$args{'parameterlist'}}) {
1063ecbcfba1SJani Nikula	print "``$parameter``\n";
1064c0d1b6eeSJonathan Corbet	if ($args{'parameterdescs'}{$parameter} ne $undescribed) {
1065c0d1b6eeSJonathan Corbet	    output_highlight_rst($args{'parameterdescs'}{$parameter});
1066c0d1b6eeSJonathan Corbet	} else {
1067d4b08e0cSJani Nikula	    print "  *undescribed*\n";
1068c0d1b6eeSJonathan Corbet	}
1069c0d1b6eeSJonathan Corbet	print "\n";
1070c0d1b6eeSJonathan Corbet    }
1071c099ff69SJani Nikula
1072c0d1b6eeSJonathan Corbet    $lineprefix = $oldprefix;
1073c0d1b6eeSJonathan Corbet    output_section_rst(@_);
1074c0d1b6eeSJonathan Corbet}
1075c0d1b6eeSJonathan Corbet
1076c0d1b6eeSJonathan Corbetsub output_typedef_rst(%) {
1077c0d1b6eeSJonathan Corbet    my %args = %{$_[0]};
1078c0d1b6eeSJonathan Corbet    my ($parameter);
1079c099ff69SJani Nikula    my $oldprefix = $lineprefix;
1080efa44475SMauro Carvalho Chehab    my $name;
1081c0d1b6eeSJonathan Corbet
1082efa44475SMauro Carvalho Chehab    if ($sphinx_major < 3) {
1083efa44475SMauro Carvalho Chehab	$name = "typedef " . $args{'typedef'};
1084efa44475SMauro Carvalho Chehab    } else {
1085efa44475SMauro Carvalho Chehab	$name = $args{'typedef'};
1086efa44475SMauro Carvalho Chehab    }
108762850976SJani Nikula    print "\n\n.. c:type:: " . $name . "\n\n";
10880b0f5f29SDaniel Vetter    print_lineno($declaration_start_line);
1089c099ff69SJani Nikula    $lineprefix = "   ";
1090c099ff69SJani Nikula    output_highlight_rst($args{'purpose'});
1091c099ff69SJani Nikula    print "\n";
1092c0d1b6eeSJonathan Corbet
1093c099ff69SJani Nikula    $lineprefix = $oldprefix;
1094c0d1b6eeSJonathan Corbet    output_section_rst(@_);
1095c0d1b6eeSJonathan Corbet}
1096c0d1b6eeSJonathan Corbet
1097c0d1b6eeSJonathan Corbetsub output_struct_rst(%) {
1098c0d1b6eeSJonathan Corbet    my %args = %{$_[0]};
1099c0d1b6eeSJonathan Corbet    my ($parameter);
1100c099ff69SJani Nikula    my $oldprefix = $lineprefix;
1101c0d1b6eeSJonathan Corbet
1102efa44475SMauro Carvalho Chehab    if ($sphinx_major < 3) {
1103efa44475SMauro Carvalho Chehab	my $name = $args{'type'} . " " . $args{'struct'};
110462850976SJani Nikula	print "\n\n.. c:type:: " . $name . "\n\n";
1105efa44475SMauro Carvalho Chehab    } else {
1106efa44475SMauro Carvalho Chehab	my $name = $args{'struct'};
110772b97d0bSMauro Carvalho Chehab	if ($args{'type'} eq 'union') {
110872b97d0bSMauro Carvalho Chehab	    print "\n\n.. c:union:: " . $name . "\n\n";
110972b97d0bSMauro Carvalho Chehab	} else {
1110efa44475SMauro Carvalho Chehab	    print "\n\n.. c:struct:: " . $name . "\n\n";
1111efa44475SMauro Carvalho Chehab	}
111272b97d0bSMauro Carvalho Chehab    }
11130b0f5f29SDaniel Vetter    print_lineno($declaration_start_line);
1114c099ff69SJani Nikula    $lineprefix = "   ";
1115c099ff69SJani Nikula    output_highlight_rst($args{'purpose'});
1116c099ff69SJani Nikula    print "\n";
1117c0d1b6eeSJonathan Corbet
1118ecbcfba1SJani Nikula    print "**Definition**\n\n";
1119c0d1b6eeSJonathan Corbet    print "::\n\n";
11208ad72163SMauro Carvalho Chehab    my $declaration = $args{'definition'};
11218ad72163SMauro Carvalho Chehab    $declaration =~ s/\t/  /g;
11228ad72163SMauro Carvalho Chehab    print "  " . $args{'type'} . " " . $args{'struct'} . " {\n$declaration  };\n\n";
1123c0d1b6eeSJonathan Corbet
1124ecbcfba1SJani Nikula    print "**Members**\n\n";
1125c099ff69SJani Nikula    $lineprefix = "  ";
1126c0d1b6eeSJonathan Corbet    foreach $parameter (@{$args{'parameterlist'}}) {
1127c0d1b6eeSJonathan Corbet	($parameter =~ /^#/) && next;
1128c0d1b6eeSJonathan Corbet
1129c0d1b6eeSJonathan Corbet	my $parameter_name = $parameter;
1130c0d1b6eeSJonathan Corbet	$parameter_name =~ s/\[.*//;
1131c0d1b6eeSJonathan Corbet
1132c0d1b6eeSJonathan Corbet	($args{'parameterdescs'}{$parameter_name} ne $undescribed) || next;
1133c0d1b6eeSJonathan Corbet	$type = $args{'parametertypes'}{$parameter};
11340b0f5f29SDaniel Vetter        print_lineno($parameterdesc_start_lines{$parameter_name});
11356d232c80SMauro Carvalho Chehab	print "``" . $parameter . "``\n";
1136c0d1b6eeSJonathan Corbet	output_highlight_rst($args{'parameterdescs'}{$parameter_name});
1137c0d1b6eeSJonathan Corbet	print "\n";
1138c0d1b6eeSJonathan Corbet    }
1139c0d1b6eeSJonathan Corbet    print "\n";
1140c099ff69SJani Nikula
1141c099ff69SJani Nikula    $lineprefix = $oldprefix;
1142c0d1b6eeSJonathan Corbet    output_section_rst(@_);
1143c0d1b6eeSJonathan Corbet}
1144c0d1b6eeSJonathan Corbet
11453a025e1dSMatthew Wilcox## none mode output functions
11463a025e1dSMatthew Wilcox
11473a025e1dSMatthew Wilcoxsub output_function_none(%) {
11483a025e1dSMatthew Wilcox}
11493a025e1dSMatthew Wilcox
11503a025e1dSMatthew Wilcoxsub output_enum_none(%) {
11513a025e1dSMatthew Wilcox}
11523a025e1dSMatthew Wilcox
11533a025e1dSMatthew Wilcoxsub output_typedef_none(%) {
11543a025e1dSMatthew Wilcox}
11553a025e1dSMatthew Wilcox
11563a025e1dSMatthew Wilcoxsub output_struct_none(%) {
11573a025e1dSMatthew Wilcox}
11583a025e1dSMatthew Wilcox
11593a025e1dSMatthew Wilcoxsub output_blockhead_none(%) {
11603a025e1dSMatthew Wilcox}
11613a025e1dSMatthew Wilcox
11621da177e4SLinus Torvalds##
116327205744SRandy Dunlap# generic output function for all types (function, struct/union, typedef, enum);
116427205744SRandy Dunlap# calls the generated, variable output_ function name based on
116527205744SRandy Dunlap# functype and output_mode
11661da177e4SLinus Torvaldssub output_declaration {
11671da177e4SLinus Torvalds    no strict 'refs';
11681da177e4SLinus Torvalds    my $name = shift;
11691da177e4SLinus Torvalds    my $functype = shift;
11701da177e4SLinus Torvalds    my $func = "output_${functype}_$output_mode";
1171eab795ddSMauro Carvalho Chehab
1172eab795ddSMauro Carvalho Chehab    return if (defined($nosymbol_table{$name}));
1173eab795ddSMauro Carvalho Chehab
1174b6c3f456SJani Nikula    if (($output_selection == OUTPUT_ALL) ||
1175b6c3f456SJani Nikula	(($output_selection == OUTPUT_INCLUDE ||
1176b6c3f456SJani Nikula	  $output_selection == OUTPUT_EXPORTED) &&
117786ae2e38SJani Nikula	 defined($function_table{$name})) ||
1178eab795ddSMauro Carvalho Chehab	($output_selection == OUTPUT_INTERNAL &&
117986ae2e38SJani Nikula	 !($functype eq "function" && defined($function_table{$name}))))
11801da177e4SLinus Torvalds    {
11811da177e4SLinus Torvalds	&$func(@_);
11821da177e4SLinus Torvalds	$section_counter++;
11831da177e4SLinus Torvalds    }
11841da177e4SLinus Torvalds}
11851da177e4SLinus Torvalds
11861da177e4SLinus Torvalds##
118727205744SRandy Dunlap# generic output function - calls the right one based on current output mode.
1188b112e0f7SJohannes Bergsub output_blockhead {
11891da177e4SLinus Torvalds    no strict 'refs';
1190b112e0f7SJohannes Berg    my $func = "output_blockhead_" . $output_mode;
11911da177e4SLinus Torvalds    &$func(@_);
11921da177e4SLinus Torvalds    $section_counter++;
11931da177e4SLinus Torvalds}
11941da177e4SLinus Torvalds
11951da177e4SLinus Torvalds##
11961da177e4SLinus Torvalds# takes a declaration (struct, union, enum, typedef) and
11971da177e4SLinus Torvalds# invokes the right handler. NOT called for functions.
11981da177e4SLinus Torvaldssub dump_declaration($$) {
11991da177e4SLinus Torvalds    no strict 'refs';
12001da177e4SLinus Torvalds    my ($prototype, $file) = @_;
12011da177e4SLinus Torvalds    my $func = "dump_" . $decl_type;
12021da177e4SLinus Torvalds    &$func(@_);
12031da177e4SLinus Torvalds}
12041da177e4SLinus Torvalds
12051da177e4SLinus Torvaldssub dump_union($$) {
12061da177e4SLinus Torvalds    dump_struct(@_);
12071da177e4SLinus Torvalds}
12081da177e4SLinus Torvalds
12091da177e4SLinus Torvaldssub dump_struct($$) {
12101da177e4SLinus Torvalds    my $x = shift;
12111da177e4SLinus Torvalds    my $file = shift;
1212a746fe32SAditya Srivastava    my $decl_type;
1213a746fe32SAditya Srivastava    my $members;
1214a746fe32SAditya Srivastava    my $type = qr{struct|union};
1215a746fe32SAditya Srivastava    # For capturing struct/union definition body, i.e. "{members*}qualifiers*"
1216e86bdb24SAditya Srivastava    my $qualifiers = qr{$attribute|__packed|__aligned|____cacheline_aligned_in_smp|____cacheline_aligned};
1217e86bdb24SAditya Srivastava    my $definition_body = qr{\{(.*)\}\s*$qualifiers*};
1218e86bdb24SAditya Srivastava    my $struct_members = qr{($type)([^\{\};]+)\{([^\{\}]*)\}([^\{\}\;]*)\;};
12191da177e4SLinus Torvalds
1220a746fe32SAditya Srivastava    if ($x =~ /($type)\s+(\w+)\s*$definition_body/) {
1221a746fe32SAditya Srivastava	$decl_type = $1;
12221da177e4SLinus Torvalds	$declaration_name = $2;
1223a746fe32SAditya Srivastava	$members = $3;
1224a746fe32SAditya Srivastava    } elsif ($x =~ /typedef\s+($type)\s*$definition_body\s*(\w+)\s*;/) {
1225a746fe32SAditya Srivastava	$decl_type = $1;
1226a746fe32SAditya Srivastava	$declaration_name = $3;
1227a746fe32SAditya Srivastava	$members = $2;
1228a746fe32SAditya Srivastava    }
12291da177e4SLinus Torvalds
1230a746fe32SAditya Srivastava    if ($members) {
123152042e2dSMauro Carvalho Chehab	if ($identifier ne $declaration_name) {
123252042e2dSMauro Carvalho Chehab	    print STDERR "${file}:$.: warning: expecting prototype for $decl_type $identifier. Prototype was for $decl_type $declaration_name instead\n";
123352042e2dSMauro Carvalho Chehab	    return;
123452042e2dSMauro Carvalho Chehab	}
123552042e2dSMauro Carvalho Chehab
1236aeec46b9SMartin Waitz	# ignore members marked private:
12370d8c39e6SMauro Carvalho Chehab	$members =~ s/\/\*\s*private:.*?\/\*\s*public:.*?\*\///gosi;
12380d8c39e6SMauro Carvalho Chehab	$members =~ s/\/\*\s*private:.*//gosi;
1239aeec46b9SMartin Waitz	# strip comments:
1240aeec46b9SMartin Waitz	$members =~ s/\/\*.*?\*\///gos;
1241ef5da59fSRandy Dunlap	# strip attributes
1242e86bdb24SAditya Srivastava	$members =~ s/\s*$attribute/ /gi;
12433d9bfb19SSakari Ailus	$members =~ s/\s*__aligned\s*\([^;]*\)/ /gos;
12443d9bfb19SSakari Ailus	$members =~ s/\s*__packed\s*/ /gos;
1245f0074929SJonathan Corbet	$members =~ s/\s*CRYPTO_MINALIGN_ATTR/ /gos;
1246f861537dSAndré Almeida	$members =~ s/\s*____cacheline_aligned_in_smp/ /gos;
1247a070991fSJonathan Cameron	$members =~ s/\s*____cacheline_aligned/ /gos;
12483556108eSMauro Carvalho Chehab
1249e86bdb24SAditya Srivastava	my $args = qr{([^,)]+)};
1250b22b5a9eSConchúr Navid	# replace DECLARE_BITMAP
12513556108eSMauro Carvalho Chehab	$members =~ s/__ETHTOOL_DECLARE_LINK_MODE_MASK\s*\(([^\)]+)\)/DECLARE_BITMAP($1, __ETHTOOL_LINK_MODE_MASK_NBITS)/gos;
1252e86bdb24SAditya Srivastava	$members =~ s/DECLARE_BITMAP\s*\($args,\s*$args\)/unsigned long $1\[BITS_TO_LONGS($2)\]/gos;
12531cb566baSJakub Kicinski	# replace DECLARE_HASHTABLE
1254e86bdb24SAditya Srivastava	$members =~ s/DECLARE_HASHTABLE\s*\($args,\s*$args\)/unsigned long $1\[1 << (($2) - 1)\]/gos;
125545005b27SMauro Carvalho Chehab	# replace DECLARE_KFIFO
1256e86bdb24SAditya Srivastava	$members =~ s/DECLARE_KFIFO\s*\($args,\s*$args,\s*$args\)/$2 \*$1/gos;
125745005b27SMauro Carvalho Chehab	# replace DECLARE_KFIFO_PTR
1258e86bdb24SAditya Srivastava	$members =~ s/DECLARE_KFIFO_PTR\s*\($args,\s*$args\)/$2 \*$1/gos;
12598ad72163SMauro Carvalho Chehab	my $declaration = $members;
12608ad72163SMauro Carvalho Chehab
12618ad72163SMauro Carvalho Chehab	# Split nested struct/union elements as newer ones
1262e86bdb24SAditya Srivastava	while ($members =~ m/$struct_members/) {
126384ce5b98SMauro Carvalho Chehab		my $newmember;
126484ce5b98SMauro Carvalho Chehab		my $maintype = $1;
126584ce5b98SMauro Carvalho Chehab		my $ids = $4;
12668ad72163SMauro Carvalho Chehab		my $content = $3;
126784ce5b98SMauro Carvalho Chehab		foreach my $id(split /,/, $ids) {
126884ce5b98SMauro Carvalho Chehab			$newmember .= "$maintype $id; ";
126984ce5b98SMauro Carvalho Chehab
12708ad72163SMauro Carvalho Chehab			$id =~ s/[:\[].*//;
127184ce5b98SMauro Carvalho Chehab			$id =~ s/^\s*\**(\S+)\s*/$1/;
12728ad72163SMauro Carvalho Chehab			foreach my $arg (split /;/, $content) {
12738ad72163SMauro Carvalho Chehab				next if ($arg =~ m/^\s*$/);
12747c0d7e87SMauro Carvalho Chehab				if ($arg =~ m/^([^\(]+\(\*?\s*)([\w\.]*)(\s*\).*)/) {
12757c0d7e87SMauro Carvalho Chehab					# pointer-to-function
12767c0d7e87SMauro Carvalho Chehab					my $type = $1;
12777c0d7e87SMauro Carvalho Chehab					my $name = $2;
12787c0d7e87SMauro Carvalho Chehab					my $extra = $3;
12797c0d7e87SMauro Carvalho Chehab					next if (!$name);
12807c0d7e87SMauro Carvalho Chehab					if ($id =~ m/^\s*$/) {
12817c0d7e87SMauro Carvalho Chehab						# anonymous struct/union
12827c0d7e87SMauro Carvalho Chehab						$newmember .= "$type$name$extra; ";
12837c0d7e87SMauro Carvalho Chehab					} else {
12847c0d7e87SMauro Carvalho Chehab						$newmember .= "$type$id.$name$extra; ";
12857c0d7e87SMauro Carvalho Chehab					}
12867c0d7e87SMauro Carvalho Chehab				} else {
128784ce5b98SMauro Carvalho Chehab					my $type;
128884ce5b98SMauro Carvalho Chehab					my $names;
128984ce5b98SMauro Carvalho Chehab					$arg =~ s/^\s+//;
129084ce5b98SMauro Carvalho Chehab					$arg =~ s/\s+$//;
129184ce5b98SMauro Carvalho Chehab					# Handle bitmaps
129284ce5b98SMauro Carvalho Chehab					$arg =~ s/:\s*\d+\s*//g;
129384ce5b98SMauro Carvalho Chehab					# Handle arrays
1294d404d579SMauro Carvalho Chehab					$arg =~ s/\[.*\]//g;
129584ce5b98SMauro Carvalho Chehab					# The type may have multiple words,
129684ce5b98SMauro Carvalho Chehab					# and multiple IDs can be defined, like:
129784ce5b98SMauro Carvalho Chehab					#	const struct foo, *bar, foobar
129884ce5b98SMauro Carvalho Chehab					# So, we remove spaces when parsing the
129984ce5b98SMauro Carvalho Chehab					# names, in order to match just names
130084ce5b98SMauro Carvalho Chehab					# and commas for the names
130184ce5b98SMauro Carvalho Chehab					$arg =~ s/\s*,\s*/,/g;
130284ce5b98SMauro Carvalho Chehab					if ($arg =~ m/(.*)\s+([\S+,]+)/) {
130384ce5b98SMauro Carvalho Chehab						$type = $1;
130484ce5b98SMauro Carvalho Chehab						$names = $2;
130584ce5b98SMauro Carvalho Chehab					} else {
130684ce5b98SMauro Carvalho Chehab						$newmember .= "$arg; ";
130784ce5b98SMauro Carvalho Chehab						next;
130884ce5b98SMauro Carvalho Chehab					}
130984ce5b98SMauro Carvalho Chehab					foreach my $name (split /,/, $names) {
131084ce5b98SMauro Carvalho Chehab						$name =~ s/^\s*\**(\S+)\s*/$1/;
13118ad72163SMauro Carvalho Chehab						next if (($name =~ m/^\s*$/));
13128ad72163SMauro Carvalho Chehab						if ($id =~ m/^\s*$/) {
13138ad72163SMauro Carvalho Chehab							# anonymous struct/union
13148ad72163SMauro Carvalho Chehab							$newmember .= "$type $name; ";
13158ad72163SMauro Carvalho Chehab						} else {
13168ad72163SMauro Carvalho Chehab							$newmember .= "$type $id.$name; ";
13178ad72163SMauro Carvalho Chehab						}
13188ad72163SMauro Carvalho Chehab					}
13197c0d7e87SMauro Carvalho Chehab				}
132084ce5b98SMauro Carvalho Chehab			}
132184ce5b98SMauro Carvalho Chehab		}
1322e86bdb24SAditya Srivastava		$members =~ s/$struct_members/$newmember/;
132384ce5b98SMauro Carvalho Chehab	}
13248ad72163SMauro Carvalho Chehab
13258ad72163SMauro Carvalho Chehab	# Ignore other nested elements, like enums
1326673bb2dfSBen Hutchings	$members =~ s/(\{[^\{\}]*\})//g;
13278ad72163SMauro Carvalho Chehab
1328151c468bSMauro Carvalho Chehab	create_parameterlist($members, ';', $file, $declaration_name);
13291081de2dSMauro Carvalho Chehab	check_sections($file, $declaration_name, $decl_type, $sectcheck, $struct_actual);
13301da177e4SLinus Torvalds
13318ad72163SMauro Carvalho Chehab	# Adjust declaration for better display
1332673bb2dfSBen Hutchings	$declaration =~ s/([\{;])/$1\n/g;
1333673bb2dfSBen Hutchings	$declaration =~ s/\}\s+;/};/g;
13348ad72163SMauro Carvalho Chehab	# Better handle inlined enums
1335673bb2dfSBen Hutchings	do {} while ($declaration =~ s/(enum\s+\{[^\}]+),([^\n])/$1,\n$2/);
13368ad72163SMauro Carvalho Chehab
13378ad72163SMauro Carvalho Chehab	my @def_args = split /\n/, $declaration;
13388ad72163SMauro Carvalho Chehab	my $level = 1;
13398ad72163SMauro Carvalho Chehab	$declaration = "";
13408ad72163SMauro Carvalho Chehab	foreach my $clause (@def_args) {
13418ad72163SMauro Carvalho Chehab		$clause =~ s/^\s+//;
13428ad72163SMauro Carvalho Chehab		$clause =~ s/\s+$//;
13438ad72163SMauro Carvalho Chehab		$clause =~ s/\s+/ /;
13448ad72163SMauro Carvalho Chehab		next if (!$clause);
1345673bb2dfSBen Hutchings		$level-- if ($clause =~ m/(\})/ && $level > 1);
13468ad72163SMauro Carvalho Chehab		if (!($clause =~ m/^\s*#/)) {
13478ad72163SMauro Carvalho Chehab			$declaration .= "\t" x $level;
13488ad72163SMauro Carvalho Chehab		}
13498ad72163SMauro Carvalho Chehab		$declaration .= "\t" . $clause . "\n";
1350673bb2dfSBen Hutchings		$level++ if ($clause =~ m/(\{)/ && !($clause =~m/\}/));
13518ad72163SMauro Carvalho Chehab	}
13521da177e4SLinus Torvalds	output_declaration($declaration_name,
13531da177e4SLinus Torvalds			   'struct',
13541da177e4SLinus Torvalds			   {'struct' => $declaration_name,
13551da177e4SLinus Torvalds			    'module' => $modulename,
13568ad72163SMauro Carvalho Chehab			    'definition' => $declaration,
13571da177e4SLinus Torvalds			    'parameterlist' => \@parameterlist,
13581da177e4SLinus Torvalds			    'parameterdescs' => \%parameterdescs,
13591da177e4SLinus Torvalds			    'parametertypes' => \%parametertypes,
13601da177e4SLinus Torvalds			    'sectionlist' => \@sectionlist,
13611da177e4SLinus Torvalds			    'sections' => \%sections,
13621da177e4SLinus Torvalds			    'purpose' => $declaration_purpose,
13631da177e4SLinus Torvalds			    'type' => $decl_type
13641da177e4SLinus Torvalds			   });
13651da177e4SLinus Torvalds    }
13661da177e4SLinus Torvalds    else {
1367d40e1e65SBart Van Assche	print STDERR "${file}:$.: error: Cannot parse struct or union!\n";
13681da177e4SLinus Torvalds	++$errors;
13691da177e4SLinus Torvalds    }
13701da177e4SLinus Torvalds}
13711da177e4SLinus Torvalds
137285afe608SMauro Carvalho Chehab
137385afe608SMauro Carvalho Chehabsub show_warnings($$) {
137485afe608SMauro Carvalho Chehab	my $functype = shift;
137585afe608SMauro Carvalho Chehab	my $name = shift;
137685afe608SMauro Carvalho Chehab
1377eab795ddSMauro Carvalho Chehab	return 0 if (defined($nosymbol_table{$name}));
1378eab795ddSMauro Carvalho Chehab
137985afe608SMauro Carvalho Chehab	return 1 if ($output_selection == OUTPUT_ALL);
138085afe608SMauro Carvalho Chehab
138185afe608SMauro Carvalho Chehab	if ($output_selection == OUTPUT_EXPORTED) {
138285afe608SMauro Carvalho Chehab		if (defined($function_table{$name})) {
138385afe608SMauro Carvalho Chehab			return 1;
138485afe608SMauro Carvalho Chehab		} else {
138585afe608SMauro Carvalho Chehab			return 0;
138685afe608SMauro Carvalho Chehab		}
138785afe608SMauro Carvalho Chehab	}
138885afe608SMauro Carvalho Chehab        if ($output_selection == OUTPUT_INTERNAL) {
138985afe608SMauro Carvalho Chehab		if (!($functype eq "function" && defined($function_table{$name}))) {
139085afe608SMauro Carvalho Chehab			return 1;
139185afe608SMauro Carvalho Chehab		} else {
139285afe608SMauro Carvalho Chehab			return 0;
139385afe608SMauro Carvalho Chehab		}
139485afe608SMauro Carvalho Chehab	}
139585afe608SMauro Carvalho Chehab	if ($output_selection == OUTPUT_INCLUDE) {
139685afe608SMauro Carvalho Chehab		if (defined($function_table{$name})) {
139785afe608SMauro Carvalho Chehab			return 1;
139885afe608SMauro Carvalho Chehab		} else {
139985afe608SMauro Carvalho Chehab			return 0;
140085afe608SMauro Carvalho Chehab		}
140185afe608SMauro Carvalho Chehab	}
140285afe608SMauro Carvalho Chehab	die("Please add the new output type at show_warnings()");
140385afe608SMauro Carvalho Chehab}
140485afe608SMauro Carvalho Chehab
14051da177e4SLinus Torvaldssub dump_enum($$) {
14061da177e4SLinus Torvalds    my $x = shift;
14071da177e4SLinus Torvalds    my $file = shift;
1408d38c8cfbSMauro Carvalho Chehab    my $members;
1409d38c8cfbSMauro Carvalho Chehab
14101da177e4SLinus Torvalds
1411aeec46b9SMartin Waitz    $x =~ s@/\*.*?\*/@@gos;	# strip comments.
14124468e21eSConchúr Navid    # strip #define macros inside enums
14134468e21eSConchúr Navid    $x =~ s@#\s*((define|ifdef)\s+|endif)[^;]*;@@gos;
1414b6d676dbSRandy Dunlap
1415d38c8cfbSMauro Carvalho Chehab    if ($x =~ /typedef\s+enum\s*\{(.*)\}\s*(\w*)\s*;/) {
1416d38c8cfbSMauro Carvalho Chehab	$declaration_name = $2;
1417d38c8cfbSMauro Carvalho Chehab	$members = $1;
1418d38c8cfbSMauro Carvalho Chehab    } elsif ($x =~ /enum\s+(\w*)\s*\{(.*)\}/) {
14191da177e4SLinus Torvalds	$declaration_name = $1;
1420d38c8cfbSMauro Carvalho Chehab	$members = $2;
1421d38c8cfbSMauro Carvalho Chehab    }
1422d38c8cfbSMauro Carvalho Chehab
1423ae5b17e4SAndy Shevchenko    if ($members) {
142452042e2dSMauro Carvalho Chehab	if ($identifier ne $declaration_name) {
14250b54c2e3SMauro Carvalho Chehab	    if ($identifier eq "") {
14260b54c2e3SMauro Carvalho Chehab		print STDERR "${file}:$.: warning: wrong kernel-doc identifier on line:\n";
14270b54c2e3SMauro Carvalho Chehab	    } else {
142852042e2dSMauro Carvalho Chehab		print STDERR "${file}:$.: warning: expecting prototype for enum $identifier. Prototype was for enum $declaration_name instead\n";
14290b54c2e3SMauro Carvalho Chehab	    }
143052042e2dSMauro Carvalho Chehab	    return;
143152042e2dSMauro Carvalho Chehab	}
14320b54c2e3SMauro Carvalho Chehab	$declaration_name = "(anonymous)" if ($declaration_name eq "");
143352042e2dSMauro Carvalho Chehab
14345cb5c31cSJohannes Berg	my %_members;
14355cb5c31cSJohannes Berg
1436463a0fdcSMarkus Heiser	$members =~ s/\s+$//;
14371da177e4SLinus Torvalds
14381da177e4SLinus Torvalds	foreach my $arg (split ',', $members) {
14391da177e4SLinus Torvalds	    $arg =~ s/^\s*(\w+).*/$1/;
14401da177e4SLinus Torvalds	    push @parameterlist, $arg;
14411da177e4SLinus Torvalds	    if (!$parameterdescs{$arg}) {
14421da177e4SLinus Torvalds		$parameterdescs{$arg} = $undescribed;
144385afe608SMauro Carvalho Chehab	        if (show_warnings("enum", $declaration_name)) {
14442defb272SMauro Carvalho Chehab			print STDERR "${file}:$.: warning: Enum value '$arg' not described in enum '$declaration_name'\n";
14452defb272SMauro Carvalho Chehab		}
14461da177e4SLinus Torvalds	    }
14475cb5c31cSJohannes Berg	    $_members{$arg} = 1;
14485cb5c31cSJohannes Berg	}
14491da177e4SLinus Torvalds
14505cb5c31cSJohannes Berg	while (my ($k, $v) = each %parameterdescs) {
14515cb5c31cSJohannes Berg	    if (!exists($_members{$k})) {
145285afe608SMauro Carvalho Chehab	        if (show_warnings("enum", $declaration_name)) {
14532defb272SMauro Carvalho Chehab		     print STDERR "${file}:$.: warning: Excess enum value '$k' description in '$declaration_name'\n";
14542defb272SMauro Carvalho Chehab		}
14555cb5c31cSJohannes Berg	    }
14561da177e4SLinus Torvalds        }
14571da177e4SLinus Torvalds
14581da177e4SLinus Torvalds	output_declaration($declaration_name,
14591da177e4SLinus Torvalds			   'enum',
14601da177e4SLinus Torvalds			   {'enum' => $declaration_name,
14611da177e4SLinus Torvalds			    'module' => $modulename,
14621da177e4SLinus Torvalds			    'parameterlist' => \@parameterlist,
14631da177e4SLinus Torvalds			    'parameterdescs' => \%parameterdescs,
14641da177e4SLinus Torvalds			    'sectionlist' => \@sectionlist,
14651da177e4SLinus Torvalds			    'sections' => \%sections,
14661da177e4SLinus Torvalds			    'purpose' => $declaration_purpose
14671da177e4SLinus Torvalds			   });
1468d38c8cfbSMauro Carvalho Chehab    } else {
1469d40e1e65SBart Van Assche	print STDERR "${file}:$.: error: Cannot parse enum!\n";
14701da177e4SLinus Torvalds	++$errors;
14711da177e4SLinus Torvalds    }
14721da177e4SLinus Torvalds}
14731da177e4SLinus Torvalds
14747d2c6b1eSMauro Carvalho Chehabmy $typedef_type = qr { ((?:\s+[\w\*]+\b){1,8})\s* }x;
14757efc6c42SMauro Carvalho Chehabmy $typedef_ident = qr { \*?\s*(\w\S+)\s* }x;
14767efc6c42SMauro Carvalho Chehabmy $typedef_args = qr { \s*\((.*)\); }x;
14777efc6c42SMauro Carvalho Chehab
14787efc6c42SMauro Carvalho Chehabmy $typedef1 = qr { typedef$typedef_type\($typedef_ident\)$typedef_args }x;
14797efc6c42SMauro Carvalho Chehabmy $typedef2 = qr { typedef$typedef_type$typedef_ident$typedef_args }x;
14807efc6c42SMauro Carvalho Chehab
14811da177e4SLinus Torvaldssub dump_typedef($$) {
14821da177e4SLinus Torvalds    my $x = shift;
14831da177e4SLinus Torvalds    my $file = shift;
14841da177e4SLinus Torvalds
1485aeec46b9SMartin Waitz    $x =~ s@/\*.*?\*/@@gos;	# strip comments.
148683766452SMauro Carvalho Chehab
14877efc6c42SMauro Carvalho Chehab    # Parse function typedef prototypes
14887efc6c42SMauro Carvalho Chehab    if ($x =~ $typedef1 || $x =~ $typedef2) {
148983766452SMauro Carvalho Chehab	$return_type = $1;
149083766452SMauro Carvalho Chehab	$declaration_name = $2;
149183766452SMauro Carvalho Chehab	my $args = $3;
14926b80975cSMauro Carvalho Chehab	$return_type =~ s/^\s+//;
149383766452SMauro Carvalho Chehab
149452042e2dSMauro Carvalho Chehab	if ($identifier ne $declaration_name) {
149552042e2dSMauro Carvalho Chehab	    print STDERR "${file}:$.: warning: expecting prototype for typedef $identifier. Prototype was for typedef $declaration_name instead\n";
149652042e2dSMauro Carvalho Chehab	    return;
149752042e2dSMauro Carvalho Chehab	}
149852042e2dSMauro Carvalho Chehab
1499151c468bSMauro Carvalho Chehab	create_parameterlist($args, ',', $file, $declaration_name);
150083766452SMauro Carvalho Chehab
150183766452SMauro Carvalho Chehab	output_declaration($declaration_name,
150283766452SMauro Carvalho Chehab			   'function',
150383766452SMauro Carvalho Chehab			   {'function' => $declaration_name,
150482801d06SMauro Carvalho Chehab			    'typedef' => 1,
150583766452SMauro Carvalho Chehab			    'module' => $modulename,
150683766452SMauro Carvalho Chehab			    'functiontype' => $return_type,
150783766452SMauro Carvalho Chehab			    'parameterlist' => \@parameterlist,
150883766452SMauro Carvalho Chehab			    'parameterdescs' => \%parameterdescs,
150983766452SMauro Carvalho Chehab			    'parametertypes' => \%parametertypes,
151083766452SMauro Carvalho Chehab			    'sectionlist' => \@sectionlist,
151183766452SMauro Carvalho Chehab			    'sections' => \%sections,
151283766452SMauro Carvalho Chehab			    'purpose' => $declaration_purpose
151383766452SMauro Carvalho Chehab			   });
151483766452SMauro Carvalho Chehab	return;
151583766452SMauro Carvalho Chehab    }
151683766452SMauro Carvalho Chehab
15171da177e4SLinus Torvalds    while (($x =~ /\(*.\)\s*;$/) || ($x =~ /\[*.\]\s*;$/)) {
15181da177e4SLinus Torvalds	$x =~ s/\(*.\)\s*;$/;/;
15191da177e4SLinus Torvalds	$x =~ s/\[*.\]\s*;$/;/;
15201da177e4SLinus Torvalds    }
15211da177e4SLinus Torvalds
15221da177e4SLinus Torvalds    if ($x =~ /typedef.*\s+(\w+)\s*;/) {
15231da177e4SLinus Torvalds	$declaration_name = $1;
15241da177e4SLinus Torvalds
152552042e2dSMauro Carvalho Chehab	if ($identifier ne $declaration_name) {
152652042e2dSMauro Carvalho Chehab	    print STDERR "${file}:$.: warning: expecting prototype for typedef $identifier. Prototype was for typedef $declaration_name instead\n";
152752042e2dSMauro Carvalho Chehab	    return;
152852042e2dSMauro Carvalho Chehab	}
152952042e2dSMauro Carvalho Chehab
15301da177e4SLinus Torvalds	output_declaration($declaration_name,
15311da177e4SLinus Torvalds			   'typedef',
15321da177e4SLinus Torvalds			   {'typedef' => $declaration_name,
15331da177e4SLinus Torvalds			    'module' => $modulename,
15341da177e4SLinus Torvalds			    'sectionlist' => \@sectionlist,
15351da177e4SLinus Torvalds			    'sections' => \%sections,
15361da177e4SLinus Torvalds			    'purpose' => $declaration_purpose
15371da177e4SLinus Torvalds			   });
15381da177e4SLinus Torvalds    }
15391da177e4SLinus Torvalds    else {
1540d40e1e65SBart Van Assche	print STDERR "${file}:$.: error: Cannot parse typedef!\n";
15411da177e4SLinus Torvalds	++$errors;
15421da177e4SLinus Torvalds    }
15431da177e4SLinus Torvalds}
15441da177e4SLinus Torvalds
1545a1d94aa5SRandy Dunlapsub save_struct_actual($) {
1546a1d94aa5SRandy Dunlap    my $actual = shift;
1547a1d94aa5SRandy Dunlap
1548a1d94aa5SRandy Dunlap    # strip all spaces from the actual param so that it looks like one string item
1549a1d94aa5SRandy Dunlap    $actual =~ s/\s*//g;
1550a1d94aa5SRandy Dunlap    $struct_actual = $struct_actual . $actual . " ";
1551a1d94aa5SRandy Dunlap}
1552a1d94aa5SRandy Dunlap
1553151c468bSMauro Carvalho Chehabsub create_parameterlist($$$$) {
15541da177e4SLinus Torvalds    my $args = shift;
15551da177e4SLinus Torvalds    my $splitter = shift;
15561da177e4SLinus Torvalds    my $file = shift;
1557151c468bSMauro Carvalho Chehab    my $declaration_name = shift;
15581da177e4SLinus Torvalds    my $type;
15591da177e4SLinus Torvalds    my $param;
15601da177e4SLinus Torvalds
1561a6d3fe77SMartin Waitz    # temporarily replace commas inside function pointer definition
1562e86bdb24SAditya Srivastava    my $arg_expr = qr{\([^\),]+};
1563e86bdb24SAditya Srivastava    while ($args =~ /$arg_expr,/) {
1564e86bdb24SAditya Srivastava	$args =~ s/($arg_expr),/$1#/g;
15651da177e4SLinus Torvalds    }
15661da177e4SLinus Torvalds
15671da177e4SLinus Torvalds    foreach my $arg (split($splitter, $args)) {
15681da177e4SLinus Torvalds	# strip comments
15691da177e4SLinus Torvalds	$arg =~ s/\/\*.*\*\///;
15701da177e4SLinus Torvalds	# strip leading/trailing spaces
15711da177e4SLinus Torvalds	$arg =~ s/^\s*//;
15721da177e4SLinus Torvalds	$arg =~ s/\s*$//;
15731da177e4SLinus Torvalds	$arg =~ s/\s+/ /;
15741da177e4SLinus Torvalds
15751da177e4SLinus Torvalds	if ($arg =~ /^#/) {
15761da177e4SLinus Torvalds	    # Treat preprocessor directive as a typeless variable just to fill
15771da177e4SLinus Torvalds	    # corresponding data structures "correctly". Catch it later in
15781da177e4SLinus Torvalds	    # output_* subs.
1579ed8348e2SMauro Carvalho Chehab	    push_parameter($arg, "", "", $file);
158000d62961SRichard Kennedy	} elsif ($arg =~ m/\(.+\)\s*\(/) {
15811da177e4SLinus Torvalds	    # pointer-to-function
15821da177e4SLinus Torvalds	    $arg =~ tr/#/,/;
1583336ced2dSAditya Srivastava	    $arg =~ m/[^\(]+\(\*?\s*([\w\[\]\.]*)\s*\)/;
15841da177e4SLinus Torvalds	    $param = $1;
15851da177e4SLinus Torvalds	    $type = $arg;
158600d62961SRichard Kennedy	    $type =~ s/([^\(]+\(\*?)\s*$param/$1/;
1587a1d94aa5SRandy Dunlap	    save_struct_actual($param);
1588ed8348e2SMauro Carvalho Chehab	    push_parameter($param, $type, $arg, $file, $declaration_name);
1589aeec46b9SMartin Waitz	} elsif ($arg) {
15901da177e4SLinus Torvalds	    $arg =~ s/\s*:\s*/:/g;
15911da177e4SLinus Torvalds	    $arg =~ s/\s*\[/\[/g;
15921da177e4SLinus Torvalds
15931da177e4SLinus Torvalds	    my @args = split('\s*,\s*', $arg);
15941da177e4SLinus Torvalds	    if ($args[0] =~ m/\*/) {
15951da177e4SLinus Torvalds		$args[0] =~ s/(\*+)\s*/ $1/;
15961da177e4SLinus Torvalds	    }
1597884f2810SBorislav Petkov
1598884f2810SBorislav Petkov	    my @first_arg;
1599884f2810SBorislav Petkov	    if ($args[0] =~ /^(.*\s+)(.*?\[.*\].*)$/) {
1600884f2810SBorislav Petkov		    shift @args;
1601884f2810SBorislav Petkov		    push(@first_arg, split('\s+', $1));
1602884f2810SBorislav Petkov		    push(@first_arg, $2);
1603884f2810SBorislav Petkov	    } else {
1604884f2810SBorislav Petkov		    @first_arg = split('\s+', shift @args);
1605884f2810SBorislav Petkov	    }
1606884f2810SBorislav Petkov
16071da177e4SLinus Torvalds	    unshift(@args, pop @first_arg);
16081da177e4SLinus Torvalds	    $type = join " ", @first_arg;
16091da177e4SLinus Torvalds
16101da177e4SLinus Torvalds	    foreach $param (@args) {
16111da177e4SLinus Torvalds		if ($param =~ m/^(\*+)\s*(.*)/) {
1612a1d94aa5SRandy Dunlap		    save_struct_actual($2);
1613ed8348e2SMauro Carvalho Chehab
1614ed8348e2SMauro Carvalho Chehab		    push_parameter($2, "$type $1", $arg, $file, $declaration_name);
16151da177e4SLinus Torvalds		}
16161da177e4SLinus Torvalds		elsif ($param =~ m/(.*?):(\d+)/) {
16177b97887eSRandy Dunlap		    if ($type ne "") { # skip unnamed bit-fields
1618a1d94aa5SRandy Dunlap			save_struct_actual($1);
1619ed8348e2SMauro Carvalho Chehab			push_parameter($1, "$type:$2", $arg, $file, $declaration_name)
16201da177e4SLinus Torvalds		    }
16217b97887eSRandy Dunlap		}
16221da177e4SLinus Torvalds		else {
1623a1d94aa5SRandy Dunlap		    save_struct_actual($param);
1624ed8348e2SMauro Carvalho Chehab		    push_parameter($param, $type, $arg, $file, $declaration_name);
16251da177e4SLinus Torvalds		}
16261da177e4SLinus Torvalds	    }
16271da177e4SLinus Torvalds	}
16281da177e4SLinus Torvalds    }
16291da177e4SLinus Torvalds}
16301da177e4SLinus Torvalds
1631ed8348e2SMauro Carvalho Chehabsub push_parameter($$$$$) {
16321da177e4SLinus Torvalds	my $param = shift;
16331da177e4SLinus Torvalds	my $type = shift;
1634ed8348e2SMauro Carvalho Chehab	my $org_arg = shift;
16351da177e4SLinus Torvalds	my $file = shift;
1636151c468bSMauro Carvalho Chehab	my $declaration_name = shift;
16371da177e4SLinus Torvalds
16385f8c7c98SRandy Dunlap	if (($anon_struct_union == 1) && ($type eq "") &&
16395f8c7c98SRandy Dunlap	    ($param eq "}")) {
16405f8c7c98SRandy Dunlap		return;		# ignore the ending }; from anon. struct/union
16415f8c7c98SRandy Dunlap	}
16425f8c7c98SRandy Dunlap
16435f8c7c98SRandy Dunlap	$anon_struct_union = 0;
1644f9b5c530SMauro Carvalho Chehab	$param =~ s/[\[\)].*//;
16451da177e4SLinus Torvalds
1646a6d3fe77SMartin Waitz	if ($type eq "" && $param =~ /\.\.\.$/)
16471da177e4SLinus Torvalds	{
1648c950a173SSilvio Fricke	    if (!$param =~ /\w\.\.\.$/) {
1649c950a173SSilvio Fricke	      # handles unnamed variable parameters
1650ef00028bSJonathan Corbet	      $param = "...";
1651c950a173SSilvio Fricke	    }
165243756e34SJonathan Neuschäfer	    elsif ($param =~ /\w\.\.\.$/) {
165343756e34SJonathan Neuschäfer	      # for named variable parameters of the form `x...`, remove the dots
165443756e34SJonathan Neuschäfer	      $param =~ s/\.\.\.$//;
165543756e34SJonathan Neuschäfer	    }
1656ced69090SRandy Dunlap	    if (!defined $parameterdescs{$param} || $parameterdescs{$param} eq "") {
1657a6d3fe77SMartin Waitz		$parameterdescs{$param} = "variable arguments";
16581da177e4SLinus Torvalds	    }
1659ced69090SRandy Dunlap	}
16601da177e4SLinus Torvalds	elsif ($type eq "" && ($param eq "" or $param eq "void"))
16611da177e4SLinus Torvalds	{
16621da177e4SLinus Torvalds	    $param="void";
16631da177e4SLinus Torvalds	    $parameterdescs{void} = "no arguments";
16641da177e4SLinus Torvalds	}
1665134fe01bSRandy Dunlap	elsif ($type eq "" && ($param eq "struct" or $param eq "union"))
1666134fe01bSRandy Dunlap	# handle unnamed (anonymous) union or struct:
1667134fe01bSRandy Dunlap	{
1668134fe01bSRandy Dunlap		$type = $param;
1669134fe01bSRandy Dunlap		$param = "{unnamed_" . $param . "}";
1670134fe01bSRandy Dunlap		$parameterdescs{$param} = "anonymous\n";
16715f8c7c98SRandy Dunlap		$anon_struct_union = 1;
1672134fe01bSRandy Dunlap	}
1673134fe01bSRandy Dunlap
1674a6d3fe77SMartin Waitz	# warn if parameter has no description
1675134fe01bSRandy Dunlap	# (but ignore ones starting with # as these are not parameters
1676134fe01bSRandy Dunlap	# but inline preprocessor statements);
1677151c468bSMauro Carvalho Chehab	# Note: It will also ignore void params and unnamed structs/unions
1678f9b5c530SMauro Carvalho Chehab	if (!defined $parameterdescs{$param} && $param !~ /^#/) {
1679f9b5c530SMauro Carvalho Chehab		$parameterdescs{$param} = $undescribed;
16801da177e4SLinus Torvalds
1681be5cd20cSJonathan Corbet	        if (show_warnings($type, $declaration_name) && $param !~ /\./) {
1682151c468bSMauro Carvalho Chehab			print STDERR
1683151c468bSMauro Carvalho Chehab			      "${file}:$.: warning: Function parameter or member '$param' not described in '$declaration_name'\n";
16841da177e4SLinus Torvalds			++$warnings;
16851da177e4SLinus Torvalds		}
16862defb272SMauro Carvalho Chehab	}
16871da177e4SLinus Torvalds
168825985edcSLucas De Marchi	# strip spaces from $param so that it is one continuous string
1689e34e7dbbSRandy Dunlap	# on @parameterlist;
1690e34e7dbbSRandy Dunlap	# this fixes a problem where check_sections() cannot find
1691e34e7dbbSRandy Dunlap	# a parameter like "addr[6 + 2]" because it actually appears
1692e34e7dbbSRandy Dunlap	# as "addr[6", "+", "2]" on the parameter list;
1693e34e7dbbSRandy Dunlap	# but it's better to maintain the param string unchanged for output,
1694e34e7dbbSRandy Dunlap	# so just weaken the string compare in check_sections() to ignore
1695e34e7dbbSRandy Dunlap	# "[blah" in a parameter string;
1696e34e7dbbSRandy Dunlap	###$param =~ s/\s*//g;
16971da177e4SLinus Torvalds	push @parameterlist, $param;
1698ed8348e2SMauro Carvalho Chehab	$org_arg =~ s/\s\s+/ /g;
1699ed8348e2SMauro Carvalho Chehab	$parametertypes{$param} = $org_arg;
17001da177e4SLinus Torvalds}
17011da177e4SLinus Torvalds
17021081de2dSMauro Carvalho Chehabsub check_sections($$$$$) {
17031081de2dSMauro Carvalho Chehab	my ($file, $decl_name, $decl_type, $sectcheck, $prmscheck) = @_;
1704a1d94aa5SRandy Dunlap	my @sects = split ' ', $sectcheck;
1705a1d94aa5SRandy Dunlap	my @prms = split ' ', $prmscheck;
1706a1d94aa5SRandy Dunlap	my $err;
1707a1d94aa5SRandy Dunlap	my ($px, $sx);
1708a1d94aa5SRandy Dunlap	my $prm_clean;		# strip trailing "[array size]" and/or beginning "*"
1709a1d94aa5SRandy Dunlap
1710a1d94aa5SRandy Dunlap	foreach $sx (0 .. $#sects) {
1711a1d94aa5SRandy Dunlap		$err = 1;
1712a1d94aa5SRandy Dunlap		foreach $px (0 .. $#prms) {
1713a1d94aa5SRandy Dunlap			$prm_clean = $prms[$px];
1714a1d94aa5SRandy Dunlap			$prm_clean =~ s/\[.*\]//;
1715e86bdb24SAditya Srivastava			$prm_clean =~ s/$attribute//i;
1716e34e7dbbSRandy Dunlap			# ignore array size in a parameter string;
1717e34e7dbbSRandy Dunlap			# however, the original param string may contain
1718e34e7dbbSRandy Dunlap			# spaces, e.g.:  addr[6 + 2]
1719e34e7dbbSRandy Dunlap			# and this appears in @prms as "addr[6" since the
1720e34e7dbbSRandy Dunlap			# parameter list is split at spaces;
1721e34e7dbbSRandy Dunlap			# hence just ignore "[..." for the sections check;
1722e34e7dbbSRandy Dunlap			$prm_clean =~ s/\[.*//;
1723e34e7dbbSRandy Dunlap
1724a1d94aa5SRandy Dunlap			##$prm_clean =~ s/^\**//;
1725a1d94aa5SRandy Dunlap			if ($prm_clean eq $sects[$sx]) {
1726a1d94aa5SRandy Dunlap				$err = 0;
1727a1d94aa5SRandy Dunlap				last;
1728a1d94aa5SRandy Dunlap			}
1729a1d94aa5SRandy Dunlap		}
1730a1d94aa5SRandy Dunlap		if ($err) {
1731a1d94aa5SRandy Dunlap			if ($decl_type eq "function") {
1732d40e1e65SBart Van Assche				print STDERR "${file}:$.: warning: " .
1733a1d94aa5SRandy Dunlap					"Excess function parameter " .
1734a1d94aa5SRandy Dunlap					"'$sects[$sx]' " .
1735a1d94aa5SRandy Dunlap					"description in '$decl_name'\n";
1736a1d94aa5SRandy Dunlap				++$warnings;
1737a1d94aa5SRandy Dunlap			}
1738a1d94aa5SRandy Dunlap		}
1739a1d94aa5SRandy Dunlap	}
1740a1d94aa5SRandy Dunlap}
1741a1d94aa5SRandy Dunlap
17421da177e4SLinus Torvalds##
17434092bac7SYacine Belkadi# Checks the section describing the return value of a function.
17444092bac7SYacine Belkadisub check_return_section {
17454092bac7SYacine Belkadi        my $file = shift;
17464092bac7SYacine Belkadi        my $declaration_name = shift;
17474092bac7SYacine Belkadi        my $return_type = shift;
17484092bac7SYacine Belkadi
17494092bac7SYacine Belkadi        # Ignore an empty return type (It's a macro)
17504092bac7SYacine Belkadi        # Ignore functions with a "void" return type. (But don't ignore "void *")
17514092bac7SYacine Belkadi        if (($return_type eq "") || ($return_type =~ /void\s*\w*\s*$/)) {
17524092bac7SYacine Belkadi                return;
17534092bac7SYacine Belkadi        }
17544092bac7SYacine Belkadi
17554092bac7SYacine Belkadi        if (!defined($sections{$section_return}) ||
17564092bac7SYacine Belkadi            $sections{$section_return} eq "") {
1757d40e1e65SBart Van Assche                print STDERR "${file}:$.: warning: " .
17584092bac7SYacine Belkadi                        "No description found for return value of " .
17594092bac7SYacine Belkadi                        "'$declaration_name'\n";
17604092bac7SYacine Belkadi                ++$warnings;
17614092bac7SYacine Belkadi        }
17624092bac7SYacine Belkadi}
17634092bac7SYacine Belkadi
17644092bac7SYacine Belkadi##
17651da177e4SLinus Torvalds# takes a function prototype and the name of the current file being
17661da177e4SLinus Torvalds# processed and spits out all the details stored in the global
17671da177e4SLinus Torvalds# arrays/hashes.
17681da177e4SLinus Torvaldssub dump_function($$) {
17691da177e4SLinus Torvalds    my $prototype = shift;
17701da177e4SLinus Torvalds    my $file = shift;
1771cbb4d3e6SHoria Geanta    my $noret = 0;
17721da177e4SLinus Torvalds
17735ef09c96SMauro Carvalho Chehab    print_lineno($new_start_line);
17745eb6b4b3SMauro Carvalho Chehab
17751da177e4SLinus Torvalds    $prototype =~ s/^static +//;
17761da177e4SLinus Torvalds    $prototype =~ s/^extern +//;
17774dc3b16bSPavel Pisa    $prototype =~ s/^asmlinkage +//;
17781da177e4SLinus Torvalds    $prototype =~ s/^inline +//;
17791da177e4SLinus Torvalds    $prototype =~ s/^__inline__ +//;
178032e79401SRandy Dunlap    $prototype =~ s/^__inline +//;
178132e79401SRandy Dunlap    $prototype =~ s/^__always_inline +//;
178232e79401SRandy Dunlap    $prototype =~ s/^noinline +//;
178374fc5c65SRandy Dunlap    $prototype =~ s/__init +//;
178420072205SRandy Dunlap    $prototype =~ s/__init_or_module +//;
178580342d48SMatthew Wilcox    $prototype =~ s/__deprecated +//;
1786084aa001SAditya Srivastava    $prototype =~ s/__flatten +//;
1787270a0096SRandy Dunlap    $prototype =~ s/__meminit +//;
178870c95b00SRandy Dunlap    $prototype =~ s/__must_check +//;
17890df7c0e3SRandy Dunlap    $prototype =~ s/__weak +//;
17900891f959SMatthew Wilcox    $prototype =~ s/__sched +//;
179195e760cbSRandy Dunlap    $prototype =~ s/__printf\s*\(\s*\d*\s*,\s*\d*\s*\) +//;
1792*a40a8a11SKees Cook    $prototype =~ s/__alloc_size\s*\(\s*\d+\s*(?:,\s*\d+\s*)?\) +//;
1793cbb4d3e6SHoria Geanta    my $define = $prototype =~ s/^#\s*define\s+//; #ak added
1794084aa001SAditya Srivastava    $prototype =~ s/__attribute_const__ +//;
1795b1aaa546SPaolo Bonzini    $prototype =~ s/__attribute__\s*\(\(
1796b1aaa546SPaolo Bonzini            (?:
1797b1aaa546SPaolo Bonzini                 [\w\s]++          # attribute name
1798b1aaa546SPaolo Bonzini                 (?:\([^)]*+\))?   # attribute arguments
1799b1aaa546SPaolo Bonzini                 \s*+,?            # optional comma at the end
1800b1aaa546SPaolo Bonzini            )+
1801b1aaa546SPaolo Bonzini          \)\)\s+//x;
18021da177e4SLinus Torvalds
18031da177e4SLinus Torvalds    # Yes, this truly is vile.  We are looking for:
18041da177e4SLinus Torvalds    # 1. Return type (may be nothing if we're looking at a macro)
18051da177e4SLinus Torvalds    # 2. Function name
18061da177e4SLinus Torvalds    # 3. Function parameters.
18071da177e4SLinus Torvalds    #
18081da177e4SLinus Torvalds    # All the while we have to watch out for function pointer parameters
18091da177e4SLinus Torvalds    # (which IIRC is what the two sections are for), C types (these
18101da177e4SLinus Torvalds    # regexps don't even start to express all the possibilities), and
18111da177e4SLinus Torvalds    # so on.
18121da177e4SLinus Torvalds    #
18131da177e4SLinus Torvalds    # If you mess with these regexps, it's a good idea to check that
18141da177e4SLinus Torvalds    # the following functions' documentation still comes out right:
18151da177e4SLinus Torvalds    # - parport_register_device (function pointer parameters)
18161da177e4SLinus Torvalds    # - atomic_set (macro)
18179598f91fSMartin Waitz    # - pci_match_device, __copy_to_user (long return type)
1818e86bdb24SAditya Srivastava    my $name = qr{[a-zA-Z0-9_~:]+};
1819e86bdb24SAditya Srivastava    my $prototype_end1 = qr{[^\(]*};
1820e86bdb24SAditya Srivastava    my $prototype_end2 = qr{[^\{]*};
1821e86bdb24SAditya Srivastava    my $prototype_end = qr{\(($prototype_end1|$prototype_end2)\)};
1822e86bdb24SAditya Srivastava    my $type1 = qr{[\w\s]+};
1823e86bdb24SAditya Srivastava    my $type2 = qr{$type1\*+};
18241da177e4SLinus Torvalds
1825e86bdb24SAditya Srivastava    if ($define && $prototype =~ m/^()($name)\s+/) {
1826cbb4d3e6SHoria Geanta        # This is an object-like macro, it has no return type and no parameter
1827cbb4d3e6SHoria Geanta        # list.
1828cbb4d3e6SHoria Geanta        # Function-like macros are not allowed to have spaces between
1829cbb4d3e6SHoria Geanta        # declaration_name and opening parenthesis (notice the \s+).
1830cbb4d3e6SHoria Geanta        $return_type = $1;
1831cbb4d3e6SHoria Geanta        $declaration_name = $2;
1832cbb4d3e6SHoria Geanta        $noret = 1;
1833e86bdb24SAditya Srivastava    } elsif ($prototype =~ m/^()($name)\s*$prototype_end/ ||
1834e86bdb24SAditya Srivastava	$prototype =~ m/^($type1)\s+($name)\s*$prototype_end/ ||
1835e86bdb24SAditya Srivastava	$prototype =~ m/^($type2+)\s*($name)\s*$prototype_end/)  {
18361da177e4SLinus Torvalds	$return_type = $1;
18371da177e4SLinus Torvalds	$declaration_name = $2;
18381da177e4SLinus Torvalds	my $args = $3;
18391da177e4SLinus Torvalds
1840151c468bSMauro Carvalho Chehab	create_parameterlist($args, ',', $file, $declaration_name);
18411da177e4SLinus Torvalds    } else {
1842d40e1e65SBart Van Assche	print STDERR "${file}:$.: warning: cannot understand function prototype: '$prototype'\n";
18431da177e4SLinus Torvalds	return;
18441da177e4SLinus Torvalds    }
18451da177e4SLinus Torvalds
184652042e2dSMauro Carvalho Chehab    if ($identifier ne $declaration_name) {
184752042e2dSMauro Carvalho Chehab	print STDERR "${file}:$.: warning: expecting prototype for $identifier(). Prototype was for $declaration_name() instead\n";
184852042e2dSMauro Carvalho Chehab	return;
184952042e2dSMauro Carvalho Chehab    }
185052042e2dSMauro Carvalho Chehab
1851a1d94aa5SRandy Dunlap    my $prms = join " ", @parameterlist;
18521081de2dSMauro Carvalho Chehab    check_sections($file, $declaration_name, "function", $sectcheck, $prms);
1853a1d94aa5SRandy Dunlap
18544092bac7SYacine Belkadi    # This check emits a lot of warnings at the moment, because many
18554092bac7SYacine Belkadi    # functions don't have a 'Return' doc section. So until the number
18564092bac7SYacine Belkadi    # of warnings goes sufficiently down, the check is only performed in
18574092bac7SYacine Belkadi    # verbose mode.
18584092bac7SYacine Belkadi    # TODO: always perform the check.
1859cbb4d3e6SHoria Geanta    if ($verbose && !$noret) {
18604092bac7SYacine Belkadi	    check_return_section($file, $declaration_name, $return_type);
18614092bac7SYacine Belkadi    }
18624092bac7SYacine Belkadi
186347bcacfdSMauro Carvalho Chehab    # The function parser can be called with a typedef parameter.
186447bcacfdSMauro Carvalho Chehab    # Handle it.
186547bcacfdSMauro Carvalho Chehab    if ($return_type =~ /typedef/) {
186647bcacfdSMauro Carvalho Chehab	output_declaration($declaration_name,
186747bcacfdSMauro Carvalho Chehab			   'function',
186847bcacfdSMauro Carvalho Chehab			   {'function' => $declaration_name,
186947bcacfdSMauro Carvalho Chehab			    'typedef' => 1,
187047bcacfdSMauro Carvalho Chehab			    'module' => $modulename,
187147bcacfdSMauro Carvalho Chehab			    'functiontype' => $return_type,
187247bcacfdSMauro Carvalho Chehab			    'parameterlist' => \@parameterlist,
187347bcacfdSMauro Carvalho Chehab			    'parameterdescs' => \%parameterdescs,
187447bcacfdSMauro Carvalho Chehab			    'parametertypes' => \%parametertypes,
187547bcacfdSMauro Carvalho Chehab			    'sectionlist' => \@sectionlist,
187647bcacfdSMauro Carvalho Chehab			    'sections' => \%sections,
187747bcacfdSMauro Carvalho Chehab			    'purpose' => $declaration_purpose
187847bcacfdSMauro Carvalho Chehab			   });
187947bcacfdSMauro Carvalho Chehab    } else {
18801da177e4SLinus Torvalds	output_declaration($declaration_name,
18811da177e4SLinus Torvalds			   'function',
18821da177e4SLinus Torvalds			   {'function' => $declaration_name,
18831da177e4SLinus Torvalds			    'module' => $modulename,
18841da177e4SLinus Torvalds			    'functiontype' => $return_type,
18851da177e4SLinus Torvalds			    'parameterlist' => \@parameterlist,
18861da177e4SLinus Torvalds			    'parameterdescs' => \%parameterdescs,
18871da177e4SLinus Torvalds			    'parametertypes' => \%parametertypes,
18881da177e4SLinus Torvalds			    'sectionlist' => \@sectionlist,
18891da177e4SLinus Torvalds			    'sections' => \%sections,
18901da177e4SLinus Torvalds			    'purpose' => $declaration_purpose
18911da177e4SLinus Torvalds			   });
18921da177e4SLinus Torvalds    }
189347bcacfdSMauro Carvalho Chehab}
18941da177e4SLinus Torvalds
18951da177e4SLinus Torvaldssub reset_state {
18961da177e4SLinus Torvalds    $function = "";
18971da177e4SLinus Torvalds    %parameterdescs = ();
18981da177e4SLinus Torvalds    %parametertypes = ();
18991da177e4SLinus Torvalds    @parameterlist = ();
19001da177e4SLinus Torvalds    %sections = ();
19011da177e4SLinus Torvalds    @sectionlist = ();
1902a1d94aa5SRandy Dunlap    $sectcheck = "";
1903a1d94aa5SRandy Dunlap    $struct_actual = "";
19041da177e4SLinus Torvalds    $prototype = "";
19051da177e4SLinus Torvalds
190648af606aSJani Nikula    $state = STATE_NORMAL;
190748af606aSJani Nikula    $inline_doc_state = STATE_INLINE_NA;
19081da177e4SLinus Torvalds}
19091da177e4SLinus Torvalds
191056afb0f8SJason Baronsub tracepoint_munge($) {
191156afb0f8SJason Baron	my $file = shift;
191256afb0f8SJason Baron	my $tracepointname = 0;
191356afb0f8SJason Baron	my $tracepointargs = 0;
191456afb0f8SJason Baron
191556afb0f8SJason Baron	if ($prototype =~ m/TRACE_EVENT\((.*?),/) {
191656afb0f8SJason Baron		$tracepointname = $1;
191756afb0f8SJason Baron	}
19183a9089fdSJason Baron	if ($prototype =~ m/DEFINE_SINGLE_EVENT\((.*?),/) {
19193a9089fdSJason Baron		$tracepointname = $1;
19203a9089fdSJason Baron	}
19213a9089fdSJason Baron	if ($prototype =~ m/DEFINE_EVENT\((.*?),(.*?),/) {
19223a9089fdSJason Baron		$tracepointname = $2;
19233a9089fdSJason Baron	}
19243a9089fdSJason Baron	$tracepointname =~ s/^\s+//; #strip leading whitespace
192556afb0f8SJason Baron	if ($prototype =~ m/TP_PROTO\((.*?)\)/) {
192656afb0f8SJason Baron		$tracepointargs = $1;
192756afb0f8SJason Baron	}
192856afb0f8SJason Baron	if (($tracepointname eq 0) || ($tracepointargs eq 0)) {
1929d40e1e65SBart Van Assche		print STDERR "${file}:$.: warning: Unrecognized tracepoint format: \n".
193056afb0f8SJason Baron			     "$prototype\n";
193156afb0f8SJason Baron	} else {
193256afb0f8SJason Baron		$prototype = "static inline void trace_$tracepointname($tracepointargs)";
193352042e2dSMauro Carvalho Chehab		$identifier = "trace_$identifier";
193456afb0f8SJason Baron	}
193556afb0f8SJason Baron}
193656afb0f8SJason Baron
1937b4870bc5SRandy Dunlapsub syscall_munge() {
1938b4870bc5SRandy Dunlap	my $void = 0;
1939b4870bc5SRandy Dunlap
19407c9aa015SMauro Carvalho Chehab	$prototype =~ s@[\r\n]+@ @gos; # strip newlines/CR's
1941b4870bc5SRandy Dunlap##	if ($prototype =~ m/SYSCALL_DEFINE0\s*\(\s*(a-zA-Z0-9_)*\s*\)/) {
1942b4870bc5SRandy Dunlap	if ($prototype =~ m/SYSCALL_DEFINE0/) {
1943b4870bc5SRandy Dunlap		$void = 1;
1944b4870bc5SRandy Dunlap##		$prototype = "long sys_$1(void)";
1945b4870bc5SRandy Dunlap	}
1946b4870bc5SRandy Dunlap
1947b4870bc5SRandy Dunlap	$prototype =~ s/SYSCALL_DEFINE.*\(/long sys_/; # fix return type & func name
1948b4870bc5SRandy Dunlap	if ($prototype =~ m/long (sys_.*?),/) {
1949b4870bc5SRandy Dunlap		$prototype =~ s/,/\(/;
1950b4870bc5SRandy Dunlap	} elsif ($void) {
1951b4870bc5SRandy Dunlap		$prototype =~ s/\)/\(void\)/;
1952b4870bc5SRandy Dunlap	}
1953b4870bc5SRandy Dunlap
1954b4870bc5SRandy Dunlap	# now delete all of the odd-number commas in $prototype
1955b4870bc5SRandy Dunlap	# so that arg types & arg names don't have a comma between them
1956b4870bc5SRandy Dunlap	my $count = 0;
1957b4870bc5SRandy Dunlap	my $len = length($prototype);
1958b4870bc5SRandy Dunlap	if ($void) {
1959b4870bc5SRandy Dunlap		$len = 0;	# skip the for-loop
1960b4870bc5SRandy Dunlap	}
1961b4870bc5SRandy Dunlap	for (my $ix = 0; $ix < $len; $ix++) {
1962b4870bc5SRandy Dunlap		if (substr($prototype, $ix, 1) eq ',') {
1963b4870bc5SRandy Dunlap			$count++;
1964b4870bc5SRandy Dunlap			if ($count % 2 == 1) {
1965b4870bc5SRandy Dunlap				substr($prototype, $ix, 1) = ' ';
1966b4870bc5SRandy Dunlap			}
1967b4870bc5SRandy Dunlap		}
1968b4870bc5SRandy Dunlap	}
1969b4870bc5SRandy Dunlap}
1970b4870bc5SRandy Dunlap
1971b7afa92bSDaniel Vettersub process_proto_function($$) {
19721da177e4SLinus Torvalds    my $x = shift;
19731da177e4SLinus Torvalds    my $file = shift;
19741da177e4SLinus Torvalds
197551f5a0c8SRandy Dunlap    $x =~ s@\/\/.*$@@gos; # strip C99-style comments to end of line
197651f5a0c8SRandy Dunlap
1977890c78c2SRandy Dunlap    if ($x =~ m#\s*/\*\s+MACDOC\s*#io || ($x =~ /^#/ && $x !~ /^#\s*define/)) {
19781da177e4SLinus Torvalds	# do nothing
19791da177e4SLinus Torvalds    }
19801da177e4SLinus Torvalds    elsif ($x =~ /([^\{]*)/) {
19811da177e4SLinus Torvalds	$prototype .= $1;
19821da177e4SLinus Torvalds    }
1983b4870bc5SRandy Dunlap
1984890c78c2SRandy Dunlap    if (($x =~ /\{/) || ($x =~ /\#\s*define/) || ($x =~ /;/)) {
19851da177e4SLinus Torvalds	$prototype =~ s@/\*.*?\*/@@gos;	# strip comments.
19861da177e4SLinus Torvalds	$prototype =~ s@[\r\n]+@ @gos; # strip newlines/cr's.
19871da177e4SLinus Torvalds	$prototype =~ s@^\s+@@gos; # strip leading spaces
19887ae281b0SMauro Carvalho Chehab
19897ae281b0SMauro Carvalho Chehab	 # Handle prototypes for function pointers like:
19907ae281b0SMauro Carvalho Chehab	 # int (*pcs_config)(struct foo)
19917ae281b0SMauro Carvalho Chehab	$prototype =~ s@^(\S+\s+)\(\s*\*(\S+)\)@$1$2@gos;
19927ae281b0SMauro Carvalho Chehab
1993b4870bc5SRandy Dunlap	if ($prototype =~ /SYSCALL_DEFINE/) {
1994b4870bc5SRandy Dunlap		syscall_munge();
1995b4870bc5SRandy Dunlap	}
19963a9089fdSJason Baron	if ($prototype =~ /TRACE_EVENT/ || $prototype =~ /DEFINE_EVENT/ ||
19973a9089fdSJason Baron	    $prototype =~ /DEFINE_SINGLE_EVENT/)
19983a9089fdSJason Baron	{
199956afb0f8SJason Baron		tracepoint_munge($file);
200056afb0f8SJason Baron	}
20011da177e4SLinus Torvalds	dump_function($prototype, $file);
20021da177e4SLinus Torvalds	reset_state();
20031da177e4SLinus Torvalds    }
20041da177e4SLinus Torvalds}
20051da177e4SLinus Torvalds
2006b7afa92bSDaniel Vettersub process_proto_type($$) {
20071da177e4SLinus Torvalds    my $x = shift;
20081da177e4SLinus Torvalds    my $file = shift;
20091da177e4SLinus Torvalds
20101da177e4SLinus Torvalds    $x =~ s@[\r\n]+@ @gos; # strip newlines/cr's.
20111da177e4SLinus Torvalds    $x =~ s@^\s+@@gos; # strip leading spaces
20121da177e4SLinus Torvalds    $x =~ s@\s+$@@gos; # strip trailing spaces
201351f5a0c8SRandy Dunlap    $x =~ s@\/\/.*$@@gos; # strip C99-style comments to end of line
201451f5a0c8SRandy Dunlap
20151da177e4SLinus Torvalds    if ($x =~ /^#/) {
20161da177e4SLinus Torvalds	# To distinguish preprocessor directive from regular declaration later.
20171da177e4SLinus Torvalds	$x .= ";";
20181da177e4SLinus Torvalds    }
20191da177e4SLinus Torvalds
20201da177e4SLinus Torvalds    while (1) {
2021673bb2dfSBen Hutchings	if ( $x =~ /([^\{\};]*)([\{\};])(.*)/ ) {
2022463a0fdcSMarkus Heiser            if( length $prototype ) {
2023463a0fdcSMarkus Heiser                $prototype .= " "
2024463a0fdcSMarkus Heiser            }
20251da177e4SLinus Torvalds	    $prototype .= $1 . $2;
20261da177e4SLinus Torvalds	    ($2 eq '{') && $brcount++;
20271da177e4SLinus Torvalds	    ($2 eq '}') && $brcount--;
20281da177e4SLinus Torvalds	    if (($2 eq ';') && ($brcount == 0)) {
20291da177e4SLinus Torvalds		dump_declaration($prototype, $file);
20301da177e4SLinus Torvalds		reset_state();
20311da177e4SLinus Torvalds		last;
20321da177e4SLinus Torvalds	    }
20331da177e4SLinus Torvalds	    $x = $3;
20341da177e4SLinus Torvalds	} else {
20351da177e4SLinus Torvalds	    $prototype .= $x;
20361da177e4SLinus Torvalds	    last;
20371da177e4SLinus Torvalds	}
20381da177e4SLinus Torvalds    }
20391da177e4SLinus Torvalds}
20401da177e4SLinus Torvalds
20416b5b55f6SRandy Dunlap
20421ad560e4SJani Nikulasub map_filename($) {
20431ad560e4SJani Nikula    my $file;
20441ad560e4SJani Nikula    my ($orig_file) = @_;
20451ad560e4SJani Nikula
20461ad560e4SJani Nikula    if (defined($ENV{'SRCTREE'})) {
20471ad560e4SJani Nikula	$file = "$ENV{'SRCTREE'}" . "/" . $orig_file;
20481ad560e4SJani Nikula    } else {
20491ad560e4SJani Nikula	$file = $orig_file;
20501ad560e4SJani Nikula    }
20511ad560e4SJani Nikula
20521ad560e4SJani Nikula    if (defined($source_map{$file})) {
20531ad560e4SJani Nikula	$file = $source_map{$file};
20541ad560e4SJani Nikula    }
20551ad560e4SJani Nikula
20561ad560e4SJani Nikula    return $file;
20571ad560e4SJani Nikula}
20581ad560e4SJani Nikula
205988c2b57dSJani Nikulasub process_export_file($) {
206088c2b57dSJani Nikula    my ($orig_file) = @_;
206188c2b57dSJani Nikula    my $file = map_filename($orig_file);
206288c2b57dSJani Nikula
206388c2b57dSJani Nikula    if (!open(IN,"<$file")) {
206488c2b57dSJani Nikula	print STDERR "Error: Cannot open file $file\n";
206588c2b57dSJani Nikula	++$errors;
206688c2b57dSJani Nikula	return;
206788c2b57dSJani Nikula    }
206888c2b57dSJani Nikula
206988c2b57dSJani Nikula    while (<IN>) {
207088c2b57dSJani Nikula	if (/$export_symbol/) {
2071eab795ddSMauro Carvalho Chehab	    next if (defined($nosymbol_table{$2}));
207288c2b57dSJani Nikula	    $function_table{$2} = 1;
207388c2b57dSJani Nikula	}
207488c2b57dSJani Nikula    }
207588c2b57dSJani Nikula
207688c2b57dSJani Nikula    close(IN);
207788c2b57dSJani Nikula}
207888c2b57dSJani Nikula
207907048d13SJonathan Corbet#
208007048d13SJonathan Corbet# Parsers for the various processing states.
208107048d13SJonathan Corbet#
208207048d13SJonathan Corbet# STATE_NORMAL: looking for the /** to begin everything.
208307048d13SJonathan Corbet#
208407048d13SJonathan Corbetsub process_normal() {
20851da177e4SLinus Torvalds    if (/$doc_start/o) {
208648af606aSJani Nikula	$state = STATE_NAME;	# next line is always the function name
2087850622dfSRandy Dunlap	$in_doc_sect = 0;
20880b0f5f29SDaniel Vetter	$declaration_start_line = $. + 1;
20891da177e4SLinus Torvalds    }
209007048d13SJonathan Corbet}
209107048d13SJonathan Corbet
20923cac2bc4SJonathan Corbet#
20933cac2bc4SJonathan Corbet# STATE_NAME: Looking for the "name - description" line
20943cac2bc4SJonathan Corbet#
20953cac2bc4SJonathan Corbetsub process_name($$) {
20963cac2bc4SJonathan Corbet    my $file = shift;
20971da177e4SLinus Torvalds    my $descr;
20981da177e4SLinus Torvalds
20991da177e4SLinus Torvalds    if (/$doc_block/o) {
210048af606aSJani Nikula	$state = STATE_DOCBLOCK;
21011da177e4SLinus Torvalds	$contents = "";
21025ef09c96SMauro Carvalho Chehab	$new_start_line = $.;
21030b0f5f29SDaniel Vetter
21041da177e4SLinus Torvalds	if ( $1 eq "" ) {
21051da177e4SLinus Torvalds	    $section = $section_intro;
21061da177e4SLinus Torvalds	} else {
21071da177e4SLinus Torvalds	    $section = $1;
21081da177e4SLinus Torvalds	}
210952042e2dSMauro Carvalho Chehab    } elsif (/$doc_decl/o) {
21101da177e4SLinus Torvalds	$identifier = $1;
21113e58e839SAditya Srivastava	my $is_kernel_comment = 0;
2112e86bdb24SAditya Srivastava	my $decl_start = qr{$doc_com};
2113f9bbc12cSAditya Srivastava	# test for pointer declaration type, foo * bar() - desc
2114f9bbc12cSAditya Srivastava	my $fn_type = qr{\w+\s*\*\s*};
2115f9bbc12cSAditya Srivastava	my $parenthesis = qr{\(\w*\)};
2116f9bbc12cSAditya Srivastava	my $decl_end = qr{[-:].*};
2117e86bdb24SAditya Srivastava	if (/^$decl_start([\w\s]+?)$parenthesis?\s*$decl_end?$/) {
21181da177e4SLinus Torvalds	    $identifier = $1;
21191da177e4SLinus Torvalds	}
212052042e2dSMauro Carvalho Chehab	if ($identifier =~ m/^(struct|union|enum|typedef)\b\s*(\S*)/) {
212152042e2dSMauro Carvalho Chehab	    $decl_type = $1;
212252042e2dSMauro Carvalho Chehab	    $identifier = $2;
21233e58e839SAditya Srivastava	    $is_kernel_comment = 1;
212452042e2dSMauro Carvalho Chehab	}
2125f9bbc12cSAditya Srivastava	# Look for foo() or static void foo() - description; or misspelt
2126f9bbc12cSAditya Srivastava	# identifier
2127e86bdb24SAditya Srivastava	elsif (/^$decl_start$fn_type?(\w+)\s*$parenthesis?\s*$decl_end?$/ ||
2128e86bdb24SAditya Srivastava	    /^$decl_start$fn_type?(\w+.*)$parenthesis?\s*$decl_end$/) {
2129f9bbc12cSAditya Srivastava	    $identifier = $1;
2130f9bbc12cSAditya Srivastava	    $decl_type = 'function';
2131f9bbc12cSAditya Srivastava	    $identifier =~ s/^define\s+//;
2132f9bbc12cSAditya Srivastava	    $is_kernel_comment = 1;
2133f9bbc12cSAditya Srivastava	}
213452042e2dSMauro Carvalho Chehab	$identifier =~ s/\s+$//;
21351da177e4SLinus Torvalds
213617b78717SJonathan Corbet	$state = STATE_BODY;
21370b0f5f29SDaniel Vetter	# if there's no @param blocks need to set up default section
21380b0f5f29SDaniel Vetter	# here
21392f4ad40aSJani Nikula	$contents = "";
21402f4ad40aSJani Nikula	$section = $section_default;
21410b0f5f29SDaniel Vetter	$new_start_line = $. + 1;
214252042e2dSMauro Carvalho Chehab	if (/[-:](.*)/) {
214351f5a0c8SRandy Dunlap	    # strip leading/trailing/multiple spaces
2144a21217daSRandy Dunlap	    $descr= $1;
2145a21217daSRandy Dunlap	    $descr =~ s/^\s*//;
2146a21217daSRandy Dunlap	    $descr =~ s/\s*$//;
214712ae6779SDaniel Santos	    $descr =~ s/\s+/ /g;
21480bba924cSJonathan Corbet	    $declaration_purpose = $descr;
214917b78717SJonathan Corbet	    $state = STATE_BODY_MAYBE;
21501da177e4SLinus Torvalds	} else {
21511da177e4SLinus Torvalds	    $declaration_purpose = "";
21521da177e4SLinus Torvalds	}
215377cc23b8SRandy Dunlap
21543e58e839SAditya Srivastava	if (!$is_kernel_comment) {
21553e58e839SAditya Srivastava	    print STDERR "${file}:$.: warning: This comment starts with '/**', but isn't a kernel-doc comment. Refer Documentation/doc-guide/kernel-doc.rst\n";
21563e58e839SAditya Srivastava	    print STDERR $_;
21573e58e839SAditya Srivastava	    ++$warnings;
21583e58e839SAditya Srivastava	    $state = STATE_NORMAL;
21593e58e839SAditya Srivastava	}
21603e58e839SAditya Srivastava
216177cc23b8SRandy Dunlap	if (($declaration_purpose eq "") && $verbose) {
2162d40e1e65SBart Van Assche	    print STDERR "${file}:$.: warning: missing initial short description on line:\n";
216377cc23b8SRandy Dunlap	    print STDERR $_;
216477cc23b8SRandy Dunlap	    ++$warnings;
216577cc23b8SRandy Dunlap	}
216677cc23b8SRandy Dunlap
21670b54c2e3SMauro Carvalho Chehab	if ($identifier eq "" && $decl_type ne "enum") {
216852042e2dSMauro Carvalho Chehab	    print STDERR "${file}:$.: warning: wrong kernel-doc identifier on line:\n";
216952042e2dSMauro Carvalho Chehab	    print STDERR $_;
217052042e2dSMauro Carvalho Chehab	    ++$warnings;
217152042e2dSMauro Carvalho Chehab	    $state = STATE_NORMAL;
21721da177e4SLinus Torvalds	}
21731da177e4SLinus Torvalds
21741da177e4SLinus Torvalds	if ($verbose) {
217552042e2dSMauro Carvalho Chehab	    print STDERR "${file}:$.: info: Scanning doc for $decl_type $identifier\n";
21761da177e4SLinus Torvalds	}
21771da177e4SLinus Torvalds    } else {
2178d40e1e65SBart Van Assche	print STDERR "${file}:$.: warning: Cannot understand $_ on line $.",
21791da177e4SLinus Torvalds	    " - I thought it was a doc line\n";
21801da177e4SLinus Torvalds	++$warnings;
218148af606aSJani Nikula	$state = STATE_NORMAL;
21821da177e4SLinus Torvalds    }
21833cac2bc4SJonathan Corbet}
21843cac2bc4SJonathan Corbet
21853cac2bc4SJonathan Corbet
2186d742f24dSJonathan Corbet#
2187d742f24dSJonathan Corbet# STATE_BODY and STATE_BODY_MAYBE: the bulk of a kerneldoc comment.
2188d742f24dSJonathan Corbet#
2189d742f24dSJonathan Corbetsub process_body($$) {
2190d742f24dSJonathan Corbet    my $file = shift;
21913cac2bc4SJonathan Corbet
219243756e34SJonathan Neuschäfer    # Until all named variable macro parameters are
219343756e34SJonathan Neuschäfer    # documented using the bare name (`x`) rather than with
219443756e34SJonathan Neuschäfer    # dots (`x...`), strip the dots:
219543756e34SJonathan Neuschäfer    if ($section =~ /\w\.\.\.$/) {
219643756e34SJonathan Neuschäfer	$section =~ s/\.\.\.$//;
219743756e34SJonathan Neuschäfer
219843756e34SJonathan Neuschäfer	if ($verbose) {
219943756e34SJonathan Neuschäfer	    print STDERR "${file}:$.: warning: Variable macro arguments should be documented without dots\n";
220043756e34SJonathan Neuschäfer	    ++$warnings;
220143756e34SJonathan Neuschäfer	}
220243756e34SJonathan Neuschäfer    }
220343756e34SJonathan Neuschäfer
22040d55d48bSMauro Carvalho Chehab    if ($state == STATE_BODY_WITH_BLANK_LINE && /^\s*\*\s?\S/) {
22050d55d48bSMauro Carvalho Chehab	dump_section($file, $section, $contents);
22060d55d48bSMauro Carvalho Chehab	$section = $section_default;
22075ef09c96SMauro Carvalho Chehab	$new_start_line = $.;
22080d55d48bSMauro Carvalho Chehab	$contents = "";
22090d55d48bSMauro Carvalho Chehab    }
22100d55d48bSMauro Carvalho Chehab
2211f624adefSJani Nikula    if (/$doc_sect/i) { # case insensitive for supported section names
22121da177e4SLinus Torvalds	$newsection = $1;
22131da177e4SLinus Torvalds	$newcontents = $2;
22141da177e4SLinus Torvalds
2215f624adefSJani Nikula	# map the supported section names to the canonical names
2216f624adefSJani Nikula	if ($newsection =~ m/^description$/i) {
2217f624adefSJani Nikula	    $newsection = $section_default;
2218f624adefSJani Nikula	} elsif ($newsection =~ m/^context$/i) {
2219f624adefSJani Nikula	    $newsection = $section_context;
2220f624adefSJani Nikula	} elsif ($newsection =~ m/^returns?$/i) {
2221f624adefSJani Nikula	    $newsection = $section_return;
2222f624adefSJani Nikula	} elsif ($newsection =~ m/^\@return$/) {
2223f624adefSJani Nikula	    # special: @return is a section, not a param description
2224f624adefSJani Nikula	    $newsection = $section_return;
2225f624adefSJani Nikula	}
2226f624adefSJani Nikula
2227792aa2f2SRandy Dunlap	if (($contents ne "") && ($contents ne "\n")) {
2228850622dfSRandy Dunlap	    if (!$in_doc_sect && $verbose) {
2229d40e1e65SBart Van Assche		print STDERR "${file}:$.: warning: contents before sections\n";
2230850622dfSRandy Dunlap		++$warnings;
2231850622dfSRandy Dunlap	    }
22320bba924cSJonathan Corbet	    dump_section($file, $section, $contents);
22331da177e4SLinus Torvalds	    $section = $section_default;
22341da177e4SLinus Torvalds	}
22351da177e4SLinus Torvalds
2236850622dfSRandy Dunlap	$in_doc_sect = 1;
223717b78717SJonathan Corbet	$state = STATE_BODY;
22381da177e4SLinus Torvalds	$contents = $newcontents;
22390b0f5f29SDaniel Vetter	$new_start_line = $.;
22407c9aa015SMauro Carvalho Chehab	while (substr($contents, 0, 1) eq " ") {
224105189497SRandy Dunlap	    $contents = substr($contents, 1);
224205189497SRandy Dunlap	}
22430a726301SJani Nikula	if ($contents ne "") {
22441da177e4SLinus Torvalds	    $contents .= "\n";
22451da177e4SLinus Torvalds	}
22461da177e4SLinus Torvalds	$section = $newsection;
2247b7886de4SJani Nikula	$leading_space = undef;
22481da177e4SLinus Torvalds    } elsif (/$doc_end/) {
22494c98ecafSRandy Dunlap	if (($contents ne "") && ($contents ne "\n")) {
22500bba924cSJonathan Corbet	    dump_section($file, $section, $contents);
22511da177e4SLinus Torvalds	    $section = $section_default;
22521da177e4SLinus Torvalds	    $contents = "";
22531da177e4SLinus Torvalds	}
225446b958ebSRandy Dunlap	# look for doc_com + <text> + doc_end:
225546b958ebSRandy Dunlap	if ($_ =~ m'\s*\*\s*[a-zA-Z_0-9:\.]+\*/') {
2256d40e1e65SBart Van Assche	    print STDERR "${file}:$.: warning: suspicious ending line: $_";
225746b958ebSRandy Dunlap	    ++$warnings;
225846b958ebSRandy Dunlap	}
22591da177e4SLinus Torvalds
22601da177e4SLinus Torvalds	$prototype = "";
226148af606aSJani Nikula	$state = STATE_PROTO;
22621da177e4SLinus Torvalds	$brcount = 0;
22635ef09c96SMauro Carvalho Chehab        $new_start_line = $. + 1;
22641da177e4SLinus Torvalds    } elsif (/$doc_content/) {
22656423133bSJohannes Weiner	if ($1 eq "") {
22660d55d48bSMauro Carvalho Chehab	    if ($section eq $section_context) {
22670bba924cSJonathan Corbet		dump_section($file, $section, $contents);
22681da177e4SLinus Torvalds		$section = $section_default;
22691da177e4SLinus Torvalds		$contents = "";
22700b0f5f29SDaniel Vetter		$new_start_line = $.;
22710d55d48bSMauro Carvalho Chehab		$state = STATE_BODY;
22721da177e4SLinus Torvalds	    } else {
22730d55d48bSMauro Carvalho Chehab		if ($section ne $section_default) {
22740d55d48bSMauro Carvalho Chehab		    $state = STATE_BODY_WITH_BLANK_LINE;
22750d55d48bSMauro Carvalho Chehab		} else {
22760d55d48bSMauro Carvalho Chehab		    $state = STATE_BODY;
22770d55d48bSMauro Carvalho Chehab		}
22786423133bSJohannes Weiner		$contents .= "\n";
22796423133bSJohannes Weiner	    }
228017b78717SJonathan Corbet	} elsif ($state == STATE_BODY_MAYBE) {
22816423133bSJohannes Weiner	    # Continued declaration purpose
22826423133bSJohannes Weiner	    chomp($declaration_purpose);
22830bba924cSJonathan Corbet	    $declaration_purpose .= " " . $1;
228412ae6779SDaniel Santos	    $declaration_purpose =~ s/\s+/ /g;
22856423133bSJohannes Weiner	} else {
2286b7886de4SJani Nikula	    my $cont = $1;
2287b7886de4SJani Nikula	    if ($section =~ m/^@/ || $section eq $section_context) {
2288b7886de4SJani Nikula		if (!defined $leading_space) {
2289b7886de4SJani Nikula		    if ($cont =~ m/^(\s+)/) {
2290b7886de4SJani Nikula			$leading_space = $1;
2291b7886de4SJani Nikula		    } else {
2292b7886de4SJani Nikula			$leading_space = "";
2293b7886de4SJani Nikula		    }
2294b7886de4SJani Nikula		}
2295b7886de4SJani Nikula		$cont =~ s/^$leading_space//;
2296b7886de4SJani Nikula	    }
2297b7886de4SJani Nikula	    $contents .= $cont . "\n";
22981da177e4SLinus Torvalds	}
22991da177e4SLinus Torvalds    } else {
23001da177e4SLinus Torvalds	# i dont know - bad line?  ignore.
2301d40e1e65SBart Van Assche	print STDERR "${file}:$.: warning: bad line: $_";
23021da177e4SLinus Torvalds	++$warnings;
23031da177e4SLinus Torvalds    }
2304d742f24dSJonathan Corbet}
2305d742f24dSJonathan Corbet
2306d742f24dSJonathan Corbet
2307cc794812SJonathan Corbet#
2308cc794812SJonathan Corbet# STATE_PROTO: reading a function/whatever prototype.
2309cc794812SJonathan Corbet#
2310cc794812SJonathan Corbetsub process_proto($$) {
2311cc794812SJonathan Corbet    my $file = shift;
2312cc794812SJonathan Corbet
2313cc794812SJonathan Corbet    if (/$doc_inline_oneline/) {
2314cc794812SJonathan Corbet	$section = $1;
2315cc794812SJonathan Corbet	$contents = $2;
2316cc794812SJonathan Corbet	if ($contents ne "") {
2317cc794812SJonathan Corbet	    $contents .= "\n";
2318cc794812SJonathan Corbet	    dump_section($file, $section, $contents);
2319cc794812SJonathan Corbet	    $section = $section_default;
2320cc794812SJonathan Corbet	    $contents = "";
2321cc794812SJonathan Corbet	}
2322cc794812SJonathan Corbet    } elsif (/$doc_inline_start/) {
2323cc794812SJonathan Corbet	$state = STATE_INLINE;
2324cc794812SJonathan Corbet	$inline_doc_state = STATE_INLINE_NAME;
2325cc794812SJonathan Corbet    } elsif ($decl_type eq 'function') {
2326cc794812SJonathan Corbet	process_proto_function($_, $file);
2327cc794812SJonathan Corbet    } else {
2328cc794812SJonathan Corbet	process_proto_type($_, $file);
2329cc794812SJonathan Corbet    }
2330cc794812SJonathan Corbet}
2331cc794812SJonathan Corbet
2332c17add56SJonathan Corbet#
2333c17add56SJonathan Corbet# STATE_DOCBLOCK: within a DOC: block.
2334c17add56SJonathan Corbet#
2335c17add56SJonathan Corbetsub process_docblock($$) {
2336c17add56SJonathan Corbet    my $file = shift;
2337cc794812SJonathan Corbet
2338c17add56SJonathan Corbet    if (/$doc_end/) {
2339c17add56SJonathan Corbet	dump_doc_section($file, $section, $contents);
2340c17add56SJonathan Corbet	$section = $section_default;
2341c17add56SJonathan Corbet	$contents = "";
2342c17add56SJonathan Corbet	$function = "";
2343c17add56SJonathan Corbet	%parameterdescs = ();
2344c17add56SJonathan Corbet	%parametertypes = ();
2345c17add56SJonathan Corbet	@parameterlist = ();
2346c17add56SJonathan Corbet	%sections = ();
2347c17add56SJonathan Corbet	@sectionlist = ();
2348c17add56SJonathan Corbet	$prototype = "";
2349c17add56SJonathan Corbet	$state = STATE_NORMAL;
2350c17add56SJonathan Corbet    } elsif (/$doc_content/) {
2351c17add56SJonathan Corbet	if ( $1 eq "" )	{
2352c17add56SJonathan Corbet	    $contents .= $blankline;
2353c17add56SJonathan Corbet	} else {
2354c17add56SJonathan Corbet	    $contents .= $1 . "\n";
2355c17add56SJonathan Corbet	}
2356c17add56SJonathan Corbet    }
2357d742f24dSJonathan Corbet}
2358d742f24dSJonathan Corbet
2359c17add56SJonathan Corbet#
2360c17add56SJonathan Corbet# STATE_INLINE: docbook comments within a prototype.
2361c17add56SJonathan Corbet#
2362c17add56SJonathan Corbetsub process_inline($$) {
2363c17add56SJonathan Corbet    my $file = shift;
2364d742f24dSJonathan Corbet
2365a4c6ebedSDanilo Cesar Lemes de Paula    # First line (state 1) needs to be a @parameter
236648af606aSJani Nikula    if ($inline_doc_state == STATE_INLINE_NAME && /$doc_inline_sect/o) {
2367a4c6ebedSDanilo Cesar Lemes de Paula	$section = $1;
2368a4c6ebedSDanilo Cesar Lemes de Paula	$contents = $2;
23690b0f5f29SDaniel Vetter	$new_start_line = $.;
2370a4c6ebedSDanilo Cesar Lemes de Paula	if ($contents ne "") {
23717c9aa015SMauro Carvalho Chehab	    while (substr($contents, 0, 1) eq " ") {
2372a4c6ebedSDanilo Cesar Lemes de Paula		$contents = substr($contents, 1);
2373a4c6ebedSDanilo Cesar Lemes de Paula	    }
2374a4c6ebedSDanilo Cesar Lemes de Paula	    $contents .= "\n";
2375a4c6ebedSDanilo Cesar Lemes de Paula	}
237648af606aSJani Nikula	$inline_doc_state = STATE_INLINE_TEXT;
2377a4c6ebedSDanilo Cesar Lemes de Paula	# Documentation block end */
237848af606aSJani Nikula    } elsif (/$doc_inline_end/) {
2379a4c6ebedSDanilo Cesar Lemes de Paula	if (($contents ne "") && ($contents ne "\n")) {
23800bba924cSJonathan Corbet	    dump_section($file, $section, $contents);
2381a4c6ebedSDanilo Cesar Lemes de Paula	    $section = $section_default;
2382a4c6ebedSDanilo Cesar Lemes de Paula	    $contents = "";
2383a4c6ebedSDanilo Cesar Lemes de Paula	}
238448af606aSJani Nikula	$state = STATE_PROTO;
238548af606aSJani Nikula	$inline_doc_state = STATE_INLINE_NA;
2386a4c6ebedSDanilo Cesar Lemes de Paula	# Regular text
2387a4c6ebedSDanilo Cesar Lemes de Paula    } elsif (/$doc_content/) {
238848af606aSJani Nikula	if ($inline_doc_state == STATE_INLINE_TEXT) {
2389a4c6ebedSDanilo Cesar Lemes de Paula	    $contents .= $1 . "\n";
23906450c895SJani Nikula	    # nuke leading blank lines
23916450c895SJani Nikula	    if ($contents =~ /^\s*$/) {
23926450c895SJani Nikula		$contents = "";
23936450c895SJani Nikula	    }
239448af606aSJani Nikula	} elsif ($inline_doc_state == STATE_INLINE_NAME) {
239548af606aSJani Nikula	    $inline_doc_state = STATE_INLINE_ERROR;
2396e7ca311eSDaniel Vetter	    print STDERR "${file}:$.: warning: ";
2397a4c6ebedSDanilo Cesar Lemes de Paula	    print STDERR "Incorrect use of kernel-doc format: $_";
2398a4c6ebedSDanilo Cesar Lemes de Paula	    ++$warnings;
2399a4c6ebedSDanilo Cesar Lemes de Paula	}
2400a4c6ebedSDanilo Cesar Lemes de Paula    }
24010c9aa209SJani Nikula}
2402c17add56SJonathan Corbet
2403c17add56SJonathan Corbet
2404c17add56SJonathan Corbetsub process_file($) {
2405c17add56SJonathan Corbet    my $file;
2406c17add56SJonathan Corbet    my $initial_section_counter = $section_counter;
2407c17add56SJonathan Corbet    my ($orig_file) = @_;
2408c17add56SJonathan Corbet
2409c17add56SJonathan Corbet    $file = map_filename($orig_file);
2410c17add56SJonathan Corbet
2411dbe8ba00SMauro Carvalho Chehab    if (!open(IN_FILE,"<$file")) {
2412c17add56SJonathan Corbet	print STDERR "Error: Cannot open file $file\n";
2413c17add56SJonathan Corbet	++$errors;
2414c17add56SJonathan Corbet	return;
24151da177e4SLinus Torvalds    }
2416c17add56SJonathan Corbet
2417c17add56SJonathan Corbet    $. = 1;
2418c17add56SJonathan Corbet
2419c17add56SJonathan Corbet    $section_counter = 0;
2420dbe8ba00SMauro Carvalho Chehab    while (<IN_FILE>) {
2421c17add56SJonathan Corbet	while (s/\\\s*$//) {
2422dbe8ba00SMauro Carvalho Chehab	    $_ .= <IN_FILE>;
2423c17add56SJonathan Corbet	}
2424c17add56SJonathan Corbet	# Replace tabs by spaces
2425c17add56SJonathan Corbet        while ($_ =~ s/\t+/' ' x (length($&) * 8 - length($`) % 8)/e) {};
2426c17add56SJonathan Corbet	# Hand this line to the appropriate state handler
2427c17add56SJonathan Corbet	if ($state == STATE_NORMAL) {
2428c17add56SJonathan Corbet	    process_normal();
2429c17add56SJonathan Corbet	} elsif ($state == STATE_NAME) {
2430c17add56SJonathan Corbet	    process_name($file, $_);
24310d55d48bSMauro Carvalho Chehab	} elsif ($state == STATE_BODY || $state == STATE_BODY_MAYBE ||
24320d55d48bSMauro Carvalho Chehab		 $state == STATE_BODY_WITH_BLANK_LINE) {
2433c17add56SJonathan Corbet	    process_body($file, $_);
2434c17add56SJonathan Corbet	} elsif ($state == STATE_INLINE) { # scanning for inline parameters
2435c17add56SJonathan Corbet	    process_inline($file, $_);
2436cc794812SJonathan Corbet	} elsif ($state == STATE_PROTO) {
2437cc794812SJonathan Corbet	    process_proto($file, $_);
243848af606aSJani Nikula	} elsif ($state == STATE_DOCBLOCK) {
2439c17add56SJonathan Corbet	    process_docblock($file, $_);
24401da177e4SLinus Torvalds	}
24411da177e4SLinus Torvalds    }
2442c17add56SJonathan Corbet
2443c17add56SJonathan Corbet    # Make sure we got something interesting.
2444b0d60bfbSJonathan Corbet    if ($initial_section_counter == $section_counter && $
2445b0d60bfbSJonathan Corbet	output_mode ne "none") {
2446b0d60bfbSJonathan Corbet	if ($output_selection == OUTPUT_INCLUDE) {
2447b0d60bfbSJonathan Corbet	    print STDERR "${file}:1: warning: '$_' not found\n"
2448b0d60bfbSJonathan Corbet		for keys %function_table;
24493a025e1dSMatthew Wilcox	}
2450b0d60bfbSJonathan Corbet	else {
2451b0d60bfbSJonathan Corbet	    print STDERR "${file}:1: warning: no structured comments found\n";
2452e946c43aSJohannes Berg	}
24531da177e4SLinus Torvalds    }
2454dbe8ba00SMauro Carvalho Chehab    close IN_FILE;
24551da177e4SLinus Torvalds}
24568484baaaSRandy Dunlap
24578484baaaSRandy Dunlap
245893351d41SMauro Carvalho Chehabif ($output_mode eq "rst") {
245993351d41SMauro Carvalho Chehab	get_sphinx_version() if (!$sphinx_major);
246093351d41SMauro Carvalho Chehab}
246193351d41SMauro Carvalho Chehab
24628484baaaSRandy Dunlap$kernelversion = get_kernel_version();
24638484baaaSRandy Dunlap
24648484baaaSRandy Dunlap# generate a sequence of code that will splice in highlighting information
24658484baaaSRandy Dunlap# using the s// operator.
24661ef06233SMauro Carvalho Chehabfor (my $k = 0; $k < @highlights; $k++) {
24674d732701SDanilo Cesar Lemes de Paula    my $pattern = $highlights[$k][0];
24684d732701SDanilo Cesar Lemes de Paula    my $result = $highlights[$k][1];
24694d732701SDanilo Cesar Lemes de Paula#   print STDERR "scanning pattern:$pattern, highlight:($result)\n";
24704d732701SDanilo Cesar Lemes de Paula    $dohighlight .=  "\$contents =~ s:$pattern:$result:gs;\n";
24718484baaaSRandy Dunlap}
24728484baaaSRandy Dunlap
24738484baaaSRandy Dunlap# Read the file that maps relative names to absolute names for
24748484baaaSRandy Dunlap# separate source and object directories and for shadow trees.
24758484baaaSRandy Dunlapif (open(SOURCE_MAP, "<.tmp_filelist.txt")) {
24768484baaaSRandy Dunlap	my ($relname, $absname);
24778484baaaSRandy Dunlap	while(<SOURCE_MAP>) {
24788484baaaSRandy Dunlap		chop();
24798484baaaSRandy Dunlap		($relname, $absname) = (split())[0..1];
24808484baaaSRandy Dunlap		$relname =~ s:^/+::;
24818484baaaSRandy Dunlap		$source_map{$relname} = $absname;
24828484baaaSRandy Dunlap	}
24838484baaaSRandy Dunlap	close(SOURCE_MAP);
24848484baaaSRandy Dunlap}
24858484baaaSRandy Dunlap
248688c2b57dSJani Nikulaif ($output_selection == OUTPUT_EXPORTED ||
248788c2b57dSJani Nikula    $output_selection == OUTPUT_INTERNAL) {
2488c9b2cfb3SJani Nikula
2489c9b2cfb3SJani Nikula    push(@export_file_list, @ARGV);
2490c9b2cfb3SJani Nikula
249188c2b57dSJani Nikula    foreach (@export_file_list) {
249288c2b57dSJani Nikula	chomp;
249388c2b57dSJani Nikula	process_export_file($_);
249488c2b57dSJani Nikula    }
249588c2b57dSJani Nikula}
249688c2b57dSJani Nikula
24978484baaaSRandy Dunlapforeach (@ARGV) {
24988484baaaSRandy Dunlap    chomp;
24998484baaaSRandy Dunlap    process_file($_);
25008484baaaSRandy Dunlap}
25018484baaaSRandy Dunlapif ($verbose && $errors) {
25028484baaaSRandy Dunlap  print STDERR "$errors errors\n";
25038484baaaSRandy Dunlap}
25048484baaaSRandy Dunlapif ($verbose && $warnings) {
25058484baaaSRandy Dunlap  print STDERR "$warnings warnings\n";
25068484baaaSRandy Dunlap}
25078484baaaSRandy Dunlap
25082c12c810SPierre-Louis Bossartif ($Werror && $warnings) {
25092c12c810SPierre-Louis Bossart    print STDERR "$warnings warnings as Errors\n";
25102c12c810SPierre-Louis Bossart    exit($warnings);
25112c12c810SPierre-Louis Bossart} else {
25122c12c810SPierre-Louis Bossart    exit($output_mode eq "none" ? 0 : $errors)
25132c12c810SPierre-Louis Bossart}
2514