xref: /linux/scripts/kernel-doc (revision 0d55d48b19ff3a31b295836da23223a52de7ef06)
1cb77f0d6SKamil Rytarowski#!/usr/bin/env perl
238476378SJonathan Corbet# SPDX-License-Identifier: GPL-2.0
31da177e4SLinus Torvalds
4cb77f0d6SKamil Rytarowskiuse warnings;
51da177e4SLinus Torvaldsuse strict;
61da177e4SLinus Torvalds
71da177e4SLinus Torvalds## Copyright (c) 1998 Michael Zucchi, All Rights Reserved        ##
81da177e4SLinus Torvalds## Copyright (C) 2000, 1  Tim Waugh <twaugh@redhat.com>          ##
91da177e4SLinus Torvalds## Copyright (C) 2001  Simon Huggins                             ##
1070c95b00SRandy Dunlap## Copyright (C) 2005-2012  Randy Dunlap                         ##
111b40c194SDan Luedtke## Copyright (C) 2012  Dan Luedtke                               ##
121da177e4SLinus Torvalds## 								 ##
131da177e4SLinus Torvalds## #define enhancements by Armin Kuster <akuster@mvista.com>	 ##
141da177e4SLinus Torvalds## Copyright (c) 2000 MontaVista Software, Inc.			 ##
151da177e4SLinus Torvalds## 								 ##
161da177e4SLinus Torvalds## This software falls under the GNU General Public License.     ##
171da177e4SLinus Torvalds## Please read the COPYING file for more information             ##
181da177e4SLinus Torvalds
191da177e4SLinus Torvalds# 18/01/2001 - 	Cleanups
201da177e4SLinus Torvalds# 		Functions prototyped as foo(void) same as foo()
211da177e4SLinus Torvalds# 		Stop eval'ing where we don't need to.
221da177e4SLinus Torvalds# -- huggie@earth.li
231da177e4SLinus Torvalds
241da177e4SLinus Torvalds# 27/06/2001 -  Allowed whitespace after initial "/**" and
251da177e4SLinus Torvalds#               allowed comments before function declarations.
261da177e4SLinus Torvalds# -- Christian Kreibich <ck@whoop.org>
271da177e4SLinus Torvalds
281da177e4SLinus Torvalds# Still to do:
291da177e4SLinus Torvalds# 	- add perldoc documentation
301da177e4SLinus Torvalds# 	- Look more closely at some of the scarier bits :)
311da177e4SLinus Torvalds
321da177e4SLinus Torvalds# 26/05/2001 - 	Support for separate source and object trees.
331da177e4SLinus Torvalds#		Return error code.
341da177e4SLinus Torvalds# 		Keith Owens <kaos@ocs.com.au>
351da177e4SLinus Torvalds
361da177e4SLinus Torvalds# 23/09/2001 - Added support for typedefs, structs, enums and unions
371da177e4SLinus Torvalds#              Support for Context section; can be terminated using empty line
381da177e4SLinus Torvalds#              Small fixes (like spaces vs. \s in regex)
391da177e4SLinus Torvalds# -- Tim Jansen <tim@tjansen.de>
401da177e4SLinus Torvalds
411b40c194SDan Luedtke# 25/07/2012 - Added support for HTML5
421b40c194SDan Luedtke# -- Dan Luedtke <mail@danrl.de>
431da177e4SLinus Torvalds
44fadc0b31SJani Nikulasub usage {
45fadc0b31SJani Nikula    my $message = <<"EOF";
46fadc0b31SJani NikulaUsage: $0 [OPTION ...] FILE ...
471da177e4SLinus Torvalds
48fadc0b31SJani NikulaRead C language source or header FILEs, extract embedded documentation comments,
49fadc0b31SJani Nikulaand print formatted documentation to standard output.
501da177e4SLinus Torvalds
51fadc0b31SJani NikulaThe documentation comments are identified by "/**" opening comment mark. See
52857af3b7SMauro Carvalho ChehabDocumentation/doc-guide/kernel-doc.rst for the documentation comment syntax.
53fadc0b31SJani Nikula
54fadc0b31SJani NikulaOutput format selection (mutually exclusive):
55fadc0b31SJani Nikula  -man			Output troff manual page format. This is the default.
56c0d1b6eeSJonathan Corbet  -rst			Output reStructuredText format.
573a025e1dSMatthew Wilcox  -none			Do not output documentation, only warnings.
58fadc0b31SJani Nikula
59fadc0b31SJani NikulaOutput selection (mutually exclusive):
6086ae2e38SJani Nikula  -export		Only output documentation for symbols that have been
6186ae2e38SJani Nikula			exported using EXPORT_SYMBOL() or EXPORT_SYMBOL_GPL()
62c9b2cfb3SJani Nikula                        in any input FILE or -export-file FILE.
6386ae2e38SJani Nikula  -internal		Only output documentation for symbols that have NOT been
6486ae2e38SJani Nikula			exported using EXPORT_SYMBOL() or EXPORT_SYMBOL_GPL()
65c9b2cfb3SJani Nikula                        in any input FILE or -export-file FILE.
66fadc0b31SJani Nikula  -function NAME	Only output documentation for the given function(s)
67fadc0b31SJani Nikula			or DOC: section title(s). All other functions and DOC:
68fadc0b31SJani Nikula			sections are ignored. May be specified multiple times.
69fadc0b31SJani Nikula  -nofunction NAME	Do NOT output documentation for the given function(s);
70fadc0b31SJani Nikula			only output documentation for the other functions and
71fadc0b31SJani Nikula			DOC: sections. May be specified multiple times.
72fadc0b31SJani Nikula
73fadc0b31SJani NikulaOutput selection modifiers:
74fadc0b31SJani Nikula  -no-doc-sections	Do not output DOC: sections.
750b0f5f29SDaniel Vetter  -enable-lineno        Enable output of #define LINENO lines. Only works with
760b0f5f29SDaniel Vetter                        reStructuredText format.
7788c2b57dSJani Nikula  -export-file FILE     Specify an additional FILE in which to look for
7888c2b57dSJani Nikula                        EXPORT_SYMBOL() and EXPORT_SYMBOL_GPL(). To be used with
7988c2b57dSJani Nikula                        -export or -internal. May be specified multiple times.
80fadc0b31SJani Nikula
81fadc0b31SJani NikulaOther parameters:
82fadc0b31SJani Nikula  -v			Verbose output, more warnings and other information.
83fadc0b31SJani Nikula  -h			Print this help.
84fadc0b31SJani Nikula
85fadc0b31SJani NikulaEOF
86fadc0b31SJani Nikula    print $message;
87fadc0b31SJani Nikula    exit 1;
88fadc0b31SJani Nikula}
891da177e4SLinus Torvalds
901da177e4SLinus Torvalds#
911da177e4SLinus Torvalds# format of comments.
921da177e4SLinus Torvalds# In the following table, (...)? signifies optional structure.
931da177e4SLinus Torvalds#                         (...)* signifies 0 or more structure elements
941da177e4SLinus Torvalds# /**
951da177e4SLinus Torvalds#  * function_name(:)? (- short description)?
961da177e4SLinus Torvalds# (* @parameterx: (description of parameter x)?)*
971da177e4SLinus Torvalds# (* a blank line)?
981da177e4SLinus Torvalds#  * (Description:)? (Description of function)?
991da177e4SLinus Torvalds#  * (section header: (section description)? )*
1001da177e4SLinus Torvalds#  (*)?*/
1011da177e4SLinus Torvalds#
1021da177e4SLinus Torvalds# So .. the trivial example would be:
1031da177e4SLinus Torvalds#
1041da177e4SLinus Torvalds# /**
1051da177e4SLinus Torvalds#  * my_function
106b9d97328SRandy Dunlap#  */
1071da177e4SLinus Torvalds#
108891dcd2fSRandy Dunlap# If the Description: header tag is omitted, then there must be a blank line
1091da177e4SLinus Torvalds# after the last parameter specification.
1101da177e4SLinus Torvalds# e.g.
1111da177e4SLinus Torvalds# /**
1121da177e4SLinus Torvalds#  * my_function - does my stuff
1131da177e4SLinus Torvalds#  * @my_arg: its mine damnit
1141da177e4SLinus Torvalds#  *
1151da177e4SLinus Torvalds#  * Does my stuff explained.
1161da177e4SLinus Torvalds#  */
1171da177e4SLinus Torvalds#
1181da177e4SLinus Torvalds#  or, could also use:
1191da177e4SLinus Torvalds# /**
1201da177e4SLinus Torvalds#  * my_function - does my stuff
1211da177e4SLinus Torvalds#  * @my_arg: its mine damnit
1221da177e4SLinus Torvalds#  * Description: Does my stuff explained.
1231da177e4SLinus Torvalds#  */
1241da177e4SLinus Torvalds# etc.
1251da177e4SLinus Torvalds#
126b9d97328SRandy Dunlap# Besides functions you can also write documentation for structs, unions,
1271da177e4SLinus Torvalds# enums and typedefs. Instead of the function name you must write the name
1281da177e4SLinus Torvalds# of the declaration;  the struct/union/enum/typedef must always precede
1291da177e4SLinus Torvalds# the name. Nesting of declarations is not supported.
1301da177e4SLinus Torvalds# Use the argument mechanism to document members or constants.
1311da177e4SLinus Torvalds# e.g.
1321da177e4SLinus Torvalds# /**
1331da177e4SLinus Torvalds#  * struct my_struct - short description
1341da177e4SLinus Torvalds#  * @a: first member
1351da177e4SLinus Torvalds#  * @b: second member
1361da177e4SLinus Torvalds#  *
1371da177e4SLinus Torvalds#  * Longer description
1381da177e4SLinus Torvalds#  */
1391da177e4SLinus Torvalds# struct my_struct {
1401da177e4SLinus Torvalds#     int a;
1411da177e4SLinus Torvalds#     int b;
142aeec46b9SMartin Waitz# /* private: */
143aeec46b9SMartin Waitz#     int c;
1441da177e4SLinus Torvalds# };
1451da177e4SLinus Torvalds#
1461da177e4SLinus Torvalds# All descriptions can be multiline, except the short function description.
1471da177e4SLinus Torvalds#
148a4c6ebedSDanilo Cesar Lemes de Paula# For really longs structs, you can also describe arguments inside the
149a4c6ebedSDanilo Cesar Lemes de Paula# body of the struct.
150a4c6ebedSDanilo Cesar Lemes de Paula# eg.
151a4c6ebedSDanilo Cesar Lemes de Paula# /**
152a4c6ebedSDanilo Cesar Lemes de Paula#  * struct my_struct - short description
153a4c6ebedSDanilo Cesar Lemes de Paula#  * @a: first member
154a4c6ebedSDanilo Cesar Lemes de Paula#  * @b: second member
155a4c6ebedSDanilo Cesar Lemes de Paula#  *
156a4c6ebedSDanilo Cesar Lemes de Paula#  * Longer description
157a4c6ebedSDanilo Cesar Lemes de Paula#  */
158a4c6ebedSDanilo Cesar Lemes de Paula# struct my_struct {
159a4c6ebedSDanilo Cesar Lemes de Paula#     int a;
160a4c6ebedSDanilo Cesar Lemes de Paula#     int b;
161a4c6ebedSDanilo Cesar Lemes de Paula#     /**
162a4c6ebedSDanilo Cesar Lemes de Paula#      * @c: This is longer description of C
163a4c6ebedSDanilo Cesar Lemes de Paula#      *
164a4c6ebedSDanilo Cesar Lemes de Paula#      * You can use paragraphs to describe arguments
165a4c6ebedSDanilo Cesar Lemes de Paula#      * using this method.
166a4c6ebedSDanilo Cesar Lemes de Paula#      */
167a4c6ebedSDanilo Cesar Lemes de Paula#     int c;
168a4c6ebedSDanilo Cesar Lemes de Paula# };
169a4c6ebedSDanilo Cesar Lemes de Paula#
170a4c6ebedSDanilo Cesar Lemes de Paula# This should be use only for struct/enum members.
171a4c6ebedSDanilo Cesar Lemes de Paula#
1721da177e4SLinus Torvalds# You can also add additional sections. When documenting kernel functions you
1731da177e4SLinus Torvalds# should document the "Context:" of the function, e.g. whether the functions
1741da177e4SLinus Torvalds# can be called form interrupts. Unlike other sections you can end it with an
1751da177e4SLinus Torvalds# empty line.
1764092bac7SYacine Belkadi# A non-void function should have a "Return:" section describing the return
1774092bac7SYacine Belkadi# value(s).
1781da177e4SLinus Torvalds# Example-sections should contain the string EXAMPLE so that they are marked
1791da177e4SLinus Torvalds# appropriately in DocBook.
1801da177e4SLinus Torvalds#
1811da177e4SLinus Torvalds# Example:
1821da177e4SLinus Torvalds# /**
1831da177e4SLinus Torvalds#  * user_function - function that can only be called in user context
1841da177e4SLinus Torvalds#  * @a: some argument
1851da177e4SLinus Torvalds#  * Context: !in_interrupt()
1861da177e4SLinus Torvalds#  *
1871da177e4SLinus Torvalds#  * Some description
1881da177e4SLinus Torvalds#  * Example:
1891da177e4SLinus Torvalds#  *    user_function(22);
1901da177e4SLinus Torvalds#  */
1911da177e4SLinus Torvalds# ...
1921da177e4SLinus Torvalds#
1931da177e4SLinus Torvalds#
1941da177e4SLinus Torvalds# All descriptive text is further processed, scanning for the following special
1951da177e4SLinus Torvalds# patterns, which are highlighted appropriately.
1961da177e4SLinus Torvalds#
1971da177e4SLinus Torvalds# 'funcname()' - function
1981da177e4SLinus Torvalds# '$ENVVAR' - environmental variable
1991da177e4SLinus Torvalds# '&struct_name' - name of a structure (up to two words including 'struct')
2005267dd35SPaolo Bonzini# '&struct_name.member' - name of a structure member
2011da177e4SLinus Torvalds# '@parameter' - name of a parameter
2021da177e4SLinus Torvalds# '%CONST' - name of a constant.
203b97f193aSMauro Carvalho Chehab# '``LITERAL``' - literal string without any spaces on it.
2041da177e4SLinus Torvalds
2058484baaaSRandy Dunlap## init lots of data
2068484baaaSRandy Dunlap
2071da177e4SLinus Torvaldsmy $errors = 0;
2081da177e4SLinus Torvaldsmy $warnings = 0;
2095f8c7c98SRandy Dunlapmy $anon_struct_union = 0;
2101da177e4SLinus Torvalds
2111da177e4SLinus Torvalds# match expressions used to find embedded type information
212b97f193aSMauro Carvalho Chehabmy $type_constant = '\b``([^\`]+)``\b';
213b97f193aSMauro Carvalho Chehabmy $type_constant2 = '\%([-_\w]+)';
2141da177e4SLinus Torvaldsmy $type_func = '(\w+)\(\)';
215bfd228c7SMike Rapoportmy $type_param = '\@(\w*((\.\w+)|(->\w+))*(\.\.\.)?)';
216ee2aa759SMauro Carvalho Chehabmy $type_param_ref = '([\!]?)\@(\w*((\.\w+)|(->\w+))*(\.\.\.)?)';
2175219f18aSJonathan Corbetmy $type_fp_param = '\@(\w+)\(\)';  # Special RST handling for func ptr params
218346282dbSMauro Carvalho Chehabmy $type_fp_param2 = '\@(\w+->\S+)\(\)';  # Special RST handling for structs with func ptr params
2191da177e4SLinus Torvaldsmy $type_env = '(\$\w+)';
220df31175bSPaolo Bonzinimy $type_enum = '\&(enum\s*([_\w]+))';
221df31175bSPaolo Bonzinimy $type_struct = '\&(struct\s*([_\w]+))';
222df31175bSPaolo Bonzinimy $type_typedef = '\&(typedef\s*([_\w]+))';
223df31175bSPaolo Bonzinimy $type_union = '\&(union\s*([_\w]+))';
2245267dd35SPaolo Bonzinimy $type_member = '\&([_\w]+)(\.|->)([_\w]+)';
225df31175bSPaolo Bonzinimy $type_fallback = '\&([_\w]+)';
226f3341dcfSJani Nikulamy $type_member_func = $type_member . '\(\)';
2271da177e4SLinus Torvalds
2281da177e4SLinus Torvalds# Output conversion substitutions.
2291da177e4SLinus Torvalds#  One for each output format
2301da177e4SLinus Torvalds
2311da177e4SLinus Torvalds# these are pretty rough
2324d732701SDanilo Cesar Lemes de Paulamy @highlights_man = (
2334d732701SDanilo Cesar Lemes de Paula                      [$type_constant, "\$1"],
234b97f193aSMauro Carvalho Chehab                      [$type_constant2, "\$1"],
2354d732701SDanilo Cesar Lemes de Paula                      [$type_func, "\\\\fB\$1\\\\fP"],
236df31175bSPaolo Bonzini                      [$type_enum, "\\\\fI\$1\\\\fP"],
2374d732701SDanilo Cesar Lemes de Paula                      [$type_struct, "\\\\fI\$1\\\\fP"],
238df31175bSPaolo Bonzini                      [$type_typedef, "\\\\fI\$1\\\\fP"],
239df31175bSPaolo Bonzini                      [$type_union, "\\\\fI\$1\\\\fP"],
2405267dd35SPaolo Bonzini                      [$type_param, "\\\\fI\$1\\\\fP"],
241ee2aa759SMauro Carvalho Chehab                      [$type_param_ref, "\\\\fI\$1\$2\\\\fP"],
242df31175bSPaolo Bonzini                      [$type_member, "\\\\fI\$1\$2\$3\\\\fP"],
243df31175bSPaolo Bonzini                      [$type_fallback, "\\\\fI\$1\\\\fP"]
2444d732701SDanilo Cesar Lemes de Paula		     );
2451da177e4SLinus Torvaldsmy $blankline_man = "";
2461da177e4SLinus Torvalds
247c0d1b6eeSJonathan Corbet# rst-mode
248c0d1b6eeSJonathan Corbetmy @highlights_rst = (
249c0d1b6eeSJonathan Corbet                       [$type_constant, "``\$1``"],
250b97f193aSMauro Carvalho Chehab                       [$type_constant2, "``\$1``"],
251f3341dcfSJani Nikula                       # Note: need to escape () to avoid func matching later
2525267dd35SPaolo Bonzini                       [$type_member_func, "\\:c\\:type\\:`\$1\$2\$3\\\\(\\\\) <\$1>`"],
2535267dd35SPaolo Bonzini                       [$type_member, "\\:c\\:type\\:`\$1\$2\$3 <\$1>`"],
2545219f18aSJonathan Corbet		       [$type_fp_param, "**\$1\\\\(\\\\)**"],
255346282dbSMauro Carvalho Chehab		       [$type_fp_param2, "**\$1\\\\(\\\\)**"],
256344fdb28SJonathan Corbet                       [$type_func, "\$1()"],
257df31175bSPaolo Bonzini                       [$type_enum, "\\:c\\:type\\:`\$1 <\$2>`"],
258df31175bSPaolo Bonzini                       [$type_struct, "\\:c\\:type\\:`\$1 <\$2>`"],
259df31175bSPaolo Bonzini                       [$type_typedef, "\\:c\\:type\\:`\$1 <\$2>`"],
260df31175bSPaolo Bonzini                       [$type_union, "\\:c\\:type\\:`\$1 <\$2>`"],
261a7291e7eSJani Nikula                       # in rst this can refer to any type
262df31175bSPaolo Bonzini                       [$type_fallback, "\\:c\\:type\\:`\$1`"],
263ee2aa759SMauro Carvalho Chehab                       [$type_param_ref, "**\$1\$2**"]
264c0d1b6eeSJonathan Corbet		      );
265c0d1b6eeSJonathan Corbetmy $blankline_rst = "\n";
266c0d1b6eeSJonathan Corbet
2671da177e4SLinus Torvalds# read arguments
2681da177e4SLinus Torvaldsif ($#ARGV == -1) {
2691da177e4SLinus Torvalds    usage();
2701da177e4SLinus Torvalds}
2711da177e4SLinus Torvalds
2728484baaaSRandy Dunlapmy $kernelversion;
2738484baaaSRandy Dunlapmy $dohighlight = "";
2748484baaaSRandy Dunlap
2751da177e4SLinus Torvaldsmy $verbose = 0;
276bdfe2be3SMauro Carvalho Chehabmy $output_mode = "rst";
277e314ba31SDaniel Santosmy $output_preformatted = 0;
2784b44595aSJohannes Bergmy $no_doc_sections = 0;
2790b0f5f29SDaniel Vettermy $enable_lineno = 0;
280bdfe2be3SMauro Carvalho Chehabmy @highlights = @highlights_rst;
281bdfe2be3SMauro Carvalho Chehabmy $blankline = $blankline_rst;
2821da177e4SLinus Torvaldsmy $modulename = "Kernel API";
283b6c3f456SJani Nikula
284b6c3f456SJani Nikulause constant {
285b6c3f456SJani Nikula    OUTPUT_ALL          => 0, # output all symbols and doc sections
286b6c3f456SJani Nikula    OUTPUT_INCLUDE      => 1, # output only specified symbols
287b6c3f456SJani Nikula    OUTPUT_EXCLUDE      => 2, # output everything except specified symbols
288b6c3f456SJani Nikula    OUTPUT_EXPORTED     => 3, # output exported symbols
289b6c3f456SJani Nikula    OUTPUT_INTERNAL     => 4, # output non-exported symbols
290b6c3f456SJani Nikula};
291b6c3f456SJani Nikulamy $output_selection = OUTPUT_ALL;
292b0d60bfbSJonathan Corbetmy $show_not_found = 0;	# No longer used
293b2c4105bSBen Hutchings
29488c2b57dSJani Nikulamy @export_file_list;
29588c2b57dSJani Nikula
296b2c4105bSBen Hutchingsmy @build_time;
297b2c4105bSBen Hutchingsif (defined($ENV{'KBUILD_BUILD_TIMESTAMP'}) &&
298b2c4105bSBen Hutchings    (my $seconds = `date -d"${ENV{'KBUILD_BUILD_TIMESTAMP'}}" +%s`) ne '') {
299b2c4105bSBen Hutchings    @build_time = gmtime($seconds);
300b2c4105bSBen Hutchings} else {
301b2c4105bSBen Hutchings    @build_time = localtime;
302b2c4105bSBen Hutchings}
303b2c4105bSBen Hutchings
3041da177e4SLinus Torvaldsmy $man_date = ('January', 'February', 'March', 'April', 'May', 'June',
3051da177e4SLinus Torvalds		'July', 'August', 'September', 'October',
306b2c4105bSBen Hutchings		'November', 'December')[$build_time[4]] .
307b2c4105bSBen Hutchings  " " . ($build_time[5]+1900);
3081da177e4SLinus Torvalds
3098484baaaSRandy Dunlap# Essentially these are globals.
310b9d97328SRandy Dunlap# They probably want to be tidied up, made more localised or something.
311b9d97328SRandy Dunlap# CAVEAT EMPTOR!  Some of the others I localised may not want to be, which
3121da177e4SLinus Torvalds# could cause "use of undefined value" or other bugs.
3131da177e4SLinus Torvaldsmy ($function, %function_table, %parametertypes, $declaration_purpose);
3140b0f5f29SDaniel Vettermy $declaration_start_line;
3151da177e4SLinus Torvaldsmy ($type, $declaration_name, $return_type);
3161c32fd0cSIlya Dryomovmy ($newsection, $newcontents, $prototype, $brcount, %source_map);
3171da177e4SLinus Torvalds
318bd0e88e5SRandy Dunlapif (defined($ENV{'KBUILD_VERBOSE'})) {
319bd0e88e5SRandy Dunlap	$verbose = "$ENV{'KBUILD_VERBOSE'}";
320bd0e88e5SRandy Dunlap}
321bd0e88e5SRandy Dunlap
3221da177e4SLinus Torvalds# Generated docbook code is inserted in a template at a point where
3231da177e4SLinus Torvalds# docbook v3.1 requires a non-zero sequence of RefEntry's; see:
3241da177e4SLinus Torvalds# http://www.oasis-open.org/docbook/documentation/reference/html/refentry.html
3251da177e4SLinus Torvalds# We keep track of number of generated entries and generate a dummy
3261da177e4SLinus Torvalds# if needs be to ensure the expanded template can be postprocessed
3271da177e4SLinus Torvalds# into html.
3281da177e4SLinus Torvaldsmy $section_counter = 0;
3291da177e4SLinus Torvalds
3301da177e4SLinus Torvaldsmy $lineprefix="";
3311da177e4SLinus Torvalds
33248af606aSJani Nikula# Parser states
33348af606aSJani Nikulause constant {
33448af606aSJani Nikula    STATE_NORMAL        => 0,        # normal code
33548af606aSJani Nikula    STATE_NAME          => 1,        # looking for function name
33617b78717SJonathan Corbet    STATE_BODY_MAYBE    => 2,        # body - or maybe more description
33717b78717SJonathan Corbet    STATE_BODY          => 3,        # the body of the comment
338*0d55d48bSMauro Carvalho Chehab    STATE_BODY_WITH_BLANK_LINE => 4, # the body, which has a blank line
339*0d55d48bSMauro Carvalho Chehab    STATE_PROTO         => 5,        # scanning prototype
340*0d55d48bSMauro Carvalho Chehab    STATE_DOCBLOCK      => 6,        # documentation block
341*0d55d48bSMauro Carvalho Chehab    STATE_INLINE        => 7,        # gathering doc outside main block
34248af606aSJani Nikula};
3431da177e4SLinus Torvaldsmy $state;
344850622dfSRandy Dunlapmy $in_doc_sect;
345d742f24dSJonathan Corbetmy $leading_space;
3461da177e4SLinus Torvalds
34748af606aSJani Nikula# Inline documentation state
34848af606aSJani Nikulause constant {
34948af606aSJani Nikula    STATE_INLINE_NA     => 0, # not applicable ($state != STATE_INLINE)
35048af606aSJani Nikula    STATE_INLINE_NAME   => 1, # looking for member name (@foo:)
35148af606aSJani Nikula    STATE_INLINE_TEXT   => 2, # looking for member documentation
35248af606aSJani Nikula    STATE_INLINE_END    => 3, # done
35348af606aSJani Nikula    STATE_INLINE_ERROR  => 4, # error - Comment without header was found.
35448af606aSJani Nikula                              # Spit a warning as it's not
355a4c6ebedSDanilo Cesar Lemes de Paula                              # proper kernel-doc and ignore the rest.
35648af606aSJani Nikula};
35748af606aSJani Nikulamy $inline_doc_state;
358a4c6ebedSDanilo Cesar Lemes de Paula
3591da177e4SLinus Torvalds#declaration types: can be
3601da177e4SLinus Torvalds# 'function', 'struct', 'union', 'enum', 'typedef'
3611da177e4SLinus Torvaldsmy $decl_type;
3621da177e4SLinus Torvalds
3631da177e4SLinus Torvaldsmy $doc_start = '^/\*\*\s*$'; # Allow whitespace at end of comment start.
3641da177e4SLinus Torvaldsmy $doc_end = '\*/';
3651da177e4SLinus Torvaldsmy $doc_com = '\s*\*\s*';
36612ae6779SDaniel Santosmy $doc_com_body = '\s*\* ?';
3671da177e4SLinus Torvaldsmy $doc_decl = $doc_com . '(\w+)';
368f624adefSJani Nikula# @params and a strictly limited set of supported section names
3698569de68SJonathan Corbetmy $doc_sect = $doc_com .
370ef00028bSJonathan Corbet    '\s*(\@[.\w]+|\@\.\.\.|description|context|returns?|notes?|examples?)\s*:(.*)';
37112ae6779SDaniel Santosmy $doc_content = $doc_com_body . '(.*)';
3721da177e4SLinus Torvaldsmy $doc_block = $doc_com . 'DOC:\s*(.*)?';
37348af606aSJani Nikulamy $doc_inline_start = '^\s*/\*\*\s*$';
374fe7bc493SMauro Carvalho Chehabmy $doc_inline_sect = '\s*\*\s*(@\s*[\w][\w\.]*\s*):(.*)';
37548af606aSJani Nikulamy $doc_inline_end = '^\s*\*/\s*$';
3760c9aa209SJani Nikulamy $doc_inline_oneline = '^\s*/\*\*\s*(@[\w\s]+):\s*(.*)\s*\*/\s*$';
37786ae2e38SJani Nikulamy $export_symbol = '^\s*EXPORT_SYMBOL(_GPL)?\s*\(\s*(\w+)\s*\)\s*;';
3781da177e4SLinus Torvalds
3791da177e4SLinus Torvaldsmy %parameterdescs;
3800b0f5f29SDaniel Vettermy %parameterdesc_start_lines;
3811da177e4SLinus Torvaldsmy @parameterlist;
3821da177e4SLinus Torvaldsmy %sections;
3831da177e4SLinus Torvaldsmy @sectionlist;
3840b0f5f29SDaniel Vettermy %section_start_lines;
385a1d94aa5SRandy Dunlapmy $sectcheck;
386a1d94aa5SRandy Dunlapmy $struct_actual;
3871da177e4SLinus Torvalds
3881da177e4SLinus Torvaldsmy $contents = "";
3890b0f5f29SDaniel Vettermy $new_start_line = 0;
390f624adefSJani Nikula
391f624adefSJani Nikula# the canonical section names. see also $doc_sect above.
3921da177e4SLinus Torvaldsmy $section_default = "Description";	# default section
3931da177e4SLinus Torvaldsmy $section_intro = "Introduction";
3941da177e4SLinus Torvaldsmy $section = $section_default;
3951da177e4SLinus Torvaldsmy $section_context = "Context";
3964092bac7SYacine Belkadimy $section_return = "Return";
3971da177e4SLinus Torvalds
3981da177e4SLinus Torvaldsmy $undescribed = "-- undescribed --";
3991da177e4SLinus Torvalds
4001da177e4SLinus Torvaldsreset_state();
4011da177e4SLinus Torvalds
402b031ac4eSMauro Carvalho Chehabwhile ($ARGV[0] =~ m/^--?(.*)/) {
403b031ac4eSMauro Carvalho Chehab    my $cmd = $1;
404b031ac4eSMauro Carvalho Chehab    shift @ARGV;
405b031ac4eSMauro Carvalho Chehab    if ($cmd eq "man") {
4061da177e4SLinus Torvalds	$output_mode = "man";
4074d732701SDanilo Cesar Lemes de Paula	@highlights = @highlights_man;
4081da177e4SLinus Torvalds	$blankline = $blankline_man;
409b031ac4eSMauro Carvalho Chehab    } elsif ($cmd eq "rst") {
410c0d1b6eeSJonathan Corbet	$output_mode = "rst";
411c0d1b6eeSJonathan Corbet	@highlights = @highlights_rst;
412c0d1b6eeSJonathan Corbet	$blankline = $blankline_rst;
413b031ac4eSMauro Carvalho Chehab    } elsif ($cmd eq "none") {
4143a025e1dSMatthew Wilcox	$output_mode = "none";
415b031ac4eSMauro Carvalho Chehab    } elsif ($cmd eq "module") { # not needed for XML, inherits from calling document
4161da177e4SLinus Torvalds	$modulename = shift @ARGV;
417b031ac4eSMauro Carvalho Chehab    } elsif ($cmd eq "function") { # to only output specific functions
418b6c3f456SJani Nikula	$output_selection = OUTPUT_INCLUDE;
4191da177e4SLinus Torvalds	$function = shift @ARGV;
4201da177e4SLinus Torvalds	$function_table{$function} = 1;
421b031ac4eSMauro Carvalho Chehab    } elsif ($cmd eq "nofunction") { # output all except specific functions
422b6c3f456SJani Nikula	$output_selection = OUTPUT_EXCLUDE;
4231da177e4SLinus Torvalds	$function = shift @ARGV;
4241da177e4SLinus Torvalds	$function_table{$function} = 1;
425b031ac4eSMauro Carvalho Chehab    } elsif ($cmd eq "export") { # only exported symbols
426b6c3f456SJani Nikula	$output_selection = OUTPUT_EXPORTED;
427da9726ecSJani Nikula	%function_table = ();
428b031ac4eSMauro Carvalho Chehab    } elsif ($cmd eq "internal") { # only non-exported symbols
429b6c3f456SJani Nikula	$output_selection = OUTPUT_INTERNAL;
430da9726ecSJani Nikula	%function_table = ();
431b031ac4eSMauro Carvalho Chehab    } elsif ($cmd eq "export-file") {
43288c2b57dSJani Nikula	my $file = shift @ARGV;
43388c2b57dSJani Nikula	push(@export_file_list, $file);
434b031ac4eSMauro Carvalho Chehab    } elsif ($cmd eq "v") {
4351da177e4SLinus Torvalds	$verbose = 1;
436b031ac4eSMauro Carvalho Chehab    } elsif (($cmd eq "h") || ($cmd eq "help")) {
4371da177e4SLinus Torvalds	usage();
438b031ac4eSMauro Carvalho Chehab    } elsif ($cmd eq 'no-doc-sections') {
4394b44595aSJohannes Berg	    $no_doc_sections = 1;
440b031ac4eSMauro Carvalho Chehab    } elsif ($cmd eq 'enable-lineno') {
4410b0f5f29SDaniel Vetter	    $enable_lineno = 1;
442b031ac4eSMauro Carvalho Chehab    } elsif ($cmd eq 'show-not-found') {
443b0d60bfbSJonathan Corbet	$show_not_found = 1;  # A no-op but don't fail
444b031ac4eSMauro Carvalho Chehab    } else {
445b031ac4eSMauro Carvalho Chehab	# Unknown argument
446b031ac4eSMauro Carvalho Chehab        usage();
4471da177e4SLinus Torvalds    }
4481da177e4SLinus Torvalds}
4491da177e4SLinus Torvalds
4508484baaaSRandy Dunlap# continue execution near EOF;
4518484baaaSRandy Dunlap
45253f049faSBorislav Petkov# get kernel version from env
45353f049faSBorislav Petkovsub get_kernel_version() {
4541b9bc22dSJohannes Berg    my $version = 'unknown kernel version';
45553f049faSBorislav Petkov
45653f049faSBorislav Petkov    if (defined($ENV{'KERNELVERSION'})) {
45753f049faSBorislav Petkov	$version = $ENV{'KERNELVERSION'};
45853f049faSBorislav Petkov    }
45953f049faSBorislav Petkov    return $version;
46053f049faSBorislav Petkov}
4611da177e4SLinus Torvalds
4620b0f5f29SDaniel Vetter#
4630b0f5f29SDaniel Vettersub print_lineno {
4640b0f5f29SDaniel Vetter    my $lineno = shift;
4650b0f5f29SDaniel Vetter    if ($enable_lineno && defined($lineno)) {
4660b0f5f29SDaniel Vetter        print "#define LINENO " . $lineno . "\n";
4670b0f5f29SDaniel Vetter    }
4680b0f5f29SDaniel Vetter}
4691da177e4SLinus Torvalds##
4701da177e4SLinus Torvalds# dumps section contents to arrays/hashes intended for that purpose.
4711da177e4SLinus Torvalds#
4721da177e4SLinus Torvaldssub dump_section {
47394dc7ad5SRandy Dunlap    my $file = shift;
4741da177e4SLinus Torvalds    my $name = shift;
4751da177e4SLinus Torvalds    my $contents = join "\n", @_;
4761da177e4SLinus Torvalds
47713901ef2SJani Nikula    if ($name =~ m/$type_param/) {
4781da177e4SLinus Torvalds	$name = $1;
4791da177e4SLinus Torvalds	$parameterdescs{$name} = $contents;
480a1d94aa5SRandy Dunlap	$sectcheck = $sectcheck . $name . " ";
4810b0f5f29SDaniel Vetter        $parameterdesc_start_lines{$name} = $new_start_line;
4820b0f5f29SDaniel Vetter        $new_start_line = 0;
483ced69090SRandy Dunlap    } elsif ($name eq "@\.\.\.") {
484ced69090SRandy Dunlap	$name = "...";
485ced69090SRandy Dunlap	$parameterdescs{$name} = $contents;
486a1d94aa5SRandy Dunlap	$sectcheck = $sectcheck . $name . " ";
4870b0f5f29SDaniel Vetter        $parameterdesc_start_lines{$name} = $new_start_line;
4880b0f5f29SDaniel Vetter        $new_start_line = 0;
4891da177e4SLinus Torvalds    } else {
49094dc7ad5SRandy Dunlap	if (defined($sections{$name}) && ($sections{$name} ne "")) {
49195b6be9dSJani Nikula	    # Only warn on user specified duplicate section names.
49295b6be9dSJani Nikula	    if ($name ne $section_default) {
49332217761SJani Nikula		print STDERR "${file}:$.: warning: duplicate section name '$name'\n";
49432217761SJani Nikula		++$warnings;
49595b6be9dSJani Nikula	    }
49632217761SJani Nikula	    $sections{$name} .= $contents;
49732217761SJani Nikula	} else {
4981da177e4SLinus Torvalds	    $sections{$name} = $contents;
4991da177e4SLinus Torvalds	    push @sectionlist, $name;
5000b0f5f29SDaniel Vetter            $section_start_lines{$name} = $new_start_line;
5010b0f5f29SDaniel Vetter            $new_start_line = 0;
5021da177e4SLinus Torvalds	}
5031da177e4SLinus Torvalds    }
50432217761SJani Nikula}
5051da177e4SLinus Torvalds
5061da177e4SLinus Torvalds##
507b112e0f7SJohannes Berg# dump DOC: section after checking that it should go out
508b112e0f7SJohannes Berg#
509b112e0f7SJohannes Bergsub dump_doc_section {
51094dc7ad5SRandy Dunlap    my $file = shift;
511b112e0f7SJohannes Berg    my $name = shift;
512b112e0f7SJohannes Berg    my $contents = join "\n", @_;
513b112e0f7SJohannes Berg
5144b44595aSJohannes Berg    if ($no_doc_sections) {
5154b44595aSJohannes Berg        return;
5164b44595aSJohannes Berg    }
5174b44595aSJohannes Berg
518b6c3f456SJani Nikula    if (($output_selection == OUTPUT_ALL) ||
519b6c3f456SJani Nikula	($output_selection == OUTPUT_INCLUDE &&
520b6c3f456SJani Nikula	 defined($function_table{$name})) ||
521b6c3f456SJani Nikula	($output_selection == OUTPUT_EXCLUDE &&
522b6c3f456SJani Nikula	 !defined($function_table{$name})))
523b112e0f7SJohannes Berg    {
52494dc7ad5SRandy Dunlap	dump_section($file, $name, $contents);
525b112e0f7SJohannes Berg	output_blockhead({'sectionlist' => \@sectionlist,
526b112e0f7SJohannes Berg			  'sections' => \%sections,
527b112e0f7SJohannes Berg			  'module' => $modulename,
528b6c3f456SJani Nikula			  'content-only' => ($output_selection != OUTPUT_ALL), });
529b112e0f7SJohannes Berg    }
530b112e0f7SJohannes Berg}
531b112e0f7SJohannes Berg
532b112e0f7SJohannes Berg##
5331da177e4SLinus Torvalds# output function
5341da177e4SLinus Torvalds#
5351da177e4SLinus Torvalds# parameterdescs, a hash.
5361da177e4SLinus Torvalds#  function => "function name"
5371da177e4SLinus Torvalds#  parameterlist => @list of parameters
5381da177e4SLinus Torvalds#  parameterdescs => %parameter descriptions
5391da177e4SLinus Torvalds#  sectionlist => @list of sections
540a21217daSRandy Dunlap#  sections => %section descriptions
5411da177e4SLinus Torvalds#
5421da177e4SLinus Torvalds
5431da177e4SLinus Torvaldssub output_highlight {
5441da177e4SLinus Torvalds    my $contents = join "\n",@_;
5451da177e4SLinus Torvalds    my $line;
5461da177e4SLinus Torvalds
5471da177e4SLinus Torvalds#   DEBUG
5481da177e4SLinus Torvalds#   if (!defined $contents) {
5491da177e4SLinus Torvalds#	use Carp;
5501da177e4SLinus Torvalds#	confess "output_highlight got called with no args?\n";
5511da177e4SLinus Torvalds#   }
5521da177e4SLinus Torvalds
5533eb014a1SRandy Dunlap#   print STDERR "contents b4:$contents\n";
5541da177e4SLinus Torvalds    eval $dohighlight;
5551da177e4SLinus Torvalds    die $@ if $@;
5563eb014a1SRandy Dunlap#   print STDERR "contents af:$contents\n";
5573eb014a1SRandy Dunlap
5581da177e4SLinus Torvalds    foreach $line (split "\n", $contents) {
55912ae6779SDaniel Santos	if (! $output_preformatted) {
56012ae6779SDaniel Santos	    $line =~ s/^\s*//;
56112ae6779SDaniel Santos	}
5621da177e4SLinus Torvalds	if ($line eq ""){
563e314ba31SDaniel Santos	    if (! $output_preformatted) {
5640bba924cSJonathan Corbet		print $lineprefix, $blankline;
565e314ba31SDaniel Santos	    }
5661da177e4SLinus Torvalds	} else {
567cdccb316SRandy Dunlap	    if ($output_mode eq "man" && substr($line, 0, 1) eq ".") {
568cdccb316SRandy Dunlap		print "\\&$line";
569cdccb316SRandy Dunlap	    } else {
5701da177e4SLinus Torvalds		print $lineprefix, $line;
5711da177e4SLinus Torvalds	    }
572cdccb316SRandy Dunlap	}
5731da177e4SLinus Torvalds	print "\n";
5741da177e4SLinus Torvalds    }
5751da177e4SLinus Torvalds}
5761da177e4SLinus Torvalds
5771da177e4SLinus Torvalds##
5781da177e4SLinus Torvalds# output function in man
5791da177e4SLinus Torvaldssub output_function_man(%) {
5801da177e4SLinus Torvalds    my %args = %{$_[0]};
5811da177e4SLinus Torvalds    my ($parameter, $section);
5821da177e4SLinus Torvalds    my $count;
5831da177e4SLinus Torvalds
5841da177e4SLinus Torvalds    print ".TH \"$args{'function'}\" 9 \"$args{'function'}\" \"$man_date\" \"Kernel Hacker's Manual\" LINUX\n";
5851da177e4SLinus Torvalds
5861da177e4SLinus Torvalds    print ".SH NAME\n";
5871da177e4SLinus Torvalds    print $args{'function'} . " \\- " . $args{'purpose'} . "\n";
5881da177e4SLinus Torvalds
5891da177e4SLinus Torvalds    print ".SH SYNOPSIS\n";
590a21217daSRandy Dunlap    if ($args{'functiontype'} ne "") {
5911da177e4SLinus Torvalds	print ".B \"" . $args{'functiontype'} . "\" " . $args{'function'} . "\n";
592a21217daSRandy Dunlap    } else {
593a21217daSRandy Dunlap	print ".B \"" . $args{'function'} . "\n";
594a21217daSRandy Dunlap    }
5951da177e4SLinus Torvalds    $count = 0;
5961da177e4SLinus Torvalds    my $parenth = "(";
5971da177e4SLinus Torvalds    my $post = ",";
5981da177e4SLinus Torvalds    foreach my $parameter (@{$args{'parameterlist'}}) {
5991da177e4SLinus Torvalds	if ($count == $#{$args{'parameterlist'}}) {
6001da177e4SLinus Torvalds	    $post = ");";
6011da177e4SLinus Torvalds	}
6021da177e4SLinus Torvalds	$type = $args{'parametertypes'}{$parameter};
6031da177e4SLinus Torvalds	if ($type =~ m/([^\(]*\(\*)\s*\)\s*\(([^\)]*)\)/) {
6041da177e4SLinus Torvalds	    # pointer-to-function
6051da177e4SLinus Torvalds	    print ".BI \"" . $parenth . $1 . "\" " . $parameter . " \") (" . $2 . ")" . $post . "\"\n";
6061da177e4SLinus Torvalds	} else {
6071da177e4SLinus Torvalds	    $type =~ s/([^\*])$/$1 /;
6081da177e4SLinus Torvalds	    print ".BI \"" . $parenth . $type . "\" " . $parameter . " \"" . $post . "\"\n";
6091da177e4SLinus Torvalds	}
6101da177e4SLinus Torvalds	$count++;
6111da177e4SLinus Torvalds	$parenth = "";
6121da177e4SLinus Torvalds    }
6131da177e4SLinus Torvalds
6141da177e4SLinus Torvalds    print ".SH ARGUMENTS\n";
6151da177e4SLinus Torvalds    foreach $parameter (@{$args{'parameterlist'}}) {
6161da177e4SLinus Torvalds	my $parameter_name = $parameter;
6171da177e4SLinus Torvalds	$parameter_name =~ s/\[.*//;
6181da177e4SLinus Torvalds
6191da177e4SLinus Torvalds	print ".IP \"" . $parameter . "\" 12\n";
6201da177e4SLinus Torvalds	output_highlight($args{'parameterdescs'}{$parameter_name});
6211da177e4SLinus Torvalds    }
6221da177e4SLinus Torvalds    foreach $section (@{$args{'sectionlist'}}) {
6231da177e4SLinus Torvalds	print ".SH \"", uc $section, "\"\n";
6241da177e4SLinus Torvalds	output_highlight($args{'sections'}{$section});
6251da177e4SLinus Torvalds    }
6261da177e4SLinus Torvalds}
6271da177e4SLinus Torvalds
6281da177e4SLinus Torvalds##
6291da177e4SLinus Torvalds# output enum in man
6301da177e4SLinus Torvaldssub output_enum_man(%) {
6311da177e4SLinus Torvalds    my %args = %{$_[0]};
6321da177e4SLinus Torvalds    my ($parameter, $section);
6331da177e4SLinus Torvalds    my $count;
6341da177e4SLinus Torvalds
6351da177e4SLinus Torvalds    print ".TH \"$args{'module'}\" 9 \"enum $args{'enum'}\" \"$man_date\" \"API Manual\" LINUX\n";
6361da177e4SLinus Torvalds
6371da177e4SLinus Torvalds    print ".SH NAME\n";
6381da177e4SLinus Torvalds    print "enum " . $args{'enum'} . " \\- " . $args{'purpose'} . "\n";
6391da177e4SLinus Torvalds
6401da177e4SLinus Torvalds    print ".SH SYNOPSIS\n";
6411da177e4SLinus Torvalds    print "enum " . $args{'enum'} . " {\n";
6421da177e4SLinus Torvalds    $count = 0;
6431da177e4SLinus Torvalds    foreach my $parameter (@{$args{'parameterlist'}}) {
6441da177e4SLinus Torvalds	print ".br\n.BI \"    $parameter\"\n";
6451da177e4SLinus Torvalds	if ($count == $#{$args{'parameterlist'}}) {
6461da177e4SLinus Torvalds	    print "\n};\n";
6471da177e4SLinus Torvalds	    last;
6481da177e4SLinus Torvalds	}
6491da177e4SLinus Torvalds	else {
6501da177e4SLinus Torvalds	    print ", \n.br\n";
6511da177e4SLinus Torvalds	}
6521da177e4SLinus Torvalds	$count++;
6531da177e4SLinus Torvalds    }
6541da177e4SLinus Torvalds
6551da177e4SLinus Torvalds    print ".SH Constants\n";
6561da177e4SLinus Torvalds    foreach $parameter (@{$args{'parameterlist'}}) {
6571da177e4SLinus Torvalds	my $parameter_name = $parameter;
6581da177e4SLinus Torvalds	$parameter_name =~ s/\[.*//;
6591da177e4SLinus Torvalds
6601da177e4SLinus Torvalds	print ".IP \"" . $parameter . "\" 12\n";
6611da177e4SLinus Torvalds	output_highlight($args{'parameterdescs'}{$parameter_name});
6621da177e4SLinus Torvalds    }
6631da177e4SLinus Torvalds    foreach $section (@{$args{'sectionlist'}}) {
6641da177e4SLinus Torvalds	print ".SH \"$section\"\n";
6651da177e4SLinus Torvalds	output_highlight($args{'sections'}{$section});
6661da177e4SLinus Torvalds    }
6671da177e4SLinus Torvalds}
6681da177e4SLinus Torvalds
6691da177e4SLinus Torvalds##
6701da177e4SLinus Torvalds# output struct in man
6711da177e4SLinus Torvaldssub output_struct_man(%) {
6721da177e4SLinus Torvalds    my %args = %{$_[0]};
6731da177e4SLinus Torvalds    my ($parameter, $section);
6741da177e4SLinus Torvalds
6751da177e4SLinus Torvalds    print ".TH \"$args{'module'}\" 9 \"" . $args{'type'} . " " . $args{'struct'} . "\" \"$man_date\" \"API Manual\" LINUX\n";
6761da177e4SLinus Torvalds
6771da177e4SLinus Torvalds    print ".SH NAME\n";
6781da177e4SLinus Torvalds    print $args{'type'} . " " . $args{'struct'} . " \\- " . $args{'purpose'} . "\n";
6791da177e4SLinus Torvalds
6808ad72163SMauro Carvalho Chehab    my $declaration = $args{'definition'};
6818ad72163SMauro Carvalho Chehab    $declaration =~ s/\t/  /g;
6828ad72163SMauro Carvalho Chehab    $declaration =~ s/\n/"\n.br\n.BI \"/g;
6831da177e4SLinus Torvalds    print ".SH SYNOPSIS\n";
6841da177e4SLinus Torvalds    print $args{'type'} . " " . $args{'struct'} . " {\n.br\n";
6858ad72163SMauro Carvalho Chehab    print ".BI \"$declaration\n};\n.br\n\n";
6861da177e4SLinus Torvalds
687c51d3dacSRandy Dunlap    print ".SH Members\n";
6881da177e4SLinus Torvalds    foreach $parameter (@{$args{'parameterlist'}}) {
6891da177e4SLinus Torvalds	($parameter =~ /^#/) && next;
6901da177e4SLinus Torvalds
6911da177e4SLinus Torvalds	my $parameter_name = $parameter;
6921da177e4SLinus Torvalds	$parameter_name =~ s/\[.*//;
6931da177e4SLinus Torvalds
6941da177e4SLinus Torvalds	($args{'parameterdescs'}{$parameter_name} ne $undescribed) || next;
6951da177e4SLinus Torvalds	print ".IP \"" . $parameter . "\" 12\n";
6961da177e4SLinus Torvalds	output_highlight($args{'parameterdescs'}{$parameter_name});
6971da177e4SLinus Torvalds    }
6981da177e4SLinus Torvalds    foreach $section (@{$args{'sectionlist'}}) {
6991da177e4SLinus Torvalds	print ".SH \"$section\"\n";
7001da177e4SLinus Torvalds	output_highlight($args{'sections'}{$section});
7011da177e4SLinus Torvalds    }
7021da177e4SLinus Torvalds}
7031da177e4SLinus Torvalds
7041da177e4SLinus Torvalds##
7051da177e4SLinus Torvalds# output typedef in man
7061da177e4SLinus Torvaldssub output_typedef_man(%) {
7071da177e4SLinus Torvalds    my %args = %{$_[0]};
7081da177e4SLinus Torvalds    my ($parameter, $section);
7091da177e4SLinus Torvalds
7101da177e4SLinus Torvalds    print ".TH \"$args{'module'}\" 9 \"$args{'typedef'}\" \"$man_date\" \"API Manual\" LINUX\n";
7111da177e4SLinus Torvalds
7121da177e4SLinus Torvalds    print ".SH NAME\n";
7131da177e4SLinus Torvalds    print "typedef " . $args{'typedef'} . " \\- " . $args{'purpose'} . "\n";
7141da177e4SLinus Torvalds
7151da177e4SLinus Torvalds    foreach $section (@{$args{'sectionlist'}}) {
7161da177e4SLinus Torvalds	print ".SH \"$section\"\n";
7171da177e4SLinus Torvalds	output_highlight($args{'sections'}{$section});
7181da177e4SLinus Torvalds    }
7191da177e4SLinus Torvalds}
7201da177e4SLinus Torvalds
721b112e0f7SJohannes Bergsub output_blockhead_man(%) {
7221da177e4SLinus Torvalds    my %args = %{$_[0]};
7231da177e4SLinus Torvalds    my ($parameter, $section);
7241da177e4SLinus Torvalds    my $count;
7251da177e4SLinus Torvalds
7261da177e4SLinus Torvalds    print ".TH \"$args{'module'}\" 9 \"$args{'module'}\" \"$man_date\" \"API Manual\" LINUX\n";
7271da177e4SLinus Torvalds
7281da177e4SLinus Torvalds    foreach $section (@{$args{'sectionlist'}}) {
7291da177e4SLinus Torvalds	print ".SH \"$section\"\n";
7301da177e4SLinus Torvalds	output_highlight($args{'sections'}{$section});
7311da177e4SLinus Torvalds    }
7321da177e4SLinus Torvalds}
7331da177e4SLinus Torvalds
7341da177e4SLinus Torvalds##
735c0d1b6eeSJonathan Corbet# output in restructured text
736c0d1b6eeSJonathan Corbet#
737c0d1b6eeSJonathan Corbet
738c0d1b6eeSJonathan Corbet#
739c0d1b6eeSJonathan Corbet# This could use some work; it's used to output the DOC: sections, and
740c0d1b6eeSJonathan Corbet# starts by putting out the name of the doc section itself, but that tends
741c0d1b6eeSJonathan Corbet# to duplicate a header already in the template file.
742c0d1b6eeSJonathan Corbet#
743c0d1b6eeSJonathan Corbetsub output_blockhead_rst(%) {
744c0d1b6eeSJonathan Corbet    my %args = %{$_[0]};
745c0d1b6eeSJonathan Corbet    my ($parameter, $section);
746c0d1b6eeSJonathan Corbet
747c0d1b6eeSJonathan Corbet    foreach $section (@{$args{'sectionlist'}}) {
7489e72184bSJani Nikula	if ($output_selection != OUTPUT_INCLUDE) {
749c0d1b6eeSJonathan Corbet	    print "**$section**\n\n";
7509e72184bSJani Nikula	}
7510b0f5f29SDaniel Vetter        print_lineno($section_start_lines{$section});
752c0d1b6eeSJonathan Corbet	output_highlight_rst($args{'sections'}{$section});
753c0d1b6eeSJonathan Corbet	print "\n";
754c0d1b6eeSJonathan Corbet    }
755c0d1b6eeSJonathan Corbet}
756c0d1b6eeSJonathan Corbet
757af250290SJonathan Corbet#
758af250290SJonathan Corbet# Apply the RST highlights to a sub-block of text.
759af250290SJonathan Corbet#
760af250290SJonathan Corbetsub highlight_block($) {
761af250290SJonathan Corbet    # The dohighlight kludge requires the text be called $contents
762af250290SJonathan Corbet    my $contents = shift;
763c0d1b6eeSJonathan Corbet    eval $dohighlight;
764c0d1b6eeSJonathan Corbet    die $@ if $@;
765af250290SJonathan Corbet    return $contents;
766af250290SJonathan Corbet}
767c0d1b6eeSJonathan Corbet
768af250290SJonathan Corbet#
769af250290SJonathan Corbet# Regexes used only here.
770af250290SJonathan Corbet#
771af250290SJonathan Corbetmy $sphinx_literal = '^[^.].*::$';
772af250290SJonathan Corbetmy $sphinx_cblock = '^\.\.\ +code-block::';
773af250290SJonathan Corbet
774af250290SJonathan Corbetsub output_highlight_rst {
775af250290SJonathan Corbet    my $input = join "\n",@_;
776af250290SJonathan Corbet    my $output = "";
777af250290SJonathan Corbet    my $line;
778af250290SJonathan Corbet    my $in_literal = 0;
779af250290SJonathan Corbet    my $litprefix;
780af250290SJonathan Corbet    my $block = "";
781af250290SJonathan Corbet
782af250290SJonathan Corbet    foreach $line (split "\n",$input) {
783af250290SJonathan Corbet	#
784af250290SJonathan Corbet	# If we're in a literal block, see if we should drop out
785af250290SJonathan Corbet	# of it.  Otherwise pass the line straight through unmunged.
786af250290SJonathan Corbet	#
787af250290SJonathan Corbet	if ($in_literal) {
788af250290SJonathan Corbet	    if (! ($line =~ /^\s*$/)) {
789af250290SJonathan Corbet		#
790af250290SJonathan Corbet		# If this is the first non-blank line in a literal
791af250290SJonathan Corbet		# block we need to figure out what the proper indent is.
792af250290SJonathan Corbet		#
793af250290SJonathan Corbet		if ($litprefix eq "") {
794af250290SJonathan Corbet		    $line =~ /^(\s*)/;
795af250290SJonathan Corbet		    $litprefix = '^' . $1;
796af250290SJonathan Corbet		    $output .= $line . "\n";
797af250290SJonathan Corbet		} elsif (! ($line =~ /$litprefix/)) {
798af250290SJonathan Corbet		    $in_literal = 0;
799af250290SJonathan Corbet		} else {
800af250290SJonathan Corbet		    $output .= $line . "\n";
801af250290SJonathan Corbet		}
802af250290SJonathan Corbet	    } else {
803af250290SJonathan Corbet		$output .= $line . "\n";
804af250290SJonathan Corbet	    }
805af250290SJonathan Corbet	}
806af250290SJonathan Corbet	#
807af250290SJonathan Corbet	# Not in a literal block (or just dropped out)
808af250290SJonathan Corbet	#
809af250290SJonathan Corbet	if (! $in_literal) {
810af250290SJonathan Corbet	    $block .= $line . "\n";
811af250290SJonathan Corbet	    if (($line =~ /$sphinx_literal/) || ($line =~ /$sphinx_cblock/)) {
812af250290SJonathan Corbet		$in_literal = 1;
813af250290SJonathan Corbet		$litprefix = "";
814af250290SJonathan Corbet		$output .= highlight_block($block);
815af250290SJonathan Corbet		$block = ""
816af250290SJonathan Corbet	    }
817af250290SJonathan Corbet	}
818af250290SJonathan Corbet    }
819af250290SJonathan Corbet
820af250290SJonathan Corbet    if ($block) {
821af250290SJonathan Corbet	$output .= highlight_block($block);
822af250290SJonathan Corbet    }
823af250290SJonathan Corbet    foreach $line (split "\n", $output) {
824830066a7SJani Nikula	print $lineprefix . $line . "\n";
825c0d1b6eeSJonathan Corbet    }
826c0d1b6eeSJonathan Corbet}
827c0d1b6eeSJonathan Corbet
828c0d1b6eeSJonathan Corbetsub output_function_rst(%) {
829c0d1b6eeSJonathan Corbet    my %args = %{$_[0]};
830c0d1b6eeSJonathan Corbet    my ($parameter, $section);
831c099ff69SJani Nikula    my $oldprefix = $lineprefix;
83282801d06SMauro Carvalho Chehab    my $start = "";
833c0d1b6eeSJonathan Corbet
83482801d06SMauro Carvalho Chehab    if ($args{'typedef'}) {
83582801d06SMauro Carvalho Chehab	print ".. c:type:: ". $args{'function'} . "\n\n";
83682801d06SMauro Carvalho Chehab	print_lineno($declaration_start_line);
83782801d06SMauro Carvalho Chehab	print "   **Typedef**: ";
83882801d06SMauro Carvalho Chehab	$lineprefix = "";
83982801d06SMauro Carvalho Chehab	output_highlight_rst($args{'purpose'});
84082801d06SMauro Carvalho Chehab	$start = "\n\n**Syntax**\n\n  ``";
841c0d1b6eeSJonathan Corbet    } else {
84282801d06SMauro Carvalho Chehab	print ".. c:function:: ";
84382801d06SMauro Carvalho Chehab    }
84482801d06SMauro Carvalho Chehab    if ($args{'functiontype'} ne "") {
84582801d06SMauro Carvalho Chehab	$start .= $args{'functiontype'} . " " . $args{'function'} . " (";
84682801d06SMauro Carvalho Chehab    } else {
84782801d06SMauro Carvalho Chehab	$start .= $args{'function'} . " (";
848c0d1b6eeSJonathan Corbet    }
849c0d1b6eeSJonathan Corbet    print $start;
850c0d1b6eeSJonathan Corbet
851c0d1b6eeSJonathan Corbet    my $count = 0;
852c0d1b6eeSJonathan Corbet    foreach my $parameter (@{$args{'parameterlist'}}) {
853c0d1b6eeSJonathan Corbet	if ($count ne 0) {
854c0d1b6eeSJonathan Corbet	    print ", ";
855c0d1b6eeSJonathan Corbet	}
856c0d1b6eeSJonathan Corbet	$count++;
857c0d1b6eeSJonathan Corbet	$type = $args{'parametertypes'}{$parameter};
858a88b1672SMauro Carvalho Chehab
859c0d1b6eeSJonathan Corbet	if ($type =~ m/([^\(]*\(\*)\s*\)\s*\(([^\)]*)\)/) {
860c0d1b6eeSJonathan Corbet	    # pointer-to-function
861e8f4ba83SPeter Maydell	    print $1 . $parameter . ") (" . $2 . ")";
862c0d1b6eeSJonathan Corbet	} else {
863c0d1b6eeSJonathan Corbet	    print $type . " " . $parameter;
864c0d1b6eeSJonathan Corbet	}
865c0d1b6eeSJonathan Corbet    }
86682801d06SMauro Carvalho Chehab    if ($args{'typedef'}) {
86782801d06SMauro Carvalho Chehab	print ");``\n\n";
86882801d06SMauro Carvalho Chehab    } else {
869c099ff69SJani Nikula	print ")\n\n";
8700b0f5f29SDaniel Vetter	print_lineno($declaration_start_line);
871c099ff69SJani Nikula	$lineprefix = "   ";
872c099ff69SJani Nikula	output_highlight_rst($args{'purpose'});
873c099ff69SJani Nikula	print "\n";
87482801d06SMauro Carvalho Chehab    }
875c0d1b6eeSJonathan Corbet
876ecbcfba1SJani Nikula    print "**Parameters**\n\n";
877c099ff69SJani Nikula    $lineprefix = "  ";
878c0d1b6eeSJonathan Corbet    foreach $parameter (@{$args{'parameterlist'}}) {
879c0d1b6eeSJonathan Corbet	my $parameter_name = $parameter;
880ada5f446SGabriel Krisman Bertazi	$parameter_name =~ s/\[.*//;
881c0d1b6eeSJonathan Corbet	$type = $args{'parametertypes'}{$parameter};
882c0d1b6eeSJonathan Corbet
883c0d1b6eeSJonathan Corbet	if ($type ne "") {
884c0d1b6eeSJonathan Corbet	    print "``$type $parameter``\n";
885c0d1b6eeSJonathan Corbet	} else {
886c0d1b6eeSJonathan Corbet	    print "``$parameter``\n";
887c0d1b6eeSJonathan Corbet	}
8880b0f5f29SDaniel Vetter
8890b0f5f29SDaniel Vetter        print_lineno($parameterdesc_start_lines{$parameter_name});
8900b0f5f29SDaniel Vetter
8915e64fa9cSJani Nikula	if (defined($args{'parameterdescs'}{$parameter_name}) &&
8925e64fa9cSJani Nikula	    $args{'parameterdescs'}{$parameter_name} ne $undescribed) {
893c0d1b6eeSJonathan Corbet	    output_highlight_rst($args{'parameterdescs'}{$parameter_name});
894c0d1b6eeSJonathan Corbet	} else {
895d4b08e0cSJani Nikula	    print "  *undescribed*\n";
896c0d1b6eeSJonathan Corbet	}
897c0d1b6eeSJonathan Corbet	print "\n";
898c0d1b6eeSJonathan Corbet    }
899c099ff69SJani Nikula
900c099ff69SJani Nikula    $lineprefix = $oldprefix;
901c0d1b6eeSJonathan Corbet    output_section_rst(@_);
902c0d1b6eeSJonathan Corbet}
903c0d1b6eeSJonathan Corbet
904c0d1b6eeSJonathan Corbetsub output_section_rst(%) {
905c0d1b6eeSJonathan Corbet    my %args = %{$_[0]};
906c0d1b6eeSJonathan Corbet    my $section;
907c0d1b6eeSJonathan Corbet    my $oldprefix = $lineprefix;
908c0d1b6eeSJonathan Corbet    $lineprefix = "";
909c0d1b6eeSJonathan Corbet
910c0d1b6eeSJonathan Corbet    foreach $section (@{$args{'sectionlist'}}) {
911ecbcfba1SJani Nikula	print "**$section**\n\n";
9120b0f5f29SDaniel Vetter        print_lineno($section_start_lines{$section});
913c0d1b6eeSJonathan Corbet	output_highlight_rst($args{'sections'}{$section});
914c0d1b6eeSJonathan Corbet	print "\n";
915c0d1b6eeSJonathan Corbet    }
916c0d1b6eeSJonathan Corbet    print "\n";
917c0d1b6eeSJonathan Corbet    $lineprefix = $oldprefix;
918c0d1b6eeSJonathan Corbet}
919c0d1b6eeSJonathan Corbet
920c0d1b6eeSJonathan Corbetsub output_enum_rst(%) {
921c0d1b6eeSJonathan Corbet    my %args = %{$_[0]};
922c0d1b6eeSJonathan Corbet    my ($parameter);
923c099ff69SJani Nikula    my $oldprefix = $lineprefix;
924c0d1b6eeSJonathan Corbet    my $count;
925c0d1b6eeSJonathan Corbet    my $name = "enum " . $args{'enum'};
92662850976SJani Nikula
92762850976SJani Nikula    print "\n\n.. c:type:: " . $name . "\n\n";
9280b0f5f29SDaniel Vetter    print_lineno($declaration_start_line);
929c099ff69SJani Nikula    $lineprefix = "   ";
930c099ff69SJani Nikula    output_highlight_rst($args{'purpose'});
931c099ff69SJani Nikula    print "\n";
932c0d1b6eeSJonathan Corbet
933ecbcfba1SJani Nikula    print "**Constants**\n\n";
934c0d1b6eeSJonathan Corbet    $lineprefix = "  ";
935c0d1b6eeSJonathan Corbet    foreach $parameter (@{$args{'parameterlist'}}) {
936ecbcfba1SJani Nikula	print "``$parameter``\n";
937c0d1b6eeSJonathan Corbet	if ($args{'parameterdescs'}{$parameter} ne $undescribed) {
938c0d1b6eeSJonathan Corbet	    output_highlight_rst($args{'parameterdescs'}{$parameter});
939c0d1b6eeSJonathan Corbet	} else {
940d4b08e0cSJani Nikula	    print "  *undescribed*\n";
941c0d1b6eeSJonathan Corbet	}
942c0d1b6eeSJonathan Corbet	print "\n";
943c0d1b6eeSJonathan Corbet    }
944c099ff69SJani Nikula
945c0d1b6eeSJonathan Corbet    $lineprefix = $oldprefix;
946c0d1b6eeSJonathan Corbet    output_section_rst(@_);
947c0d1b6eeSJonathan Corbet}
948c0d1b6eeSJonathan Corbet
949c0d1b6eeSJonathan Corbetsub output_typedef_rst(%) {
950c0d1b6eeSJonathan Corbet    my %args = %{$_[0]};
951c0d1b6eeSJonathan Corbet    my ($parameter);
952c099ff69SJani Nikula    my $oldprefix = $lineprefix;
953c0d1b6eeSJonathan Corbet    my $name = "typedef " . $args{'typedef'};
954c0d1b6eeSJonathan Corbet
95562850976SJani Nikula    print "\n\n.. c:type:: " . $name . "\n\n";
9560b0f5f29SDaniel Vetter    print_lineno($declaration_start_line);
957c099ff69SJani Nikula    $lineprefix = "   ";
958c099ff69SJani Nikula    output_highlight_rst($args{'purpose'});
959c099ff69SJani Nikula    print "\n";
960c0d1b6eeSJonathan Corbet
961c099ff69SJani Nikula    $lineprefix = $oldprefix;
962c0d1b6eeSJonathan Corbet    output_section_rst(@_);
963c0d1b6eeSJonathan Corbet}
964c0d1b6eeSJonathan Corbet
965c0d1b6eeSJonathan Corbetsub output_struct_rst(%) {
966c0d1b6eeSJonathan Corbet    my %args = %{$_[0]};
967c0d1b6eeSJonathan Corbet    my ($parameter);
968c099ff69SJani Nikula    my $oldprefix = $lineprefix;
969c0d1b6eeSJonathan Corbet    my $name = $args{'type'} . " " . $args{'struct'};
970c0d1b6eeSJonathan Corbet
97162850976SJani Nikula    print "\n\n.. c:type:: " . $name . "\n\n";
9720b0f5f29SDaniel Vetter    print_lineno($declaration_start_line);
973c099ff69SJani Nikula    $lineprefix = "   ";
974c099ff69SJani Nikula    output_highlight_rst($args{'purpose'});
975c099ff69SJani Nikula    print "\n";
976c0d1b6eeSJonathan Corbet
977ecbcfba1SJani Nikula    print "**Definition**\n\n";
978c0d1b6eeSJonathan Corbet    print "::\n\n";
9798ad72163SMauro Carvalho Chehab    my $declaration = $args{'definition'};
9808ad72163SMauro Carvalho Chehab    $declaration =~ s/\t/  /g;
9818ad72163SMauro Carvalho Chehab    print "  " . $args{'type'} . " " . $args{'struct'} . " {\n$declaration  };\n\n";
982c0d1b6eeSJonathan Corbet
983ecbcfba1SJani Nikula    print "**Members**\n\n";
984c099ff69SJani Nikula    $lineprefix = "  ";
985c0d1b6eeSJonathan Corbet    foreach $parameter (@{$args{'parameterlist'}}) {
986c0d1b6eeSJonathan Corbet	($parameter =~ /^#/) && next;
987c0d1b6eeSJonathan Corbet
988c0d1b6eeSJonathan Corbet	my $parameter_name = $parameter;
989c0d1b6eeSJonathan Corbet	$parameter_name =~ s/\[.*//;
990c0d1b6eeSJonathan Corbet
991c0d1b6eeSJonathan Corbet	($args{'parameterdescs'}{$parameter_name} ne $undescribed) || next;
992c0d1b6eeSJonathan Corbet	$type = $args{'parametertypes'}{$parameter};
9930b0f5f29SDaniel Vetter        print_lineno($parameterdesc_start_lines{$parameter_name});
9946d232c80SMauro Carvalho Chehab	print "``" . $parameter . "``\n";
995c0d1b6eeSJonathan Corbet	output_highlight_rst($args{'parameterdescs'}{$parameter_name});
996c0d1b6eeSJonathan Corbet	print "\n";
997c0d1b6eeSJonathan Corbet    }
998c0d1b6eeSJonathan Corbet    print "\n";
999c099ff69SJani Nikula
1000c099ff69SJani Nikula    $lineprefix = $oldprefix;
1001c0d1b6eeSJonathan Corbet    output_section_rst(@_);
1002c0d1b6eeSJonathan Corbet}
1003c0d1b6eeSJonathan Corbet
10043a025e1dSMatthew Wilcox## none mode output functions
10053a025e1dSMatthew Wilcox
10063a025e1dSMatthew Wilcoxsub output_function_none(%) {
10073a025e1dSMatthew Wilcox}
10083a025e1dSMatthew Wilcox
10093a025e1dSMatthew Wilcoxsub output_enum_none(%) {
10103a025e1dSMatthew Wilcox}
10113a025e1dSMatthew Wilcox
10123a025e1dSMatthew Wilcoxsub output_typedef_none(%) {
10133a025e1dSMatthew Wilcox}
10143a025e1dSMatthew Wilcox
10153a025e1dSMatthew Wilcoxsub output_struct_none(%) {
10163a025e1dSMatthew Wilcox}
10173a025e1dSMatthew Wilcox
10183a025e1dSMatthew Wilcoxsub output_blockhead_none(%) {
10193a025e1dSMatthew Wilcox}
10203a025e1dSMatthew Wilcox
10211da177e4SLinus Torvalds##
102227205744SRandy Dunlap# generic output function for all types (function, struct/union, typedef, enum);
102327205744SRandy Dunlap# calls the generated, variable output_ function name based on
102427205744SRandy Dunlap# functype and output_mode
10251da177e4SLinus Torvaldssub output_declaration {
10261da177e4SLinus Torvalds    no strict 'refs';
10271da177e4SLinus Torvalds    my $name = shift;
10281da177e4SLinus Torvalds    my $functype = shift;
10291da177e4SLinus Torvalds    my $func = "output_${functype}_$output_mode";
1030b6c3f456SJani Nikula    if (($output_selection == OUTPUT_ALL) ||
1031b6c3f456SJani Nikula	(($output_selection == OUTPUT_INCLUDE ||
1032b6c3f456SJani Nikula	  $output_selection == OUTPUT_EXPORTED) &&
103386ae2e38SJani Nikula	 defined($function_table{$name})) ||
1034b6c3f456SJani Nikula	(($output_selection == OUTPUT_EXCLUDE ||
1035b6c3f456SJani Nikula	  $output_selection == OUTPUT_INTERNAL) &&
103686ae2e38SJani Nikula	 !($functype eq "function" && defined($function_table{$name}))))
10371da177e4SLinus Torvalds    {
10381da177e4SLinus Torvalds	&$func(@_);
10391da177e4SLinus Torvalds	$section_counter++;
10401da177e4SLinus Torvalds    }
10411da177e4SLinus Torvalds}
10421da177e4SLinus Torvalds
10431da177e4SLinus Torvalds##
104427205744SRandy Dunlap# generic output function - calls the right one based on current output mode.
1045b112e0f7SJohannes Bergsub output_blockhead {
10461da177e4SLinus Torvalds    no strict 'refs';
1047b112e0f7SJohannes Berg    my $func = "output_blockhead_" . $output_mode;
10481da177e4SLinus Torvalds    &$func(@_);
10491da177e4SLinus Torvalds    $section_counter++;
10501da177e4SLinus Torvalds}
10511da177e4SLinus Torvalds
10521da177e4SLinus Torvalds##
10531da177e4SLinus Torvalds# takes a declaration (struct, union, enum, typedef) and
10541da177e4SLinus Torvalds# invokes the right handler. NOT called for functions.
10551da177e4SLinus Torvaldssub dump_declaration($$) {
10561da177e4SLinus Torvalds    no strict 'refs';
10571da177e4SLinus Torvalds    my ($prototype, $file) = @_;
10581da177e4SLinus Torvalds    my $func = "dump_" . $decl_type;
10591da177e4SLinus Torvalds    &$func(@_);
10601da177e4SLinus Torvalds}
10611da177e4SLinus Torvalds
10621da177e4SLinus Torvaldssub dump_union($$) {
10631da177e4SLinus Torvalds    dump_struct(@_);
10641da177e4SLinus Torvalds}
10651da177e4SLinus Torvalds
10661da177e4SLinus Torvaldssub dump_struct($$) {
10671da177e4SLinus Torvalds    my $x = shift;
10681da177e4SLinus Torvalds    my $file = shift;
10691da177e4SLinus Torvalds
1070f861537dSAndré Almeida    if ($x =~ /(struct|union)\s+(\w+)\s*\{(.*)\}(\s*(__packed|__aligned|____cacheline_aligned_in_smp|__attribute__\s*\(\([a-z0-9,_\s\(\)]*\)\)))*/) {
10715cb5c31cSJohannes Berg	my $decl_type = $1;
10721da177e4SLinus Torvalds	$declaration_name = $2;
10731da177e4SLinus Torvalds	my $members = $3;
10741da177e4SLinus Torvalds
1075aeec46b9SMartin Waitz	# ignore members marked private:
10760d8c39e6SMauro Carvalho Chehab	$members =~ s/\/\*\s*private:.*?\/\*\s*public:.*?\*\///gosi;
10770d8c39e6SMauro Carvalho Chehab	$members =~ s/\/\*\s*private:.*//gosi;
1078aeec46b9SMartin Waitz	# strip comments:
1079aeec46b9SMartin Waitz	$members =~ s/\/\*.*?\*\///gos;
1080ef5da59fSRandy Dunlap	# strip attributes
10813d9bfb19SSakari Ailus	$members =~ s/\s*__attribute__\s*\(\([a-z0-9,_\*\s\(\)]*\)\)/ /gi;
10823d9bfb19SSakari Ailus	$members =~ s/\s*__aligned\s*\([^;]*\)/ /gos;
10833d9bfb19SSakari Ailus	$members =~ s/\s*__packed\s*/ /gos;
1084f0074929SJonathan Corbet	$members =~ s/\s*CRYPTO_MINALIGN_ATTR/ /gos;
1085f861537dSAndré Almeida	$members =~ s/\s*____cacheline_aligned_in_smp/ /gos;
1086b22b5a9eSConchúr Navid	# replace DECLARE_BITMAP
108745005b27SMauro Carvalho Chehab	$members =~ s/DECLARE_BITMAP\s*\(([^,)]+),\s*([^,)]+)\)/unsigned long $1\[BITS_TO_LONGS($2)\]/gos;
10881cb566baSJakub Kicinski	# replace DECLARE_HASHTABLE
108945005b27SMauro Carvalho Chehab	$members =~ s/DECLARE_HASHTABLE\s*\(([^,)]+),\s*([^,)]+)\)/unsigned long $1\[1 << (($2) - 1)\]/gos;
109045005b27SMauro Carvalho Chehab	# replace DECLARE_KFIFO
109145005b27SMauro Carvalho Chehab	$members =~ s/DECLARE_KFIFO\s*\(([^,)]+),\s*([^,)]+),\s*([^,)]+)\)/$2 \*$1/gos;
109245005b27SMauro Carvalho Chehab	# replace DECLARE_KFIFO_PTR
109345005b27SMauro Carvalho Chehab	$members =~ s/DECLARE_KFIFO_PTR\s*\(([^,)]+),\s*([^,)]+)\)/$2 \*$1/gos;
1094aeec46b9SMartin Waitz
10958ad72163SMauro Carvalho Chehab	my $declaration = $members;
10968ad72163SMauro Carvalho Chehab
10978ad72163SMauro Carvalho Chehab	# Split nested struct/union elements as newer ones
109884ce5b98SMauro Carvalho Chehab	while ($members =~ m/(struct|union)([^\{\};]+)\{([^\{\}]*)\}([^\{\}\;]*)\;/) {
109984ce5b98SMauro Carvalho Chehab		my $newmember;
110084ce5b98SMauro Carvalho Chehab		my $maintype = $1;
110184ce5b98SMauro Carvalho Chehab		my $ids = $4;
11028ad72163SMauro Carvalho Chehab		my $content = $3;
110384ce5b98SMauro Carvalho Chehab		foreach my $id(split /,/, $ids) {
110484ce5b98SMauro Carvalho Chehab			$newmember .= "$maintype $id; ";
110584ce5b98SMauro Carvalho Chehab
11068ad72163SMauro Carvalho Chehab			$id =~ s/[:\[].*//;
110784ce5b98SMauro Carvalho Chehab			$id =~ s/^\s*\**(\S+)\s*/$1/;
11088ad72163SMauro Carvalho Chehab			foreach my $arg (split /;/, $content) {
11098ad72163SMauro Carvalho Chehab				next if ($arg =~ m/^\s*$/);
11107c0d7e87SMauro Carvalho Chehab				if ($arg =~ m/^([^\(]+\(\*?\s*)([\w\.]*)(\s*\).*)/) {
11117c0d7e87SMauro Carvalho Chehab					# pointer-to-function
11127c0d7e87SMauro Carvalho Chehab					my $type = $1;
11137c0d7e87SMauro Carvalho Chehab					my $name = $2;
11147c0d7e87SMauro Carvalho Chehab					my $extra = $3;
11157c0d7e87SMauro Carvalho Chehab					next if (!$name);
11167c0d7e87SMauro Carvalho Chehab					if ($id =~ m/^\s*$/) {
11177c0d7e87SMauro Carvalho Chehab						# anonymous struct/union
11187c0d7e87SMauro Carvalho Chehab						$newmember .= "$type$name$extra; ";
11197c0d7e87SMauro Carvalho Chehab					} else {
11207c0d7e87SMauro Carvalho Chehab						$newmember .= "$type$id.$name$extra; ";
11217c0d7e87SMauro Carvalho Chehab					}
11227c0d7e87SMauro Carvalho Chehab				} else {
112384ce5b98SMauro Carvalho Chehab					my $type;
112484ce5b98SMauro Carvalho Chehab					my $names;
112584ce5b98SMauro Carvalho Chehab					$arg =~ s/^\s+//;
112684ce5b98SMauro Carvalho Chehab					$arg =~ s/\s+$//;
112784ce5b98SMauro Carvalho Chehab					# Handle bitmaps
112884ce5b98SMauro Carvalho Chehab					$arg =~ s/:\s*\d+\s*//g;
112984ce5b98SMauro Carvalho Chehab					# Handle arrays
1130d404d579SMauro Carvalho Chehab					$arg =~ s/\[.*\]//g;
113184ce5b98SMauro Carvalho Chehab					# The type may have multiple words,
113284ce5b98SMauro Carvalho Chehab					# and multiple IDs can be defined, like:
113384ce5b98SMauro Carvalho Chehab					#	const struct foo, *bar, foobar
113484ce5b98SMauro Carvalho Chehab					# So, we remove spaces when parsing the
113584ce5b98SMauro Carvalho Chehab					# names, in order to match just names
113684ce5b98SMauro Carvalho Chehab					# and commas for the names
113784ce5b98SMauro Carvalho Chehab					$arg =~ s/\s*,\s*/,/g;
113884ce5b98SMauro Carvalho Chehab					if ($arg =~ m/(.*)\s+([\S+,]+)/) {
113984ce5b98SMauro Carvalho Chehab						$type = $1;
114084ce5b98SMauro Carvalho Chehab						$names = $2;
114184ce5b98SMauro Carvalho Chehab					} else {
114284ce5b98SMauro Carvalho Chehab						$newmember .= "$arg; ";
114384ce5b98SMauro Carvalho Chehab						next;
114484ce5b98SMauro Carvalho Chehab					}
114584ce5b98SMauro Carvalho Chehab					foreach my $name (split /,/, $names) {
114684ce5b98SMauro Carvalho Chehab						$name =~ s/^\s*\**(\S+)\s*/$1/;
11478ad72163SMauro Carvalho Chehab						next if (($name =~ m/^\s*$/));
11488ad72163SMauro Carvalho Chehab						if ($id =~ m/^\s*$/) {
11498ad72163SMauro Carvalho Chehab							# anonymous struct/union
11508ad72163SMauro Carvalho Chehab							$newmember .= "$type $name; ";
11518ad72163SMauro Carvalho Chehab						} else {
11528ad72163SMauro Carvalho Chehab							$newmember .= "$type $id.$name; ";
11538ad72163SMauro Carvalho Chehab						}
11548ad72163SMauro Carvalho Chehab					}
11557c0d7e87SMauro Carvalho Chehab				}
115684ce5b98SMauro Carvalho Chehab			}
115784ce5b98SMauro Carvalho Chehab		}
1158673bb2dfSBen Hutchings		$members =~ s/(struct|union)([^\{\};]+)\{([^\{\}]*)\}([^\{\}\;]*)\;/$newmember/;
115984ce5b98SMauro Carvalho Chehab	}
11608ad72163SMauro Carvalho Chehab
11618ad72163SMauro Carvalho Chehab	# Ignore other nested elements, like enums
1162673bb2dfSBen Hutchings	$members =~ s/(\{[^\{\}]*\})//g;
11638ad72163SMauro Carvalho Chehab
1164151c468bSMauro Carvalho Chehab	create_parameterlist($members, ';', $file, $declaration_name);
11651081de2dSMauro Carvalho Chehab	check_sections($file, $declaration_name, $decl_type, $sectcheck, $struct_actual);
11661da177e4SLinus Torvalds
11678ad72163SMauro Carvalho Chehab	# Adjust declaration for better display
1168673bb2dfSBen Hutchings	$declaration =~ s/([\{;])/$1\n/g;
1169673bb2dfSBen Hutchings	$declaration =~ s/\}\s+;/};/g;
11708ad72163SMauro Carvalho Chehab	# Better handle inlined enums
1171673bb2dfSBen Hutchings	do {} while ($declaration =~ s/(enum\s+\{[^\}]+),([^\n])/$1,\n$2/);
11728ad72163SMauro Carvalho Chehab
11738ad72163SMauro Carvalho Chehab	my @def_args = split /\n/, $declaration;
11748ad72163SMauro Carvalho Chehab	my $level = 1;
11758ad72163SMauro Carvalho Chehab	$declaration = "";
11768ad72163SMauro Carvalho Chehab	foreach my $clause (@def_args) {
11778ad72163SMauro Carvalho Chehab		$clause =~ s/^\s+//;
11788ad72163SMauro Carvalho Chehab		$clause =~ s/\s+$//;
11798ad72163SMauro Carvalho Chehab		$clause =~ s/\s+/ /;
11808ad72163SMauro Carvalho Chehab		next if (!$clause);
1181673bb2dfSBen Hutchings		$level-- if ($clause =~ m/(\})/ && $level > 1);
11828ad72163SMauro Carvalho Chehab		if (!($clause =~ m/^\s*#/)) {
11838ad72163SMauro Carvalho Chehab			$declaration .= "\t" x $level;
11848ad72163SMauro Carvalho Chehab		}
11858ad72163SMauro Carvalho Chehab		$declaration .= "\t" . $clause . "\n";
1186673bb2dfSBen Hutchings		$level++ if ($clause =~ m/(\{)/ && !($clause =~m/\}/));
11878ad72163SMauro Carvalho Chehab	}
11881da177e4SLinus Torvalds	output_declaration($declaration_name,
11891da177e4SLinus Torvalds			   'struct',
11901da177e4SLinus Torvalds			   {'struct' => $declaration_name,
11911da177e4SLinus Torvalds			    'module' => $modulename,
11928ad72163SMauro Carvalho Chehab			    'definition' => $declaration,
11931da177e4SLinus Torvalds			    'parameterlist' => \@parameterlist,
11941da177e4SLinus Torvalds			    'parameterdescs' => \%parameterdescs,
11951da177e4SLinus Torvalds			    'parametertypes' => \%parametertypes,
11961da177e4SLinus Torvalds			    'sectionlist' => \@sectionlist,
11971da177e4SLinus Torvalds			    'sections' => \%sections,
11981da177e4SLinus Torvalds			    'purpose' => $declaration_purpose,
11991da177e4SLinus Torvalds			    'type' => $decl_type
12001da177e4SLinus Torvalds			   });
12011da177e4SLinus Torvalds    }
12021da177e4SLinus Torvalds    else {
1203d40e1e65SBart Van Assche	print STDERR "${file}:$.: error: Cannot parse struct or union!\n";
12041da177e4SLinus Torvalds	++$errors;
12051da177e4SLinus Torvalds    }
12061da177e4SLinus Torvalds}
12071da177e4SLinus Torvalds
120885afe608SMauro Carvalho Chehab
120985afe608SMauro Carvalho Chehabsub show_warnings($$) {
121085afe608SMauro Carvalho Chehab	my $functype = shift;
121185afe608SMauro Carvalho Chehab	my $name = shift;
121285afe608SMauro Carvalho Chehab
121385afe608SMauro Carvalho Chehab	return 1 if ($output_selection == OUTPUT_ALL);
121485afe608SMauro Carvalho Chehab
121585afe608SMauro Carvalho Chehab	if ($output_selection == OUTPUT_EXPORTED) {
121685afe608SMauro Carvalho Chehab		if (defined($function_table{$name})) {
121785afe608SMauro Carvalho Chehab			return 1;
121885afe608SMauro Carvalho Chehab		} else {
121985afe608SMauro Carvalho Chehab			return 0;
122085afe608SMauro Carvalho Chehab		}
122185afe608SMauro Carvalho Chehab	}
122285afe608SMauro Carvalho Chehab        if ($output_selection == OUTPUT_INTERNAL) {
122385afe608SMauro Carvalho Chehab		if (!($functype eq "function" && defined($function_table{$name}))) {
122485afe608SMauro Carvalho Chehab			return 1;
122585afe608SMauro Carvalho Chehab		} else {
122685afe608SMauro Carvalho Chehab			return 0;
122785afe608SMauro Carvalho Chehab		}
122885afe608SMauro Carvalho Chehab	}
122985afe608SMauro Carvalho Chehab	if ($output_selection == OUTPUT_INCLUDE) {
123085afe608SMauro Carvalho Chehab		if (defined($function_table{$name})) {
123185afe608SMauro Carvalho Chehab			return 1;
123285afe608SMauro Carvalho Chehab		} else {
123385afe608SMauro Carvalho Chehab			return 0;
123485afe608SMauro Carvalho Chehab		}
123585afe608SMauro Carvalho Chehab	}
123685afe608SMauro Carvalho Chehab	if ($output_selection == OUTPUT_EXCLUDE) {
123785afe608SMauro Carvalho Chehab		if (!defined($function_table{$name})) {
123885afe608SMauro Carvalho Chehab			return 1;
123985afe608SMauro Carvalho Chehab		} else {
124085afe608SMauro Carvalho Chehab			return 0;
124185afe608SMauro Carvalho Chehab		}
124285afe608SMauro Carvalho Chehab	}
124385afe608SMauro Carvalho Chehab	die("Please add the new output type at show_warnings()");
124485afe608SMauro Carvalho Chehab}
124585afe608SMauro Carvalho Chehab
12461da177e4SLinus Torvaldssub dump_enum($$) {
12471da177e4SLinus Torvalds    my $x = shift;
12481da177e4SLinus Torvalds    my $file = shift;
12491da177e4SLinus Torvalds
1250aeec46b9SMartin Waitz    $x =~ s@/\*.*?\*/@@gos;	# strip comments.
12514468e21eSConchúr Navid    # strip #define macros inside enums
12524468e21eSConchúr Navid    $x =~ s@#\s*((define|ifdef)\s+|endif)[^;]*;@@gos;
1253b6d676dbSRandy Dunlap
125415e2544eSAndy Shevchenko    if ($x =~ /enum\s+(\w*)\s*\{(.*)\}/) {
12551da177e4SLinus Torvalds	$declaration_name = $1;
12561da177e4SLinus Torvalds	my $members = $2;
12575cb5c31cSJohannes Berg	my %_members;
12585cb5c31cSJohannes Berg
1259463a0fdcSMarkus Heiser	$members =~ s/\s+$//;
12601da177e4SLinus Torvalds
12611da177e4SLinus Torvalds	foreach my $arg (split ',', $members) {
12621da177e4SLinus Torvalds	    $arg =~ s/^\s*(\w+).*/$1/;
12631da177e4SLinus Torvalds	    push @parameterlist, $arg;
12641da177e4SLinus Torvalds	    if (!$parameterdescs{$arg}) {
12651da177e4SLinus Torvalds		$parameterdescs{$arg} = $undescribed;
126685afe608SMauro Carvalho Chehab	        if (show_warnings("enum", $declaration_name)) {
12672defb272SMauro Carvalho Chehab			print STDERR "${file}:$.: warning: Enum value '$arg' not described in enum '$declaration_name'\n";
12682defb272SMauro Carvalho Chehab		}
12691da177e4SLinus Torvalds	    }
12705cb5c31cSJohannes Berg	    $_members{$arg} = 1;
12715cb5c31cSJohannes Berg	}
12721da177e4SLinus Torvalds
12735cb5c31cSJohannes Berg	while (my ($k, $v) = each %parameterdescs) {
12745cb5c31cSJohannes Berg	    if (!exists($_members{$k})) {
127585afe608SMauro Carvalho Chehab	        if (show_warnings("enum", $declaration_name)) {
12762defb272SMauro Carvalho Chehab		     print STDERR "${file}:$.: warning: Excess enum value '$k' description in '$declaration_name'\n";
12772defb272SMauro Carvalho Chehab		}
12785cb5c31cSJohannes Berg	    }
12791da177e4SLinus Torvalds        }
12801da177e4SLinus Torvalds
12811da177e4SLinus Torvalds	output_declaration($declaration_name,
12821da177e4SLinus Torvalds			   'enum',
12831da177e4SLinus Torvalds			   {'enum' => $declaration_name,
12841da177e4SLinus Torvalds			    'module' => $modulename,
12851da177e4SLinus Torvalds			    'parameterlist' => \@parameterlist,
12861da177e4SLinus Torvalds			    'parameterdescs' => \%parameterdescs,
12871da177e4SLinus Torvalds			    'sectionlist' => \@sectionlist,
12881da177e4SLinus Torvalds			    'sections' => \%sections,
12891da177e4SLinus Torvalds			    'purpose' => $declaration_purpose
12901da177e4SLinus Torvalds			   });
12911da177e4SLinus Torvalds    }
12921da177e4SLinus Torvalds    else {
1293d40e1e65SBart Van Assche	print STDERR "${file}:$.: error: Cannot parse enum!\n";
12941da177e4SLinus Torvalds	++$errors;
12951da177e4SLinus Torvalds    }
12961da177e4SLinus Torvalds}
12971da177e4SLinus Torvalds
12981da177e4SLinus Torvaldssub dump_typedef($$) {
12991da177e4SLinus Torvalds    my $x = shift;
13001da177e4SLinus Torvalds    my $file = shift;
13011da177e4SLinus Torvalds
1302aeec46b9SMartin Waitz    $x =~ s@/\*.*?\*/@@gos;	# strip comments.
130383766452SMauro Carvalho Chehab
130483766452SMauro Carvalho Chehab    # Parse function prototypes
1305d37c43ceSMauro Carvalho Chehab    if ($x =~ /typedef\s+(\w+)\s*\(\*\s*(\w\S+)\s*\)\s*\((.*)\);/ ||
1306d37c43ceSMauro Carvalho Chehab	$x =~ /typedef\s+(\w+)\s*(\w\S+)\s*\s*\((.*)\);/) {
1307d37c43ceSMauro Carvalho Chehab
130883766452SMauro Carvalho Chehab	# Function typedefs
130983766452SMauro Carvalho Chehab	$return_type = $1;
131083766452SMauro Carvalho Chehab	$declaration_name = $2;
131183766452SMauro Carvalho Chehab	my $args = $3;
131283766452SMauro Carvalho Chehab
1313151c468bSMauro Carvalho Chehab	create_parameterlist($args, ',', $file, $declaration_name);
131483766452SMauro Carvalho Chehab
131583766452SMauro Carvalho Chehab	output_declaration($declaration_name,
131683766452SMauro Carvalho Chehab			   'function',
131783766452SMauro Carvalho Chehab			   {'function' => $declaration_name,
131882801d06SMauro Carvalho Chehab			    'typedef' => 1,
131983766452SMauro Carvalho Chehab			    'module' => $modulename,
132083766452SMauro Carvalho Chehab			    'functiontype' => $return_type,
132183766452SMauro Carvalho Chehab			    'parameterlist' => \@parameterlist,
132283766452SMauro Carvalho Chehab			    'parameterdescs' => \%parameterdescs,
132383766452SMauro Carvalho Chehab			    'parametertypes' => \%parametertypes,
132483766452SMauro Carvalho Chehab			    'sectionlist' => \@sectionlist,
132583766452SMauro Carvalho Chehab			    'sections' => \%sections,
132683766452SMauro Carvalho Chehab			    'purpose' => $declaration_purpose
132783766452SMauro Carvalho Chehab			   });
132883766452SMauro Carvalho Chehab	return;
132983766452SMauro Carvalho Chehab    }
133083766452SMauro Carvalho Chehab
13311da177e4SLinus Torvalds    while (($x =~ /\(*.\)\s*;$/) || ($x =~ /\[*.\]\s*;$/)) {
13321da177e4SLinus Torvalds	$x =~ s/\(*.\)\s*;$/;/;
13331da177e4SLinus Torvalds	$x =~ s/\[*.\]\s*;$/;/;
13341da177e4SLinus Torvalds    }
13351da177e4SLinus Torvalds
13361da177e4SLinus Torvalds    if ($x =~ /typedef.*\s+(\w+)\s*;/) {
13371da177e4SLinus Torvalds	$declaration_name = $1;
13381da177e4SLinus Torvalds
13391da177e4SLinus Torvalds	output_declaration($declaration_name,
13401da177e4SLinus Torvalds			   'typedef',
13411da177e4SLinus Torvalds			   {'typedef' => $declaration_name,
13421da177e4SLinus Torvalds			    'module' => $modulename,
13431da177e4SLinus Torvalds			    'sectionlist' => \@sectionlist,
13441da177e4SLinus Torvalds			    'sections' => \%sections,
13451da177e4SLinus Torvalds			    'purpose' => $declaration_purpose
13461da177e4SLinus Torvalds			   });
13471da177e4SLinus Torvalds    }
13481da177e4SLinus Torvalds    else {
1349d40e1e65SBart Van Assche	print STDERR "${file}:$.: error: Cannot parse typedef!\n";
13501da177e4SLinus Torvalds	++$errors;
13511da177e4SLinus Torvalds    }
13521da177e4SLinus Torvalds}
13531da177e4SLinus Torvalds
1354a1d94aa5SRandy Dunlapsub save_struct_actual($) {
1355a1d94aa5SRandy Dunlap    my $actual = shift;
1356a1d94aa5SRandy Dunlap
1357a1d94aa5SRandy Dunlap    # strip all spaces from the actual param so that it looks like one string item
1358a1d94aa5SRandy Dunlap    $actual =~ s/\s*//g;
1359a1d94aa5SRandy Dunlap    $struct_actual = $struct_actual . $actual . " ";
1360a1d94aa5SRandy Dunlap}
1361a1d94aa5SRandy Dunlap
1362151c468bSMauro Carvalho Chehabsub create_parameterlist($$$$) {
13631da177e4SLinus Torvalds    my $args = shift;
13641da177e4SLinus Torvalds    my $splitter = shift;
13651da177e4SLinus Torvalds    my $file = shift;
1366151c468bSMauro Carvalho Chehab    my $declaration_name = shift;
13671da177e4SLinus Torvalds    my $type;
13681da177e4SLinus Torvalds    my $param;
13691da177e4SLinus Torvalds
1370a6d3fe77SMartin Waitz    # temporarily replace commas inside function pointer definition
13711da177e4SLinus Torvalds    while ($args =~ /(\([^\),]+),/) {
13721da177e4SLinus Torvalds	$args =~ s/(\([^\),]+),/$1#/g;
13731da177e4SLinus Torvalds    }
13741da177e4SLinus Torvalds
13751da177e4SLinus Torvalds    foreach my $arg (split($splitter, $args)) {
13761da177e4SLinus Torvalds	# strip comments
13771da177e4SLinus Torvalds	$arg =~ s/\/\*.*\*\///;
13781da177e4SLinus Torvalds	# strip leading/trailing spaces
13791da177e4SLinus Torvalds	$arg =~ s/^\s*//;
13801da177e4SLinus Torvalds	$arg =~ s/\s*$//;
13811da177e4SLinus Torvalds	$arg =~ s/\s+/ /;
13821da177e4SLinus Torvalds
13831da177e4SLinus Torvalds	if ($arg =~ /^#/) {
13841da177e4SLinus Torvalds	    # Treat preprocessor directive as a typeless variable just to fill
13851da177e4SLinus Torvalds	    # corresponding data structures "correctly". Catch it later in
13861da177e4SLinus Torvalds	    # output_* subs.
13871da177e4SLinus Torvalds	    push_parameter($arg, "", $file);
138800d62961SRichard Kennedy	} elsif ($arg =~ m/\(.+\)\s*\(/) {
13891da177e4SLinus Torvalds	    # pointer-to-function
13901da177e4SLinus Torvalds	    $arg =~ tr/#/,/;
13917c0d7e87SMauro Carvalho Chehab	    $arg =~ m/[^\(]+\(\*?\s*([\w\.]*)\s*\)/;
13921da177e4SLinus Torvalds	    $param = $1;
13931da177e4SLinus Torvalds	    $type = $arg;
139400d62961SRichard Kennedy	    $type =~ s/([^\(]+\(\*?)\s*$param/$1/;
1395a1d94aa5SRandy Dunlap	    save_struct_actual($param);
1396151c468bSMauro Carvalho Chehab	    push_parameter($param, $type, $file, $declaration_name);
1397aeec46b9SMartin Waitz	} elsif ($arg) {
13981da177e4SLinus Torvalds	    $arg =~ s/\s*:\s*/:/g;
13991da177e4SLinus Torvalds	    $arg =~ s/\s*\[/\[/g;
14001da177e4SLinus Torvalds
14011da177e4SLinus Torvalds	    my @args = split('\s*,\s*', $arg);
14021da177e4SLinus Torvalds	    if ($args[0] =~ m/\*/) {
14031da177e4SLinus Torvalds		$args[0] =~ s/(\*+)\s*/ $1/;
14041da177e4SLinus Torvalds	    }
1405884f2810SBorislav Petkov
1406884f2810SBorislav Petkov	    my @first_arg;
1407884f2810SBorislav Petkov	    if ($args[0] =~ /^(.*\s+)(.*?\[.*\].*)$/) {
1408884f2810SBorislav Petkov		    shift @args;
1409884f2810SBorislav Petkov		    push(@first_arg, split('\s+', $1));
1410884f2810SBorislav Petkov		    push(@first_arg, $2);
1411884f2810SBorislav Petkov	    } else {
1412884f2810SBorislav Petkov		    @first_arg = split('\s+', shift @args);
1413884f2810SBorislav Petkov	    }
1414884f2810SBorislav Petkov
14151da177e4SLinus Torvalds	    unshift(@args, pop @first_arg);
14161da177e4SLinus Torvalds	    $type = join " ", @first_arg;
14171da177e4SLinus Torvalds
14181da177e4SLinus Torvalds	    foreach $param (@args) {
14191da177e4SLinus Torvalds		if ($param =~ m/^(\*+)\s*(.*)/) {
1420a1d94aa5SRandy Dunlap		    save_struct_actual($2);
1421151c468bSMauro Carvalho Chehab		    push_parameter($2, "$type $1", $file, $declaration_name);
14221da177e4SLinus Torvalds		}
14231da177e4SLinus Torvalds		elsif ($param =~ m/(.*?):(\d+)/) {
14247b97887eSRandy Dunlap		    if ($type ne "") { # skip unnamed bit-fields
1425a1d94aa5SRandy Dunlap			save_struct_actual($1);
1426151c468bSMauro Carvalho Chehab			push_parameter($1, "$type:$2", $file, $declaration_name)
14271da177e4SLinus Torvalds		    }
14287b97887eSRandy Dunlap		}
14291da177e4SLinus Torvalds		else {
1430a1d94aa5SRandy Dunlap		    save_struct_actual($param);
1431151c468bSMauro Carvalho Chehab		    push_parameter($param, $type, $file, $declaration_name);
14321da177e4SLinus Torvalds		}
14331da177e4SLinus Torvalds	    }
14341da177e4SLinus Torvalds	}
14351da177e4SLinus Torvalds    }
14361da177e4SLinus Torvalds}
14371da177e4SLinus Torvalds
1438151c468bSMauro Carvalho Chehabsub push_parameter($$$$) {
14391da177e4SLinus Torvalds	my $param = shift;
14401da177e4SLinus Torvalds	my $type = shift;
14411da177e4SLinus Torvalds	my $file = shift;
1442151c468bSMauro Carvalho Chehab	my $declaration_name = shift;
14431da177e4SLinus Torvalds
14445f8c7c98SRandy Dunlap	if (($anon_struct_union == 1) && ($type eq "") &&
14455f8c7c98SRandy Dunlap	    ($param eq "}")) {
14465f8c7c98SRandy Dunlap		return;		# ignore the ending }; from anon. struct/union
14475f8c7c98SRandy Dunlap	}
14485f8c7c98SRandy Dunlap
14495f8c7c98SRandy Dunlap	$anon_struct_union = 0;
1450f9b5c530SMauro Carvalho Chehab	$param =~ s/[\[\)].*//;
14511da177e4SLinus Torvalds
1452a6d3fe77SMartin Waitz	if ($type eq "" && $param =~ /\.\.\.$/)
14531da177e4SLinus Torvalds	{
1454c950a173SSilvio Fricke	    if (!$param =~ /\w\.\.\.$/) {
1455c950a173SSilvio Fricke	      # handles unnamed variable parameters
1456ef00028bSJonathan Corbet	      $param = "...";
1457c950a173SSilvio Fricke	    }
145843756e34SJonathan Neuschäfer	    elsif ($param =~ /\w\.\.\.$/) {
145943756e34SJonathan Neuschäfer	      # for named variable parameters of the form `x...`, remove the dots
146043756e34SJonathan Neuschäfer	      $param =~ s/\.\.\.$//;
146143756e34SJonathan Neuschäfer	    }
1462ced69090SRandy Dunlap	    if (!defined $parameterdescs{$param} || $parameterdescs{$param} eq "") {
1463a6d3fe77SMartin Waitz		$parameterdescs{$param} = "variable arguments";
14641da177e4SLinus Torvalds	    }
1465ced69090SRandy Dunlap	}
14661da177e4SLinus Torvalds	elsif ($type eq "" && ($param eq "" or $param eq "void"))
14671da177e4SLinus Torvalds	{
14681da177e4SLinus Torvalds	    $param="void";
14691da177e4SLinus Torvalds	    $parameterdescs{void} = "no arguments";
14701da177e4SLinus Torvalds	}
1471134fe01bSRandy Dunlap	elsif ($type eq "" && ($param eq "struct" or $param eq "union"))
1472134fe01bSRandy Dunlap	# handle unnamed (anonymous) union or struct:
1473134fe01bSRandy Dunlap	{
1474134fe01bSRandy Dunlap		$type = $param;
1475134fe01bSRandy Dunlap		$param = "{unnamed_" . $param . "}";
1476134fe01bSRandy Dunlap		$parameterdescs{$param} = "anonymous\n";
14775f8c7c98SRandy Dunlap		$anon_struct_union = 1;
1478134fe01bSRandy Dunlap	}
1479134fe01bSRandy Dunlap
1480a6d3fe77SMartin Waitz	# warn if parameter has no description
1481134fe01bSRandy Dunlap	# (but ignore ones starting with # as these are not parameters
1482134fe01bSRandy Dunlap	# but inline preprocessor statements);
1483151c468bSMauro Carvalho Chehab	# Note: It will also ignore void params and unnamed structs/unions
1484f9b5c530SMauro Carvalho Chehab	if (!defined $parameterdescs{$param} && $param !~ /^#/) {
1485f9b5c530SMauro Carvalho Chehab		$parameterdescs{$param} = $undescribed;
14861da177e4SLinus Torvalds
1487be5cd20cSJonathan Corbet	        if (show_warnings($type, $declaration_name) && $param !~ /\./) {
1488151c468bSMauro Carvalho Chehab			print STDERR
1489151c468bSMauro Carvalho Chehab			      "${file}:$.: warning: Function parameter or member '$param' not described in '$declaration_name'\n";
14901da177e4SLinus Torvalds			++$warnings;
14911da177e4SLinus Torvalds		}
14922defb272SMauro Carvalho Chehab	}
14931da177e4SLinus Torvalds
149425985edcSLucas De Marchi	# strip spaces from $param so that it is one continuous string
1495e34e7dbbSRandy Dunlap	# on @parameterlist;
1496e34e7dbbSRandy Dunlap	# this fixes a problem where check_sections() cannot find
1497e34e7dbbSRandy Dunlap	# a parameter like "addr[6 + 2]" because it actually appears
1498e34e7dbbSRandy Dunlap	# as "addr[6", "+", "2]" on the parameter list;
1499e34e7dbbSRandy Dunlap	# but it's better to maintain the param string unchanged for output,
1500e34e7dbbSRandy Dunlap	# so just weaken the string compare in check_sections() to ignore
1501e34e7dbbSRandy Dunlap	# "[blah" in a parameter string;
1502e34e7dbbSRandy Dunlap	###$param =~ s/\s*//g;
15031da177e4SLinus Torvalds	push @parameterlist, $param;
150402a4f4feSPaolo Bonzini	$type =~ s/\s\s+/ /g;
15051da177e4SLinus Torvalds	$parametertypes{$param} = $type;
15061da177e4SLinus Torvalds}
15071da177e4SLinus Torvalds
15081081de2dSMauro Carvalho Chehabsub check_sections($$$$$) {
15091081de2dSMauro Carvalho Chehab	my ($file, $decl_name, $decl_type, $sectcheck, $prmscheck) = @_;
1510a1d94aa5SRandy Dunlap	my @sects = split ' ', $sectcheck;
1511a1d94aa5SRandy Dunlap	my @prms = split ' ', $prmscheck;
1512a1d94aa5SRandy Dunlap	my $err;
1513a1d94aa5SRandy Dunlap	my ($px, $sx);
1514a1d94aa5SRandy Dunlap	my $prm_clean;		# strip trailing "[array size]" and/or beginning "*"
1515a1d94aa5SRandy Dunlap
1516a1d94aa5SRandy Dunlap	foreach $sx (0 .. $#sects) {
1517a1d94aa5SRandy Dunlap		$err = 1;
1518a1d94aa5SRandy Dunlap		foreach $px (0 .. $#prms) {
1519a1d94aa5SRandy Dunlap			$prm_clean = $prms[$px];
1520a1d94aa5SRandy Dunlap			$prm_clean =~ s/\[.*\]//;
15211f3a6688SJohannes Berg			$prm_clean =~ s/__attribute__\s*\(\([a-z,_\*\s\(\)]*\)\)//i;
1522e34e7dbbSRandy Dunlap			# ignore array size in a parameter string;
1523e34e7dbbSRandy Dunlap			# however, the original param string may contain
1524e34e7dbbSRandy Dunlap			# spaces, e.g.:  addr[6 + 2]
1525e34e7dbbSRandy Dunlap			# and this appears in @prms as "addr[6" since the
1526e34e7dbbSRandy Dunlap			# parameter list is split at spaces;
1527e34e7dbbSRandy Dunlap			# hence just ignore "[..." for the sections check;
1528e34e7dbbSRandy Dunlap			$prm_clean =~ s/\[.*//;
1529e34e7dbbSRandy Dunlap
1530a1d94aa5SRandy Dunlap			##$prm_clean =~ s/^\**//;
1531a1d94aa5SRandy Dunlap			if ($prm_clean eq $sects[$sx]) {
1532a1d94aa5SRandy Dunlap				$err = 0;
1533a1d94aa5SRandy Dunlap				last;
1534a1d94aa5SRandy Dunlap			}
1535a1d94aa5SRandy Dunlap		}
1536a1d94aa5SRandy Dunlap		if ($err) {
1537a1d94aa5SRandy Dunlap			if ($decl_type eq "function") {
1538d40e1e65SBart Van Assche				print STDERR "${file}:$.: warning: " .
1539a1d94aa5SRandy Dunlap					"Excess function parameter " .
1540a1d94aa5SRandy Dunlap					"'$sects[$sx]' " .
1541a1d94aa5SRandy Dunlap					"description in '$decl_name'\n";
1542a1d94aa5SRandy Dunlap				++$warnings;
1543a1d94aa5SRandy Dunlap			}
1544a1d94aa5SRandy Dunlap		}
1545a1d94aa5SRandy Dunlap	}
1546a1d94aa5SRandy Dunlap}
1547a1d94aa5SRandy Dunlap
15481da177e4SLinus Torvalds##
15494092bac7SYacine Belkadi# Checks the section describing the return value of a function.
15504092bac7SYacine Belkadisub check_return_section {
15514092bac7SYacine Belkadi        my $file = shift;
15524092bac7SYacine Belkadi        my $declaration_name = shift;
15534092bac7SYacine Belkadi        my $return_type = shift;
15544092bac7SYacine Belkadi
15554092bac7SYacine Belkadi        # Ignore an empty return type (It's a macro)
15564092bac7SYacine Belkadi        # Ignore functions with a "void" return type. (But don't ignore "void *")
15574092bac7SYacine Belkadi        if (($return_type eq "") || ($return_type =~ /void\s*\w*\s*$/)) {
15584092bac7SYacine Belkadi                return;
15594092bac7SYacine Belkadi        }
15604092bac7SYacine Belkadi
15614092bac7SYacine Belkadi        if (!defined($sections{$section_return}) ||
15624092bac7SYacine Belkadi            $sections{$section_return} eq "") {
1563d40e1e65SBart Van Assche                print STDERR "${file}:$.: warning: " .
15644092bac7SYacine Belkadi                        "No description found for return value of " .
15654092bac7SYacine Belkadi                        "'$declaration_name'\n";
15664092bac7SYacine Belkadi                ++$warnings;
15674092bac7SYacine Belkadi        }
15684092bac7SYacine Belkadi}
15694092bac7SYacine Belkadi
15704092bac7SYacine Belkadi##
15711da177e4SLinus Torvalds# takes a function prototype and the name of the current file being
15721da177e4SLinus Torvalds# processed and spits out all the details stored in the global
15731da177e4SLinus Torvalds# arrays/hashes.
15741da177e4SLinus Torvaldssub dump_function($$) {
15751da177e4SLinus Torvalds    my $prototype = shift;
15761da177e4SLinus Torvalds    my $file = shift;
1577cbb4d3e6SHoria Geanta    my $noret = 0;
15781da177e4SLinus Torvalds
15791da177e4SLinus Torvalds    $prototype =~ s/^static +//;
15801da177e4SLinus Torvalds    $prototype =~ s/^extern +//;
15814dc3b16bSPavel Pisa    $prototype =~ s/^asmlinkage +//;
15821da177e4SLinus Torvalds    $prototype =~ s/^inline +//;
15831da177e4SLinus Torvalds    $prototype =~ s/^__inline__ +//;
158432e79401SRandy Dunlap    $prototype =~ s/^__inline +//;
158532e79401SRandy Dunlap    $prototype =~ s/^__always_inline +//;
158632e79401SRandy Dunlap    $prototype =~ s/^noinline +//;
158774fc5c65SRandy Dunlap    $prototype =~ s/__init +//;
158820072205SRandy Dunlap    $prototype =~ s/__init_or_module +//;
1589270a0096SRandy Dunlap    $prototype =~ s/__meminit +//;
159070c95b00SRandy Dunlap    $prototype =~ s/__must_check +//;
15910df7c0e3SRandy Dunlap    $prototype =~ s/__weak +//;
15920891f959SMatthew Wilcox    $prototype =~ s/__sched +//;
159395e760cbSRandy Dunlap    $prototype =~ s/__printf\s*\(\s*\d*\s*,\s*\d*\s*\) +//;
1594cbb4d3e6SHoria Geanta    my $define = $prototype =~ s/^#\s*define\s+//; #ak added
1595b1aaa546SPaolo Bonzini    $prototype =~ s/__attribute__\s*\(\(
1596b1aaa546SPaolo Bonzini            (?:
1597b1aaa546SPaolo Bonzini                 [\w\s]++          # attribute name
1598b1aaa546SPaolo Bonzini                 (?:\([^)]*+\))?   # attribute arguments
1599b1aaa546SPaolo Bonzini                 \s*+,?            # optional comma at the end
1600b1aaa546SPaolo Bonzini            )+
1601b1aaa546SPaolo Bonzini          \)\)\s+//x;
16021da177e4SLinus Torvalds
16031da177e4SLinus Torvalds    # Yes, this truly is vile.  We are looking for:
16041da177e4SLinus Torvalds    # 1. Return type (may be nothing if we're looking at a macro)
16051da177e4SLinus Torvalds    # 2. Function name
16061da177e4SLinus Torvalds    # 3. Function parameters.
16071da177e4SLinus Torvalds    #
16081da177e4SLinus Torvalds    # All the while we have to watch out for function pointer parameters
16091da177e4SLinus Torvalds    # (which IIRC is what the two sections are for), C types (these
16101da177e4SLinus Torvalds    # regexps don't even start to express all the possibilities), and
16111da177e4SLinus Torvalds    # so on.
16121da177e4SLinus Torvalds    #
16131da177e4SLinus Torvalds    # If you mess with these regexps, it's a good idea to check that
16141da177e4SLinus Torvalds    # the following functions' documentation still comes out right:
16151da177e4SLinus Torvalds    # - parport_register_device (function pointer parameters)
16161da177e4SLinus Torvalds    # - atomic_set (macro)
16179598f91fSMartin Waitz    # - pci_match_device, __copy_to_user (long return type)
16181da177e4SLinus Torvalds
1619cbb4d3e6SHoria Geanta    if ($define && $prototype =~ m/^()([a-zA-Z0-9_~:]+)\s+/) {
1620cbb4d3e6SHoria Geanta        # This is an object-like macro, it has no return type and no parameter
1621cbb4d3e6SHoria Geanta        # list.
1622cbb4d3e6SHoria Geanta        # Function-like macros are not allowed to have spaces between
1623cbb4d3e6SHoria Geanta        # declaration_name and opening parenthesis (notice the \s+).
1624cbb4d3e6SHoria Geanta        $return_type = $1;
1625cbb4d3e6SHoria Geanta        $declaration_name = $2;
1626cbb4d3e6SHoria Geanta        $noret = 1;
1627cbb4d3e6SHoria Geanta    } elsif ($prototype =~ m/^()([a-zA-Z0-9_~:]+)\s*\(([^\(]*)\)/ ||
16281da177e4SLinus Torvalds	$prototype =~ m/^(\w+)\s+([a-zA-Z0-9_~:]+)\s*\(([^\(]*)\)/ ||
16295a0bc578SMatthew Wilcox	$prototype =~ m/^(\w+\s*\*+)\s*([a-zA-Z0-9_~:]+)\s*\(([^\(]*)\)/ ||
16301da177e4SLinus Torvalds	$prototype =~ m/^(\w+\s+\w+)\s+([a-zA-Z0-9_~:]+)\s*\(([^\(]*)\)/ ||
163194b3e03cSRandy Dunlap	$prototype =~ m/^(\w+\s+\w+\s*\*+)\s*([a-zA-Z0-9_~:]+)\s*\(([^\(]*)\)/ ||
16321da177e4SLinus Torvalds	$prototype =~ m/^(\w+\s+\w+\s+\w+)\s+([a-zA-Z0-9_~:]+)\s*\(([^\(]*)\)/ ||
16335a0bc578SMatthew Wilcox	$prototype =~ m/^(\w+\s+\w+\s+\w+\s*\*+)\s*([a-zA-Z0-9_~:]+)\s*\(([^\(]*)\)/ ||
16341da177e4SLinus Torvalds	$prototype =~ m/^()([a-zA-Z0-9_~:]+)\s*\(([^\{]*)\)/ ||
16351da177e4SLinus Torvalds	$prototype =~ m/^(\w+)\s+([a-zA-Z0-9_~:]+)\s*\(([^\{]*)\)/ ||
16365a0bc578SMatthew Wilcox	$prototype =~ m/^(\w+\s*\*+)\s*([a-zA-Z0-9_~:]+)\s*\(([^\{]*)\)/ ||
16371da177e4SLinus Torvalds	$prototype =~ m/^(\w+\s+\w+)\s+([a-zA-Z0-9_~:]+)\s*\(([^\{]*)\)/ ||
16385a0bc578SMatthew Wilcox	$prototype =~ m/^(\w+\s+\w+\s*\*+)\s*([a-zA-Z0-9_~:]+)\s*\(([^\{]*)\)/ ||
16391da177e4SLinus Torvalds	$prototype =~ m/^(\w+\s+\w+\s+\w+)\s+([a-zA-Z0-9_~:]+)\s*\(([^\{]*)\)/ ||
16405a0bc578SMatthew Wilcox	$prototype =~ m/^(\w+\s+\w+\s+\w+\s*\*+)\s*([a-zA-Z0-9_~:]+)\s*\(([^\{]*)\)/ ||
16419598f91fSMartin Waitz	$prototype =~ m/^(\w+\s+\w+\s+\w+\s+\w+)\s+([a-zA-Z0-9_~:]+)\s*\(([^\{]*)\)/ ||
16425a0bc578SMatthew Wilcox	$prototype =~ m/^(\w+\s+\w+\s+\w+\s+\w+\s*\*+)\s*([a-zA-Z0-9_~:]+)\s*\(([^\{]*)\)/ ||
16435a0bc578SMatthew Wilcox	$prototype =~ m/^(\w+\s+\w+\s*\*+\s*\w+\s*\*+\s*)\s*([a-zA-Z0-9_~:]+)\s*\(([^\{]*)\)/)  {
16441da177e4SLinus Torvalds	$return_type = $1;
16451da177e4SLinus Torvalds	$declaration_name = $2;
16461da177e4SLinus Torvalds	my $args = $3;
16471da177e4SLinus Torvalds
1648151c468bSMauro Carvalho Chehab	create_parameterlist($args, ',', $file, $declaration_name);
16491da177e4SLinus Torvalds    } else {
1650d40e1e65SBart Van Assche	print STDERR "${file}:$.: warning: cannot understand function prototype: '$prototype'\n";
16511da177e4SLinus Torvalds	return;
16521da177e4SLinus Torvalds    }
16531da177e4SLinus Torvalds
1654a1d94aa5SRandy Dunlap	my $prms = join " ", @parameterlist;
16551081de2dSMauro Carvalho Chehab	check_sections($file, $declaration_name, "function", $sectcheck, $prms);
1656a1d94aa5SRandy Dunlap
16574092bac7SYacine Belkadi        # This check emits a lot of warnings at the moment, because many
16584092bac7SYacine Belkadi        # functions don't have a 'Return' doc section. So until the number
16594092bac7SYacine Belkadi        # of warnings goes sufficiently down, the check is only performed in
16604092bac7SYacine Belkadi        # verbose mode.
16614092bac7SYacine Belkadi        # TODO: always perform the check.
1662cbb4d3e6SHoria Geanta        if ($verbose && !$noret) {
16634092bac7SYacine Belkadi                check_return_section($file, $declaration_name, $return_type);
16644092bac7SYacine Belkadi        }
16654092bac7SYacine Belkadi
16661da177e4SLinus Torvalds    output_declaration($declaration_name,
16671da177e4SLinus Torvalds		       'function',
16681da177e4SLinus Torvalds		       {'function' => $declaration_name,
16691da177e4SLinus Torvalds			'module' => $modulename,
16701da177e4SLinus Torvalds			'functiontype' => $return_type,
16711da177e4SLinus Torvalds			'parameterlist' => \@parameterlist,
16721da177e4SLinus Torvalds			'parameterdescs' => \%parameterdescs,
16731da177e4SLinus Torvalds			'parametertypes' => \%parametertypes,
16741da177e4SLinus Torvalds			'sectionlist' => \@sectionlist,
16751da177e4SLinus Torvalds			'sections' => \%sections,
16761da177e4SLinus Torvalds			'purpose' => $declaration_purpose
16771da177e4SLinus Torvalds		       });
16781da177e4SLinus Torvalds}
16791da177e4SLinus Torvalds
16801da177e4SLinus Torvaldssub reset_state {
16811da177e4SLinus Torvalds    $function = "";
16821da177e4SLinus Torvalds    %parameterdescs = ();
16831da177e4SLinus Torvalds    %parametertypes = ();
16841da177e4SLinus Torvalds    @parameterlist = ();
16851da177e4SLinus Torvalds    %sections = ();
16861da177e4SLinus Torvalds    @sectionlist = ();
1687a1d94aa5SRandy Dunlap    $sectcheck = "";
1688a1d94aa5SRandy Dunlap    $struct_actual = "";
16891da177e4SLinus Torvalds    $prototype = "";
16901da177e4SLinus Torvalds
169148af606aSJani Nikula    $state = STATE_NORMAL;
169248af606aSJani Nikula    $inline_doc_state = STATE_INLINE_NA;
16931da177e4SLinus Torvalds}
16941da177e4SLinus Torvalds
169556afb0f8SJason Baronsub tracepoint_munge($) {
169656afb0f8SJason Baron	my $file = shift;
169756afb0f8SJason Baron	my $tracepointname = 0;
169856afb0f8SJason Baron	my $tracepointargs = 0;
169956afb0f8SJason Baron
170056afb0f8SJason Baron	if ($prototype =~ m/TRACE_EVENT\((.*?),/) {
170156afb0f8SJason Baron		$tracepointname = $1;
170256afb0f8SJason Baron	}
17033a9089fdSJason Baron	if ($prototype =~ m/DEFINE_SINGLE_EVENT\((.*?),/) {
17043a9089fdSJason Baron		$tracepointname = $1;
17053a9089fdSJason Baron	}
17063a9089fdSJason Baron	if ($prototype =~ m/DEFINE_EVENT\((.*?),(.*?),/) {
17073a9089fdSJason Baron		$tracepointname = $2;
17083a9089fdSJason Baron	}
17093a9089fdSJason Baron	$tracepointname =~ s/^\s+//; #strip leading whitespace
171056afb0f8SJason Baron	if ($prototype =~ m/TP_PROTO\((.*?)\)/) {
171156afb0f8SJason Baron		$tracepointargs = $1;
171256afb0f8SJason Baron	}
171356afb0f8SJason Baron	if (($tracepointname eq 0) || ($tracepointargs eq 0)) {
1714d40e1e65SBart Van Assche		print STDERR "${file}:$.: warning: Unrecognized tracepoint format: \n".
171556afb0f8SJason Baron			     "$prototype\n";
171656afb0f8SJason Baron	} else {
171756afb0f8SJason Baron		$prototype = "static inline void trace_$tracepointname($tracepointargs)";
171856afb0f8SJason Baron	}
171956afb0f8SJason Baron}
172056afb0f8SJason Baron
1721b4870bc5SRandy Dunlapsub syscall_munge() {
1722b4870bc5SRandy Dunlap	my $void = 0;
1723b4870bc5SRandy Dunlap
17247c9aa015SMauro Carvalho Chehab	$prototype =~ s@[\r\n]+@ @gos; # strip newlines/CR's
1725b4870bc5SRandy Dunlap##	if ($prototype =~ m/SYSCALL_DEFINE0\s*\(\s*(a-zA-Z0-9_)*\s*\)/) {
1726b4870bc5SRandy Dunlap	if ($prototype =~ m/SYSCALL_DEFINE0/) {
1727b4870bc5SRandy Dunlap		$void = 1;
1728b4870bc5SRandy Dunlap##		$prototype = "long sys_$1(void)";
1729b4870bc5SRandy Dunlap	}
1730b4870bc5SRandy Dunlap
1731b4870bc5SRandy Dunlap	$prototype =~ s/SYSCALL_DEFINE.*\(/long sys_/; # fix return type & func name
1732b4870bc5SRandy Dunlap	if ($prototype =~ m/long (sys_.*?),/) {
1733b4870bc5SRandy Dunlap		$prototype =~ s/,/\(/;
1734b4870bc5SRandy Dunlap	} elsif ($void) {
1735b4870bc5SRandy Dunlap		$prototype =~ s/\)/\(void\)/;
1736b4870bc5SRandy Dunlap	}
1737b4870bc5SRandy Dunlap
1738b4870bc5SRandy Dunlap	# now delete all of the odd-number commas in $prototype
1739b4870bc5SRandy Dunlap	# so that arg types & arg names don't have a comma between them
1740b4870bc5SRandy Dunlap	my $count = 0;
1741b4870bc5SRandy Dunlap	my $len = length($prototype);
1742b4870bc5SRandy Dunlap	if ($void) {
1743b4870bc5SRandy Dunlap		$len = 0;	# skip the for-loop
1744b4870bc5SRandy Dunlap	}
1745b4870bc5SRandy Dunlap	for (my $ix = 0; $ix < $len; $ix++) {
1746b4870bc5SRandy Dunlap		if (substr($prototype, $ix, 1) eq ',') {
1747b4870bc5SRandy Dunlap			$count++;
1748b4870bc5SRandy Dunlap			if ($count % 2 == 1) {
1749b4870bc5SRandy Dunlap				substr($prototype, $ix, 1) = ' ';
1750b4870bc5SRandy Dunlap			}
1751b4870bc5SRandy Dunlap		}
1752b4870bc5SRandy Dunlap	}
1753b4870bc5SRandy Dunlap}
1754b4870bc5SRandy Dunlap
1755b7afa92bSDaniel Vettersub process_proto_function($$) {
17561da177e4SLinus Torvalds    my $x = shift;
17571da177e4SLinus Torvalds    my $file = shift;
17581da177e4SLinus Torvalds
175951f5a0c8SRandy Dunlap    $x =~ s@\/\/.*$@@gos; # strip C99-style comments to end of line
176051f5a0c8SRandy Dunlap
1761890c78c2SRandy Dunlap    if ($x =~ m#\s*/\*\s+MACDOC\s*#io || ($x =~ /^#/ && $x !~ /^#\s*define/)) {
17621da177e4SLinus Torvalds	# do nothing
17631da177e4SLinus Torvalds    }
17641da177e4SLinus Torvalds    elsif ($x =~ /([^\{]*)/) {
17651da177e4SLinus Torvalds	$prototype .= $1;
17661da177e4SLinus Torvalds    }
1767b4870bc5SRandy Dunlap
1768890c78c2SRandy Dunlap    if (($x =~ /\{/) || ($x =~ /\#\s*define/) || ($x =~ /;/)) {
17691da177e4SLinus Torvalds	$prototype =~ s@/\*.*?\*/@@gos;	# strip comments.
17701da177e4SLinus Torvalds	$prototype =~ s@[\r\n]+@ @gos; # strip newlines/cr's.
17711da177e4SLinus Torvalds	$prototype =~ s@^\s+@@gos; # strip leading spaces
1772b4870bc5SRandy Dunlap	if ($prototype =~ /SYSCALL_DEFINE/) {
1773b4870bc5SRandy Dunlap		syscall_munge();
1774b4870bc5SRandy Dunlap	}
17753a9089fdSJason Baron	if ($prototype =~ /TRACE_EVENT/ || $prototype =~ /DEFINE_EVENT/ ||
17763a9089fdSJason Baron	    $prototype =~ /DEFINE_SINGLE_EVENT/)
17773a9089fdSJason Baron	{
177856afb0f8SJason Baron		tracepoint_munge($file);
177956afb0f8SJason Baron	}
17801da177e4SLinus Torvalds	dump_function($prototype, $file);
17811da177e4SLinus Torvalds	reset_state();
17821da177e4SLinus Torvalds    }
17831da177e4SLinus Torvalds}
17841da177e4SLinus Torvalds
1785b7afa92bSDaniel Vettersub process_proto_type($$) {
17861da177e4SLinus Torvalds    my $x = shift;
17871da177e4SLinus Torvalds    my $file = shift;
17881da177e4SLinus Torvalds
17891da177e4SLinus Torvalds    $x =~ s@[\r\n]+@ @gos; # strip newlines/cr's.
17901da177e4SLinus Torvalds    $x =~ s@^\s+@@gos; # strip leading spaces
17911da177e4SLinus Torvalds    $x =~ s@\s+$@@gos; # strip trailing spaces
179251f5a0c8SRandy Dunlap    $x =~ s@\/\/.*$@@gos; # strip C99-style comments to end of line
179351f5a0c8SRandy Dunlap
17941da177e4SLinus Torvalds    if ($x =~ /^#/) {
17951da177e4SLinus Torvalds	# To distinguish preprocessor directive from regular declaration later.
17961da177e4SLinus Torvalds	$x .= ";";
17971da177e4SLinus Torvalds    }
17981da177e4SLinus Torvalds
17991da177e4SLinus Torvalds    while (1) {
1800673bb2dfSBen Hutchings	if ( $x =~ /([^\{\};]*)([\{\};])(.*)/ ) {
1801463a0fdcSMarkus Heiser            if( length $prototype ) {
1802463a0fdcSMarkus Heiser                $prototype .= " "
1803463a0fdcSMarkus Heiser            }
18041da177e4SLinus Torvalds	    $prototype .= $1 . $2;
18051da177e4SLinus Torvalds	    ($2 eq '{') && $brcount++;
18061da177e4SLinus Torvalds	    ($2 eq '}') && $brcount--;
18071da177e4SLinus Torvalds	    if (($2 eq ';') && ($brcount == 0)) {
18081da177e4SLinus Torvalds		dump_declaration($prototype, $file);
18091da177e4SLinus Torvalds		reset_state();
18101da177e4SLinus Torvalds		last;
18111da177e4SLinus Torvalds	    }
18121da177e4SLinus Torvalds	    $x = $3;
18131da177e4SLinus Torvalds	} else {
18141da177e4SLinus Torvalds	    $prototype .= $x;
18151da177e4SLinus Torvalds	    last;
18161da177e4SLinus Torvalds	}
18171da177e4SLinus Torvalds    }
18181da177e4SLinus Torvalds}
18191da177e4SLinus Torvalds
18206b5b55f6SRandy Dunlap
18211ad560e4SJani Nikulasub map_filename($) {
18221ad560e4SJani Nikula    my $file;
18231ad560e4SJani Nikula    my ($orig_file) = @_;
18241ad560e4SJani Nikula
18251ad560e4SJani Nikula    if (defined($ENV{'SRCTREE'})) {
18261ad560e4SJani Nikula	$file = "$ENV{'SRCTREE'}" . "/" . $orig_file;
18271ad560e4SJani Nikula    } else {
18281ad560e4SJani Nikula	$file = $orig_file;
18291ad560e4SJani Nikula    }
18301ad560e4SJani Nikula
18311ad560e4SJani Nikula    if (defined($source_map{$file})) {
18321ad560e4SJani Nikula	$file = $source_map{$file};
18331ad560e4SJani Nikula    }
18341ad560e4SJani Nikula
18351ad560e4SJani Nikula    return $file;
18361ad560e4SJani Nikula}
18371ad560e4SJani Nikula
183888c2b57dSJani Nikulasub process_export_file($) {
183988c2b57dSJani Nikula    my ($orig_file) = @_;
184088c2b57dSJani Nikula    my $file = map_filename($orig_file);
184188c2b57dSJani Nikula
184288c2b57dSJani Nikula    if (!open(IN,"<$file")) {
184388c2b57dSJani Nikula	print STDERR "Error: Cannot open file $file\n";
184488c2b57dSJani Nikula	++$errors;
184588c2b57dSJani Nikula	return;
184688c2b57dSJani Nikula    }
184788c2b57dSJani Nikula
184888c2b57dSJani Nikula    while (<IN>) {
184988c2b57dSJani Nikula	if (/$export_symbol/) {
185088c2b57dSJani Nikula	    $function_table{$2} = 1;
185188c2b57dSJani Nikula	}
185288c2b57dSJani Nikula    }
185388c2b57dSJani Nikula
185488c2b57dSJani Nikula    close(IN);
185588c2b57dSJani Nikula}
185688c2b57dSJani Nikula
185707048d13SJonathan Corbet#
185807048d13SJonathan Corbet# Parsers for the various processing states.
185907048d13SJonathan Corbet#
186007048d13SJonathan Corbet# STATE_NORMAL: looking for the /** to begin everything.
186107048d13SJonathan Corbet#
186207048d13SJonathan Corbetsub process_normal() {
18631da177e4SLinus Torvalds    if (/$doc_start/o) {
186448af606aSJani Nikula	$state = STATE_NAME;	# next line is always the function name
1865850622dfSRandy Dunlap	$in_doc_sect = 0;
18660b0f5f29SDaniel Vetter	$declaration_start_line = $. + 1;
18671da177e4SLinus Torvalds    }
186807048d13SJonathan Corbet}
186907048d13SJonathan Corbet
18703cac2bc4SJonathan Corbet#
18713cac2bc4SJonathan Corbet# STATE_NAME: Looking for the "name - description" line
18723cac2bc4SJonathan Corbet#
18733cac2bc4SJonathan Corbetsub process_name($$) {
18743cac2bc4SJonathan Corbet    my $file = shift;
18751da177e4SLinus Torvalds    my $identifier;
18761da177e4SLinus Torvalds    my $descr;
18771da177e4SLinus Torvalds
18781da177e4SLinus Torvalds    if (/$doc_block/o) {
187948af606aSJani Nikula	$state = STATE_DOCBLOCK;
18801da177e4SLinus Torvalds	$contents = "";
18810b0f5f29SDaniel Vetter	$new_start_line = $. + 1;
18820b0f5f29SDaniel Vetter
18831da177e4SLinus Torvalds	if ( $1 eq "" ) {
18841da177e4SLinus Torvalds	    $section = $section_intro;
18851da177e4SLinus Torvalds	} else {
18861da177e4SLinus Torvalds	    $section = $1;
18871da177e4SLinus Torvalds	}
18881da177e4SLinus Torvalds    }
18891da177e4SLinus Torvalds    elsif (/$doc_decl/o) {
18901da177e4SLinus Torvalds	$identifier = $1;
1891a8dae20bSMike Rapoport	if (/\s*([\w\s]+?)(\(\))?\s*-/) {
18921da177e4SLinus Torvalds	    $identifier = $1;
18931da177e4SLinus Torvalds	}
18941da177e4SLinus Torvalds
189517b78717SJonathan Corbet	$state = STATE_BODY;
18960b0f5f29SDaniel Vetter	# if there's no @param blocks need to set up default section
18970b0f5f29SDaniel Vetter	# here
18982f4ad40aSJani Nikula	$contents = "";
18992f4ad40aSJani Nikula	$section = $section_default;
19000b0f5f29SDaniel Vetter	$new_start_line = $. + 1;
19011da177e4SLinus Torvalds	if (/-(.*)/) {
190251f5a0c8SRandy Dunlap	    # strip leading/trailing/multiple spaces
1903a21217daSRandy Dunlap	    $descr= $1;
1904a21217daSRandy Dunlap	    $descr =~ s/^\s*//;
1905a21217daSRandy Dunlap	    $descr =~ s/\s*$//;
190612ae6779SDaniel Santos	    $descr =~ s/\s+/ /g;
19070bba924cSJonathan Corbet	    $declaration_purpose = $descr;
190817b78717SJonathan Corbet	    $state = STATE_BODY_MAYBE;
19091da177e4SLinus Torvalds	} else {
19101da177e4SLinus Torvalds	    $declaration_purpose = "";
19111da177e4SLinus Torvalds	}
191277cc23b8SRandy Dunlap
191377cc23b8SRandy Dunlap	if (($declaration_purpose eq "") && $verbose) {
1914d40e1e65SBart Van Assche	    print STDERR "${file}:$.: warning: missing initial short description on line:\n";
191577cc23b8SRandy Dunlap	    print STDERR $_;
191677cc23b8SRandy Dunlap	    ++$warnings;
191777cc23b8SRandy Dunlap	}
191877cc23b8SRandy Dunlap
1919cf419d54SRandy Dunlap	if ($identifier =~ m/^struct\b/) {
19201da177e4SLinus Torvalds	    $decl_type = 'struct';
1921cf419d54SRandy Dunlap	} elsif ($identifier =~ m/^union\b/) {
19221da177e4SLinus Torvalds	    $decl_type = 'union';
1923cf419d54SRandy Dunlap	} elsif ($identifier =~ m/^enum\b/) {
19241da177e4SLinus Torvalds	    $decl_type = 'enum';
1925cf419d54SRandy Dunlap	} elsif ($identifier =~ m/^typedef\b/) {
19261da177e4SLinus Torvalds	    $decl_type = 'typedef';
19271da177e4SLinus Torvalds	} else {
19281da177e4SLinus Torvalds	    $decl_type = 'function';
19291da177e4SLinus Torvalds	}
19301da177e4SLinus Torvalds
19311da177e4SLinus Torvalds	if ($verbose) {
1932d40e1e65SBart Van Assche	    print STDERR "${file}:$.: info: Scanning doc for $identifier\n";
19331da177e4SLinus Torvalds	}
19341da177e4SLinus Torvalds    } else {
1935d40e1e65SBart Van Assche	print STDERR "${file}:$.: warning: Cannot understand $_ on line $.",
19361da177e4SLinus Torvalds	    " - I thought it was a doc line\n";
19371da177e4SLinus Torvalds	++$warnings;
193848af606aSJani Nikula	$state = STATE_NORMAL;
19391da177e4SLinus Torvalds    }
19403cac2bc4SJonathan Corbet}
19413cac2bc4SJonathan Corbet
19423cac2bc4SJonathan Corbet
1943d742f24dSJonathan Corbet#
1944d742f24dSJonathan Corbet# STATE_BODY and STATE_BODY_MAYBE: the bulk of a kerneldoc comment.
1945d742f24dSJonathan Corbet#
1946d742f24dSJonathan Corbetsub process_body($$) {
1947d742f24dSJonathan Corbet    my $file = shift;
19483cac2bc4SJonathan Corbet
194943756e34SJonathan Neuschäfer    # Until all named variable macro parameters are
195043756e34SJonathan Neuschäfer    # documented using the bare name (`x`) rather than with
195143756e34SJonathan Neuschäfer    # dots (`x...`), strip the dots:
195243756e34SJonathan Neuschäfer    if ($section =~ /\w\.\.\.$/) {
195343756e34SJonathan Neuschäfer	$section =~ s/\.\.\.$//;
195443756e34SJonathan Neuschäfer
195543756e34SJonathan Neuschäfer	if ($verbose) {
195643756e34SJonathan Neuschäfer	    print STDERR "${file}:$.: warning: Variable macro arguments should be documented without dots\n";
195743756e34SJonathan Neuschäfer	    ++$warnings;
195843756e34SJonathan Neuschäfer	}
195943756e34SJonathan Neuschäfer    }
196043756e34SJonathan Neuschäfer
1961*0d55d48bSMauro Carvalho Chehab    if ($state == STATE_BODY_WITH_BLANK_LINE && /^\s*\*\s?\S/) {
1962*0d55d48bSMauro Carvalho Chehab	dump_section($file, $section, $contents);
1963*0d55d48bSMauro Carvalho Chehab	$section = $section_default;
1964*0d55d48bSMauro Carvalho Chehab	$contents = "";
1965*0d55d48bSMauro Carvalho Chehab    }
1966*0d55d48bSMauro Carvalho Chehab
1967f624adefSJani Nikula    if (/$doc_sect/i) { # case insensitive for supported section names
19681da177e4SLinus Torvalds	$newsection = $1;
19691da177e4SLinus Torvalds	$newcontents = $2;
19701da177e4SLinus Torvalds
1971f624adefSJani Nikula	# map the supported section names to the canonical names
1972f624adefSJani Nikula	if ($newsection =~ m/^description$/i) {
1973f624adefSJani Nikula	    $newsection = $section_default;
1974f624adefSJani Nikula	} elsif ($newsection =~ m/^context$/i) {
1975f624adefSJani Nikula	    $newsection = $section_context;
1976f624adefSJani Nikula	} elsif ($newsection =~ m/^returns?$/i) {
1977f624adefSJani Nikula	    $newsection = $section_return;
1978f624adefSJani Nikula	} elsif ($newsection =~ m/^\@return$/) {
1979f624adefSJani Nikula	    # special: @return is a section, not a param description
1980f624adefSJani Nikula	    $newsection = $section_return;
1981f624adefSJani Nikula	}
1982f624adefSJani Nikula
1983792aa2f2SRandy Dunlap	if (($contents ne "") && ($contents ne "\n")) {
1984850622dfSRandy Dunlap	    if (!$in_doc_sect && $verbose) {
1985d40e1e65SBart Van Assche		print STDERR "${file}:$.: warning: contents before sections\n";
1986850622dfSRandy Dunlap		++$warnings;
1987850622dfSRandy Dunlap	    }
19880bba924cSJonathan Corbet	    dump_section($file, $section, $contents);
19891da177e4SLinus Torvalds	    $section = $section_default;
19901da177e4SLinus Torvalds	}
19911da177e4SLinus Torvalds
1992850622dfSRandy Dunlap	$in_doc_sect = 1;
199317b78717SJonathan Corbet	$state = STATE_BODY;
19941da177e4SLinus Torvalds	$contents = $newcontents;
19950b0f5f29SDaniel Vetter	$new_start_line = $.;
19967c9aa015SMauro Carvalho Chehab	while (substr($contents, 0, 1) eq " ") {
199705189497SRandy Dunlap	    $contents = substr($contents, 1);
199805189497SRandy Dunlap	}
19990a726301SJani Nikula	if ($contents ne "") {
20001da177e4SLinus Torvalds	    $contents .= "\n";
20011da177e4SLinus Torvalds	}
20021da177e4SLinus Torvalds	$section = $newsection;
2003b7886de4SJani Nikula	$leading_space = undef;
20041da177e4SLinus Torvalds    } elsif (/$doc_end/) {
20054c98ecafSRandy Dunlap	if (($contents ne "") && ($contents ne "\n")) {
20060bba924cSJonathan Corbet	    dump_section($file, $section, $contents);
20071da177e4SLinus Torvalds	    $section = $section_default;
20081da177e4SLinus Torvalds	    $contents = "";
20091da177e4SLinus Torvalds	}
201046b958ebSRandy Dunlap	# look for doc_com + <text> + doc_end:
201146b958ebSRandy Dunlap	if ($_ =~ m'\s*\*\s*[a-zA-Z_0-9:\.]+\*/') {
2012d40e1e65SBart Van Assche	    print STDERR "${file}:$.: warning: suspicious ending line: $_";
201346b958ebSRandy Dunlap	    ++$warnings;
201446b958ebSRandy Dunlap	}
20151da177e4SLinus Torvalds
20161da177e4SLinus Torvalds	$prototype = "";
201748af606aSJani Nikula	$state = STATE_PROTO;
20181da177e4SLinus Torvalds	$brcount = 0;
20191da177e4SLinus Torvalds    } elsif (/$doc_content/) {
20206423133bSJohannes Weiner	if ($1 eq "") {
2021*0d55d48bSMauro Carvalho Chehab	    if ($section eq $section_context) {
20220bba924cSJonathan Corbet		dump_section($file, $section, $contents);
20231da177e4SLinus Torvalds		$section = $section_default;
20241da177e4SLinus Torvalds		$contents = "";
20250b0f5f29SDaniel Vetter		$new_start_line = $.;
2026*0d55d48bSMauro Carvalho Chehab		$state = STATE_BODY;
20271da177e4SLinus Torvalds	    } else {
2028*0d55d48bSMauro Carvalho Chehab		if ($section ne $section_default) {
2029*0d55d48bSMauro Carvalho Chehab		    $state = STATE_BODY_WITH_BLANK_LINE;
2030*0d55d48bSMauro Carvalho Chehab		} else {
2031*0d55d48bSMauro Carvalho Chehab		    $state = STATE_BODY;
2032*0d55d48bSMauro Carvalho Chehab		}
20336423133bSJohannes Weiner		$contents .= "\n";
20346423133bSJohannes Weiner	    }
203517b78717SJonathan Corbet	} elsif ($state == STATE_BODY_MAYBE) {
20366423133bSJohannes Weiner	    # Continued declaration purpose
20376423133bSJohannes Weiner	    chomp($declaration_purpose);
20380bba924cSJonathan Corbet	    $declaration_purpose .= " " . $1;
203912ae6779SDaniel Santos	    $declaration_purpose =~ s/\s+/ /g;
20406423133bSJohannes Weiner	} else {
2041b7886de4SJani Nikula	    my $cont = $1;
2042b7886de4SJani Nikula	    if ($section =~ m/^@/ || $section eq $section_context) {
2043b7886de4SJani Nikula		if (!defined $leading_space) {
2044b7886de4SJani Nikula		    if ($cont =~ m/^(\s+)/) {
2045b7886de4SJani Nikula			$leading_space = $1;
2046b7886de4SJani Nikula		    } else {
2047b7886de4SJani Nikula			$leading_space = "";
2048b7886de4SJani Nikula		    }
2049b7886de4SJani Nikula		}
2050b7886de4SJani Nikula		$cont =~ s/^$leading_space//;
2051b7886de4SJani Nikula	    }
2052b7886de4SJani Nikula	    $contents .= $cont . "\n";
20531da177e4SLinus Torvalds	}
20541da177e4SLinus Torvalds    } else {
20551da177e4SLinus Torvalds	# i dont know - bad line?  ignore.
2056d40e1e65SBart Van Assche	print STDERR "${file}:$.: warning: bad line: $_";
20571da177e4SLinus Torvalds	++$warnings;
20581da177e4SLinus Torvalds    }
2059d742f24dSJonathan Corbet}
2060d742f24dSJonathan Corbet
2061d742f24dSJonathan Corbet
2062cc794812SJonathan Corbet#
2063cc794812SJonathan Corbet# STATE_PROTO: reading a function/whatever prototype.
2064cc794812SJonathan Corbet#
2065cc794812SJonathan Corbetsub process_proto($$) {
2066cc794812SJonathan Corbet    my $file = shift;
2067cc794812SJonathan Corbet
2068cc794812SJonathan Corbet    if (/$doc_inline_oneline/) {
2069cc794812SJonathan Corbet	$section = $1;
2070cc794812SJonathan Corbet	$contents = $2;
2071cc794812SJonathan Corbet	if ($contents ne "") {
2072cc794812SJonathan Corbet	    $contents .= "\n";
2073cc794812SJonathan Corbet	    dump_section($file, $section, $contents);
2074cc794812SJonathan Corbet	    $section = $section_default;
2075cc794812SJonathan Corbet	    $contents = "";
2076cc794812SJonathan Corbet	}
2077cc794812SJonathan Corbet    } elsif (/$doc_inline_start/) {
2078cc794812SJonathan Corbet	$state = STATE_INLINE;
2079cc794812SJonathan Corbet	$inline_doc_state = STATE_INLINE_NAME;
2080cc794812SJonathan Corbet    } elsif ($decl_type eq 'function') {
2081cc794812SJonathan Corbet	process_proto_function($_, $file);
2082cc794812SJonathan Corbet    } else {
2083cc794812SJonathan Corbet	process_proto_type($_, $file);
2084cc794812SJonathan Corbet    }
2085cc794812SJonathan Corbet}
2086cc794812SJonathan Corbet
2087c17add56SJonathan Corbet#
2088c17add56SJonathan Corbet# STATE_DOCBLOCK: within a DOC: block.
2089c17add56SJonathan Corbet#
2090c17add56SJonathan Corbetsub process_docblock($$) {
2091c17add56SJonathan Corbet    my $file = shift;
2092cc794812SJonathan Corbet
2093c17add56SJonathan Corbet    if (/$doc_end/) {
2094c17add56SJonathan Corbet	dump_doc_section($file, $section, $contents);
2095c17add56SJonathan Corbet	$section = $section_default;
2096c17add56SJonathan Corbet	$contents = "";
2097c17add56SJonathan Corbet	$function = "";
2098c17add56SJonathan Corbet	%parameterdescs = ();
2099c17add56SJonathan Corbet	%parametertypes = ();
2100c17add56SJonathan Corbet	@parameterlist = ();
2101c17add56SJonathan Corbet	%sections = ();
2102c17add56SJonathan Corbet	@sectionlist = ();
2103c17add56SJonathan Corbet	$prototype = "";
2104c17add56SJonathan Corbet	$state = STATE_NORMAL;
2105c17add56SJonathan Corbet    } elsif (/$doc_content/) {
2106c17add56SJonathan Corbet	if ( $1 eq "" )	{
2107c17add56SJonathan Corbet	    $contents .= $blankline;
2108c17add56SJonathan Corbet	} else {
2109c17add56SJonathan Corbet	    $contents .= $1 . "\n";
2110c17add56SJonathan Corbet	}
2111c17add56SJonathan Corbet    }
2112d742f24dSJonathan Corbet}
2113d742f24dSJonathan Corbet
2114c17add56SJonathan Corbet#
2115c17add56SJonathan Corbet# STATE_INLINE: docbook comments within a prototype.
2116c17add56SJonathan Corbet#
2117c17add56SJonathan Corbetsub process_inline($$) {
2118c17add56SJonathan Corbet    my $file = shift;
2119d742f24dSJonathan Corbet
2120a4c6ebedSDanilo Cesar Lemes de Paula    # First line (state 1) needs to be a @parameter
212148af606aSJani Nikula    if ($inline_doc_state == STATE_INLINE_NAME && /$doc_inline_sect/o) {
2122a4c6ebedSDanilo Cesar Lemes de Paula	$section = $1;
2123a4c6ebedSDanilo Cesar Lemes de Paula	$contents = $2;
21240b0f5f29SDaniel Vetter	$new_start_line = $.;
2125a4c6ebedSDanilo Cesar Lemes de Paula	if ($contents ne "") {
21267c9aa015SMauro Carvalho Chehab	    while (substr($contents, 0, 1) eq " ") {
2127a4c6ebedSDanilo Cesar Lemes de Paula		$contents = substr($contents, 1);
2128a4c6ebedSDanilo Cesar Lemes de Paula	    }
2129a4c6ebedSDanilo Cesar Lemes de Paula	    $contents .= "\n";
2130a4c6ebedSDanilo Cesar Lemes de Paula	}
213148af606aSJani Nikula	$inline_doc_state = STATE_INLINE_TEXT;
2132a4c6ebedSDanilo Cesar Lemes de Paula	# Documentation block end */
213348af606aSJani Nikula    } elsif (/$doc_inline_end/) {
2134a4c6ebedSDanilo Cesar Lemes de Paula	if (($contents ne "") && ($contents ne "\n")) {
21350bba924cSJonathan Corbet	    dump_section($file, $section, $contents);
2136a4c6ebedSDanilo Cesar Lemes de Paula	    $section = $section_default;
2137a4c6ebedSDanilo Cesar Lemes de Paula	    $contents = "";
2138a4c6ebedSDanilo Cesar Lemes de Paula	}
213948af606aSJani Nikula	$state = STATE_PROTO;
214048af606aSJani Nikula	$inline_doc_state = STATE_INLINE_NA;
2141a4c6ebedSDanilo Cesar Lemes de Paula	# Regular text
2142a4c6ebedSDanilo Cesar Lemes de Paula    } elsif (/$doc_content/) {
214348af606aSJani Nikula	if ($inline_doc_state == STATE_INLINE_TEXT) {
2144a4c6ebedSDanilo Cesar Lemes de Paula	    $contents .= $1 . "\n";
21456450c895SJani Nikula	    # nuke leading blank lines
21466450c895SJani Nikula	    if ($contents =~ /^\s*$/) {
21476450c895SJani Nikula		$contents = "";
21486450c895SJani Nikula	    }
214948af606aSJani Nikula	} elsif ($inline_doc_state == STATE_INLINE_NAME) {
215048af606aSJani Nikula	    $inline_doc_state = STATE_INLINE_ERROR;
2151e7ca311eSDaniel Vetter	    print STDERR "${file}:$.: warning: ";
2152a4c6ebedSDanilo Cesar Lemes de Paula	    print STDERR "Incorrect use of kernel-doc format: $_";
2153a4c6ebedSDanilo Cesar Lemes de Paula	    ++$warnings;
2154a4c6ebedSDanilo Cesar Lemes de Paula	}
2155a4c6ebedSDanilo Cesar Lemes de Paula    }
21560c9aa209SJani Nikula}
2157c17add56SJonathan Corbet
2158c17add56SJonathan Corbet
2159c17add56SJonathan Corbetsub process_file($) {
2160c17add56SJonathan Corbet    my $file;
2161c17add56SJonathan Corbet    my $initial_section_counter = $section_counter;
2162c17add56SJonathan Corbet    my ($orig_file) = @_;
2163c17add56SJonathan Corbet
2164c17add56SJonathan Corbet    $file = map_filename($orig_file);
2165c17add56SJonathan Corbet
2166c17add56SJonathan Corbet    if (!open(IN,"<$file")) {
2167c17add56SJonathan Corbet	print STDERR "Error: Cannot open file $file\n";
2168c17add56SJonathan Corbet	++$errors;
2169c17add56SJonathan Corbet	return;
21701da177e4SLinus Torvalds    }
2171c17add56SJonathan Corbet
2172c17add56SJonathan Corbet    $. = 1;
2173c17add56SJonathan Corbet
2174c17add56SJonathan Corbet    $section_counter = 0;
2175c17add56SJonathan Corbet    while (<IN>) {
2176c17add56SJonathan Corbet	while (s/\\\s*$//) {
2177c17add56SJonathan Corbet	    $_ .= <IN>;
2178c17add56SJonathan Corbet	}
2179c17add56SJonathan Corbet	# Replace tabs by spaces
2180c17add56SJonathan Corbet        while ($_ =~ s/\t+/' ' x (length($&) * 8 - length($`) % 8)/e) {};
2181c17add56SJonathan Corbet	# Hand this line to the appropriate state handler
2182c17add56SJonathan Corbet	if ($state == STATE_NORMAL) {
2183c17add56SJonathan Corbet	    process_normal();
2184c17add56SJonathan Corbet	} elsif ($state == STATE_NAME) {
2185c17add56SJonathan Corbet	    process_name($file, $_);
2186*0d55d48bSMauro Carvalho Chehab	} elsif ($state == STATE_BODY || $state == STATE_BODY_MAYBE ||
2187*0d55d48bSMauro Carvalho Chehab		 $state == STATE_BODY_WITH_BLANK_LINE) {
2188c17add56SJonathan Corbet	    process_body($file, $_);
2189c17add56SJonathan Corbet	} elsif ($state == STATE_INLINE) { # scanning for inline parameters
2190c17add56SJonathan Corbet	    process_inline($file, $_);
2191cc794812SJonathan Corbet	} elsif ($state == STATE_PROTO) {
2192cc794812SJonathan Corbet	    process_proto($file, $_);
219348af606aSJani Nikula	} elsif ($state == STATE_DOCBLOCK) {
2194c17add56SJonathan Corbet	    process_docblock($file, $_);
21951da177e4SLinus Torvalds	}
21961da177e4SLinus Torvalds    }
2197c17add56SJonathan Corbet
2198c17add56SJonathan Corbet    # Make sure we got something interesting.
2199b0d60bfbSJonathan Corbet    if ($initial_section_counter == $section_counter && $
2200b0d60bfbSJonathan Corbet	output_mode ne "none") {
2201b0d60bfbSJonathan Corbet	if ($output_selection == OUTPUT_INCLUDE) {
2202b0d60bfbSJonathan Corbet	    print STDERR "${file}:1: warning: '$_' not found\n"
2203b0d60bfbSJonathan Corbet		for keys %function_table;
22043a025e1dSMatthew Wilcox	}
2205b0d60bfbSJonathan Corbet	else {
2206b0d60bfbSJonathan Corbet	    print STDERR "${file}:1: warning: no structured comments found\n";
2207e946c43aSJohannes Berg	}
22081da177e4SLinus Torvalds    }
22091da177e4SLinus Torvalds}
22108484baaaSRandy Dunlap
22118484baaaSRandy Dunlap
22128484baaaSRandy Dunlap$kernelversion = get_kernel_version();
22138484baaaSRandy Dunlap
22148484baaaSRandy Dunlap# generate a sequence of code that will splice in highlighting information
22158484baaaSRandy Dunlap# using the s// operator.
22161ef06233SMauro Carvalho Chehabfor (my $k = 0; $k < @highlights; $k++) {
22174d732701SDanilo Cesar Lemes de Paula    my $pattern = $highlights[$k][0];
22184d732701SDanilo Cesar Lemes de Paula    my $result = $highlights[$k][1];
22194d732701SDanilo Cesar Lemes de Paula#   print STDERR "scanning pattern:$pattern, highlight:($result)\n";
22204d732701SDanilo Cesar Lemes de Paula    $dohighlight .=  "\$contents =~ s:$pattern:$result:gs;\n";
22218484baaaSRandy Dunlap}
22228484baaaSRandy Dunlap
22238484baaaSRandy Dunlap# Read the file that maps relative names to absolute names for
22248484baaaSRandy Dunlap# separate source and object directories and for shadow trees.
22258484baaaSRandy Dunlapif (open(SOURCE_MAP, "<.tmp_filelist.txt")) {
22268484baaaSRandy Dunlap	my ($relname, $absname);
22278484baaaSRandy Dunlap	while(<SOURCE_MAP>) {
22288484baaaSRandy Dunlap		chop();
22298484baaaSRandy Dunlap		($relname, $absname) = (split())[0..1];
22308484baaaSRandy Dunlap		$relname =~ s:^/+::;
22318484baaaSRandy Dunlap		$source_map{$relname} = $absname;
22328484baaaSRandy Dunlap	}
22338484baaaSRandy Dunlap	close(SOURCE_MAP);
22348484baaaSRandy Dunlap}
22358484baaaSRandy Dunlap
223688c2b57dSJani Nikulaif ($output_selection == OUTPUT_EXPORTED ||
223788c2b57dSJani Nikula    $output_selection == OUTPUT_INTERNAL) {
2238c9b2cfb3SJani Nikula
2239c9b2cfb3SJani Nikula    push(@export_file_list, @ARGV);
2240c9b2cfb3SJani Nikula
224188c2b57dSJani Nikula    foreach (@export_file_list) {
224288c2b57dSJani Nikula	chomp;
224388c2b57dSJani Nikula	process_export_file($_);
224488c2b57dSJani Nikula    }
224588c2b57dSJani Nikula}
224688c2b57dSJani Nikula
22478484baaaSRandy Dunlapforeach (@ARGV) {
22488484baaaSRandy Dunlap    chomp;
22498484baaaSRandy Dunlap    process_file($_);
22508484baaaSRandy Dunlap}
22518484baaaSRandy Dunlapif ($verbose && $errors) {
22528484baaaSRandy Dunlap  print STDERR "$errors errors\n";
22538484baaaSRandy Dunlap}
22548484baaaSRandy Dunlapif ($verbose && $warnings) {
22558484baaaSRandy Dunlap  print STDERR "$warnings warnings\n";
22568484baaaSRandy Dunlap}
22578484baaaSRandy Dunlap
2258e814bccbSWill Deaconexit($output_mode eq "none" ? 0 : $errors);
2259