blob: a27c7016f72def376fef7060116edec80ed4adf1 [file] [log] [blame]
Kamil Rytarowskicb77f0d2017-05-07 23:25:26 +02001#!/usr/bin/env perl
Linus Torvalds1da177e2005-04-16 15:20:36 -07002
Kamil Rytarowskicb77f0d2017-05-07 23:25:26 +02003use warnings;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004use strict;
5
6## Copyright (c) 1998 Michael Zucchi, All Rights Reserved ##
7## Copyright (C) 2000, 1 Tim Waugh <twaugh@redhat.com> ##
8## Copyright (C) 2001 Simon Huggins ##
Randy Dunlap70c95b02012-01-21 10:31:54 -08009## Copyright (C) 2005-2012 Randy Dunlap ##
Dan Luedtke1b40c192012-08-12 10:46:15 +020010## Copyright (C) 2012 Dan Luedtke ##
Linus Torvalds1da177e2005-04-16 15:20:36 -070011## ##
12## #define enhancements by Armin Kuster <akuster@mvista.com> ##
13## Copyright (c) 2000 MontaVista Software, Inc. ##
14## ##
15## This software falls under the GNU General Public License. ##
16## Please read the COPYING file for more information ##
17
Linus Torvalds1da177e2005-04-16 15:20:36 -070018# 18/01/2001 - Cleanups
19# Functions prototyped as foo(void) same as foo()
20# Stop eval'ing where we don't need to.
21# -- huggie@earth.li
22
23# 27/06/2001 - Allowed whitespace after initial "/**" and
24# allowed comments before function declarations.
25# -- Christian Kreibich <ck@whoop.org>
26
27# Still to do:
28# - add perldoc documentation
29# - Look more closely at some of the scarier bits :)
30
31# 26/05/2001 - Support for separate source and object trees.
32# Return error code.
33# Keith Owens <kaos@ocs.com.au>
34
35# 23/09/2001 - Added support for typedefs, structs, enums and unions
36# Support for Context section; can be terminated using empty line
37# Small fixes (like spaces vs. \s in regex)
38# -- Tim Jansen <tim@tjansen.de>
39
Dan Luedtke1b40c192012-08-12 10:46:15 +020040# 25/07/2012 - Added support for HTML5
41# -- Dan Luedtke <mail@danrl.de>
Linus Torvalds1da177e2005-04-16 15:20:36 -070042
Jani Nikulafadc0b32016-05-12 16:15:36 +030043sub usage {
44 my $message = <<"EOF";
45Usage: $0 [OPTION ...] FILE ...
Linus Torvalds1da177e2005-04-16 15:20:36 -070046
Jani Nikulafadc0b32016-05-12 16:15:36 +030047Read C language source or header FILEs, extract embedded documentation comments,
48and print formatted documentation to standard output.
Linus Torvalds1da177e2005-04-16 15:20:36 -070049
Jani Nikulafadc0b32016-05-12 16:15:36 +030050The documentation comments are identified by "/**" opening comment mark. See
Mauro Carvalho Chehab857af3b2017-12-18 10:30:08 -020051Documentation/doc-guide/kernel-doc.rst for the documentation comment syntax.
Jani Nikulafadc0b32016-05-12 16:15:36 +030052
53Output format selection (mutually exclusive):
Jani Nikulafadc0b32016-05-12 16:15:36 +030054 -man Output troff manual page format. This is the default.
Jonathan Corbetc0d1b6e2016-05-12 16:15:37 +030055 -rst Output reStructuredText format.
Matthew Wilcox3a025e12017-11-20 10:40:40 -080056 -none Do not output documentation, only warnings.
Jani Nikulafadc0b32016-05-12 16:15:36 +030057
58Output selection (mutually exclusive):
Jani Nikula86ae2e32016-01-21 13:05:22 +020059 -export Only output documentation for symbols that have been
60 exported using EXPORT_SYMBOL() or EXPORT_SYMBOL_GPL()
Jani Nikulac9b2cfb2016-06-07 11:05:53 +030061 in any input FILE or -export-file FILE.
Jani Nikula86ae2e32016-01-21 13:05:22 +020062 -internal Only output documentation for symbols that have NOT been
63 exported using EXPORT_SYMBOL() or EXPORT_SYMBOL_GPL()
Jani Nikulac9b2cfb2016-06-07 11:05:53 +030064 in any input FILE or -export-file FILE.
Jani Nikulafadc0b32016-05-12 16:15:36 +030065 -function NAME Only output documentation for the given function(s)
66 or DOC: section title(s). All other functions and DOC:
67 sections are ignored. May be specified multiple times.
68 -nofunction NAME Do NOT output documentation for the given function(s);
69 only output documentation for the other functions and
70 DOC: sections. May be specified multiple times.
71
72Output selection modifiers:
73 -no-doc-sections Do not output DOC: sections.
Daniel Vetter0b0f5f292016-06-03 22:21:34 +020074 -enable-lineno Enable output of #define LINENO lines. Only works with
75 reStructuredText format.
Jani Nikula88c2b572016-06-07 11:00:52 +030076 -export-file FILE Specify an additional FILE in which to look for
77 EXPORT_SYMBOL() and EXPORT_SYMBOL_GPL(). To be used with
78 -export or -internal. May be specified multiple times.
Jani Nikulafadc0b32016-05-12 16:15:36 +030079
80Other parameters:
81 -v Verbose output, more warnings and other information.
82 -h Print this help.
83
84EOF
85 print $message;
86 exit 1;
87}
Linus Torvalds1da177e2005-04-16 15:20:36 -070088
89#
90# format of comments.
91# In the following table, (...)? signifies optional structure.
92# (...)* signifies 0 or more structure elements
93# /**
94# * function_name(:)? (- short description)?
95# (* @parameterx: (description of parameter x)?)*
96# (* a blank line)?
97# * (Description:)? (Description of function)?
98# * (section header: (section description)? )*
99# (*)?*/
100#
101# So .. the trivial example would be:
102#
103# /**
104# * my_function
Randy Dunlapb9d973282009-06-09 08:50:38 -0700105# */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106#
Randy Dunlap891dcd22007-02-10 01:45:53 -0800107# If the Description: header tag is omitted, then there must be a blank line
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108# after the last parameter specification.
109# e.g.
110# /**
111# * my_function - does my stuff
112# * @my_arg: its mine damnit
113# *
Randy Dunlap3c3b8092006-02-01 03:06:58 -0800114# * Does my stuff explained.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115# */
116#
117# or, could also use:
118# /**
119# * my_function - does my stuff
120# * @my_arg: its mine damnit
Randy Dunlap3c3b8092006-02-01 03:06:58 -0800121# * Description: Does my stuff explained.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122# */
123# etc.
124#
Randy Dunlapb9d973282009-06-09 08:50:38 -0700125# Besides functions you can also write documentation for structs, unions,
Randy Dunlap3c3b8092006-02-01 03:06:58 -0800126# enums and typedefs. Instead of the function name you must write the name
127# of the declaration; the struct/union/enum/typedef must always precede
128# the name. Nesting of declarations is not supported.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129# Use the argument mechanism to document members or constants.
130# e.g.
131# /**
132# * struct my_struct - short description
133# * @a: first member
134# * @b: second member
Randy Dunlap3c3b8092006-02-01 03:06:58 -0800135# *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136# * Longer description
137# */
138# struct my_struct {
139# int a;
140# int b;
Martin Waitzaeec46b2005-11-13 16:08:13 -0800141# /* private: */
142# int c;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143# };
144#
145# All descriptions can be multiline, except the short function description.
Randy Dunlap3c3b8092006-02-01 03:06:58 -0800146#
Danilo Cesar Lemes de Paulaa4c6ebe2015-08-04 09:04:08 -0300147# For really longs structs, you can also describe arguments inside the
148# body of the struct.
149# eg.
150# /**
151# * struct my_struct - short description
152# * @a: first member
153# * @b: second member
154# *
155# * Longer description
156# */
157# struct my_struct {
158# int a;
159# int b;
160# /**
161# * @c: This is longer description of C
162# *
163# * You can use paragraphs to describe arguments
164# * using this method.
165# */
166# int c;
167# };
168#
169# This should be use only for struct/enum members.
170#
Randy Dunlap3c3b8092006-02-01 03:06:58 -0800171# You can also add additional sections. When documenting kernel functions you
172# should document the "Context:" of the function, e.g. whether the functions
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173# can be called form interrupts. Unlike other sections you can end it with an
Randy Dunlap3c3b8092006-02-01 03:06:58 -0800174# empty line.
Yacine Belkadi4092bac2012-11-26 22:22:27 +0100175# A non-void function should have a "Return:" section describing the return
176# value(s).
Randy Dunlap3c3b8092006-02-01 03:06:58 -0800177# Example-sections should contain the string EXAMPLE so that they are marked
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178# appropriately in DocBook.
179#
180# Example:
181# /**
182# * user_function - function that can only be called in user context
183# * @a: some argument
184# * Context: !in_interrupt()
Randy Dunlap3c3b8092006-02-01 03:06:58 -0800185# *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186# * Some description
187# * Example:
188# * user_function(22);
189# */
190# ...
191#
192#
193# All descriptive text is further processed, scanning for the following special
194# patterns, which are highlighted appropriately.
195#
196# 'funcname()' - function
197# '$ENVVAR' - environmental variable
198# '&struct_name' - name of a structure (up to two words including 'struct')
Paolo Bonzini5267dd32017-01-02 16:22:26 +0100199# '&struct_name.member' - name of a structure member
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200# '@parameter' - name of a parameter
201# '%CONST' - name of a constant.
Mauro Carvalho Chehabb97f1932017-03-30 17:11:28 -0300202# '``LITERAL``' - literal string without any spaces on it.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203
Randy Dunlap8484baa2011-01-05 16:28:43 -0800204## init lots of data
205
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206my $errors = 0;
207my $warnings = 0;
Randy Dunlap5f8c7c92007-07-19 01:48:24 -0700208my $anon_struct_union = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209
210# match expressions used to find embedded type information
Mauro Carvalho Chehabb97f1932017-03-30 17:11:28 -0300211my $type_constant = '\b``([^\`]+)``\b';
212my $type_constant2 = '\%([-_\w]+)';
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213my $type_func = '(\w+)\(\)';
Mauro Carvalho Chehab8ad72162017-12-18 10:30:13 -0200214my $type_param = '\@(\w*(\.\w+)*(\.\.\.)?)';
Jonathan Corbet5219f182016-08-24 16:31:15 -0600215my $type_fp_param = '\@(\w+)\(\)'; # Special RST handling for func ptr params
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216my $type_env = '(\$\w+)';
Paolo Bonzinidf311752017-01-02 16:22:27 +0100217my $type_enum = '\&(enum\s*([_\w]+))';
218my $type_struct = '\&(struct\s*([_\w]+))';
219my $type_typedef = '\&(typedef\s*([_\w]+))';
220my $type_union = '\&(union\s*([_\w]+))';
Paolo Bonzini5267dd32017-01-02 16:22:26 +0100221my $type_member = '\&([_\w]+)(\.|->)([_\w]+)';
Paolo Bonzinidf311752017-01-02 16:22:27 +0100222my $type_fallback = '\&([_\w]+)';
Jani Nikulaf3341dc2016-05-26 16:35:02 +0300223my $type_member_func = $type_member . '\(\)';
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224
225# Output conversion substitutions.
226# One for each output format
227
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228# these are pretty rough
Danilo Cesar Lemes de Paula4d732702015-09-07 17:01:59 -0300229my @highlights_man = (
230 [$type_constant, "\$1"],
Mauro Carvalho Chehabb97f1932017-03-30 17:11:28 -0300231 [$type_constant2, "\$1"],
Danilo Cesar Lemes de Paula4d732702015-09-07 17:01:59 -0300232 [$type_func, "\\\\fB\$1\\\\fP"],
Paolo Bonzinidf311752017-01-02 16:22:27 +0100233 [$type_enum, "\\\\fI\$1\\\\fP"],
Danilo Cesar Lemes de Paula4d732702015-09-07 17:01:59 -0300234 [$type_struct, "\\\\fI\$1\\\\fP"],
Paolo Bonzinidf311752017-01-02 16:22:27 +0100235 [$type_typedef, "\\\\fI\$1\\\\fP"],
236 [$type_union, "\\\\fI\$1\\\\fP"],
Paolo Bonzini5267dd32017-01-02 16:22:26 +0100237 [$type_param, "\\\\fI\$1\\\\fP"],
Paolo Bonzinidf311752017-01-02 16:22:27 +0100238 [$type_member, "\\\\fI\$1\$2\$3\\\\fP"],
239 [$type_fallback, "\\\\fI\$1\\\\fP"]
Danilo Cesar Lemes de Paula4d732702015-09-07 17:01:59 -0300240 );
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241my $blankline_man = "";
242
Jonathan Corbetc0d1b6e2016-05-12 16:15:37 +0300243# rst-mode
244my @highlights_rst = (
245 [$type_constant, "``\$1``"],
Mauro Carvalho Chehabb97f1932017-03-30 17:11:28 -0300246 [$type_constant2, "``\$1``"],
Jani Nikulaf3341dc2016-05-26 16:35:02 +0300247 # Note: need to escape () to avoid func matching later
Paolo Bonzini5267dd32017-01-02 16:22:26 +0100248 [$type_member_func, "\\:c\\:type\\:`\$1\$2\$3\\\\(\\\\) <\$1>`"],
249 [$type_member, "\\:c\\:type\\:`\$1\$2\$3 <\$1>`"],
Jonathan Corbet5219f182016-08-24 16:31:15 -0600250 [$type_fp_param, "**\$1\\\\(\\\\)**"],
Jani Nikulaa19bce62016-05-26 11:28:16 +0300251 [$type_func, "\\:c\\:func\\:`\$1()`"],
Paolo Bonzinidf311752017-01-02 16:22:27 +0100252 [$type_enum, "\\:c\\:type\\:`\$1 <\$2>`"],
253 [$type_struct, "\\:c\\:type\\:`\$1 <\$2>`"],
254 [$type_typedef, "\\:c\\:type\\:`\$1 <\$2>`"],
255 [$type_union, "\\:c\\:type\\:`\$1 <\$2>`"],
Jani Nikulaa7291e72016-05-26 13:57:06 +0300256 # in rst this can refer to any type
Paolo Bonzinidf311752017-01-02 16:22:27 +0100257 [$type_fallback, "\\:c\\:type\\:`\$1`"],
Jonathan Corbetc0d1b6e2016-05-12 16:15:37 +0300258 [$type_param, "**\$1**"]
259 );
260my $blankline_rst = "\n";
261
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262# read arguments
Randy Dunlapb9d973282009-06-09 08:50:38 -0700263if ($#ARGV == -1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 usage();
265}
266
Randy Dunlap8484baa2011-01-05 16:28:43 -0800267my $kernelversion;
268my $dohighlight = "";
269
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270my $verbose = 0;
Mauro Carvalho Chehabbdfe2be2017-12-18 10:30:11 -0200271my $output_mode = "rst";
Daniel Santose314ba32012-10-04 17:15:08 -0700272my $output_preformatted = 0;
Johannes Berg4b445952007-10-24 15:08:48 -0700273my $no_doc_sections = 0;
Daniel Vetter0b0f5f292016-06-03 22:21:34 +0200274my $enable_lineno = 0;
Mauro Carvalho Chehabbdfe2be2017-12-18 10:30:11 -0200275my @highlights = @highlights_rst;
276my $blankline = $blankline_rst;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277my $modulename = "Kernel API";
Jani Nikulab6c3f452016-05-29 22:19:35 +0300278
279use constant {
280 OUTPUT_ALL => 0, # output all symbols and doc sections
281 OUTPUT_INCLUDE => 1, # output only specified symbols
282 OUTPUT_EXCLUDE => 2, # output everything except specified symbols
283 OUTPUT_EXPORTED => 3, # output exported symbols
284 OUTPUT_INTERNAL => 4, # output non-exported symbols
285};
286my $output_selection = OUTPUT_ALL;
Ben Hutchingsb2c41052015-07-08 20:07:16 +0100287my $show_not_found = 0;
288
Jani Nikula88c2b572016-06-07 11:00:52 +0300289my @export_file_list;
290
Ben Hutchingsb2c41052015-07-08 20:07:16 +0100291my @build_time;
292if (defined($ENV{'KBUILD_BUILD_TIMESTAMP'}) &&
293 (my $seconds = `date -d"${ENV{'KBUILD_BUILD_TIMESTAMP'}}" +%s`) ne '') {
294 @build_time = gmtime($seconds);
295} else {
296 @build_time = localtime;
297}
298
Randy Dunlap3c3b8092006-02-01 03:06:58 -0800299my $man_date = ('January', 'February', 'March', 'April', 'May', 'June',
300 'July', 'August', 'September', 'October',
Ben Hutchingsb2c41052015-07-08 20:07:16 +0100301 'November', 'December')[$build_time[4]] .
302 " " . ($build_time[5]+1900);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303
Randy Dunlap8484baa2011-01-05 16:28:43 -0800304# Essentially these are globals.
Randy Dunlapb9d973282009-06-09 08:50:38 -0700305# They probably want to be tidied up, made more localised or something.
306# CAVEAT EMPTOR! Some of the others I localised may not want to be, which
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307# could cause "use of undefined value" or other bugs.
Randy Dunlapb9d973282009-06-09 08:50:38 -0700308my ($function, %function_table, %parametertypes, $declaration_purpose);
Daniel Vetter0b0f5f292016-06-03 22:21:34 +0200309my $declaration_start_line;
Randy Dunlapb9d973282009-06-09 08:50:38 -0700310my ($type, $declaration_name, $return_type);
Ilya Dryomov1c32fd02010-02-26 13:06:03 -0800311my ($newsection, $newcontents, $prototype, $brcount, %source_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312
Randy Dunlapbd0e88e2008-03-13 12:32:43 -0700313if (defined($ENV{'KBUILD_VERBOSE'})) {
314 $verbose = "$ENV{'KBUILD_VERBOSE'}";
315}
316
Randy Dunlap3c3b8092006-02-01 03:06:58 -0800317# Generated docbook code is inserted in a template at a point where
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318# docbook v3.1 requires a non-zero sequence of RefEntry's; see:
319# http://www.oasis-open.org/docbook/documentation/reference/html/refentry.html
320# We keep track of number of generated entries and generate a dummy
321# if needs be to ensure the expanded template can be postprocessed
322# into html.
323my $section_counter = 0;
324
325my $lineprefix="";
326
Jani Nikula48af6062016-05-26 14:56:05 +0300327# Parser states
328use constant {
329 STATE_NORMAL => 0, # normal code
330 STATE_NAME => 1, # looking for function name
Jonathan Corbet17b78712018-02-05 13:56:45 -0700331 STATE_BODY_MAYBE => 2, # body - or maybe more description
332 STATE_BODY => 3, # the body of the comment
333 STATE_PROTO => 4, # scanning prototype
334 STATE_DOCBLOCK => 5, # documentation block
335 STATE_INLINE => 6, # gathering documentation outside main block
Jani Nikula48af6062016-05-26 14:56:05 +0300336};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337my $state;
Randy Dunlap850622d2006-06-25 05:48:55 -0700338my $in_doc_sect;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339
Jani Nikula48af6062016-05-26 14:56:05 +0300340# Inline documentation state
341use constant {
342 STATE_INLINE_NA => 0, # not applicable ($state != STATE_INLINE)
343 STATE_INLINE_NAME => 1, # looking for member name (@foo:)
344 STATE_INLINE_TEXT => 2, # looking for member documentation
345 STATE_INLINE_END => 3, # done
346 STATE_INLINE_ERROR => 4, # error - Comment without header was found.
347 # Spit a warning as it's not
348 # proper kernel-doc and ignore the rest.
349};
350my $inline_doc_state;
Danilo Cesar Lemes de Paulaa4c6ebe2015-08-04 09:04:08 -0300351
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352#declaration types: can be
353# 'function', 'struct', 'union', 'enum', 'typedef'
354my $decl_type;
355
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356my $doc_start = '^/\*\*\s*$'; # Allow whitespace at end of comment start.
357my $doc_end = '\*/';
358my $doc_com = '\s*\*\s*';
Daniel Santos12ae6772012-10-04 17:15:10 -0700359my $doc_com_body = '\s*\* ?';
Randy Dunlapb9d973282009-06-09 08:50:38 -0700360my $doc_decl = $doc_com . '(\w+)';
Jani Nikulaf624ade2016-05-29 11:35:28 +0300361# @params and a strictly limited set of supported section names
Jonathan Corbet8569de62016-06-09 13:35:05 -0600362my $doc_sect = $doc_com .
Jonathan Corbetef000282016-08-26 07:14:08 -0600363 '\s*(\@[.\w]+|\@\.\.\.|description|context|returns?|notes?|examples?)\s*:(.*)';
Daniel Santos12ae6772012-10-04 17:15:10 -0700364my $doc_content = $doc_com_body . '(.*)';
Randy Dunlapb9d973282009-06-09 08:50:38 -0700365my $doc_block = $doc_com . 'DOC:\s*(.*)?';
Jani Nikula48af6062016-05-26 14:56:05 +0300366my $doc_inline_start = '^\s*/\*\*\s*$';
367my $doc_inline_sect = '\s*\*\s*(@[\w\s]+):(.*)';
368my $doc_inline_end = '^\s*\*/\s*$';
Jani Nikula0c9aa202016-11-16 17:26:16 +0200369my $doc_inline_oneline = '^\s*/\*\*\s*(@[\w\s]+):\s*(.*)\s*\*/\s*$';
Jani Nikula86ae2e32016-01-21 13:05:22 +0200370my $export_symbol = '^\s*EXPORT_SYMBOL(_GPL)?\s*\(\s*(\w+)\s*\)\s*;';
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372my %parameterdescs;
Daniel Vetter0b0f5f292016-06-03 22:21:34 +0200373my %parameterdesc_start_lines;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374my @parameterlist;
375my %sections;
376my @sectionlist;
Daniel Vetter0b0f5f292016-06-03 22:21:34 +0200377my %section_start_lines;
Randy Dunlapa1d94aa2008-12-19 08:49:30 -0800378my $sectcheck;
379my $struct_actual;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380
381my $contents = "";
Daniel Vetter0b0f5f292016-06-03 22:21:34 +0200382my $new_start_line = 0;
Jani Nikulaf624ade2016-05-29 11:35:28 +0300383
384# the canonical section names. see also $doc_sect above.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385my $section_default = "Description"; # default section
386my $section_intro = "Introduction";
387my $section = $section_default;
388my $section_context = "Context";
Yacine Belkadi4092bac2012-11-26 22:22:27 +0100389my $section_return = "Return";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390
391my $undescribed = "-- undescribed --";
392
393reset_state();
394
Mauro Carvalho Chehabb031ac42017-12-18 10:30:10 -0200395while ($ARGV[0] =~ m/^--?(.*)/) {
396 my $cmd = $1;
397 shift @ARGV;
398 if ($cmd eq "man") {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 $output_mode = "man";
Danilo Cesar Lemes de Paula4d732702015-09-07 17:01:59 -0300400 @highlights = @highlights_man;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 $blankline = $blankline_man;
Mauro Carvalho Chehabb031ac42017-12-18 10:30:10 -0200402 } elsif ($cmd eq "rst") {
Jonathan Corbetc0d1b6e2016-05-12 16:15:37 +0300403 $output_mode = "rst";
404 @highlights = @highlights_rst;
405 $blankline = $blankline_rst;
Mauro Carvalho Chehabb031ac42017-12-18 10:30:10 -0200406 } elsif ($cmd eq "none") {
Matthew Wilcox3a025e12017-11-20 10:40:40 -0800407 $output_mode = "none";
Mauro Carvalho Chehabb031ac42017-12-18 10:30:10 -0200408 } elsif ($cmd eq "module") { # not needed for XML, inherits from calling document
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 $modulename = shift @ARGV;
Mauro Carvalho Chehabb031ac42017-12-18 10:30:10 -0200410 } elsif ($cmd eq "function") { # to only output specific functions
Jani Nikulab6c3f452016-05-29 22:19:35 +0300411 $output_selection = OUTPUT_INCLUDE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 $function = shift @ARGV;
413 $function_table{$function} = 1;
Mauro Carvalho Chehabb031ac42017-12-18 10:30:10 -0200414 } elsif ($cmd eq "nofunction") { # output all except specific functions
Jani Nikulab6c3f452016-05-29 22:19:35 +0300415 $output_selection = OUTPUT_EXCLUDE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 $function = shift @ARGV;
417 $function_table{$function} = 1;
Mauro Carvalho Chehabb031ac42017-12-18 10:30:10 -0200418 } elsif ($cmd eq "export") { # only exported symbols
Jani Nikulab6c3f452016-05-29 22:19:35 +0300419 $output_selection = OUTPUT_EXPORTED;
Jani Nikulada9726e2016-06-07 10:29:59 +0300420 %function_table = ();
Mauro Carvalho Chehabb031ac42017-12-18 10:30:10 -0200421 } elsif ($cmd eq "internal") { # only non-exported symbols
Jani Nikulab6c3f452016-05-29 22:19:35 +0300422 $output_selection = OUTPUT_INTERNAL;
Jani Nikulada9726e2016-06-07 10:29:59 +0300423 %function_table = ();
Mauro Carvalho Chehabb031ac42017-12-18 10:30:10 -0200424 } elsif ($cmd eq "export-file") {
Jani Nikula88c2b572016-06-07 11:00:52 +0300425 my $file = shift @ARGV;
426 push(@export_file_list, $file);
Mauro Carvalho Chehabb031ac42017-12-18 10:30:10 -0200427 } elsif ($cmd eq "v") {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 $verbose = 1;
Mauro Carvalho Chehabb031ac42017-12-18 10:30:10 -0200429 } elsif (($cmd eq "h") || ($cmd eq "help")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 usage();
Mauro Carvalho Chehabb031ac42017-12-18 10:30:10 -0200431 } elsif ($cmd eq 'no-doc-sections') {
Johannes Berg4b445952007-10-24 15:08:48 -0700432 $no_doc_sections = 1;
Mauro Carvalho Chehabb031ac42017-12-18 10:30:10 -0200433 } elsif ($cmd eq 'enable-lineno') {
Daniel Vetter0b0f5f292016-06-03 22:21:34 +0200434 $enable_lineno = 1;
Mauro Carvalho Chehabb031ac42017-12-18 10:30:10 -0200435 } elsif ($cmd eq 'show-not-found') {
Johannes Berge946c43a2013-11-12 15:11:12 -0800436 $show_not_found = 1;
Mauro Carvalho Chehabb031ac42017-12-18 10:30:10 -0200437 } else {
438 # Unknown argument
439 usage();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 }
441}
442
Randy Dunlap8484baa2011-01-05 16:28:43 -0800443# continue execution near EOF;
444
Borislav Petkov53f049f2007-05-08 00:30:54 -0700445# get kernel version from env
446sub get_kernel_version() {
Johannes Berg1b9bc222007-10-24 15:08:48 -0700447 my $version = 'unknown kernel version';
Borislav Petkov53f049f2007-05-08 00:30:54 -0700448
449 if (defined($ENV{'KERNELVERSION'})) {
450 $version = $ENV{'KERNELVERSION'};
451 }
452 return $version;
453}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454
Daniel Vetter0b0f5f292016-06-03 22:21:34 +0200455#
456sub print_lineno {
457 my $lineno = shift;
458 if ($enable_lineno && defined($lineno)) {
459 print "#define LINENO " . $lineno . "\n";
460 }
461}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462##
463# dumps section contents to arrays/hashes intended for that purpose.
464#
465sub dump_section {
Randy Dunlap94dc7ad2008-04-28 02:16:34 -0700466 my $file = shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 my $name = shift;
468 my $contents = join "\n", @_;
469
Jani Nikula13901ef2016-05-26 08:57:29 +0300470 if ($name =~ m/$type_param/) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 $name = $1;
472 $parameterdescs{$name} = $contents;
Randy Dunlapa1d94aa2008-12-19 08:49:30 -0800473 $sectcheck = $sectcheck . $name . " ";
Daniel Vetter0b0f5f292016-06-03 22:21:34 +0200474 $parameterdesc_start_lines{$name} = $new_start_line;
475 $new_start_line = 0;
Randy Dunlapced69092008-12-01 13:14:03 -0800476 } elsif ($name eq "@\.\.\.") {
Randy Dunlapced69092008-12-01 13:14:03 -0800477 $name = "...";
478 $parameterdescs{$name} = $contents;
Randy Dunlapa1d94aa2008-12-19 08:49:30 -0800479 $sectcheck = $sectcheck . $name . " ";
Daniel Vetter0b0f5f292016-06-03 22:21:34 +0200480 $parameterdesc_start_lines{$name} = $new_start_line;
481 $new_start_line = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 } else {
Randy Dunlap94dc7ad2008-04-28 02:16:34 -0700483 if (defined($sections{$name}) && ($sections{$name} ne "")) {
Jani Nikula95b6be92016-06-10 11:14:05 +0300484 # Only warn on user specified duplicate section names.
485 if ($name ne $section_default) {
486 print STDERR "${file}:$.: warning: duplicate section name '$name'\n";
487 ++$warnings;
488 }
Jani Nikula32217762016-05-29 09:40:44 +0300489 $sections{$name} .= $contents;
490 } else {
491 $sections{$name} = $contents;
492 push @sectionlist, $name;
Daniel Vetter0b0f5f292016-06-03 22:21:34 +0200493 $section_start_lines{$name} = $new_start_line;
494 $new_start_line = 0;
Randy Dunlap94dc7ad2008-04-28 02:16:34 -0700495 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 }
497}
498
499##
Johannes Bergb112e0f2007-10-24 15:08:48 -0700500# dump DOC: section after checking that it should go out
501#
502sub dump_doc_section {
Randy Dunlap94dc7ad2008-04-28 02:16:34 -0700503 my $file = shift;
Johannes Bergb112e0f2007-10-24 15:08:48 -0700504 my $name = shift;
505 my $contents = join "\n", @_;
506
Johannes Berg4b445952007-10-24 15:08:48 -0700507 if ($no_doc_sections) {
508 return;
509 }
510
Jani Nikulab6c3f452016-05-29 22:19:35 +0300511 if (($output_selection == OUTPUT_ALL) ||
512 ($output_selection == OUTPUT_INCLUDE &&
513 defined($function_table{$name})) ||
514 ($output_selection == OUTPUT_EXCLUDE &&
515 !defined($function_table{$name})))
Johannes Bergb112e0f2007-10-24 15:08:48 -0700516 {
Randy Dunlap94dc7ad2008-04-28 02:16:34 -0700517 dump_section($file, $name, $contents);
Johannes Bergb112e0f2007-10-24 15:08:48 -0700518 output_blockhead({'sectionlist' => \@sectionlist,
519 'sections' => \%sections,
520 'module' => $modulename,
Jani Nikulab6c3f452016-05-29 22:19:35 +0300521 'content-only' => ($output_selection != OUTPUT_ALL), });
Johannes Bergb112e0f2007-10-24 15:08:48 -0700522 }
523}
524
525##
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526# output function
527#
528# parameterdescs, a hash.
529# function => "function name"
530# parameterlist => @list of parameters
531# parameterdescs => %parameter descriptions
532# sectionlist => @list of sections
Randy Dunlapa21217d2007-02-10 01:46:04 -0800533# sections => %section descriptions
Randy Dunlap3c3b8092006-02-01 03:06:58 -0800534#
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535
536sub output_highlight {
537 my $contents = join "\n",@_;
538 my $line;
539
540# DEBUG
541# if (!defined $contents) {
542# use Carp;
543# confess "output_highlight got called with no args?\n";
544# }
545
Randy Dunlap3eb014a2007-05-08 00:29:51 -0700546# print STDERR "contents b4:$contents\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 eval $dohighlight;
548 die $@ if $@;
Randy Dunlap3eb014a2007-05-08 00:29:51 -0700549# print STDERR "contents af:$contents\n";
550
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 foreach $line (split "\n", $contents) {
Daniel Santos12ae6772012-10-04 17:15:10 -0700552 if (! $output_preformatted) {
553 $line =~ s/^\s*//;
554 }
Randy Dunlap3c308792007-05-08 00:24:39 -0700555 if ($line eq ""){
Daniel Santose314ba32012-10-04 17:15:08 -0700556 if (! $output_preformatted) {
Jonathan Corbet0bba9242018-02-05 12:40:15 -0700557 print $lineprefix, $blankline;
Daniel Santose314ba32012-10-04 17:15:08 -0700558 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 } else {
Randy Dunlapcdccb312007-07-19 01:48:25 -0700560 if ($output_mode eq "man" && substr($line, 0, 1) eq ".") {
561 print "\\&$line";
562 } else {
563 print $lineprefix, $line;
564 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 }
566 print "\n";
567 }
568}
569
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570##
571# output function in man
572sub output_function_man(%) {
573 my %args = %{$_[0]};
574 my ($parameter, $section);
575 my $count;
576
577 print ".TH \"$args{'function'}\" 9 \"$args{'function'}\" \"$man_date\" \"Kernel Hacker's Manual\" LINUX\n";
578
579 print ".SH NAME\n";
Randy Dunlapb9d973282009-06-09 08:50:38 -0700580 print $args{'function'} . " \\- " . $args{'purpose'} . "\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581
582 print ".SH SYNOPSIS\n";
Randy Dunlapa21217d2007-02-10 01:46:04 -0800583 if ($args{'functiontype'} ne "") {
Randy Dunlapb9d973282009-06-09 08:50:38 -0700584 print ".B \"" . $args{'functiontype'} . "\" " . $args{'function'} . "\n";
Randy Dunlapa21217d2007-02-10 01:46:04 -0800585 } else {
Randy Dunlapb9d973282009-06-09 08:50:38 -0700586 print ".B \"" . $args{'function'} . "\n";
Randy Dunlapa21217d2007-02-10 01:46:04 -0800587 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 $count = 0;
589 my $parenth = "(";
590 my $post = ",";
591 foreach my $parameter (@{$args{'parameterlist'}}) {
592 if ($count == $#{$args{'parameterlist'}}) {
593 $post = ");";
594 }
595 $type = $args{'parametertypes'}{$parameter};
596 if ($type =~ m/([^\(]*\(\*)\s*\)\s*\(([^\)]*)\)/) {
597 # pointer-to-function
Randy Dunlapb9d973282009-06-09 08:50:38 -0700598 print ".BI \"" . $parenth . $1 . "\" " . $parameter . " \") (" . $2 . ")" . $post . "\"\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 } else {
600 $type =~ s/([^\*])$/$1 /;
Randy Dunlapb9d973282009-06-09 08:50:38 -0700601 print ".BI \"" . $parenth . $type . "\" " . $parameter . " \"" . $post . "\"\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 }
603 $count++;
604 $parenth = "";
605 }
606
607 print ".SH ARGUMENTS\n";
608 foreach $parameter (@{$args{'parameterlist'}}) {
609 my $parameter_name = $parameter;
610 $parameter_name =~ s/\[.*//;
611
Randy Dunlapb9d973282009-06-09 08:50:38 -0700612 print ".IP \"" . $parameter . "\" 12\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 output_highlight($args{'parameterdescs'}{$parameter_name});
614 }
615 foreach $section (@{$args{'sectionlist'}}) {
616 print ".SH \"", uc $section, "\"\n";
617 output_highlight($args{'sections'}{$section});
618 }
619}
620
621##
622# output enum in man
623sub output_enum_man(%) {
624 my %args = %{$_[0]};
625 my ($parameter, $section);
626 my $count;
627
628 print ".TH \"$args{'module'}\" 9 \"enum $args{'enum'}\" \"$man_date\" \"API Manual\" LINUX\n";
629
630 print ".SH NAME\n";
Randy Dunlapb9d973282009-06-09 08:50:38 -0700631 print "enum " . $args{'enum'} . " \\- " . $args{'purpose'} . "\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632
633 print ".SH SYNOPSIS\n";
Randy Dunlapb9d973282009-06-09 08:50:38 -0700634 print "enum " . $args{'enum'} . " {\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 $count = 0;
636 foreach my $parameter (@{$args{'parameterlist'}}) {
Randy Dunlap3c308792007-05-08 00:24:39 -0700637 print ".br\n.BI \" $parameter\"\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 if ($count == $#{$args{'parameterlist'}}) {
639 print "\n};\n";
640 last;
641 }
642 else {
643 print ", \n.br\n";
644 }
645 $count++;
646 }
647
648 print ".SH Constants\n";
649 foreach $parameter (@{$args{'parameterlist'}}) {
650 my $parameter_name = $parameter;
651 $parameter_name =~ s/\[.*//;
652
Randy Dunlapb9d973282009-06-09 08:50:38 -0700653 print ".IP \"" . $parameter . "\" 12\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 output_highlight($args{'parameterdescs'}{$parameter_name});
655 }
656 foreach $section (@{$args{'sectionlist'}}) {
657 print ".SH \"$section\"\n";
658 output_highlight($args{'sections'}{$section});
659 }
660}
661
662##
663# output struct in man
664sub output_struct_man(%) {
665 my %args = %{$_[0]};
666 my ($parameter, $section);
667
Randy Dunlapb9d973282009-06-09 08:50:38 -0700668 print ".TH \"$args{'module'}\" 9 \"" . $args{'type'} . " " . $args{'struct'} . "\" \"$man_date\" \"API Manual\" LINUX\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669
670 print ".SH NAME\n";
Randy Dunlapb9d973282009-06-09 08:50:38 -0700671 print $args{'type'} . " " . $args{'struct'} . " \\- " . $args{'purpose'} . "\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672
Mauro Carvalho Chehab8ad72162017-12-18 10:30:13 -0200673 my $declaration = $args{'definition'};
674 $declaration =~ s/\t/ /g;
675 $declaration =~ s/\n/"\n.br\n.BI \"/g;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 print ".SH SYNOPSIS\n";
Randy Dunlapb9d973282009-06-09 08:50:38 -0700677 print $args{'type'} . " " . $args{'struct'} . " {\n.br\n";
Mauro Carvalho Chehab8ad72162017-12-18 10:30:13 -0200678 print ".BI \"$declaration\n};\n.br\n\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679
Randy Dunlapc51d3da2006-06-25 05:49:14 -0700680 print ".SH Members\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 foreach $parameter (@{$args{'parameterlist'}}) {
682 ($parameter =~ /^#/) && next;
683
684 my $parameter_name = $parameter;
685 $parameter_name =~ s/\[.*//;
686
Randy Dunlap3c308792007-05-08 00:24:39 -0700687 ($args{'parameterdescs'}{$parameter_name} ne $undescribed) || next;
Randy Dunlapb9d973282009-06-09 08:50:38 -0700688 print ".IP \"" . $parameter . "\" 12\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 output_highlight($args{'parameterdescs'}{$parameter_name});
690 }
691 foreach $section (@{$args{'sectionlist'}}) {
692 print ".SH \"$section\"\n";
693 output_highlight($args{'sections'}{$section});
694 }
695}
696
697##
698# output typedef in man
699sub output_typedef_man(%) {
700 my %args = %{$_[0]};
701 my ($parameter, $section);
702
703 print ".TH \"$args{'module'}\" 9 \"$args{'typedef'}\" \"$man_date\" \"API Manual\" LINUX\n";
704
705 print ".SH NAME\n";
Randy Dunlapb9d973282009-06-09 08:50:38 -0700706 print "typedef " . $args{'typedef'} . " \\- " . $args{'purpose'} . "\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707
708 foreach $section (@{$args{'sectionlist'}}) {
709 print ".SH \"$section\"\n";
710 output_highlight($args{'sections'}{$section});
711 }
712}
713
Johannes Bergb112e0f2007-10-24 15:08:48 -0700714sub output_blockhead_man(%) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 my %args = %{$_[0]};
716 my ($parameter, $section);
717 my $count;
718
719 print ".TH \"$args{'module'}\" 9 \"$args{'module'}\" \"$man_date\" \"API Manual\" LINUX\n";
720
721 foreach $section (@{$args{'sectionlist'}}) {
722 print ".SH \"$section\"\n";
723 output_highlight($args{'sections'}{$section});
724 }
725}
726
727##
Jonathan Corbetc0d1b6e2016-05-12 16:15:37 +0300728# output in restructured text
729#
730
731#
732# This could use some work; it's used to output the DOC: sections, and
733# starts by putting out the name of the doc section itself, but that tends
734# to duplicate a header already in the template file.
735#
736sub output_blockhead_rst(%) {
737 my %args = %{$_[0]};
738 my ($parameter, $section);
739
740 foreach $section (@{$args{'sectionlist'}}) {
Jani Nikula9e721842016-05-29 22:27:35 +0300741 if ($output_selection != OUTPUT_INCLUDE) {
742 print "**$section**\n\n";
743 }
Daniel Vetter0b0f5f292016-06-03 22:21:34 +0200744 print_lineno($section_start_lines{$section});
Jonathan Corbetc0d1b6e2016-05-12 16:15:37 +0300745 output_highlight_rst($args{'sections'}{$section});
746 print "\n";
747 }
748}
749
750sub output_highlight_rst {
751 my $contents = join "\n",@_;
752 my $line;
753
Jonathan Corbetc0d1b6e2016-05-12 16:15:37 +0300754 eval $dohighlight;
755 die $@ if $@;
756
757 foreach $line (split "\n", $contents) {
Jani Nikula830066a2016-05-26 22:04:33 +0300758 print $lineprefix . $line . "\n";
Jonathan Corbetc0d1b6e2016-05-12 16:15:37 +0300759 }
760}
761
762sub output_function_rst(%) {
763 my %args = %{$_[0]};
764 my ($parameter, $section);
Jani Nikulac099ff62016-05-26 17:18:17 +0300765 my $oldprefix = $lineprefix;
Mauro Carvalho Chehab82801d02016-08-30 20:20:58 -0300766 my $start = "";
Jonathan Corbetc0d1b6e2016-05-12 16:15:37 +0300767
Mauro Carvalho Chehab82801d02016-08-30 20:20:58 -0300768 if ($args{'typedef'}) {
769 print ".. c:type:: ". $args{'function'} . "\n\n";
770 print_lineno($declaration_start_line);
771 print " **Typedef**: ";
772 $lineprefix = "";
773 output_highlight_rst($args{'purpose'});
774 $start = "\n\n**Syntax**\n\n ``";
Jonathan Corbetc0d1b6e2016-05-12 16:15:37 +0300775 } else {
Mauro Carvalho Chehab82801d02016-08-30 20:20:58 -0300776 print ".. c:function:: ";
777 }
778 if ($args{'functiontype'} ne "") {
779 $start .= $args{'functiontype'} . " " . $args{'function'} . " (";
780 } else {
781 $start .= $args{'function'} . " (";
Jonathan Corbetc0d1b6e2016-05-12 16:15:37 +0300782 }
783 print $start;
784
785 my $count = 0;
786 foreach my $parameter (@{$args{'parameterlist'}}) {
787 if ($count ne 0) {
788 print ", ";
789 }
790 $count++;
791 $type = $args{'parametertypes'}{$parameter};
Mauro Carvalho Chehaba88b1672016-07-22 11:46:36 -0300792
Jonathan Corbetc0d1b6e2016-05-12 16:15:37 +0300793 if ($type =~ m/([^\(]*\(\*)\s*\)\s*\(([^\)]*)\)/) {
794 # pointer-to-function
795 print $1 . $parameter . ") (" . $2;
796 } else {
797 print $type . " " . $parameter;
798 }
799 }
Mauro Carvalho Chehab82801d02016-08-30 20:20:58 -0300800 if ($args{'typedef'}) {
801 print ");``\n\n";
802 } else {
803 print ")\n\n";
804 print_lineno($declaration_start_line);
805 $lineprefix = " ";
806 output_highlight_rst($args{'purpose'});
807 print "\n";
808 }
Jonathan Corbetc0d1b6e2016-05-12 16:15:37 +0300809
Jani Nikulaecbcfba2016-05-26 18:30:27 +0300810 print "**Parameters**\n\n";
811 $lineprefix = " ";
Jonathan Corbetc0d1b6e2016-05-12 16:15:37 +0300812 foreach $parameter (@{$args{'parameterlist'}}) {
813 my $parameter_name = $parameter;
Gabriel Krisman Bertaziada5f442017-01-09 18:11:57 -0200814 $parameter_name =~ s/\[.*//;
Jonathan Corbetc0d1b6e2016-05-12 16:15:37 +0300815 $type = $args{'parametertypes'}{$parameter};
816
817 if ($type ne "") {
Jani Nikulaecbcfba2016-05-26 18:30:27 +0300818 print "``$type $parameter``\n";
Jonathan Corbetc0d1b6e2016-05-12 16:15:37 +0300819 } else {
Jani Nikulaecbcfba2016-05-26 18:30:27 +0300820 print "``$parameter``\n";
Jonathan Corbetc0d1b6e2016-05-12 16:15:37 +0300821 }
Daniel Vetter0b0f5f292016-06-03 22:21:34 +0200822
823 print_lineno($parameterdesc_start_lines{$parameter_name});
824
Jani Nikula5e64fa92016-05-19 20:32:48 +0300825 if (defined($args{'parameterdescs'}{$parameter_name}) &&
826 $args{'parameterdescs'}{$parameter_name} ne $undescribed) {
Jonathan Corbetc0d1b6e2016-05-12 16:15:37 +0300827 output_highlight_rst($args{'parameterdescs'}{$parameter_name});
Jonathan Corbetc0d1b6e2016-05-12 16:15:37 +0300828 } else {
Jani Nikulad4b08e02016-05-28 00:48:17 +0300829 print " *undescribed*\n";
Jonathan Corbetc0d1b6e2016-05-12 16:15:37 +0300830 }
831 print "\n";
832 }
Jani Nikulac099ff62016-05-26 17:18:17 +0300833
834 $lineprefix = $oldprefix;
Jonathan Corbetc0d1b6e2016-05-12 16:15:37 +0300835 output_section_rst(@_);
836}
837
838sub output_section_rst(%) {
839 my %args = %{$_[0]};
840 my $section;
841 my $oldprefix = $lineprefix;
Jani Nikulaecbcfba2016-05-26 18:30:27 +0300842 $lineprefix = "";
Jonathan Corbetc0d1b6e2016-05-12 16:15:37 +0300843
844 foreach $section (@{$args{'sectionlist'}}) {
Jani Nikulaecbcfba2016-05-26 18:30:27 +0300845 print "**$section**\n\n";
Daniel Vetter0b0f5f292016-06-03 22:21:34 +0200846 print_lineno($section_start_lines{$section});
Jonathan Corbetc0d1b6e2016-05-12 16:15:37 +0300847 output_highlight_rst($args{'sections'}{$section});
848 print "\n";
849 }
850 print "\n";
851 $lineprefix = $oldprefix;
852}
853
854sub output_enum_rst(%) {
855 my %args = %{$_[0]};
856 my ($parameter);
Jani Nikulac099ff62016-05-26 17:18:17 +0300857 my $oldprefix = $lineprefix;
Jonathan Corbetc0d1b6e2016-05-12 16:15:37 +0300858 my $count;
Jonathan Corbetc0d1b6e2016-05-12 16:15:37 +0300859 my $name = "enum " . $args{'enum'};
Jani Nikula62850972016-05-12 16:15:38 +0300860
861 print "\n\n.. c:type:: " . $name . "\n\n";
Daniel Vetter0b0f5f292016-06-03 22:21:34 +0200862 print_lineno($declaration_start_line);
Jani Nikulac099ff62016-05-26 17:18:17 +0300863 $lineprefix = " ";
864 output_highlight_rst($args{'purpose'});
865 print "\n";
Jonathan Corbetc0d1b6e2016-05-12 16:15:37 +0300866
Jani Nikulaecbcfba2016-05-26 18:30:27 +0300867 print "**Constants**\n\n";
868 $lineprefix = " ";
Jonathan Corbetc0d1b6e2016-05-12 16:15:37 +0300869 foreach $parameter (@{$args{'parameterlist'}}) {
Jani Nikulaecbcfba2016-05-26 18:30:27 +0300870 print "``$parameter``\n";
Jonathan Corbetc0d1b6e2016-05-12 16:15:37 +0300871 if ($args{'parameterdescs'}{$parameter} ne $undescribed) {
872 output_highlight_rst($args{'parameterdescs'}{$parameter});
873 } else {
Jani Nikulad4b08e02016-05-28 00:48:17 +0300874 print " *undescribed*\n";
Jonathan Corbetc0d1b6e2016-05-12 16:15:37 +0300875 }
876 print "\n";
877 }
Jani Nikulac099ff62016-05-26 17:18:17 +0300878
Jonathan Corbetc0d1b6e2016-05-12 16:15:37 +0300879 $lineprefix = $oldprefix;
880 output_section_rst(@_);
881}
882
883sub output_typedef_rst(%) {
884 my %args = %{$_[0]};
885 my ($parameter);
Jani Nikulac099ff62016-05-26 17:18:17 +0300886 my $oldprefix = $lineprefix;
Jonathan Corbetc0d1b6e2016-05-12 16:15:37 +0300887 my $name = "typedef " . $args{'typedef'};
888
Jani Nikula62850972016-05-12 16:15:38 +0300889 print "\n\n.. c:type:: " . $name . "\n\n";
Daniel Vetter0b0f5f292016-06-03 22:21:34 +0200890 print_lineno($declaration_start_line);
Jani Nikulac099ff62016-05-26 17:18:17 +0300891 $lineprefix = " ";
892 output_highlight_rst($args{'purpose'});
893 print "\n";
Jonathan Corbetc0d1b6e2016-05-12 16:15:37 +0300894
Jani Nikulac099ff62016-05-26 17:18:17 +0300895 $lineprefix = $oldprefix;
Jonathan Corbetc0d1b6e2016-05-12 16:15:37 +0300896 output_section_rst(@_);
897}
898
899sub output_struct_rst(%) {
900 my %args = %{$_[0]};
901 my ($parameter);
Jani Nikulac099ff62016-05-26 17:18:17 +0300902 my $oldprefix = $lineprefix;
Jonathan Corbetc0d1b6e2016-05-12 16:15:37 +0300903 my $name = $args{'type'} . " " . $args{'struct'};
904
Jani Nikula62850972016-05-12 16:15:38 +0300905 print "\n\n.. c:type:: " . $name . "\n\n";
Daniel Vetter0b0f5f292016-06-03 22:21:34 +0200906 print_lineno($declaration_start_line);
Jani Nikulac099ff62016-05-26 17:18:17 +0300907 $lineprefix = " ";
908 output_highlight_rst($args{'purpose'});
909 print "\n";
Jonathan Corbetc0d1b6e2016-05-12 16:15:37 +0300910
Jani Nikulaecbcfba2016-05-26 18:30:27 +0300911 print "**Definition**\n\n";
912 print "::\n\n";
Mauro Carvalho Chehab8ad72162017-12-18 10:30:13 -0200913 my $declaration = $args{'definition'};
914 $declaration =~ s/\t/ /g;
915 print " " . $args{'type'} . " " . $args{'struct'} . " {\n$declaration };\n\n";
Jonathan Corbetc0d1b6e2016-05-12 16:15:37 +0300916
Jani Nikulaecbcfba2016-05-26 18:30:27 +0300917 print "**Members**\n\n";
918 $lineprefix = " ";
Jonathan Corbetc0d1b6e2016-05-12 16:15:37 +0300919 foreach $parameter (@{$args{'parameterlist'}}) {
920 ($parameter =~ /^#/) && next;
921
922 my $parameter_name = $parameter;
923 $parameter_name =~ s/\[.*//;
924
925 ($args{'parameterdescs'}{$parameter_name} ne $undescribed) || next;
926 $type = $args{'parametertypes'}{$parameter};
Daniel Vetter0b0f5f292016-06-03 22:21:34 +0200927 print_lineno($parameterdesc_start_lines{$parameter_name});
Mauro Carvalho Chehab6d232c82016-08-22 22:02:57 -0300928 print "``" . $parameter . "``\n";
Jonathan Corbetc0d1b6e2016-05-12 16:15:37 +0300929 output_highlight_rst($args{'parameterdescs'}{$parameter_name});
Jonathan Corbetc0d1b6e2016-05-12 16:15:37 +0300930 print "\n";
931 }
932 print "\n";
Jani Nikulac099ff62016-05-26 17:18:17 +0300933
934 $lineprefix = $oldprefix;
Jonathan Corbetc0d1b6e2016-05-12 16:15:37 +0300935 output_section_rst(@_);
936}
937
Matthew Wilcox3a025e12017-11-20 10:40:40 -0800938## none mode output functions
939
940sub output_function_none(%) {
941}
942
943sub output_enum_none(%) {
944}
945
946sub output_typedef_none(%) {
947}
948
949sub output_struct_none(%) {
950}
951
952sub output_blockhead_none(%) {
953}
954
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955##
Randy Dunlap27205742006-10-11 01:22:12 -0700956# generic output function for all types (function, struct/union, typedef, enum);
957# calls the generated, variable output_ function name based on
958# functype and output_mode
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959sub output_declaration {
960 no strict 'refs';
961 my $name = shift;
962 my $functype = shift;
963 my $func = "output_${functype}_$output_mode";
Jani Nikulab6c3f452016-05-29 22:19:35 +0300964 if (($output_selection == OUTPUT_ALL) ||
965 (($output_selection == OUTPUT_INCLUDE ||
966 $output_selection == OUTPUT_EXPORTED) &&
967 defined($function_table{$name})) ||
968 (($output_selection == OUTPUT_EXCLUDE ||
969 $output_selection == OUTPUT_INTERNAL) &&
970 !($functype eq "function" && defined($function_table{$name}))))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971 {
Randy Dunlap3c308792007-05-08 00:24:39 -0700972 &$func(@_);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973 $section_counter++;
974 }
975}
976
977##
Randy Dunlap27205742006-10-11 01:22:12 -0700978# generic output function - calls the right one based on current output mode.
Johannes Bergb112e0f2007-10-24 15:08:48 -0700979sub output_blockhead {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980 no strict 'refs';
Randy Dunlapb9d973282009-06-09 08:50:38 -0700981 my $func = "output_blockhead_" . $output_mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982 &$func(@_);
983 $section_counter++;
984}
985
986##
Randy Dunlap3c3b8092006-02-01 03:06:58 -0800987# takes a declaration (struct, union, enum, typedef) and
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988# invokes the right handler. NOT called for functions.
989sub dump_declaration($$) {
990 no strict 'refs';
991 my ($prototype, $file) = @_;
Randy Dunlapb9d973282009-06-09 08:50:38 -0700992 my $func = "dump_" . $decl_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993 &$func(@_);
994}
995
996sub dump_union($$) {
997 dump_struct(@_);
998}
999
1000sub dump_struct($$) {
1001 my $x = shift;
1002 my $file = shift;
1003
Randy Dunlap52dc5ae2009-04-30 15:08:53 -07001004 if ($x =~ /(struct|union)\s+(\w+)\s*{(.*)}/) {
Johannes Berg5cb5c312017-09-19 13:08:13 +02001005 my $decl_type = $1;
Randy Dunlap3c308792007-05-08 00:24:39 -07001006 $declaration_name = $2;
1007 my $members = $3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008
Martin Waitzaeec46b2005-11-13 16:08:13 -08001009 # ignore members marked private:
Mauro Carvalho Chehab0d8c39e2015-10-05 09:03:48 -03001010 $members =~ s/\/\*\s*private:.*?\/\*\s*public:.*?\*\///gosi;
1011 $members =~ s/\/\*\s*private:.*//gosi;
Martin Waitzaeec46b2005-11-13 16:08:13 -08001012 # strip comments:
1013 $members =~ s/\/\*.*?\*\///gos;
Randy Dunlapef5da592010-03-23 13:35:14 -07001014 # strip attributes
Jonathan Corbetf0074922015-08-23 13:35:23 -06001015 $members =~ s/__attribute__\s*\(\([a-z,_\*\s\(\)]*\)\)//i;
Johannes Berg7b990782014-12-10 15:41:28 -08001016 $members =~ s/__aligned\s*\([^;]*\)//gos;
Jonathan Corbetf0074922015-08-23 13:35:23 -06001017 $members =~ s/\s*CRYPTO_MINALIGN_ATTR//gos;
Conchúr Navidb22b5a92015-11-08 10:52:00 +01001018 # replace DECLARE_BITMAP
Mauro Carvalho Chehab45005b22017-12-08 09:05:12 -05001019 $members =~ s/DECLARE_BITMAP\s*\(([^,)]+),\s*([^,)]+)\)/unsigned long $1\[BITS_TO_LONGS($2)\]/gos;
Jakub Kicinski1cb566b2017-06-30 19:09:59 -07001020 # replace DECLARE_HASHTABLE
Mauro Carvalho Chehab45005b22017-12-08 09:05:12 -05001021 $members =~ s/DECLARE_HASHTABLE\s*\(([^,)]+),\s*([^,)]+)\)/unsigned long $1\[1 << (($2) - 1)\]/gos;
1022 # replace DECLARE_KFIFO
1023 $members =~ s/DECLARE_KFIFO\s*\(([^,)]+),\s*([^,)]+),\s*([^,)]+)\)/$2 \*$1/gos;
1024 # replace DECLARE_KFIFO_PTR
1025 $members =~ s/DECLARE_KFIFO_PTR\s*\(([^,)]+),\s*([^,)]+)\)/$2 \*$1/gos;
Martin Waitzaeec46b2005-11-13 16:08:13 -08001026
Mauro Carvalho Chehab8ad72162017-12-18 10:30:13 -02001027 my $declaration = $members;
1028
1029 # Split nested struct/union elements as newer ones
Mauro Carvalho Chehab84ce5b92017-12-18 10:30:17 -02001030 while ($members =~ m/(struct|union)([^\{\};]+)\{([^\{\}]*)\}([^\{\}\;]*)\;/) {
1031 my $newmember;
1032 my $maintype = $1;
1033 my $ids = $4;
1034 my $content = $3;
1035 foreach my $id(split /,/, $ids) {
1036 $newmember .= "$maintype $id; ";
1037
Mauro Carvalho Chehab8ad72162017-12-18 10:30:13 -02001038 $id =~ s/[:\[].*//;
Mauro Carvalho Chehab84ce5b92017-12-18 10:30:17 -02001039 $id =~ s/^\s*\**(\S+)\s*/$1/;
Mauro Carvalho Chehab8ad72162017-12-18 10:30:13 -02001040 foreach my $arg (split /;/, $content) {
1041 next if ($arg =~ m/^\s*$/);
Mauro Carvalho Chehab7c0d7e82017-12-18 10:30:16 -02001042 if ($arg =~ m/^([^\(]+\(\*?\s*)([\w\.]*)(\s*\).*)/) {
1043 # pointer-to-function
1044 my $type = $1;
1045 my $name = $2;
1046 my $extra = $3;
1047 next if (!$name);
1048 if ($id =~ m/^\s*$/) {
1049 # anonymous struct/union
Mauro Carvalho Chehab84ce5b92017-12-18 10:30:17 -02001050 $newmember .= "$type$name$extra; ";
Mauro Carvalho Chehab7c0d7e82017-12-18 10:30:16 -02001051 } else {
Mauro Carvalho Chehab84ce5b92017-12-18 10:30:17 -02001052 $newmember .= "$type$id.$name$extra; ";
Mauro Carvalho Chehab7c0d7e82017-12-18 10:30:16 -02001053 }
Mauro Carvalho Chehab8ad72162017-12-18 10:30:13 -02001054 } else {
Mauro Carvalho Chehab84ce5b92017-12-18 10:30:17 -02001055 my $type;
1056 my $names;
1057 $arg =~ s/^\s+//;
1058 $arg =~ s/\s+$//;
1059 # Handle bitmaps
1060 $arg =~ s/:\s*\d+\s*//g;
1061 # Handle arrays
1062 $arg =~ s/\[\S+\]//g;
1063 # The type may have multiple words,
1064 # and multiple IDs can be defined, like:
1065 # const struct foo, *bar, foobar
1066 # So, we remove spaces when parsing the
1067 # names, in order to match just names
1068 # and commas for the names
1069 $arg =~ s/\s*,\s*/,/g;
1070 if ($arg =~ m/(.*)\s+([\S+,]+)/) {
1071 $type = $1;
1072 $names = $2;
Mauro Carvalho Chehab7c0d7e82017-12-18 10:30:16 -02001073 } else {
Mauro Carvalho Chehab84ce5b92017-12-18 10:30:17 -02001074 $newmember .= "$arg; ";
1075 next;
1076 }
1077 foreach my $name (split /,/, $names) {
1078 $name =~ s/^\s*\**(\S+)\s*/$1/;
1079 next if (($name =~ m/^\s*$/));
1080 if ($id =~ m/^\s*$/) {
1081 # anonymous struct/union
1082 $newmember .= "$type $name; ";
1083 } else {
1084 $newmember .= "$type $id.$name; ";
1085 }
Mauro Carvalho Chehab7c0d7e82017-12-18 10:30:16 -02001086 }
Mauro Carvalho Chehab8ad72162017-12-18 10:30:13 -02001087 }
1088 }
Mauro Carvalho Chehab84ce5b92017-12-18 10:30:17 -02001089 }
1090 $members =~ s/(struct|union)([^\{\};]+)\{([^\{\}]*)}([^\{\}\;]*)\;/$newmember/;
1091 }
Mauro Carvalho Chehab8ad72162017-12-18 10:30:13 -02001092
1093 # Ignore other nested elements, like enums
1094 $members =~ s/({[^\{\}]*})//g;
Mauro Carvalho Chehab8ad72162017-12-18 10:30:13 -02001095
Mauro Carvalho Chehab151c4682017-12-18 10:30:15 -02001096 create_parameterlist($members, ';', $file, $declaration_name);
Mauro Carvalho Chehab1081de22017-12-18 10:30:14 -02001097 check_sections($file, $declaration_name, $decl_type, $sectcheck, $struct_actual);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098
Mauro Carvalho Chehab8ad72162017-12-18 10:30:13 -02001099 # Adjust declaration for better display
1100 $declaration =~ s/([{;])/$1\n/g;
1101 $declaration =~ s/}\s+;/};/g;
1102 # Better handle inlined enums
1103 do {} while ($declaration =~ s/(enum\s+{[^}]+),([^\n])/$1,\n$2/);
1104
1105 my @def_args = split /\n/, $declaration;
1106 my $level = 1;
1107 $declaration = "";
1108 foreach my $clause (@def_args) {
1109 $clause =~ s/^\s+//;
1110 $clause =~ s/\s+$//;
1111 $clause =~ s/\s+/ /;
1112 next if (!$clause);
1113 $level-- if ($clause =~ m/(})/ && $level > 1);
1114 if (!($clause =~ m/^\s*#/)) {
1115 $declaration .= "\t" x $level;
1116 }
1117 $declaration .= "\t" . $clause . "\n";
1118 $level++ if ($clause =~ m/({)/ && !($clause =~m/}/));
1119 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120 output_declaration($declaration_name,
1121 'struct',
1122 {'struct' => $declaration_name,
1123 'module' => $modulename,
Mauro Carvalho Chehab8ad72162017-12-18 10:30:13 -02001124 'definition' => $declaration,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125 'parameterlist' => \@parameterlist,
1126 'parameterdescs' => \%parameterdescs,
1127 'parametertypes' => \%parametertypes,
1128 'sectionlist' => \@sectionlist,
1129 'sections' => \%sections,
1130 'purpose' => $declaration_purpose,
1131 'type' => $decl_type
1132 });
1133 }
1134 else {
Bart Van Assched40e1e62015-09-04 15:43:21 -07001135 print STDERR "${file}:$.: error: Cannot parse struct or union!\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136 ++$errors;
1137 }
1138}
1139
Mauro Carvalho Chehab85afe602017-12-30 22:32:55 -02001140
1141sub show_warnings($$) {
1142 my $functype = shift;
1143 my $name = shift;
1144
1145 return 1 if ($output_selection == OUTPUT_ALL);
1146
1147 if ($output_selection == OUTPUT_EXPORTED) {
1148 if (defined($function_table{$name})) {
1149 return 1;
1150 } else {
1151 return 0;
1152 }
1153 }
1154 if ($output_selection == OUTPUT_INTERNAL) {
1155 if (!($functype eq "function" && defined($function_table{$name}))) {
1156 return 1;
1157 } else {
1158 return 0;
1159 }
1160 }
1161 if ($output_selection == OUTPUT_INCLUDE) {
1162 if (defined($function_table{$name})) {
1163 return 1;
1164 } else {
1165 return 0;
1166 }
1167 }
1168 if ($output_selection == OUTPUT_EXCLUDE) {
1169 if (!defined($function_table{$name})) {
1170 return 1;
1171 } else {
1172 return 0;
1173 }
1174 }
1175 die("Please add the new output type at show_warnings()");
1176}
1177
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178sub dump_enum($$) {
1179 my $x = shift;
1180 my $file = shift;
1181
Martin Waitzaeec46b2005-11-13 16:08:13 -08001182 $x =~ s@/\*.*?\*/@@gos; # strip comments.
Conchúr Navid4468e212015-11-08 10:48:05 +01001183 # strip #define macros inside enums
1184 $x =~ s@#\s*((define|ifdef)\s+|endif)[^;]*;@@gos;
Randy Dunlapb6d676d2010-08-10 18:02:50 -07001185
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186 if ($x =~ /enum\s+(\w+)\s*{(.*)}/) {
Randy Dunlap3c308792007-05-08 00:24:39 -07001187 $declaration_name = $1;
1188 my $members = $2;
Johannes Berg5cb5c312017-09-19 13:08:13 +02001189 my %_members;
1190
Markus Heiser463a0fd2017-06-16 21:27:48 +02001191 $members =~ s/\s+$//;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192
1193 foreach my $arg (split ',', $members) {
1194 $arg =~ s/^\s*(\w+).*/$1/;
1195 push @parameterlist, $arg;
1196 if (!$parameterdescs{$arg}) {
Randy Dunlap3c308792007-05-08 00:24:39 -07001197 $parameterdescs{$arg} = $undescribed;
Mauro Carvalho Chehab85afe602017-12-30 22:32:55 -02001198 if (show_warnings("enum", $declaration_name)) {
Mauro Carvalho Chehab2defb272017-12-18 10:30:18 -02001199 print STDERR "${file}:$.: warning: Enum value '$arg' not described in enum '$declaration_name'\n";
1200 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201 }
Johannes Berg5cb5c312017-09-19 13:08:13 +02001202 $_members{$arg} = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203 }
Randy Dunlap3c3b8092006-02-01 03:06:58 -08001204
Johannes Berg5cb5c312017-09-19 13:08:13 +02001205 while (my ($k, $v) = each %parameterdescs) {
1206 if (!exists($_members{$k})) {
Mauro Carvalho Chehab85afe602017-12-30 22:32:55 -02001207 if (show_warnings("enum", $declaration_name)) {
Mauro Carvalho Chehab2defb272017-12-18 10:30:18 -02001208 print STDERR "${file}:$.: warning: Excess enum value '$k' description in '$declaration_name'\n";
1209 }
Johannes Berg5cb5c312017-09-19 13:08:13 +02001210 }
1211 }
1212
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213 output_declaration($declaration_name,
1214 'enum',
1215 {'enum' => $declaration_name,
1216 'module' => $modulename,
1217 'parameterlist' => \@parameterlist,
1218 'parameterdescs' => \%parameterdescs,
1219 'sectionlist' => \@sectionlist,
1220 'sections' => \%sections,
1221 'purpose' => $declaration_purpose
1222 });
1223 }
1224 else {
Bart Van Assched40e1e62015-09-04 15:43:21 -07001225 print STDERR "${file}:$.: error: Cannot parse enum!\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226 ++$errors;
1227 }
1228}
1229
1230sub dump_typedef($$) {
1231 my $x = shift;
1232 my $file = shift;
1233
Martin Waitzaeec46b2005-11-13 16:08:13 -08001234 $x =~ s@/\*.*?\*/@@gos; # strip comments.
Mauro Carvalho Chehab83766452015-10-08 16:14:45 -03001235
1236 # Parse function prototypes
Mauro Carvalho Chehabd37c43c2016-08-30 20:20:57 -03001237 if ($x =~ /typedef\s+(\w+)\s*\(\*\s*(\w\S+)\s*\)\s*\((.*)\);/ ||
1238 $x =~ /typedef\s+(\w+)\s*(\w\S+)\s*\s*\((.*)\);/) {
1239
Mauro Carvalho Chehab83766452015-10-08 16:14:45 -03001240 # Function typedefs
1241 $return_type = $1;
1242 $declaration_name = $2;
1243 my $args = $3;
1244
Mauro Carvalho Chehab151c4682017-12-18 10:30:15 -02001245 create_parameterlist($args, ',', $file, $declaration_name);
Mauro Carvalho Chehab83766452015-10-08 16:14:45 -03001246
1247 output_declaration($declaration_name,
1248 'function',
1249 {'function' => $declaration_name,
Mauro Carvalho Chehab82801d02016-08-30 20:20:58 -03001250 'typedef' => 1,
Mauro Carvalho Chehab83766452015-10-08 16:14:45 -03001251 'module' => $modulename,
1252 'functiontype' => $return_type,
1253 'parameterlist' => \@parameterlist,
1254 'parameterdescs' => \%parameterdescs,
1255 'parametertypes' => \%parametertypes,
1256 'sectionlist' => \@sectionlist,
1257 'sections' => \%sections,
1258 'purpose' => $declaration_purpose
1259 });
1260 return;
1261 }
1262
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263 while (($x =~ /\(*.\)\s*;$/) || ($x =~ /\[*.\]\s*;$/)) {
Randy Dunlap3c308792007-05-08 00:24:39 -07001264 $x =~ s/\(*.\)\s*;$/;/;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265 $x =~ s/\[*.\]\s*;$/;/;
1266 }
1267
1268 if ($x =~ /typedef.*\s+(\w+)\s*;/) {
Randy Dunlap3c308792007-05-08 00:24:39 -07001269 $declaration_name = $1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270
1271 output_declaration($declaration_name,
1272 'typedef',
1273 {'typedef' => $declaration_name,
1274 'module' => $modulename,
1275 'sectionlist' => \@sectionlist,
1276 'sections' => \%sections,
1277 'purpose' => $declaration_purpose
1278 });
1279 }
1280 else {
Bart Van Assched40e1e62015-09-04 15:43:21 -07001281 print STDERR "${file}:$.: error: Cannot parse typedef!\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282 ++$errors;
1283 }
1284}
1285
Randy Dunlapa1d94aa2008-12-19 08:49:30 -08001286sub save_struct_actual($) {
1287 my $actual = shift;
1288
1289 # strip all spaces from the actual param so that it looks like one string item
1290 $actual =~ s/\s*//g;
1291 $struct_actual = $struct_actual . $actual . " ";
1292}
1293
Mauro Carvalho Chehab151c4682017-12-18 10:30:15 -02001294sub create_parameterlist($$$$) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295 my $args = shift;
1296 my $splitter = shift;
1297 my $file = shift;
Mauro Carvalho Chehab151c4682017-12-18 10:30:15 -02001298 my $declaration_name = shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299 my $type;
1300 my $param;
1301
Martin Waitza6d3fe72006-01-09 20:53:55 -08001302 # temporarily replace commas inside function pointer definition
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303 while ($args =~ /(\([^\),]+),/) {
Randy Dunlap3c308792007-05-08 00:24:39 -07001304 $args =~ s/(\([^\),]+),/$1#/g;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305 }
Randy Dunlap3c3b8092006-02-01 03:06:58 -08001306
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307 foreach my $arg (split($splitter, $args)) {
1308 # strip comments
1309 $arg =~ s/\/\*.*\*\///;
Randy Dunlap3c308792007-05-08 00:24:39 -07001310 # strip leading/trailing spaces
1311 $arg =~ s/^\s*//;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312 $arg =~ s/\s*$//;
1313 $arg =~ s/\s+/ /;
1314
1315 if ($arg =~ /^#/) {
1316 # Treat preprocessor directive as a typeless variable just to fill
1317 # corresponding data structures "correctly". Catch it later in
1318 # output_* subs.
1319 push_parameter($arg, "", $file);
Richard Kennedy00d62962008-02-23 15:24:01 -08001320 } elsif ($arg =~ m/\(.+\)\s*\(/) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321 # pointer-to-function
1322 $arg =~ tr/#/,/;
Mauro Carvalho Chehab7c0d7e82017-12-18 10:30:16 -02001323 $arg =~ m/[^\(]+\(\*?\s*([\w\.]*)\s*\)/;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324 $param = $1;
1325 $type = $arg;
Richard Kennedy00d62962008-02-23 15:24:01 -08001326 $type =~ s/([^\(]+\(\*?)\s*$param/$1/;
Randy Dunlapa1d94aa2008-12-19 08:49:30 -08001327 save_struct_actual($param);
Mauro Carvalho Chehab151c4682017-12-18 10:30:15 -02001328 push_parameter($param, $type, $file, $declaration_name);
Martin Waitzaeec46b2005-11-13 16:08:13 -08001329 } elsif ($arg) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330 $arg =~ s/\s*:\s*/:/g;
1331 $arg =~ s/\s*\[/\[/g;
1332
1333 my @args = split('\s*,\s*', $arg);
1334 if ($args[0] =~ m/\*/) {
1335 $args[0] =~ s/(\*+)\s*/ $1/;
1336 }
Borislav Petkov884f2812007-05-08 00:29:05 -07001337
1338 my @first_arg;
1339 if ($args[0] =~ /^(.*\s+)(.*?\[.*\].*)$/) {
1340 shift @args;
1341 push(@first_arg, split('\s+', $1));
1342 push(@first_arg, $2);
1343 } else {
1344 @first_arg = split('\s+', shift @args);
1345 }
1346
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347 unshift(@args, pop @first_arg);
1348 $type = join " ", @first_arg;
1349
1350 foreach $param (@args) {
1351 if ($param =~ m/^(\*+)\s*(.*)/) {
Randy Dunlapa1d94aa2008-12-19 08:49:30 -08001352 save_struct_actual($2);
Mauro Carvalho Chehab151c4682017-12-18 10:30:15 -02001353 push_parameter($2, "$type $1", $file, $declaration_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354 }
1355 elsif ($param =~ m/(.*?):(\d+)/) {
Randy Dunlap7b978872008-05-16 15:45:52 -07001356 if ($type ne "") { # skip unnamed bit-fields
Randy Dunlapa1d94aa2008-12-19 08:49:30 -08001357 save_struct_actual($1);
Mauro Carvalho Chehab151c4682017-12-18 10:30:15 -02001358 push_parameter($1, "$type:$2", $file, $declaration_name)
Randy Dunlap7b978872008-05-16 15:45:52 -07001359 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360 }
1361 else {
Randy Dunlapa1d94aa2008-12-19 08:49:30 -08001362 save_struct_actual($param);
Mauro Carvalho Chehab151c4682017-12-18 10:30:15 -02001363 push_parameter($param, $type, $file, $declaration_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364 }
1365 }
1366 }
1367 }
1368}
1369
Mauro Carvalho Chehab151c4682017-12-18 10:30:15 -02001370sub push_parameter($$$$) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371 my $param = shift;
1372 my $type = shift;
1373 my $file = shift;
Mauro Carvalho Chehab151c4682017-12-18 10:30:15 -02001374 my $declaration_name = shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375
Randy Dunlap5f8c7c92007-07-19 01:48:24 -07001376 if (($anon_struct_union == 1) && ($type eq "") &&
1377 ($param eq "}")) {
1378 return; # ignore the ending }; from anon. struct/union
1379 }
1380
1381 $anon_struct_union = 0;
Mauro Carvalho Chehabf9b5c532017-03-30 17:11:29 -03001382 $param =~ s/[\[\)].*//;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383
Martin Waitza6d3fe72006-01-09 20:53:55 -08001384 if ($type eq "" && $param =~ /\.\.\.$/)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385 {
Silvio Frickec950a172016-10-28 10:14:08 +02001386 if (!$param =~ /\w\.\.\.$/) {
1387 # handles unnamed variable parameters
1388 $param = "...";
1389 }
Randy Dunlapced69092008-12-01 13:14:03 -08001390 if (!defined $parameterdescs{$param} || $parameterdescs{$param} eq "") {
1391 $parameterdescs{$param} = "variable arguments";
1392 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393 }
1394 elsif ($type eq "" && ($param eq "" or $param eq "void"))
1395 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396 $param="void";
1397 $parameterdescs{void} = "no arguments";
1398 }
Randy Dunlap134fe012006-12-22 01:10:50 -08001399 elsif ($type eq "" && ($param eq "struct" or $param eq "union"))
1400 # handle unnamed (anonymous) union or struct:
1401 {
1402 $type = $param;
Randy Dunlap5f8c7c92007-07-19 01:48:24 -07001403 $param = "{unnamed_" . $param . "}";
Randy Dunlap134fe012006-12-22 01:10:50 -08001404 $parameterdescs{$param} = "anonymous\n";
Randy Dunlap5f8c7c92007-07-19 01:48:24 -07001405 $anon_struct_union = 1;
Randy Dunlap134fe012006-12-22 01:10:50 -08001406 }
1407
Martin Waitza6d3fe72006-01-09 20:53:55 -08001408 # warn if parameter has no description
Randy Dunlap134fe012006-12-22 01:10:50 -08001409 # (but ignore ones starting with # as these are not parameters
1410 # but inline preprocessor statements);
Mauro Carvalho Chehab151c4682017-12-18 10:30:15 -02001411 # Note: It will also ignore void params and unnamed structs/unions
Mauro Carvalho Chehabf9b5c532017-03-30 17:11:29 -03001412 if (!defined $parameterdescs{$param} && $param !~ /^#/) {
Mauro Carvalho Chehab151c4682017-12-18 10:30:15 -02001413 $parameterdescs{$param} = $undescribed;
Martin Waitza6d3fe72006-01-09 20:53:55 -08001414
Mauro Carvalho Chehab85afe602017-12-30 22:32:55 -02001415 if (show_warnings($type, $declaration_name)) {
Mauro Carvalho Chehab2defb272017-12-18 10:30:18 -02001416 print STDERR
1417 "${file}:$.: warning: Function parameter or member '$param' not described in '$declaration_name'\n";
1418 ++$warnings;
1419 }
Randy Dunlap3c308792007-05-08 00:24:39 -07001420 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001422 # strip spaces from $param so that it is one continuous string
Randy Dunlape34e7db2009-06-17 17:37:47 -07001423 # on @parameterlist;
1424 # this fixes a problem where check_sections() cannot find
1425 # a parameter like "addr[6 + 2]" because it actually appears
1426 # as "addr[6", "+", "2]" on the parameter list;
1427 # but it's better to maintain the param string unchanged for output,
1428 # so just weaken the string compare in check_sections() to ignore
1429 # "[blah" in a parameter string;
1430 ###$param =~ s/\s*//g;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431 push @parameterlist, $param;
Paolo Bonzini02a4f4f2017-01-02 16:22:23 +01001432 $type =~ s/\s\s+/ /g;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433 $parametertypes{$param} = $type;
1434}
1435
Mauro Carvalho Chehab1081de22017-12-18 10:30:14 -02001436sub check_sections($$$$$) {
1437 my ($file, $decl_name, $decl_type, $sectcheck, $prmscheck) = @_;
Randy Dunlapa1d94aa2008-12-19 08:49:30 -08001438 my @sects = split ' ', $sectcheck;
1439 my @prms = split ' ', $prmscheck;
1440 my $err;
1441 my ($px, $sx);
1442 my $prm_clean; # strip trailing "[array size]" and/or beginning "*"
1443
1444 foreach $sx (0 .. $#sects) {
1445 $err = 1;
1446 foreach $px (0 .. $#prms) {
1447 $prm_clean = $prms[$px];
1448 $prm_clean =~ s/\[.*\]//;
Johannes Berg1f3a6682010-09-11 15:55:12 -07001449 $prm_clean =~ s/__attribute__\s*\(\([a-z,_\*\s\(\)]*\)\)//i;
Randy Dunlape34e7db2009-06-17 17:37:47 -07001450 # ignore array size in a parameter string;
1451 # however, the original param string may contain
1452 # spaces, e.g.: addr[6 + 2]
1453 # and this appears in @prms as "addr[6" since the
1454 # parameter list is split at spaces;
1455 # hence just ignore "[..." for the sections check;
1456 $prm_clean =~ s/\[.*//;
1457
Randy Dunlapa1d94aa2008-12-19 08:49:30 -08001458 ##$prm_clean =~ s/^\**//;
1459 if ($prm_clean eq $sects[$sx]) {
1460 $err = 0;
1461 last;
1462 }
1463 }
1464 if ($err) {
1465 if ($decl_type eq "function") {
Bart Van Assched40e1e62015-09-04 15:43:21 -07001466 print STDERR "${file}:$.: warning: " .
Randy Dunlapa1d94aa2008-12-19 08:49:30 -08001467 "Excess function parameter " .
1468 "'$sects[$sx]' " .
1469 "description in '$decl_name'\n";
1470 ++$warnings;
Randy Dunlapa1d94aa2008-12-19 08:49:30 -08001471 }
1472 }
1473 }
1474}
1475
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476##
Yacine Belkadi4092bac2012-11-26 22:22:27 +01001477# Checks the section describing the return value of a function.
1478sub check_return_section {
1479 my $file = shift;
1480 my $declaration_name = shift;
1481 my $return_type = shift;
1482
1483 # Ignore an empty return type (It's a macro)
1484 # Ignore functions with a "void" return type. (But don't ignore "void *")
1485 if (($return_type eq "") || ($return_type =~ /void\s*\w*\s*$/)) {
1486 return;
1487 }
1488
1489 if (!defined($sections{$section_return}) ||
1490 $sections{$section_return} eq "") {
Bart Van Assched40e1e62015-09-04 15:43:21 -07001491 print STDERR "${file}:$.: warning: " .
Yacine Belkadi4092bac2012-11-26 22:22:27 +01001492 "No description found for return value of " .
1493 "'$declaration_name'\n";
1494 ++$warnings;
1495 }
1496}
1497
1498##
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499# takes a function prototype and the name of the current file being
1500# processed and spits out all the details stored in the global
1501# arrays/hashes.
1502sub dump_function($$) {
1503 my $prototype = shift;
1504 my $file = shift;
Horia Geantacbb4d3e2014-07-12 09:55:03 -07001505 my $noret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506
1507 $prototype =~ s/^static +//;
1508 $prototype =~ s/^extern +//;
Pavel Pisa4dc3b162005-05-01 08:59:25 -07001509 $prototype =~ s/^asmlinkage +//;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510 $prototype =~ s/^inline +//;
1511 $prototype =~ s/^__inline__ +//;
Randy Dunlap32e79402006-10-11 01:22:10 -07001512 $prototype =~ s/^__inline +//;
1513 $prototype =~ s/^__always_inline +//;
1514 $prototype =~ s/^noinline +//;
Randy Dunlap74fc5c62008-06-19 16:03:29 -07001515 $prototype =~ s/__init +//;
Randy Dunlap20072202010-03-23 13:35:24 -07001516 $prototype =~ s/__init_or_module +//;
Randy Dunlap270a0092014-08-24 18:17:17 -07001517 $prototype =~ s/__meminit +//;
Randy Dunlap70c95b02012-01-21 10:31:54 -08001518 $prototype =~ s/__must_check +//;
Randy Dunlap0df7c0e2012-08-16 16:23:20 -07001519 $prototype =~ s/__weak +//;
Horia Geantacbb4d3e2014-07-12 09:55:03 -07001520 my $define = $prototype =~ s/^#\s*define\s+//; #ak added
Paolo Bonzinib1aaa542017-01-02 16:22:24 +01001521 $prototype =~ s/__attribute__\s*\(\(
1522 (?:
1523 [\w\s]++ # attribute name
1524 (?:\([^)]*+\))? # attribute arguments
1525 \s*+,? # optional comma at the end
1526 )+
1527 \)\)\s+//x;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528
1529 # Yes, this truly is vile. We are looking for:
1530 # 1. Return type (may be nothing if we're looking at a macro)
1531 # 2. Function name
1532 # 3. Function parameters.
1533 #
1534 # All the while we have to watch out for function pointer parameters
1535 # (which IIRC is what the two sections are for), C types (these
1536 # regexps don't even start to express all the possibilities), and
1537 # so on.
1538 #
1539 # If you mess with these regexps, it's a good idea to check that
1540 # the following functions' documentation still comes out right:
1541 # - parport_register_device (function pointer parameters)
1542 # - atomic_set (macro)
Martin Waitz9598f912006-02-01 03:06:55 -08001543 # - pci_match_device, __copy_to_user (long return type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544
Horia Geantacbb4d3e2014-07-12 09:55:03 -07001545 if ($define && $prototype =~ m/^()([a-zA-Z0-9_~:]+)\s+/) {
1546 # This is an object-like macro, it has no return type and no parameter
1547 # list.
1548 # Function-like macros are not allowed to have spaces between
1549 # declaration_name and opening parenthesis (notice the \s+).
1550 $return_type = $1;
1551 $declaration_name = $2;
1552 $noret = 1;
1553 } elsif ($prototype =~ m/^()([a-zA-Z0-9_~:]+)\s*\(([^\(]*)\)/ ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554 $prototype =~ m/^(\w+)\s+([a-zA-Z0-9_~:]+)\s*\(([^\(]*)\)/ ||
Matthew Wilcox5a0bc572017-01-23 00:18:10 -08001555 $prototype =~ m/^(\w+\s*\*+)\s*([a-zA-Z0-9_~:]+)\s*\(([^\(]*)\)/ ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556 $prototype =~ m/^(\w+\s+\w+)\s+([a-zA-Z0-9_~:]+)\s*\(([^\(]*)\)/ ||
Randy Dunlap94b3e032008-02-07 00:13:26 -08001557 $prototype =~ m/^(\w+\s+\w+\s*\*+)\s*([a-zA-Z0-9_~:]+)\s*\(([^\(]*)\)/ ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558 $prototype =~ m/^(\w+\s+\w+\s+\w+)\s+([a-zA-Z0-9_~:]+)\s*\(([^\(]*)\)/ ||
Matthew Wilcox5a0bc572017-01-23 00:18:10 -08001559 $prototype =~ m/^(\w+\s+\w+\s+\w+\s*\*+)\s*([a-zA-Z0-9_~:]+)\s*\(([^\(]*)\)/ ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560 $prototype =~ m/^()([a-zA-Z0-9_~:]+)\s*\(([^\{]*)\)/ ||
1561 $prototype =~ m/^(\w+)\s+([a-zA-Z0-9_~:]+)\s*\(([^\{]*)\)/ ||
Matthew Wilcox5a0bc572017-01-23 00:18:10 -08001562 $prototype =~ m/^(\w+\s*\*+)\s*([a-zA-Z0-9_~:]+)\s*\(([^\{]*)\)/ ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563 $prototype =~ m/^(\w+\s+\w+)\s+([a-zA-Z0-9_~:]+)\s*\(([^\{]*)\)/ ||
Matthew Wilcox5a0bc572017-01-23 00:18:10 -08001564 $prototype =~ m/^(\w+\s+\w+\s*\*+)\s*([a-zA-Z0-9_~:]+)\s*\(([^\{]*)\)/ ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565 $prototype =~ m/^(\w+\s+\w+\s+\w+)\s+([a-zA-Z0-9_~:]+)\s*\(([^\{]*)\)/ ||
Matthew Wilcox5a0bc572017-01-23 00:18:10 -08001566 $prototype =~ m/^(\w+\s+\w+\s+\w+\s*\*+)\s*([a-zA-Z0-9_~:]+)\s*\(([^\{]*)\)/ ||
Martin Waitz9598f912006-02-01 03:06:55 -08001567 $prototype =~ m/^(\w+\s+\w+\s+\w+\s+\w+)\s+([a-zA-Z0-9_~:]+)\s*\(([^\{]*)\)/ ||
Matthew Wilcox5a0bc572017-01-23 00:18:10 -08001568 $prototype =~ m/^(\w+\s+\w+\s+\w+\s+\w+\s*\*+)\s*([a-zA-Z0-9_~:]+)\s*\(([^\{]*)\)/ ||
1569 $prototype =~ m/^(\w+\s+\w+\s*\*+\s*\w+\s*\*+\s*)\s*([a-zA-Z0-9_~:]+)\s*\(([^\{]*)\)/) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570 $return_type = $1;
1571 $declaration_name = $2;
1572 my $args = $3;
1573
Mauro Carvalho Chehab151c4682017-12-18 10:30:15 -02001574 create_parameterlist($args, ',', $file, $declaration_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575 } else {
Bart Van Assched40e1e62015-09-04 15:43:21 -07001576 print STDERR "${file}:$.: warning: cannot understand function prototype: '$prototype'\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577 return;
1578 }
1579
Randy Dunlapa1d94aa2008-12-19 08:49:30 -08001580 my $prms = join " ", @parameterlist;
Mauro Carvalho Chehab1081de22017-12-18 10:30:14 -02001581 check_sections($file, $declaration_name, "function", $sectcheck, $prms);
Randy Dunlapa1d94aa2008-12-19 08:49:30 -08001582
Yacine Belkadi4092bac2012-11-26 22:22:27 +01001583 # This check emits a lot of warnings at the moment, because many
1584 # functions don't have a 'Return' doc section. So until the number
1585 # of warnings goes sufficiently down, the check is only performed in
1586 # verbose mode.
1587 # TODO: always perform the check.
Horia Geantacbb4d3e2014-07-12 09:55:03 -07001588 if ($verbose && !$noret) {
Yacine Belkadi4092bac2012-11-26 22:22:27 +01001589 check_return_section($file, $declaration_name, $return_type);
1590 }
1591
Randy Dunlap3c3b8092006-02-01 03:06:58 -08001592 output_declaration($declaration_name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593 'function',
1594 {'function' => $declaration_name,
1595 'module' => $modulename,
1596 'functiontype' => $return_type,
1597 'parameterlist' => \@parameterlist,
1598 'parameterdescs' => \%parameterdescs,
1599 'parametertypes' => \%parametertypes,
1600 'sectionlist' => \@sectionlist,
1601 'sections' => \%sections,
1602 'purpose' => $declaration_purpose
1603 });
1604}
1605
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606sub reset_state {
1607 $function = "";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608 %parameterdescs = ();
1609 %parametertypes = ();
1610 @parameterlist = ();
1611 %sections = ();
1612 @sectionlist = ();
Randy Dunlapa1d94aa2008-12-19 08:49:30 -08001613 $sectcheck = "";
1614 $struct_actual = "";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615 $prototype = "";
Randy Dunlap3c3b8092006-02-01 03:06:58 -08001616
Jani Nikula48af6062016-05-26 14:56:05 +03001617 $state = STATE_NORMAL;
1618 $inline_doc_state = STATE_INLINE_NA;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619}
1620
Jason Baron56afb0f2009-04-30 13:29:36 -04001621sub tracepoint_munge($) {
1622 my $file = shift;
1623 my $tracepointname = 0;
1624 my $tracepointargs = 0;
1625
Jason Baron3a9089f2009-12-01 12:18:49 -05001626 if ($prototype =~ m/TRACE_EVENT\((.*?),/) {
Jason Baron56afb0f2009-04-30 13:29:36 -04001627 $tracepointname = $1;
1628 }
Jason Baron3a9089f2009-12-01 12:18:49 -05001629 if ($prototype =~ m/DEFINE_SINGLE_EVENT\((.*?),/) {
1630 $tracepointname = $1;
1631 }
1632 if ($prototype =~ m/DEFINE_EVENT\((.*?),(.*?),/) {
1633 $tracepointname = $2;
1634 }
1635 $tracepointname =~ s/^\s+//; #strip leading whitespace
1636 if ($prototype =~ m/TP_PROTO\((.*?)\)/) {
Jason Baron56afb0f2009-04-30 13:29:36 -04001637 $tracepointargs = $1;
1638 }
1639 if (($tracepointname eq 0) || ($tracepointargs eq 0)) {
Bart Van Assched40e1e62015-09-04 15:43:21 -07001640 print STDERR "${file}:$.: warning: Unrecognized tracepoint format: \n".
Jason Baron56afb0f2009-04-30 13:29:36 -04001641 "$prototype\n";
1642 } else {
1643 $prototype = "static inline void trace_$tracepointname($tracepointargs)";
1644 }
1645}
1646
Randy Dunlapb4870bc2009-02-11 13:04:33 -08001647sub syscall_munge() {
1648 my $void = 0;
1649
Mauro Carvalho Chehab7c9aa012017-12-18 10:30:12 -02001650 $prototype =~ s@[\r\n]+@ @gos; # strip newlines/CR's
Randy Dunlapb4870bc2009-02-11 13:04:33 -08001651## if ($prototype =~ m/SYSCALL_DEFINE0\s*\(\s*(a-zA-Z0-9_)*\s*\)/) {
1652 if ($prototype =~ m/SYSCALL_DEFINE0/) {
1653 $void = 1;
1654## $prototype = "long sys_$1(void)";
1655 }
1656
1657 $prototype =~ s/SYSCALL_DEFINE.*\(/long sys_/; # fix return type & func name
1658 if ($prototype =~ m/long (sys_.*?),/) {
1659 $prototype =~ s/,/\(/;
1660 } elsif ($void) {
1661 $prototype =~ s/\)/\(void\)/;
1662 }
1663
1664 # now delete all of the odd-number commas in $prototype
1665 # so that arg types & arg names don't have a comma between them
1666 my $count = 0;
1667 my $len = length($prototype);
1668 if ($void) {
1669 $len = 0; # skip the for-loop
1670 }
1671 for (my $ix = 0; $ix < $len; $ix++) {
1672 if (substr($prototype, $ix, 1) eq ',') {
1673 $count++;
1674 if ($count % 2 == 1) {
1675 substr($prototype, $ix, 1) = ' ';
1676 }
1677 }
1678 }
1679}
1680
Daniel Vetterb7afa922016-06-01 23:46:24 +02001681sub process_proto_function($$) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682 my $x = shift;
1683 my $file = shift;
1684
Randy Dunlap51f5a0c2007-07-19 01:48:24 -07001685 $x =~ s@\/\/.*$@@gos; # strip C99-style comments to end of line
1686
Randy Dunlap890c78c2008-10-25 17:06:43 -07001687 if ($x =~ m#\s*/\*\s+MACDOC\s*#io || ($x =~ /^#/ && $x !~ /^#\s*define/)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688 # do nothing
1689 }
1690 elsif ($x =~ /([^\{]*)/) {
Randy Dunlap3c308792007-05-08 00:24:39 -07001691 $prototype .= $1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692 }
Randy Dunlapb4870bc2009-02-11 13:04:33 -08001693
Randy Dunlap890c78c2008-10-25 17:06:43 -07001694 if (($x =~ /\{/) || ($x =~ /\#\s*define/) || ($x =~ /;/)) {
Randy Dunlap3c308792007-05-08 00:24:39 -07001695 $prototype =~ s@/\*.*?\*/@@gos; # strip comments.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696 $prototype =~ s@[\r\n]+@ @gos; # strip newlines/cr's.
1697 $prototype =~ s@^\s+@@gos; # strip leading spaces
Randy Dunlapb4870bc2009-02-11 13:04:33 -08001698 if ($prototype =~ /SYSCALL_DEFINE/) {
1699 syscall_munge();
1700 }
Jason Baron3a9089f2009-12-01 12:18:49 -05001701 if ($prototype =~ /TRACE_EVENT/ || $prototype =~ /DEFINE_EVENT/ ||
1702 $prototype =~ /DEFINE_SINGLE_EVENT/)
1703 {
Jason Baron56afb0f2009-04-30 13:29:36 -04001704 tracepoint_munge($file);
1705 }
Randy Dunlapb4870bc2009-02-11 13:04:33 -08001706 dump_function($prototype, $file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707 reset_state();
1708 }
1709}
1710
Daniel Vetterb7afa922016-06-01 23:46:24 +02001711sub process_proto_type($$) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712 my $x = shift;
1713 my $file = shift;
1714
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715 $x =~ s@[\r\n]+@ @gos; # strip newlines/cr's.
1716 $x =~ s@^\s+@@gos; # strip leading spaces
1717 $x =~ s@\s+$@@gos; # strip trailing spaces
Randy Dunlap51f5a0c2007-07-19 01:48:24 -07001718 $x =~ s@\/\/.*$@@gos; # strip C99-style comments to end of line
1719
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720 if ($x =~ /^#/) {
1721 # To distinguish preprocessor directive from regular declaration later.
1722 $x .= ";";
1723 }
1724
1725 while (1) {
Randy Dunlap3c308792007-05-08 00:24:39 -07001726 if ( $x =~ /([^{};]*)([{};])(.*)/ ) {
Markus Heiser463a0fd2017-06-16 21:27:48 +02001727 if( length $prototype ) {
1728 $prototype .= " "
1729 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730 $prototype .= $1 . $2;
1731 ($2 eq '{') && $brcount++;
1732 ($2 eq '}') && $brcount--;
1733 if (($2 eq ';') && ($brcount == 0)) {
Randy Dunlapb9d973282009-06-09 08:50:38 -07001734 dump_declaration($prototype, $file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735 reset_state();
Randy Dunlap3c308792007-05-08 00:24:39 -07001736 last;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737 }
1738 $x = $3;
Randy Dunlap3c308792007-05-08 00:24:39 -07001739 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740 $prototype .= $x;
1741 last;
1742 }
1743 }
1744}
1745
Randy Dunlap6b5b55f2007-10-16 23:31:20 -07001746
Jani Nikula1ad560e2016-06-07 10:53:39 +03001747sub map_filename($) {
1748 my $file;
1749 my ($orig_file) = @_;
1750
1751 if (defined($ENV{'SRCTREE'})) {
1752 $file = "$ENV{'SRCTREE'}" . "/" . $orig_file;
1753 } else {
1754 $file = $orig_file;
1755 }
1756
1757 if (defined($source_map{$file})) {
1758 $file = $source_map{$file};
1759 }
1760
1761 return $file;
1762}
1763
Jani Nikula88c2b572016-06-07 11:00:52 +03001764sub process_export_file($) {
1765 my ($orig_file) = @_;
1766 my $file = map_filename($orig_file);
1767
1768 if (!open(IN,"<$file")) {
1769 print STDERR "Error: Cannot open file $file\n";
1770 ++$errors;
1771 return;
1772 }
1773
1774 while (<IN>) {
1775 if (/$export_symbol/) {
1776 $function_table{$2} = 1;
1777 }
1778 }
1779
1780 close(IN);
1781}
1782
Jonathan Corbet07048d12018-02-05 14:15:19 -07001783#
1784# Parsers for the various processing states.
1785#
1786# STATE_NORMAL: looking for the /** to begin everything.
1787#
1788sub process_normal() {
1789 if (/$doc_start/o) {
1790 $state = STATE_NAME; # next line is always the function name
1791 $in_doc_sect = 0;
1792 $declaration_start_line = $. + 1;
1793 }
1794}
1795
Jonathan Corbet3cac2bc2018-02-05 14:36:33 -07001796#
1797# STATE_NAME: Looking for the "name - description" line
1798#
1799sub process_name($$) {
1800 my $file = shift;
1801 my $identifier;
1802 my $descr;
Jonathan Corbet07048d12018-02-05 14:15:19 -07001803
Jonathan Corbet3cac2bc2018-02-05 14:36:33 -07001804 if (/$doc_block/o) {
1805 $state = STATE_DOCBLOCK;
1806 $contents = "";
1807 $new_start_line = $. + 1;
1808
1809 if ( $1 eq "" ) {
1810 $section = $section_intro;
1811 } else {
1812 $section = $1;
1813 }
1814 }
1815 elsif (/$doc_decl/o) {
1816 $identifier = $1;
1817 if (/\s*([\w\s]+?)\s*-/) {
1818 $identifier = $1;
1819 }
1820
1821 $state = STATE_BODY;
1822 # if there's no @param blocks need to set up default section
1823 # here
1824 $contents = "";
1825 $section = $section_default;
1826 $new_start_line = $. + 1;
1827 if (/-(.*)/) {
1828 # strip leading/trailing/multiple spaces
1829 $descr= $1;
1830 $descr =~ s/^\s*//;
1831 $descr =~ s/\s*$//;
1832 $descr =~ s/\s+/ /g;
1833 $declaration_purpose = $descr;
1834 $state = STATE_BODY_MAYBE;
1835 } else {
1836 $declaration_purpose = "";
1837 }
1838
1839 if (($declaration_purpose eq "") && $verbose) {
1840 print STDERR "${file}:$.: warning: missing initial short description on line:\n";
1841 print STDERR $_;
1842 ++$warnings;
1843 }
1844
1845 if ($identifier =~ m/^struct/) {
1846 $decl_type = 'struct';
1847 } elsif ($identifier =~ m/^union/) {
1848 $decl_type = 'union';
1849 } elsif ($identifier =~ m/^enum/) {
1850 $decl_type = 'enum';
1851 } elsif ($identifier =~ m/^typedef/) {
1852 $decl_type = 'typedef';
1853 } else {
1854 $decl_type = 'function';
1855 }
1856
1857 if ($verbose) {
1858 print STDERR "${file}:$.: info: Scanning doc for $identifier\n";
1859 }
1860 } else {
1861 print STDERR "${file}:$.: warning: Cannot understand $_ on line $.",
1862 " - I thought it was a doc line\n";
1863 ++$warnings;
1864 $state = STATE_NORMAL;
1865 }
1866}
Jonathan Corbet07048d12018-02-05 14:15:19 -07001867
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868sub process_file($) {
Randy Dunlap2283a112005-07-07 15:39:26 -07001869 my $file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001870 my $func;
1871 my $initial_section_counter = $section_counter;
Ben Hutchings68f86662015-09-01 23:48:49 +01001872 my ($orig_file) = @_;
Jani Nikulab7886de2016-05-28 00:41:50 +03001873 my $leading_space;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874
Jani Nikula1ad560e2016-06-07 10:53:39 +03001875 $file = map_filename($orig_file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001876
1877 if (!open(IN,"<$file")) {
1878 print STDERR "Error: Cannot open file $file\n";
1879 ++$errors;
1880 return;
1881 }
1882
Ilya Dryomova9e73142010-02-26 13:05:47 -08001883 $. = 1;
1884
Linus Torvalds1da177e2005-04-16 15:20:36 -07001885 $section_counter = 0;
1886 while (<IN>) {
Daniel Santos65478422012-10-04 17:15:05 -07001887 while (s/\\\s*$//) {
1888 $_ .= <IN>;
1889 }
Mauro Carvalho Chehab7c9aa012017-12-18 10:30:12 -02001890 # Replace tabs by spaces
1891 while ($_ =~ s/\t+/' ' x (length($&) * 8 - length($`) % 8)/e) {};
Jani Nikula48af6062016-05-26 14:56:05 +03001892 if ($state == STATE_NORMAL) {
Jonathan Corbet07048d12018-02-05 14:15:19 -07001893 process_normal();
Jonathan Corbet3cac2bc2018-02-05 14:36:33 -07001894 } elsif ($state == STATE_NAME) {
1895 process_name($file, $_);
Jonathan Corbet17b78712018-02-05 13:56:45 -07001896 } elsif ($state == STATE_BODY || $state == STATE_BODY_MAYBE) {
Jani Nikulaf624ade2016-05-29 11:35:28 +03001897 if (/$doc_sect/i) { # case insensitive for supported section names
Linus Torvalds1da177e2005-04-16 15:20:36 -07001898 $newsection = $1;
1899 $newcontents = $2;
1900
Jani Nikulaf624ade2016-05-29 11:35:28 +03001901 # map the supported section names to the canonical names
1902 if ($newsection =~ m/^description$/i) {
1903 $newsection = $section_default;
1904 } elsif ($newsection =~ m/^context$/i) {
1905 $newsection = $section_context;
1906 } elsif ($newsection =~ m/^returns?$/i) {
1907 $newsection = $section_return;
1908 } elsif ($newsection =~ m/^\@return$/) {
1909 # special: @return is a section, not a param description
1910 $newsection = $section_return;
1911 }
1912
Randy Dunlap792aa2f2008-02-07 00:13:41 -08001913 if (($contents ne "") && ($contents ne "\n")) {
Randy Dunlap850622d2006-06-25 05:48:55 -07001914 if (!$in_doc_sect && $verbose) {
Bart Van Assched40e1e62015-09-04 15:43:21 -07001915 print STDERR "${file}:$.: warning: contents before sections\n";
Randy Dunlap850622d2006-06-25 05:48:55 -07001916 ++$warnings;
1917 }
Jonathan Corbet0bba9242018-02-05 12:40:15 -07001918 dump_section($file, $section, $contents);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919 $section = $section_default;
1920 }
1921
Randy Dunlap850622d2006-06-25 05:48:55 -07001922 $in_doc_sect = 1;
Jonathan Corbet17b78712018-02-05 13:56:45 -07001923 $state = STATE_BODY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924 $contents = $newcontents;
Daniel Vetter0b0f5f292016-06-03 22:21:34 +02001925 $new_start_line = $.;
Mauro Carvalho Chehab7c9aa012017-12-18 10:30:12 -02001926 while (substr($contents, 0, 1) eq " ") {
Jani Nikula0a726302016-05-28 14:50:20 +03001927 $contents = substr($contents, 1);
1928 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929 if ($contents ne "") {
1930 $contents .= "\n";
1931 }
1932 $section = $newsection;
Jani Nikulab7886de2016-05-28 00:41:50 +03001933 $leading_space = undef;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934 } elsif (/$doc_end/) {
Randy Dunlap4c98eca2010-03-10 15:22:02 -08001935 if (($contents ne "") && ($contents ne "\n")) {
Jonathan Corbet0bba9242018-02-05 12:40:15 -07001936 dump_section($file, $section, $contents);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001937 $section = $section_default;
1938 $contents = "";
1939 }
Randy Dunlap46b958e2008-04-28 02:16:35 -07001940 # look for doc_com + <text> + doc_end:
1941 if ($_ =~ m'\s*\*\s*[a-zA-Z_0-9:\.]+\*/') {
Bart Van Assched40e1e62015-09-04 15:43:21 -07001942 print STDERR "${file}:$.: warning: suspicious ending line: $_";
Randy Dunlap46b958e2008-04-28 02:16:35 -07001943 ++$warnings;
1944 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945
1946 $prototype = "";
Jani Nikula48af6062016-05-26 14:56:05 +03001947 $state = STATE_PROTO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948 $brcount = 0;
Randy Dunlap232acbc2006-06-25 05:47:48 -07001949# print STDERR "end of doc comment, looking for prototype\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001950 } elsif (/$doc_content/) {
1951 # miguel-style comment kludge, look for blank lines after
1952 # @parameter line to signify start of description
Johannes Weiner64231332009-09-17 19:26:53 -07001953 if ($1 eq "") {
1954 if ($section =~ m/^@/ || $section eq $section_context) {
Jonathan Corbet0bba9242018-02-05 12:40:15 -07001955 dump_section($file, $section, $contents);
Johannes Weiner64231332009-09-17 19:26:53 -07001956 $section = $section_default;
1957 $contents = "";
Daniel Vetter0b0f5f292016-06-03 22:21:34 +02001958 $new_start_line = $.;
Johannes Weiner64231332009-09-17 19:26:53 -07001959 } else {
1960 $contents .= "\n";
1961 }
Jonathan Corbet17b78712018-02-05 13:56:45 -07001962 $state = STATE_BODY;
1963 } elsif ($state == STATE_BODY_MAYBE) {
Johannes Weiner64231332009-09-17 19:26:53 -07001964 # Continued declaration purpose
1965 chomp($declaration_purpose);
Jonathan Corbet0bba9242018-02-05 12:40:15 -07001966 $declaration_purpose .= " " . $1;
Daniel Santos12ae6772012-10-04 17:15:10 -07001967 $declaration_purpose =~ s/\s+/ /g;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968 } else {
Jani Nikulab7886de2016-05-28 00:41:50 +03001969 my $cont = $1;
1970 if ($section =~ m/^@/ || $section eq $section_context) {
1971 if (!defined $leading_space) {
1972 if ($cont =~ m/^(\s+)/) {
1973 $leading_space = $1;
1974 } else {
1975 $leading_space = "";
1976 }
1977 }
1978
1979 $cont =~ s/^$leading_space//;
1980 }
1981 $contents .= $cont . "\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001982 }
1983 } else {
1984 # i dont know - bad line? ignore.
Bart Van Assched40e1e62015-09-04 15:43:21 -07001985 print STDERR "${file}:$.: warning: bad line: $_";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986 ++$warnings;
1987 }
Jani Nikula48af6062016-05-26 14:56:05 +03001988 } elsif ($state == STATE_INLINE) { # scanning for inline parameters
Danilo Cesar Lemes de Paulaa4c6ebe2015-08-04 09:04:08 -03001989 # First line (state 1) needs to be a @parameter
Jani Nikula48af6062016-05-26 14:56:05 +03001990 if ($inline_doc_state == STATE_INLINE_NAME && /$doc_inline_sect/o) {
Danilo Cesar Lemes de Paulaa4c6ebe2015-08-04 09:04:08 -03001991 $section = $1;
1992 $contents = $2;
Daniel Vetter0b0f5f292016-06-03 22:21:34 +02001993 $new_start_line = $.;
Danilo Cesar Lemes de Paulaa4c6ebe2015-08-04 09:04:08 -03001994 if ($contents ne "") {
Mauro Carvalho Chehab7c9aa012017-12-18 10:30:12 -02001995 while (substr($contents, 0, 1) eq " ") {
Danilo Cesar Lemes de Paulaa4c6ebe2015-08-04 09:04:08 -03001996 $contents = substr($contents, 1);
1997 }
Jani Nikulaa0b96c22016-05-26 22:04:06 +03001998 $contents .= "\n";
Danilo Cesar Lemes de Paulaa4c6ebe2015-08-04 09:04:08 -03001999 }
Jani Nikula48af6062016-05-26 14:56:05 +03002000 $inline_doc_state = STATE_INLINE_TEXT;
Danilo Cesar Lemes de Paulaa4c6ebe2015-08-04 09:04:08 -03002001 # Documentation block end */
Jani Nikula48af6062016-05-26 14:56:05 +03002002 } elsif (/$doc_inline_end/) {
Danilo Cesar Lemes de Paulaa4c6ebe2015-08-04 09:04:08 -03002003 if (($contents ne "") && ($contents ne "\n")) {
Jonathan Corbet0bba9242018-02-05 12:40:15 -07002004 dump_section($file, $section, $contents);
Danilo Cesar Lemes de Paulaa4c6ebe2015-08-04 09:04:08 -03002005 $section = $section_default;
2006 $contents = "";
2007 }
Jani Nikula48af6062016-05-26 14:56:05 +03002008 $state = STATE_PROTO;
2009 $inline_doc_state = STATE_INLINE_NA;
Danilo Cesar Lemes de Paulaa4c6ebe2015-08-04 09:04:08 -03002010 # Regular text
2011 } elsif (/$doc_content/) {
Jani Nikula48af6062016-05-26 14:56:05 +03002012 if ($inline_doc_state == STATE_INLINE_TEXT) {
Danilo Cesar Lemes de Paulaa4c6ebe2015-08-04 09:04:08 -03002013 $contents .= $1 . "\n";
Jani Nikula6450c892016-05-26 22:04:42 +03002014 # nuke leading blank lines
2015 if ($contents =~ /^\s*$/) {
2016 $contents = "";
2017 }
Jani Nikula48af6062016-05-26 14:56:05 +03002018 } elsif ($inline_doc_state == STATE_INLINE_NAME) {
2019 $inline_doc_state = STATE_INLINE_ERROR;
Daniel Vettere7ca3112016-07-15 10:19:30 +02002020 print STDERR "${file}:$.: warning: ";
Danilo Cesar Lemes de Paulaa4c6ebe2015-08-04 09:04:08 -03002021 print STDERR "Incorrect use of kernel-doc format: $_";
2022 ++$warnings;
2023 }
2024 }
Jani Nikula48af6062016-05-26 14:56:05 +03002025 } elsif ($state == STATE_PROTO) { # scanning for function '{' (end of prototype)
Jani Nikula0c9aa202016-11-16 17:26:16 +02002026 if (/$doc_inline_oneline/) {
2027 $section = $1;
2028 $contents = $2;
2029 if ($contents ne "") {
2030 $contents .= "\n";
Jonathan Corbet0bba9242018-02-05 12:40:15 -07002031 dump_section($file, $section, $contents);
Jani Nikula0c9aa202016-11-16 17:26:16 +02002032 $section = $section_default;
2033 $contents = "";
2034 }
2035 } elsif (/$doc_inline_start/) {
Jani Nikula48af6062016-05-26 14:56:05 +03002036 $state = STATE_INLINE;
2037 $inline_doc_state = STATE_INLINE_NAME;
Danilo Cesar Lemes de Paulaa4c6ebe2015-08-04 09:04:08 -03002038 } elsif ($decl_type eq 'function') {
Daniel Vetterb7afa922016-06-01 23:46:24 +02002039 process_proto_function($_, $file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002040 } else {
Daniel Vetterb7afa922016-06-01 23:46:24 +02002041 process_proto_type($_, $file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042 }
Jani Nikula48af6062016-05-26 14:56:05 +03002043 } elsif ($state == STATE_DOCBLOCK) {
Daniel Vetterebff7f92016-06-01 23:46:23 +02002044 if (/$doc_end/)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002045 {
Jonathan Corbet0bba9242018-02-05 12:40:15 -07002046 dump_doc_section($file, $section, $contents);
Jani Nikula2f4ad402016-05-30 11:21:06 +03002047 $section = $section_default;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048 $contents = "";
2049 $function = "";
Linus Torvalds1da177e2005-04-16 15:20:36 -07002050 %parameterdescs = ();
2051 %parametertypes = ();
2052 @parameterlist = ();
2053 %sections = ();
2054 @sectionlist = ();
2055 $prototype = "";
Jani Nikula48af6062016-05-26 14:56:05 +03002056 $state = STATE_NORMAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002057 }
2058 elsif (/$doc_content/)
2059 {
2060 if ( $1 eq "" )
2061 {
2062 $contents .= $blankline;
2063 }
2064 else
2065 {
2066 $contents .= $1 . "\n";
Randy Dunlap3c3b8092006-02-01 03:06:58 -08002067 }
Randy Dunlap3c308792007-05-08 00:24:39 -07002068 }
2069 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002070 }
2071 if ($initial_section_counter == $section_counter) {
Matthew Wilcox3a025e12017-11-20 10:40:40 -08002072 if ($output_mode ne "none") {
2073 print STDERR "${file}:1: warning: no structured comments found\n";
2074 }
Jani Nikulab6c3f452016-05-29 22:19:35 +03002075 if (($output_selection == OUTPUT_INCLUDE) && ($show_not_found == 1)) {
Johannes Berge946c43a2013-11-12 15:11:12 -08002076 print STDERR " Was looking for '$_'.\n" for keys %function_table;
2077 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078 }
2079}
Randy Dunlap8484baa2011-01-05 16:28:43 -08002080
2081
2082$kernelversion = get_kernel_version();
2083
2084# generate a sequence of code that will splice in highlighting information
2085# using the s// operator.
Mauro Carvalho Chehab1ef06232015-11-17 13:29:49 -02002086for (my $k = 0; $k < @highlights; $k++) {
Danilo Cesar Lemes de Paula4d732702015-09-07 17:01:59 -03002087 my $pattern = $highlights[$k][0];
2088 my $result = $highlights[$k][1];
2089# print STDERR "scanning pattern:$pattern, highlight:($result)\n";
2090 $dohighlight .= "\$contents =~ s:$pattern:$result:gs;\n";
Randy Dunlap8484baa2011-01-05 16:28:43 -08002091}
2092
2093# Read the file that maps relative names to absolute names for
2094# separate source and object directories and for shadow trees.
2095if (open(SOURCE_MAP, "<.tmp_filelist.txt")) {
2096 my ($relname, $absname);
2097 while(<SOURCE_MAP>) {
2098 chop();
2099 ($relname, $absname) = (split())[0..1];
2100 $relname =~ s:^/+::;
2101 $source_map{$relname} = $absname;
2102 }
2103 close(SOURCE_MAP);
2104}
2105
Jani Nikula88c2b572016-06-07 11:00:52 +03002106if ($output_selection == OUTPUT_EXPORTED ||
2107 $output_selection == OUTPUT_INTERNAL) {
Jani Nikulac9b2cfb2016-06-07 11:05:53 +03002108
2109 push(@export_file_list, @ARGV);
2110
Jani Nikula88c2b572016-06-07 11:00:52 +03002111 foreach (@export_file_list) {
2112 chomp;
2113 process_export_file($_);
2114 }
2115}
2116
Randy Dunlap8484baa2011-01-05 16:28:43 -08002117foreach (@ARGV) {
2118 chomp;
2119 process_file($_);
2120}
2121if ($verbose && $errors) {
2122 print STDERR "$errors errors\n";
2123}
2124if ($verbose && $warnings) {
2125 print STDERR "$warnings warnings\n";
2126}
2127
Will Deacone814bcc2017-11-29 15:20:03 +00002128exit($output_mode eq "none" ? 0 : $errors);