xref: /openbmc/linux/scripts/kernel-doc (revision c15de5a1)
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
1943caf1a6STomasz Warniełłouse Pod::Usage qw/pod2usage/;
2043caf1a6STomasz Warniełło
21a5cdaea5STomasz Warniełło=head1 NAME
22a5cdaea5STomasz Warniełło
23a5cdaea5STomasz Warniełłokernel-doc - Print formatted kernel documentation to stdout
24a5cdaea5STomasz Warniełło
25a5cdaea5STomasz Warniełło=head1 SYNOPSIS
26a5cdaea5STomasz Warniełło
27a5cdaea5STomasz Warniełło kernel-doc [-h] [-v] [-Werror]
28a5cdaea5STomasz Warniełło   [ -man |
29a5cdaea5STomasz Warniełło     -rst [-sphinx-version VERSION] [-enable-lineno] |
30a5cdaea5STomasz Warniełło     -none
31a5cdaea5STomasz Warniełło   ]
32a5cdaea5STomasz Warniełło   [
33a5cdaea5STomasz Warniełło     -export |
34a5cdaea5STomasz Warniełło     -internal |
35a5cdaea5STomasz Warniełło     [-function NAME] ... |
36a5cdaea5STomasz Warniełło     [-nosymbol NAME] ...
37a5cdaea5STomasz Warniełło   ]
38a5cdaea5STomasz Warniełło   [-no-doc-sections]
39a5cdaea5STomasz Warniełło   [-export-file FILE] ...
40a5cdaea5STomasz Warniełło   FILE ...
41a5cdaea5STomasz Warniełło
42a5cdaea5STomasz WarniełłoRun `kernel-doc -h` for details.
43a5cdaea5STomasz Warniełło
44f1583922STomasz Warniełło=head1 DESCRIPTION
45f1583922STomasz Warniełło
46f1583922STomasz WarniełłoRead C language source or header FILEs, extract embedded documentation comments,
47f1583922STomasz Warniełłoand print formatted documentation to standard output.
48f1583922STomasz Warniełło
49f1583922STomasz WarniełłoThe documentation comments are identified by the "/**" opening comment mark.
50f1583922STomasz Warniełło
51f1583922STomasz WarniełłoSee Documentation/doc-guide/kernel-doc.rst for the documentation comment syntax.
52f1583922STomasz Warniełło
53a5cdaea5STomasz Warniełło=cut
54a5cdaea5STomasz Warniełło
552875f787STomasz Warniełło# more perldoc at the end of the file
562875f787STomasz Warniełło
571da177e4SLinus Torvalds# 18/01/2001 - 	Cleanups
581da177e4SLinus Torvalds# 		Functions prototyped as foo(void) same as foo()
591da177e4SLinus Torvalds# 		Stop eval'ing where we don't need to.
601da177e4SLinus Torvalds# -- huggie@earth.li
611da177e4SLinus Torvalds
621da177e4SLinus Torvalds# 27/06/2001 -  Allowed whitespace after initial "/**" and
631da177e4SLinus Torvalds#               allowed comments before function declarations.
641da177e4SLinus Torvalds# -- Christian Kreibich <ck@whoop.org>
651da177e4SLinus Torvalds
661da177e4SLinus Torvalds# Still to do:
671da177e4SLinus Torvalds# 	- add perldoc documentation
681da177e4SLinus Torvalds# 	- Look more closely at some of the scarier bits :)
691da177e4SLinus Torvalds
701da177e4SLinus Torvalds# 26/05/2001 - 	Support for separate source and object trees.
711da177e4SLinus Torvalds#		Return error code.
721da177e4SLinus Torvalds# 		Keith Owens <kaos@ocs.com.au>
731da177e4SLinus Torvalds
741da177e4SLinus Torvalds# 23/09/2001 - Added support for typedefs, structs, enums and unions
751da177e4SLinus Torvalds#              Support for Context section; can be terminated using empty line
761da177e4SLinus Torvalds#              Small fixes (like spaces vs. \s in regex)
771da177e4SLinus Torvalds# -- Tim Jansen <tim@tjansen.de>
781da177e4SLinus Torvalds
791b40c194SDan Luedtke# 25/07/2012 - Added support for HTML5
801b40c194SDan Luedtke# -- Dan Luedtke <mail@danrl.de>
811da177e4SLinus Torvalds
82fadc0b31SJani Nikulasub usage {
83fadc0b31SJani Nikula    my $message = <<"EOF";
84fadc0b31SJani NikulaUsage: $0 [OPTION ...] FILE ...
851da177e4SLinus Torvalds
86fadc0b31SJani NikulaOther parameters:
87fadc0b31SJani Nikula  -v			Verbose output, more warnings and other information.
88fadc0b31SJani Nikula  -h			Print this help.
892c12c810SPierre-Louis Bossart  -Werror		Treat warnings as errors.
90fadc0b31SJani Nikula
91fadc0b31SJani NikulaEOF
92fadc0b31SJani Nikula    print $message;
93fadc0b31SJani Nikula    exit 1;
94fadc0b31SJani Nikula}
951da177e4SLinus Torvalds
961da177e4SLinus Torvalds#
971da177e4SLinus Torvalds# format of comments.
981da177e4SLinus Torvalds# In the following table, (...)? signifies optional structure.
991da177e4SLinus Torvalds#                         (...)* signifies 0 or more structure elements
1001da177e4SLinus Torvalds# /**
1011da177e4SLinus Torvalds#  * function_name(:)? (- short description)?
1021da177e4SLinus Torvalds# (* @parameterx: (description of parameter x)?)*
1031da177e4SLinus Torvalds# (* a blank line)?
1041da177e4SLinus Torvalds#  * (Description:)? (Description of function)?
1051da177e4SLinus Torvalds#  * (section header: (section description)? )*
1061da177e4SLinus Torvalds#  (*)?*/
1071da177e4SLinus Torvalds#
1081da177e4SLinus Torvalds# So .. the trivial example would be:
1091da177e4SLinus Torvalds#
1101da177e4SLinus Torvalds# /**
1111da177e4SLinus Torvalds#  * my_function
112b9d97328SRandy Dunlap#  */
1131da177e4SLinus Torvalds#
114891dcd2fSRandy Dunlap# If the Description: header tag is omitted, then there must be a blank line
1151da177e4SLinus Torvalds# after the last parameter specification.
1161da177e4SLinus Torvalds# e.g.
1171da177e4SLinus Torvalds# /**
1181da177e4SLinus Torvalds#  * my_function - does my stuff
1191da177e4SLinus Torvalds#  * @my_arg: its mine damnit
1201da177e4SLinus Torvalds#  *
1211da177e4SLinus Torvalds#  * Does my stuff explained.
1221da177e4SLinus Torvalds#  */
1231da177e4SLinus Torvalds#
1241da177e4SLinus Torvalds#  or, could also use:
1251da177e4SLinus Torvalds# /**
1261da177e4SLinus Torvalds#  * my_function - does my stuff
1271da177e4SLinus Torvalds#  * @my_arg: its mine damnit
1281da177e4SLinus Torvalds#  * Description: Does my stuff explained.
1291da177e4SLinus Torvalds#  */
1301da177e4SLinus Torvalds# etc.
1311da177e4SLinus Torvalds#
132b9d97328SRandy Dunlap# Besides functions you can also write documentation for structs, unions,
1331da177e4SLinus Torvalds# enums and typedefs. Instead of the function name you must write the name
1341da177e4SLinus Torvalds# of the declaration;  the struct/union/enum/typedef must always precede
1351da177e4SLinus Torvalds# the name. Nesting of declarations is not supported.
1361da177e4SLinus Torvalds# Use the argument mechanism to document members or constants.
1371da177e4SLinus Torvalds# e.g.
1381da177e4SLinus Torvalds# /**
1391da177e4SLinus Torvalds#  * struct my_struct - short description
1401da177e4SLinus Torvalds#  * @a: first member
1411da177e4SLinus Torvalds#  * @b: second member
1421da177e4SLinus Torvalds#  *
1431da177e4SLinus Torvalds#  * Longer description
1441da177e4SLinus Torvalds#  */
1451da177e4SLinus Torvalds# struct my_struct {
1461da177e4SLinus Torvalds#     int a;
1471da177e4SLinus Torvalds#     int b;
148aeec46b9SMartin Waitz# /* private: */
149aeec46b9SMartin Waitz#     int c;
1501da177e4SLinus Torvalds# };
1511da177e4SLinus Torvalds#
1521da177e4SLinus Torvalds# All descriptions can be multiline, except the short function description.
1531da177e4SLinus Torvalds#
154a4c6ebedSDanilo Cesar Lemes de Paula# For really longs structs, you can also describe arguments inside the
155a4c6ebedSDanilo Cesar Lemes de Paula# body of the struct.
156a4c6ebedSDanilo Cesar Lemes de Paula# eg.
157a4c6ebedSDanilo Cesar Lemes de Paula# /**
158a4c6ebedSDanilo Cesar Lemes de Paula#  * struct my_struct - short description
159a4c6ebedSDanilo Cesar Lemes de Paula#  * @a: first member
160a4c6ebedSDanilo Cesar Lemes de Paula#  * @b: second member
161a4c6ebedSDanilo Cesar Lemes de Paula#  *
162a4c6ebedSDanilo Cesar Lemes de Paula#  * Longer description
163a4c6ebedSDanilo Cesar Lemes de Paula#  */
164a4c6ebedSDanilo Cesar Lemes de Paula# struct my_struct {
165a4c6ebedSDanilo Cesar Lemes de Paula#     int a;
166a4c6ebedSDanilo Cesar Lemes de Paula#     int b;
167a4c6ebedSDanilo Cesar Lemes de Paula#     /**
168a4c6ebedSDanilo Cesar Lemes de Paula#      * @c: This is longer description of C
169a4c6ebedSDanilo Cesar Lemes de Paula#      *
170a4c6ebedSDanilo Cesar Lemes de Paula#      * You can use paragraphs to describe arguments
171a4c6ebedSDanilo Cesar Lemes de Paula#      * using this method.
172a4c6ebedSDanilo Cesar Lemes de Paula#      */
173a4c6ebedSDanilo Cesar Lemes de Paula#     int c;
174a4c6ebedSDanilo Cesar Lemes de Paula# };
175a4c6ebedSDanilo Cesar Lemes de Paula#
176a4c6ebedSDanilo Cesar Lemes de Paula# This should be use only for struct/enum members.
177a4c6ebedSDanilo Cesar Lemes de Paula#
1781da177e4SLinus Torvalds# You can also add additional sections. When documenting kernel functions you
1791da177e4SLinus Torvalds# should document the "Context:" of the function, e.g. whether the functions
1801da177e4SLinus Torvalds# can be called form interrupts. Unlike other sections you can end it with an
1811da177e4SLinus Torvalds# empty line.
1824092bac7SYacine Belkadi# A non-void function should have a "Return:" section describing the return
1834092bac7SYacine Belkadi# value(s).
1841da177e4SLinus Torvalds# Example-sections should contain the string EXAMPLE so that they are marked
1851da177e4SLinus Torvalds# appropriately in DocBook.
1861da177e4SLinus Torvalds#
1871da177e4SLinus Torvalds# Example:
1881da177e4SLinus Torvalds# /**
1891da177e4SLinus Torvalds#  * user_function - function that can only be called in user context
1901da177e4SLinus Torvalds#  * @a: some argument
1911da177e4SLinus Torvalds#  * Context: !in_interrupt()
1921da177e4SLinus Torvalds#  *
1931da177e4SLinus Torvalds#  * Some description
1941da177e4SLinus Torvalds#  * Example:
1951da177e4SLinus Torvalds#  *    user_function(22);
1961da177e4SLinus Torvalds#  */
1971da177e4SLinus Torvalds# ...
1981da177e4SLinus Torvalds#
1991da177e4SLinus Torvalds#
2001da177e4SLinus Torvalds# All descriptive text is further processed, scanning for the following special
2011da177e4SLinus Torvalds# patterns, which are highlighted appropriately.
2021da177e4SLinus Torvalds#
2031da177e4SLinus Torvalds# 'funcname()' - function
2041da177e4SLinus Torvalds# '$ENVVAR' - environmental variable
2051da177e4SLinus Torvalds# '&struct_name' - name of a structure (up to two words including 'struct')
2065267dd35SPaolo Bonzini# '&struct_name.member' - name of a structure member
2071da177e4SLinus Torvalds# '@parameter' - name of a parameter
2081da177e4SLinus Torvalds# '%CONST' - name of a constant.
209b97f193aSMauro Carvalho Chehab# '``LITERAL``' - literal string without any spaces on it.
2101da177e4SLinus Torvalds
2118484baaaSRandy Dunlap## init lots of data
2128484baaaSRandy Dunlap
2131da177e4SLinus Torvaldsmy $errors = 0;
2141da177e4SLinus Torvaldsmy $warnings = 0;
2155f8c7c98SRandy Dunlapmy $anon_struct_union = 0;
2161da177e4SLinus Torvalds
2171da177e4SLinus Torvalds# match expressions used to find embedded type information
218b97f193aSMauro Carvalho Chehabmy $type_constant = '\b``([^\`]+)``\b';
219b97f193aSMauro Carvalho Chehabmy $type_constant2 = '\%([-_\w]+)';
2201da177e4SLinus Torvaldsmy $type_func = '(\w+)\(\)';
221bfd228c7SMike Rapoportmy $type_param = '\@(\w*((\.\w+)|(->\w+))*(\.\.\.)?)';
222ee2aa759SMauro Carvalho Chehabmy $type_param_ref = '([\!]?)\@(\w*((\.\w+)|(->\w+))*(\.\.\.)?)';
2235219f18aSJonathan Corbetmy $type_fp_param = '\@(\w+)\(\)';  # Special RST handling for func ptr params
224346282dbSMauro Carvalho Chehabmy $type_fp_param2 = '\@(\w+->\S+)\(\)';  # Special RST handling for structs with func ptr params
2251da177e4SLinus Torvaldsmy $type_env = '(\$\w+)';
226df31175bSPaolo Bonzinimy $type_enum = '\&(enum\s*([_\w]+))';
227df31175bSPaolo Bonzinimy $type_struct = '\&(struct\s*([_\w]+))';
228df31175bSPaolo Bonzinimy $type_typedef = '\&(typedef\s*([_\w]+))';
229df31175bSPaolo Bonzinimy $type_union = '\&(union\s*([_\w]+))';
2305267dd35SPaolo Bonzinimy $type_member = '\&([_\w]+)(\.|->)([_\w]+)';
231df31175bSPaolo Bonzinimy $type_fallback = '\&([_\w]+)';
232f3341dcfSJani Nikulamy $type_member_func = $type_member . '\(\)';
2331da177e4SLinus Torvalds
2341da177e4SLinus Torvalds# Output conversion substitutions.
2351da177e4SLinus Torvalds#  One for each output format
2361da177e4SLinus Torvalds
2371da177e4SLinus Torvalds# these are pretty rough
2384d732701SDanilo Cesar Lemes de Paulamy @highlights_man = (
2394d732701SDanilo Cesar Lemes de Paula                      [$type_constant, "\$1"],
240b97f193aSMauro Carvalho Chehab                      [$type_constant2, "\$1"],
2414d732701SDanilo Cesar Lemes de Paula                      [$type_func, "\\\\fB\$1\\\\fP"],
242df31175bSPaolo Bonzini                      [$type_enum, "\\\\fI\$1\\\\fP"],
2434d732701SDanilo Cesar Lemes de Paula                      [$type_struct, "\\\\fI\$1\\\\fP"],
244df31175bSPaolo Bonzini                      [$type_typedef, "\\\\fI\$1\\\\fP"],
245df31175bSPaolo Bonzini                      [$type_union, "\\\\fI\$1\\\\fP"],
2465267dd35SPaolo Bonzini                      [$type_param, "\\\\fI\$1\\\\fP"],
247ee2aa759SMauro Carvalho Chehab                      [$type_param_ref, "\\\\fI\$1\$2\\\\fP"],
248df31175bSPaolo Bonzini                      [$type_member, "\\\\fI\$1\$2\$3\\\\fP"],
249df31175bSPaolo Bonzini                      [$type_fallback, "\\\\fI\$1\\\\fP"]
2504d732701SDanilo Cesar Lemes de Paula		     );
2511da177e4SLinus Torvaldsmy $blankline_man = "";
2521da177e4SLinus Torvalds
253c0d1b6eeSJonathan Corbet# rst-mode
254c0d1b6eeSJonathan Corbetmy @highlights_rst = (
255c0d1b6eeSJonathan Corbet                       [$type_constant, "``\$1``"],
256b97f193aSMauro Carvalho Chehab                       [$type_constant2, "``\$1``"],
257f3341dcfSJani Nikula                       # Note: need to escape () to avoid func matching later
2585267dd35SPaolo Bonzini                       [$type_member_func, "\\:c\\:type\\:`\$1\$2\$3\\\\(\\\\) <\$1>`"],
2595267dd35SPaolo Bonzini                       [$type_member, "\\:c\\:type\\:`\$1\$2\$3 <\$1>`"],
2605219f18aSJonathan Corbet		       [$type_fp_param, "**\$1\\\\(\\\\)**"],
261346282dbSMauro Carvalho Chehab		       [$type_fp_param2, "**\$1\\\\(\\\\)**"],
262344fdb28SJonathan Corbet                       [$type_func, "\$1()"],
263df31175bSPaolo Bonzini                       [$type_enum, "\\:c\\:type\\:`\$1 <\$2>`"],
264df31175bSPaolo Bonzini                       [$type_struct, "\\:c\\:type\\:`\$1 <\$2>`"],
265df31175bSPaolo Bonzini                       [$type_typedef, "\\:c\\:type\\:`\$1 <\$2>`"],
266df31175bSPaolo Bonzini                       [$type_union, "\\:c\\:type\\:`\$1 <\$2>`"],
267a7291e7eSJani Nikula                       # in rst this can refer to any type
268df31175bSPaolo Bonzini                       [$type_fallback, "\\:c\\:type\\:`\$1`"],
269ee2aa759SMauro Carvalho Chehab                       [$type_param_ref, "**\$1\$2**"]
270c0d1b6eeSJonathan Corbet		      );
271c0d1b6eeSJonathan Corbetmy $blankline_rst = "\n";
272c0d1b6eeSJonathan Corbet
2731da177e4SLinus Torvalds# read arguments
2741da177e4SLinus Torvaldsif ($#ARGV == -1) {
27543caf1a6STomasz Warniełło	pod2usage(
27643caf1a6STomasz Warniełło		-message => "No arguments!\n",
27743caf1a6STomasz Warniełło		-exitval => 1,
27843caf1a6STomasz Warniełło		-verbose => 99,
27943caf1a6STomasz Warniełło		-sections => 'SYNOPSIS',
28043caf1a6STomasz Warniełło		-output => \*STDERR,
28143caf1a6STomasz Warniełło	);
2821da177e4SLinus Torvalds}
2831da177e4SLinus Torvalds
2848484baaaSRandy Dunlapmy $kernelversion;
28593351d41SMauro Carvalho Chehabmy ($sphinx_major, $sphinx_minor, $sphinx_patch);
286efa44475SMauro Carvalho Chehab
2878484baaaSRandy Dunlapmy $dohighlight = "";
2888484baaaSRandy Dunlap
2891da177e4SLinus Torvaldsmy $verbose = 0;
2902c12c810SPierre-Louis Bossartmy $Werror = 0;
291bdfe2be3SMauro Carvalho Chehabmy $output_mode = "rst";
292e314ba31SDaniel Santosmy $output_preformatted = 0;
2934b44595aSJohannes Bergmy $no_doc_sections = 0;
2940b0f5f29SDaniel Vettermy $enable_lineno = 0;
295bdfe2be3SMauro Carvalho Chehabmy @highlights = @highlights_rst;
296bdfe2be3SMauro Carvalho Chehabmy $blankline = $blankline_rst;
2971da177e4SLinus Torvaldsmy $modulename = "Kernel API";
298b6c3f456SJani Nikula
299b6c3f456SJani Nikulause constant {
300b6c3f456SJani Nikula    OUTPUT_ALL          => 0, # output all symbols and doc sections
301b6c3f456SJani Nikula    OUTPUT_INCLUDE      => 1, # output only specified symbols
302eab795ddSMauro Carvalho Chehab    OUTPUT_EXPORTED     => 2, # output exported symbols
303eab795ddSMauro Carvalho Chehab    OUTPUT_INTERNAL     => 3, # output non-exported symbols
304b6c3f456SJani Nikula};
305b6c3f456SJani Nikulamy $output_selection = OUTPUT_ALL;
306b0d60bfbSJonathan Corbetmy $show_not_found = 0;	# No longer used
307b2c4105bSBen Hutchings
30888c2b57dSJani Nikulamy @export_file_list;
30988c2b57dSJani Nikula
310b2c4105bSBen Hutchingsmy @build_time;
311b2c4105bSBen Hutchingsif (defined($ENV{'KBUILD_BUILD_TIMESTAMP'}) &&
312b2c4105bSBen Hutchings    (my $seconds = `date -d"${ENV{'KBUILD_BUILD_TIMESTAMP'}}" +%s`) ne '') {
313b2c4105bSBen Hutchings    @build_time = gmtime($seconds);
314b2c4105bSBen Hutchings} else {
315b2c4105bSBen Hutchings    @build_time = localtime;
316b2c4105bSBen Hutchings}
317b2c4105bSBen Hutchings
3181da177e4SLinus Torvaldsmy $man_date = ('January', 'February', 'March', 'April', 'May', 'June',
3191da177e4SLinus Torvalds		'July', 'August', 'September', 'October',
320b2c4105bSBen Hutchings		'November', 'December')[$build_time[4]] .
321b2c4105bSBen Hutchings  " " . ($build_time[5]+1900);
3221da177e4SLinus Torvalds
3238484baaaSRandy Dunlap# Essentially these are globals.
324b9d97328SRandy Dunlap# They probably want to be tidied up, made more localised or something.
325b9d97328SRandy Dunlap# CAVEAT EMPTOR!  Some of the others I localised may not want to be, which
3261da177e4SLinus Torvalds# could cause "use of undefined value" or other bugs.
3271da177e4SLinus Torvaldsmy ($function, %function_table, %parametertypes, $declaration_purpose);
328eab795ddSMauro Carvalho Chehabmy %nosymbol_table = ();
3290b0f5f29SDaniel Vettermy $declaration_start_line;
3301da177e4SLinus Torvaldsmy ($type, $declaration_name, $return_type);
3311c32fd0cSIlya Dryomovmy ($newsection, $newcontents, $prototype, $brcount, %source_map);
3321da177e4SLinus Torvalds
333bd0e88e5SRandy Dunlapif (defined($ENV{'KBUILD_VERBOSE'})) {
334bd0e88e5SRandy Dunlap	$verbose = "$ENV{'KBUILD_VERBOSE'}";
335bd0e88e5SRandy Dunlap}
336bd0e88e5SRandy Dunlap
3372c12c810SPierre-Louis Bossartif (defined($ENV{'KCFLAGS'})) {
3382c12c810SPierre-Louis Bossart	my $kcflags = "$ENV{'KCFLAGS'}";
3392c12c810SPierre-Louis Bossart
3402c12c810SPierre-Louis Bossart	if ($kcflags =~ /Werror/) {
3412c12c810SPierre-Louis Bossart		$Werror = 1;
3422c12c810SPierre-Louis Bossart	}
3432c12c810SPierre-Louis Bossart}
3442c12c810SPierre-Louis Bossart
345bed4ed30SLaurent Pinchartif (defined($ENV{'KDOC_WERROR'})) {
346bed4ed30SLaurent Pinchart	$Werror = "$ENV{'KDOC_WERROR'}";
347bed4ed30SLaurent Pinchart}
348bed4ed30SLaurent Pinchart
3491da177e4SLinus Torvalds# Generated docbook code is inserted in a template at a point where
3501da177e4SLinus Torvalds# docbook v3.1 requires a non-zero sequence of RefEntry's; see:
35193431e06SAlexander A. Klimov# https://www.oasis-open.org/docbook/documentation/reference/html/refentry.html
3521da177e4SLinus Torvalds# We keep track of number of generated entries and generate a dummy
3531da177e4SLinus Torvalds# if needs be to ensure the expanded template can be postprocessed
3541da177e4SLinus Torvalds# into html.
3551da177e4SLinus Torvaldsmy $section_counter = 0;
3561da177e4SLinus Torvalds
3571da177e4SLinus Torvaldsmy $lineprefix="";
3581da177e4SLinus Torvalds
35948af606aSJani Nikula# Parser states
36048af606aSJani Nikulause constant {
36148af606aSJani Nikula    STATE_NORMAL        => 0,        # normal code
36248af606aSJani Nikula    STATE_NAME          => 1,        # looking for function name
36317b78717SJonathan Corbet    STATE_BODY_MAYBE    => 2,        # body - or maybe more description
36417b78717SJonathan Corbet    STATE_BODY          => 3,        # the body of the comment
3650d55d48bSMauro Carvalho Chehab    STATE_BODY_WITH_BLANK_LINE => 4, # the body, which has a blank line
3660d55d48bSMauro Carvalho Chehab    STATE_PROTO         => 5,        # scanning prototype
3670d55d48bSMauro Carvalho Chehab    STATE_DOCBLOCK      => 6,        # documentation block
3680d55d48bSMauro Carvalho Chehab    STATE_INLINE        => 7,        # gathering doc outside main block
36948af606aSJani Nikula};
3701da177e4SLinus Torvaldsmy $state;
371850622dfSRandy Dunlapmy $in_doc_sect;
372d742f24dSJonathan Corbetmy $leading_space;
3731da177e4SLinus Torvalds
37448af606aSJani Nikula# Inline documentation state
37548af606aSJani Nikulause constant {
37648af606aSJani Nikula    STATE_INLINE_NA     => 0, # not applicable ($state != STATE_INLINE)
37748af606aSJani Nikula    STATE_INLINE_NAME   => 1, # looking for member name (@foo:)
37848af606aSJani Nikula    STATE_INLINE_TEXT   => 2, # looking for member documentation
37948af606aSJani Nikula    STATE_INLINE_END    => 3, # done
38048af606aSJani Nikula    STATE_INLINE_ERROR  => 4, # error - Comment without header was found.
38148af606aSJani Nikula                              # Spit a warning as it's not
382a4c6ebedSDanilo Cesar Lemes de Paula                              # proper kernel-doc and ignore the rest.
38348af606aSJani Nikula};
38448af606aSJani Nikulamy $inline_doc_state;
385a4c6ebedSDanilo Cesar Lemes de Paula
3861da177e4SLinus Torvalds#declaration types: can be
3871da177e4SLinus Torvalds# 'function', 'struct', 'union', 'enum', 'typedef'
3881da177e4SLinus Torvaldsmy $decl_type;
3891da177e4SLinus Torvalds
39052042e2dSMauro Carvalho Chehab# Name of the kernel-doc identifier for non-DOC markups
39152042e2dSMauro Carvalho Chehabmy $identifier;
39252042e2dSMauro Carvalho Chehab
3931da177e4SLinus Torvaldsmy $doc_start = '^/\*\*\s*$'; # Allow whitespace at end of comment start.
3941da177e4SLinus Torvaldsmy $doc_end = '\*/';
3951da177e4SLinus Torvaldsmy $doc_com = '\s*\*\s*';
39612ae6779SDaniel Santosmy $doc_com_body = '\s*\* ?';
3971da177e4SLinus Torvaldsmy $doc_decl = $doc_com . '(\w+)';
398f624adefSJani Nikula# @params and a strictly limited set of supported section names
399212209cfSJonathan Corbet# Specifically:
400212209cfSJonathan Corbet#   Match @word:
401212209cfSJonathan Corbet#	  @...:
402212209cfSJonathan Corbet#         @{section-name}:
403212209cfSJonathan Corbet# while trying to not match literal block starts like "example::"
404212209cfSJonathan Corbet#
4058569de68SJonathan Corbetmy $doc_sect = $doc_com .
406212209cfSJonathan Corbet    '\s*(\@[.\w]+|\@\.\.\.|description|context|returns?|notes?|examples?)\s*:([^:].*)?$';
40712ae6779SDaniel Santosmy $doc_content = $doc_com_body . '(.*)';
4081da177e4SLinus Torvaldsmy $doc_block = $doc_com . 'DOC:\s*(.*)?';
40948af606aSJani Nikulamy $doc_inline_start = '^\s*/\*\*\s*$';
410fe7bc493SMauro Carvalho Chehabmy $doc_inline_sect = '\s*\*\s*(@\s*[\w][\w\.]*\s*):(.*)';
41148af606aSJani Nikulamy $doc_inline_end = '^\s*\*/\s*$';
4120c9aa209SJani Nikulamy $doc_inline_oneline = '^\s*/\*\*\s*(@[\w\s]+):\s*(.*)\s*\*/\s*$';
41386ae2e38SJani Nikulamy $export_symbol = '^\s*EXPORT_SYMBOL(_GPL)?\s*\(\s*(\w+)\s*\)\s*;';
414e86bdb24SAditya Srivastavamy $function_pointer = qr{([^\(]*\(\*)\s*\)\s*\(([^\)]*)\)};
415e86bdb24SAditya Srivastavamy $attribute = qr{__attribute__\s*\(\([a-z0-9,_\*\s\(\)]*\)\)}i;
4161da177e4SLinus Torvalds
4171da177e4SLinus Torvaldsmy %parameterdescs;
4180b0f5f29SDaniel Vettermy %parameterdesc_start_lines;
4191da177e4SLinus Torvaldsmy @parameterlist;
4201da177e4SLinus Torvaldsmy %sections;
4211da177e4SLinus Torvaldsmy @sectionlist;
4220b0f5f29SDaniel Vettermy %section_start_lines;
423a1d94aa5SRandy Dunlapmy $sectcheck;
424a1d94aa5SRandy Dunlapmy $struct_actual;
4251da177e4SLinus Torvalds
4261da177e4SLinus Torvaldsmy $contents = "";
4270b0f5f29SDaniel Vettermy $new_start_line = 0;
428f624adefSJani Nikula
429f624adefSJani Nikula# the canonical section names. see also $doc_sect above.
4301da177e4SLinus Torvaldsmy $section_default = "Description";	# default section
4311da177e4SLinus Torvaldsmy $section_intro = "Introduction";
4321da177e4SLinus Torvaldsmy $section = $section_default;
4331da177e4SLinus Torvaldsmy $section_context = "Context";
4344092bac7SYacine Belkadimy $section_return = "Return";
4351da177e4SLinus Torvalds
4361da177e4SLinus Torvaldsmy $undescribed = "-- undescribed --";
4371da177e4SLinus Torvalds
4381da177e4SLinus Torvaldsreset_state();
4391da177e4SLinus Torvalds
440b031ac4eSMauro Carvalho Chehabwhile ($ARGV[0] =~ m/^--?(.*)/) {
441b031ac4eSMauro Carvalho Chehab    my $cmd = $1;
442b031ac4eSMauro Carvalho Chehab    shift @ARGV;
443b031ac4eSMauro Carvalho Chehab    if ($cmd eq "man") {
4441da177e4SLinus Torvalds	$output_mode = "man";
4454d732701SDanilo Cesar Lemes de Paula	@highlights = @highlights_man;
4461da177e4SLinus Torvalds	$blankline = $blankline_man;
447b031ac4eSMauro Carvalho Chehab    } elsif ($cmd eq "rst") {
448c0d1b6eeSJonathan Corbet	$output_mode = "rst";
449c0d1b6eeSJonathan Corbet	@highlights = @highlights_rst;
450c0d1b6eeSJonathan Corbet	$blankline = $blankline_rst;
451b031ac4eSMauro Carvalho Chehab    } elsif ($cmd eq "none") {
4523a025e1dSMatthew Wilcox	$output_mode = "none";
453b031ac4eSMauro Carvalho Chehab    } elsif ($cmd eq "module") { # not needed for XML, inherits from calling document
4541da177e4SLinus Torvalds	$modulename = shift @ARGV;
455b031ac4eSMauro Carvalho Chehab    } elsif ($cmd eq "function") { # to only output specific functions
456b6c3f456SJani Nikula	$output_selection = OUTPUT_INCLUDE;
4571da177e4SLinus Torvalds	$function = shift @ARGV;
4581da177e4SLinus Torvalds	$function_table{$function} = 1;
459eab795ddSMauro Carvalho Chehab    } elsif ($cmd eq "nosymbol") { # Exclude specific symbols
460eab795ddSMauro Carvalho Chehab	my $symbol = shift @ARGV;
461eab795ddSMauro Carvalho Chehab	$nosymbol_table{$symbol} = 1;
462b031ac4eSMauro Carvalho Chehab    } elsif ($cmd eq "export") { # only exported symbols
463b6c3f456SJani Nikula	$output_selection = OUTPUT_EXPORTED;
464da9726ecSJani Nikula	%function_table = ();
465b031ac4eSMauro Carvalho Chehab    } elsif ($cmd eq "internal") { # only non-exported symbols
466b6c3f456SJani Nikula	$output_selection = OUTPUT_INTERNAL;
467da9726ecSJani Nikula	%function_table = ();
468b031ac4eSMauro Carvalho Chehab    } elsif ($cmd eq "export-file") {
46988c2b57dSJani Nikula	my $file = shift @ARGV;
47088c2b57dSJani Nikula	push(@export_file_list, $file);
471b031ac4eSMauro Carvalho Chehab    } elsif ($cmd eq "v") {
4721da177e4SLinus Torvalds	$verbose = 1;
4732c12c810SPierre-Louis Bossart    } elsif ($cmd eq "Werror") {
4742c12c810SPierre-Louis Bossart	$Werror = 1;
475b031ac4eSMauro Carvalho Chehab    } elsif (($cmd eq "h") || ($cmd eq "help")) {
4761da177e4SLinus Torvalds	usage();
477b031ac4eSMauro Carvalho Chehab    } elsif ($cmd eq 'no-doc-sections') {
4784b44595aSJohannes Berg	    $no_doc_sections = 1;
479b031ac4eSMauro Carvalho Chehab    } elsif ($cmd eq 'enable-lineno') {
4800b0f5f29SDaniel Vetter	    $enable_lineno = 1;
481b031ac4eSMauro Carvalho Chehab    } elsif ($cmd eq 'show-not-found') {
482b0d60bfbSJonathan Corbet	$show_not_found = 1;  # A no-op but don't fail
48393351d41SMauro Carvalho Chehab    } elsif ($cmd eq "sphinx-version") {
48493351d41SMauro Carvalho Chehab	my $ver_string = shift @ARGV;
48593351d41SMauro Carvalho Chehab	if ($ver_string =~ m/^(\d+)(\.\d+)?(\.\d+)?/) {
48693351d41SMauro Carvalho Chehab	    $sphinx_major = $1;
48793351d41SMauro Carvalho Chehab	    if (defined($2)) {
48893351d41SMauro Carvalho Chehab		$sphinx_minor = substr($2,1);
48993351d41SMauro Carvalho Chehab	    } else {
49093351d41SMauro Carvalho Chehab		$sphinx_minor = 0;
49193351d41SMauro Carvalho Chehab	    }
49293351d41SMauro Carvalho Chehab	    if (defined($3)) {
49393351d41SMauro Carvalho Chehab		$sphinx_patch = substr($3,1)
49493351d41SMauro Carvalho Chehab	    } else {
49593351d41SMauro Carvalho Chehab		$sphinx_patch = 0;
49693351d41SMauro Carvalho Chehab	    }
49793351d41SMauro Carvalho Chehab	} else {
49893351d41SMauro Carvalho Chehab	    die "Sphinx version should either major.minor or major.minor.patch format\n";
49993351d41SMauro Carvalho Chehab	}
500b031ac4eSMauro Carvalho Chehab    } else {
501b031ac4eSMauro Carvalho Chehab		# Unknown argument
50243caf1a6STomasz Warniełło		pod2usage(
50343caf1a6STomasz Warniełło			-message => "Argument unknown!\n",
50443caf1a6STomasz Warniełło			-exitval => 1,
50543caf1a6STomasz Warniełło			-verbose => 99,
50643caf1a6STomasz Warniełło			-sections => 'SYNOPSIS',
50743caf1a6STomasz Warniełło			-output => \*STDERR,
50843caf1a6STomasz Warniełło		);
5091da177e4SLinus Torvalds    }
5101da177e4SLinus Torvalds}
5111da177e4SLinus Torvalds
5128484baaaSRandy Dunlap# continue execution near EOF;
5138484baaaSRandy Dunlap
514efa44475SMauro Carvalho Chehab# The C domain dialect changed on Sphinx 3. So, we need to check the
515efa44475SMauro Carvalho Chehab# version in order to produce the right tags.
516efa44475SMauro Carvalho Chehabsub findprog($)
517efa44475SMauro Carvalho Chehab{
518efa44475SMauro Carvalho Chehab	foreach(split(/:/, $ENV{PATH})) {
519efa44475SMauro Carvalho Chehab		return "$_/$_[0]" if(-x "$_/$_[0]");
520efa44475SMauro Carvalho Chehab	}
521efa44475SMauro Carvalho Chehab}
522efa44475SMauro Carvalho Chehab
523efa44475SMauro Carvalho Chehabsub get_sphinx_version()
524efa44475SMauro Carvalho Chehab{
525efa44475SMauro Carvalho Chehab	my $ver;
526efa44475SMauro Carvalho Chehab
527efa44475SMauro Carvalho Chehab	my $cmd = "sphinx-build";
528efa44475SMauro Carvalho Chehab	if (!findprog($cmd)) {
529efa44475SMauro Carvalho Chehab		my $cmd = "sphinx-build3";
53093351d41SMauro Carvalho Chehab		if (!findprog($cmd)) {
53193351d41SMauro Carvalho Chehab			$sphinx_major = 1;
53293351d41SMauro Carvalho Chehab			$sphinx_minor = 2;
53393351d41SMauro Carvalho Chehab			$sphinx_patch = 0;
53493351d41SMauro Carvalho Chehab			printf STDERR "Warning: Sphinx version not found. Using default (Sphinx version %d.%d.%d)\n",
53593351d41SMauro Carvalho Chehab			       $sphinx_major, $sphinx_minor, $sphinx_patch;
53693351d41SMauro Carvalho Chehab			return;
53793351d41SMauro Carvalho Chehab		}
538efa44475SMauro Carvalho Chehab	}
539efa44475SMauro Carvalho Chehab
540efa44475SMauro Carvalho Chehab	open IN, "$cmd --version 2>&1 |";
541efa44475SMauro Carvalho Chehab	while (<IN>) {
542efa44475SMauro Carvalho Chehab		if (m/^\s*sphinx-build\s+([\d]+)\.([\d\.]+)(\+\/[\da-f]+)?$/) {
54393351d41SMauro Carvalho Chehab			$sphinx_major = $1;
54493351d41SMauro Carvalho Chehab			$sphinx_minor = $2;
54593351d41SMauro Carvalho Chehab			$sphinx_patch = $3;
546efa44475SMauro Carvalho Chehab			last;
547efa44475SMauro Carvalho Chehab		}
548efa44475SMauro Carvalho Chehab		# Sphinx 1.2.x uses a different format
549efa44475SMauro Carvalho Chehab		if (m/^\s*Sphinx.*\s+([\d]+)\.([\d\.]+)$/) {
55093351d41SMauro Carvalho Chehab			$sphinx_major = $1;
55193351d41SMauro Carvalho Chehab			$sphinx_minor = $2;
55293351d41SMauro Carvalho Chehab			$sphinx_patch = $3;
553efa44475SMauro Carvalho Chehab			last;
554efa44475SMauro Carvalho Chehab		}
555efa44475SMauro Carvalho Chehab	}
556efa44475SMauro Carvalho Chehab	close IN;
557efa44475SMauro Carvalho Chehab}
558efa44475SMauro Carvalho Chehab
55953f049faSBorislav Petkov# get kernel version from env
56053f049faSBorislav Petkovsub get_kernel_version() {
5611b9bc22dSJohannes Berg    my $version = 'unknown kernel version';
56253f049faSBorislav Petkov
56353f049faSBorislav Petkov    if (defined($ENV{'KERNELVERSION'})) {
56453f049faSBorislav Petkov	$version = $ENV{'KERNELVERSION'};
56553f049faSBorislav Petkov    }
56653f049faSBorislav Petkov    return $version;
56753f049faSBorislav Petkov}
5681da177e4SLinus Torvalds
5690b0f5f29SDaniel Vetter#
5700b0f5f29SDaniel Vettersub print_lineno {
5710b0f5f29SDaniel Vetter    my $lineno = shift;
5720b0f5f29SDaniel Vetter    if ($enable_lineno && defined($lineno)) {
5730b0f5f29SDaniel Vetter        print "#define LINENO " . $lineno . "\n";
5740b0f5f29SDaniel Vetter    }
5750b0f5f29SDaniel Vetter}
5761da177e4SLinus Torvalds##
5771da177e4SLinus Torvalds# dumps section contents to arrays/hashes intended for that purpose.
5781da177e4SLinus Torvalds#
5791da177e4SLinus Torvaldssub dump_section {
58094dc7ad5SRandy Dunlap    my $file = shift;
5811da177e4SLinus Torvalds    my $name = shift;
5821da177e4SLinus Torvalds    my $contents = join "\n", @_;
5831da177e4SLinus Torvalds
58413901ef2SJani Nikula    if ($name =~ m/$type_param/) {
5851da177e4SLinus Torvalds	$name = $1;
5861da177e4SLinus Torvalds	$parameterdescs{$name} = $contents;
587a1d94aa5SRandy Dunlap	$sectcheck = $sectcheck . $name . " ";
5880b0f5f29SDaniel Vetter        $parameterdesc_start_lines{$name} = $new_start_line;
5890b0f5f29SDaniel Vetter        $new_start_line = 0;
590ced69090SRandy Dunlap    } elsif ($name eq "@\.\.\.") {
591ced69090SRandy Dunlap	$name = "...";
592ced69090SRandy Dunlap	$parameterdescs{$name} = $contents;
593a1d94aa5SRandy Dunlap	$sectcheck = $sectcheck . $name . " ";
5940b0f5f29SDaniel Vetter        $parameterdesc_start_lines{$name} = $new_start_line;
5950b0f5f29SDaniel Vetter        $new_start_line = 0;
5961da177e4SLinus Torvalds    } else {
59794dc7ad5SRandy Dunlap	if (defined($sections{$name}) && ($sections{$name} ne "")) {
59895b6be9dSJani Nikula	    # Only warn on user specified duplicate section names.
59995b6be9dSJani Nikula	    if ($name ne $section_default) {
60032217761SJani Nikula		print STDERR "${file}:$.: warning: duplicate section name '$name'\n";
60132217761SJani Nikula		++$warnings;
60295b6be9dSJani Nikula	    }
60332217761SJani Nikula	    $sections{$name} .= $contents;
60432217761SJani Nikula	} else {
6051da177e4SLinus Torvalds	    $sections{$name} = $contents;
6061da177e4SLinus Torvalds	    push @sectionlist, $name;
6070b0f5f29SDaniel Vetter            $section_start_lines{$name} = $new_start_line;
6080b0f5f29SDaniel Vetter            $new_start_line = 0;
6091da177e4SLinus Torvalds	}
6101da177e4SLinus Torvalds    }
61132217761SJani Nikula}
6121da177e4SLinus Torvalds
6131da177e4SLinus Torvalds##
614b112e0f7SJohannes Berg# dump DOC: section after checking that it should go out
615b112e0f7SJohannes Berg#
616b112e0f7SJohannes Bergsub dump_doc_section {
61794dc7ad5SRandy Dunlap    my $file = shift;
618b112e0f7SJohannes Berg    my $name = shift;
619b112e0f7SJohannes Berg    my $contents = join "\n", @_;
620b112e0f7SJohannes Berg
6214b44595aSJohannes Berg    if ($no_doc_sections) {
6224b44595aSJohannes Berg        return;
6234b44595aSJohannes Berg    }
6244b44595aSJohannes Berg
625eab795ddSMauro Carvalho Chehab    return if (defined($nosymbol_table{$name}));
626eab795ddSMauro Carvalho Chehab
627b6c3f456SJani Nikula    if (($output_selection == OUTPUT_ALL) ||
628eab795ddSMauro Carvalho Chehab	(($output_selection == OUTPUT_INCLUDE) &&
629eab795ddSMauro Carvalho Chehab	 defined($function_table{$name})))
630b112e0f7SJohannes Berg    {
63194dc7ad5SRandy Dunlap	dump_section($file, $name, $contents);
632b112e0f7SJohannes Berg	output_blockhead({'sectionlist' => \@sectionlist,
633b112e0f7SJohannes Berg			  'sections' => \%sections,
634b112e0f7SJohannes Berg			  'module' => $modulename,
635b6c3f456SJani Nikula			  'content-only' => ($output_selection != OUTPUT_ALL), });
636b112e0f7SJohannes Berg    }
637b112e0f7SJohannes Berg}
638b112e0f7SJohannes Berg
639b112e0f7SJohannes Berg##
6401da177e4SLinus Torvalds# output function
6411da177e4SLinus Torvalds#
6421da177e4SLinus Torvalds# parameterdescs, a hash.
6431da177e4SLinus Torvalds#  function => "function name"
6441da177e4SLinus Torvalds#  parameterlist => @list of parameters
6451da177e4SLinus Torvalds#  parameterdescs => %parameter descriptions
6461da177e4SLinus Torvalds#  sectionlist => @list of sections
647a21217daSRandy Dunlap#  sections => %section descriptions
6481da177e4SLinus Torvalds#
6491da177e4SLinus Torvalds
6501da177e4SLinus Torvaldssub output_highlight {
6511da177e4SLinus Torvalds    my $contents = join "\n",@_;
6521da177e4SLinus Torvalds    my $line;
6531da177e4SLinus Torvalds
6541da177e4SLinus Torvalds#   DEBUG
6551da177e4SLinus Torvalds#   if (!defined $contents) {
6561da177e4SLinus Torvalds#	use Carp;
6571da177e4SLinus Torvalds#	confess "output_highlight got called with no args?\n";
6581da177e4SLinus Torvalds#   }
6591da177e4SLinus Torvalds
6603eb014a1SRandy Dunlap#   print STDERR "contents b4:$contents\n";
6611da177e4SLinus Torvalds    eval $dohighlight;
6621da177e4SLinus Torvalds    die $@ if $@;
6633eb014a1SRandy Dunlap#   print STDERR "contents af:$contents\n";
6643eb014a1SRandy Dunlap
6651da177e4SLinus Torvalds    foreach $line (split "\n", $contents) {
66612ae6779SDaniel Santos	if (! $output_preformatted) {
66712ae6779SDaniel Santos	    $line =~ s/^\s*//;
66812ae6779SDaniel Santos	}
6691da177e4SLinus Torvalds	if ($line eq ""){
670e314ba31SDaniel Santos	    if (! $output_preformatted) {
6710bba924cSJonathan Corbet		print $lineprefix, $blankline;
672e314ba31SDaniel Santos	    }
6731da177e4SLinus Torvalds	} else {
674cdccb316SRandy Dunlap	    if ($output_mode eq "man" && substr($line, 0, 1) eq ".") {
675cdccb316SRandy Dunlap		print "\\&$line";
676cdccb316SRandy Dunlap	    } else {
6771da177e4SLinus Torvalds		print $lineprefix, $line;
6781da177e4SLinus Torvalds	    }
679cdccb316SRandy Dunlap	}
6801da177e4SLinus Torvalds	print "\n";
6811da177e4SLinus Torvalds    }
6821da177e4SLinus Torvalds}
6831da177e4SLinus Torvalds
6841da177e4SLinus Torvalds##
6851da177e4SLinus Torvalds# output function in man
6861da177e4SLinus Torvaldssub output_function_man(%) {
6871da177e4SLinus Torvalds    my %args = %{$_[0]};
6881da177e4SLinus Torvalds    my ($parameter, $section);
6891da177e4SLinus Torvalds    my $count;
6901da177e4SLinus Torvalds
6911da177e4SLinus Torvalds    print ".TH \"$args{'function'}\" 9 \"$args{'function'}\" \"$man_date\" \"Kernel Hacker's Manual\" LINUX\n";
6921da177e4SLinus Torvalds
6931da177e4SLinus Torvalds    print ".SH NAME\n";
6941da177e4SLinus Torvalds    print $args{'function'} . " \\- " . $args{'purpose'} . "\n";
6951da177e4SLinus Torvalds
6961da177e4SLinus Torvalds    print ".SH SYNOPSIS\n";
697a21217daSRandy Dunlap    if ($args{'functiontype'} ne "") {
6981da177e4SLinus Torvalds	print ".B \"" . $args{'functiontype'} . "\" " . $args{'function'} . "\n";
699a21217daSRandy Dunlap    } else {
700a21217daSRandy Dunlap	print ".B \"" . $args{'function'} . "\n";
701a21217daSRandy Dunlap    }
7021da177e4SLinus Torvalds    $count = 0;
7031da177e4SLinus Torvalds    my $parenth = "(";
7041da177e4SLinus Torvalds    my $post = ",";
7051da177e4SLinus Torvalds    foreach my $parameter (@{$args{'parameterlist'}}) {
7061da177e4SLinus Torvalds	if ($count == $#{$args{'parameterlist'}}) {
7071da177e4SLinus Torvalds	    $post = ");";
7081da177e4SLinus Torvalds	}
7091da177e4SLinus Torvalds	$type = $args{'parametertypes'}{$parameter};
710e86bdb24SAditya Srivastava	if ($type =~ m/$function_pointer/) {
7111da177e4SLinus Torvalds	    # pointer-to-function
712ed8348e2SMauro Carvalho Chehab	    print ".BI \"" . $parenth . $1 . "\" " . " \") (" . $2 . ")" . $post . "\"\n";
7131da177e4SLinus Torvalds	} else {
7141da177e4SLinus Torvalds	    $type =~ s/([^\*])$/$1 /;
715ed8348e2SMauro Carvalho Chehab	    print ".BI \"" . $parenth . $type . "\" " . " \"" . $post . "\"\n";
7161da177e4SLinus Torvalds	}
7171da177e4SLinus Torvalds	$count++;
7181da177e4SLinus Torvalds	$parenth = "";
7191da177e4SLinus Torvalds    }
7201da177e4SLinus Torvalds
7211da177e4SLinus Torvalds    print ".SH ARGUMENTS\n";
7221da177e4SLinus Torvalds    foreach $parameter (@{$args{'parameterlist'}}) {
7231da177e4SLinus Torvalds	my $parameter_name = $parameter;
7241da177e4SLinus Torvalds	$parameter_name =~ s/\[.*//;
7251da177e4SLinus Torvalds
7261da177e4SLinus Torvalds	print ".IP \"" . $parameter . "\" 12\n";
7271da177e4SLinus Torvalds	output_highlight($args{'parameterdescs'}{$parameter_name});
7281da177e4SLinus Torvalds    }
7291da177e4SLinus Torvalds    foreach $section (@{$args{'sectionlist'}}) {
7301da177e4SLinus Torvalds	print ".SH \"", uc $section, "\"\n";
7311da177e4SLinus Torvalds	output_highlight($args{'sections'}{$section});
7321da177e4SLinus Torvalds    }
7331da177e4SLinus Torvalds}
7341da177e4SLinus Torvalds
7351da177e4SLinus Torvalds##
7361da177e4SLinus Torvalds# output enum in man
7371da177e4SLinus Torvaldssub output_enum_man(%) {
7381da177e4SLinus Torvalds    my %args = %{$_[0]};
7391da177e4SLinus Torvalds    my ($parameter, $section);
7401da177e4SLinus Torvalds    my $count;
7411da177e4SLinus Torvalds
7421da177e4SLinus Torvalds    print ".TH \"$args{'module'}\" 9 \"enum $args{'enum'}\" \"$man_date\" \"API Manual\" LINUX\n";
7431da177e4SLinus Torvalds
7441da177e4SLinus Torvalds    print ".SH NAME\n";
7451da177e4SLinus Torvalds    print "enum " . $args{'enum'} . " \\- " . $args{'purpose'} . "\n";
7461da177e4SLinus Torvalds
7471da177e4SLinus Torvalds    print ".SH SYNOPSIS\n";
7481da177e4SLinus Torvalds    print "enum " . $args{'enum'} . " {\n";
7491da177e4SLinus Torvalds    $count = 0;
7501da177e4SLinus Torvalds    foreach my $parameter (@{$args{'parameterlist'}}) {
7511da177e4SLinus Torvalds	print ".br\n.BI \"    $parameter\"\n";
7521da177e4SLinus Torvalds	if ($count == $#{$args{'parameterlist'}}) {
7531da177e4SLinus Torvalds	    print "\n};\n";
7541da177e4SLinus Torvalds	    last;
7551da177e4SLinus Torvalds	}
7561da177e4SLinus Torvalds	else {
7571da177e4SLinus Torvalds	    print ", \n.br\n";
7581da177e4SLinus Torvalds	}
7591da177e4SLinus Torvalds	$count++;
7601da177e4SLinus Torvalds    }
7611da177e4SLinus Torvalds
7621da177e4SLinus Torvalds    print ".SH Constants\n";
7631da177e4SLinus Torvalds    foreach $parameter (@{$args{'parameterlist'}}) {
7641da177e4SLinus Torvalds	my $parameter_name = $parameter;
7651da177e4SLinus Torvalds	$parameter_name =~ s/\[.*//;
7661da177e4SLinus Torvalds
7671da177e4SLinus Torvalds	print ".IP \"" . $parameter . "\" 12\n";
7681da177e4SLinus Torvalds	output_highlight($args{'parameterdescs'}{$parameter_name});
7691da177e4SLinus Torvalds    }
7701da177e4SLinus Torvalds    foreach $section (@{$args{'sectionlist'}}) {
7711da177e4SLinus Torvalds	print ".SH \"$section\"\n";
7721da177e4SLinus Torvalds	output_highlight($args{'sections'}{$section});
7731da177e4SLinus Torvalds    }
7741da177e4SLinus Torvalds}
7751da177e4SLinus Torvalds
7761da177e4SLinus Torvalds##
7771da177e4SLinus Torvalds# output struct in man
7781da177e4SLinus Torvaldssub output_struct_man(%) {
7791da177e4SLinus Torvalds    my %args = %{$_[0]};
7801da177e4SLinus Torvalds    my ($parameter, $section);
7811da177e4SLinus Torvalds
7821da177e4SLinus Torvalds    print ".TH \"$args{'module'}\" 9 \"" . $args{'type'} . " " . $args{'struct'} . "\" \"$man_date\" \"API Manual\" LINUX\n";
7831da177e4SLinus Torvalds
7841da177e4SLinus Torvalds    print ".SH NAME\n";
7851da177e4SLinus Torvalds    print $args{'type'} . " " . $args{'struct'} . " \\- " . $args{'purpose'} . "\n";
7861da177e4SLinus Torvalds
7878ad72163SMauro Carvalho Chehab    my $declaration = $args{'definition'};
7888ad72163SMauro Carvalho Chehab    $declaration =~ s/\t/  /g;
7898ad72163SMauro Carvalho Chehab    $declaration =~ s/\n/"\n.br\n.BI \"/g;
7901da177e4SLinus Torvalds    print ".SH SYNOPSIS\n";
7911da177e4SLinus Torvalds    print $args{'type'} . " " . $args{'struct'} . " {\n.br\n";
7928ad72163SMauro Carvalho Chehab    print ".BI \"$declaration\n};\n.br\n\n";
7931da177e4SLinus Torvalds
794c51d3dacSRandy Dunlap    print ".SH Members\n";
7951da177e4SLinus Torvalds    foreach $parameter (@{$args{'parameterlist'}}) {
7961da177e4SLinus Torvalds	($parameter =~ /^#/) && next;
7971da177e4SLinus Torvalds
7981da177e4SLinus Torvalds	my $parameter_name = $parameter;
7991da177e4SLinus Torvalds	$parameter_name =~ s/\[.*//;
8001da177e4SLinus Torvalds
8011da177e4SLinus Torvalds	($args{'parameterdescs'}{$parameter_name} ne $undescribed) || next;
8021da177e4SLinus Torvalds	print ".IP \"" . $parameter . "\" 12\n";
8031da177e4SLinus Torvalds	output_highlight($args{'parameterdescs'}{$parameter_name});
8041da177e4SLinus Torvalds    }
8051da177e4SLinus Torvalds    foreach $section (@{$args{'sectionlist'}}) {
8061da177e4SLinus Torvalds	print ".SH \"$section\"\n";
8071da177e4SLinus Torvalds	output_highlight($args{'sections'}{$section});
8081da177e4SLinus Torvalds    }
8091da177e4SLinus Torvalds}
8101da177e4SLinus Torvalds
8111da177e4SLinus Torvalds##
8121da177e4SLinus Torvalds# output typedef in man
8131da177e4SLinus Torvaldssub output_typedef_man(%) {
8141da177e4SLinus Torvalds    my %args = %{$_[0]};
8151da177e4SLinus Torvalds    my ($parameter, $section);
8161da177e4SLinus Torvalds
8171da177e4SLinus Torvalds    print ".TH \"$args{'module'}\" 9 \"$args{'typedef'}\" \"$man_date\" \"API Manual\" LINUX\n";
8181da177e4SLinus Torvalds
8191da177e4SLinus Torvalds    print ".SH NAME\n";
8201da177e4SLinus Torvalds    print "typedef " . $args{'typedef'} . " \\- " . $args{'purpose'} . "\n";
8211da177e4SLinus Torvalds
8221da177e4SLinus Torvalds    foreach $section (@{$args{'sectionlist'}}) {
8231da177e4SLinus Torvalds	print ".SH \"$section\"\n";
8241da177e4SLinus Torvalds	output_highlight($args{'sections'}{$section});
8251da177e4SLinus Torvalds    }
8261da177e4SLinus Torvalds}
8271da177e4SLinus Torvalds
828b112e0f7SJohannes Bergsub output_blockhead_man(%) {
8291da177e4SLinus Torvalds    my %args = %{$_[0]};
8301da177e4SLinus Torvalds    my ($parameter, $section);
8311da177e4SLinus Torvalds    my $count;
8321da177e4SLinus Torvalds
8331da177e4SLinus Torvalds    print ".TH \"$args{'module'}\" 9 \"$args{'module'}\" \"$man_date\" \"API Manual\" LINUX\n";
8341da177e4SLinus Torvalds
8351da177e4SLinus Torvalds    foreach $section (@{$args{'sectionlist'}}) {
8361da177e4SLinus Torvalds	print ".SH \"$section\"\n";
8371da177e4SLinus Torvalds	output_highlight($args{'sections'}{$section});
8381da177e4SLinus Torvalds    }
8391da177e4SLinus Torvalds}
8401da177e4SLinus Torvalds
8411da177e4SLinus Torvalds##
842c0d1b6eeSJonathan Corbet# output in restructured text
843c0d1b6eeSJonathan Corbet#
844c0d1b6eeSJonathan Corbet
845c0d1b6eeSJonathan Corbet#
846c0d1b6eeSJonathan Corbet# This could use some work; it's used to output the DOC: sections, and
847c0d1b6eeSJonathan Corbet# starts by putting out the name of the doc section itself, but that tends
848c0d1b6eeSJonathan Corbet# to duplicate a header already in the template file.
849c0d1b6eeSJonathan Corbet#
850c0d1b6eeSJonathan Corbetsub output_blockhead_rst(%) {
851c0d1b6eeSJonathan Corbet    my %args = %{$_[0]};
852c0d1b6eeSJonathan Corbet    my ($parameter, $section);
853c0d1b6eeSJonathan Corbet
854c0d1b6eeSJonathan Corbet    foreach $section (@{$args{'sectionlist'}}) {
855eab795ddSMauro Carvalho Chehab	next if (defined($nosymbol_table{$section}));
856eab795ddSMauro Carvalho Chehab
8579e72184bSJani Nikula	if ($output_selection != OUTPUT_INCLUDE) {
85806a755d6SMichal Wajdeczko	    print ".. _$section:\n\n";
859c0d1b6eeSJonathan Corbet	    print "**$section**\n\n";
8609e72184bSJani Nikula	}
8610b0f5f29SDaniel Vetter        print_lineno($section_start_lines{$section});
862c0d1b6eeSJonathan Corbet	output_highlight_rst($args{'sections'}{$section});
863c0d1b6eeSJonathan Corbet	print "\n";
864c0d1b6eeSJonathan Corbet    }
865c0d1b6eeSJonathan Corbet}
866c0d1b6eeSJonathan Corbet
867af250290SJonathan Corbet#
868af250290SJonathan Corbet# Apply the RST highlights to a sub-block of text.
869af250290SJonathan Corbet#
870af250290SJonathan Corbetsub highlight_block($) {
871af250290SJonathan Corbet    # The dohighlight kludge requires the text be called $contents
872af250290SJonathan Corbet    my $contents = shift;
873c0d1b6eeSJonathan Corbet    eval $dohighlight;
874c0d1b6eeSJonathan Corbet    die $@ if $@;
875af250290SJonathan Corbet    return $contents;
876af250290SJonathan Corbet}
877c0d1b6eeSJonathan Corbet
878af250290SJonathan Corbet#
879af250290SJonathan Corbet# Regexes used only here.
880af250290SJonathan Corbet#
881af250290SJonathan Corbetmy $sphinx_literal = '^[^.].*::$';
882af250290SJonathan Corbetmy $sphinx_cblock = '^\.\.\ +code-block::';
883af250290SJonathan Corbet
884af250290SJonathan Corbetsub output_highlight_rst {
885af250290SJonathan Corbet    my $input = join "\n",@_;
886af250290SJonathan Corbet    my $output = "";
887af250290SJonathan Corbet    my $line;
888af250290SJonathan Corbet    my $in_literal = 0;
889af250290SJonathan Corbet    my $litprefix;
890af250290SJonathan Corbet    my $block = "";
891af250290SJonathan Corbet
892af250290SJonathan Corbet    foreach $line (split "\n",$input) {
893af250290SJonathan Corbet	#
894af250290SJonathan Corbet	# If we're in a literal block, see if we should drop out
895af250290SJonathan Corbet	# of it.  Otherwise pass the line straight through unmunged.
896af250290SJonathan Corbet	#
897af250290SJonathan Corbet	if ($in_literal) {
898af250290SJonathan Corbet	    if (! ($line =~ /^\s*$/)) {
899af250290SJonathan Corbet		#
900af250290SJonathan Corbet		# If this is the first non-blank line in a literal
901af250290SJonathan Corbet		# block we need to figure out what the proper indent is.
902af250290SJonathan Corbet		#
903af250290SJonathan Corbet		if ($litprefix eq "") {
904af250290SJonathan Corbet		    $line =~ /^(\s*)/;
905af250290SJonathan Corbet		    $litprefix = '^' . $1;
906af250290SJonathan Corbet		    $output .= $line . "\n";
907af250290SJonathan Corbet		} elsif (! ($line =~ /$litprefix/)) {
908af250290SJonathan Corbet		    $in_literal = 0;
909af250290SJonathan Corbet		} else {
910af250290SJonathan Corbet		    $output .= $line . "\n";
911af250290SJonathan Corbet		}
912af250290SJonathan Corbet	    } else {
913af250290SJonathan Corbet		$output .= $line . "\n";
914af250290SJonathan Corbet	    }
915af250290SJonathan Corbet	}
916af250290SJonathan Corbet	#
917af250290SJonathan Corbet	# Not in a literal block (or just dropped out)
918af250290SJonathan Corbet	#
919af250290SJonathan Corbet	if (! $in_literal) {
920af250290SJonathan Corbet	    $block .= $line . "\n";
921af250290SJonathan Corbet	    if (($line =~ /$sphinx_literal/) || ($line =~ /$sphinx_cblock/)) {
922af250290SJonathan Corbet		$in_literal = 1;
923af250290SJonathan Corbet		$litprefix = "";
924af250290SJonathan Corbet		$output .= highlight_block($block);
925af250290SJonathan Corbet		$block = ""
926af250290SJonathan Corbet	    }
927af250290SJonathan Corbet	}
928af250290SJonathan Corbet    }
929af250290SJonathan Corbet
930af250290SJonathan Corbet    if ($block) {
931af250290SJonathan Corbet	$output .= highlight_block($block);
932af250290SJonathan Corbet    }
933af250290SJonathan Corbet    foreach $line (split "\n", $output) {
934830066a7SJani Nikula	print $lineprefix . $line . "\n";
935c0d1b6eeSJonathan Corbet    }
936c0d1b6eeSJonathan Corbet}
937c0d1b6eeSJonathan Corbet
938c0d1b6eeSJonathan Corbetsub output_function_rst(%) {
939c0d1b6eeSJonathan Corbet    my %args = %{$_[0]};
940c0d1b6eeSJonathan Corbet    my ($parameter, $section);
941c099ff69SJani Nikula    my $oldprefix = $lineprefix;
94282801d06SMauro Carvalho Chehab    my $start = "";
9436e9e4158SMauro Carvalho Chehab    my $is_macro = 0;
944c0d1b6eeSJonathan Corbet
945efa44475SMauro Carvalho Chehab    if ($sphinx_major < 3) {
946e3ad05feSMauro Carvalho Chehab	if ($args{'typedef'}) {
94782801d06SMauro Carvalho Chehab	    print ".. c:type:: ". $args{'function'} . "\n\n";
94882801d06SMauro Carvalho Chehab	    print_lineno($declaration_start_line);
94982801d06SMauro Carvalho Chehab	    print "   **Typedef**: ";
95082801d06SMauro Carvalho Chehab	    $lineprefix = "";
95182801d06SMauro Carvalho Chehab	    output_highlight_rst($args{'purpose'});
95282801d06SMauro Carvalho Chehab	    $start = "\n\n**Syntax**\n\n  ``";
9536e9e4158SMauro Carvalho Chehab	    $is_macro = 1;
954c0d1b6eeSJonathan Corbet	} else {
95582801d06SMauro Carvalho Chehab	    print ".. c:function:: ";
95682801d06SMauro Carvalho Chehab	}
957e3ad05feSMauro Carvalho Chehab    } else {
9586e9e4158SMauro Carvalho Chehab	if ($args{'typedef'} || $args{'functiontype'} eq "") {
9596e9e4158SMauro Carvalho Chehab	    $is_macro = 1;
960e3ad05feSMauro Carvalho Chehab	    print ".. c:macro:: ". $args{'function'} . "\n\n";
9616e9e4158SMauro Carvalho Chehab	} else {
9626e9e4158SMauro Carvalho Chehab	    print ".. c:function:: ";
9636e9e4158SMauro Carvalho Chehab	}
964e3ad05feSMauro Carvalho Chehab
965e3ad05feSMauro Carvalho Chehab	if ($args{'typedef'}) {
966e3ad05feSMauro Carvalho Chehab	    print_lineno($declaration_start_line);
967e3ad05feSMauro Carvalho Chehab	    print "   **Typedef**: ";
968e3ad05feSMauro Carvalho Chehab	    $lineprefix = "";
969e3ad05feSMauro Carvalho Chehab	    output_highlight_rst($args{'purpose'});
970e3ad05feSMauro Carvalho Chehab	    $start = "\n\n**Syntax**\n\n  ``";
971e3ad05feSMauro Carvalho Chehab	} else {
9726e9e4158SMauro Carvalho Chehab	    print "``" if ($is_macro);
973e3ad05feSMauro Carvalho Chehab	}
974e3ad05feSMauro Carvalho Chehab    }
97582801d06SMauro Carvalho Chehab    if ($args{'functiontype'} ne "") {
97682801d06SMauro Carvalho Chehab	$start .= $args{'functiontype'} . " " . $args{'function'} . " (";
97782801d06SMauro Carvalho Chehab    } else {
97882801d06SMauro Carvalho Chehab	$start .= $args{'function'} . " (";
979c0d1b6eeSJonathan Corbet    }
980c0d1b6eeSJonathan Corbet    print $start;
981c0d1b6eeSJonathan Corbet
982c0d1b6eeSJonathan Corbet    my $count = 0;
983c0d1b6eeSJonathan Corbet    foreach my $parameter (@{$args{'parameterlist'}}) {
984c0d1b6eeSJonathan Corbet	if ($count ne 0) {
985c0d1b6eeSJonathan Corbet	    print ", ";
986c0d1b6eeSJonathan Corbet	}
987c0d1b6eeSJonathan Corbet	$count++;
988c0d1b6eeSJonathan Corbet	$type = $args{'parametertypes'}{$parameter};
989a88b1672SMauro Carvalho Chehab
990e86bdb24SAditya Srivastava	if ($type =~ m/$function_pointer/) {
991c0d1b6eeSJonathan Corbet	    # pointer-to-function
992e8f4ba83SPeter Maydell	    print $1 . $parameter . ") (" . $2 . ")";
993c0d1b6eeSJonathan Corbet	} else {
994ed8348e2SMauro Carvalho Chehab	    print $type;
995c0d1b6eeSJonathan Corbet	}
996c0d1b6eeSJonathan Corbet    }
9976e9e4158SMauro Carvalho Chehab    if ($is_macro) {
9986e9e4158SMauro Carvalho Chehab	print ")``\n\n";
99982801d06SMauro Carvalho Chehab    } else {
1000c099ff69SJani Nikula	print ")\n\n";
1001e3ad05feSMauro Carvalho Chehab    }
10026e9e4158SMauro Carvalho Chehab    if (!$args{'typedef'}) {
10030b0f5f29SDaniel Vetter	print_lineno($declaration_start_line);
1004c099ff69SJani Nikula	$lineprefix = "   ";
1005c099ff69SJani Nikula	output_highlight_rst($args{'purpose'});
1006c099ff69SJani Nikula	print "\n";
100782801d06SMauro Carvalho Chehab    }
1008c0d1b6eeSJonathan Corbet
1009ecbcfba1SJani Nikula    print "**Parameters**\n\n";
1010c099ff69SJani Nikula    $lineprefix = "  ";
1011c0d1b6eeSJonathan Corbet    foreach $parameter (@{$args{'parameterlist'}}) {
1012c0d1b6eeSJonathan Corbet	my $parameter_name = $parameter;
1013ada5f446SGabriel Krisman Bertazi	$parameter_name =~ s/\[.*//;
1014c0d1b6eeSJonathan Corbet	$type = $args{'parametertypes'}{$parameter};
1015c0d1b6eeSJonathan Corbet
1016c0d1b6eeSJonathan Corbet	if ($type ne "") {
1017ed8348e2SMauro Carvalho Chehab	    print "``$type``\n";
1018c0d1b6eeSJonathan Corbet	} else {
1019c0d1b6eeSJonathan Corbet	    print "``$parameter``\n";
1020c0d1b6eeSJonathan Corbet	}
10210b0f5f29SDaniel Vetter
10220b0f5f29SDaniel Vetter        print_lineno($parameterdesc_start_lines{$parameter_name});
10230b0f5f29SDaniel Vetter
10245e64fa9cSJani Nikula	if (defined($args{'parameterdescs'}{$parameter_name}) &&
10255e64fa9cSJani Nikula	    $args{'parameterdescs'}{$parameter_name} ne $undescribed) {
1026c0d1b6eeSJonathan Corbet	    output_highlight_rst($args{'parameterdescs'}{$parameter_name});
1027c0d1b6eeSJonathan Corbet	} else {
1028d4b08e0cSJani Nikula	    print "  *undescribed*\n";
1029c0d1b6eeSJonathan Corbet	}
1030c0d1b6eeSJonathan Corbet	print "\n";
1031c0d1b6eeSJonathan Corbet    }
1032c099ff69SJani Nikula
1033c099ff69SJani Nikula    $lineprefix = $oldprefix;
1034c0d1b6eeSJonathan Corbet    output_section_rst(@_);
1035c0d1b6eeSJonathan Corbet}
1036c0d1b6eeSJonathan Corbet
1037c0d1b6eeSJonathan Corbetsub output_section_rst(%) {
1038c0d1b6eeSJonathan Corbet    my %args = %{$_[0]};
1039c0d1b6eeSJonathan Corbet    my $section;
1040c0d1b6eeSJonathan Corbet    my $oldprefix = $lineprefix;
1041c0d1b6eeSJonathan Corbet    $lineprefix = "";
1042c0d1b6eeSJonathan Corbet
1043c0d1b6eeSJonathan Corbet    foreach $section (@{$args{'sectionlist'}}) {
1044ecbcfba1SJani Nikula	print "**$section**\n\n";
10450b0f5f29SDaniel Vetter        print_lineno($section_start_lines{$section});
1046c0d1b6eeSJonathan Corbet	output_highlight_rst($args{'sections'}{$section});
1047c0d1b6eeSJonathan Corbet	print "\n";
1048c0d1b6eeSJonathan Corbet    }
1049c0d1b6eeSJonathan Corbet    print "\n";
1050c0d1b6eeSJonathan Corbet    $lineprefix = $oldprefix;
1051c0d1b6eeSJonathan Corbet}
1052c0d1b6eeSJonathan Corbet
1053c0d1b6eeSJonathan Corbetsub output_enum_rst(%) {
1054c0d1b6eeSJonathan Corbet    my %args = %{$_[0]};
1055c0d1b6eeSJonathan Corbet    my ($parameter);
1056c099ff69SJani Nikula    my $oldprefix = $lineprefix;
1057c0d1b6eeSJonathan Corbet    my $count;
105862850976SJani Nikula
1059efa44475SMauro Carvalho Chehab    if ($sphinx_major < 3) {
1060efa44475SMauro Carvalho Chehab	my $name = "enum " . $args{'enum'};
106162850976SJani Nikula	print "\n\n.. c:type:: " . $name . "\n\n";
1062efa44475SMauro Carvalho Chehab    } else {
1063efa44475SMauro Carvalho Chehab	my $name = $args{'enum'};
1064efa44475SMauro Carvalho Chehab	print "\n\n.. c:enum:: " . $name . "\n\n";
1065efa44475SMauro Carvalho Chehab    }
10660b0f5f29SDaniel Vetter    print_lineno($declaration_start_line);
1067c099ff69SJani Nikula    $lineprefix = "   ";
1068c099ff69SJani Nikula    output_highlight_rst($args{'purpose'});
1069c099ff69SJani Nikula    print "\n";
1070c0d1b6eeSJonathan Corbet
1071ecbcfba1SJani Nikula    print "**Constants**\n\n";
1072c0d1b6eeSJonathan Corbet    $lineprefix = "  ";
1073c0d1b6eeSJonathan Corbet    foreach $parameter (@{$args{'parameterlist'}}) {
1074ecbcfba1SJani Nikula	print "``$parameter``\n";
1075c0d1b6eeSJonathan Corbet	if ($args{'parameterdescs'}{$parameter} ne $undescribed) {
1076c0d1b6eeSJonathan Corbet	    output_highlight_rst($args{'parameterdescs'}{$parameter});
1077c0d1b6eeSJonathan Corbet	} else {
1078d4b08e0cSJani Nikula	    print "  *undescribed*\n";
1079c0d1b6eeSJonathan Corbet	}
1080c0d1b6eeSJonathan Corbet	print "\n";
1081c0d1b6eeSJonathan Corbet    }
1082c099ff69SJani Nikula
1083c0d1b6eeSJonathan Corbet    $lineprefix = $oldprefix;
1084c0d1b6eeSJonathan Corbet    output_section_rst(@_);
1085c0d1b6eeSJonathan Corbet}
1086c0d1b6eeSJonathan Corbet
1087c0d1b6eeSJonathan Corbetsub output_typedef_rst(%) {
1088c0d1b6eeSJonathan Corbet    my %args = %{$_[0]};
1089c0d1b6eeSJonathan Corbet    my ($parameter);
1090c099ff69SJani Nikula    my $oldprefix = $lineprefix;
1091efa44475SMauro Carvalho Chehab    my $name;
1092c0d1b6eeSJonathan Corbet
1093efa44475SMauro Carvalho Chehab    if ($sphinx_major < 3) {
1094efa44475SMauro Carvalho Chehab	$name = "typedef " . $args{'typedef'};
1095efa44475SMauro Carvalho Chehab    } else {
1096efa44475SMauro Carvalho Chehab	$name = $args{'typedef'};
1097efa44475SMauro Carvalho Chehab    }
109862850976SJani Nikula    print "\n\n.. c:type:: " . $name . "\n\n";
10990b0f5f29SDaniel Vetter    print_lineno($declaration_start_line);
1100c099ff69SJani Nikula    $lineprefix = "   ";
1101c099ff69SJani Nikula    output_highlight_rst($args{'purpose'});
1102c099ff69SJani Nikula    print "\n";
1103c0d1b6eeSJonathan Corbet
1104c099ff69SJani Nikula    $lineprefix = $oldprefix;
1105c0d1b6eeSJonathan Corbet    output_section_rst(@_);
1106c0d1b6eeSJonathan Corbet}
1107c0d1b6eeSJonathan Corbet
1108c0d1b6eeSJonathan Corbetsub output_struct_rst(%) {
1109c0d1b6eeSJonathan Corbet    my %args = %{$_[0]};
1110c0d1b6eeSJonathan Corbet    my ($parameter);
1111c099ff69SJani Nikula    my $oldprefix = $lineprefix;
1112c0d1b6eeSJonathan Corbet
1113efa44475SMauro Carvalho Chehab    if ($sphinx_major < 3) {
1114efa44475SMauro Carvalho Chehab	my $name = $args{'type'} . " " . $args{'struct'};
111562850976SJani Nikula	print "\n\n.. c:type:: " . $name . "\n\n";
1116efa44475SMauro Carvalho Chehab    } else {
1117efa44475SMauro Carvalho Chehab	my $name = $args{'struct'};
111872b97d0bSMauro Carvalho Chehab	if ($args{'type'} eq 'union') {
111972b97d0bSMauro Carvalho Chehab	    print "\n\n.. c:union:: " . $name . "\n\n";
112072b97d0bSMauro Carvalho Chehab	} else {
1121efa44475SMauro Carvalho Chehab	    print "\n\n.. c:struct:: " . $name . "\n\n";
1122efa44475SMauro Carvalho Chehab	}
112372b97d0bSMauro Carvalho Chehab    }
11240b0f5f29SDaniel Vetter    print_lineno($declaration_start_line);
1125c099ff69SJani Nikula    $lineprefix = "   ";
1126c099ff69SJani Nikula    output_highlight_rst($args{'purpose'});
1127c099ff69SJani Nikula    print "\n";
1128c0d1b6eeSJonathan Corbet
1129ecbcfba1SJani Nikula    print "**Definition**\n\n";
1130c0d1b6eeSJonathan Corbet    print "::\n\n";
11318ad72163SMauro Carvalho Chehab    my $declaration = $args{'definition'};
11328ad72163SMauro Carvalho Chehab    $declaration =~ s/\t/  /g;
11338ad72163SMauro Carvalho Chehab    print "  " . $args{'type'} . " " . $args{'struct'} . " {\n$declaration  };\n\n";
1134c0d1b6eeSJonathan Corbet
1135ecbcfba1SJani Nikula    print "**Members**\n\n";
1136c099ff69SJani Nikula    $lineprefix = "  ";
1137c0d1b6eeSJonathan Corbet    foreach $parameter (@{$args{'parameterlist'}}) {
1138c0d1b6eeSJonathan Corbet	($parameter =~ /^#/) && next;
1139c0d1b6eeSJonathan Corbet
1140c0d1b6eeSJonathan Corbet	my $parameter_name = $parameter;
1141c0d1b6eeSJonathan Corbet	$parameter_name =~ s/\[.*//;
1142c0d1b6eeSJonathan Corbet
1143c0d1b6eeSJonathan Corbet	($args{'parameterdescs'}{$parameter_name} ne $undescribed) || next;
1144c0d1b6eeSJonathan Corbet	$type = $args{'parametertypes'}{$parameter};
11450b0f5f29SDaniel Vetter        print_lineno($parameterdesc_start_lines{$parameter_name});
11466d232c80SMauro Carvalho Chehab	print "``" . $parameter . "``\n";
1147c0d1b6eeSJonathan Corbet	output_highlight_rst($args{'parameterdescs'}{$parameter_name});
1148c0d1b6eeSJonathan Corbet	print "\n";
1149c0d1b6eeSJonathan Corbet    }
1150c0d1b6eeSJonathan Corbet    print "\n";
1151c099ff69SJani Nikula
1152c099ff69SJani Nikula    $lineprefix = $oldprefix;
1153c0d1b6eeSJonathan Corbet    output_section_rst(@_);
1154c0d1b6eeSJonathan Corbet}
1155c0d1b6eeSJonathan Corbet
11563a025e1dSMatthew Wilcox## none mode output functions
11573a025e1dSMatthew Wilcox
11583a025e1dSMatthew Wilcoxsub output_function_none(%) {
11593a025e1dSMatthew Wilcox}
11603a025e1dSMatthew Wilcox
11613a025e1dSMatthew Wilcoxsub output_enum_none(%) {
11623a025e1dSMatthew Wilcox}
11633a025e1dSMatthew Wilcox
11643a025e1dSMatthew Wilcoxsub output_typedef_none(%) {
11653a025e1dSMatthew Wilcox}
11663a025e1dSMatthew Wilcox
11673a025e1dSMatthew Wilcoxsub output_struct_none(%) {
11683a025e1dSMatthew Wilcox}
11693a025e1dSMatthew Wilcox
11703a025e1dSMatthew Wilcoxsub output_blockhead_none(%) {
11713a025e1dSMatthew Wilcox}
11723a025e1dSMatthew Wilcox
11731da177e4SLinus Torvalds##
117427205744SRandy Dunlap# generic output function for all types (function, struct/union, typedef, enum);
117527205744SRandy Dunlap# calls the generated, variable output_ function name based on
117627205744SRandy Dunlap# functype and output_mode
11771da177e4SLinus Torvaldssub output_declaration {
11781da177e4SLinus Torvalds    no strict 'refs';
11791da177e4SLinus Torvalds    my $name = shift;
11801da177e4SLinus Torvalds    my $functype = shift;
11811da177e4SLinus Torvalds    my $func = "output_${functype}_$output_mode";
1182eab795ddSMauro Carvalho Chehab
1183eab795ddSMauro Carvalho Chehab    return if (defined($nosymbol_table{$name}));
1184eab795ddSMauro Carvalho Chehab
1185b6c3f456SJani Nikula    if (($output_selection == OUTPUT_ALL) ||
1186b6c3f456SJani Nikula	(($output_selection == OUTPUT_INCLUDE ||
1187b6c3f456SJani Nikula	  $output_selection == OUTPUT_EXPORTED) &&
118886ae2e38SJani Nikula	 defined($function_table{$name})) ||
1189eab795ddSMauro Carvalho Chehab	($output_selection == OUTPUT_INTERNAL &&
119086ae2e38SJani Nikula	 !($functype eq "function" && defined($function_table{$name}))))
11911da177e4SLinus Torvalds    {
11921da177e4SLinus Torvalds	&$func(@_);
11931da177e4SLinus Torvalds	$section_counter++;
11941da177e4SLinus Torvalds    }
11951da177e4SLinus Torvalds}
11961da177e4SLinus Torvalds
11971da177e4SLinus Torvalds##
119827205744SRandy Dunlap# generic output function - calls the right one based on current output mode.
1199b112e0f7SJohannes Bergsub output_blockhead {
12001da177e4SLinus Torvalds    no strict 'refs';
1201b112e0f7SJohannes Berg    my $func = "output_blockhead_" . $output_mode;
12021da177e4SLinus Torvalds    &$func(@_);
12031da177e4SLinus Torvalds    $section_counter++;
12041da177e4SLinus Torvalds}
12051da177e4SLinus Torvalds
12061da177e4SLinus Torvalds##
12071da177e4SLinus Torvalds# takes a declaration (struct, union, enum, typedef) and
12081da177e4SLinus Torvalds# invokes the right handler. NOT called for functions.
12091da177e4SLinus Torvaldssub dump_declaration($$) {
12101da177e4SLinus Torvalds    no strict 'refs';
12111da177e4SLinus Torvalds    my ($prototype, $file) = @_;
12121da177e4SLinus Torvalds    my $func = "dump_" . $decl_type;
12131da177e4SLinus Torvalds    &$func(@_);
12141da177e4SLinus Torvalds}
12151da177e4SLinus Torvalds
12161da177e4SLinus Torvaldssub dump_union($$) {
12171da177e4SLinus Torvalds    dump_struct(@_);
12181da177e4SLinus Torvalds}
12191da177e4SLinus Torvalds
12201da177e4SLinus Torvaldssub dump_struct($$) {
12211da177e4SLinus Torvalds    my $x = shift;
12221da177e4SLinus Torvalds    my $file = shift;
1223a746fe32SAditya Srivastava    my $decl_type;
1224a746fe32SAditya Srivastava    my $members;
1225a746fe32SAditya Srivastava    my $type = qr{struct|union};
1226a746fe32SAditya Srivastava    # For capturing struct/union definition body, i.e. "{members*}qualifiers*"
1227e86bdb24SAditya Srivastava    my $qualifiers = qr{$attribute|__packed|__aligned|____cacheline_aligned_in_smp|____cacheline_aligned};
1228e86bdb24SAditya Srivastava    my $definition_body = qr{\{(.*)\}\s*$qualifiers*};
1229e86bdb24SAditya Srivastava    my $struct_members = qr{($type)([^\{\};]+)\{([^\{\}]*)\}([^\{\}\;]*)\;};
12301da177e4SLinus Torvalds
1231a746fe32SAditya Srivastava    if ($x =~ /($type)\s+(\w+)\s*$definition_body/) {
1232a746fe32SAditya Srivastava	$decl_type = $1;
12331da177e4SLinus Torvalds	$declaration_name = $2;
1234a746fe32SAditya Srivastava	$members = $3;
1235a746fe32SAditya Srivastava    } elsif ($x =~ /typedef\s+($type)\s*$definition_body\s*(\w+)\s*;/) {
1236a746fe32SAditya Srivastava	$decl_type = $1;
1237a746fe32SAditya Srivastava	$declaration_name = $3;
1238a746fe32SAditya Srivastava	$members = $2;
1239a746fe32SAditya Srivastava    }
12401da177e4SLinus Torvalds
1241a746fe32SAditya Srivastava    if ($members) {
124252042e2dSMauro Carvalho Chehab	if ($identifier ne $declaration_name) {
124352042e2dSMauro Carvalho Chehab	    print STDERR "${file}:$.: warning: expecting prototype for $decl_type $identifier. Prototype was for $decl_type $declaration_name instead\n";
124452042e2dSMauro Carvalho Chehab	    return;
124552042e2dSMauro Carvalho Chehab	}
124652042e2dSMauro Carvalho Chehab
1247aeec46b9SMartin Waitz	# ignore members marked private:
12480d8c39e6SMauro Carvalho Chehab	$members =~ s/\/\*\s*private:.*?\/\*\s*public:.*?\*\///gosi;
12490d8c39e6SMauro Carvalho Chehab	$members =~ s/\/\*\s*private:.*//gosi;
1250aeec46b9SMartin Waitz	# strip comments:
1251aeec46b9SMartin Waitz	$members =~ s/\/\*.*?\*\///gos;
1252ef5da59fSRandy Dunlap	# strip attributes
1253e86bdb24SAditya Srivastava	$members =~ s/\s*$attribute/ /gi;
12543d9bfb19SSakari Ailus	$members =~ s/\s*__aligned\s*\([^;]*\)/ /gos;
12553d9bfb19SSakari Ailus	$members =~ s/\s*__packed\s*/ /gos;
1256f0074929SJonathan Corbet	$members =~ s/\s*CRYPTO_MINALIGN_ATTR/ /gos;
1257f861537dSAndré Almeida	$members =~ s/\s*____cacheline_aligned_in_smp/ /gos;
1258a070991fSJonathan Cameron	$members =~ s/\s*____cacheline_aligned/ /gos;
125950d7bd38SKees Cook	# unwrap struct_group():
126050d7bd38SKees Cook	# - first eat non-declaration parameters and rewrite for final match
126150d7bd38SKees Cook	# - then remove macro, outer parens, and trailing semicolon
126250d7bd38SKees Cook	$members =~ s/\bstruct_group\s*\(([^,]*,)/STRUCT_GROUP(/gos;
126350d7bd38SKees Cook	$members =~ s/\bstruct_group_(attr|tagged)\s*\(([^,]*,){2}/STRUCT_GROUP(/gos;
126450d7bd38SKees Cook	$members =~ s/\b__struct_group\s*\(([^,]*,){3}/STRUCT_GROUP(/gos;
126550d7bd38SKees Cook	$members =~ s/\bSTRUCT_GROUP(\(((?:(?>[^)(]+)|(?1))*)\))[^;]*;/$2/gos;
12663556108eSMauro Carvalho Chehab
1267e86bdb24SAditya Srivastava	my $args = qr{([^,)]+)};
1268b22b5a9eSConchúr Navid	# replace DECLARE_BITMAP
12693556108eSMauro Carvalho Chehab	$members =~ s/__ETHTOOL_DECLARE_LINK_MODE_MASK\s*\(([^\)]+)\)/DECLARE_BITMAP($1, __ETHTOOL_LINK_MODE_MASK_NBITS)/gos;
1270603bdf5dSRandy Dunlap	$members =~ s/DECLARE_PHY_INTERFACE_MASK\s*\(([^\)]+)\)/DECLARE_BITMAP($1, PHY_INTERFACE_MODE_MAX)/gos;
1271e86bdb24SAditya Srivastava	$members =~ s/DECLARE_BITMAP\s*\($args,\s*$args\)/unsigned long $1\[BITS_TO_LONGS($2)\]/gos;
12721cb566baSJakub Kicinski	# replace DECLARE_HASHTABLE
1273e86bdb24SAditya Srivastava	$members =~ s/DECLARE_HASHTABLE\s*\($args,\s*$args\)/unsigned long $1\[1 << (($2) - 1)\]/gos;
127445005b27SMauro Carvalho Chehab	# replace DECLARE_KFIFO
1275e86bdb24SAditya Srivastava	$members =~ s/DECLARE_KFIFO\s*\($args,\s*$args,\s*$args\)/$2 \*$1/gos;
127645005b27SMauro Carvalho Chehab	# replace DECLARE_KFIFO_PTR
1277e86bdb24SAditya Srivastava	$members =~ s/DECLARE_KFIFO_PTR\s*\($args,\s*$args\)/$2 \*$1/gos;
12783080ea55SKees Cook	# replace DECLARE_FLEX_ARRAY
12793080ea55SKees Cook	$members =~ s/(?:__)?DECLARE_FLEX_ARRAY\s*\($args,\s*$args\)/$1 $2\[\]/gos;
12808ad72163SMauro Carvalho Chehab	my $declaration = $members;
12818ad72163SMauro Carvalho Chehab
12828ad72163SMauro Carvalho Chehab	# Split nested struct/union elements as newer ones
1283e86bdb24SAditya Srivastava	while ($members =~ m/$struct_members/) {
128484ce5b98SMauro Carvalho Chehab		my $newmember;
128584ce5b98SMauro Carvalho Chehab		my $maintype = $1;
128684ce5b98SMauro Carvalho Chehab		my $ids = $4;
12878ad72163SMauro Carvalho Chehab		my $content = $3;
128884ce5b98SMauro Carvalho Chehab		foreach my $id(split /,/, $ids) {
128984ce5b98SMauro Carvalho Chehab			$newmember .= "$maintype $id; ";
129084ce5b98SMauro Carvalho Chehab
12918ad72163SMauro Carvalho Chehab			$id =~ s/[:\[].*//;
129284ce5b98SMauro Carvalho Chehab			$id =~ s/^\s*\**(\S+)\s*/$1/;
12938ad72163SMauro Carvalho Chehab			foreach my $arg (split /;/, $content) {
12948ad72163SMauro Carvalho Chehab				next if ($arg =~ m/^\s*$/);
12957c0d7e87SMauro Carvalho Chehab				if ($arg =~ m/^([^\(]+\(\*?\s*)([\w\.]*)(\s*\).*)/) {
12967c0d7e87SMauro Carvalho Chehab					# pointer-to-function
12977c0d7e87SMauro Carvalho Chehab					my $type = $1;
12987c0d7e87SMauro Carvalho Chehab					my $name = $2;
12997c0d7e87SMauro Carvalho Chehab					my $extra = $3;
13007c0d7e87SMauro Carvalho Chehab					next if (!$name);
13017c0d7e87SMauro Carvalho Chehab					if ($id =~ m/^\s*$/) {
13027c0d7e87SMauro Carvalho Chehab						# anonymous struct/union
13037c0d7e87SMauro Carvalho Chehab						$newmember .= "$type$name$extra; ";
13047c0d7e87SMauro Carvalho Chehab					} else {
13057c0d7e87SMauro Carvalho Chehab						$newmember .= "$type$id.$name$extra; ";
13067c0d7e87SMauro Carvalho Chehab					}
13077c0d7e87SMauro Carvalho Chehab				} else {
130884ce5b98SMauro Carvalho Chehab					my $type;
130984ce5b98SMauro Carvalho Chehab					my $names;
131084ce5b98SMauro Carvalho Chehab					$arg =~ s/^\s+//;
131184ce5b98SMauro Carvalho Chehab					$arg =~ s/\s+$//;
131284ce5b98SMauro Carvalho Chehab					# Handle bitmaps
131384ce5b98SMauro Carvalho Chehab					$arg =~ s/:\s*\d+\s*//g;
131484ce5b98SMauro Carvalho Chehab					# Handle arrays
1315d404d579SMauro Carvalho Chehab					$arg =~ s/\[.*\]//g;
131684ce5b98SMauro Carvalho Chehab					# The type may have multiple words,
131784ce5b98SMauro Carvalho Chehab					# and multiple IDs can be defined, like:
131884ce5b98SMauro Carvalho Chehab					#	const struct foo, *bar, foobar
131984ce5b98SMauro Carvalho Chehab					# So, we remove spaces when parsing the
132084ce5b98SMauro Carvalho Chehab					# names, in order to match just names
132184ce5b98SMauro Carvalho Chehab					# and commas for the names
132284ce5b98SMauro Carvalho Chehab					$arg =~ s/\s*,\s*/,/g;
132384ce5b98SMauro Carvalho Chehab					if ($arg =~ m/(.*)\s+([\S+,]+)/) {
132484ce5b98SMauro Carvalho Chehab						$type = $1;
132584ce5b98SMauro Carvalho Chehab						$names = $2;
132684ce5b98SMauro Carvalho Chehab					} else {
132784ce5b98SMauro Carvalho Chehab						$newmember .= "$arg; ";
132884ce5b98SMauro Carvalho Chehab						next;
132984ce5b98SMauro Carvalho Chehab					}
133084ce5b98SMauro Carvalho Chehab					foreach my $name (split /,/, $names) {
133184ce5b98SMauro Carvalho Chehab						$name =~ s/^\s*\**(\S+)\s*/$1/;
13328ad72163SMauro Carvalho Chehab						next if (($name =~ m/^\s*$/));
13338ad72163SMauro Carvalho Chehab						if ($id =~ m/^\s*$/) {
13348ad72163SMauro Carvalho Chehab							# anonymous struct/union
13358ad72163SMauro Carvalho Chehab							$newmember .= "$type $name; ";
13368ad72163SMauro Carvalho Chehab						} else {
13378ad72163SMauro Carvalho Chehab							$newmember .= "$type $id.$name; ";
13388ad72163SMauro Carvalho Chehab						}
13398ad72163SMauro Carvalho Chehab					}
13407c0d7e87SMauro Carvalho Chehab				}
134184ce5b98SMauro Carvalho Chehab			}
134284ce5b98SMauro Carvalho Chehab		}
1343e86bdb24SAditya Srivastava		$members =~ s/$struct_members/$newmember/;
134484ce5b98SMauro Carvalho Chehab	}
13458ad72163SMauro Carvalho Chehab
13468ad72163SMauro Carvalho Chehab	# Ignore other nested elements, like enums
1347673bb2dfSBen Hutchings	$members =~ s/(\{[^\{\}]*\})//g;
13488ad72163SMauro Carvalho Chehab
1349151c468bSMauro Carvalho Chehab	create_parameterlist($members, ';', $file, $declaration_name);
13501081de2dSMauro Carvalho Chehab	check_sections($file, $declaration_name, $decl_type, $sectcheck, $struct_actual);
13511da177e4SLinus Torvalds
13528ad72163SMauro Carvalho Chehab	# Adjust declaration for better display
1353673bb2dfSBen Hutchings	$declaration =~ s/([\{;])/$1\n/g;
1354673bb2dfSBen Hutchings	$declaration =~ s/\}\s+;/};/g;
13558ad72163SMauro Carvalho Chehab	# Better handle inlined enums
1356673bb2dfSBen Hutchings	do {} while ($declaration =~ s/(enum\s+\{[^\}]+),([^\n])/$1,\n$2/);
13578ad72163SMauro Carvalho Chehab
13588ad72163SMauro Carvalho Chehab	my @def_args = split /\n/, $declaration;
13598ad72163SMauro Carvalho Chehab	my $level = 1;
13608ad72163SMauro Carvalho Chehab	$declaration = "";
13618ad72163SMauro Carvalho Chehab	foreach my $clause (@def_args) {
13628ad72163SMauro Carvalho Chehab		$clause =~ s/^\s+//;
13638ad72163SMauro Carvalho Chehab		$clause =~ s/\s+$//;
13648ad72163SMauro Carvalho Chehab		$clause =~ s/\s+/ /;
13658ad72163SMauro Carvalho Chehab		next if (!$clause);
1366673bb2dfSBen Hutchings		$level-- if ($clause =~ m/(\})/ && $level > 1);
13678ad72163SMauro Carvalho Chehab		if (!($clause =~ m/^\s*#/)) {
13688ad72163SMauro Carvalho Chehab			$declaration .= "\t" x $level;
13698ad72163SMauro Carvalho Chehab		}
13708ad72163SMauro Carvalho Chehab		$declaration .= "\t" . $clause . "\n";
1371673bb2dfSBen Hutchings		$level++ if ($clause =~ m/(\{)/ && !($clause =~m/\}/));
13728ad72163SMauro Carvalho Chehab	}
13731da177e4SLinus Torvalds	output_declaration($declaration_name,
13741da177e4SLinus Torvalds			   'struct',
13751da177e4SLinus Torvalds			   {'struct' => $declaration_name,
13761da177e4SLinus Torvalds			    'module' => $modulename,
13778ad72163SMauro Carvalho Chehab			    'definition' => $declaration,
13781da177e4SLinus Torvalds			    'parameterlist' => \@parameterlist,
13791da177e4SLinus Torvalds			    'parameterdescs' => \%parameterdescs,
13801da177e4SLinus Torvalds			    'parametertypes' => \%parametertypes,
13811da177e4SLinus Torvalds			    'sectionlist' => \@sectionlist,
13821da177e4SLinus Torvalds			    'sections' => \%sections,
13831da177e4SLinus Torvalds			    'purpose' => $declaration_purpose,
13841da177e4SLinus Torvalds			    'type' => $decl_type
13851da177e4SLinus Torvalds			   });
13861da177e4SLinus Torvalds    }
13871da177e4SLinus Torvalds    else {
1388d40e1e65SBart Van Assche	print STDERR "${file}:$.: error: Cannot parse struct or union!\n";
13891da177e4SLinus Torvalds	++$errors;
13901da177e4SLinus Torvalds    }
13911da177e4SLinus Torvalds}
13921da177e4SLinus Torvalds
139385afe608SMauro Carvalho Chehab
139485afe608SMauro Carvalho Chehabsub show_warnings($$) {
139585afe608SMauro Carvalho Chehab	my $functype = shift;
139685afe608SMauro Carvalho Chehab	my $name = shift;
139785afe608SMauro Carvalho Chehab
1398eab795ddSMauro Carvalho Chehab	return 0 if (defined($nosymbol_table{$name}));
1399eab795ddSMauro Carvalho Chehab
140085afe608SMauro Carvalho Chehab	return 1 if ($output_selection == OUTPUT_ALL);
140185afe608SMauro Carvalho Chehab
140285afe608SMauro Carvalho Chehab	if ($output_selection == OUTPUT_EXPORTED) {
140385afe608SMauro Carvalho Chehab		if (defined($function_table{$name})) {
140485afe608SMauro Carvalho Chehab			return 1;
140585afe608SMauro Carvalho Chehab		} else {
140685afe608SMauro Carvalho Chehab			return 0;
140785afe608SMauro Carvalho Chehab		}
140885afe608SMauro Carvalho Chehab	}
140985afe608SMauro Carvalho Chehab        if ($output_selection == OUTPUT_INTERNAL) {
141085afe608SMauro Carvalho Chehab		if (!($functype eq "function" && defined($function_table{$name}))) {
141185afe608SMauro Carvalho Chehab			return 1;
141285afe608SMauro Carvalho Chehab		} else {
141385afe608SMauro Carvalho Chehab			return 0;
141485afe608SMauro Carvalho Chehab		}
141585afe608SMauro Carvalho Chehab	}
141685afe608SMauro Carvalho Chehab	if ($output_selection == OUTPUT_INCLUDE) {
141785afe608SMauro Carvalho Chehab		if (defined($function_table{$name})) {
141885afe608SMauro Carvalho Chehab			return 1;
141985afe608SMauro Carvalho Chehab		} else {
142085afe608SMauro Carvalho Chehab			return 0;
142185afe608SMauro Carvalho Chehab		}
142285afe608SMauro Carvalho Chehab	}
142385afe608SMauro Carvalho Chehab	die("Please add the new output type at show_warnings()");
142485afe608SMauro Carvalho Chehab}
142585afe608SMauro Carvalho Chehab
14261da177e4SLinus Torvaldssub dump_enum($$) {
14271da177e4SLinus Torvalds    my $x = shift;
14281da177e4SLinus Torvalds    my $file = shift;
1429d38c8cfbSMauro Carvalho Chehab    my $members;
1430d38c8cfbSMauro Carvalho Chehab
14311da177e4SLinus Torvalds
1432aeec46b9SMartin Waitz    $x =~ s@/\*.*?\*/@@gos;	# strip comments.
14334468e21eSConchúr Navid    # strip #define macros inside enums
14344468e21eSConchúr Navid    $x =~ s@#\s*((define|ifdef)\s+|endif)[^;]*;@@gos;
1435b6d676dbSRandy Dunlap
1436d38c8cfbSMauro Carvalho Chehab    if ($x =~ /typedef\s+enum\s*\{(.*)\}\s*(\w*)\s*;/) {
1437d38c8cfbSMauro Carvalho Chehab	$declaration_name = $2;
1438d38c8cfbSMauro Carvalho Chehab	$members = $1;
1439d38c8cfbSMauro Carvalho Chehab    } elsif ($x =~ /enum\s+(\w*)\s*\{(.*)\}/) {
14401da177e4SLinus Torvalds	$declaration_name = $1;
1441d38c8cfbSMauro Carvalho Chehab	$members = $2;
1442d38c8cfbSMauro Carvalho Chehab    }
1443d38c8cfbSMauro Carvalho Chehab
1444ae5b17e4SAndy Shevchenko    if ($members) {
144552042e2dSMauro Carvalho Chehab	if ($identifier ne $declaration_name) {
14460b54c2e3SMauro Carvalho Chehab	    if ($identifier eq "") {
14470b54c2e3SMauro Carvalho Chehab		print STDERR "${file}:$.: warning: wrong kernel-doc identifier on line:\n";
14480b54c2e3SMauro Carvalho Chehab	    } else {
144952042e2dSMauro Carvalho Chehab		print STDERR "${file}:$.: warning: expecting prototype for enum $identifier. Prototype was for enum $declaration_name instead\n";
14500b54c2e3SMauro Carvalho Chehab	    }
145152042e2dSMauro Carvalho Chehab	    return;
145252042e2dSMauro Carvalho Chehab	}
14530b54c2e3SMauro Carvalho Chehab	$declaration_name = "(anonymous)" if ($declaration_name eq "");
145452042e2dSMauro Carvalho Chehab
14555cb5c31cSJohannes Berg	my %_members;
14565cb5c31cSJohannes Berg
1457463a0fdcSMarkus Heiser	$members =~ s/\s+$//;
14581da177e4SLinus Torvalds
14591da177e4SLinus Torvalds	foreach my $arg (split ',', $members) {
14601da177e4SLinus Torvalds	    $arg =~ s/^\s*(\w+).*/$1/;
14611da177e4SLinus Torvalds	    push @parameterlist, $arg;
14621da177e4SLinus Torvalds	    if (!$parameterdescs{$arg}) {
14631da177e4SLinus Torvalds		$parameterdescs{$arg} = $undescribed;
146485afe608SMauro Carvalho Chehab	        if (show_warnings("enum", $declaration_name)) {
14652defb272SMauro Carvalho Chehab			print STDERR "${file}:$.: warning: Enum value '$arg' not described in enum '$declaration_name'\n";
14662defb272SMauro Carvalho Chehab		}
14671da177e4SLinus Torvalds	    }
14685cb5c31cSJohannes Berg	    $_members{$arg} = 1;
14695cb5c31cSJohannes Berg	}
14701da177e4SLinus Torvalds
14715cb5c31cSJohannes Berg	while (my ($k, $v) = each %parameterdescs) {
14725cb5c31cSJohannes Berg	    if (!exists($_members{$k})) {
147385afe608SMauro Carvalho Chehab	        if (show_warnings("enum", $declaration_name)) {
14742defb272SMauro Carvalho Chehab		     print STDERR "${file}:$.: warning: Excess enum value '$k' description in '$declaration_name'\n";
14752defb272SMauro Carvalho Chehab		}
14765cb5c31cSJohannes Berg	    }
14771da177e4SLinus Torvalds        }
14781da177e4SLinus Torvalds
14791da177e4SLinus Torvalds	output_declaration($declaration_name,
14801da177e4SLinus Torvalds			   'enum',
14811da177e4SLinus Torvalds			   {'enum' => $declaration_name,
14821da177e4SLinus Torvalds			    'module' => $modulename,
14831da177e4SLinus Torvalds			    'parameterlist' => \@parameterlist,
14841da177e4SLinus Torvalds			    'parameterdescs' => \%parameterdescs,
14851da177e4SLinus Torvalds			    'sectionlist' => \@sectionlist,
14861da177e4SLinus Torvalds			    'sections' => \%sections,
14871da177e4SLinus Torvalds			    'purpose' => $declaration_purpose
14881da177e4SLinus Torvalds			   });
1489d38c8cfbSMauro Carvalho Chehab    } else {
1490d40e1e65SBart Van Assche	print STDERR "${file}:$.: error: Cannot parse enum!\n";
14911da177e4SLinus Torvalds	++$errors;
14921da177e4SLinus Torvalds    }
14931da177e4SLinus Torvalds}
14941da177e4SLinus Torvalds
14957d2c6b1eSMauro Carvalho Chehabmy $typedef_type = qr { ((?:\s+[\w\*]+\b){1,8})\s* }x;
14967efc6c42SMauro Carvalho Chehabmy $typedef_ident = qr { \*?\s*(\w\S+)\s* }x;
14977efc6c42SMauro Carvalho Chehabmy $typedef_args = qr { \s*\((.*)\); }x;
14987efc6c42SMauro Carvalho Chehab
14997efc6c42SMauro Carvalho Chehabmy $typedef1 = qr { typedef$typedef_type\($typedef_ident\)$typedef_args }x;
15007efc6c42SMauro Carvalho Chehabmy $typedef2 = qr { typedef$typedef_type$typedef_ident$typedef_args }x;
15017efc6c42SMauro Carvalho Chehab
15021da177e4SLinus Torvaldssub dump_typedef($$) {
15031da177e4SLinus Torvalds    my $x = shift;
15041da177e4SLinus Torvalds    my $file = shift;
15051da177e4SLinus Torvalds
1506aeec46b9SMartin Waitz    $x =~ s@/\*.*?\*/@@gos;	# strip comments.
150783766452SMauro Carvalho Chehab
15087efc6c42SMauro Carvalho Chehab    # Parse function typedef prototypes
15097efc6c42SMauro Carvalho Chehab    if ($x =~ $typedef1 || $x =~ $typedef2) {
151083766452SMauro Carvalho Chehab	$return_type = $1;
151183766452SMauro Carvalho Chehab	$declaration_name = $2;
151283766452SMauro Carvalho Chehab	my $args = $3;
15136b80975cSMauro Carvalho Chehab	$return_type =~ s/^\s+//;
151483766452SMauro Carvalho Chehab
151552042e2dSMauro Carvalho Chehab	if ($identifier ne $declaration_name) {
151652042e2dSMauro Carvalho Chehab	    print STDERR "${file}:$.: warning: expecting prototype for typedef $identifier. Prototype was for typedef $declaration_name instead\n";
151752042e2dSMauro Carvalho Chehab	    return;
151852042e2dSMauro Carvalho Chehab	}
151952042e2dSMauro Carvalho Chehab
1520151c468bSMauro Carvalho Chehab	create_parameterlist($args, ',', $file, $declaration_name);
152183766452SMauro Carvalho Chehab
152283766452SMauro Carvalho Chehab	output_declaration($declaration_name,
152383766452SMauro Carvalho Chehab			   'function',
152483766452SMauro Carvalho Chehab			   {'function' => $declaration_name,
152582801d06SMauro Carvalho Chehab			    'typedef' => 1,
152683766452SMauro Carvalho Chehab			    'module' => $modulename,
152783766452SMauro Carvalho Chehab			    'functiontype' => $return_type,
152883766452SMauro Carvalho Chehab			    'parameterlist' => \@parameterlist,
152983766452SMauro Carvalho Chehab			    'parameterdescs' => \%parameterdescs,
153083766452SMauro Carvalho Chehab			    'parametertypes' => \%parametertypes,
153183766452SMauro Carvalho Chehab			    'sectionlist' => \@sectionlist,
153283766452SMauro Carvalho Chehab			    'sections' => \%sections,
153383766452SMauro Carvalho Chehab			    'purpose' => $declaration_purpose
153483766452SMauro Carvalho Chehab			   });
153583766452SMauro Carvalho Chehab	return;
153683766452SMauro Carvalho Chehab    }
153783766452SMauro Carvalho Chehab
15381da177e4SLinus Torvalds    while (($x =~ /\(*.\)\s*;$/) || ($x =~ /\[*.\]\s*;$/)) {
15391da177e4SLinus Torvalds	$x =~ s/\(*.\)\s*;$/;/;
15401da177e4SLinus Torvalds	$x =~ s/\[*.\]\s*;$/;/;
15411da177e4SLinus Torvalds    }
15421da177e4SLinus Torvalds
15431da177e4SLinus Torvalds    if ($x =~ /typedef.*\s+(\w+)\s*;/) {
15441da177e4SLinus Torvalds	$declaration_name = $1;
15451da177e4SLinus Torvalds
154652042e2dSMauro Carvalho Chehab	if ($identifier ne $declaration_name) {
154752042e2dSMauro Carvalho Chehab	    print STDERR "${file}:$.: warning: expecting prototype for typedef $identifier. Prototype was for typedef $declaration_name instead\n";
154852042e2dSMauro Carvalho Chehab	    return;
154952042e2dSMauro Carvalho Chehab	}
155052042e2dSMauro Carvalho Chehab
15511da177e4SLinus Torvalds	output_declaration($declaration_name,
15521da177e4SLinus Torvalds			   'typedef',
15531da177e4SLinus Torvalds			   {'typedef' => $declaration_name,
15541da177e4SLinus Torvalds			    'module' => $modulename,
15551da177e4SLinus Torvalds			    'sectionlist' => \@sectionlist,
15561da177e4SLinus Torvalds			    'sections' => \%sections,
15571da177e4SLinus Torvalds			    'purpose' => $declaration_purpose
15581da177e4SLinus Torvalds			   });
15591da177e4SLinus Torvalds    }
15601da177e4SLinus Torvalds    else {
1561d40e1e65SBart Van Assche	print STDERR "${file}:$.: error: Cannot parse typedef!\n";
15621da177e4SLinus Torvalds	++$errors;
15631da177e4SLinus Torvalds    }
15641da177e4SLinus Torvalds}
15651da177e4SLinus Torvalds
1566a1d94aa5SRandy Dunlapsub save_struct_actual($) {
1567a1d94aa5SRandy Dunlap    my $actual = shift;
1568a1d94aa5SRandy Dunlap
1569a1d94aa5SRandy Dunlap    # strip all spaces from the actual param so that it looks like one string item
1570a1d94aa5SRandy Dunlap    $actual =~ s/\s*//g;
1571a1d94aa5SRandy Dunlap    $struct_actual = $struct_actual . $actual . " ";
1572a1d94aa5SRandy Dunlap}
1573a1d94aa5SRandy Dunlap
1574151c468bSMauro Carvalho Chehabsub create_parameterlist($$$$) {
15751da177e4SLinus Torvalds    my $args = shift;
15761da177e4SLinus Torvalds    my $splitter = shift;
15771da177e4SLinus Torvalds    my $file = shift;
1578151c468bSMauro Carvalho Chehab    my $declaration_name = shift;
15791da177e4SLinus Torvalds    my $type;
15801da177e4SLinus Torvalds    my $param;
15811da177e4SLinus Torvalds
1582a6d3fe77SMartin Waitz    # temporarily replace commas inside function pointer definition
1583e86bdb24SAditya Srivastava    my $arg_expr = qr{\([^\),]+};
1584e86bdb24SAditya Srivastava    while ($args =~ /$arg_expr,/) {
1585e86bdb24SAditya Srivastava	$args =~ s/($arg_expr),/$1#/g;
15861da177e4SLinus Torvalds    }
15871da177e4SLinus Torvalds
15881da177e4SLinus Torvalds    foreach my $arg (split($splitter, $args)) {
15891da177e4SLinus Torvalds	# strip comments
15901da177e4SLinus Torvalds	$arg =~ s/\/\*.*\*\///;
15911da177e4SLinus Torvalds	# strip leading/trailing spaces
15921da177e4SLinus Torvalds	$arg =~ s/^\s*//;
15931da177e4SLinus Torvalds	$arg =~ s/\s*$//;
15941da177e4SLinus Torvalds	$arg =~ s/\s+/ /;
15951da177e4SLinus Torvalds
15961da177e4SLinus Torvalds	if ($arg =~ /^#/) {
15971da177e4SLinus Torvalds	    # Treat preprocessor directive as a typeless variable just to fill
15981da177e4SLinus Torvalds	    # corresponding data structures "correctly". Catch it later in
15991da177e4SLinus Torvalds	    # output_* subs.
1600ed8348e2SMauro Carvalho Chehab	    push_parameter($arg, "", "", $file);
160100d62961SRichard Kennedy	} elsif ($arg =~ m/\(.+\)\s*\(/) {
16021da177e4SLinus Torvalds	    # pointer-to-function
16031da177e4SLinus Torvalds	    $arg =~ tr/#/,/;
1604336ced2dSAditya Srivastava	    $arg =~ m/[^\(]+\(\*?\s*([\w\[\]\.]*)\s*\)/;
16051da177e4SLinus Torvalds	    $param = $1;
16061da177e4SLinus Torvalds	    $type = $arg;
160700d62961SRichard Kennedy	    $type =~ s/([^\(]+\(\*?)\s*$param/$1/;
1608a1d94aa5SRandy Dunlap	    save_struct_actual($param);
1609ed8348e2SMauro Carvalho Chehab	    push_parameter($param, $type, $arg, $file, $declaration_name);
1610aeec46b9SMartin Waitz	} elsif ($arg) {
16111da177e4SLinus Torvalds	    $arg =~ s/\s*:\s*/:/g;
16121da177e4SLinus Torvalds	    $arg =~ s/\s*\[/\[/g;
16131da177e4SLinus Torvalds
16141da177e4SLinus Torvalds	    my @args = split('\s*,\s*', $arg);
16151da177e4SLinus Torvalds	    if ($args[0] =~ m/\*/) {
16161da177e4SLinus Torvalds		$args[0] =~ s/(\*+)\s*/ $1/;
16171da177e4SLinus Torvalds	    }
1618884f2810SBorislav Petkov
1619884f2810SBorislav Petkov	    my @first_arg;
1620884f2810SBorislav Petkov	    if ($args[0] =~ /^(.*\s+)(.*?\[.*\].*)$/) {
1621884f2810SBorislav Petkov		    shift @args;
1622884f2810SBorislav Petkov		    push(@first_arg, split('\s+', $1));
1623884f2810SBorislav Petkov		    push(@first_arg, $2);
1624884f2810SBorislav Petkov	    } else {
1625884f2810SBorislav Petkov		    @first_arg = split('\s+', shift @args);
1626884f2810SBorislav Petkov	    }
1627884f2810SBorislav Petkov
16281da177e4SLinus Torvalds	    unshift(@args, pop @first_arg);
16291da177e4SLinus Torvalds	    $type = join " ", @first_arg;
16301da177e4SLinus Torvalds
16311da177e4SLinus Torvalds	    foreach $param (@args) {
16321da177e4SLinus Torvalds		if ($param =~ m/^(\*+)\s*(.*)/) {
1633a1d94aa5SRandy Dunlap		    save_struct_actual($2);
1634ed8348e2SMauro Carvalho Chehab
1635ed8348e2SMauro Carvalho Chehab		    push_parameter($2, "$type $1", $arg, $file, $declaration_name);
16361da177e4SLinus Torvalds		}
16371da177e4SLinus Torvalds		elsif ($param =~ m/(.*?):(\d+)/) {
16387b97887eSRandy Dunlap		    if ($type ne "") { # skip unnamed bit-fields
1639a1d94aa5SRandy Dunlap			save_struct_actual($1);
1640ed8348e2SMauro Carvalho Chehab			push_parameter($1, "$type:$2", $arg, $file, $declaration_name)
16411da177e4SLinus Torvalds		    }
16427b97887eSRandy Dunlap		}
16431da177e4SLinus Torvalds		else {
1644a1d94aa5SRandy Dunlap		    save_struct_actual($param);
1645ed8348e2SMauro Carvalho Chehab		    push_parameter($param, $type, $arg, $file, $declaration_name);
16461da177e4SLinus Torvalds		}
16471da177e4SLinus Torvalds	    }
16481da177e4SLinus Torvalds	}
16491da177e4SLinus Torvalds    }
16501da177e4SLinus Torvalds}
16511da177e4SLinus Torvalds
1652ed8348e2SMauro Carvalho Chehabsub push_parameter($$$$$) {
16531da177e4SLinus Torvalds	my $param = shift;
16541da177e4SLinus Torvalds	my $type = shift;
1655ed8348e2SMauro Carvalho Chehab	my $org_arg = shift;
16561da177e4SLinus Torvalds	my $file = shift;
1657151c468bSMauro Carvalho Chehab	my $declaration_name = shift;
16581da177e4SLinus Torvalds
16595f8c7c98SRandy Dunlap	if (($anon_struct_union == 1) && ($type eq "") &&
16605f8c7c98SRandy Dunlap	    ($param eq "}")) {
16615f8c7c98SRandy Dunlap		return;		# ignore the ending }; from anon. struct/union
16625f8c7c98SRandy Dunlap	}
16635f8c7c98SRandy Dunlap
16645f8c7c98SRandy Dunlap	$anon_struct_union = 0;
1665f9b5c530SMauro Carvalho Chehab	$param =~ s/[\[\)].*//;
16661da177e4SLinus Torvalds
1667a6d3fe77SMartin Waitz	if ($type eq "" && $param =~ /\.\.\.$/)
16681da177e4SLinus Torvalds	{
1669c950a173SSilvio Fricke	    if (!$param =~ /\w\.\.\.$/) {
1670c950a173SSilvio Fricke	      # handles unnamed variable parameters
1671ef00028bSJonathan Corbet	      $param = "...";
1672c950a173SSilvio Fricke	    }
167343756e34SJonathan Neuschäfer	    elsif ($param =~ /\w\.\.\.$/) {
167443756e34SJonathan Neuschäfer	      # for named variable parameters of the form `x...`, remove the dots
167543756e34SJonathan Neuschäfer	      $param =~ s/\.\.\.$//;
167643756e34SJonathan Neuschäfer	    }
1677ced69090SRandy Dunlap	    if (!defined $parameterdescs{$param} || $parameterdescs{$param} eq "") {
1678a6d3fe77SMartin Waitz		$parameterdescs{$param} = "variable arguments";
16791da177e4SLinus Torvalds	    }
1680ced69090SRandy Dunlap	}
16811da177e4SLinus Torvalds	elsif ($type eq "" && ($param eq "" or $param eq "void"))
16821da177e4SLinus Torvalds	{
16831da177e4SLinus Torvalds	    $param="void";
16841da177e4SLinus Torvalds	    $parameterdescs{void} = "no arguments";
16851da177e4SLinus Torvalds	}
1686134fe01bSRandy Dunlap	elsif ($type eq "" && ($param eq "struct" or $param eq "union"))
1687134fe01bSRandy Dunlap	# handle unnamed (anonymous) union or struct:
1688134fe01bSRandy Dunlap	{
1689134fe01bSRandy Dunlap		$type = $param;
1690134fe01bSRandy Dunlap		$param = "{unnamed_" . $param . "}";
1691134fe01bSRandy Dunlap		$parameterdescs{$param} = "anonymous\n";
16925f8c7c98SRandy Dunlap		$anon_struct_union = 1;
1693134fe01bSRandy Dunlap	}
1694134fe01bSRandy Dunlap
1695a6d3fe77SMartin Waitz	# warn if parameter has no description
1696134fe01bSRandy Dunlap	# (but ignore ones starting with # as these are not parameters
1697134fe01bSRandy Dunlap	# but inline preprocessor statements);
1698151c468bSMauro Carvalho Chehab	# Note: It will also ignore void params and unnamed structs/unions
1699f9b5c530SMauro Carvalho Chehab	if (!defined $parameterdescs{$param} && $param !~ /^#/) {
1700f9b5c530SMauro Carvalho Chehab		$parameterdescs{$param} = $undescribed;
17011da177e4SLinus Torvalds
1702be5cd20cSJonathan Corbet	        if (show_warnings($type, $declaration_name) && $param !~ /\./) {
1703151c468bSMauro Carvalho Chehab			print STDERR
1704151c468bSMauro Carvalho Chehab			      "${file}:$.: warning: Function parameter or member '$param' not described in '$declaration_name'\n";
17051da177e4SLinus Torvalds			++$warnings;
17061da177e4SLinus Torvalds		}
17072defb272SMauro Carvalho Chehab	}
17081da177e4SLinus Torvalds
170925985edcSLucas De Marchi	# strip spaces from $param so that it is one continuous string
1710e34e7dbbSRandy Dunlap	# on @parameterlist;
1711e34e7dbbSRandy Dunlap	# this fixes a problem where check_sections() cannot find
1712e34e7dbbSRandy Dunlap	# a parameter like "addr[6 + 2]" because it actually appears
1713e34e7dbbSRandy Dunlap	# as "addr[6", "+", "2]" on the parameter list;
1714e34e7dbbSRandy Dunlap	# but it's better to maintain the param string unchanged for output,
1715e34e7dbbSRandy Dunlap	# so just weaken the string compare in check_sections() to ignore
1716e34e7dbbSRandy Dunlap	# "[blah" in a parameter string;
1717e34e7dbbSRandy Dunlap	###$param =~ s/\s*//g;
17181da177e4SLinus Torvalds	push @parameterlist, $param;
1719ed8348e2SMauro Carvalho Chehab	$org_arg =~ s/\s\s+/ /g;
1720ed8348e2SMauro Carvalho Chehab	$parametertypes{$param} = $org_arg;
17211da177e4SLinus Torvalds}
17221da177e4SLinus Torvalds
17231081de2dSMauro Carvalho Chehabsub check_sections($$$$$) {
17241081de2dSMauro Carvalho Chehab	my ($file, $decl_name, $decl_type, $sectcheck, $prmscheck) = @_;
1725a1d94aa5SRandy Dunlap	my @sects = split ' ', $sectcheck;
1726a1d94aa5SRandy Dunlap	my @prms = split ' ', $prmscheck;
1727a1d94aa5SRandy Dunlap	my $err;
1728a1d94aa5SRandy Dunlap	my ($px, $sx);
1729a1d94aa5SRandy Dunlap	my $prm_clean;		# strip trailing "[array size]" and/or beginning "*"
1730a1d94aa5SRandy Dunlap
1731a1d94aa5SRandy Dunlap	foreach $sx (0 .. $#sects) {
1732a1d94aa5SRandy Dunlap		$err = 1;
1733a1d94aa5SRandy Dunlap		foreach $px (0 .. $#prms) {
1734a1d94aa5SRandy Dunlap			$prm_clean = $prms[$px];
1735a1d94aa5SRandy Dunlap			$prm_clean =~ s/\[.*\]//;
1736e86bdb24SAditya Srivastava			$prm_clean =~ s/$attribute//i;
1737e34e7dbbSRandy Dunlap			# ignore array size in a parameter string;
1738e34e7dbbSRandy Dunlap			# however, the original param string may contain
1739e34e7dbbSRandy Dunlap			# spaces, e.g.:  addr[6 + 2]
1740e34e7dbbSRandy Dunlap			# and this appears in @prms as "addr[6" since the
1741e34e7dbbSRandy Dunlap			# parameter list is split at spaces;
1742e34e7dbbSRandy Dunlap			# hence just ignore "[..." for the sections check;
1743e34e7dbbSRandy Dunlap			$prm_clean =~ s/\[.*//;
1744e34e7dbbSRandy Dunlap
1745a1d94aa5SRandy Dunlap			##$prm_clean =~ s/^\**//;
1746a1d94aa5SRandy Dunlap			if ($prm_clean eq $sects[$sx]) {
1747a1d94aa5SRandy Dunlap				$err = 0;
1748a1d94aa5SRandy Dunlap				last;
1749a1d94aa5SRandy Dunlap			}
1750a1d94aa5SRandy Dunlap		}
1751a1d94aa5SRandy Dunlap		if ($err) {
1752a1d94aa5SRandy Dunlap			if ($decl_type eq "function") {
1753d40e1e65SBart Van Assche				print STDERR "${file}:$.: warning: " .
1754a1d94aa5SRandy Dunlap					"Excess function parameter " .
1755a1d94aa5SRandy Dunlap					"'$sects[$sx]' " .
1756a1d94aa5SRandy Dunlap					"description in '$decl_name'\n";
1757a1d94aa5SRandy Dunlap				++$warnings;
1758a1d94aa5SRandy Dunlap			}
1759a1d94aa5SRandy Dunlap		}
1760a1d94aa5SRandy Dunlap	}
1761a1d94aa5SRandy Dunlap}
1762a1d94aa5SRandy Dunlap
17631da177e4SLinus Torvalds##
17644092bac7SYacine Belkadi# Checks the section describing the return value of a function.
17654092bac7SYacine Belkadisub check_return_section {
17664092bac7SYacine Belkadi        my $file = shift;
17674092bac7SYacine Belkadi        my $declaration_name = shift;
17684092bac7SYacine Belkadi        my $return_type = shift;
17694092bac7SYacine Belkadi
17704092bac7SYacine Belkadi        # Ignore an empty return type (It's a macro)
17714092bac7SYacine Belkadi        # Ignore functions with a "void" return type. (But don't ignore "void *")
17724092bac7SYacine Belkadi        if (($return_type eq "") || ($return_type =~ /void\s*\w*\s*$/)) {
17734092bac7SYacine Belkadi                return;
17744092bac7SYacine Belkadi        }
17754092bac7SYacine Belkadi
17764092bac7SYacine Belkadi        if (!defined($sections{$section_return}) ||
17774092bac7SYacine Belkadi            $sections{$section_return} eq "") {
1778d40e1e65SBart Van Assche                print STDERR "${file}:$.: warning: " .
17794092bac7SYacine Belkadi                        "No description found for return value of " .
17804092bac7SYacine Belkadi                        "'$declaration_name'\n";
17814092bac7SYacine Belkadi                ++$warnings;
17824092bac7SYacine Belkadi        }
17834092bac7SYacine Belkadi}
17844092bac7SYacine Belkadi
17854092bac7SYacine Belkadi##
17861da177e4SLinus Torvalds# takes a function prototype and the name of the current file being
17871da177e4SLinus Torvalds# processed and spits out all the details stored in the global
17881da177e4SLinus Torvalds# arrays/hashes.
17891da177e4SLinus Torvaldssub dump_function($$) {
17901da177e4SLinus Torvalds    my $prototype = shift;
17911da177e4SLinus Torvalds    my $file = shift;
1792cbb4d3e6SHoria Geanta    my $noret = 0;
17931da177e4SLinus Torvalds
17945ef09c96SMauro Carvalho Chehab    print_lineno($new_start_line);
17955eb6b4b3SMauro Carvalho Chehab
17961da177e4SLinus Torvalds    $prototype =~ s/^static +//;
17971da177e4SLinus Torvalds    $prototype =~ s/^extern +//;
17984dc3b16bSPavel Pisa    $prototype =~ s/^asmlinkage +//;
17991da177e4SLinus Torvalds    $prototype =~ s/^inline +//;
18001da177e4SLinus Torvalds    $prototype =~ s/^__inline__ +//;
180132e79401SRandy Dunlap    $prototype =~ s/^__inline +//;
180232e79401SRandy Dunlap    $prototype =~ s/^__always_inline +//;
180332e79401SRandy Dunlap    $prototype =~ s/^noinline +//;
180474fc5c65SRandy Dunlap    $prototype =~ s/__init +//;
180520072205SRandy Dunlap    $prototype =~ s/__init_or_module +//;
180680342d48SMatthew Wilcox    $prototype =~ s/__deprecated +//;
1807084aa001SAditya Srivastava    $prototype =~ s/__flatten +//;
1808270a0096SRandy Dunlap    $prototype =~ s/__meminit +//;
180970c95b00SRandy Dunlap    $prototype =~ s/__must_check +//;
18100df7c0e3SRandy Dunlap    $prototype =~ s/__weak +//;
18110891f959SMatthew Wilcox    $prototype =~ s/__sched +//;
181295e760cbSRandy Dunlap    $prototype =~ s/__printf\s*\(\s*\d*\s*,\s*\d*\s*\) +//;
1813a40a8a11SKees Cook    $prototype =~ s/__alloc_size\s*\(\s*\d+\s*(?:,\s*\d+\s*)?\) +//;
1814cbb4d3e6SHoria Geanta    my $define = $prototype =~ s/^#\s*define\s+//; #ak added
1815084aa001SAditya Srivastava    $prototype =~ s/__attribute_const__ +//;
1816b1aaa546SPaolo Bonzini    $prototype =~ s/__attribute__\s*\(\(
1817b1aaa546SPaolo Bonzini            (?:
1818b1aaa546SPaolo Bonzini                 [\w\s]++          # attribute name
1819b1aaa546SPaolo Bonzini                 (?:\([^)]*+\))?   # attribute arguments
1820b1aaa546SPaolo Bonzini                 \s*+,?            # optional comma at the end
1821b1aaa546SPaolo Bonzini            )+
1822b1aaa546SPaolo Bonzini          \)\)\s+//x;
18231da177e4SLinus Torvalds
18241da177e4SLinus Torvalds    # Yes, this truly is vile.  We are looking for:
18251da177e4SLinus Torvalds    # 1. Return type (may be nothing if we're looking at a macro)
18261da177e4SLinus Torvalds    # 2. Function name
18271da177e4SLinus Torvalds    # 3. Function parameters.
18281da177e4SLinus Torvalds    #
18291da177e4SLinus Torvalds    # All the while we have to watch out for function pointer parameters
18301da177e4SLinus Torvalds    # (which IIRC is what the two sections are for), C types (these
18311da177e4SLinus Torvalds    # regexps don't even start to express all the possibilities), and
18321da177e4SLinus Torvalds    # so on.
18331da177e4SLinus Torvalds    #
18341da177e4SLinus Torvalds    # If you mess with these regexps, it's a good idea to check that
18351da177e4SLinus Torvalds    # the following functions' documentation still comes out right:
18361da177e4SLinus Torvalds    # - parport_register_device (function pointer parameters)
18371da177e4SLinus Torvalds    # - atomic_set (macro)
18389598f91fSMartin Waitz    # - pci_match_device, __copy_to_user (long return type)
1839e86bdb24SAditya Srivastava    my $name = qr{[a-zA-Z0-9_~:]+};
1840e86bdb24SAditya Srivastava    my $prototype_end1 = qr{[^\(]*};
1841e86bdb24SAditya Srivastava    my $prototype_end2 = qr{[^\{]*};
1842e86bdb24SAditya Srivastava    my $prototype_end = qr{\(($prototype_end1|$prototype_end2)\)};
1843e86bdb24SAditya Srivastava    my $type1 = qr{[\w\s]+};
1844e86bdb24SAditya Srivastava    my $type2 = qr{$type1\*+};
18451da177e4SLinus Torvalds
1846e86bdb24SAditya Srivastava    if ($define && $prototype =~ m/^()($name)\s+/) {
1847cbb4d3e6SHoria Geanta        # This is an object-like macro, it has no return type and no parameter
1848cbb4d3e6SHoria Geanta        # list.
1849cbb4d3e6SHoria Geanta        # Function-like macros are not allowed to have spaces between
1850cbb4d3e6SHoria Geanta        # declaration_name and opening parenthesis (notice the \s+).
1851cbb4d3e6SHoria Geanta        $return_type = $1;
1852cbb4d3e6SHoria Geanta        $declaration_name = $2;
1853cbb4d3e6SHoria Geanta        $noret = 1;
1854e86bdb24SAditya Srivastava    } elsif ($prototype =~ m/^()($name)\s*$prototype_end/ ||
1855e86bdb24SAditya Srivastava	$prototype =~ m/^($type1)\s+($name)\s*$prototype_end/ ||
1856e86bdb24SAditya Srivastava	$prototype =~ m/^($type2+)\s*($name)\s*$prototype_end/)  {
18571da177e4SLinus Torvalds	$return_type = $1;
18581da177e4SLinus Torvalds	$declaration_name = $2;
18591da177e4SLinus Torvalds	my $args = $3;
18601da177e4SLinus Torvalds
1861151c468bSMauro Carvalho Chehab	create_parameterlist($args, ',', $file, $declaration_name);
18621da177e4SLinus Torvalds    } else {
1863d40e1e65SBart Van Assche	print STDERR "${file}:$.: warning: cannot understand function prototype: '$prototype'\n";
18641da177e4SLinus Torvalds	return;
18651da177e4SLinus Torvalds    }
18661da177e4SLinus Torvalds
186752042e2dSMauro Carvalho Chehab    if ($identifier ne $declaration_name) {
186852042e2dSMauro Carvalho Chehab	print STDERR "${file}:$.: warning: expecting prototype for $identifier(). Prototype was for $declaration_name() instead\n";
186952042e2dSMauro Carvalho Chehab	return;
187052042e2dSMauro Carvalho Chehab    }
187152042e2dSMauro Carvalho Chehab
1872a1d94aa5SRandy Dunlap    my $prms = join " ", @parameterlist;
18731081de2dSMauro Carvalho Chehab    check_sections($file, $declaration_name, "function", $sectcheck, $prms);
1874a1d94aa5SRandy Dunlap
18754092bac7SYacine Belkadi    # This check emits a lot of warnings at the moment, because many
18764092bac7SYacine Belkadi    # functions don't have a 'Return' doc section. So until the number
18774092bac7SYacine Belkadi    # of warnings goes sufficiently down, the check is only performed in
18784092bac7SYacine Belkadi    # verbose mode.
18794092bac7SYacine Belkadi    # TODO: always perform the check.
1880cbb4d3e6SHoria Geanta    if ($verbose && !$noret) {
18814092bac7SYacine Belkadi	    check_return_section($file, $declaration_name, $return_type);
18824092bac7SYacine Belkadi    }
18834092bac7SYacine Belkadi
188447bcacfdSMauro Carvalho Chehab    # The function parser can be called with a typedef parameter.
188547bcacfdSMauro Carvalho Chehab    # Handle it.
188647bcacfdSMauro Carvalho Chehab    if ($return_type =~ /typedef/) {
188747bcacfdSMauro Carvalho Chehab	output_declaration($declaration_name,
188847bcacfdSMauro Carvalho Chehab			   'function',
188947bcacfdSMauro Carvalho Chehab			   {'function' => $declaration_name,
189047bcacfdSMauro Carvalho Chehab			    'typedef' => 1,
189147bcacfdSMauro Carvalho Chehab			    'module' => $modulename,
189247bcacfdSMauro Carvalho Chehab			    'functiontype' => $return_type,
189347bcacfdSMauro Carvalho Chehab			    'parameterlist' => \@parameterlist,
189447bcacfdSMauro Carvalho Chehab			    'parameterdescs' => \%parameterdescs,
189547bcacfdSMauro Carvalho Chehab			    'parametertypes' => \%parametertypes,
189647bcacfdSMauro Carvalho Chehab			    'sectionlist' => \@sectionlist,
189747bcacfdSMauro Carvalho Chehab			    'sections' => \%sections,
189847bcacfdSMauro Carvalho Chehab			    'purpose' => $declaration_purpose
189947bcacfdSMauro Carvalho Chehab			   });
190047bcacfdSMauro Carvalho Chehab    } else {
19011da177e4SLinus Torvalds	output_declaration($declaration_name,
19021da177e4SLinus Torvalds			   'function',
19031da177e4SLinus Torvalds			   {'function' => $declaration_name,
19041da177e4SLinus Torvalds			    'module' => $modulename,
19051da177e4SLinus Torvalds			    'functiontype' => $return_type,
19061da177e4SLinus Torvalds			    'parameterlist' => \@parameterlist,
19071da177e4SLinus Torvalds			    'parameterdescs' => \%parameterdescs,
19081da177e4SLinus Torvalds			    'parametertypes' => \%parametertypes,
19091da177e4SLinus Torvalds			    'sectionlist' => \@sectionlist,
19101da177e4SLinus Torvalds			    'sections' => \%sections,
19111da177e4SLinus Torvalds			    'purpose' => $declaration_purpose
19121da177e4SLinus Torvalds			   });
19131da177e4SLinus Torvalds    }
191447bcacfdSMauro Carvalho Chehab}
19151da177e4SLinus Torvalds
19161da177e4SLinus Torvaldssub reset_state {
19171da177e4SLinus Torvalds    $function = "";
19181da177e4SLinus Torvalds    %parameterdescs = ();
19191da177e4SLinus Torvalds    %parametertypes = ();
19201da177e4SLinus Torvalds    @parameterlist = ();
19211da177e4SLinus Torvalds    %sections = ();
19221da177e4SLinus Torvalds    @sectionlist = ();
1923a1d94aa5SRandy Dunlap    $sectcheck = "";
1924a1d94aa5SRandy Dunlap    $struct_actual = "";
19251da177e4SLinus Torvalds    $prototype = "";
19261da177e4SLinus Torvalds
192748af606aSJani Nikula    $state = STATE_NORMAL;
192848af606aSJani Nikula    $inline_doc_state = STATE_INLINE_NA;
19291da177e4SLinus Torvalds}
19301da177e4SLinus Torvalds
193156afb0f8SJason Baronsub tracepoint_munge($) {
193256afb0f8SJason Baron	my $file = shift;
193356afb0f8SJason Baron	my $tracepointname = 0;
193456afb0f8SJason Baron	my $tracepointargs = 0;
193556afb0f8SJason Baron
193656afb0f8SJason Baron	if ($prototype =~ m/TRACE_EVENT\((.*?),/) {
193756afb0f8SJason Baron		$tracepointname = $1;
193856afb0f8SJason Baron	}
19393a9089fdSJason Baron	if ($prototype =~ m/DEFINE_SINGLE_EVENT\((.*?),/) {
19403a9089fdSJason Baron		$tracepointname = $1;
19413a9089fdSJason Baron	}
19423a9089fdSJason Baron	if ($prototype =~ m/DEFINE_EVENT\((.*?),(.*?),/) {
19433a9089fdSJason Baron		$tracepointname = $2;
19443a9089fdSJason Baron	}
19453a9089fdSJason Baron	$tracepointname =~ s/^\s+//; #strip leading whitespace
194656afb0f8SJason Baron	if ($prototype =~ m/TP_PROTO\((.*?)\)/) {
194756afb0f8SJason Baron		$tracepointargs = $1;
194856afb0f8SJason Baron	}
194956afb0f8SJason Baron	if (($tracepointname eq 0) || ($tracepointargs eq 0)) {
1950d40e1e65SBart Van Assche		print STDERR "${file}:$.: warning: Unrecognized tracepoint format: \n".
195156afb0f8SJason Baron			     "$prototype\n";
195256afb0f8SJason Baron	} else {
195356afb0f8SJason Baron		$prototype = "static inline void trace_$tracepointname($tracepointargs)";
195452042e2dSMauro Carvalho Chehab		$identifier = "trace_$identifier";
195556afb0f8SJason Baron	}
195656afb0f8SJason Baron}
195756afb0f8SJason Baron
1958b4870bc5SRandy Dunlapsub syscall_munge() {
1959b4870bc5SRandy Dunlap	my $void = 0;
1960b4870bc5SRandy Dunlap
19617c9aa015SMauro Carvalho Chehab	$prototype =~ s@[\r\n]+@ @gos; # strip newlines/CR's
1962b4870bc5SRandy Dunlap##	if ($prototype =~ m/SYSCALL_DEFINE0\s*\(\s*(a-zA-Z0-9_)*\s*\)/) {
1963b4870bc5SRandy Dunlap	if ($prototype =~ m/SYSCALL_DEFINE0/) {
1964b4870bc5SRandy Dunlap		$void = 1;
1965b4870bc5SRandy Dunlap##		$prototype = "long sys_$1(void)";
1966b4870bc5SRandy Dunlap	}
1967b4870bc5SRandy Dunlap
1968b4870bc5SRandy Dunlap	$prototype =~ s/SYSCALL_DEFINE.*\(/long sys_/; # fix return type & func name
1969b4870bc5SRandy Dunlap	if ($prototype =~ m/long (sys_.*?),/) {
1970b4870bc5SRandy Dunlap		$prototype =~ s/,/\(/;
1971b4870bc5SRandy Dunlap	} elsif ($void) {
1972b4870bc5SRandy Dunlap		$prototype =~ s/\)/\(void\)/;
1973b4870bc5SRandy Dunlap	}
1974b4870bc5SRandy Dunlap
1975b4870bc5SRandy Dunlap	# now delete all of the odd-number commas in $prototype
1976b4870bc5SRandy Dunlap	# so that arg types & arg names don't have a comma between them
1977b4870bc5SRandy Dunlap	my $count = 0;
1978b4870bc5SRandy Dunlap	my $len = length($prototype);
1979b4870bc5SRandy Dunlap	if ($void) {
1980b4870bc5SRandy Dunlap		$len = 0;	# skip the for-loop
1981b4870bc5SRandy Dunlap	}
1982b4870bc5SRandy Dunlap	for (my $ix = 0; $ix < $len; $ix++) {
1983b4870bc5SRandy Dunlap		if (substr($prototype, $ix, 1) eq ',') {
1984b4870bc5SRandy Dunlap			$count++;
1985b4870bc5SRandy Dunlap			if ($count % 2 == 1) {
1986b4870bc5SRandy Dunlap				substr($prototype, $ix, 1) = ' ';
1987b4870bc5SRandy Dunlap			}
1988b4870bc5SRandy Dunlap		}
1989b4870bc5SRandy Dunlap	}
1990b4870bc5SRandy Dunlap}
1991b4870bc5SRandy Dunlap
1992b7afa92bSDaniel Vettersub process_proto_function($$) {
19931da177e4SLinus Torvalds    my $x = shift;
19941da177e4SLinus Torvalds    my $file = shift;
19951da177e4SLinus Torvalds
199651f5a0c8SRandy Dunlap    $x =~ s@\/\/.*$@@gos; # strip C99-style comments to end of line
199751f5a0c8SRandy Dunlap
1998890c78c2SRandy Dunlap    if ($x =~ m#\s*/\*\s+MACDOC\s*#io || ($x =~ /^#/ && $x !~ /^#\s*define/)) {
19991da177e4SLinus Torvalds	# do nothing
20001da177e4SLinus Torvalds    }
20011da177e4SLinus Torvalds    elsif ($x =~ /([^\{]*)/) {
20021da177e4SLinus Torvalds	$prototype .= $1;
20031da177e4SLinus Torvalds    }
2004b4870bc5SRandy Dunlap
2005890c78c2SRandy Dunlap    if (($x =~ /\{/) || ($x =~ /\#\s*define/) || ($x =~ /;/)) {
20061da177e4SLinus Torvalds	$prototype =~ s@/\*.*?\*/@@gos;	# strip comments.
20071da177e4SLinus Torvalds	$prototype =~ s@[\r\n]+@ @gos; # strip newlines/cr's.
20081da177e4SLinus Torvalds	$prototype =~ s@^\s+@@gos; # strip leading spaces
20097ae281b0SMauro Carvalho Chehab
20107ae281b0SMauro Carvalho Chehab	 # Handle prototypes for function pointers like:
20117ae281b0SMauro Carvalho Chehab	 # int (*pcs_config)(struct foo)
20127ae281b0SMauro Carvalho Chehab	$prototype =~ s@^(\S+\s+)\(\s*\*(\S+)\)@$1$2@gos;
20137ae281b0SMauro Carvalho Chehab
2014b4870bc5SRandy Dunlap	if ($prototype =~ /SYSCALL_DEFINE/) {
2015b4870bc5SRandy Dunlap		syscall_munge();
2016b4870bc5SRandy Dunlap	}
20173a9089fdSJason Baron	if ($prototype =~ /TRACE_EVENT/ || $prototype =~ /DEFINE_EVENT/ ||
20183a9089fdSJason Baron	    $prototype =~ /DEFINE_SINGLE_EVENT/)
20193a9089fdSJason Baron	{
202056afb0f8SJason Baron		tracepoint_munge($file);
202156afb0f8SJason Baron	}
20221da177e4SLinus Torvalds	dump_function($prototype, $file);
20231da177e4SLinus Torvalds	reset_state();
20241da177e4SLinus Torvalds    }
20251da177e4SLinus Torvalds}
20261da177e4SLinus Torvalds
2027b7afa92bSDaniel Vettersub process_proto_type($$) {
20281da177e4SLinus Torvalds    my $x = shift;
20291da177e4SLinus Torvalds    my $file = shift;
20301da177e4SLinus Torvalds
20311da177e4SLinus Torvalds    $x =~ s@[\r\n]+@ @gos; # strip newlines/cr's.
20321da177e4SLinus Torvalds    $x =~ s@^\s+@@gos; # strip leading spaces
20331da177e4SLinus Torvalds    $x =~ s@\s+$@@gos; # strip trailing spaces
203451f5a0c8SRandy Dunlap    $x =~ s@\/\/.*$@@gos; # strip C99-style comments to end of line
203551f5a0c8SRandy Dunlap
20361da177e4SLinus Torvalds    if ($x =~ /^#/) {
20371da177e4SLinus Torvalds	# To distinguish preprocessor directive from regular declaration later.
20381da177e4SLinus Torvalds	$x .= ";";
20391da177e4SLinus Torvalds    }
20401da177e4SLinus Torvalds
20411da177e4SLinus Torvalds    while (1) {
2042673bb2dfSBen Hutchings	if ( $x =~ /([^\{\};]*)([\{\};])(.*)/ ) {
2043463a0fdcSMarkus Heiser            if( length $prototype ) {
2044463a0fdcSMarkus Heiser                $prototype .= " "
2045463a0fdcSMarkus Heiser            }
20461da177e4SLinus Torvalds	    $prototype .= $1 . $2;
20471da177e4SLinus Torvalds	    ($2 eq '{') && $brcount++;
20481da177e4SLinus Torvalds	    ($2 eq '}') && $brcount--;
20491da177e4SLinus Torvalds	    if (($2 eq ';') && ($brcount == 0)) {
20501da177e4SLinus Torvalds		dump_declaration($prototype, $file);
20511da177e4SLinus Torvalds		reset_state();
20521da177e4SLinus Torvalds		last;
20531da177e4SLinus Torvalds	    }
20541da177e4SLinus Torvalds	    $x = $3;
20551da177e4SLinus Torvalds	} else {
20561da177e4SLinus Torvalds	    $prototype .= $x;
20571da177e4SLinus Torvalds	    last;
20581da177e4SLinus Torvalds	}
20591da177e4SLinus Torvalds    }
20601da177e4SLinus Torvalds}
20611da177e4SLinus Torvalds
20626b5b55f6SRandy Dunlap
20631ad560e4SJani Nikulasub map_filename($) {
20641ad560e4SJani Nikula    my $file;
20651ad560e4SJani Nikula    my ($orig_file) = @_;
20661ad560e4SJani Nikula
20671ad560e4SJani Nikula    if (defined($ENV{'SRCTREE'})) {
20681ad560e4SJani Nikula	$file = "$ENV{'SRCTREE'}" . "/" . $orig_file;
20691ad560e4SJani Nikula    } else {
20701ad560e4SJani Nikula	$file = $orig_file;
20711ad560e4SJani Nikula    }
20721ad560e4SJani Nikula
20731ad560e4SJani Nikula    if (defined($source_map{$file})) {
20741ad560e4SJani Nikula	$file = $source_map{$file};
20751ad560e4SJani Nikula    }
20761ad560e4SJani Nikula
20771ad560e4SJani Nikula    return $file;
20781ad560e4SJani Nikula}
20791ad560e4SJani Nikula
208088c2b57dSJani Nikulasub process_export_file($) {
208188c2b57dSJani Nikula    my ($orig_file) = @_;
208288c2b57dSJani Nikula    my $file = map_filename($orig_file);
208388c2b57dSJani Nikula
208488c2b57dSJani Nikula    if (!open(IN,"<$file")) {
208588c2b57dSJani Nikula	print STDERR "Error: Cannot open file $file\n";
208688c2b57dSJani Nikula	++$errors;
208788c2b57dSJani Nikula	return;
208888c2b57dSJani Nikula    }
208988c2b57dSJani Nikula
209088c2b57dSJani Nikula    while (<IN>) {
209188c2b57dSJani Nikula	if (/$export_symbol/) {
2092eab795ddSMauro Carvalho Chehab	    next if (defined($nosymbol_table{$2}));
209388c2b57dSJani Nikula	    $function_table{$2} = 1;
209488c2b57dSJani Nikula	}
209588c2b57dSJani Nikula    }
209688c2b57dSJani Nikula
209788c2b57dSJani Nikula    close(IN);
209888c2b57dSJani Nikula}
209988c2b57dSJani Nikula
210007048d13SJonathan Corbet#
210107048d13SJonathan Corbet# Parsers for the various processing states.
210207048d13SJonathan Corbet#
210307048d13SJonathan Corbet# STATE_NORMAL: looking for the /** to begin everything.
210407048d13SJonathan Corbet#
210507048d13SJonathan Corbetsub process_normal() {
21061da177e4SLinus Torvalds    if (/$doc_start/o) {
210748af606aSJani Nikula	$state = STATE_NAME;	# next line is always the function name
2108850622dfSRandy Dunlap	$in_doc_sect = 0;
21090b0f5f29SDaniel Vetter	$declaration_start_line = $. + 1;
21101da177e4SLinus Torvalds    }
211107048d13SJonathan Corbet}
211207048d13SJonathan Corbet
21133cac2bc4SJonathan Corbet#
21143cac2bc4SJonathan Corbet# STATE_NAME: Looking for the "name - description" line
21153cac2bc4SJonathan Corbet#
21163cac2bc4SJonathan Corbetsub process_name($$) {
21173cac2bc4SJonathan Corbet    my $file = shift;
21181da177e4SLinus Torvalds    my $descr;
21191da177e4SLinus Torvalds
21201da177e4SLinus Torvalds    if (/$doc_block/o) {
212148af606aSJani Nikula	$state = STATE_DOCBLOCK;
21221da177e4SLinus Torvalds	$contents = "";
21235ef09c96SMauro Carvalho Chehab	$new_start_line = $.;
21240b0f5f29SDaniel Vetter
21251da177e4SLinus Torvalds	if ( $1 eq "" ) {
21261da177e4SLinus Torvalds	    $section = $section_intro;
21271da177e4SLinus Torvalds	} else {
21281da177e4SLinus Torvalds	    $section = $1;
21291da177e4SLinus Torvalds	}
213052042e2dSMauro Carvalho Chehab    } elsif (/$doc_decl/o) {
21311da177e4SLinus Torvalds	$identifier = $1;
21323e58e839SAditya Srivastava	my $is_kernel_comment = 0;
2133e86bdb24SAditya Srivastava	my $decl_start = qr{$doc_com};
2134f9bbc12cSAditya Srivastava	# test for pointer declaration type, foo * bar() - desc
2135f9bbc12cSAditya Srivastava	my $fn_type = qr{\w+\s*\*\s*};
2136f9bbc12cSAditya Srivastava	my $parenthesis = qr{\(\w*\)};
2137f9bbc12cSAditya Srivastava	my $decl_end = qr{[-:].*};
2138e86bdb24SAditya Srivastava	if (/^$decl_start([\w\s]+?)$parenthesis?\s*$decl_end?$/) {
21391da177e4SLinus Torvalds	    $identifier = $1;
21401da177e4SLinus Torvalds	}
214152042e2dSMauro Carvalho Chehab	if ($identifier =~ m/^(struct|union|enum|typedef)\b\s*(\S*)/) {
214252042e2dSMauro Carvalho Chehab	    $decl_type = $1;
214352042e2dSMauro Carvalho Chehab	    $identifier = $2;
21443e58e839SAditya Srivastava	    $is_kernel_comment = 1;
214552042e2dSMauro Carvalho Chehab	}
2146f9bbc12cSAditya Srivastava	# Look for foo() or static void foo() - description; or misspelt
2147f9bbc12cSAditya Srivastava	# identifier
2148e86bdb24SAditya Srivastava	elsif (/^$decl_start$fn_type?(\w+)\s*$parenthesis?\s*$decl_end?$/ ||
2149e86bdb24SAditya Srivastava	    /^$decl_start$fn_type?(\w+.*)$parenthesis?\s*$decl_end$/) {
2150f9bbc12cSAditya Srivastava	    $identifier = $1;
2151f9bbc12cSAditya Srivastava	    $decl_type = 'function';
2152f9bbc12cSAditya Srivastava	    $identifier =~ s/^define\s+//;
2153f9bbc12cSAditya Srivastava	    $is_kernel_comment = 1;
2154f9bbc12cSAditya Srivastava	}
215552042e2dSMauro Carvalho Chehab	$identifier =~ s/\s+$//;
21561da177e4SLinus Torvalds
215717b78717SJonathan Corbet	$state = STATE_BODY;
21580b0f5f29SDaniel Vetter	# if there's no @param blocks need to set up default section
21590b0f5f29SDaniel Vetter	# here
21602f4ad40aSJani Nikula	$contents = "";
21612f4ad40aSJani Nikula	$section = $section_default;
21620b0f5f29SDaniel Vetter	$new_start_line = $. + 1;
216352042e2dSMauro Carvalho Chehab	if (/[-:](.*)/) {
216451f5a0c8SRandy Dunlap	    # strip leading/trailing/multiple spaces
2165a21217daSRandy Dunlap	    $descr= $1;
2166a21217daSRandy Dunlap	    $descr =~ s/^\s*//;
2167a21217daSRandy Dunlap	    $descr =~ s/\s*$//;
216812ae6779SDaniel Santos	    $descr =~ s/\s+/ /g;
21690bba924cSJonathan Corbet	    $declaration_purpose = $descr;
217017b78717SJonathan Corbet	    $state = STATE_BODY_MAYBE;
21711da177e4SLinus Torvalds	} else {
21721da177e4SLinus Torvalds	    $declaration_purpose = "";
21731da177e4SLinus Torvalds	}
217477cc23b8SRandy Dunlap
21753e58e839SAditya Srivastava	if (!$is_kernel_comment) {
21763e58e839SAditya Srivastava	    print STDERR "${file}:$.: warning: This comment starts with '/**', but isn't a kernel-doc comment. Refer Documentation/doc-guide/kernel-doc.rst\n";
21773e58e839SAditya Srivastava	    print STDERR $_;
21783e58e839SAditya Srivastava	    ++$warnings;
21793e58e839SAditya Srivastava	    $state = STATE_NORMAL;
21803e58e839SAditya Srivastava	}
21813e58e839SAditya Srivastava
218277cc23b8SRandy Dunlap	if (($declaration_purpose eq "") && $verbose) {
2183d40e1e65SBart Van Assche	    print STDERR "${file}:$.: warning: missing initial short description on line:\n";
218477cc23b8SRandy Dunlap	    print STDERR $_;
218577cc23b8SRandy Dunlap	    ++$warnings;
218677cc23b8SRandy Dunlap	}
218777cc23b8SRandy Dunlap
21880b54c2e3SMauro Carvalho Chehab	if ($identifier eq "" && $decl_type ne "enum") {
218952042e2dSMauro Carvalho Chehab	    print STDERR "${file}:$.: warning: wrong kernel-doc identifier on line:\n";
219052042e2dSMauro Carvalho Chehab	    print STDERR $_;
219152042e2dSMauro Carvalho Chehab	    ++$warnings;
219252042e2dSMauro Carvalho Chehab	    $state = STATE_NORMAL;
21931da177e4SLinus Torvalds	}
21941da177e4SLinus Torvalds
21951da177e4SLinus Torvalds	if ($verbose) {
219652042e2dSMauro Carvalho Chehab	    print STDERR "${file}:$.: info: Scanning doc for $decl_type $identifier\n";
21971da177e4SLinus Torvalds	}
21981da177e4SLinus Torvalds    } else {
2199d40e1e65SBart Van Assche	print STDERR "${file}:$.: warning: Cannot understand $_ on line $.",
22001da177e4SLinus Torvalds	    " - I thought it was a doc line\n";
22011da177e4SLinus Torvalds	++$warnings;
220248af606aSJani Nikula	$state = STATE_NORMAL;
22031da177e4SLinus Torvalds    }
22043cac2bc4SJonathan Corbet}
22053cac2bc4SJonathan Corbet
22063cac2bc4SJonathan Corbet
2207d742f24dSJonathan Corbet#
2208d742f24dSJonathan Corbet# STATE_BODY and STATE_BODY_MAYBE: the bulk of a kerneldoc comment.
2209d742f24dSJonathan Corbet#
2210d742f24dSJonathan Corbetsub process_body($$) {
2211d742f24dSJonathan Corbet    my $file = shift;
22123cac2bc4SJonathan Corbet
221343756e34SJonathan Neuschäfer    # Until all named variable macro parameters are
221443756e34SJonathan Neuschäfer    # documented using the bare name (`x`) rather than with
221543756e34SJonathan Neuschäfer    # dots (`x...`), strip the dots:
221643756e34SJonathan Neuschäfer    if ($section =~ /\w\.\.\.$/) {
221743756e34SJonathan Neuschäfer	$section =~ s/\.\.\.$//;
221843756e34SJonathan Neuschäfer
221943756e34SJonathan Neuschäfer	if ($verbose) {
222043756e34SJonathan Neuschäfer	    print STDERR "${file}:$.: warning: Variable macro arguments should be documented without dots\n";
222143756e34SJonathan Neuschäfer	    ++$warnings;
222243756e34SJonathan Neuschäfer	}
222343756e34SJonathan Neuschäfer    }
222443756e34SJonathan Neuschäfer
22250d55d48bSMauro Carvalho Chehab    if ($state == STATE_BODY_WITH_BLANK_LINE && /^\s*\*\s?\S/) {
22260d55d48bSMauro Carvalho Chehab	dump_section($file, $section, $contents);
22270d55d48bSMauro Carvalho Chehab	$section = $section_default;
22285ef09c96SMauro Carvalho Chehab	$new_start_line = $.;
22290d55d48bSMauro Carvalho Chehab	$contents = "";
22300d55d48bSMauro Carvalho Chehab    }
22310d55d48bSMauro Carvalho Chehab
2232f624adefSJani Nikula    if (/$doc_sect/i) { # case insensitive for supported section names
22331da177e4SLinus Torvalds	$newsection = $1;
22341da177e4SLinus Torvalds	$newcontents = $2;
22351da177e4SLinus Torvalds
2236f624adefSJani Nikula	# map the supported section names to the canonical names
2237f624adefSJani Nikula	if ($newsection =~ m/^description$/i) {
2238f624adefSJani Nikula	    $newsection = $section_default;
2239f624adefSJani Nikula	} elsif ($newsection =~ m/^context$/i) {
2240f624adefSJani Nikula	    $newsection = $section_context;
2241f624adefSJani Nikula	} elsif ($newsection =~ m/^returns?$/i) {
2242f624adefSJani Nikula	    $newsection = $section_return;
2243f624adefSJani Nikula	} elsif ($newsection =~ m/^\@return$/) {
2244f624adefSJani Nikula	    # special: @return is a section, not a param description
2245f624adefSJani Nikula	    $newsection = $section_return;
2246f624adefSJani Nikula	}
2247f624adefSJani Nikula
2248792aa2f2SRandy Dunlap	if (($contents ne "") && ($contents ne "\n")) {
2249850622dfSRandy Dunlap	    if (!$in_doc_sect && $verbose) {
2250d40e1e65SBart Van Assche		print STDERR "${file}:$.: warning: contents before sections\n";
2251850622dfSRandy Dunlap		++$warnings;
2252850622dfSRandy Dunlap	    }
22530bba924cSJonathan Corbet	    dump_section($file, $section, $contents);
22541da177e4SLinus Torvalds	    $section = $section_default;
22551da177e4SLinus Torvalds	}
22561da177e4SLinus Torvalds
2257850622dfSRandy Dunlap	$in_doc_sect = 1;
225817b78717SJonathan Corbet	$state = STATE_BODY;
22591da177e4SLinus Torvalds	$contents = $newcontents;
22600b0f5f29SDaniel Vetter	$new_start_line = $.;
22617c9aa015SMauro Carvalho Chehab	while (substr($contents, 0, 1) eq " ") {
226205189497SRandy Dunlap	    $contents = substr($contents, 1);
226305189497SRandy Dunlap	}
22640a726301SJani Nikula	if ($contents ne "") {
22651da177e4SLinus Torvalds	    $contents .= "\n";
22661da177e4SLinus Torvalds	}
22671da177e4SLinus Torvalds	$section = $newsection;
2268b7886de4SJani Nikula	$leading_space = undef;
22691da177e4SLinus Torvalds    } elsif (/$doc_end/) {
22704c98ecafSRandy Dunlap	if (($contents ne "") && ($contents ne "\n")) {
22710bba924cSJonathan Corbet	    dump_section($file, $section, $contents);
22721da177e4SLinus Torvalds	    $section = $section_default;
22731da177e4SLinus Torvalds	    $contents = "";
22741da177e4SLinus Torvalds	}
227546b958ebSRandy Dunlap	# look for doc_com + <text> + doc_end:
227646b958ebSRandy Dunlap	if ($_ =~ m'\s*\*\s*[a-zA-Z_0-9:\.]+\*/') {
2277d40e1e65SBart Van Assche	    print STDERR "${file}:$.: warning: suspicious ending line: $_";
227846b958ebSRandy Dunlap	    ++$warnings;
227946b958ebSRandy Dunlap	}
22801da177e4SLinus Torvalds
22811da177e4SLinus Torvalds	$prototype = "";
228248af606aSJani Nikula	$state = STATE_PROTO;
22831da177e4SLinus Torvalds	$brcount = 0;
22845ef09c96SMauro Carvalho Chehab        $new_start_line = $. + 1;
22851da177e4SLinus Torvalds    } elsif (/$doc_content/) {
22866423133bSJohannes Weiner	if ($1 eq "") {
22870d55d48bSMauro Carvalho Chehab	    if ($section eq $section_context) {
22880bba924cSJonathan Corbet		dump_section($file, $section, $contents);
22891da177e4SLinus Torvalds		$section = $section_default;
22901da177e4SLinus Torvalds		$contents = "";
22910b0f5f29SDaniel Vetter		$new_start_line = $.;
22920d55d48bSMauro Carvalho Chehab		$state = STATE_BODY;
22931da177e4SLinus Torvalds	    } else {
22940d55d48bSMauro Carvalho Chehab		if ($section ne $section_default) {
22950d55d48bSMauro Carvalho Chehab		    $state = STATE_BODY_WITH_BLANK_LINE;
22960d55d48bSMauro Carvalho Chehab		} else {
22970d55d48bSMauro Carvalho Chehab		    $state = STATE_BODY;
22980d55d48bSMauro Carvalho Chehab		}
22996423133bSJohannes Weiner		$contents .= "\n";
23006423133bSJohannes Weiner	    }
230117b78717SJonathan Corbet	} elsif ($state == STATE_BODY_MAYBE) {
23026423133bSJohannes Weiner	    # Continued declaration purpose
23036423133bSJohannes Weiner	    chomp($declaration_purpose);
23040bba924cSJonathan Corbet	    $declaration_purpose .= " " . $1;
230512ae6779SDaniel Santos	    $declaration_purpose =~ s/\s+/ /g;
23066423133bSJohannes Weiner	} else {
2307b7886de4SJani Nikula	    my $cont = $1;
2308b7886de4SJani Nikula	    if ($section =~ m/^@/ || $section eq $section_context) {
2309b7886de4SJani Nikula		if (!defined $leading_space) {
2310b7886de4SJani Nikula		    if ($cont =~ m/^(\s+)/) {
2311b7886de4SJani Nikula			$leading_space = $1;
2312b7886de4SJani Nikula		    } else {
2313b7886de4SJani Nikula			$leading_space = "";
2314b7886de4SJani Nikula		    }
2315b7886de4SJani Nikula		}
2316b7886de4SJani Nikula		$cont =~ s/^$leading_space//;
2317b7886de4SJani Nikula	    }
2318b7886de4SJani Nikula	    $contents .= $cont . "\n";
23191da177e4SLinus Torvalds	}
23201da177e4SLinus Torvalds    } else {
23211da177e4SLinus Torvalds	# i dont know - bad line?  ignore.
2322d40e1e65SBart Van Assche	print STDERR "${file}:$.: warning: bad line: $_";
23231da177e4SLinus Torvalds	++$warnings;
23241da177e4SLinus Torvalds    }
2325d742f24dSJonathan Corbet}
2326d742f24dSJonathan Corbet
2327d742f24dSJonathan Corbet
2328cc794812SJonathan Corbet#
2329cc794812SJonathan Corbet# STATE_PROTO: reading a function/whatever prototype.
2330cc794812SJonathan Corbet#
2331cc794812SJonathan Corbetsub process_proto($$) {
2332cc794812SJonathan Corbet    my $file = shift;
2333cc794812SJonathan Corbet
2334cc794812SJonathan Corbet    if (/$doc_inline_oneline/) {
2335cc794812SJonathan Corbet	$section = $1;
2336cc794812SJonathan Corbet	$contents = $2;
2337cc794812SJonathan Corbet	if ($contents ne "") {
2338cc794812SJonathan Corbet	    $contents .= "\n";
2339cc794812SJonathan Corbet	    dump_section($file, $section, $contents);
2340cc794812SJonathan Corbet	    $section = $section_default;
2341cc794812SJonathan Corbet	    $contents = "";
2342cc794812SJonathan Corbet	}
2343cc794812SJonathan Corbet    } elsif (/$doc_inline_start/) {
2344cc794812SJonathan Corbet	$state = STATE_INLINE;
2345cc794812SJonathan Corbet	$inline_doc_state = STATE_INLINE_NAME;
2346cc794812SJonathan Corbet    } elsif ($decl_type eq 'function') {
2347cc794812SJonathan Corbet	process_proto_function($_, $file);
2348cc794812SJonathan Corbet    } else {
2349cc794812SJonathan Corbet	process_proto_type($_, $file);
2350cc794812SJonathan Corbet    }
2351cc794812SJonathan Corbet}
2352cc794812SJonathan Corbet
2353c17add56SJonathan Corbet#
2354c17add56SJonathan Corbet# STATE_DOCBLOCK: within a DOC: block.
2355c17add56SJonathan Corbet#
2356c17add56SJonathan Corbetsub process_docblock($$) {
2357c17add56SJonathan Corbet    my $file = shift;
2358cc794812SJonathan Corbet
2359c17add56SJonathan Corbet    if (/$doc_end/) {
2360c17add56SJonathan Corbet	dump_doc_section($file, $section, $contents);
2361c17add56SJonathan Corbet	$section = $section_default;
2362c17add56SJonathan Corbet	$contents = "";
2363c17add56SJonathan Corbet	$function = "";
2364c17add56SJonathan Corbet	%parameterdescs = ();
2365c17add56SJonathan Corbet	%parametertypes = ();
2366c17add56SJonathan Corbet	@parameterlist = ();
2367c17add56SJonathan Corbet	%sections = ();
2368c17add56SJonathan Corbet	@sectionlist = ();
2369c17add56SJonathan Corbet	$prototype = "";
2370c17add56SJonathan Corbet	$state = STATE_NORMAL;
2371c17add56SJonathan Corbet    } elsif (/$doc_content/) {
2372c17add56SJonathan Corbet	if ( $1 eq "" )	{
2373c17add56SJonathan Corbet	    $contents .= $blankline;
2374c17add56SJonathan Corbet	} else {
2375c17add56SJonathan Corbet	    $contents .= $1 . "\n";
2376c17add56SJonathan Corbet	}
2377c17add56SJonathan Corbet    }
2378d742f24dSJonathan Corbet}
2379d742f24dSJonathan Corbet
2380c17add56SJonathan Corbet#
2381c17add56SJonathan Corbet# STATE_INLINE: docbook comments within a prototype.
2382c17add56SJonathan Corbet#
2383c17add56SJonathan Corbetsub process_inline($$) {
2384c17add56SJonathan Corbet    my $file = shift;
2385d742f24dSJonathan Corbet
2386a4c6ebedSDanilo Cesar Lemes de Paula    # First line (state 1) needs to be a @parameter
238748af606aSJani Nikula    if ($inline_doc_state == STATE_INLINE_NAME && /$doc_inline_sect/o) {
2388a4c6ebedSDanilo Cesar Lemes de Paula	$section = $1;
2389a4c6ebedSDanilo Cesar Lemes de Paula	$contents = $2;
23900b0f5f29SDaniel Vetter	$new_start_line = $.;
2391a4c6ebedSDanilo Cesar Lemes de Paula	if ($contents ne "") {
23927c9aa015SMauro Carvalho Chehab	    while (substr($contents, 0, 1) eq " ") {
2393a4c6ebedSDanilo Cesar Lemes de Paula		$contents = substr($contents, 1);
2394a4c6ebedSDanilo Cesar Lemes de Paula	    }
2395a4c6ebedSDanilo Cesar Lemes de Paula	    $contents .= "\n";
2396a4c6ebedSDanilo Cesar Lemes de Paula	}
239748af606aSJani Nikula	$inline_doc_state = STATE_INLINE_TEXT;
2398a4c6ebedSDanilo Cesar Lemes de Paula	# Documentation block end */
239948af606aSJani Nikula    } elsif (/$doc_inline_end/) {
2400a4c6ebedSDanilo Cesar Lemes de Paula	if (($contents ne "") && ($contents ne "\n")) {
24010bba924cSJonathan Corbet	    dump_section($file, $section, $contents);
2402a4c6ebedSDanilo Cesar Lemes de Paula	    $section = $section_default;
2403a4c6ebedSDanilo Cesar Lemes de Paula	    $contents = "";
2404a4c6ebedSDanilo Cesar Lemes de Paula	}
240548af606aSJani Nikula	$state = STATE_PROTO;
240648af606aSJani Nikula	$inline_doc_state = STATE_INLINE_NA;
2407a4c6ebedSDanilo Cesar Lemes de Paula	# Regular text
2408a4c6ebedSDanilo Cesar Lemes de Paula    } elsif (/$doc_content/) {
240948af606aSJani Nikula	if ($inline_doc_state == STATE_INLINE_TEXT) {
2410a4c6ebedSDanilo Cesar Lemes de Paula	    $contents .= $1 . "\n";
24116450c895SJani Nikula	    # nuke leading blank lines
24126450c895SJani Nikula	    if ($contents =~ /^\s*$/) {
24136450c895SJani Nikula		$contents = "";
24146450c895SJani Nikula	    }
241548af606aSJani Nikula	} elsif ($inline_doc_state == STATE_INLINE_NAME) {
241648af606aSJani Nikula	    $inline_doc_state = STATE_INLINE_ERROR;
2417e7ca311eSDaniel Vetter	    print STDERR "${file}:$.: warning: ";
2418a4c6ebedSDanilo Cesar Lemes de Paula	    print STDERR "Incorrect use of kernel-doc format: $_";
2419a4c6ebedSDanilo Cesar Lemes de Paula	    ++$warnings;
2420a4c6ebedSDanilo Cesar Lemes de Paula	}
2421a4c6ebedSDanilo Cesar Lemes de Paula    }
24220c9aa209SJani Nikula}
2423c17add56SJonathan Corbet
2424c17add56SJonathan Corbet
2425c17add56SJonathan Corbetsub process_file($) {
2426c17add56SJonathan Corbet    my $file;
2427c17add56SJonathan Corbet    my $initial_section_counter = $section_counter;
2428c17add56SJonathan Corbet    my ($orig_file) = @_;
2429c17add56SJonathan Corbet
2430c17add56SJonathan Corbet    $file = map_filename($orig_file);
2431c17add56SJonathan Corbet
2432dbe8ba00SMauro Carvalho Chehab    if (!open(IN_FILE,"<$file")) {
2433c17add56SJonathan Corbet	print STDERR "Error: Cannot open file $file\n";
2434c17add56SJonathan Corbet	++$errors;
2435c17add56SJonathan Corbet	return;
24361da177e4SLinus Torvalds    }
2437c17add56SJonathan Corbet
2438c17add56SJonathan Corbet    $. = 1;
2439c17add56SJonathan Corbet
2440c17add56SJonathan Corbet    $section_counter = 0;
2441dbe8ba00SMauro Carvalho Chehab    while (<IN_FILE>) {
2442c17add56SJonathan Corbet	while (s/\\\s*$//) {
2443dbe8ba00SMauro Carvalho Chehab	    $_ .= <IN_FILE>;
2444c17add56SJonathan Corbet	}
2445c17add56SJonathan Corbet	# Replace tabs by spaces
2446c17add56SJonathan Corbet        while ($_ =~ s/\t+/' ' x (length($&) * 8 - length($`) % 8)/e) {};
2447c17add56SJonathan Corbet	# Hand this line to the appropriate state handler
2448c17add56SJonathan Corbet	if ($state == STATE_NORMAL) {
2449c17add56SJonathan Corbet	    process_normal();
2450c17add56SJonathan Corbet	} elsif ($state == STATE_NAME) {
2451c17add56SJonathan Corbet	    process_name($file, $_);
24520d55d48bSMauro Carvalho Chehab	} elsif ($state == STATE_BODY || $state == STATE_BODY_MAYBE ||
24530d55d48bSMauro Carvalho Chehab		 $state == STATE_BODY_WITH_BLANK_LINE) {
2454c17add56SJonathan Corbet	    process_body($file, $_);
2455c17add56SJonathan Corbet	} elsif ($state == STATE_INLINE) { # scanning for inline parameters
2456c17add56SJonathan Corbet	    process_inline($file, $_);
2457cc794812SJonathan Corbet	} elsif ($state == STATE_PROTO) {
2458cc794812SJonathan Corbet	    process_proto($file, $_);
245948af606aSJani Nikula	} elsif ($state == STATE_DOCBLOCK) {
2460c17add56SJonathan Corbet	    process_docblock($file, $_);
24611da177e4SLinus Torvalds	}
24621da177e4SLinus Torvalds    }
2463c17add56SJonathan Corbet
2464c17add56SJonathan Corbet    # Make sure we got something interesting.
2465b0d60bfbSJonathan Corbet    if ($initial_section_counter == $section_counter && $
2466b0d60bfbSJonathan Corbet	output_mode ne "none") {
2467b0d60bfbSJonathan Corbet	if ($output_selection == OUTPUT_INCLUDE) {
2468b0d60bfbSJonathan Corbet	    print STDERR "${file}:1: warning: '$_' not found\n"
2469b0d60bfbSJonathan Corbet		for keys %function_table;
24703a025e1dSMatthew Wilcox	}
2471b0d60bfbSJonathan Corbet	else {
2472b0d60bfbSJonathan Corbet	    print STDERR "${file}:1: warning: no structured comments found\n";
2473e946c43aSJohannes Berg	}
24741da177e4SLinus Torvalds    }
2475dbe8ba00SMauro Carvalho Chehab    close IN_FILE;
24761da177e4SLinus Torvalds}
24778484baaaSRandy Dunlap
24788484baaaSRandy Dunlap
247993351d41SMauro Carvalho Chehabif ($output_mode eq "rst") {
248093351d41SMauro Carvalho Chehab	get_sphinx_version() if (!$sphinx_major);
248193351d41SMauro Carvalho Chehab}
248293351d41SMauro Carvalho Chehab
24838484baaaSRandy Dunlap$kernelversion = get_kernel_version();
24848484baaaSRandy Dunlap
24858484baaaSRandy Dunlap# generate a sequence of code that will splice in highlighting information
24868484baaaSRandy Dunlap# using the s// operator.
24871ef06233SMauro Carvalho Chehabfor (my $k = 0; $k < @highlights; $k++) {
24884d732701SDanilo Cesar Lemes de Paula    my $pattern = $highlights[$k][0];
24894d732701SDanilo Cesar Lemes de Paula    my $result = $highlights[$k][1];
24904d732701SDanilo Cesar Lemes de Paula#   print STDERR "scanning pattern:$pattern, highlight:($result)\n";
24914d732701SDanilo Cesar Lemes de Paula    $dohighlight .=  "\$contents =~ s:$pattern:$result:gs;\n";
24928484baaaSRandy Dunlap}
24938484baaaSRandy Dunlap
24948484baaaSRandy Dunlap# Read the file that maps relative names to absolute names for
24958484baaaSRandy Dunlap# separate source and object directories and for shadow trees.
24968484baaaSRandy Dunlapif (open(SOURCE_MAP, "<.tmp_filelist.txt")) {
24978484baaaSRandy Dunlap	my ($relname, $absname);
24988484baaaSRandy Dunlap	while(<SOURCE_MAP>) {
24998484baaaSRandy Dunlap		chop();
25008484baaaSRandy Dunlap		($relname, $absname) = (split())[0..1];
25018484baaaSRandy Dunlap		$relname =~ s:^/+::;
25028484baaaSRandy Dunlap		$source_map{$relname} = $absname;
25038484baaaSRandy Dunlap	}
25048484baaaSRandy Dunlap	close(SOURCE_MAP);
25058484baaaSRandy Dunlap}
25068484baaaSRandy Dunlap
250788c2b57dSJani Nikulaif ($output_selection == OUTPUT_EXPORTED ||
250888c2b57dSJani Nikula    $output_selection == OUTPUT_INTERNAL) {
2509c9b2cfb3SJani Nikula
2510c9b2cfb3SJani Nikula    push(@export_file_list, @ARGV);
2511c9b2cfb3SJani Nikula
251288c2b57dSJani Nikula    foreach (@export_file_list) {
251388c2b57dSJani Nikula	chomp;
251488c2b57dSJani Nikula	process_export_file($_);
251588c2b57dSJani Nikula    }
251688c2b57dSJani Nikula}
251788c2b57dSJani Nikula
25188484baaaSRandy Dunlapforeach (@ARGV) {
25198484baaaSRandy Dunlap    chomp;
25208484baaaSRandy Dunlap    process_file($_);
25218484baaaSRandy Dunlap}
25228484baaaSRandy Dunlapif ($verbose && $errors) {
25238484baaaSRandy Dunlap  print STDERR "$errors errors\n";
25248484baaaSRandy Dunlap}
25258484baaaSRandy Dunlapif ($verbose && $warnings) {
25268484baaaSRandy Dunlap  print STDERR "$warnings warnings\n";
25278484baaaSRandy Dunlap}
25288484baaaSRandy Dunlap
25292c12c810SPierre-Louis Bossartif ($Werror && $warnings) {
25302c12c810SPierre-Louis Bossart    print STDERR "$warnings warnings as Errors\n";
25312c12c810SPierre-Louis Bossart    exit($warnings);
25322c12c810SPierre-Louis Bossart} else {
25332c12c810SPierre-Louis Bossart    exit($output_mode eq "none" ? 0 : $errors)
25342c12c810SPierre-Louis Bossart}
25352875f787STomasz Warniełło
25362875f787STomasz Warniełło__END__
25372875f787STomasz Warniełło
25382875f787STomasz Warniełło=head1 OPTIONS
25392875f787STomasz Warniełło
25402875f787STomasz Warniełło=head2 Output format selection (mutually exclusive):
25412875f787STomasz Warniełło
25422875f787STomasz Warniełło=over 8
25432875f787STomasz Warniełło
25442875f787STomasz Warniełło=item -man
25452875f787STomasz Warniełło
25462875f787STomasz WarniełłoOutput troff manual page format.
25472875f787STomasz Warniełło
25482875f787STomasz Warniełło=item -rst
25492875f787STomasz Warniełło
25502875f787STomasz WarniełłoOutput reStructuredText format. This is the default.
25512875f787STomasz Warniełło
25522875f787STomasz Warniełło=item -none
25532875f787STomasz Warniełło
25542875f787STomasz WarniełłoDo not output documentation, only warnings.
25552875f787STomasz Warniełło
25562875f787STomasz Warniełło=back
25572875f787STomasz Warniełło
2558dd803b04STomasz Warniełło=head2 Output format modifiers
2559dd803b04STomasz Warniełło
2560dd803b04STomasz Warniełło=head3 reStructuredText only
2561dd803b04STomasz Warniełło
2562dd803b04STomasz Warniełło=over 8
2563dd803b04STomasz Warniełło
2564dd803b04STomasz Warniełło=item -sphinx-version VERSION
2565dd803b04STomasz Warniełło
2566dd803b04STomasz WarniełłoUse the ReST C domain dialect compatible with a specific Sphinx Version.
2567dd803b04STomasz Warniełło
2568dd803b04STomasz WarniełłoIf not specified, kernel-doc will auto-detect using the sphinx-build version
2569dd803b04STomasz Warniełłofound on PATH.
2570dd803b04STomasz Warniełło
2571dd803b04STomasz Warniełło=back
2572dd803b04STomasz Warniełło
25739c77f108STomasz Warniełło=head2 Output selection (mutually exclusive):
25749c77f108STomasz Warniełło
25759c77f108STomasz Warniełło=over 8
25769c77f108STomasz Warniełło
25779c77f108STomasz Warniełło=item -export
25789c77f108STomasz Warniełło
25799c77f108STomasz WarniełłoOnly output documentation for the symbols that have been exported using
25809c77f108STomasz WarniełłoEXPORT_SYMBOL() or EXPORT_SYMBOL_GPL() in any input FILE or -export-file FILE.
25819c77f108STomasz Warniełło
25829c77f108STomasz Warniełło=item -internal
25839c77f108STomasz Warniełło
25849c77f108STomasz WarniełłoOnly output documentation for the symbols that have NOT been exported using
25859c77f108STomasz WarniełłoEXPORT_SYMBOL() or EXPORT_SYMBOL_GPL() in any input FILE or -export-file FILE.
25869c77f108STomasz Warniełło
25879c77f108STomasz Warniełło=item -function NAME
25889c77f108STomasz Warniełło
25899c77f108STomasz WarniełłoOnly output documentation for the given function or DOC: section title.
25909c77f108STomasz WarniełłoAll other functions and DOC: sections are ignored.
25919c77f108STomasz Warniełło
25929c77f108STomasz WarniełłoMay be specified multiple times.
25939c77f108STomasz Warniełło
25949c77f108STomasz Warniełło=item -nosymbol NAME
25959c77f108STomasz Warniełło
25969c77f108STomasz WarniełłoExclude the specified symbol from the output documentation.
25979c77f108STomasz Warniełło
25989c77f108STomasz WarniełłoMay be specified multiple times.
25999c77f108STomasz Warniełło
26009c77f108STomasz Warniełło=back
26019c77f108STomasz Warniełło
2602*c15de5a1STomasz Warniełło=head2 Output selection modifiers:
2603*c15de5a1STomasz Warniełło
2604*c15de5a1STomasz Warniełło=over 8
2605*c15de5a1STomasz Warniełło
2606*c15de5a1STomasz Warniełło=item -no-doc-sections
2607*c15de5a1STomasz Warniełło
2608*c15de5a1STomasz WarniełłoDo not output DOC: sections.
2609*c15de5a1STomasz Warniełło
2610*c15de5a1STomasz Warniełło=item -export-file FILE
2611*c15de5a1STomasz Warniełło
2612*c15de5a1STomasz WarniełłoSpecify an additional FILE in which to look for EXPORT_SYMBOL() and
2613*c15de5a1STomasz WarniełłoEXPORT_SYMBOL_GPL().
2614*c15de5a1STomasz Warniełło
2615*c15de5a1STomasz WarniełłoTo be used with -export or -internal.
2616*c15de5a1STomasz Warniełło
2617*c15de5a1STomasz WarniełłoMay be specified multiple times.
2618*c15de5a1STomasz Warniełło
2619*c15de5a1STomasz Warniełło=back
2620*c15de5a1STomasz Warniełło
2621*c15de5a1STomasz Warniełło=head3 reStructuredText only
2622*c15de5a1STomasz Warniełło
2623*c15de5a1STomasz Warniełło=over 8
2624*c15de5a1STomasz Warniełło
2625*c15de5a1STomasz Warniełło=item -enable-lineno
2626*c15de5a1STomasz Warniełło
2627*c15de5a1STomasz WarniełłoEnable output of #define LINENO lines.
2628*c15de5a1STomasz Warniełło
2629*c15de5a1STomasz Warniełło=back
2630*c15de5a1STomasz Warniełło
26312875f787STomasz Warniełło=cut
2632