xref: /openbmc/qemu/scripts/kernel-doc (revision 486966e4a4e2cc0647e01f9902b8c1752c44d38e)
1e2c54635SPaolo Bonzini#!/usr/bin/env perl
2e2c54635SPaolo Bonzini# SPDX-License-Identifier: GPL-2.0
3e2c54635SPaolo Bonzini
4e2c54635SPaolo Bonziniuse warnings;
5e2c54635SPaolo Bonziniuse strict;
6e2c54635SPaolo Bonzini
7e2c54635SPaolo Bonzini## Copyright (c) 1998 Michael Zucchi, All Rights Reserved        ##
8e2c54635SPaolo Bonzini## Copyright (C) 2000, 1  Tim Waugh <twaugh@redhat.com>          ##
9e2c54635SPaolo Bonzini## Copyright (C) 2001  Simon Huggins                             ##
10e2c54635SPaolo Bonzini## Copyright (C) 2005-2012  Randy Dunlap                         ##
11e2c54635SPaolo Bonzini## Copyright (C) 2012  Dan Luedtke                               ##
12e2c54635SPaolo Bonzini## 								 ##
13e2c54635SPaolo Bonzini## #define enhancements by Armin Kuster <akuster@mvista.com>	 ##
14e2c54635SPaolo Bonzini## Copyright (c) 2000 MontaVista Software, Inc.			 ##
15e2c54635SPaolo Bonzini## 								 ##
16e2c54635SPaolo Bonzini## This software falls under the GNU General Public License.     ##
17e2c54635SPaolo Bonzini## Please read the COPYING file for more information             ##
18e2c54635SPaolo Bonzini
19e2c54635SPaolo Bonzini# 18/01/2001 - 	Cleanups
20e2c54635SPaolo Bonzini# 		Functions prototyped as foo(void) same as foo()
21e2c54635SPaolo Bonzini# 		Stop eval'ing where we don't need to.
22e2c54635SPaolo Bonzini# -- huggie@earth.li
23e2c54635SPaolo Bonzini
24e2c54635SPaolo Bonzini# 27/06/2001 -  Allowed whitespace after initial "/**" and
25e2c54635SPaolo Bonzini#               allowed comments before function declarations.
26e2c54635SPaolo Bonzini# -- Christian Kreibich <ck@whoop.org>
27e2c54635SPaolo Bonzini
28e2c54635SPaolo Bonzini# Still to do:
29e2c54635SPaolo Bonzini# 	- add perldoc documentation
30e2c54635SPaolo Bonzini# 	- Look more closely at some of the scarier bits :)
31e2c54635SPaolo Bonzini
32e2c54635SPaolo Bonzini# 26/05/2001 - 	Support for separate source and object trees.
33e2c54635SPaolo Bonzini#		Return error code.
34e2c54635SPaolo Bonzini# 		Keith Owens <kaos@ocs.com.au>
35e2c54635SPaolo Bonzini
36e2c54635SPaolo Bonzini# 23/09/2001 - Added support for typedefs, structs, enums and unions
37e2c54635SPaolo Bonzini#              Support for Context section; can be terminated using empty line
38e2c54635SPaolo Bonzini#              Small fixes (like spaces vs. \s in regex)
39e2c54635SPaolo Bonzini# -- Tim Jansen <tim@tjansen.de>
40e2c54635SPaolo Bonzini
41e2c54635SPaolo Bonzini# 25/07/2012 - Added support for HTML5
42e2c54635SPaolo Bonzini# -- Dan Luedtke <mail@danrl.de>
43e2c54635SPaolo Bonzini
44e2c54635SPaolo Bonzinisub usage {
45e2c54635SPaolo Bonzini    my $message = <<"EOF";
46e2c54635SPaolo BonziniUsage: $0 [OPTION ...] FILE ...
47e2c54635SPaolo Bonzini
48e2c54635SPaolo BonziniRead C language source or header FILEs, extract embedded documentation comments,
49e2c54635SPaolo Bonziniand print formatted documentation to standard output.
50e2c54635SPaolo Bonzini
51e2c54635SPaolo BonziniThe documentation comments are identified by "/**" opening comment mark. See
52e2c54635SPaolo BonziniDocumentation/doc-guide/kernel-doc.rst for the documentation comment syntax.
53e2c54635SPaolo Bonzini
54e2c54635SPaolo BonziniOutput format selection (mutually exclusive):
55e2c54635SPaolo Bonzini  -man			Output troff manual page format. This is the default.
56e2c54635SPaolo Bonzini  -rst			Output reStructuredText format.
57e2c54635SPaolo Bonzini  -none			Do not output documentation, only warnings.
58e2c54635SPaolo Bonzini
59*486966e4SMauro Carvalho ChehabOutput format selection modifier (affects only ReST output):
60*486966e4SMauro Carvalho Chehab
61*486966e4SMauro Carvalho Chehab  -sphinx-version	Use the ReST C domain dialect compatible with an
62*486966e4SMauro Carvalho Chehab			specific Sphinx Version.
63*486966e4SMauro Carvalho Chehab			If not specified, kernel-doc will auto-detect using
64*486966e4SMauro Carvalho Chehab			the sphinx-build version found on PATH.
65*486966e4SMauro Carvalho Chehab
66e2c54635SPaolo BonziniOutput selection (mutually exclusive):
67e2c54635SPaolo Bonzini  -export		Only output documentation for symbols that have been
68e2c54635SPaolo Bonzini			exported using EXPORT_SYMBOL() or EXPORT_SYMBOL_GPL()
69e2c54635SPaolo Bonzini                        in any input FILE or -export-file FILE.
70e2c54635SPaolo Bonzini  -internal		Only output documentation for symbols that have NOT been
71e2c54635SPaolo Bonzini			exported using EXPORT_SYMBOL() or EXPORT_SYMBOL_GPL()
72e2c54635SPaolo Bonzini                        in any input FILE or -export-file FILE.
73e2c54635SPaolo Bonzini  -function NAME	Only output documentation for the given function(s)
74e2c54635SPaolo Bonzini			or DOC: section title(s). All other functions and DOC:
75e2c54635SPaolo Bonzini			sections are ignored. May be specified multiple times.
7678c8c92cSMauro Carvalho Chehab  -nosymbol NAME	Exclude the specified symbols from the output
7778c8c92cSMauro Carvalho Chehab		        documentation. May be specified multiple times.
78e2c54635SPaolo Bonzini
79e2c54635SPaolo BonziniOutput selection modifiers:
80e2c54635SPaolo Bonzini  -no-doc-sections	Do not output DOC: sections.
81e2c54635SPaolo Bonzini  -enable-lineno        Enable output of #define LINENO lines. Only works with
82e2c54635SPaolo Bonzini                        reStructuredText format.
83e2c54635SPaolo Bonzini  -export-file FILE     Specify an additional FILE in which to look for
84e2c54635SPaolo Bonzini                        EXPORT_SYMBOL() and EXPORT_SYMBOL_GPL(). To be used with
85e2c54635SPaolo Bonzini                        -export or -internal. May be specified multiple times.
86e2c54635SPaolo Bonzini
87e2c54635SPaolo BonziniOther parameters:
88e2c54635SPaolo Bonzini  -v			Verbose output, more warnings and other information.
89e2c54635SPaolo Bonzini  -h			Print this help.
9086cba217SPierre-Louis Bossart  -Werror		Treat warnings as errors.
91e2c54635SPaolo Bonzini
92e2c54635SPaolo BonziniEOF
93e2c54635SPaolo Bonzini    print $message;
94e2c54635SPaolo Bonzini    exit 1;
95e2c54635SPaolo Bonzini}
96e2c54635SPaolo Bonzini
97e2c54635SPaolo Bonzini#
98e2c54635SPaolo Bonzini# format of comments.
99e2c54635SPaolo Bonzini# In the following table, (...)? signifies optional structure.
100e2c54635SPaolo Bonzini#                         (...)* signifies 0 or more structure elements
101e2c54635SPaolo Bonzini# /**
102e2c54635SPaolo Bonzini#  * function_name(:)? (- short description)?
103e2c54635SPaolo Bonzini# (* @parameterx: (description of parameter x)?)*
104e2c54635SPaolo Bonzini# (* a blank line)?
105e2c54635SPaolo Bonzini#  * (Description:)? (Description of function)?
106e2c54635SPaolo Bonzini#  * (section header: (section description)? )*
107e2c54635SPaolo Bonzini#  (*)?*/
108e2c54635SPaolo Bonzini#
109e2c54635SPaolo Bonzini# So .. the trivial example would be:
110e2c54635SPaolo Bonzini#
111e2c54635SPaolo Bonzini# /**
112e2c54635SPaolo Bonzini#  * my_function
113e2c54635SPaolo Bonzini#  */
114e2c54635SPaolo Bonzini#
115e2c54635SPaolo Bonzini# If the Description: header tag is omitted, then there must be a blank line
116e2c54635SPaolo Bonzini# after the last parameter specification.
117e2c54635SPaolo Bonzini# e.g.
118e2c54635SPaolo Bonzini# /**
119e2c54635SPaolo Bonzini#  * my_function - does my stuff
120e2c54635SPaolo Bonzini#  * @my_arg: its mine damnit
121e2c54635SPaolo Bonzini#  *
122e2c54635SPaolo Bonzini#  * Does my stuff explained.
123e2c54635SPaolo Bonzini#  */
124e2c54635SPaolo Bonzini#
125e2c54635SPaolo Bonzini#  or, could also use:
126e2c54635SPaolo Bonzini# /**
127e2c54635SPaolo Bonzini#  * my_function - does my stuff
128e2c54635SPaolo Bonzini#  * @my_arg: its mine damnit
129e2c54635SPaolo Bonzini#  * Description: Does my stuff explained.
130e2c54635SPaolo Bonzini#  */
131e2c54635SPaolo Bonzini# etc.
132e2c54635SPaolo Bonzini#
133e2c54635SPaolo Bonzini# Besides functions you can also write documentation for structs, unions,
134e2c54635SPaolo Bonzini# enums and typedefs. Instead of the function name you must write the name
135e2c54635SPaolo Bonzini# of the declaration;  the struct/union/enum/typedef must always precede
136e2c54635SPaolo Bonzini# the name. Nesting of declarations is not supported.
137e2c54635SPaolo Bonzini# Use the argument mechanism to document members or constants.
138e2c54635SPaolo Bonzini# e.g.
139e2c54635SPaolo Bonzini# /**
140e2c54635SPaolo Bonzini#  * struct my_struct - short description
141e2c54635SPaolo Bonzini#  * @a: first member
142e2c54635SPaolo Bonzini#  * @b: second member
143e2c54635SPaolo Bonzini#  *
144e2c54635SPaolo Bonzini#  * Longer description
145e2c54635SPaolo Bonzini#  */
146e2c54635SPaolo Bonzini# struct my_struct {
147e2c54635SPaolo Bonzini#     int a;
148e2c54635SPaolo Bonzini#     int b;
149e2c54635SPaolo Bonzini# /* private: */
150e2c54635SPaolo Bonzini#     int c;
151e2c54635SPaolo Bonzini# };
152e2c54635SPaolo Bonzini#
153e2c54635SPaolo Bonzini# All descriptions can be multiline, except the short function description.
154e2c54635SPaolo Bonzini#
155e2c54635SPaolo Bonzini# For really longs structs, you can also describe arguments inside the
156e2c54635SPaolo Bonzini# body of the struct.
157e2c54635SPaolo Bonzini# eg.
158e2c54635SPaolo Bonzini# /**
159e2c54635SPaolo Bonzini#  * struct my_struct - short description
160e2c54635SPaolo Bonzini#  * @a: first member
161e2c54635SPaolo Bonzini#  * @b: second member
162e2c54635SPaolo Bonzini#  *
163e2c54635SPaolo Bonzini#  * Longer description
164e2c54635SPaolo Bonzini#  */
165e2c54635SPaolo Bonzini# struct my_struct {
166e2c54635SPaolo Bonzini#     int a;
167e2c54635SPaolo Bonzini#     int b;
168e2c54635SPaolo Bonzini#     /**
169e2c54635SPaolo Bonzini#      * @c: This is longer description of C
170e2c54635SPaolo Bonzini#      *
171e2c54635SPaolo Bonzini#      * You can use paragraphs to describe arguments
172e2c54635SPaolo Bonzini#      * using this method.
173e2c54635SPaolo Bonzini#      */
174e2c54635SPaolo Bonzini#     int c;
175e2c54635SPaolo Bonzini# };
176e2c54635SPaolo Bonzini#
177e2c54635SPaolo Bonzini# This should be use only for struct/enum members.
178e2c54635SPaolo Bonzini#
179e2c54635SPaolo Bonzini# You can also add additional sections. When documenting kernel functions you
180e2c54635SPaolo Bonzini# should document the "Context:" of the function, e.g. whether the functions
181e2c54635SPaolo Bonzini# can be called form interrupts. Unlike other sections you can end it with an
182e2c54635SPaolo Bonzini# empty line.
183e2c54635SPaolo Bonzini# A non-void function should have a "Return:" section describing the return
184e2c54635SPaolo Bonzini# value(s).
185e2c54635SPaolo Bonzini# Example-sections should contain the string EXAMPLE so that they are marked
186e2c54635SPaolo Bonzini# appropriately in DocBook.
187e2c54635SPaolo Bonzini#
188e2c54635SPaolo Bonzini# Example:
189e2c54635SPaolo Bonzini# /**
190e2c54635SPaolo Bonzini#  * user_function - function that can only be called in user context
191e2c54635SPaolo Bonzini#  * @a: some argument
192e2c54635SPaolo Bonzini#  * Context: !in_interrupt()
193e2c54635SPaolo Bonzini#  *
194e2c54635SPaolo Bonzini#  * Some description
195e2c54635SPaolo Bonzini#  * Example:
196e2c54635SPaolo Bonzini#  *    user_function(22);
197e2c54635SPaolo Bonzini#  */
198e2c54635SPaolo Bonzini# ...
199e2c54635SPaolo Bonzini#
200e2c54635SPaolo Bonzini#
201e2c54635SPaolo Bonzini# All descriptive text is further processed, scanning for the following special
202e2c54635SPaolo Bonzini# patterns, which are highlighted appropriately.
203e2c54635SPaolo Bonzini#
204e2c54635SPaolo Bonzini# 'funcname()' - function
205e2c54635SPaolo Bonzini# '$ENVVAR' - environmental variable
206e2c54635SPaolo Bonzini# '&struct_name' - name of a structure (up to two words including 'struct')
207e2c54635SPaolo Bonzini# '&struct_name.member' - name of a structure member
208e2c54635SPaolo Bonzini# '@parameter' - name of a parameter
209e2c54635SPaolo Bonzini# '%CONST' - name of a constant.
210e2c54635SPaolo Bonzini# '``LITERAL``' - literal string without any spaces on it.
211e2c54635SPaolo Bonzini
212e2c54635SPaolo Bonzini## init lots of data
213e2c54635SPaolo Bonzini
214e2c54635SPaolo Bonzinimy $errors = 0;
215e2c54635SPaolo Bonzinimy $warnings = 0;
216e2c54635SPaolo Bonzinimy $anon_struct_union = 0;
217e2c54635SPaolo Bonzini
218e2c54635SPaolo Bonzini# match expressions used to find embedded type information
219e2c54635SPaolo Bonzinimy $type_constant = '\b``([^\`]+)``\b';
220e2c54635SPaolo Bonzinimy $type_constant2 = '\%([-_\w]+)';
221e2c54635SPaolo Bonzinimy $type_func = '(\w+)\(\)';
222e2c54635SPaolo Bonzinimy $type_param = '\@(\w*((\.\w+)|(->\w+))*(\.\.\.)?)';
223b1e8e720SMauro Carvalho Chehabmy $type_param_ref = '([\!]?)\@(\w*((\.\w+)|(->\w+))*(\.\.\.)?)';
224e2c54635SPaolo Bonzinimy $type_fp_param = '\@(\w+)\(\)';  # Special RST handling for func ptr params
22576083982SMauro Carvalho Chehabmy $type_fp_param2 = '\@(\w+->\S+)\(\)';  # Special RST handling for structs with func ptr params
226e2c54635SPaolo Bonzinimy $type_env = '(\$\w+)';
2274cf41794SPaolo Bonzinimy $type_enum = '#(enum\s*([_\w]+))';
2284cf41794SPaolo Bonzinimy $type_struct = '#(struct\s*([_\w]+))';
2294cf41794SPaolo Bonzinimy $type_typedef = '#(([A-Z][_\w]*))';
2304cf41794SPaolo Bonzinimy $type_union = '#(union\s*([_\w]+))';
2314cf41794SPaolo Bonzinimy $type_member = '#([_\w]+)(\.|->)([_\w]+)';
2324cf41794SPaolo Bonzinimy $type_fallback = '(?!)';    # this never matches
233e2c54635SPaolo Bonzinimy $type_member_func = $type_member . '\(\)';
234e2c54635SPaolo Bonzini
235e2c54635SPaolo Bonzini# Output conversion substitutions.
236e2c54635SPaolo Bonzini#  One for each output format
237e2c54635SPaolo Bonzini
238e2c54635SPaolo Bonzini# these are pretty rough
239e2c54635SPaolo Bonzinimy @highlights_man = (
240e2c54635SPaolo Bonzini                      [$type_constant, "\$1"],
241e2c54635SPaolo Bonzini                      [$type_constant2, "\$1"],
242e2c54635SPaolo Bonzini                      [$type_func, "\\\\fB\$1\\\\fP"],
243e2c54635SPaolo Bonzini                      [$type_enum, "\\\\fI\$1\\\\fP"],
244e2c54635SPaolo Bonzini                      [$type_struct, "\\\\fI\$1\\\\fP"],
245e2c54635SPaolo Bonzini                      [$type_typedef, "\\\\fI\$1\\\\fP"],
246e2c54635SPaolo Bonzini                      [$type_union, "\\\\fI\$1\\\\fP"],
247e2c54635SPaolo Bonzini                      [$type_param, "\\\\fI\$1\\\\fP"],
248b1e8e720SMauro Carvalho Chehab                      [$type_param_ref, "\\\\fI\$1\$2\\\\fP"],
249e2c54635SPaolo Bonzini                      [$type_member, "\\\\fI\$1\$2\$3\\\\fP"],
250e2c54635SPaolo Bonzini                      [$type_fallback, "\\\\fI\$1\\\\fP"]
251e2c54635SPaolo Bonzini		     );
252e2c54635SPaolo Bonzinimy $blankline_man = "";
253e2c54635SPaolo Bonzini
254e2c54635SPaolo Bonzini# rst-mode
255e2c54635SPaolo Bonzinimy @highlights_rst = (
256e2c54635SPaolo Bonzini                       [$type_constant, "``\$1``"],
257e2c54635SPaolo Bonzini                       [$type_constant2, "``\$1``"],
258e2c54635SPaolo Bonzini                       # Note: need to escape () to avoid func matching later
259e2c54635SPaolo Bonzini                       [$type_member_func, "\\:c\\:type\\:`\$1\$2\$3\\\\(\\\\) <\$1>`"],
260e2c54635SPaolo Bonzini                       [$type_member, "\\:c\\:type\\:`\$1\$2\$3 <\$1>`"],
261e2c54635SPaolo Bonzini		       [$type_fp_param, "**\$1\\\\(\\\\)**"],
26276083982SMauro Carvalho Chehab		       [$type_fp_param2, "**\$1\\\\(\\\\)**"],
263e2c54635SPaolo Bonzini                       [$type_func, "\$1()"],
264e2c54635SPaolo Bonzini                       [$type_enum, "\\:c\\:type\\:`\$1 <\$2>`"],
265e2c54635SPaolo Bonzini                       [$type_struct, "\\:c\\:type\\:`\$1 <\$2>`"],
266e2c54635SPaolo Bonzini                       [$type_typedef, "\\:c\\:type\\:`\$1 <\$2>`"],
267e2c54635SPaolo Bonzini                       [$type_union, "\\:c\\:type\\:`\$1 <\$2>`"],
268e2c54635SPaolo Bonzini                       # in rst this can refer to any type
269e2c54635SPaolo Bonzini                       [$type_fallback, "\\:c\\:type\\:`\$1`"],
270b1e8e720SMauro Carvalho Chehab                       [$type_param_ref, "**\$1\$2**"]
271e2c54635SPaolo Bonzini		      );
272e2c54635SPaolo Bonzinimy $blankline_rst = "\n";
273e2c54635SPaolo Bonzini
274e2c54635SPaolo Bonzini# read arguments
275e2c54635SPaolo Bonziniif ($#ARGV == -1) {
276e2c54635SPaolo Bonzini    usage();
277e2c54635SPaolo Bonzini}
278e2c54635SPaolo Bonzini
279e2c54635SPaolo Bonzinimy $kernelversion;
280*486966e4SMauro Carvalho Chehabmy ($sphinx_major, $sphinx_minor, $sphinx_patch);
2816d3a3cfcSMauro Carvalho Chehab
282e2c54635SPaolo Bonzinimy $dohighlight = "";
283e2c54635SPaolo Bonzini
284e2c54635SPaolo Bonzinimy $verbose = 0;
28586cba217SPierre-Louis Bossartmy $Werror = 0;
286e2c54635SPaolo Bonzinimy $output_mode = "rst";
287e2c54635SPaolo Bonzinimy $output_preformatted = 0;
288e2c54635SPaolo Bonzinimy $no_doc_sections = 0;
289e2c54635SPaolo Bonzinimy $enable_lineno = 0;
290e2c54635SPaolo Bonzinimy @highlights = @highlights_rst;
291e2c54635SPaolo Bonzinimy $blankline = $blankline_rst;
292e2c54635SPaolo Bonzinimy $modulename = "Kernel API";
293e2c54635SPaolo Bonzini
294e2c54635SPaolo Bonziniuse constant {
295e2c54635SPaolo Bonzini    OUTPUT_ALL          => 0, # output all symbols and doc sections
296e2c54635SPaolo Bonzini    OUTPUT_INCLUDE      => 1, # output only specified symbols
29778c8c92cSMauro Carvalho Chehab    OUTPUT_EXPORTED     => 2, # output exported symbols
29878c8c92cSMauro Carvalho Chehab    OUTPUT_INTERNAL     => 3, # output non-exported symbols
299e2c54635SPaolo Bonzini};
300e2c54635SPaolo Bonzinimy $output_selection = OUTPUT_ALL;
301e2c54635SPaolo Bonzinimy $show_not_found = 0;	# No longer used
302e2c54635SPaolo Bonzini
303e2c54635SPaolo Bonzinimy @export_file_list;
304e2c54635SPaolo Bonzini
305e2c54635SPaolo Bonzinimy @build_time;
306e2c54635SPaolo Bonziniif (defined($ENV{'KBUILD_BUILD_TIMESTAMP'}) &&
307e2c54635SPaolo Bonzini    (my $seconds = `date -d"${ENV{'KBUILD_BUILD_TIMESTAMP'}}" +%s`) ne '') {
308e2c54635SPaolo Bonzini    @build_time = gmtime($seconds);
309e2c54635SPaolo Bonzini} else {
310e2c54635SPaolo Bonzini    @build_time = localtime;
311e2c54635SPaolo Bonzini}
312e2c54635SPaolo Bonzini
313e2c54635SPaolo Bonzinimy $man_date = ('January', 'February', 'March', 'April', 'May', 'June',
314e2c54635SPaolo Bonzini		'July', 'August', 'September', 'October',
315e2c54635SPaolo Bonzini		'November', 'December')[$build_time[4]] .
316e2c54635SPaolo Bonzini  " " . ($build_time[5]+1900);
317e2c54635SPaolo Bonzini
318e2c54635SPaolo Bonzini# Essentially these are globals.
319e2c54635SPaolo Bonzini# They probably want to be tidied up, made more localised or something.
320e2c54635SPaolo Bonzini# CAVEAT EMPTOR!  Some of the others I localised may not want to be, which
321e2c54635SPaolo Bonzini# could cause "use of undefined value" or other bugs.
322e2c54635SPaolo Bonzinimy ($function, %function_table, %parametertypes, $declaration_purpose);
32378c8c92cSMauro Carvalho Chehabmy %nosymbol_table = ();
324e2c54635SPaolo Bonzinimy $declaration_start_line;
325e2c54635SPaolo Bonzinimy ($type, $declaration_name, $return_type);
326e2c54635SPaolo Bonzinimy ($newsection, $newcontents, $prototype, $brcount, %source_map);
327e2c54635SPaolo Bonzini
328e2c54635SPaolo Bonziniif (defined($ENV{'KBUILD_VERBOSE'})) {
329e2c54635SPaolo Bonzini	$verbose = "$ENV{'KBUILD_VERBOSE'}";
330e2c54635SPaolo Bonzini}
331e2c54635SPaolo Bonzini
33286cba217SPierre-Louis Bossartif (defined($ENV{'KDOC_WERROR'})) {
33386cba217SPierre-Louis Bossart	$Werror = "$ENV{'KDOC_WERROR'}";
33486cba217SPierre-Louis Bossart}
33586cba217SPierre-Louis Bossart
33686cba217SPierre-Louis Bossartif (defined($ENV{'KCFLAGS'})) {
33786cba217SPierre-Louis Bossart	my $kcflags = "$ENV{'KCFLAGS'}";
33886cba217SPierre-Louis Bossart
33986cba217SPierre-Louis Bossart	if ($kcflags =~ /Werror/) {
34086cba217SPierre-Louis Bossart		$Werror = 1;
34186cba217SPierre-Louis Bossart	}
34286cba217SPierre-Louis Bossart}
34386cba217SPierre-Louis Bossart
344e2c54635SPaolo Bonzini# Generated docbook code is inserted in a template at a point where
345e2c54635SPaolo Bonzini# docbook v3.1 requires a non-zero sequence of RefEntry's; see:
346cd08b809SAlexander A. Klimov# https://www.oasis-open.org/docbook/documentation/reference/html/refentry.html
347e2c54635SPaolo Bonzini# We keep track of number of generated entries and generate a dummy
348e2c54635SPaolo Bonzini# if needs be to ensure the expanded template can be postprocessed
349e2c54635SPaolo Bonzini# into html.
350e2c54635SPaolo Bonzinimy $section_counter = 0;
351e2c54635SPaolo Bonzini
352e2c54635SPaolo Bonzinimy $lineprefix="";
353e2c54635SPaolo Bonzini
354e2c54635SPaolo Bonzini# Parser states
355e2c54635SPaolo Bonziniuse constant {
356e2c54635SPaolo Bonzini    STATE_NORMAL        => 0,        # normal code
357e2c54635SPaolo Bonzini    STATE_NAME          => 1,        # looking for function name
358e2c54635SPaolo Bonzini    STATE_BODY_MAYBE    => 2,        # body - or maybe more description
359e2c54635SPaolo Bonzini    STATE_BODY          => 3,        # the body of the comment
3609f2b463aSMauro Carvalho Chehab    STATE_BODY_WITH_BLANK_LINE => 4, # the body, which has a blank line
3619f2b463aSMauro Carvalho Chehab    STATE_PROTO         => 5,        # scanning prototype
3629f2b463aSMauro Carvalho Chehab    STATE_DOCBLOCK      => 6,        # documentation block
3639f2b463aSMauro Carvalho Chehab    STATE_INLINE        => 7,        # gathering doc outside main block
364e2c54635SPaolo Bonzini};
365e2c54635SPaolo Bonzinimy $state;
366e2c54635SPaolo Bonzinimy $in_doc_sect;
367e2c54635SPaolo Bonzinimy $leading_space;
368e2c54635SPaolo Bonzini
369e2c54635SPaolo Bonzini# Inline documentation state
370e2c54635SPaolo Bonziniuse constant {
371e2c54635SPaolo Bonzini    STATE_INLINE_NA     => 0, # not applicable ($state != STATE_INLINE)
372e2c54635SPaolo Bonzini    STATE_INLINE_NAME   => 1, # looking for member name (@foo:)
373e2c54635SPaolo Bonzini    STATE_INLINE_TEXT   => 2, # looking for member documentation
374e2c54635SPaolo Bonzini    STATE_INLINE_END    => 3, # done
375e2c54635SPaolo Bonzini    STATE_INLINE_ERROR  => 4, # error - Comment without header was found.
376e2c54635SPaolo Bonzini                              # Spit a warning as it's not
377e2c54635SPaolo Bonzini                              # proper kernel-doc and ignore the rest.
378e2c54635SPaolo Bonzini};
379e2c54635SPaolo Bonzinimy $inline_doc_state;
380e2c54635SPaolo Bonzini
381e2c54635SPaolo Bonzini#declaration types: can be
382e2c54635SPaolo Bonzini# 'function', 'struct', 'union', 'enum', 'typedef'
383e2c54635SPaolo Bonzinimy $decl_type;
384e2c54635SPaolo Bonzini
385e2c54635SPaolo Bonzinimy $doc_start = '^/\*\*\s*$'; # Allow whitespace at end of comment start.
386e2c54635SPaolo Bonzinimy $doc_end = '\*/';
387e2c54635SPaolo Bonzinimy $doc_com = '\s*\*\s*';
388e2c54635SPaolo Bonzinimy $doc_com_body = '\s*\* ?';
389e2c54635SPaolo Bonzinimy $doc_decl = $doc_com . '(\w+)';
390e2c54635SPaolo Bonzini# @params and a strictly limited set of supported section names
391e2c54635SPaolo Bonzinimy $doc_sect = $doc_com .
392e2c54635SPaolo Bonzini    '\s*(\@[.\w]+|\@\.\.\.|description|context|returns?|notes?|examples?)\s*:(.*)';
393e2c54635SPaolo Bonzinimy $doc_content = $doc_com_body . '(.*)';
394e2c54635SPaolo Bonzinimy $doc_block = $doc_com . 'DOC:\s*(.*)?';
395e2c54635SPaolo Bonzinimy $doc_inline_start = '^\s*/\*\*\s*$';
396e2c54635SPaolo Bonzinimy $doc_inline_sect = '\s*\*\s*(@\s*[\w][\w\.]*\s*):(.*)';
397e2c54635SPaolo Bonzinimy $doc_inline_end = '^\s*\*/\s*$';
398e2c54635SPaolo Bonzinimy $doc_inline_oneline = '^\s*/\*\*\s*(@[\w\s]+):\s*(.*)\s*\*/\s*$';
399e2c54635SPaolo Bonzinimy $export_symbol = '^\s*EXPORT_SYMBOL(_GPL)?\s*\(\s*(\w+)\s*\)\s*;';
400e2c54635SPaolo Bonzini
401e2c54635SPaolo Bonzinimy %parameterdescs;
402e2c54635SPaolo Bonzinimy %parameterdesc_start_lines;
403e2c54635SPaolo Bonzinimy @parameterlist;
404e2c54635SPaolo Bonzinimy %sections;
405e2c54635SPaolo Bonzinimy @sectionlist;
406e2c54635SPaolo Bonzinimy %section_start_lines;
407e2c54635SPaolo Bonzinimy $sectcheck;
408e2c54635SPaolo Bonzinimy $struct_actual;
409e2c54635SPaolo Bonzini
410e2c54635SPaolo Bonzinimy $contents = "";
411e2c54635SPaolo Bonzinimy $new_start_line = 0;
412e2c54635SPaolo Bonzini
413e2c54635SPaolo Bonzini# the canonical section names. see also $doc_sect above.
414e2c54635SPaolo Bonzinimy $section_default = "Description";	# default section
415e2c54635SPaolo Bonzinimy $section_intro = "Introduction";
416e2c54635SPaolo Bonzinimy $section = $section_default;
417e2c54635SPaolo Bonzinimy $section_context = "Context";
418e2c54635SPaolo Bonzinimy $section_return = "Return";
419e2c54635SPaolo Bonzini
420e2c54635SPaolo Bonzinimy $undescribed = "-- undescribed --";
421e2c54635SPaolo Bonzini
422e2c54635SPaolo Bonzinireset_state();
423e2c54635SPaolo Bonzini
424e2c54635SPaolo Bonziniwhile ($ARGV[0] =~ m/^--?(.*)/) {
425e2c54635SPaolo Bonzini    my $cmd = $1;
426e2c54635SPaolo Bonzini    shift @ARGV;
427e2c54635SPaolo Bonzini    if ($cmd eq "man") {
428e2c54635SPaolo Bonzini	$output_mode = "man";
429e2c54635SPaolo Bonzini	@highlights = @highlights_man;
430e2c54635SPaolo Bonzini	$blankline = $blankline_man;
431e2c54635SPaolo Bonzini    } elsif ($cmd eq "rst") {
432e2c54635SPaolo Bonzini	$output_mode = "rst";
433e2c54635SPaolo Bonzini	@highlights = @highlights_rst;
434e2c54635SPaolo Bonzini	$blankline = $blankline_rst;
435e2c54635SPaolo Bonzini    } elsif ($cmd eq "none") {
436e2c54635SPaolo Bonzini	$output_mode = "none";
437e2c54635SPaolo Bonzini    } elsif ($cmd eq "module") { # not needed for XML, inherits from calling document
438e2c54635SPaolo Bonzini	$modulename = shift @ARGV;
439e2c54635SPaolo Bonzini    } elsif ($cmd eq "function") { # to only output specific functions
440e2c54635SPaolo Bonzini	$output_selection = OUTPUT_INCLUDE;
441e2c54635SPaolo Bonzini	$function = shift @ARGV;
442e2c54635SPaolo Bonzini	$function_table{$function} = 1;
44378c8c92cSMauro Carvalho Chehab    } elsif ($cmd eq "nosymbol") { # Exclude specific symbols
44478c8c92cSMauro Carvalho Chehab	my $symbol = shift @ARGV;
44578c8c92cSMauro Carvalho Chehab	$nosymbol_table{$symbol} = 1;
446e2c54635SPaolo Bonzini    } elsif ($cmd eq "export") { # only exported symbols
447e2c54635SPaolo Bonzini	$output_selection = OUTPUT_EXPORTED;
448e2c54635SPaolo Bonzini	%function_table = ();
449e2c54635SPaolo Bonzini    } elsif ($cmd eq "internal") { # only non-exported symbols
450e2c54635SPaolo Bonzini	$output_selection = OUTPUT_INTERNAL;
451e2c54635SPaolo Bonzini	%function_table = ();
452e2c54635SPaolo Bonzini    } elsif ($cmd eq "export-file") {
453e2c54635SPaolo Bonzini	my $file = shift @ARGV;
454e2c54635SPaolo Bonzini	push(@export_file_list, $file);
455e2c54635SPaolo Bonzini    } elsif ($cmd eq "v") {
456e2c54635SPaolo Bonzini	$verbose = 1;
45786cba217SPierre-Louis Bossart    } elsif ($cmd eq "Werror") {
45886cba217SPierre-Louis Bossart	$Werror = 1;
459e2c54635SPaolo Bonzini    } elsif (($cmd eq "h") || ($cmd eq "help")) {
460e2c54635SPaolo Bonzini	usage();
461e2c54635SPaolo Bonzini    } elsif ($cmd eq 'no-doc-sections') {
462e2c54635SPaolo Bonzini	    $no_doc_sections = 1;
463e2c54635SPaolo Bonzini    } elsif ($cmd eq 'enable-lineno') {
464e2c54635SPaolo Bonzini	    $enable_lineno = 1;
465e2c54635SPaolo Bonzini    } elsif ($cmd eq 'show-not-found') {
466e2c54635SPaolo Bonzini	$show_not_found = 1;  # A no-op but don't fail
467*486966e4SMauro Carvalho Chehab    } elsif ($cmd eq "sphinx-version") {
468*486966e4SMauro Carvalho Chehab	my $ver_string = shift @ARGV;
469*486966e4SMauro Carvalho Chehab	if ($ver_string =~ m/^(\d+)(\.\d+)?(\.\d+)?/) {
470*486966e4SMauro Carvalho Chehab	    $sphinx_major = $1;
471*486966e4SMauro Carvalho Chehab	    if (defined($2)) {
472*486966e4SMauro Carvalho Chehab		$sphinx_minor = substr($2,1);
473*486966e4SMauro Carvalho Chehab	    } else {
474*486966e4SMauro Carvalho Chehab		$sphinx_minor = 0;
475*486966e4SMauro Carvalho Chehab	    }
476*486966e4SMauro Carvalho Chehab	    if (defined($3)) {
477*486966e4SMauro Carvalho Chehab		$sphinx_patch = substr($3,1)
478*486966e4SMauro Carvalho Chehab	    } else {
479*486966e4SMauro Carvalho Chehab		$sphinx_patch = 0;
480*486966e4SMauro Carvalho Chehab	    }
481*486966e4SMauro Carvalho Chehab	} else {
482*486966e4SMauro Carvalho Chehab	    die "Sphinx version should either major.minor or major.minor.patch format\n";
483*486966e4SMauro Carvalho Chehab	}
484e2c54635SPaolo Bonzini    } else {
485e2c54635SPaolo Bonzini	# Unknown argument
486e2c54635SPaolo Bonzini        usage();
487e2c54635SPaolo Bonzini    }
488e2c54635SPaolo Bonzini}
489e2c54635SPaolo Bonzini
490e2c54635SPaolo Bonzini# continue execution near EOF;
491e2c54635SPaolo Bonzini
4926d3a3cfcSMauro Carvalho Chehab# The C domain dialect changed on Sphinx 3. So, we need to check the
4936d3a3cfcSMauro Carvalho Chehab# version in order to produce the right tags.
4946d3a3cfcSMauro Carvalho Chehabsub findprog($)
4956d3a3cfcSMauro Carvalho Chehab{
4966d3a3cfcSMauro Carvalho Chehab	foreach(split(/:/, $ENV{PATH})) {
4976d3a3cfcSMauro Carvalho Chehab		return "$_/$_[0]" if(-x "$_/$_[0]");
4986d3a3cfcSMauro Carvalho Chehab	}
4996d3a3cfcSMauro Carvalho Chehab}
5006d3a3cfcSMauro Carvalho Chehab
5016d3a3cfcSMauro Carvalho Chehabsub get_sphinx_version()
5026d3a3cfcSMauro Carvalho Chehab{
5036d3a3cfcSMauro Carvalho Chehab	my $ver;
5046d3a3cfcSMauro Carvalho Chehab
5056d3a3cfcSMauro Carvalho Chehab	my $cmd = "sphinx-build";
5066d3a3cfcSMauro Carvalho Chehab	if (!findprog($cmd)) {
5076d3a3cfcSMauro Carvalho Chehab		my $cmd = "sphinx-build3";
508*486966e4SMauro Carvalho Chehab		if (!findprog($cmd)) {
509*486966e4SMauro Carvalho Chehab			$sphinx_major = 1;
510*486966e4SMauro Carvalho Chehab			$sphinx_minor = 2;
511*486966e4SMauro Carvalho Chehab			$sphinx_patch = 0;
512*486966e4SMauro Carvalho Chehab			printf STDERR "Warning: Sphinx version not found. Using default (Sphinx version %d.%d.%d)\n",
513*486966e4SMauro Carvalho Chehab			       $sphinx_major, $sphinx_minor, $sphinx_patch;
514*486966e4SMauro Carvalho Chehab			return;
515*486966e4SMauro Carvalho Chehab		}
5166d3a3cfcSMauro Carvalho Chehab	}
5176d3a3cfcSMauro Carvalho Chehab
5186d3a3cfcSMauro Carvalho Chehab	open IN, "$cmd --version 2>&1 |";
5196d3a3cfcSMauro Carvalho Chehab	while (<IN>) {
5206d3a3cfcSMauro Carvalho Chehab		if (m/^\s*sphinx-build\s+([\d]+)\.([\d\.]+)(\+\/[\da-f]+)?$/) {
521*486966e4SMauro Carvalho Chehab			$sphinx_major = $1;
522*486966e4SMauro Carvalho Chehab			$sphinx_minor = $2;
523*486966e4SMauro Carvalho Chehab			$sphinx_patch = $3;
5246d3a3cfcSMauro Carvalho Chehab			last;
5256d3a3cfcSMauro Carvalho Chehab		}
5266d3a3cfcSMauro Carvalho Chehab		# Sphinx 1.2.x uses a different format
5276d3a3cfcSMauro Carvalho Chehab		if (m/^\s*Sphinx.*\s+([\d]+)\.([\d\.]+)$/) {
528*486966e4SMauro Carvalho Chehab			$sphinx_major = $1;
529*486966e4SMauro Carvalho Chehab			$sphinx_minor = $2;
530*486966e4SMauro Carvalho Chehab			$sphinx_patch = $3;
5316d3a3cfcSMauro Carvalho Chehab			last;
5326d3a3cfcSMauro Carvalho Chehab		}
5336d3a3cfcSMauro Carvalho Chehab	}
5346d3a3cfcSMauro Carvalho Chehab	close IN;
5356d3a3cfcSMauro Carvalho Chehab}
5366d3a3cfcSMauro Carvalho Chehab
537e2c54635SPaolo Bonzini# get kernel version from env
538e2c54635SPaolo Bonzinisub get_kernel_version() {
539e2c54635SPaolo Bonzini    my $version = 'unknown kernel version';
540e2c54635SPaolo Bonzini
541e2c54635SPaolo Bonzini    if (defined($ENV{'KERNELVERSION'})) {
542e2c54635SPaolo Bonzini	$version = $ENV{'KERNELVERSION'};
543e2c54635SPaolo Bonzini    }
544e2c54635SPaolo Bonzini    return $version;
545e2c54635SPaolo Bonzini}
546e2c54635SPaolo Bonzini
547e2c54635SPaolo Bonzini#
548e2c54635SPaolo Bonzinisub print_lineno {
549e2c54635SPaolo Bonzini    my $lineno = shift;
550e2c54635SPaolo Bonzini    if ($enable_lineno && defined($lineno)) {
551e2c54635SPaolo Bonzini        print "#define LINENO " . $lineno . "\n";
552e2c54635SPaolo Bonzini    }
553e2c54635SPaolo Bonzini}
554e2c54635SPaolo Bonzini##
555e2c54635SPaolo Bonzini# dumps section contents to arrays/hashes intended for that purpose.
556e2c54635SPaolo Bonzini#
557e2c54635SPaolo Bonzinisub dump_section {
558e2c54635SPaolo Bonzini    my $file = shift;
559e2c54635SPaolo Bonzini    my $name = shift;
560e2c54635SPaolo Bonzini    my $contents = join "\n", @_;
561e2c54635SPaolo Bonzini
562e2c54635SPaolo Bonzini    if ($name =~ m/$type_param/) {
563e2c54635SPaolo Bonzini	$name = $1;
564e2c54635SPaolo Bonzini	$parameterdescs{$name} = $contents;
565e2c54635SPaolo Bonzini	$sectcheck = $sectcheck . $name . " ";
566e2c54635SPaolo Bonzini        $parameterdesc_start_lines{$name} = $new_start_line;
567e2c54635SPaolo Bonzini        $new_start_line = 0;
568e2c54635SPaolo Bonzini    } elsif ($name eq "@\.\.\.") {
569e2c54635SPaolo Bonzini	$name = "...";
570e2c54635SPaolo Bonzini	$parameterdescs{$name} = $contents;
571e2c54635SPaolo Bonzini	$sectcheck = $sectcheck . $name . " ";
572e2c54635SPaolo Bonzini        $parameterdesc_start_lines{$name} = $new_start_line;
573e2c54635SPaolo Bonzini        $new_start_line = 0;
574e2c54635SPaolo Bonzini    } else {
575e2c54635SPaolo Bonzini	if (defined($sections{$name}) && ($sections{$name} ne "")) {
576e2c54635SPaolo Bonzini	    # Only warn on user specified duplicate section names.
577e2c54635SPaolo Bonzini	    if ($name ne $section_default) {
578e2c54635SPaolo Bonzini		print STDERR "${file}:$.: warning: duplicate section name '$name'\n";
579e2c54635SPaolo Bonzini		++$warnings;
580e2c54635SPaolo Bonzini	    }
581e2c54635SPaolo Bonzini	    $sections{$name} .= $contents;
582e2c54635SPaolo Bonzini	} else {
583e2c54635SPaolo Bonzini	    $sections{$name} = $contents;
584e2c54635SPaolo Bonzini	    push @sectionlist, $name;
585e2c54635SPaolo Bonzini            $section_start_lines{$name} = $new_start_line;
586e2c54635SPaolo Bonzini            $new_start_line = 0;
587e2c54635SPaolo Bonzini	}
588e2c54635SPaolo Bonzini    }
589e2c54635SPaolo Bonzini}
590e2c54635SPaolo Bonzini
591e2c54635SPaolo Bonzini##
592e2c54635SPaolo Bonzini# dump DOC: section after checking that it should go out
593e2c54635SPaolo Bonzini#
594e2c54635SPaolo Bonzinisub dump_doc_section {
595e2c54635SPaolo Bonzini    my $file = shift;
596e2c54635SPaolo Bonzini    my $name = shift;
597e2c54635SPaolo Bonzini    my $contents = join "\n", @_;
598e2c54635SPaolo Bonzini
599e2c54635SPaolo Bonzini    if ($no_doc_sections) {
600e2c54635SPaolo Bonzini        return;
601e2c54635SPaolo Bonzini    }
602e2c54635SPaolo Bonzini
60378c8c92cSMauro Carvalho Chehab    return if (defined($nosymbol_table{$name}));
60478c8c92cSMauro Carvalho Chehab
605e2c54635SPaolo Bonzini    if (($output_selection == OUTPUT_ALL) ||
60678c8c92cSMauro Carvalho Chehab	(($output_selection == OUTPUT_INCLUDE) &&
60778c8c92cSMauro Carvalho Chehab	 defined($function_table{$name})))
608e2c54635SPaolo Bonzini    {
609e2c54635SPaolo Bonzini	dump_section($file, $name, $contents);
610e2c54635SPaolo Bonzini	output_blockhead({'sectionlist' => \@sectionlist,
611e2c54635SPaolo Bonzini			  'sections' => \%sections,
612e2c54635SPaolo Bonzini			  'module' => $modulename,
613e2c54635SPaolo Bonzini			  'content-only' => ($output_selection != OUTPUT_ALL), });
614e2c54635SPaolo Bonzini    }
615e2c54635SPaolo Bonzini}
616e2c54635SPaolo Bonzini
617e2c54635SPaolo Bonzini##
618e2c54635SPaolo Bonzini# output function
619e2c54635SPaolo Bonzini#
620e2c54635SPaolo Bonzini# parameterdescs, a hash.
621e2c54635SPaolo Bonzini#  function => "function name"
622e2c54635SPaolo Bonzini#  parameterlist => @list of parameters
623e2c54635SPaolo Bonzini#  parameterdescs => %parameter descriptions
624e2c54635SPaolo Bonzini#  sectionlist => @list of sections
625e2c54635SPaolo Bonzini#  sections => %section descriptions
626e2c54635SPaolo Bonzini#
627e2c54635SPaolo Bonzini
628e2c54635SPaolo Bonzinisub output_highlight {
629e2c54635SPaolo Bonzini    my $contents = join "\n",@_;
630e2c54635SPaolo Bonzini    my $line;
631e2c54635SPaolo Bonzini
632e2c54635SPaolo Bonzini#   DEBUG
633e2c54635SPaolo Bonzini#   if (!defined $contents) {
634e2c54635SPaolo Bonzini#	use Carp;
635e2c54635SPaolo Bonzini#	confess "output_highlight got called with no args?\n";
636e2c54635SPaolo Bonzini#   }
637e2c54635SPaolo Bonzini
638e2c54635SPaolo Bonzini#   print STDERR "contents b4:$contents\n";
639e2c54635SPaolo Bonzini    eval $dohighlight;
640e2c54635SPaolo Bonzini    die $@ if $@;
641e2c54635SPaolo Bonzini#   print STDERR "contents af:$contents\n";
642e2c54635SPaolo Bonzini
643e2c54635SPaolo Bonzini    foreach $line (split "\n", $contents) {
644e2c54635SPaolo Bonzini	if (! $output_preformatted) {
645e2c54635SPaolo Bonzini	    $line =~ s/^\s*//;
646e2c54635SPaolo Bonzini	}
647e2c54635SPaolo Bonzini	if ($line eq ""){
648e2c54635SPaolo Bonzini	    if (! $output_preformatted) {
649e2c54635SPaolo Bonzini		print $lineprefix, $blankline;
650e2c54635SPaolo Bonzini	    }
651e2c54635SPaolo Bonzini	} else {
652e2c54635SPaolo Bonzini	    if ($output_mode eq "man" && substr($line, 0, 1) eq ".") {
653e2c54635SPaolo Bonzini		print "\\&$line";
654e2c54635SPaolo Bonzini	    } else {
655e2c54635SPaolo Bonzini		print $lineprefix, $line;
656e2c54635SPaolo Bonzini	    }
657e2c54635SPaolo Bonzini	}
658e2c54635SPaolo Bonzini	print "\n";
659e2c54635SPaolo Bonzini    }
660e2c54635SPaolo Bonzini}
661e2c54635SPaolo Bonzini
662e2c54635SPaolo Bonzini##
663e2c54635SPaolo Bonzini# output function in man
664e2c54635SPaolo Bonzinisub output_function_man(%) {
665e2c54635SPaolo Bonzini    my %args = %{$_[0]};
666e2c54635SPaolo Bonzini    my ($parameter, $section);
667e2c54635SPaolo Bonzini    my $count;
668e2c54635SPaolo Bonzini
669e2c54635SPaolo Bonzini    print ".TH \"$args{'function'}\" 9 \"$args{'function'}\" \"$man_date\" \"Kernel Hacker's Manual\" LINUX\n";
670e2c54635SPaolo Bonzini
671e2c54635SPaolo Bonzini    print ".SH NAME\n";
672e2c54635SPaolo Bonzini    print $args{'function'} . " \\- " . $args{'purpose'} . "\n";
673e2c54635SPaolo Bonzini
674e2c54635SPaolo Bonzini    print ".SH SYNOPSIS\n";
675e2c54635SPaolo Bonzini    if ($args{'functiontype'} ne "") {
676e2c54635SPaolo Bonzini	print ".B \"" . $args{'functiontype'} . "\" " . $args{'function'} . "\n";
677e2c54635SPaolo Bonzini    } else {
678e2c54635SPaolo Bonzini	print ".B \"" . $args{'function'} . "\n";
679e2c54635SPaolo Bonzini    }
680e2c54635SPaolo Bonzini    $count = 0;
681e2c54635SPaolo Bonzini    my $parenth = "(";
682e2c54635SPaolo Bonzini    my $post = ",";
683e2c54635SPaolo Bonzini    foreach my $parameter (@{$args{'parameterlist'}}) {
684e2c54635SPaolo Bonzini	if ($count == $#{$args{'parameterlist'}}) {
685e2c54635SPaolo Bonzini	    $post = ");";
686e2c54635SPaolo Bonzini	}
687e2c54635SPaolo Bonzini	$type = $args{'parametertypes'}{$parameter};
688e2c54635SPaolo Bonzini	if ($type =~ m/([^\(]*\(\*)\s*\)\s*\(([^\)]*)\)/) {
689e2c54635SPaolo Bonzini	    # pointer-to-function
6900c771852SMauro Carvalho Chehab	    print ".BI \"" . $parenth . $1 . "\" " . " \") (" . $2 . ")" . $post . "\"\n";
691e2c54635SPaolo Bonzini	} else {
692e2c54635SPaolo Bonzini	    $type =~ s/([^\*])$/$1 /;
6930c771852SMauro Carvalho Chehab	    print ".BI \"" . $parenth . $type . "\" " . " \"" . $post . "\"\n";
694e2c54635SPaolo Bonzini	}
695e2c54635SPaolo Bonzini	$count++;
696e2c54635SPaolo Bonzini	$parenth = "";
697e2c54635SPaolo Bonzini    }
698e2c54635SPaolo Bonzini
699e2c54635SPaolo Bonzini    print ".SH ARGUMENTS\n";
700e2c54635SPaolo Bonzini    foreach $parameter (@{$args{'parameterlist'}}) {
701e2c54635SPaolo Bonzini	my $parameter_name = $parameter;
702e2c54635SPaolo Bonzini	$parameter_name =~ s/\[.*//;
703e2c54635SPaolo Bonzini
704e2c54635SPaolo Bonzini	print ".IP \"" . $parameter . "\" 12\n";
705e2c54635SPaolo Bonzini	output_highlight($args{'parameterdescs'}{$parameter_name});
706e2c54635SPaolo Bonzini    }
707e2c54635SPaolo Bonzini    foreach $section (@{$args{'sectionlist'}}) {
708e2c54635SPaolo Bonzini	print ".SH \"", uc $section, "\"\n";
709e2c54635SPaolo Bonzini	output_highlight($args{'sections'}{$section});
710e2c54635SPaolo Bonzini    }
711e2c54635SPaolo Bonzini}
712e2c54635SPaolo Bonzini
713e2c54635SPaolo Bonzini##
714e2c54635SPaolo Bonzini# output enum in man
715e2c54635SPaolo Bonzinisub output_enum_man(%) {
716e2c54635SPaolo Bonzini    my %args = %{$_[0]};
717e2c54635SPaolo Bonzini    my ($parameter, $section);
718e2c54635SPaolo Bonzini    my $count;
719e2c54635SPaolo Bonzini
720e2c54635SPaolo Bonzini    print ".TH \"$args{'module'}\" 9 \"enum $args{'enum'}\" \"$man_date\" \"API Manual\" LINUX\n";
721e2c54635SPaolo Bonzini
722e2c54635SPaolo Bonzini    print ".SH NAME\n";
723e2c54635SPaolo Bonzini    print "enum " . $args{'enum'} . " \\- " . $args{'purpose'} . "\n";
724e2c54635SPaolo Bonzini
725e2c54635SPaolo Bonzini    print ".SH SYNOPSIS\n";
726e2c54635SPaolo Bonzini    print "enum " . $args{'enum'} . " {\n";
727e2c54635SPaolo Bonzini    $count = 0;
728e2c54635SPaolo Bonzini    foreach my $parameter (@{$args{'parameterlist'}}) {
729e2c54635SPaolo Bonzini	print ".br\n.BI \"    $parameter\"\n";
730e2c54635SPaolo Bonzini	if ($count == $#{$args{'parameterlist'}}) {
731e2c54635SPaolo Bonzini	    print "\n};\n";
732e2c54635SPaolo Bonzini	    last;
733e2c54635SPaolo Bonzini	}
734e2c54635SPaolo Bonzini	else {
735e2c54635SPaolo Bonzini	    print ", \n.br\n";
736e2c54635SPaolo Bonzini	}
737e2c54635SPaolo Bonzini	$count++;
738e2c54635SPaolo Bonzini    }
739e2c54635SPaolo Bonzini
740e2c54635SPaolo Bonzini    print ".SH Constants\n";
741e2c54635SPaolo Bonzini    foreach $parameter (@{$args{'parameterlist'}}) {
742e2c54635SPaolo Bonzini	my $parameter_name = $parameter;
743e2c54635SPaolo Bonzini	$parameter_name =~ s/\[.*//;
744e2c54635SPaolo Bonzini
745e2c54635SPaolo Bonzini	print ".IP \"" . $parameter . "\" 12\n";
746e2c54635SPaolo Bonzini	output_highlight($args{'parameterdescs'}{$parameter_name});
747e2c54635SPaolo Bonzini    }
748e2c54635SPaolo Bonzini    foreach $section (@{$args{'sectionlist'}}) {
749e2c54635SPaolo Bonzini	print ".SH \"$section\"\n";
750e2c54635SPaolo Bonzini	output_highlight($args{'sections'}{$section});
751e2c54635SPaolo Bonzini    }
752e2c54635SPaolo Bonzini}
753e2c54635SPaolo Bonzini
754e2c54635SPaolo Bonzini##
755e2c54635SPaolo Bonzini# output struct in man
756e2c54635SPaolo Bonzinisub output_struct_man(%) {
757e2c54635SPaolo Bonzini    my %args = %{$_[0]};
758e2c54635SPaolo Bonzini    my ($parameter, $section);
759e2c54635SPaolo Bonzini
760e2c54635SPaolo Bonzini    print ".TH \"$args{'module'}\" 9 \"" . $args{'type'} . " " . $args{'struct'} . "\" \"$man_date\" \"API Manual\" LINUX\n";
761e2c54635SPaolo Bonzini
762e2c54635SPaolo Bonzini    print ".SH NAME\n";
763e2c54635SPaolo Bonzini    print $args{'type'} . " " . $args{'struct'} . " \\- " . $args{'purpose'} . "\n";
764e2c54635SPaolo Bonzini
765e2c54635SPaolo Bonzini    my $declaration = $args{'definition'};
766e2c54635SPaolo Bonzini    $declaration =~ s/\t/  /g;
767e2c54635SPaolo Bonzini    $declaration =~ s/\n/"\n.br\n.BI \"/g;
768e2c54635SPaolo Bonzini    print ".SH SYNOPSIS\n";
769e2c54635SPaolo Bonzini    print $args{'type'} . " " . $args{'struct'} . " {\n.br\n";
770e2c54635SPaolo Bonzini    print ".BI \"$declaration\n};\n.br\n\n";
771e2c54635SPaolo Bonzini
772e2c54635SPaolo Bonzini    print ".SH Members\n";
773e2c54635SPaolo Bonzini    foreach $parameter (@{$args{'parameterlist'}}) {
774e2c54635SPaolo Bonzini	($parameter =~ /^#/) && next;
775e2c54635SPaolo Bonzini
776e2c54635SPaolo Bonzini	my $parameter_name = $parameter;
777e2c54635SPaolo Bonzini	$parameter_name =~ s/\[.*//;
778e2c54635SPaolo Bonzini
779e2c54635SPaolo Bonzini	($args{'parameterdescs'}{$parameter_name} ne $undescribed) || next;
780e2c54635SPaolo Bonzini	print ".IP \"" . $parameter . "\" 12\n";
781e2c54635SPaolo Bonzini	output_highlight($args{'parameterdescs'}{$parameter_name});
782e2c54635SPaolo Bonzini    }
783e2c54635SPaolo Bonzini    foreach $section (@{$args{'sectionlist'}}) {
784e2c54635SPaolo Bonzini	print ".SH \"$section\"\n";
785e2c54635SPaolo Bonzini	output_highlight($args{'sections'}{$section});
786e2c54635SPaolo Bonzini    }
787e2c54635SPaolo Bonzini}
788e2c54635SPaolo Bonzini
789e2c54635SPaolo Bonzini##
790e2c54635SPaolo Bonzini# output typedef in man
791e2c54635SPaolo Bonzinisub output_typedef_man(%) {
792e2c54635SPaolo Bonzini    my %args = %{$_[0]};
793e2c54635SPaolo Bonzini    my ($parameter, $section);
794e2c54635SPaolo Bonzini
795e2c54635SPaolo Bonzini    print ".TH \"$args{'module'}\" 9 \"$args{'typedef'}\" \"$man_date\" \"API Manual\" LINUX\n";
796e2c54635SPaolo Bonzini
797e2c54635SPaolo Bonzini    print ".SH NAME\n";
798e2c54635SPaolo Bonzini    print "typedef " . $args{'typedef'} . " \\- " . $args{'purpose'} . "\n";
799e2c54635SPaolo Bonzini
800e2c54635SPaolo Bonzini    foreach $section (@{$args{'sectionlist'}}) {
801e2c54635SPaolo Bonzini	print ".SH \"$section\"\n";
802e2c54635SPaolo Bonzini	output_highlight($args{'sections'}{$section});
803e2c54635SPaolo Bonzini    }
804e2c54635SPaolo Bonzini}
805e2c54635SPaolo Bonzini
806e2c54635SPaolo Bonzinisub output_blockhead_man(%) {
807e2c54635SPaolo Bonzini    my %args = %{$_[0]};
808e2c54635SPaolo Bonzini    my ($parameter, $section);
809e2c54635SPaolo Bonzini    my $count;
810e2c54635SPaolo Bonzini
811e2c54635SPaolo Bonzini    print ".TH \"$args{'module'}\" 9 \"$args{'module'}\" \"$man_date\" \"API Manual\" LINUX\n";
812e2c54635SPaolo Bonzini
813e2c54635SPaolo Bonzini    foreach $section (@{$args{'sectionlist'}}) {
814e2c54635SPaolo Bonzini	print ".SH \"$section\"\n";
815e2c54635SPaolo Bonzini	output_highlight($args{'sections'}{$section});
816e2c54635SPaolo Bonzini    }
817e2c54635SPaolo Bonzini}
818e2c54635SPaolo Bonzini
819e2c54635SPaolo Bonzini##
820e2c54635SPaolo Bonzini# output in restructured text
821e2c54635SPaolo Bonzini#
822e2c54635SPaolo Bonzini
823e2c54635SPaolo Bonzini#
824e2c54635SPaolo Bonzini# This could use some work; it's used to output the DOC: sections, and
825e2c54635SPaolo Bonzini# starts by putting out the name of the doc section itself, but that tends
826e2c54635SPaolo Bonzini# to duplicate a header already in the template file.
827e2c54635SPaolo Bonzini#
828e2c54635SPaolo Bonzinisub output_blockhead_rst(%) {
829e2c54635SPaolo Bonzini    my %args = %{$_[0]};
830e2c54635SPaolo Bonzini    my ($parameter, $section);
831e2c54635SPaolo Bonzini
832e2c54635SPaolo Bonzini    foreach $section (@{$args{'sectionlist'}}) {
83378c8c92cSMauro Carvalho Chehab	next if (defined($nosymbol_table{$section}));
83478c8c92cSMauro Carvalho Chehab
835e2c54635SPaolo Bonzini	if ($output_selection != OUTPUT_INCLUDE) {
836e2c54635SPaolo Bonzini	    print "**$section**\n\n";
837e2c54635SPaolo Bonzini	}
838e2c54635SPaolo Bonzini        print_lineno($section_start_lines{$section});
839e2c54635SPaolo Bonzini	output_highlight_rst($args{'sections'}{$section});
840e2c54635SPaolo Bonzini	print "\n";
841e2c54635SPaolo Bonzini    }
842e2c54635SPaolo Bonzini}
843e2c54635SPaolo Bonzini
844e2c54635SPaolo Bonzini#
845e2c54635SPaolo Bonzini# Apply the RST highlights to a sub-block of text.
846e2c54635SPaolo Bonzini#
847e2c54635SPaolo Bonzinisub highlight_block($) {
848e2c54635SPaolo Bonzini    # The dohighlight kludge requires the text be called $contents
849e2c54635SPaolo Bonzini    my $contents = shift;
850e2c54635SPaolo Bonzini    eval $dohighlight;
851e2c54635SPaolo Bonzini    die $@ if $@;
852e2c54635SPaolo Bonzini    return $contents;
853e2c54635SPaolo Bonzini}
854e2c54635SPaolo Bonzini
855e2c54635SPaolo Bonzini#
856e2c54635SPaolo Bonzini# Regexes used only here.
857e2c54635SPaolo Bonzini#
858e2c54635SPaolo Bonzinimy $sphinx_literal = '^[^.].*::$';
859e2c54635SPaolo Bonzinimy $sphinx_cblock = '^\.\.\ +code-block::';
860e2c54635SPaolo Bonzini
861e2c54635SPaolo Bonzinisub output_highlight_rst {
862e2c54635SPaolo Bonzini    my $input = join "\n",@_;
863e2c54635SPaolo Bonzini    my $output = "";
864e2c54635SPaolo Bonzini    my $line;
865e2c54635SPaolo Bonzini    my $in_literal = 0;
866e2c54635SPaolo Bonzini    my $litprefix;
867e2c54635SPaolo Bonzini    my $block = "";
868e2c54635SPaolo Bonzini
869e2c54635SPaolo Bonzini    foreach $line (split "\n",$input) {
870e2c54635SPaolo Bonzini	#
871e2c54635SPaolo Bonzini	# If we're in a literal block, see if we should drop out
872e2c54635SPaolo Bonzini	# of it.  Otherwise pass the line straight through unmunged.
873e2c54635SPaolo Bonzini	#
874e2c54635SPaolo Bonzini	if ($in_literal) {
875e2c54635SPaolo Bonzini	    if (! ($line =~ /^\s*$/)) {
876e2c54635SPaolo Bonzini		#
877e2c54635SPaolo Bonzini		# If this is the first non-blank line in a literal
878e2c54635SPaolo Bonzini		# block we need to figure out what the proper indent is.
879e2c54635SPaolo Bonzini		#
880e2c54635SPaolo Bonzini		if ($litprefix eq "") {
881e2c54635SPaolo Bonzini		    $line =~ /^(\s*)/;
882e2c54635SPaolo Bonzini		    $litprefix = '^' . $1;
883e2c54635SPaolo Bonzini		    $output .= $line . "\n";
884e2c54635SPaolo Bonzini		} elsif (! ($line =~ /$litprefix/)) {
885e2c54635SPaolo Bonzini		    $in_literal = 0;
886e2c54635SPaolo Bonzini		} else {
887e2c54635SPaolo Bonzini		    $output .= $line . "\n";
888e2c54635SPaolo Bonzini		}
889e2c54635SPaolo Bonzini	    } else {
890e2c54635SPaolo Bonzini		$output .= $line . "\n";
891e2c54635SPaolo Bonzini	    }
892e2c54635SPaolo Bonzini	}
893e2c54635SPaolo Bonzini	#
894e2c54635SPaolo Bonzini	# Not in a literal block (or just dropped out)
895e2c54635SPaolo Bonzini	#
896e2c54635SPaolo Bonzini	if (! $in_literal) {
897e2c54635SPaolo Bonzini	    $block .= $line . "\n";
898e2c54635SPaolo Bonzini	    if (($line =~ /$sphinx_literal/) || ($line =~ /$sphinx_cblock/)) {
899e2c54635SPaolo Bonzini		$in_literal = 1;
900e2c54635SPaolo Bonzini		$litprefix = "";
901e2c54635SPaolo Bonzini		$output .= highlight_block($block);
902e2c54635SPaolo Bonzini		$block = ""
903e2c54635SPaolo Bonzini	    }
904e2c54635SPaolo Bonzini	}
905e2c54635SPaolo Bonzini    }
906e2c54635SPaolo Bonzini
907e2c54635SPaolo Bonzini    if ($block) {
908e2c54635SPaolo Bonzini	$output .= highlight_block($block);
909e2c54635SPaolo Bonzini    }
910e2c54635SPaolo Bonzini    foreach $line (split "\n", $output) {
911e2c54635SPaolo Bonzini	print $lineprefix . $line . "\n";
912e2c54635SPaolo Bonzini    }
913e2c54635SPaolo Bonzini}
914e2c54635SPaolo Bonzini
915e2c54635SPaolo Bonzinisub output_function_rst(%) {
916e2c54635SPaolo Bonzini    my %args = %{$_[0]};
917e2c54635SPaolo Bonzini    my ($parameter, $section);
918e2c54635SPaolo Bonzini    my $oldprefix = $lineprefix;
919e2c54635SPaolo Bonzini    my $start = "";
920e2c54635SPaolo Bonzini
9216d3a3cfcSMauro Carvalho Chehab    if ($sphinx_major < 3) {
92260ef7c18SMauro Carvalho Chehab	if ($args{'typedef'}) {
923e2c54635SPaolo Bonzini	    print ".. c:type:: ". $args{'function'} . "\n\n";
924e2c54635SPaolo Bonzini	    print_lineno($declaration_start_line);
925e2c54635SPaolo Bonzini	    print "   **Typedef**: ";
926e2c54635SPaolo Bonzini	    $lineprefix = "";
927e2c54635SPaolo Bonzini	    output_highlight_rst($args{'purpose'});
928e2c54635SPaolo Bonzini	    $start = "\n\n**Syntax**\n\n  ``";
929e2c54635SPaolo Bonzini	} else {
930e2c54635SPaolo Bonzini	    print ".. c:function:: ";
931e2c54635SPaolo Bonzini	}
93260ef7c18SMauro Carvalho Chehab    } else {
93360ef7c18SMauro Carvalho Chehab	print ".. c:macro:: ". $args{'function'} . "\n\n";
93460ef7c18SMauro Carvalho Chehab
93560ef7c18SMauro Carvalho Chehab	if ($args{'typedef'}) {
93660ef7c18SMauro Carvalho Chehab	    print_lineno($declaration_start_line);
93760ef7c18SMauro Carvalho Chehab	    print "   **Typedef**: ";
93860ef7c18SMauro Carvalho Chehab	    $lineprefix = "";
93960ef7c18SMauro Carvalho Chehab	    output_highlight_rst($args{'purpose'});
94060ef7c18SMauro Carvalho Chehab	    $start = "\n\n**Syntax**\n\n  ``";
94160ef7c18SMauro Carvalho Chehab	} else {
94260ef7c18SMauro Carvalho Chehab	    print "``";
94360ef7c18SMauro Carvalho Chehab	}
94460ef7c18SMauro Carvalho Chehab    }
945e2c54635SPaolo Bonzini    if ($args{'functiontype'} ne "") {
946e2c54635SPaolo Bonzini	$start .= $args{'functiontype'} . " " . $args{'function'} . " (";
947e2c54635SPaolo Bonzini    } else {
948e2c54635SPaolo Bonzini	$start .= $args{'function'} . " (";
949e2c54635SPaolo Bonzini    }
950e2c54635SPaolo Bonzini    print $start;
951e2c54635SPaolo Bonzini
952e2c54635SPaolo Bonzini    my $count = 0;
953e2c54635SPaolo Bonzini    foreach my $parameter (@{$args{'parameterlist'}}) {
954e2c54635SPaolo Bonzini	if ($count ne 0) {
955e2c54635SPaolo Bonzini	    print ", ";
956e2c54635SPaolo Bonzini	}
957e2c54635SPaolo Bonzini	$count++;
958e2c54635SPaolo Bonzini	$type = $args{'parametertypes'}{$parameter};
959e2c54635SPaolo Bonzini
960e2c54635SPaolo Bonzini	if ($type =~ m/([^\(]*\(\*)\s*\)\s*\(([^\)]*)\)/) {
961e2c54635SPaolo Bonzini	    # pointer-to-function
962a62d5637SPeter Maydell	    print $1 . $parameter . ") (" . $2 . ")";
963e2c54635SPaolo Bonzini	} else {
9640c771852SMauro Carvalho Chehab	    print $type;
965e2c54635SPaolo Bonzini	}
966e2c54635SPaolo Bonzini    }
967e2c54635SPaolo Bonzini    if ($args{'typedef'}) {
968e2c54635SPaolo Bonzini	print ");``\n\n";
969e2c54635SPaolo Bonzini    } else {
97060ef7c18SMauro Carvalho Chehab	if ($sphinx_major < 3) {
971e2c54635SPaolo Bonzini	    print ")\n\n";
97260ef7c18SMauro Carvalho Chehab	} else {
97360ef7c18SMauro Carvalho Chehab	    print ")``\n";
97460ef7c18SMauro Carvalho Chehab	}
975e2c54635SPaolo Bonzini	print_lineno($declaration_start_line);
976e2c54635SPaolo Bonzini	$lineprefix = "   ";
977e2c54635SPaolo Bonzini	output_highlight_rst($args{'purpose'});
978e2c54635SPaolo Bonzini	print "\n";
979e2c54635SPaolo Bonzini    }
980e2c54635SPaolo Bonzini
981e2c54635SPaolo Bonzini    print "**Parameters**\n\n";
982e2c54635SPaolo Bonzini    $lineprefix = "  ";
983e2c54635SPaolo Bonzini    foreach $parameter (@{$args{'parameterlist'}}) {
984e2c54635SPaolo Bonzini	my $parameter_name = $parameter;
985e2c54635SPaolo Bonzini	$parameter_name =~ s/\[.*//;
986e2c54635SPaolo Bonzini	$type = $args{'parametertypes'}{$parameter};
987e2c54635SPaolo Bonzini
988e2c54635SPaolo Bonzini	if ($type ne "") {
9890c771852SMauro Carvalho Chehab	    print "``$type``\n";
990e2c54635SPaolo Bonzini	} else {
991e2c54635SPaolo Bonzini	    print "``$parameter``\n";
992e2c54635SPaolo Bonzini	}
993e2c54635SPaolo Bonzini
994e2c54635SPaolo Bonzini        print_lineno($parameterdesc_start_lines{$parameter_name});
995e2c54635SPaolo Bonzini
996e2c54635SPaolo Bonzini	if (defined($args{'parameterdescs'}{$parameter_name}) &&
997e2c54635SPaolo Bonzini	    $args{'parameterdescs'}{$parameter_name} ne $undescribed) {
998e2c54635SPaolo Bonzini	    output_highlight_rst($args{'parameterdescs'}{$parameter_name});
999e2c54635SPaolo Bonzini	} else {
1000e2c54635SPaolo Bonzini	    print "  *undescribed*\n";
1001e2c54635SPaolo Bonzini	}
1002e2c54635SPaolo Bonzini	print "\n";
1003e2c54635SPaolo Bonzini    }
1004e2c54635SPaolo Bonzini
1005e2c54635SPaolo Bonzini    $lineprefix = $oldprefix;
1006e2c54635SPaolo Bonzini    output_section_rst(@_);
1007e2c54635SPaolo Bonzini}
1008e2c54635SPaolo Bonzini
1009e2c54635SPaolo Bonzinisub output_section_rst(%) {
1010e2c54635SPaolo Bonzini    my %args = %{$_[0]};
1011e2c54635SPaolo Bonzini    my $section;
1012e2c54635SPaolo Bonzini    my $oldprefix = $lineprefix;
1013e2c54635SPaolo Bonzini    $lineprefix = "";
1014e2c54635SPaolo Bonzini
1015e2c54635SPaolo Bonzini    foreach $section (@{$args{'sectionlist'}}) {
1016e2c54635SPaolo Bonzini	print "**$section**\n\n";
1017e2c54635SPaolo Bonzini        print_lineno($section_start_lines{$section});
1018e2c54635SPaolo Bonzini	output_highlight_rst($args{'sections'}{$section});
1019e2c54635SPaolo Bonzini	print "\n";
1020e2c54635SPaolo Bonzini    }
1021e2c54635SPaolo Bonzini    print "\n";
1022e2c54635SPaolo Bonzini    $lineprefix = $oldprefix;
1023e2c54635SPaolo Bonzini}
1024e2c54635SPaolo Bonzini
1025e2c54635SPaolo Bonzinisub output_enum_rst(%) {
1026e2c54635SPaolo Bonzini    my %args = %{$_[0]};
1027e2c54635SPaolo Bonzini    my ($parameter);
1028e2c54635SPaolo Bonzini    my $oldprefix = $lineprefix;
1029e2c54635SPaolo Bonzini    my $count;
1030e2c54635SPaolo Bonzini
10316d3a3cfcSMauro Carvalho Chehab    if ($sphinx_major < 3) {
10326d3a3cfcSMauro Carvalho Chehab	my $name = "enum " . $args{'enum'};
1033e2c54635SPaolo Bonzini	print "\n\n.. c:type:: " . $name . "\n\n";
10346d3a3cfcSMauro Carvalho Chehab    } else {
10356d3a3cfcSMauro Carvalho Chehab	my $name = $args{'enum'};
10366d3a3cfcSMauro Carvalho Chehab	print "\n\n.. c:enum:: " . $name . "\n\n";
10376d3a3cfcSMauro Carvalho Chehab    }
1038e2c54635SPaolo Bonzini    print_lineno($declaration_start_line);
1039e2c54635SPaolo Bonzini    $lineprefix = "   ";
1040e2c54635SPaolo Bonzini    output_highlight_rst($args{'purpose'});
1041e2c54635SPaolo Bonzini    print "\n";
1042e2c54635SPaolo Bonzini
1043e2c54635SPaolo Bonzini    print "**Constants**\n\n";
1044e2c54635SPaolo Bonzini    $lineprefix = "  ";
1045e2c54635SPaolo Bonzini    foreach $parameter (@{$args{'parameterlist'}}) {
1046e2c54635SPaolo Bonzini	print "``$parameter``\n";
1047e2c54635SPaolo Bonzini	if ($args{'parameterdescs'}{$parameter} ne $undescribed) {
1048e2c54635SPaolo Bonzini	    output_highlight_rst($args{'parameterdescs'}{$parameter});
1049e2c54635SPaolo Bonzini	} else {
1050e2c54635SPaolo Bonzini	    print "  *undescribed*\n";
1051e2c54635SPaolo Bonzini	}
1052e2c54635SPaolo Bonzini	print "\n";
1053e2c54635SPaolo Bonzini    }
1054e2c54635SPaolo Bonzini
1055e2c54635SPaolo Bonzini    $lineprefix = $oldprefix;
1056e2c54635SPaolo Bonzini    output_section_rst(@_);
1057e2c54635SPaolo Bonzini}
1058e2c54635SPaolo Bonzini
1059e2c54635SPaolo Bonzinisub output_typedef_rst(%) {
1060e2c54635SPaolo Bonzini    my %args = %{$_[0]};
1061e2c54635SPaolo Bonzini    my ($parameter);
1062e2c54635SPaolo Bonzini    my $oldprefix = $lineprefix;
10636d3a3cfcSMauro Carvalho Chehab    my $name;
1064e2c54635SPaolo Bonzini
10656d3a3cfcSMauro Carvalho Chehab    if ($sphinx_major < 3) {
10666d3a3cfcSMauro Carvalho Chehab	$name = "typedef " . $args{'typedef'};
10676d3a3cfcSMauro Carvalho Chehab    } else {
10686d3a3cfcSMauro Carvalho Chehab	$name = $args{'typedef'};
10696d3a3cfcSMauro Carvalho Chehab    }
1070e2c54635SPaolo Bonzini    print "\n\n.. c:type:: " . $name . "\n\n";
1071e2c54635SPaolo Bonzini    print_lineno($declaration_start_line);
1072e2c54635SPaolo Bonzini    $lineprefix = "   ";
1073e2c54635SPaolo Bonzini    output_highlight_rst($args{'purpose'});
1074e2c54635SPaolo Bonzini    print "\n";
1075e2c54635SPaolo Bonzini
1076e2c54635SPaolo Bonzini    $lineprefix = $oldprefix;
1077e2c54635SPaolo Bonzini    output_section_rst(@_);
1078e2c54635SPaolo Bonzini}
1079e2c54635SPaolo Bonzini
1080e2c54635SPaolo Bonzinisub output_struct_rst(%) {
1081e2c54635SPaolo Bonzini    my %args = %{$_[0]};
1082e2c54635SPaolo Bonzini    my ($parameter);
1083e2c54635SPaolo Bonzini    my $oldprefix = $lineprefix;
1084e2c54635SPaolo Bonzini
10856d3a3cfcSMauro Carvalho Chehab    if ($sphinx_major < 3) {
10866d3a3cfcSMauro Carvalho Chehab	my $name = $args{'type'} . " " . $args{'struct'};
1087e2c54635SPaolo Bonzini	print "\n\n.. c:type:: " . $name . "\n\n";
10886d3a3cfcSMauro Carvalho Chehab    } else {
10896d3a3cfcSMauro Carvalho Chehab	my $name = $args{'struct'};
10906d3a3cfcSMauro Carvalho Chehab	print "\n\n.. c:struct:: " . $name . "\n\n";
10916d3a3cfcSMauro Carvalho Chehab    }
1092e2c54635SPaolo Bonzini    print_lineno($declaration_start_line);
1093e2c54635SPaolo Bonzini    $lineprefix = "   ";
1094e2c54635SPaolo Bonzini    output_highlight_rst($args{'purpose'});
1095e2c54635SPaolo Bonzini    print "\n";
1096e2c54635SPaolo Bonzini
1097e2c54635SPaolo Bonzini    print "**Definition**\n\n";
1098e2c54635SPaolo Bonzini    print "::\n\n";
1099e2c54635SPaolo Bonzini    my $declaration = $args{'definition'};
1100e2c54635SPaolo Bonzini    $declaration =~ s/\t/  /g;
1101e2c54635SPaolo Bonzini    print "  " . $args{'type'} . " " . $args{'struct'} . " {\n$declaration  };\n\n";
1102e2c54635SPaolo Bonzini
1103e2c54635SPaolo Bonzini    print "**Members**\n\n";
1104e2c54635SPaolo Bonzini    $lineprefix = "  ";
1105e2c54635SPaolo Bonzini    foreach $parameter (@{$args{'parameterlist'}}) {
1106e2c54635SPaolo Bonzini	($parameter =~ /^#/) && next;
1107e2c54635SPaolo Bonzini
1108e2c54635SPaolo Bonzini	my $parameter_name = $parameter;
1109e2c54635SPaolo Bonzini	$parameter_name =~ s/\[.*//;
1110e2c54635SPaolo Bonzini
1111e2c54635SPaolo Bonzini	($args{'parameterdescs'}{$parameter_name} ne $undescribed) || next;
1112e2c54635SPaolo Bonzini	$type = $args{'parametertypes'}{$parameter};
1113e2c54635SPaolo Bonzini        print_lineno($parameterdesc_start_lines{$parameter_name});
1114e2c54635SPaolo Bonzini	print "``" . $parameter . "``\n";
1115e2c54635SPaolo Bonzini	output_highlight_rst($args{'parameterdescs'}{$parameter_name});
1116e2c54635SPaolo Bonzini	print "\n";
1117e2c54635SPaolo Bonzini    }
1118e2c54635SPaolo Bonzini    print "\n";
1119e2c54635SPaolo Bonzini
1120e2c54635SPaolo Bonzini    $lineprefix = $oldprefix;
1121e2c54635SPaolo Bonzini    output_section_rst(@_);
1122e2c54635SPaolo Bonzini}
1123e2c54635SPaolo Bonzini
1124e2c54635SPaolo Bonzini## none mode output functions
1125e2c54635SPaolo Bonzini
1126e2c54635SPaolo Bonzinisub output_function_none(%) {
1127e2c54635SPaolo Bonzini}
1128e2c54635SPaolo Bonzini
1129e2c54635SPaolo Bonzinisub output_enum_none(%) {
1130e2c54635SPaolo Bonzini}
1131e2c54635SPaolo Bonzini
1132e2c54635SPaolo Bonzinisub output_typedef_none(%) {
1133e2c54635SPaolo Bonzini}
1134e2c54635SPaolo Bonzini
1135e2c54635SPaolo Bonzinisub output_struct_none(%) {
1136e2c54635SPaolo Bonzini}
1137e2c54635SPaolo Bonzini
1138e2c54635SPaolo Bonzinisub output_blockhead_none(%) {
1139e2c54635SPaolo Bonzini}
1140e2c54635SPaolo Bonzini
1141e2c54635SPaolo Bonzini##
1142e2c54635SPaolo Bonzini# generic output function for all types (function, struct/union, typedef, enum);
1143e2c54635SPaolo Bonzini# calls the generated, variable output_ function name based on
1144e2c54635SPaolo Bonzini# functype and output_mode
1145e2c54635SPaolo Bonzinisub output_declaration {
1146e2c54635SPaolo Bonzini    no strict 'refs';
1147e2c54635SPaolo Bonzini    my $name = shift;
1148e2c54635SPaolo Bonzini    my $functype = shift;
1149e2c54635SPaolo Bonzini    my $func = "output_${functype}_$output_mode";
115078c8c92cSMauro Carvalho Chehab
115178c8c92cSMauro Carvalho Chehab    return if (defined($nosymbol_table{$name}));
115278c8c92cSMauro Carvalho Chehab
1153e2c54635SPaolo Bonzini    if (($output_selection == OUTPUT_ALL) ||
1154e2c54635SPaolo Bonzini	(($output_selection == OUTPUT_INCLUDE ||
1155e2c54635SPaolo Bonzini	  $output_selection == OUTPUT_EXPORTED) &&
1156e2c54635SPaolo Bonzini	 defined($function_table{$name})) ||
115778c8c92cSMauro Carvalho Chehab	($output_selection == OUTPUT_INTERNAL &&
1158e2c54635SPaolo Bonzini	 !($functype eq "function" && defined($function_table{$name}))))
1159e2c54635SPaolo Bonzini    {
1160e2c54635SPaolo Bonzini	&$func(@_);
1161e2c54635SPaolo Bonzini	$section_counter++;
1162e2c54635SPaolo Bonzini    }
1163e2c54635SPaolo Bonzini}
1164e2c54635SPaolo Bonzini
1165e2c54635SPaolo Bonzini##
1166e2c54635SPaolo Bonzini# generic output function - calls the right one based on current output mode.
1167e2c54635SPaolo Bonzinisub output_blockhead {
1168e2c54635SPaolo Bonzini    no strict 'refs';
1169e2c54635SPaolo Bonzini    my $func = "output_blockhead_" . $output_mode;
1170e2c54635SPaolo Bonzini    &$func(@_);
1171e2c54635SPaolo Bonzini    $section_counter++;
1172e2c54635SPaolo Bonzini}
1173e2c54635SPaolo Bonzini
1174e2c54635SPaolo Bonzini##
1175e2c54635SPaolo Bonzini# takes a declaration (struct, union, enum, typedef) and
1176e2c54635SPaolo Bonzini# invokes the right handler. NOT called for functions.
1177e2c54635SPaolo Bonzinisub dump_declaration($$) {
1178e2c54635SPaolo Bonzini    no strict 'refs';
1179e2c54635SPaolo Bonzini    my ($prototype, $file) = @_;
1180e2c54635SPaolo Bonzini    my $func = "dump_" . $decl_type;
1181e2c54635SPaolo Bonzini    &$func(@_);
1182e2c54635SPaolo Bonzini}
1183e2c54635SPaolo Bonzini
1184e2c54635SPaolo Bonzinisub dump_union($$) {
1185e2c54635SPaolo Bonzini    dump_struct(@_);
1186e2c54635SPaolo Bonzini}
1187e2c54635SPaolo Bonzini
1188e2c54635SPaolo Bonzinisub dump_struct($$) {
1189e2c54635SPaolo Bonzini    my $x = shift;
1190e2c54635SPaolo Bonzini    my $file = shift;
1191e2c54635SPaolo Bonzini
11925c51f435SJonathan Cameron    if ($x =~ /(struct|union)\s+(\w+)\s*\{(.*)\}(\s*(__packed|__aligned|____cacheline_aligned_in_smp|____cacheline_aligned|__attribute__\s*\(\([a-z0-9,_\s\(\)]*\)\)))*/) {
1193e2c54635SPaolo Bonzini	my $decl_type = $1;
1194e2c54635SPaolo Bonzini	$declaration_name = $2;
1195e2c54635SPaolo Bonzini	my $members = $3;
1196e2c54635SPaolo Bonzini
1197e2c54635SPaolo Bonzini	# ignore members marked private:
1198e2c54635SPaolo Bonzini	$members =~ s/\/\*\s*private:.*?\/\*\s*public:.*?\*\///gosi;
1199e2c54635SPaolo Bonzini	$members =~ s/\/\*\s*private:.*//gosi;
1200e2c54635SPaolo Bonzini	# strip comments:
1201e2c54635SPaolo Bonzini	$members =~ s/\/\*.*?\*\///gos;
1202e2c54635SPaolo Bonzini	# strip attributes
1203e2c54635SPaolo Bonzini	$members =~ s/\s*__attribute__\s*\(\([a-z0-9,_\*\s\(\)]*\)\)/ /gi;
1204e2c54635SPaolo Bonzini	$members =~ s/\s*__aligned\s*\([^;]*\)/ /gos;
1205e2c54635SPaolo Bonzini	$members =~ s/\s*__packed\s*/ /gos;
1206e2c54635SPaolo Bonzini	$members =~ s/\s*CRYPTO_MINALIGN_ATTR/ /gos;
1207b5a8dfb5SAndré Almeida	$members =~ s/\s*____cacheline_aligned_in_smp/ /gos;
12085c51f435SJonathan Cameron	$members =~ s/\s*____cacheline_aligned/ /gos;
1209f0fd307dSMauro Carvalho Chehab
1210e2c54635SPaolo Bonzini	# replace DECLARE_BITMAP
1211f0fd307dSMauro Carvalho Chehab	$members =~ s/__ETHTOOL_DECLARE_LINK_MODE_MASK\s*\(([^\)]+)\)/DECLARE_BITMAP($1, __ETHTOOL_LINK_MODE_MASK_NBITS)/gos;
1212e2c54635SPaolo Bonzini	$members =~ s/DECLARE_BITMAP\s*\(([^,)]+),\s*([^,)]+)\)/unsigned long $1\[BITS_TO_LONGS($2)\]/gos;
1213e2c54635SPaolo Bonzini	# replace DECLARE_HASHTABLE
1214e2c54635SPaolo Bonzini	$members =~ s/DECLARE_HASHTABLE\s*\(([^,)]+),\s*([^,)]+)\)/unsigned long $1\[1 << (($2) - 1)\]/gos;
1215e2c54635SPaolo Bonzini	# replace DECLARE_KFIFO
1216e2c54635SPaolo Bonzini	$members =~ s/DECLARE_KFIFO\s*\(([^,)]+),\s*([^,)]+),\s*([^,)]+)\)/$2 \*$1/gos;
1217e2c54635SPaolo Bonzini	# replace DECLARE_KFIFO_PTR
1218e2c54635SPaolo Bonzini	$members =~ s/DECLARE_KFIFO_PTR\s*\(([^,)]+),\s*([^,)]+)\)/$2 \*$1/gos;
1219e2c54635SPaolo Bonzini
1220e2c54635SPaolo Bonzini	my $declaration = $members;
1221e2c54635SPaolo Bonzini
1222e2c54635SPaolo Bonzini	# Split nested struct/union elements as newer ones
1223e2c54635SPaolo Bonzini	while ($members =~ m/(struct|union)([^\{\};]+)\{([^\{\}]*)\}([^\{\}\;]*)\;/) {
1224e2c54635SPaolo Bonzini		my $newmember;
1225e2c54635SPaolo Bonzini		my $maintype = $1;
1226e2c54635SPaolo Bonzini		my $ids = $4;
1227e2c54635SPaolo Bonzini		my $content = $3;
1228e2c54635SPaolo Bonzini		foreach my $id(split /,/, $ids) {
1229e2c54635SPaolo Bonzini			$newmember .= "$maintype $id; ";
1230e2c54635SPaolo Bonzini
1231e2c54635SPaolo Bonzini			$id =~ s/[:\[].*//;
1232e2c54635SPaolo Bonzini			$id =~ s/^\s*\**(\S+)\s*/$1/;
1233e2c54635SPaolo Bonzini			foreach my $arg (split /;/, $content) {
1234e2c54635SPaolo Bonzini				next if ($arg =~ m/^\s*$/);
1235e2c54635SPaolo Bonzini				if ($arg =~ m/^([^\(]+\(\*?\s*)([\w\.]*)(\s*\).*)/) {
1236e2c54635SPaolo Bonzini					# pointer-to-function
1237e2c54635SPaolo Bonzini					my $type = $1;
1238e2c54635SPaolo Bonzini					my $name = $2;
1239e2c54635SPaolo Bonzini					my $extra = $3;
1240e2c54635SPaolo Bonzini					next if (!$name);
1241e2c54635SPaolo Bonzini					if ($id =~ m/^\s*$/) {
1242e2c54635SPaolo Bonzini						# anonymous struct/union
1243e2c54635SPaolo Bonzini						$newmember .= "$type$name$extra; ";
1244e2c54635SPaolo Bonzini					} else {
1245e2c54635SPaolo Bonzini						$newmember .= "$type$id.$name$extra; ";
1246e2c54635SPaolo Bonzini					}
1247e2c54635SPaolo Bonzini				} else {
1248e2c54635SPaolo Bonzini					my $type;
1249e2c54635SPaolo Bonzini					my $names;
1250e2c54635SPaolo Bonzini					$arg =~ s/^\s+//;
1251e2c54635SPaolo Bonzini					$arg =~ s/\s+$//;
1252e2c54635SPaolo Bonzini					# Handle bitmaps
1253e2c54635SPaolo Bonzini					$arg =~ s/:\s*\d+\s*//g;
1254e2c54635SPaolo Bonzini					# Handle arrays
1255e2c54635SPaolo Bonzini					$arg =~ s/\[.*\]//g;
1256e2c54635SPaolo Bonzini					# The type may have multiple words,
1257e2c54635SPaolo Bonzini					# and multiple IDs can be defined, like:
1258e2c54635SPaolo Bonzini					#	const struct foo, *bar, foobar
1259e2c54635SPaolo Bonzini					# So, we remove spaces when parsing the
1260e2c54635SPaolo Bonzini					# names, in order to match just names
1261e2c54635SPaolo Bonzini					# and commas for the names
1262e2c54635SPaolo Bonzini					$arg =~ s/\s*,\s*/,/g;
1263e2c54635SPaolo Bonzini					if ($arg =~ m/(.*)\s+([\S+,]+)/) {
1264e2c54635SPaolo Bonzini						$type = $1;
1265e2c54635SPaolo Bonzini						$names = $2;
1266e2c54635SPaolo Bonzini					} else {
1267e2c54635SPaolo Bonzini						$newmember .= "$arg; ";
1268e2c54635SPaolo Bonzini						next;
1269e2c54635SPaolo Bonzini					}
1270e2c54635SPaolo Bonzini					foreach my $name (split /,/, $names) {
1271e2c54635SPaolo Bonzini						$name =~ s/^\s*\**(\S+)\s*/$1/;
1272e2c54635SPaolo Bonzini						next if (($name =~ m/^\s*$/));
1273e2c54635SPaolo Bonzini						if ($id =~ m/^\s*$/) {
1274e2c54635SPaolo Bonzini							# anonymous struct/union
1275e2c54635SPaolo Bonzini							$newmember .= "$type $name; ";
1276e2c54635SPaolo Bonzini						} else {
1277e2c54635SPaolo Bonzini							$newmember .= "$type $id.$name; ";
1278e2c54635SPaolo Bonzini						}
1279e2c54635SPaolo Bonzini					}
1280e2c54635SPaolo Bonzini				}
1281e2c54635SPaolo Bonzini			}
1282e2c54635SPaolo Bonzini		}
1283e2c54635SPaolo Bonzini		$members =~ s/(struct|union)([^\{\};]+)\{([^\{\}]*)\}([^\{\}\;]*)\;/$newmember/;
1284e2c54635SPaolo Bonzini	}
1285e2c54635SPaolo Bonzini
1286e2c54635SPaolo Bonzini	# Ignore other nested elements, like enums
1287e2c54635SPaolo Bonzini	$members =~ s/(\{[^\{\}]*\})//g;
1288e2c54635SPaolo Bonzini
1289e2c54635SPaolo Bonzini	create_parameterlist($members, ';', $file, $declaration_name);
1290e2c54635SPaolo Bonzini	check_sections($file, $declaration_name, $decl_type, $sectcheck, $struct_actual);
1291e2c54635SPaolo Bonzini
1292e2c54635SPaolo Bonzini	# Adjust declaration for better display
1293e2c54635SPaolo Bonzini	$declaration =~ s/([\{;])/$1\n/g;
1294e2c54635SPaolo Bonzini	$declaration =~ s/\}\s+;/};/g;
1295e2c54635SPaolo Bonzini	# Better handle inlined enums
1296e2c54635SPaolo Bonzini	do {} while ($declaration =~ s/(enum\s+\{[^\}]+),([^\n])/$1,\n$2/);
1297e2c54635SPaolo Bonzini
1298e2c54635SPaolo Bonzini	my @def_args = split /\n/, $declaration;
1299e2c54635SPaolo Bonzini	my $level = 1;
1300e2c54635SPaolo Bonzini	$declaration = "";
1301e2c54635SPaolo Bonzini	foreach my $clause (@def_args) {
1302e2c54635SPaolo Bonzini		$clause =~ s/^\s+//;
1303e2c54635SPaolo Bonzini		$clause =~ s/\s+$//;
1304e2c54635SPaolo Bonzini		$clause =~ s/\s+/ /;
1305e2c54635SPaolo Bonzini		next if (!$clause);
1306e2c54635SPaolo Bonzini		$level-- if ($clause =~ m/(\})/ && $level > 1);
1307e2c54635SPaolo Bonzini		if (!($clause =~ m/^\s*#/)) {
1308e2c54635SPaolo Bonzini			$declaration .= "\t" x $level;
1309e2c54635SPaolo Bonzini		}
1310e2c54635SPaolo Bonzini		$declaration .= "\t" . $clause . "\n";
1311e2c54635SPaolo Bonzini		$level++ if ($clause =~ m/(\{)/ && !($clause =~m/\}/));
1312e2c54635SPaolo Bonzini	}
1313e2c54635SPaolo Bonzini	output_declaration($declaration_name,
1314e2c54635SPaolo Bonzini			   'struct',
1315e2c54635SPaolo Bonzini			   {'struct' => $declaration_name,
1316e2c54635SPaolo Bonzini			    'module' => $modulename,
1317e2c54635SPaolo Bonzini			    'definition' => $declaration,
1318e2c54635SPaolo Bonzini			    'parameterlist' => \@parameterlist,
1319e2c54635SPaolo Bonzini			    'parameterdescs' => \%parameterdescs,
1320e2c54635SPaolo Bonzini			    'parametertypes' => \%parametertypes,
1321e2c54635SPaolo Bonzini			    'sectionlist' => \@sectionlist,
1322e2c54635SPaolo Bonzini			    'sections' => \%sections,
1323e2c54635SPaolo Bonzini			    'purpose' => $declaration_purpose,
1324e2c54635SPaolo Bonzini			    'type' => $decl_type
1325e2c54635SPaolo Bonzini			   });
1326e2c54635SPaolo Bonzini    }
1327e2c54635SPaolo Bonzini    else {
1328e2c54635SPaolo Bonzini	print STDERR "${file}:$.: error: Cannot parse struct or union!\n";
1329e2c54635SPaolo Bonzini	++$errors;
1330e2c54635SPaolo Bonzini    }
1331e2c54635SPaolo Bonzini}
1332e2c54635SPaolo Bonzini
1333e2c54635SPaolo Bonzini
1334e2c54635SPaolo Bonzinisub show_warnings($$) {
1335e2c54635SPaolo Bonzini	my $functype = shift;
1336e2c54635SPaolo Bonzini	my $name = shift;
1337e2c54635SPaolo Bonzini
133878c8c92cSMauro Carvalho Chehab	return 0 if (defined($nosymbol_table{$name}));
133978c8c92cSMauro Carvalho Chehab
1340e2c54635SPaolo Bonzini	return 1 if ($output_selection == OUTPUT_ALL);
1341e2c54635SPaolo Bonzini
1342e2c54635SPaolo Bonzini	if ($output_selection == OUTPUT_EXPORTED) {
1343e2c54635SPaolo Bonzini		if (defined($function_table{$name})) {
1344e2c54635SPaolo Bonzini			return 1;
1345e2c54635SPaolo Bonzini		} else {
1346e2c54635SPaolo Bonzini			return 0;
1347e2c54635SPaolo Bonzini		}
1348e2c54635SPaolo Bonzini	}
1349e2c54635SPaolo Bonzini        if ($output_selection == OUTPUT_INTERNAL) {
1350e2c54635SPaolo Bonzini		if (!($functype eq "function" && defined($function_table{$name}))) {
1351e2c54635SPaolo Bonzini			return 1;
1352e2c54635SPaolo Bonzini		} else {
1353e2c54635SPaolo Bonzini			return 0;
1354e2c54635SPaolo Bonzini		}
1355e2c54635SPaolo Bonzini	}
1356e2c54635SPaolo Bonzini	if ($output_selection == OUTPUT_INCLUDE) {
1357e2c54635SPaolo Bonzini		if (defined($function_table{$name})) {
1358e2c54635SPaolo Bonzini			return 1;
1359e2c54635SPaolo Bonzini		} else {
1360e2c54635SPaolo Bonzini			return 0;
1361e2c54635SPaolo Bonzini		}
1362e2c54635SPaolo Bonzini	}
1363e2c54635SPaolo Bonzini	die("Please add the new output type at show_warnings()");
1364e2c54635SPaolo Bonzini}
1365e2c54635SPaolo Bonzini
1366e2c54635SPaolo Bonzinisub dump_enum($$) {
1367e2c54635SPaolo Bonzini    my $x = shift;
1368e2c54635SPaolo Bonzini    my $file = shift;
1369306b015cSMauro Carvalho Chehab    my $members;
1370306b015cSMauro Carvalho Chehab
1371e2c54635SPaolo Bonzini
1372e2c54635SPaolo Bonzini    $x =~ s@/\*.*?\*/@@gos;	# strip comments.
1373e2c54635SPaolo Bonzini    # strip #define macros inside enums
1374e2c54635SPaolo Bonzini    $x =~ s@#\s*((define|ifdef)\s+|endif)[^;]*;@@gos;
1375e2c54635SPaolo Bonzini
1376306b015cSMauro Carvalho Chehab    if ($x =~ /typedef\s+enum\s*\{(.*)\}\s*(\w*)\s*;/) {
1377306b015cSMauro Carvalho Chehab	$declaration_name = $2;
1378306b015cSMauro Carvalho Chehab	$members = $1;
1379306b015cSMauro Carvalho Chehab    } elsif ($x =~ /enum\s+(\w*)\s*\{(.*)\}/) {
1380e2c54635SPaolo Bonzini	$declaration_name = $1;
1381306b015cSMauro Carvalho Chehab	$members = $2;
1382306b015cSMauro Carvalho Chehab    }
1383306b015cSMauro Carvalho Chehab
1384306b015cSMauro Carvalho Chehab    if ($declaration_name) {
1385e2c54635SPaolo Bonzini	my %_members;
1386e2c54635SPaolo Bonzini
1387e2c54635SPaolo Bonzini	$members =~ s/\s+$//;
1388e2c54635SPaolo Bonzini
1389e2c54635SPaolo Bonzini	foreach my $arg (split ',', $members) {
1390e2c54635SPaolo Bonzini	    $arg =~ s/^\s*(\w+).*/$1/;
1391e2c54635SPaolo Bonzini	    push @parameterlist, $arg;
1392e2c54635SPaolo Bonzini	    if (!$parameterdescs{$arg}) {
1393e2c54635SPaolo Bonzini		$parameterdescs{$arg} = $undescribed;
1394e2c54635SPaolo Bonzini	        if (show_warnings("enum", $declaration_name)) {
1395e2c54635SPaolo Bonzini			print STDERR "${file}:$.: warning: Enum value '$arg' not described in enum '$declaration_name'\n";
1396e2c54635SPaolo Bonzini		}
1397e2c54635SPaolo Bonzini	    }
1398e2c54635SPaolo Bonzini	    $_members{$arg} = 1;
1399e2c54635SPaolo Bonzini	}
1400e2c54635SPaolo Bonzini
1401e2c54635SPaolo Bonzini	while (my ($k, $v) = each %parameterdescs) {
1402e2c54635SPaolo Bonzini	    if (!exists($_members{$k})) {
1403e2c54635SPaolo Bonzini	        if (show_warnings("enum", $declaration_name)) {
1404e2c54635SPaolo Bonzini		     print STDERR "${file}:$.: warning: Excess enum value '$k' description in '$declaration_name'\n";
1405e2c54635SPaolo Bonzini		}
1406e2c54635SPaolo Bonzini	    }
1407e2c54635SPaolo Bonzini        }
1408e2c54635SPaolo Bonzini
1409e2c54635SPaolo Bonzini	output_declaration($declaration_name,
1410e2c54635SPaolo Bonzini			   'enum',
1411e2c54635SPaolo Bonzini			   {'enum' => $declaration_name,
1412e2c54635SPaolo Bonzini			    'module' => $modulename,
1413e2c54635SPaolo Bonzini			    'parameterlist' => \@parameterlist,
1414e2c54635SPaolo Bonzini			    'parameterdescs' => \%parameterdescs,
1415e2c54635SPaolo Bonzini			    'sectionlist' => \@sectionlist,
1416e2c54635SPaolo Bonzini			    'sections' => \%sections,
1417e2c54635SPaolo Bonzini			    'purpose' => $declaration_purpose
1418e2c54635SPaolo Bonzini			   });
1419306b015cSMauro Carvalho Chehab    } else {
1420e2c54635SPaolo Bonzini	print STDERR "${file}:$.: error: Cannot parse enum!\n";
1421e2c54635SPaolo Bonzini	++$errors;
1422e2c54635SPaolo Bonzini    }
1423e2c54635SPaolo Bonzini}
1424e2c54635SPaolo Bonzini
1425e2c54635SPaolo Bonzinisub dump_typedef($$) {
1426e2c54635SPaolo Bonzini    my $x = shift;
1427e2c54635SPaolo Bonzini    my $file = shift;
1428e2c54635SPaolo Bonzini
1429e2c54635SPaolo Bonzini    $x =~ s@/\*.*?\*/@@gos;	# strip comments.
1430e2c54635SPaolo Bonzini
1431e2c54635SPaolo Bonzini    # Parse function prototypes
14323cd3c519SEduardo Habkost    if ($x =~ /typedef\s+(\w+\s*\**)\s*\(\*?\s*(\w\S+)\s*\)\s*\((.*)\);/ ||
143319ab6044SEduardo Habkost	$x =~ /typedef\s+(\w+\s*\**)\s*(\w\S+)\s*\s*\((.*)\);/) {
1434e2c54635SPaolo Bonzini
1435e2c54635SPaolo Bonzini	# Function typedefs
1436e2c54635SPaolo Bonzini	$return_type = $1;
1437e2c54635SPaolo Bonzini	$declaration_name = $2;
1438e2c54635SPaolo Bonzini	my $args = $3;
1439e2c54635SPaolo Bonzini
1440e2c54635SPaolo Bonzini	create_parameterlist($args, ',', $file, $declaration_name);
1441e2c54635SPaolo Bonzini
1442e2c54635SPaolo Bonzini	output_declaration($declaration_name,
1443e2c54635SPaolo Bonzini			   'function',
1444e2c54635SPaolo Bonzini			   {'function' => $declaration_name,
1445e2c54635SPaolo Bonzini			    'typedef' => 1,
1446e2c54635SPaolo Bonzini			    'module' => $modulename,
1447e2c54635SPaolo Bonzini			    'functiontype' => $return_type,
1448e2c54635SPaolo Bonzini			    'parameterlist' => \@parameterlist,
1449e2c54635SPaolo Bonzini			    'parameterdescs' => \%parameterdescs,
1450e2c54635SPaolo Bonzini			    'parametertypes' => \%parametertypes,
1451e2c54635SPaolo Bonzini			    'sectionlist' => \@sectionlist,
1452e2c54635SPaolo Bonzini			    'sections' => \%sections,
1453e2c54635SPaolo Bonzini			    'purpose' => $declaration_purpose
1454e2c54635SPaolo Bonzini			   });
1455e2c54635SPaolo Bonzini	return;
1456e2c54635SPaolo Bonzini    }
1457e2c54635SPaolo Bonzini
1458e2c54635SPaolo Bonzini    while (($x =~ /\(*.\)\s*;$/) || ($x =~ /\[*.\]\s*;$/)) {
1459e2c54635SPaolo Bonzini	$x =~ s/\(*.\)\s*;$/;/;
1460e2c54635SPaolo Bonzini	$x =~ s/\[*.\]\s*;$/;/;
1461e2c54635SPaolo Bonzini    }
1462e2c54635SPaolo Bonzini
1463e2c54635SPaolo Bonzini    if ($x =~ /typedef.*\s+(\w+)\s*;/) {
1464e2c54635SPaolo Bonzini	$declaration_name = $1;
1465e2c54635SPaolo Bonzini
1466e2c54635SPaolo Bonzini	output_declaration($declaration_name,
1467e2c54635SPaolo Bonzini			   'typedef',
1468e2c54635SPaolo Bonzini			   {'typedef' => $declaration_name,
1469e2c54635SPaolo Bonzini			    'module' => $modulename,
1470e2c54635SPaolo Bonzini			    'sectionlist' => \@sectionlist,
1471e2c54635SPaolo Bonzini			    'sections' => \%sections,
1472e2c54635SPaolo Bonzini			    'purpose' => $declaration_purpose
1473e2c54635SPaolo Bonzini			   });
1474e2c54635SPaolo Bonzini    }
1475e2c54635SPaolo Bonzini    else {
1476e2c54635SPaolo Bonzini	print STDERR "${file}:$.: error: Cannot parse typedef!\n";
1477e2c54635SPaolo Bonzini	++$errors;
1478e2c54635SPaolo Bonzini    }
1479e2c54635SPaolo Bonzini}
1480e2c54635SPaolo Bonzini
1481e2c54635SPaolo Bonzinisub save_struct_actual($) {
1482e2c54635SPaolo Bonzini    my $actual = shift;
1483e2c54635SPaolo Bonzini
1484e2c54635SPaolo Bonzini    # strip all spaces from the actual param so that it looks like one string item
1485e2c54635SPaolo Bonzini    $actual =~ s/\s*//g;
1486e2c54635SPaolo Bonzini    $struct_actual = $struct_actual . $actual . " ";
1487e2c54635SPaolo Bonzini}
1488e2c54635SPaolo Bonzini
1489e2c54635SPaolo Bonzinisub create_parameterlist($$$$) {
1490e2c54635SPaolo Bonzini    my $args = shift;
1491e2c54635SPaolo Bonzini    my $splitter = shift;
1492e2c54635SPaolo Bonzini    my $file = shift;
1493e2c54635SPaolo Bonzini    my $declaration_name = shift;
1494e2c54635SPaolo Bonzini    my $type;
1495e2c54635SPaolo Bonzini    my $param;
1496e2c54635SPaolo Bonzini
1497e2c54635SPaolo Bonzini    # temporarily replace commas inside function pointer definition
1498e2c54635SPaolo Bonzini    while ($args =~ /(\([^\),]+),/) {
1499e2c54635SPaolo Bonzini	$args =~ s/(\([^\),]+),/$1#/g;
1500e2c54635SPaolo Bonzini    }
1501e2c54635SPaolo Bonzini
1502e2c54635SPaolo Bonzini    foreach my $arg (split($splitter, $args)) {
1503e2c54635SPaolo Bonzini	# strip comments
1504e2c54635SPaolo Bonzini	$arg =~ s/\/\*.*\*\///;
1505e2c54635SPaolo Bonzini	# strip leading/trailing spaces
1506e2c54635SPaolo Bonzini	$arg =~ s/^\s*//;
1507e2c54635SPaolo Bonzini	$arg =~ s/\s*$//;
1508e2c54635SPaolo Bonzini	$arg =~ s/\s+/ /;
1509e2c54635SPaolo Bonzini
1510e2c54635SPaolo Bonzini	if ($arg =~ /^#/) {
1511e2c54635SPaolo Bonzini	    # Treat preprocessor directive as a typeless variable just to fill
1512e2c54635SPaolo Bonzini	    # corresponding data structures "correctly". Catch it later in
1513e2c54635SPaolo Bonzini	    # output_* subs.
15140c771852SMauro Carvalho Chehab	    push_parameter($arg, "", "", $file);
1515e2c54635SPaolo Bonzini	} elsif ($arg =~ m/\(.+\)\s*\(/) {
1516e2c54635SPaolo Bonzini	    # pointer-to-function
1517e2c54635SPaolo Bonzini	    $arg =~ tr/#/,/;
1518e2c54635SPaolo Bonzini	    $arg =~ m/[^\(]+\(\*?\s*([\w\.]*)\s*\)/;
1519e2c54635SPaolo Bonzini	    $param = $1;
1520e2c54635SPaolo Bonzini	    $type = $arg;
1521e2c54635SPaolo Bonzini	    $type =~ s/([^\(]+\(\*?)\s*$param/$1/;
1522e2c54635SPaolo Bonzini	    save_struct_actual($param);
15230c771852SMauro Carvalho Chehab	    push_parameter($param, $type, $arg, $file, $declaration_name);
1524e2c54635SPaolo Bonzini	} elsif ($arg) {
1525e2c54635SPaolo Bonzini	    $arg =~ s/\s*:\s*/:/g;
1526e2c54635SPaolo Bonzini	    $arg =~ s/\s*\[/\[/g;
1527e2c54635SPaolo Bonzini
1528e2c54635SPaolo Bonzini	    my @args = split('\s*,\s*', $arg);
1529e2c54635SPaolo Bonzini	    if ($args[0] =~ m/\*/) {
1530e2c54635SPaolo Bonzini		$args[0] =~ s/(\*+)\s*/ $1/;
1531e2c54635SPaolo Bonzini	    }
1532e2c54635SPaolo Bonzini
1533e2c54635SPaolo Bonzini	    my @first_arg;
1534e2c54635SPaolo Bonzini	    if ($args[0] =~ /^(.*\s+)(.*?\[.*\].*)$/) {
1535e2c54635SPaolo Bonzini		    shift @args;
1536e2c54635SPaolo Bonzini		    push(@first_arg, split('\s+', $1));
1537e2c54635SPaolo Bonzini		    push(@first_arg, $2);
1538e2c54635SPaolo Bonzini	    } else {
1539e2c54635SPaolo Bonzini		    @first_arg = split('\s+', shift @args);
1540e2c54635SPaolo Bonzini	    }
1541e2c54635SPaolo Bonzini
1542e2c54635SPaolo Bonzini	    unshift(@args, pop @first_arg);
1543e2c54635SPaolo Bonzini	    $type = join " ", @first_arg;
1544e2c54635SPaolo Bonzini
1545e2c54635SPaolo Bonzini	    foreach $param (@args) {
1546e2c54635SPaolo Bonzini		if ($param =~ m/^(\*+)\s*(.*)/) {
1547e2c54635SPaolo Bonzini		    save_struct_actual($2);
15480c771852SMauro Carvalho Chehab
15490c771852SMauro Carvalho Chehab		    push_parameter($2, "$type $1", $arg, $file, $declaration_name);
1550e2c54635SPaolo Bonzini		}
1551e2c54635SPaolo Bonzini		elsif ($param =~ m/(.*?):(\d+)/) {
1552e2c54635SPaolo Bonzini		    if ($type ne "") { # skip unnamed bit-fields
1553e2c54635SPaolo Bonzini			save_struct_actual($1);
15540c771852SMauro Carvalho Chehab			push_parameter($1, "$type:$2", $arg, $file, $declaration_name)
1555e2c54635SPaolo Bonzini		    }
1556e2c54635SPaolo Bonzini		}
1557e2c54635SPaolo Bonzini		else {
1558e2c54635SPaolo Bonzini		    save_struct_actual($param);
15590c771852SMauro Carvalho Chehab		    push_parameter($param, $type, $arg, $file, $declaration_name);
1560e2c54635SPaolo Bonzini		}
1561e2c54635SPaolo Bonzini	    }
1562e2c54635SPaolo Bonzini	}
1563e2c54635SPaolo Bonzini    }
1564e2c54635SPaolo Bonzini}
1565e2c54635SPaolo Bonzini
15660c771852SMauro Carvalho Chehabsub push_parameter($$$$$) {
1567e2c54635SPaolo Bonzini	my $param = shift;
1568e2c54635SPaolo Bonzini	my $type = shift;
15690c771852SMauro Carvalho Chehab	my $org_arg = shift;
1570e2c54635SPaolo Bonzini	my $file = shift;
1571e2c54635SPaolo Bonzini	my $declaration_name = shift;
1572e2c54635SPaolo Bonzini
1573e2c54635SPaolo Bonzini	if (($anon_struct_union == 1) && ($type eq "") &&
1574e2c54635SPaolo Bonzini	    ($param eq "}")) {
1575e2c54635SPaolo Bonzini		return;		# ignore the ending }; from anon. struct/union
1576e2c54635SPaolo Bonzini	}
1577e2c54635SPaolo Bonzini
1578e2c54635SPaolo Bonzini	$anon_struct_union = 0;
1579e2c54635SPaolo Bonzini	$param =~ s/[\[\)].*//;
1580e2c54635SPaolo Bonzini
1581e2c54635SPaolo Bonzini	if ($type eq "" && $param =~ /\.\.\.$/)
1582e2c54635SPaolo Bonzini	{
1583e2c54635SPaolo Bonzini	    if (!$param =~ /\w\.\.\.$/) {
1584e2c54635SPaolo Bonzini	      # handles unnamed variable parameters
1585e2c54635SPaolo Bonzini	      $param = "...";
1586e2c54635SPaolo Bonzini	    }
158765fb67bfSJonathan Neuschäfer	    elsif ($param =~ /\w\.\.\.$/) {
158865fb67bfSJonathan Neuschäfer	      # for named variable parameters of the form `x...`, remove the dots
158965fb67bfSJonathan Neuschäfer	      $param =~ s/\.\.\.$//;
159065fb67bfSJonathan Neuschäfer	    }
1591e2c54635SPaolo Bonzini	    if (!defined $parameterdescs{$param} || $parameterdescs{$param} eq "") {
1592e2c54635SPaolo Bonzini		$parameterdescs{$param} = "variable arguments";
1593e2c54635SPaolo Bonzini	    }
1594e2c54635SPaolo Bonzini	}
1595e2c54635SPaolo Bonzini	elsif ($type eq "" && ($param eq "" or $param eq "void"))
1596e2c54635SPaolo Bonzini	{
1597e2c54635SPaolo Bonzini	    $param="void";
1598e2c54635SPaolo Bonzini	    $parameterdescs{void} = "no arguments";
1599e2c54635SPaolo Bonzini	}
1600e2c54635SPaolo Bonzini	elsif ($type eq "" && ($param eq "struct" or $param eq "union"))
1601e2c54635SPaolo Bonzini	# handle unnamed (anonymous) union or struct:
1602e2c54635SPaolo Bonzini	{
1603e2c54635SPaolo Bonzini		$type = $param;
1604e2c54635SPaolo Bonzini		$param = "{unnamed_" . $param . "}";
1605e2c54635SPaolo Bonzini		$parameterdescs{$param} = "anonymous\n";
1606e2c54635SPaolo Bonzini		$anon_struct_union = 1;
1607e2c54635SPaolo Bonzini	}
1608e2c54635SPaolo Bonzini
1609e2c54635SPaolo Bonzini	# warn if parameter has no description
1610e2c54635SPaolo Bonzini	# (but ignore ones starting with # as these are not parameters
1611e2c54635SPaolo Bonzini	# but inline preprocessor statements);
1612e2c54635SPaolo Bonzini	# Note: It will also ignore void params and unnamed structs/unions
1613e2c54635SPaolo Bonzini	if (!defined $parameterdescs{$param} && $param !~ /^#/) {
1614e2c54635SPaolo Bonzini		$parameterdescs{$param} = $undescribed;
1615e2c54635SPaolo Bonzini
1616e2c54635SPaolo Bonzini	        if (show_warnings($type, $declaration_name) && $param !~ /\./) {
1617e2c54635SPaolo Bonzini			print STDERR
1618e2c54635SPaolo Bonzini			      "${file}:$.: warning: Function parameter or member '$param' not described in '$declaration_name'\n";
1619e2c54635SPaolo Bonzini			++$warnings;
1620e2c54635SPaolo Bonzini		}
1621e2c54635SPaolo Bonzini	}
1622e2c54635SPaolo Bonzini
1623e2c54635SPaolo Bonzini	# strip spaces from $param so that it is one continuous string
1624e2c54635SPaolo Bonzini	# on @parameterlist;
1625e2c54635SPaolo Bonzini	# this fixes a problem where check_sections() cannot find
1626e2c54635SPaolo Bonzini	# a parameter like "addr[6 + 2]" because it actually appears
1627e2c54635SPaolo Bonzini	# as "addr[6", "+", "2]" on the parameter list;
1628e2c54635SPaolo Bonzini	# but it's better to maintain the param string unchanged for output,
1629e2c54635SPaolo Bonzini	# so just weaken the string compare in check_sections() to ignore
1630e2c54635SPaolo Bonzini	# "[blah" in a parameter string;
1631e2c54635SPaolo Bonzini	###$param =~ s/\s*//g;
1632e2c54635SPaolo Bonzini	push @parameterlist, $param;
16330c771852SMauro Carvalho Chehab	$org_arg =~ s/\s\s+/ /g;
16340c771852SMauro Carvalho Chehab	$parametertypes{$param} = $org_arg;
1635e2c54635SPaolo Bonzini}
1636e2c54635SPaolo Bonzini
1637e2c54635SPaolo Bonzinisub check_sections($$$$$) {
1638e2c54635SPaolo Bonzini	my ($file, $decl_name, $decl_type, $sectcheck, $prmscheck) = @_;
1639e2c54635SPaolo Bonzini	my @sects = split ' ', $sectcheck;
1640e2c54635SPaolo Bonzini	my @prms = split ' ', $prmscheck;
1641e2c54635SPaolo Bonzini	my $err;
1642e2c54635SPaolo Bonzini	my ($px, $sx);
1643e2c54635SPaolo Bonzini	my $prm_clean;		# strip trailing "[array size]" and/or beginning "*"
1644e2c54635SPaolo Bonzini
1645e2c54635SPaolo Bonzini	foreach $sx (0 .. $#sects) {
1646e2c54635SPaolo Bonzini		$err = 1;
1647e2c54635SPaolo Bonzini		foreach $px (0 .. $#prms) {
1648e2c54635SPaolo Bonzini			$prm_clean = $prms[$px];
1649e2c54635SPaolo Bonzini			$prm_clean =~ s/\[.*\]//;
1650e2c54635SPaolo Bonzini			$prm_clean =~ s/__attribute__\s*\(\([a-z,_\*\s\(\)]*\)\)//i;
1651e2c54635SPaolo Bonzini			# ignore array size in a parameter string;
1652e2c54635SPaolo Bonzini			# however, the original param string may contain
1653e2c54635SPaolo Bonzini			# spaces, e.g.:  addr[6 + 2]
1654e2c54635SPaolo Bonzini			# and this appears in @prms as "addr[6" since the
1655e2c54635SPaolo Bonzini			# parameter list is split at spaces;
1656e2c54635SPaolo Bonzini			# hence just ignore "[..." for the sections check;
1657e2c54635SPaolo Bonzini			$prm_clean =~ s/\[.*//;
1658e2c54635SPaolo Bonzini
1659e2c54635SPaolo Bonzini			##$prm_clean =~ s/^\**//;
1660e2c54635SPaolo Bonzini			if ($prm_clean eq $sects[$sx]) {
1661e2c54635SPaolo Bonzini				$err = 0;
1662e2c54635SPaolo Bonzini				last;
1663e2c54635SPaolo Bonzini			}
1664e2c54635SPaolo Bonzini		}
1665e2c54635SPaolo Bonzini		if ($err) {
1666e2c54635SPaolo Bonzini			if ($decl_type eq "function") {
1667e2c54635SPaolo Bonzini				print STDERR "${file}:$.: warning: " .
1668e2c54635SPaolo Bonzini					"Excess function parameter " .
1669e2c54635SPaolo Bonzini					"'$sects[$sx]' " .
1670e2c54635SPaolo Bonzini					"description in '$decl_name'\n";
1671e2c54635SPaolo Bonzini				++$warnings;
1672e2c54635SPaolo Bonzini			}
1673e2c54635SPaolo Bonzini		}
1674e2c54635SPaolo Bonzini	}
1675e2c54635SPaolo Bonzini}
1676e2c54635SPaolo Bonzini
1677e2c54635SPaolo Bonzini##
1678e2c54635SPaolo Bonzini# Checks the section describing the return value of a function.
1679e2c54635SPaolo Bonzinisub check_return_section {
1680e2c54635SPaolo Bonzini        my $file = shift;
1681e2c54635SPaolo Bonzini        my $declaration_name = shift;
1682e2c54635SPaolo Bonzini        my $return_type = shift;
1683e2c54635SPaolo Bonzini
1684e2c54635SPaolo Bonzini        # Ignore an empty return type (It's a macro)
1685e2c54635SPaolo Bonzini        # Ignore functions with a "void" return type. (But don't ignore "void *")
1686e2c54635SPaolo Bonzini        if (($return_type eq "") || ($return_type =~ /void\s*\w*\s*$/)) {
1687e2c54635SPaolo Bonzini                return;
1688e2c54635SPaolo Bonzini        }
1689e2c54635SPaolo Bonzini
1690e2c54635SPaolo Bonzini        if (!defined($sections{$section_return}) ||
1691e2c54635SPaolo Bonzini            $sections{$section_return} eq "") {
1692e2c54635SPaolo Bonzini                print STDERR "${file}:$.: warning: " .
1693e2c54635SPaolo Bonzini                        "No description found for return value of " .
1694e2c54635SPaolo Bonzini                        "'$declaration_name'\n";
1695e2c54635SPaolo Bonzini                ++$warnings;
1696e2c54635SPaolo Bonzini        }
1697e2c54635SPaolo Bonzini}
1698e2c54635SPaolo Bonzini
1699e2c54635SPaolo Bonzini##
1700e2c54635SPaolo Bonzini# takes a function prototype and the name of the current file being
1701e2c54635SPaolo Bonzini# processed and spits out all the details stored in the global
1702e2c54635SPaolo Bonzini# arrays/hashes.
1703e2c54635SPaolo Bonzinisub dump_function($$) {
1704e2c54635SPaolo Bonzini    my $prototype = shift;
1705e2c54635SPaolo Bonzini    my $file = shift;
1706e2c54635SPaolo Bonzini    my $noret = 0;
1707e2c54635SPaolo Bonzini
1708697f668aSMauro Carvalho Chehab    print_lineno($.);
1709697f668aSMauro Carvalho Chehab
1710e2c54635SPaolo Bonzini    $prototype =~ s/^static +//;
1711e2c54635SPaolo Bonzini    $prototype =~ s/^extern +//;
1712e2c54635SPaolo Bonzini    $prototype =~ s/^asmlinkage +//;
1713e2c54635SPaolo Bonzini    $prototype =~ s/^inline +//;
1714e2c54635SPaolo Bonzini    $prototype =~ s/^__inline__ +//;
1715e2c54635SPaolo Bonzini    $prototype =~ s/^__inline +//;
1716e2c54635SPaolo Bonzini    $prototype =~ s/^__always_inline +//;
1717e2c54635SPaolo Bonzini    $prototype =~ s/^noinline +//;
1718e2c54635SPaolo Bonzini    $prototype =~ s/__init +//;
1719e2c54635SPaolo Bonzini    $prototype =~ s/__init_or_module +//;
1720e2c54635SPaolo Bonzini    $prototype =~ s/__meminit +//;
1721e2c54635SPaolo Bonzini    $prototype =~ s/__must_check +//;
1722e2c54635SPaolo Bonzini    $prototype =~ s/__weak +//;
1723e2c54635SPaolo Bonzini    $prototype =~ s/__sched +//;
1724e2c54635SPaolo Bonzini    $prototype =~ s/__printf\s*\(\s*\d*\s*,\s*\d*\s*\) +//;
1725e2c54635SPaolo Bonzini    my $define = $prototype =~ s/^#\s*define\s+//; #ak added
1726e2c54635SPaolo Bonzini    $prototype =~ s/__attribute__\s*\(\(
1727e2c54635SPaolo Bonzini            (?:
1728e2c54635SPaolo Bonzini                 [\w\s]++          # attribute name
1729e2c54635SPaolo Bonzini                 (?:\([^)]*+\))?   # attribute arguments
1730e2c54635SPaolo Bonzini                 \s*+,?            # optional comma at the end
1731e2c54635SPaolo Bonzini            )+
1732e2c54635SPaolo Bonzini          \)\)\s+//x;
1733e2c54635SPaolo Bonzini
1734e2c54635SPaolo Bonzini    # Yes, this truly is vile.  We are looking for:
1735e2c54635SPaolo Bonzini    # 1. Return type (may be nothing if we're looking at a macro)
1736e2c54635SPaolo Bonzini    # 2. Function name
1737e2c54635SPaolo Bonzini    # 3. Function parameters.
1738e2c54635SPaolo Bonzini    #
1739e2c54635SPaolo Bonzini    # All the while we have to watch out for function pointer parameters
1740e2c54635SPaolo Bonzini    # (which IIRC is what the two sections are for), C types (these
1741e2c54635SPaolo Bonzini    # regexps don't even start to express all the possibilities), and
1742e2c54635SPaolo Bonzini    # so on.
1743e2c54635SPaolo Bonzini    #
1744e2c54635SPaolo Bonzini    # If you mess with these regexps, it's a good idea to check that
1745e2c54635SPaolo Bonzini    # the following functions' documentation still comes out right:
1746e2c54635SPaolo Bonzini    # - parport_register_device (function pointer parameters)
1747d73415a3SStefan Hajnoczi    # - qatomic_set (macro)
1748e2c54635SPaolo Bonzini    # - pci_match_device, __copy_to_user (long return type)
1749e2c54635SPaolo Bonzini
1750e2c54635SPaolo Bonzini    if ($define && $prototype =~ m/^()([a-zA-Z0-9_~:]+)\s+/) {
1751e2c54635SPaolo Bonzini        # This is an object-like macro, it has no return type and no parameter
1752e2c54635SPaolo Bonzini        # list.
1753e2c54635SPaolo Bonzini        # Function-like macros are not allowed to have spaces between
1754e2c54635SPaolo Bonzini        # declaration_name and opening parenthesis (notice the \s+).
1755e2c54635SPaolo Bonzini        $return_type = $1;
1756e2c54635SPaolo Bonzini        $declaration_name = $2;
1757e2c54635SPaolo Bonzini        $noret = 1;
1758e2c54635SPaolo Bonzini    } elsif ($prototype =~ m/^()([a-zA-Z0-9_~:]+)\s*\(([^\(]*)\)/ ||
1759e2c54635SPaolo Bonzini	$prototype =~ m/^(\w+)\s+([a-zA-Z0-9_~:]+)\s*\(([^\(]*)\)/ ||
1760e2c54635SPaolo Bonzini	$prototype =~ m/^(\w+\s*\*+)\s*([a-zA-Z0-9_~:]+)\s*\(([^\(]*)\)/ ||
1761e2c54635SPaolo Bonzini	$prototype =~ m/^(\w+\s+\w+)\s+([a-zA-Z0-9_~:]+)\s*\(([^\(]*)\)/ ||
1762e2c54635SPaolo Bonzini	$prototype =~ m/^(\w+\s+\w+\s*\*+)\s*([a-zA-Z0-9_~:]+)\s*\(([^\(]*)\)/ ||
1763e2c54635SPaolo Bonzini	$prototype =~ m/^(\w+\s+\w+\s+\w+)\s+([a-zA-Z0-9_~:]+)\s*\(([^\(]*)\)/ ||
1764e2c54635SPaolo Bonzini	$prototype =~ m/^(\w+\s+\w+\s+\w+\s*\*+)\s*([a-zA-Z0-9_~:]+)\s*\(([^\(]*)\)/ ||
1765e2c54635SPaolo Bonzini	$prototype =~ m/^()([a-zA-Z0-9_~:]+)\s*\(([^\{]*)\)/ ||
1766e2c54635SPaolo Bonzini	$prototype =~ m/^(\w+)\s+([a-zA-Z0-9_~:]+)\s*\(([^\{]*)\)/ ||
1767e2c54635SPaolo Bonzini	$prototype =~ m/^(\w+\s*\*+)\s*([a-zA-Z0-9_~:]+)\s*\(([^\{]*)\)/ ||
1768e2c54635SPaolo Bonzini	$prototype =~ m/^(\w+\s+\w+)\s+([a-zA-Z0-9_~:]+)\s*\(([^\{]*)\)/ ||
1769e2c54635SPaolo Bonzini	$prototype =~ m/^(\w+\s+\w+\s*\*+)\s*([a-zA-Z0-9_~:]+)\s*\(([^\{]*)\)/ ||
1770e2c54635SPaolo Bonzini	$prototype =~ m/^(\w+\s+\w+\s+\w+)\s+([a-zA-Z0-9_~:]+)\s*\(([^\{]*)\)/ ||
1771e2c54635SPaolo Bonzini	$prototype =~ m/^(\w+\s+\w+\s+\w+\s*\*+)\s*([a-zA-Z0-9_~:]+)\s*\(([^\{]*)\)/ ||
1772e2c54635SPaolo Bonzini	$prototype =~ m/^(\w+\s+\w+\s+\w+\s+\w+)\s+([a-zA-Z0-9_~:]+)\s*\(([^\{]*)\)/ ||
1773e2c54635SPaolo Bonzini	$prototype =~ m/^(\w+\s+\w+\s+\w+\s+\w+\s*\*+)\s*([a-zA-Z0-9_~:]+)\s*\(([^\{]*)\)/ ||
1774e2c54635SPaolo Bonzini	$prototype =~ m/^(\w+\s+\w+\s*\*+\s*\w+\s*\*+\s*)\s*([a-zA-Z0-9_~:]+)\s*\(([^\{]*)\)/)  {
1775e2c54635SPaolo Bonzini	$return_type = $1;
1776e2c54635SPaolo Bonzini	$declaration_name = $2;
1777e2c54635SPaolo Bonzini	my $args = $3;
1778e2c54635SPaolo Bonzini
1779e2c54635SPaolo Bonzini	create_parameterlist($args, ',', $file, $declaration_name);
1780e2c54635SPaolo Bonzini    } else {
1781e2c54635SPaolo Bonzini	print STDERR "${file}:$.: warning: cannot understand function prototype: '$prototype'\n";
1782e2c54635SPaolo Bonzini	return;
1783e2c54635SPaolo Bonzini    }
1784e2c54635SPaolo Bonzini
1785e2c54635SPaolo Bonzini    my $prms = join " ", @parameterlist;
1786e2c54635SPaolo Bonzini    check_sections($file, $declaration_name, "function", $sectcheck, $prms);
1787e2c54635SPaolo Bonzini
1788e2c54635SPaolo Bonzini    # This check emits a lot of warnings at the moment, because many
1789e2c54635SPaolo Bonzini    # functions don't have a 'Return' doc section. So until the number
1790e2c54635SPaolo Bonzini    # of warnings goes sufficiently down, the check is only performed in
1791e2c54635SPaolo Bonzini    # verbose mode.
1792e2c54635SPaolo Bonzini    # TODO: always perform the check.
1793e2c54635SPaolo Bonzini    if ($verbose && !$noret) {
1794e2c54635SPaolo Bonzini	    check_return_section($file, $declaration_name, $return_type);
1795e2c54635SPaolo Bonzini    }
1796e2c54635SPaolo Bonzini
17973999ffcfSMauro Carvalho Chehab    # The function parser can be called with a typedef parameter.
17983999ffcfSMauro Carvalho Chehab    # Handle it.
17993999ffcfSMauro Carvalho Chehab    if ($return_type =~ /typedef/) {
18003999ffcfSMauro Carvalho Chehab	output_declaration($declaration_name,
18013999ffcfSMauro Carvalho Chehab			   'function',
18023999ffcfSMauro Carvalho Chehab			   {'function' => $declaration_name,
18033999ffcfSMauro Carvalho Chehab			    'typedef' => 1,
18043999ffcfSMauro Carvalho Chehab			    'module' => $modulename,
18053999ffcfSMauro Carvalho Chehab			    'functiontype' => $return_type,
18063999ffcfSMauro Carvalho Chehab			    'parameterlist' => \@parameterlist,
18073999ffcfSMauro Carvalho Chehab			    'parameterdescs' => \%parameterdescs,
18083999ffcfSMauro Carvalho Chehab			    'parametertypes' => \%parametertypes,
18093999ffcfSMauro Carvalho Chehab			    'sectionlist' => \@sectionlist,
18103999ffcfSMauro Carvalho Chehab			    'sections' => \%sections,
18113999ffcfSMauro Carvalho Chehab			    'purpose' => $declaration_purpose
18123999ffcfSMauro Carvalho Chehab			   });
18133999ffcfSMauro Carvalho Chehab    } else {
1814e2c54635SPaolo Bonzini	output_declaration($declaration_name,
1815e2c54635SPaolo Bonzini			   'function',
1816e2c54635SPaolo Bonzini			   {'function' => $declaration_name,
1817e2c54635SPaolo Bonzini			    'module' => $modulename,
1818e2c54635SPaolo Bonzini			    'functiontype' => $return_type,
1819e2c54635SPaolo Bonzini			    'parameterlist' => \@parameterlist,
1820e2c54635SPaolo Bonzini			    'parameterdescs' => \%parameterdescs,
1821e2c54635SPaolo Bonzini			    'parametertypes' => \%parametertypes,
1822e2c54635SPaolo Bonzini			    'sectionlist' => \@sectionlist,
1823e2c54635SPaolo Bonzini			    'sections' => \%sections,
1824e2c54635SPaolo Bonzini			    'purpose' => $declaration_purpose
1825e2c54635SPaolo Bonzini			   });
1826e2c54635SPaolo Bonzini    }
18273999ffcfSMauro Carvalho Chehab}
1828e2c54635SPaolo Bonzini
1829e2c54635SPaolo Bonzinisub reset_state {
1830e2c54635SPaolo Bonzini    $function = "";
1831e2c54635SPaolo Bonzini    %parameterdescs = ();
1832e2c54635SPaolo Bonzini    %parametertypes = ();
1833e2c54635SPaolo Bonzini    @parameterlist = ();
1834e2c54635SPaolo Bonzini    %sections = ();
1835e2c54635SPaolo Bonzini    @sectionlist = ();
1836e2c54635SPaolo Bonzini    $sectcheck = "";
1837e2c54635SPaolo Bonzini    $struct_actual = "";
1838e2c54635SPaolo Bonzini    $prototype = "";
1839e2c54635SPaolo Bonzini
1840e2c54635SPaolo Bonzini    $state = STATE_NORMAL;
1841e2c54635SPaolo Bonzini    $inline_doc_state = STATE_INLINE_NA;
1842e2c54635SPaolo Bonzini}
1843e2c54635SPaolo Bonzini
1844e2c54635SPaolo Bonzinisub tracepoint_munge($) {
1845e2c54635SPaolo Bonzini	my $file = shift;
1846e2c54635SPaolo Bonzini	my $tracepointname = 0;
1847e2c54635SPaolo Bonzini	my $tracepointargs = 0;
1848e2c54635SPaolo Bonzini
1849e2c54635SPaolo Bonzini	if ($prototype =~ m/TRACE_EVENT\((.*?),/) {
1850e2c54635SPaolo Bonzini		$tracepointname = $1;
1851e2c54635SPaolo Bonzini	}
1852e2c54635SPaolo Bonzini	if ($prototype =~ m/DEFINE_SINGLE_EVENT\((.*?),/) {
1853e2c54635SPaolo Bonzini		$tracepointname = $1;
1854e2c54635SPaolo Bonzini	}
1855e2c54635SPaolo Bonzini	if ($prototype =~ m/DEFINE_EVENT\((.*?),(.*?),/) {
1856e2c54635SPaolo Bonzini		$tracepointname = $2;
1857e2c54635SPaolo Bonzini	}
1858e2c54635SPaolo Bonzini	$tracepointname =~ s/^\s+//; #strip leading whitespace
1859e2c54635SPaolo Bonzini	if ($prototype =~ m/TP_PROTO\((.*?)\)/) {
1860e2c54635SPaolo Bonzini		$tracepointargs = $1;
1861e2c54635SPaolo Bonzini	}
1862e2c54635SPaolo Bonzini	if (($tracepointname eq 0) || ($tracepointargs eq 0)) {
1863e2c54635SPaolo Bonzini		print STDERR "${file}:$.: warning: Unrecognized tracepoint format: \n".
1864e2c54635SPaolo Bonzini			     "$prototype\n";
1865e2c54635SPaolo Bonzini	} else {
1866e2c54635SPaolo Bonzini		$prototype = "static inline void trace_$tracepointname($tracepointargs)";
1867e2c54635SPaolo Bonzini	}
1868e2c54635SPaolo Bonzini}
1869e2c54635SPaolo Bonzini
1870e2c54635SPaolo Bonzinisub syscall_munge() {
1871e2c54635SPaolo Bonzini	my $void = 0;
1872e2c54635SPaolo Bonzini
1873e2c54635SPaolo Bonzini	$prototype =~ s@[\r\n]+@ @gos; # strip newlines/CR's
1874e2c54635SPaolo Bonzini##	if ($prototype =~ m/SYSCALL_DEFINE0\s*\(\s*(a-zA-Z0-9_)*\s*\)/) {
1875e2c54635SPaolo Bonzini	if ($prototype =~ m/SYSCALL_DEFINE0/) {
1876e2c54635SPaolo Bonzini		$void = 1;
1877e2c54635SPaolo Bonzini##		$prototype = "long sys_$1(void)";
1878e2c54635SPaolo Bonzini	}
1879e2c54635SPaolo Bonzini
1880e2c54635SPaolo Bonzini	$prototype =~ s/SYSCALL_DEFINE.*\(/long sys_/; # fix return type & func name
1881e2c54635SPaolo Bonzini	if ($prototype =~ m/long (sys_.*?),/) {
1882e2c54635SPaolo Bonzini		$prototype =~ s/,/\(/;
1883e2c54635SPaolo Bonzini	} elsif ($void) {
1884e2c54635SPaolo Bonzini		$prototype =~ s/\)/\(void\)/;
1885e2c54635SPaolo Bonzini	}
1886e2c54635SPaolo Bonzini
1887e2c54635SPaolo Bonzini	# now delete all of the odd-number commas in $prototype
1888e2c54635SPaolo Bonzini	# so that arg types & arg names don't have a comma between them
1889e2c54635SPaolo Bonzini	my $count = 0;
1890e2c54635SPaolo Bonzini	my $len = length($prototype);
1891e2c54635SPaolo Bonzini	if ($void) {
1892e2c54635SPaolo Bonzini		$len = 0;	# skip the for-loop
1893e2c54635SPaolo Bonzini	}
1894e2c54635SPaolo Bonzini	for (my $ix = 0; $ix < $len; $ix++) {
1895e2c54635SPaolo Bonzini		if (substr($prototype, $ix, 1) eq ',') {
1896e2c54635SPaolo Bonzini			$count++;
1897e2c54635SPaolo Bonzini			if ($count % 2 == 1) {
1898e2c54635SPaolo Bonzini				substr($prototype, $ix, 1) = ' ';
1899e2c54635SPaolo Bonzini			}
1900e2c54635SPaolo Bonzini		}
1901e2c54635SPaolo Bonzini	}
1902e2c54635SPaolo Bonzini}
1903e2c54635SPaolo Bonzini
1904e2c54635SPaolo Bonzinisub process_proto_function($$) {
1905e2c54635SPaolo Bonzini    my $x = shift;
1906e2c54635SPaolo Bonzini    my $file = shift;
1907e2c54635SPaolo Bonzini
1908e2c54635SPaolo Bonzini    $x =~ s@\/\/.*$@@gos; # strip C99-style comments to end of line
1909e2c54635SPaolo Bonzini
1910e2c54635SPaolo Bonzini    if ($x =~ m#\s*/\*\s+MACDOC\s*#io || ($x =~ /^#/ && $x !~ /^#\s*define/)) {
1911e2c54635SPaolo Bonzini	# do nothing
1912e2c54635SPaolo Bonzini    }
1913e2c54635SPaolo Bonzini    elsif ($x =~ /([^\{]*)/) {
1914e2c54635SPaolo Bonzini	$prototype .= $1;
1915e2c54635SPaolo Bonzini    }
1916e2c54635SPaolo Bonzini
1917e2c54635SPaolo Bonzini    if (($x =~ /\{/) || ($x =~ /\#\s*define/) || ($x =~ /;/)) {
1918e2c54635SPaolo Bonzini	$prototype =~ s@/\*.*?\*/@@gos;	# strip comments.
1919e2c54635SPaolo Bonzini	$prototype =~ s@[\r\n]+@ @gos; # strip newlines/cr's.
1920e2c54635SPaolo Bonzini	$prototype =~ s@^\s+@@gos; # strip leading spaces
19212552f59aSMauro Carvalho Chehab
19222552f59aSMauro Carvalho Chehab	 # Handle prototypes for function pointers like:
19232552f59aSMauro Carvalho Chehab	 # int (*pcs_config)(struct foo)
19242552f59aSMauro Carvalho Chehab	$prototype =~ s@^(\S+\s+)\(\s*\*(\S+)\)@$1$2@gos;
19252552f59aSMauro Carvalho Chehab
1926e2c54635SPaolo Bonzini	if ($prototype =~ /SYSCALL_DEFINE/) {
1927e2c54635SPaolo Bonzini		syscall_munge();
1928e2c54635SPaolo Bonzini	}
1929e2c54635SPaolo Bonzini	if ($prototype =~ /TRACE_EVENT/ || $prototype =~ /DEFINE_EVENT/ ||
1930e2c54635SPaolo Bonzini	    $prototype =~ /DEFINE_SINGLE_EVENT/)
1931e2c54635SPaolo Bonzini	{
1932e2c54635SPaolo Bonzini		tracepoint_munge($file);
1933e2c54635SPaolo Bonzini	}
1934e2c54635SPaolo Bonzini	dump_function($prototype, $file);
1935e2c54635SPaolo Bonzini	reset_state();
1936e2c54635SPaolo Bonzini    }
1937e2c54635SPaolo Bonzini}
1938e2c54635SPaolo Bonzini
1939e2c54635SPaolo Bonzinisub process_proto_type($$) {
1940e2c54635SPaolo Bonzini    my $x = shift;
1941e2c54635SPaolo Bonzini    my $file = shift;
1942e2c54635SPaolo Bonzini
1943e2c54635SPaolo Bonzini    $x =~ s@[\r\n]+@ @gos; # strip newlines/cr's.
1944e2c54635SPaolo Bonzini    $x =~ s@^\s+@@gos; # strip leading spaces
1945e2c54635SPaolo Bonzini    $x =~ s@\s+$@@gos; # strip trailing spaces
1946e2c54635SPaolo Bonzini    $x =~ s@\/\/.*$@@gos; # strip C99-style comments to end of line
1947e2c54635SPaolo Bonzini
1948e2c54635SPaolo Bonzini    if ($x =~ /^#/) {
1949e2c54635SPaolo Bonzini	# To distinguish preprocessor directive from regular declaration later.
1950e2c54635SPaolo Bonzini	$x .= ";";
1951e2c54635SPaolo Bonzini    }
1952e2c54635SPaolo Bonzini
1953e2c54635SPaolo Bonzini    while (1) {
1954e2c54635SPaolo Bonzini	if ( $x =~ /([^\{\};]*)([\{\};])(.*)/ ) {
1955e2c54635SPaolo Bonzini            if( length $prototype ) {
1956e2c54635SPaolo Bonzini                $prototype .= " "
1957e2c54635SPaolo Bonzini            }
1958e2c54635SPaolo Bonzini	    $prototype .= $1 . $2;
1959e2c54635SPaolo Bonzini	    ($2 eq '{') && $brcount++;
1960e2c54635SPaolo Bonzini	    ($2 eq '}') && $brcount--;
1961e2c54635SPaolo Bonzini	    if (($2 eq ';') && ($brcount == 0)) {
1962e2c54635SPaolo Bonzini		dump_declaration($prototype, $file);
1963e2c54635SPaolo Bonzini		reset_state();
1964e2c54635SPaolo Bonzini		last;
1965e2c54635SPaolo Bonzini	    }
1966e2c54635SPaolo Bonzini	    $x = $3;
1967e2c54635SPaolo Bonzini	} else {
1968e2c54635SPaolo Bonzini	    $prototype .= $x;
1969e2c54635SPaolo Bonzini	    last;
1970e2c54635SPaolo Bonzini	}
1971e2c54635SPaolo Bonzini    }
1972e2c54635SPaolo Bonzini}
1973e2c54635SPaolo Bonzini
1974e2c54635SPaolo Bonzini
1975e2c54635SPaolo Bonzinisub map_filename($) {
1976e2c54635SPaolo Bonzini    my $file;
1977e2c54635SPaolo Bonzini    my ($orig_file) = @_;
1978e2c54635SPaolo Bonzini
1979e2c54635SPaolo Bonzini    if (defined($ENV{'SRCTREE'})) {
1980e2c54635SPaolo Bonzini	$file = "$ENV{'SRCTREE'}" . "/" . $orig_file;
1981e2c54635SPaolo Bonzini    } else {
1982e2c54635SPaolo Bonzini	$file = $orig_file;
1983e2c54635SPaolo Bonzini    }
1984e2c54635SPaolo Bonzini
1985e2c54635SPaolo Bonzini    if (defined($source_map{$file})) {
1986e2c54635SPaolo Bonzini	$file = $source_map{$file};
1987e2c54635SPaolo Bonzini    }
1988e2c54635SPaolo Bonzini
1989e2c54635SPaolo Bonzini    return $file;
1990e2c54635SPaolo Bonzini}
1991e2c54635SPaolo Bonzini
1992e2c54635SPaolo Bonzinisub process_export_file($) {
1993e2c54635SPaolo Bonzini    my ($orig_file) = @_;
1994e2c54635SPaolo Bonzini    my $file = map_filename($orig_file);
1995e2c54635SPaolo Bonzini
1996e2c54635SPaolo Bonzini    if (!open(IN,"<$file")) {
1997e2c54635SPaolo Bonzini	print STDERR "Error: Cannot open file $file\n";
1998e2c54635SPaolo Bonzini	++$errors;
1999e2c54635SPaolo Bonzini	return;
2000e2c54635SPaolo Bonzini    }
2001e2c54635SPaolo Bonzini
2002e2c54635SPaolo Bonzini    while (<IN>) {
2003e2c54635SPaolo Bonzini	if (/$export_symbol/) {
200478c8c92cSMauro Carvalho Chehab	    next if (defined($nosymbol_table{$2}));
2005e2c54635SPaolo Bonzini	    $function_table{$2} = 1;
2006e2c54635SPaolo Bonzini	}
2007e2c54635SPaolo Bonzini    }
2008e2c54635SPaolo Bonzini
2009e2c54635SPaolo Bonzini    close(IN);
2010e2c54635SPaolo Bonzini}
2011e2c54635SPaolo Bonzini
2012e2c54635SPaolo Bonzini#
2013e2c54635SPaolo Bonzini# Parsers for the various processing states.
2014e2c54635SPaolo Bonzini#
2015e2c54635SPaolo Bonzini# STATE_NORMAL: looking for the /** to begin everything.
2016e2c54635SPaolo Bonzini#
2017e2c54635SPaolo Bonzinisub process_normal() {
2018e2c54635SPaolo Bonzini    if (/$doc_start/o) {
2019e2c54635SPaolo Bonzini	$state = STATE_NAME;	# next line is always the function name
2020e2c54635SPaolo Bonzini	$in_doc_sect = 0;
2021e2c54635SPaolo Bonzini	$declaration_start_line = $. + 1;
2022e2c54635SPaolo Bonzini    }
2023e2c54635SPaolo Bonzini}
2024e2c54635SPaolo Bonzini
2025e2c54635SPaolo Bonzini#
2026e2c54635SPaolo Bonzini# STATE_NAME: Looking for the "name - description" line
2027e2c54635SPaolo Bonzini#
2028e2c54635SPaolo Bonzinisub process_name($$) {
2029e2c54635SPaolo Bonzini    my $file = shift;
2030e2c54635SPaolo Bonzini    my $identifier;
2031e2c54635SPaolo Bonzini    my $descr;
2032e2c54635SPaolo Bonzini
2033e2c54635SPaolo Bonzini    if (/$doc_block/o) {
2034e2c54635SPaolo Bonzini	$state = STATE_DOCBLOCK;
2035e2c54635SPaolo Bonzini	$contents = "";
2036e2c54635SPaolo Bonzini	$new_start_line = $. + 1;
2037e2c54635SPaolo Bonzini
2038e2c54635SPaolo Bonzini	if ( $1 eq "" ) {
2039e2c54635SPaolo Bonzini	    $section = $section_intro;
2040e2c54635SPaolo Bonzini	} else {
2041e2c54635SPaolo Bonzini	    $section = $1;
2042e2c54635SPaolo Bonzini	}
2043e2c54635SPaolo Bonzini    }
2044e2c54635SPaolo Bonzini    elsif (/$doc_decl/o) {
2045e2c54635SPaolo Bonzini	$identifier = $1;
20464cf41794SPaolo Bonzini	if (/\s*([\w\s]+?)(\s*-|:)/) {
2047e2c54635SPaolo Bonzini	    $identifier = $1;
2048e2c54635SPaolo Bonzini	}
2049e2c54635SPaolo Bonzini
2050e2c54635SPaolo Bonzini	$state = STATE_BODY;
2051e2c54635SPaolo Bonzini	# if there's no @param blocks need to set up default section
2052e2c54635SPaolo Bonzini	# here
2053e2c54635SPaolo Bonzini	$contents = "";
2054e2c54635SPaolo Bonzini	$section = $section_default;
2055e2c54635SPaolo Bonzini	$new_start_line = $. + 1;
20564cf41794SPaolo Bonzini	if (/[-:](.*)/) {
2057e2c54635SPaolo Bonzini	    # strip leading/trailing/multiple spaces
2058e2c54635SPaolo Bonzini	    $descr= $1;
2059e2c54635SPaolo Bonzini	    $descr =~ s/^\s*//;
2060e2c54635SPaolo Bonzini	    $descr =~ s/\s*$//;
2061e2c54635SPaolo Bonzini	    $descr =~ s/\s+/ /g;
2062e2c54635SPaolo Bonzini	    $declaration_purpose = $descr;
2063e2c54635SPaolo Bonzini	    $state = STATE_BODY_MAYBE;
2064e2c54635SPaolo Bonzini	} else {
2065e2c54635SPaolo Bonzini	    $declaration_purpose = "";
2066e2c54635SPaolo Bonzini	}
2067e2c54635SPaolo Bonzini
2068e2c54635SPaolo Bonzini	if (($declaration_purpose eq "") && $verbose) {
2069e2c54635SPaolo Bonzini	    print STDERR "${file}:$.: warning: missing initial short description on line:\n";
2070e2c54635SPaolo Bonzini	    print STDERR $_;
2071e2c54635SPaolo Bonzini	    ++$warnings;
2072e2c54635SPaolo Bonzini	}
2073e2c54635SPaolo Bonzini
2074eb4c9775SEduardo Habkost	if ($identifier =~ m/^struct\b/) {
2075e2c54635SPaolo Bonzini	    $decl_type = 'struct';
2076e2c54635SPaolo Bonzini	} elsif ($identifier =~ m/^union\b/) {
2077e2c54635SPaolo Bonzini	    $decl_type = 'union';
2078e2c54635SPaolo Bonzini	} elsif ($identifier =~ m/^enum\b/) {
2079e2c54635SPaolo Bonzini	    $decl_type = 'enum';
2080e2c54635SPaolo Bonzini	} elsif ($identifier =~ m/^typedef\b/) {
2081e2c54635SPaolo Bonzini	    $decl_type = 'typedef';
2082e2c54635SPaolo Bonzini	} else {
2083e2c54635SPaolo Bonzini	    $decl_type = 'function';
2084e2c54635SPaolo Bonzini	}
2085e2c54635SPaolo Bonzini
2086e2c54635SPaolo Bonzini	if ($verbose) {
2087e2c54635SPaolo Bonzini	    print STDERR "${file}:$.: info: Scanning doc for $identifier\n";
2088e2c54635SPaolo Bonzini	}
2089e2c54635SPaolo Bonzini    } else {
2090e2c54635SPaolo Bonzini	print STDERR "${file}:$.: warning: Cannot understand $_ on line $.",
2091e2c54635SPaolo Bonzini	    " - I thought it was a doc line\n";
2092e2c54635SPaolo Bonzini	++$warnings;
2093e2c54635SPaolo Bonzini	$state = STATE_NORMAL;
2094e2c54635SPaolo Bonzini    }
2095e2c54635SPaolo Bonzini}
2096e2c54635SPaolo Bonzini
2097e2c54635SPaolo Bonzini
2098e2c54635SPaolo Bonzini#
2099e2c54635SPaolo Bonzini# STATE_BODY and STATE_BODY_MAYBE: the bulk of a kerneldoc comment.
2100e2c54635SPaolo Bonzini#
2101e2c54635SPaolo Bonzinisub process_body($$) {
2102e2c54635SPaolo Bonzini    my $file = shift;
2103e2c54635SPaolo Bonzini
210465fb67bfSJonathan Neuschäfer    # Until all named variable macro parameters are
210565fb67bfSJonathan Neuschäfer    # documented using the bare name (`x`) rather than with
210665fb67bfSJonathan Neuschäfer    # dots (`x...`), strip the dots:
210765fb67bfSJonathan Neuschäfer    if ($section =~ /\w\.\.\.$/) {
210865fb67bfSJonathan Neuschäfer	$section =~ s/\.\.\.$//;
210965fb67bfSJonathan Neuschäfer
211065fb67bfSJonathan Neuschäfer	if ($verbose) {
211165fb67bfSJonathan Neuschäfer	    print STDERR "${file}:$.: warning: Variable macro arguments should be documented without dots\n";
211265fb67bfSJonathan Neuschäfer	    ++$warnings;
211365fb67bfSJonathan Neuschäfer	}
211465fb67bfSJonathan Neuschäfer    }
211565fb67bfSJonathan Neuschäfer
21169f2b463aSMauro Carvalho Chehab    if ($state == STATE_BODY_WITH_BLANK_LINE && /^\s*\*\s?\S/) {
21179f2b463aSMauro Carvalho Chehab	dump_section($file, $section, $contents);
21189f2b463aSMauro Carvalho Chehab	$section = $section_default;
21199f2b463aSMauro Carvalho Chehab	$contents = "";
21209f2b463aSMauro Carvalho Chehab    }
21219f2b463aSMauro Carvalho Chehab
2122e2c54635SPaolo Bonzini    if (/$doc_sect/i) { # case insensitive for supported section names
2123e2c54635SPaolo Bonzini	$newsection = $1;
2124e2c54635SPaolo Bonzini	$newcontents = $2;
2125e2c54635SPaolo Bonzini
2126e2c54635SPaolo Bonzini	# map the supported section names to the canonical names
2127e2c54635SPaolo Bonzini	if ($newsection =~ m/^description$/i) {
2128e2c54635SPaolo Bonzini	    $newsection = $section_default;
2129e2c54635SPaolo Bonzini	} elsif ($newsection =~ m/^context$/i) {
2130e2c54635SPaolo Bonzini	    $newsection = $section_context;
2131e2c54635SPaolo Bonzini	} elsif ($newsection =~ m/^returns?$/i) {
2132e2c54635SPaolo Bonzini	    $newsection = $section_return;
2133e2c54635SPaolo Bonzini	} elsif ($newsection =~ m/^\@return$/) {
2134e2c54635SPaolo Bonzini	    # special: @return is a section, not a param description
2135e2c54635SPaolo Bonzini	    $newsection = $section_return;
2136e2c54635SPaolo Bonzini	}
2137e2c54635SPaolo Bonzini
2138e2c54635SPaolo Bonzini	if (($contents ne "") && ($contents ne "\n")) {
2139e2c54635SPaolo Bonzini	    if (!$in_doc_sect && $verbose) {
2140e2c54635SPaolo Bonzini		print STDERR "${file}:$.: warning: contents before sections\n";
2141e2c54635SPaolo Bonzini		++$warnings;
2142e2c54635SPaolo Bonzini	    }
2143e2c54635SPaolo Bonzini	    dump_section($file, $section, $contents);
2144e2c54635SPaolo Bonzini	    $section = $section_default;
2145e2c54635SPaolo Bonzini	}
2146e2c54635SPaolo Bonzini
2147e2c54635SPaolo Bonzini	$in_doc_sect = 1;
2148e2c54635SPaolo Bonzini	$state = STATE_BODY;
2149e2c54635SPaolo Bonzini	$contents = $newcontents;
2150e2c54635SPaolo Bonzini	$new_start_line = $.;
2151e2c54635SPaolo Bonzini	while (substr($contents, 0, 1) eq " ") {
2152e2c54635SPaolo Bonzini	    $contents = substr($contents, 1);
2153e2c54635SPaolo Bonzini	}
2154e2c54635SPaolo Bonzini	if ($contents ne "") {
2155e2c54635SPaolo Bonzini	    $contents .= "\n";
2156e2c54635SPaolo Bonzini	}
2157e2c54635SPaolo Bonzini	$section = $newsection;
2158e2c54635SPaolo Bonzini	$leading_space = undef;
2159e2c54635SPaolo Bonzini    } elsif (/$doc_end/) {
2160e2c54635SPaolo Bonzini	if (($contents ne "") && ($contents ne "\n")) {
2161e2c54635SPaolo Bonzini	    dump_section($file, $section, $contents);
2162e2c54635SPaolo Bonzini	    $section = $section_default;
2163e2c54635SPaolo Bonzini	    $contents = "";
2164e2c54635SPaolo Bonzini	}
2165e2c54635SPaolo Bonzini	# look for doc_com + <text> + doc_end:
2166e2c54635SPaolo Bonzini	if ($_ =~ m'\s*\*\s*[a-zA-Z_0-9:\.]+\*/') {
2167e2c54635SPaolo Bonzini	    print STDERR "${file}:$.: warning: suspicious ending line: $_";
2168e2c54635SPaolo Bonzini	    ++$warnings;
2169e2c54635SPaolo Bonzini	}
2170e2c54635SPaolo Bonzini
2171e2c54635SPaolo Bonzini	$prototype = "";
2172e2c54635SPaolo Bonzini	$state = STATE_PROTO;
2173e2c54635SPaolo Bonzini	$brcount = 0;
2174e2c54635SPaolo Bonzini    } elsif (/$doc_content/) {
2175e2c54635SPaolo Bonzini	if ($1 eq "") {
21769f2b463aSMauro Carvalho Chehab	    if ($section eq $section_context) {
2177e2c54635SPaolo Bonzini		dump_section($file, $section, $contents);
2178e2c54635SPaolo Bonzini		$section = $section_default;
2179e2c54635SPaolo Bonzini		$contents = "";
2180e2c54635SPaolo Bonzini		$new_start_line = $.;
21819f2b463aSMauro Carvalho Chehab		$state = STATE_BODY;
2182e2c54635SPaolo Bonzini	    } else {
21839f2b463aSMauro Carvalho Chehab		if ($section ne $section_default) {
21849f2b463aSMauro Carvalho Chehab		    $state = STATE_BODY_WITH_BLANK_LINE;
21859f2b463aSMauro Carvalho Chehab		} else {
21869f2b463aSMauro Carvalho Chehab		    $state = STATE_BODY;
21879f2b463aSMauro Carvalho Chehab		}
2188e2c54635SPaolo Bonzini		$contents .= "\n";
2189e2c54635SPaolo Bonzini	    }
2190e2c54635SPaolo Bonzini	} elsif ($state == STATE_BODY_MAYBE) {
2191e2c54635SPaolo Bonzini	    # Continued declaration purpose
2192e2c54635SPaolo Bonzini	    chomp($declaration_purpose);
2193e2c54635SPaolo Bonzini	    $declaration_purpose .= " " . $1;
2194e2c54635SPaolo Bonzini	    $declaration_purpose =~ s/\s+/ /g;
2195e2c54635SPaolo Bonzini	} else {
2196e2c54635SPaolo Bonzini	    my $cont = $1;
2197e2c54635SPaolo Bonzini	    if ($section =~ m/^@/ || $section eq $section_context) {
2198e2c54635SPaolo Bonzini		if (!defined $leading_space) {
2199e2c54635SPaolo Bonzini		    if ($cont =~ m/^(\s+)/) {
2200e2c54635SPaolo Bonzini			$leading_space = $1;
2201e2c54635SPaolo Bonzini		    } else {
2202e2c54635SPaolo Bonzini			$leading_space = "";
2203e2c54635SPaolo Bonzini		    }
2204e2c54635SPaolo Bonzini		}
2205e2c54635SPaolo Bonzini		$cont =~ s/^$leading_space//;
2206e2c54635SPaolo Bonzini	    }
2207e2c54635SPaolo Bonzini	    $contents .= $cont . "\n";
2208e2c54635SPaolo Bonzini	}
2209e2c54635SPaolo Bonzini    } else {
2210e2c54635SPaolo Bonzini	# i dont know - bad line?  ignore.
2211e2c54635SPaolo Bonzini	print STDERR "${file}:$.: warning: bad line: $_";
2212e2c54635SPaolo Bonzini	++$warnings;
2213e2c54635SPaolo Bonzini    }
2214e2c54635SPaolo Bonzini}
2215e2c54635SPaolo Bonzini
2216e2c54635SPaolo Bonzini
2217e2c54635SPaolo Bonzini#
2218e2c54635SPaolo Bonzini# STATE_PROTO: reading a function/whatever prototype.
2219e2c54635SPaolo Bonzini#
2220e2c54635SPaolo Bonzinisub process_proto($$) {
2221e2c54635SPaolo Bonzini    my $file = shift;
2222e2c54635SPaolo Bonzini
2223e2c54635SPaolo Bonzini    if (/$doc_inline_oneline/) {
2224e2c54635SPaolo Bonzini	$section = $1;
2225e2c54635SPaolo Bonzini	$contents = $2;
2226e2c54635SPaolo Bonzini	if ($contents ne "") {
2227e2c54635SPaolo Bonzini	    $contents .= "\n";
2228e2c54635SPaolo Bonzini	    dump_section($file, $section, $contents);
2229e2c54635SPaolo Bonzini	    $section = $section_default;
2230e2c54635SPaolo Bonzini	    $contents = "";
2231e2c54635SPaolo Bonzini	}
2232e2c54635SPaolo Bonzini    } elsif (/$doc_inline_start/) {
2233e2c54635SPaolo Bonzini	$state = STATE_INLINE;
2234e2c54635SPaolo Bonzini	$inline_doc_state = STATE_INLINE_NAME;
2235e2c54635SPaolo Bonzini    } elsif ($decl_type eq 'function') {
2236e2c54635SPaolo Bonzini	process_proto_function($_, $file);
2237e2c54635SPaolo Bonzini    } else {
2238e2c54635SPaolo Bonzini	process_proto_type($_, $file);
2239e2c54635SPaolo Bonzini    }
2240e2c54635SPaolo Bonzini}
2241e2c54635SPaolo Bonzini
2242e2c54635SPaolo Bonzini#
2243e2c54635SPaolo Bonzini# STATE_DOCBLOCK: within a DOC: block.
2244e2c54635SPaolo Bonzini#
2245e2c54635SPaolo Bonzinisub process_docblock($$) {
2246e2c54635SPaolo Bonzini    my $file = shift;
2247e2c54635SPaolo Bonzini
2248e2c54635SPaolo Bonzini    if (/$doc_end/) {
2249e2c54635SPaolo Bonzini	dump_doc_section($file, $section, $contents);
2250e2c54635SPaolo Bonzini	$section = $section_default;
2251e2c54635SPaolo Bonzini	$contents = "";
2252e2c54635SPaolo Bonzini	$function = "";
2253e2c54635SPaolo Bonzini	%parameterdescs = ();
2254e2c54635SPaolo Bonzini	%parametertypes = ();
2255e2c54635SPaolo Bonzini	@parameterlist = ();
2256e2c54635SPaolo Bonzini	%sections = ();
2257e2c54635SPaolo Bonzini	@sectionlist = ();
2258e2c54635SPaolo Bonzini	$prototype = "";
2259e2c54635SPaolo Bonzini	$state = STATE_NORMAL;
2260e2c54635SPaolo Bonzini    } elsif (/$doc_content/) {
2261e2c54635SPaolo Bonzini	if ( $1 eq "" )	{
2262e2c54635SPaolo Bonzini	    $contents .= $blankline;
2263e2c54635SPaolo Bonzini	} else {
2264e2c54635SPaolo Bonzini	    $contents .= $1 . "\n";
2265e2c54635SPaolo Bonzini	}
2266e2c54635SPaolo Bonzini    }
2267e2c54635SPaolo Bonzini}
2268e2c54635SPaolo Bonzini
2269e2c54635SPaolo Bonzini#
2270e2c54635SPaolo Bonzini# STATE_INLINE: docbook comments within a prototype.
2271e2c54635SPaolo Bonzini#
2272e2c54635SPaolo Bonzinisub process_inline($$) {
2273e2c54635SPaolo Bonzini    my $file = shift;
2274e2c54635SPaolo Bonzini
2275e2c54635SPaolo Bonzini    # First line (state 1) needs to be a @parameter
2276e2c54635SPaolo Bonzini    if ($inline_doc_state == STATE_INLINE_NAME && /$doc_inline_sect/o) {
2277e2c54635SPaolo Bonzini	$section = $1;
2278e2c54635SPaolo Bonzini	$contents = $2;
2279e2c54635SPaolo Bonzini	$new_start_line = $.;
2280e2c54635SPaolo Bonzini	if ($contents ne "") {
2281e2c54635SPaolo Bonzini	    while (substr($contents, 0, 1) eq " ") {
2282e2c54635SPaolo Bonzini		$contents = substr($contents, 1);
2283e2c54635SPaolo Bonzini	    }
2284e2c54635SPaolo Bonzini	    $contents .= "\n";
2285e2c54635SPaolo Bonzini	}
2286e2c54635SPaolo Bonzini	$inline_doc_state = STATE_INLINE_TEXT;
2287e2c54635SPaolo Bonzini	# Documentation block end */
2288e2c54635SPaolo Bonzini    } elsif (/$doc_inline_end/) {
2289e2c54635SPaolo Bonzini	if (($contents ne "") && ($contents ne "\n")) {
2290e2c54635SPaolo Bonzini	    dump_section($file, $section, $contents);
2291e2c54635SPaolo Bonzini	    $section = $section_default;
2292e2c54635SPaolo Bonzini	    $contents = "";
2293e2c54635SPaolo Bonzini	}
2294e2c54635SPaolo Bonzini	$state = STATE_PROTO;
2295e2c54635SPaolo Bonzini	$inline_doc_state = STATE_INLINE_NA;
2296e2c54635SPaolo Bonzini	# Regular text
2297e2c54635SPaolo Bonzini    } elsif (/$doc_content/) {
2298e2c54635SPaolo Bonzini	if ($inline_doc_state == STATE_INLINE_TEXT) {
2299e2c54635SPaolo Bonzini	    $contents .= $1 . "\n";
2300e2c54635SPaolo Bonzini	    # nuke leading blank lines
2301e2c54635SPaolo Bonzini	    if ($contents =~ /^\s*$/) {
2302e2c54635SPaolo Bonzini		$contents = "";
2303e2c54635SPaolo Bonzini	    }
2304e2c54635SPaolo Bonzini	} elsif ($inline_doc_state == STATE_INLINE_NAME) {
2305e2c54635SPaolo Bonzini	    $inline_doc_state = STATE_INLINE_ERROR;
2306e2c54635SPaolo Bonzini	    print STDERR "${file}:$.: warning: ";
2307e2c54635SPaolo Bonzini	    print STDERR "Incorrect use of kernel-doc format: $_";
2308e2c54635SPaolo Bonzini	    ++$warnings;
2309e2c54635SPaolo Bonzini	}
2310e2c54635SPaolo Bonzini    }
2311e2c54635SPaolo Bonzini}
2312e2c54635SPaolo Bonzini
2313e2c54635SPaolo Bonzini
2314e2c54635SPaolo Bonzinisub process_file($) {
2315e2c54635SPaolo Bonzini    my $file;
2316e2c54635SPaolo Bonzini    my $initial_section_counter = $section_counter;
2317e2c54635SPaolo Bonzini    my ($orig_file) = @_;
2318e2c54635SPaolo Bonzini
2319e2c54635SPaolo Bonzini    $file = map_filename($orig_file);
2320e2c54635SPaolo Bonzini
23215abfaa6aSMauro Carvalho Chehab    if (!open(IN_FILE,"<$file")) {
2322e2c54635SPaolo Bonzini	print STDERR "Error: Cannot open file $file\n";
2323e2c54635SPaolo Bonzini	++$errors;
2324e2c54635SPaolo Bonzini	return;
2325e2c54635SPaolo Bonzini    }
2326e2c54635SPaolo Bonzini
2327e2c54635SPaolo Bonzini    $. = 1;
2328e2c54635SPaolo Bonzini
2329e2c54635SPaolo Bonzini    $section_counter = 0;
23305abfaa6aSMauro Carvalho Chehab    while (<IN_FILE>) {
2331e2c54635SPaolo Bonzini	while (s/\\\s*$//) {
23325abfaa6aSMauro Carvalho Chehab	    $_ .= <IN_FILE>;
2333e2c54635SPaolo Bonzini	}
2334e2c54635SPaolo Bonzini	# Replace tabs by spaces
2335e2c54635SPaolo Bonzini        while ($_ =~ s/\t+/' ' x (length($&) * 8 - length($`) % 8)/e) {};
2336e2c54635SPaolo Bonzini	# Hand this line to the appropriate state handler
2337e2c54635SPaolo Bonzini	if ($state == STATE_NORMAL) {
2338e2c54635SPaolo Bonzini	    process_normal();
2339e2c54635SPaolo Bonzini	} elsif ($state == STATE_NAME) {
2340e2c54635SPaolo Bonzini	    process_name($file, $_);
23419f2b463aSMauro Carvalho Chehab	} elsif ($state == STATE_BODY || $state == STATE_BODY_MAYBE ||
23429f2b463aSMauro Carvalho Chehab		 $state == STATE_BODY_WITH_BLANK_LINE) {
2343e2c54635SPaolo Bonzini	    process_body($file, $_);
2344e2c54635SPaolo Bonzini	} elsif ($state == STATE_INLINE) { # scanning for inline parameters
2345e2c54635SPaolo Bonzini	    process_inline($file, $_);
2346e2c54635SPaolo Bonzini	} elsif ($state == STATE_PROTO) {
2347e2c54635SPaolo Bonzini	    process_proto($file, $_);
2348e2c54635SPaolo Bonzini	} elsif ($state == STATE_DOCBLOCK) {
2349e2c54635SPaolo Bonzini	    process_docblock($file, $_);
2350e2c54635SPaolo Bonzini	}
2351e2c54635SPaolo Bonzini    }
2352e2c54635SPaolo Bonzini
2353e2c54635SPaolo Bonzini    # Make sure we got something interesting.
2354e2c54635SPaolo Bonzini    if ($initial_section_counter == $section_counter && $
2355e2c54635SPaolo Bonzini	output_mode ne "none") {
2356e2c54635SPaolo Bonzini	if ($output_selection == OUTPUT_INCLUDE) {
2357e2c54635SPaolo Bonzini	    print STDERR "${file}:1: warning: '$_' not found\n"
2358e2c54635SPaolo Bonzini		for keys %function_table;
2359e2c54635SPaolo Bonzini	}
2360e2c54635SPaolo Bonzini	else {
2361e2c54635SPaolo Bonzini	    print STDERR "${file}:1: warning: no structured comments found\n";
2362e2c54635SPaolo Bonzini	}
2363e2c54635SPaolo Bonzini    }
23645abfaa6aSMauro Carvalho Chehab    close IN_FILE;
2365e2c54635SPaolo Bonzini}
2366e2c54635SPaolo Bonzini
2367e2c54635SPaolo Bonzini
2368*486966e4SMauro Carvalho Chehabif ($output_mode eq "rst") {
2369*486966e4SMauro Carvalho Chehab	get_sphinx_version() if (!$sphinx_major);
2370*486966e4SMauro Carvalho Chehab}
2371*486966e4SMauro Carvalho Chehab
2372e2c54635SPaolo Bonzini$kernelversion = get_kernel_version();
2373e2c54635SPaolo Bonzini
2374e2c54635SPaolo Bonzini# generate a sequence of code that will splice in highlighting information
2375e2c54635SPaolo Bonzini# using the s// operator.
2376e2c54635SPaolo Bonzinifor (my $k = 0; $k < @highlights; $k++) {
2377e2c54635SPaolo Bonzini    my $pattern = $highlights[$k][0];
2378e2c54635SPaolo Bonzini    my $result = $highlights[$k][1];
2379e2c54635SPaolo Bonzini#   print STDERR "scanning pattern:$pattern, highlight:($result)\n";
2380e2c54635SPaolo Bonzini    $dohighlight .=  "\$contents =~ s:$pattern:$result:gs;\n";
2381e2c54635SPaolo Bonzini}
2382e2c54635SPaolo Bonzini
2383e2c54635SPaolo Bonzini# Read the file that maps relative names to absolute names for
2384e2c54635SPaolo Bonzini# separate source and object directories and for shadow trees.
2385e2c54635SPaolo Bonziniif (open(SOURCE_MAP, "<.tmp_filelist.txt")) {
2386e2c54635SPaolo Bonzini	my ($relname, $absname);
2387e2c54635SPaolo Bonzini	while(<SOURCE_MAP>) {
2388e2c54635SPaolo Bonzini		chop();
2389e2c54635SPaolo Bonzini		($relname, $absname) = (split())[0..1];
2390e2c54635SPaolo Bonzini		$relname =~ s:^/+::;
2391e2c54635SPaolo Bonzini		$source_map{$relname} = $absname;
2392e2c54635SPaolo Bonzini	}
2393e2c54635SPaolo Bonzini	close(SOURCE_MAP);
2394e2c54635SPaolo Bonzini}
2395e2c54635SPaolo Bonzini
2396e2c54635SPaolo Bonziniif ($output_selection == OUTPUT_EXPORTED ||
2397e2c54635SPaolo Bonzini    $output_selection == OUTPUT_INTERNAL) {
2398e2c54635SPaolo Bonzini
2399e2c54635SPaolo Bonzini    push(@export_file_list, @ARGV);
2400e2c54635SPaolo Bonzini
2401e2c54635SPaolo Bonzini    foreach (@export_file_list) {
2402e2c54635SPaolo Bonzini	chomp;
2403e2c54635SPaolo Bonzini	process_export_file($_);
2404e2c54635SPaolo Bonzini    }
2405e2c54635SPaolo Bonzini}
2406e2c54635SPaolo Bonzini
2407e2c54635SPaolo Bonziniforeach (@ARGV) {
2408e2c54635SPaolo Bonzini    chomp;
2409e2c54635SPaolo Bonzini    process_file($_);
2410e2c54635SPaolo Bonzini}
2411e2c54635SPaolo Bonziniif ($verbose && $errors) {
2412e2c54635SPaolo Bonzini  print STDERR "$errors errors\n";
2413e2c54635SPaolo Bonzini}
2414e2c54635SPaolo Bonziniif ($verbose && $warnings) {
2415e2c54635SPaolo Bonzini  print STDERR "$warnings warnings\n";
2416e2c54635SPaolo Bonzini}
2417e2c54635SPaolo Bonzini
241886cba217SPierre-Louis Bossartif ($Werror && $warnings) {
241986cba217SPierre-Louis Bossart    print STDERR "$warnings warnings as Errors\n";
242086cba217SPierre-Louis Bossart    exit($warnings);
242186cba217SPierre-Louis Bossart} else {
242286cba217SPierre-Louis Bossart    exit($output_mode eq "none" ? 0 : $errors)
242386cba217SPierre-Louis Bossart}
2424