blob: 33ad3ce66f73edd8f7cac61a433c86f38c049c50 [file] [log] [blame]
Kamil Rytarowskicb77f0d2017-05-07 23:25:26 +02001#!/usr/bin/env perl
Jonathan Corbet38476372018-02-20 12:24:23 -07002# SPDX-License-Identifier: GPL-2.0
Linus Torvalds1da177e2005-04-16 15:20:36 -07003
Kamil Rytarowskicb77f0d2017-05-07 23:25:26 +02004use warnings;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005use strict;
6
7## Copyright (c) 1998 Michael Zucchi, All Rights Reserved ##
8## Copyright (C) 2000, 1 Tim Waugh <twaugh@redhat.com> ##
9## Copyright (C) 2001 Simon Huggins ##
Randy Dunlap70c95b02012-01-21 10:31:54 -080010## Copyright (C) 2005-2012 Randy Dunlap ##
Dan Luedtke1b40c192012-08-12 10:46:15 +020011## Copyright (C) 2012 Dan Luedtke ##
Linus Torvalds1da177e2005-04-16 15:20:36 -070012## ##
13## #define enhancements by Armin Kuster <akuster@mvista.com> ##
14## Copyright (c) 2000 MontaVista Software, Inc. ##
15## ##
16## This software falls under the GNU General Public License. ##
17## Please read the COPYING file for more information ##
18
Linus Torvalds1da177e2005-04-16 15:20:36 -070019# 18/01/2001 - Cleanups
20# Functions prototyped as foo(void) same as foo()
21# Stop eval'ing where we don't need to.
22# -- huggie@earth.li
23
24# 27/06/2001 - Allowed whitespace after initial "/**" and
25# allowed comments before function declarations.
26# -- Christian Kreibich <ck@whoop.org>
27
28# Still to do:
29# - add perldoc documentation
30# - Look more closely at some of the scarier bits :)
31
32# 26/05/2001 - Support for separate source and object trees.
33# Return error code.
34# Keith Owens <kaos@ocs.com.au>
35
36# 23/09/2001 - Added support for typedefs, structs, enums and unions
37# Support for Context section; can be terminated using empty line
38# Small fixes (like spaces vs. \s in regex)
39# -- Tim Jansen <tim@tjansen.de>
40
Dan Luedtke1b40c192012-08-12 10:46:15 +020041# 25/07/2012 - Added support for HTML5
42# -- Dan Luedtke <mail@danrl.de>
Linus Torvalds1da177e2005-04-16 15:20:36 -070043
Jani Nikulafadc0b32016-05-12 16:15:36 +030044sub usage {
45 my $message = <<"EOF";
46Usage: $0 [OPTION ...] FILE ...
Linus Torvalds1da177e2005-04-16 15:20:36 -070047
Jani Nikulafadc0b32016-05-12 16:15:36 +030048Read C language source or header FILEs, extract embedded documentation comments,
49and print formatted documentation to standard output.
Linus Torvalds1da177e2005-04-16 15:20:36 -070050
Jani Nikulafadc0b32016-05-12 16:15:36 +030051The documentation comments are identified by "/**" opening comment mark. See
Mauro Carvalho Chehab857af3b2017-12-18 10:30:08 -020052Documentation/doc-guide/kernel-doc.rst for the documentation comment syntax.
Jani Nikulafadc0b32016-05-12 16:15:36 +030053
54Output format selection (mutually exclusive):
Jani Nikulafadc0b32016-05-12 16:15:36 +030055 -man Output troff manual page format. This is the default.
Jonathan Corbetc0d1b6e2016-05-12 16:15:37 +030056 -rst Output reStructuredText format.
Matthew Wilcox3a025e12017-11-20 10:40:40 -080057 -none Do not output documentation, only warnings.
Jani Nikulafadc0b32016-05-12 16:15:36 +030058
59Output selection (mutually exclusive):
Jani Nikula86ae2e32016-01-21 13:05:22 +020060 -export Only output documentation for symbols that have been
61 exported using EXPORT_SYMBOL() or EXPORT_SYMBOL_GPL()
Jani Nikulac9b2cfb2016-06-07 11:05:53 +030062 in any input FILE or -export-file FILE.
Jani Nikula86ae2e32016-01-21 13:05:22 +020063 -internal Only output documentation for symbols that have NOT been
64 exported using EXPORT_SYMBOL() or EXPORT_SYMBOL_GPL()
Jani Nikulac9b2cfb2016-06-07 11:05:53 +030065 in any input FILE or -export-file FILE.
Jani Nikulafadc0b32016-05-12 16:15:36 +030066 -function NAME Only output documentation for the given function(s)
67 or DOC: section title(s). All other functions and DOC:
68 sections are ignored. May be specified multiple times.
Mauro Carvalho Chehabeab795d2020-09-29 14:23:39 +020069 -nosymbol NAME Exclude the specified symbols from the output
70 documentation. May be specified multiple times.
Jani Nikulafadc0b32016-05-12 16:15:36 +030071
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.
Pierre-Louis Bossart2c12c812020-07-28 11:20:40 -050083 -Werror Treat warnings as errors.
Jani Nikulafadc0b32016-05-12 16:15:36 +030084
85EOF
86 print $message;
87 exit 1;
88}
Linus Torvalds1da177e2005-04-16 15:20:36 -070089
90#
91# format of comments.
92# In the following table, (...)? signifies optional structure.
93# (...)* signifies 0 or more structure elements
94# /**
95# * function_name(:)? (- short description)?
96# (* @parameterx: (description of parameter x)?)*
97# (* a blank line)?
98# * (Description:)? (Description of function)?
99# * (section header: (section description)? )*
100# (*)?*/
101#
102# So .. the trivial example would be:
103#
104# /**
105# * my_function
Randy Dunlapb9d973282009-06-09 08:50:38 -0700106# */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107#
Randy Dunlap891dcd22007-02-10 01:45:53 -0800108# If the Description: header tag is omitted, then there must be a blank line
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109# after the last parameter specification.
110# e.g.
111# /**
112# * my_function - does my stuff
113# * @my_arg: its mine damnit
114# *
Randy Dunlap3c3b8092006-02-01 03:06:58 -0800115# * Does my stuff explained.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116# */
117#
118# or, could also use:
119# /**
120# * my_function - does my stuff
121# * @my_arg: its mine damnit
Randy Dunlap3c3b8092006-02-01 03:06:58 -0800122# * Description: Does my stuff explained.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123# */
124# etc.
125#
Randy Dunlapb9d973282009-06-09 08:50:38 -0700126# Besides functions you can also write documentation for structs, unions,
Randy Dunlap3c3b8092006-02-01 03:06:58 -0800127# enums and typedefs. Instead of the function name you must write the name
128# of the declaration; the struct/union/enum/typedef must always precede
129# the name. Nesting of declarations is not supported.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130# Use the argument mechanism to document members or constants.
131# e.g.
132# /**
133# * struct my_struct - short description
134# * @a: first member
135# * @b: second member
Randy Dunlap3c3b8092006-02-01 03:06:58 -0800136# *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137# * Longer description
138# */
139# struct my_struct {
140# int a;
141# int b;
Martin Waitzaeec46b2005-11-13 16:08:13 -0800142# /* private: */
143# int c;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144# };
145#
146# All descriptions can be multiline, except the short function description.
Randy Dunlap3c3b8092006-02-01 03:06:58 -0800147#
Danilo Cesar Lemes de Paulaa4c6ebe2015-08-04 09:04:08 -0300148# For really longs structs, you can also describe arguments inside the
149# body of the struct.
150# eg.
151# /**
152# * struct my_struct - short description
153# * @a: first member
154# * @b: second member
155# *
156# * Longer description
157# */
158# struct my_struct {
159# int a;
160# int b;
161# /**
162# * @c: This is longer description of C
163# *
164# * You can use paragraphs to describe arguments
165# * using this method.
166# */
167# int c;
168# };
169#
170# This should be use only for struct/enum members.
171#
Randy Dunlap3c3b8092006-02-01 03:06:58 -0800172# You can also add additional sections. When documenting kernel functions you
173# should document the "Context:" of the function, e.g. whether the functions
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174# can be called form interrupts. Unlike other sections you can end it with an
Randy Dunlap3c3b8092006-02-01 03:06:58 -0800175# empty line.
Yacine Belkadi4092bac2012-11-26 22:22:27 +0100176# A non-void function should have a "Return:" section describing the return
177# value(s).
Randy Dunlap3c3b8092006-02-01 03:06:58 -0800178# Example-sections should contain the string EXAMPLE so that they are marked
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179# appropriately in DocBook.
180#
181# Example:
182# /**
183# * user_function - function that can only be called in user context
184# * @a: some argument
185# * Context: !in_interrupt()
Randy Dunlap3c3b8092006-02-01 03:06:58 -0800186# *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187# * Some description
188# * Example:
189# * user_function(22);
190# */
191# ...
192#
193#
194# All descriptive text is further processed, scanning for the following special
195# patterns, which are highlighted appropriately.
196#
197# 'funcname()' - function
198# '$ENVVAR' - environmental variable
199# '&struct_name' - name of a structure (up to two words including 'struct')
Paolo Bonzini5267dd32017-01-02 16:22:26 +0100200# '&struct_name.member' - name of a structure member
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201# '@parameter' - name of a parameter
202# '%CONST' - name of a constant.
Mauro Carvalho Chehabb97f1932017-03-30 17:11:28 -0300203# '``LITERAL``' - literal string without any spaces on it.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204
Randy Dunlap8484baa2011-01-05 16:28:43 -0800205## init lots of data
206
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207my $errors = 0;
208my $warnings = 0;
Randy Dunlap5f8c7c92007-07-19 01:48:24 -0700209my $anon_struct_union = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210
211# match expressions used to find embedded type information
Mauro Carvalho Chehabb97f1932017-03-30 17:11:28 -0300212my $type_constant = '\b``([^\`]+)``\b';
213my $type_constant2 = '\%([-_\w]+)';
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214my $type_func = '(\w+)\(\)';
Mike Rapoportbfd228c2018-11-07 18:47:13 +0200215my $type_param = '\@(\w*((\.\w+)|(->\w+))*(\.\.\.)?)';
Mauro Carvalho Chehabee2aa752020-04-14 18:48:28 +0200216my $type_param_ref = '([\!]?)\@(\w*((\.\w+)|(->\w+))*(\.\.\.)?)';
Jonathan Corbet5219f182016-08-24 16:31:15 -0600217my $type_fp_param = '\@(\w+)\(\)'; # Special RST handling for func ptr params
Mauro Carvalho Chehab346282d2020-04-14 18:48:27 +0200218my $type_fp_param2 = '\@(\w+->\S+)\(\)'; # Special RST handling for structs with func ptr params
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219my $type_env = '(\$\w+)';
Paolo Bonzinidf311752017-01-02 16:22:27 +0100220my $type_enum = '\&(enum\s*([_\w]+))';
221my $type_struct = '\&(struct\s*([_\w]+))';
222my $type_typedef = '\&(typedef\s*([_\w]+))';
223my $type_union = '\&(union\s*([_\w]+))';
Paolo Bonzini5267dd32017-01-02 16:22:26 +0100224my $type_member = '\&([_\w]+)(\.|->)([_\w]+)';
Paolo Bonzinidf311752017-01-02 16:22:27 +0100225my $type_fallback = '\&([_\w]+)';
Jani Nikulaf3341dc2016-05-26 16:35:02 +0300226my $type_member_func = $type_member . '\(\)';
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227
228# Output conversion substitutions.
229# One for each output format
230
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231# these are pretty rough
Danilo Cesar Lemes de Paula4d732702015-09-07 17:01:59 -0300232my @highlights_man = (
233 [$type_constant, "\$1"],
Mauro Carvalho Chehabb97f1932017-03-30 17:11:28 -0300234 [$type_constant2, "\$1"],
Danilo Cesar Lemes de Paula4d732702015-09-07 17:01:59 -0300235 [$type_func, "\\\\fB\$1\\\\fP"],
Paolo Bonzinidf311752017-01-02 16:22:27 +0100236 [$type_enum, "\\\\fI\$1\\\\fP"],
Danilo Cesar Lemes de Paula4d732702015-09-07 17:01:59 -0300237 [$type_struct, "\\\\fI\$1\\\\fP"],
Paolo Bonzinidf311752017-01-02 16:22:27 +0100238 [$type_typedef, "\\\\fI\$1\\\\fP"],
239 [$type_union, "\\\\fI\$1\\\\fP"],
Paolo Bonzini5267dd32017-01-02 16:22:26 +0100240 [$type_param, "\\\\fI\$1\\\\fP"],
Mauro Carvalho Chehabee2aa752020-04-14 18:48:28 +0200241 [$type_param_ref, "\\\\fI\$1\$2\\\\fP"],
Paolo Bonzinidf311752017-01-02 16:22:27 +0100242 [$type_member, "\\\\fI\$1\$2\$3\\\\fP"],
243 [$type_fallback, "\\\\fI\$1\\\\fP"]
Danilo Cesar Lemes de Paula4d732702015-09-07 17:01:59 -0300244 );
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245my $blankline_man = "";
246
Jonathan Corbetc0d1b6e2016-05-12 16:15:37 +0300247# rst-mode
248my @highlights_rst = (
249 [$type_constant, "``\$1``"],
Mauro Carvalho Chehabb97f1932017-03-30 17:11:28 -0300250 [$type_constant2, "``\$1``"],
Jani Nikulaf3341dc2016-05-26 16:35:02 +0300251 # Note: need to escape () to avoid func matching later
Paolo Bonzini5267dd32017-01-02 16:22:26 +0100252 [$type_member_func, "\\:c\\:type\\:`\$1\$2\$3\\\\(\\\\) <\$1>`"],
253 [$type_member, "\\:c\\:type\\:`\$1\$2\$3 <\$1>`"],
Jonathan Corbet5219f182016-08-24 16:31:15 -0600254 [$type_fp_param, "**\$1\\\\(\\\\)**"],
Mauro Carvalho Chehab346282d2020-04-14 18:48:27 +0200255 [$type_fp_param2, "**\$1\\\\(\\\\)**"],
Jonathan Corbet344fdb22019-06-21 17:34:30 -0600256 [$type_func, "\$1()"],
Paolo Bonzinidf311752017-01-02 16:22:27 +0100257 [$type_enum, "\\:c\\:type\\:`\$1 <\$2>`"],
258 [$type_struct, "\\:c\\:type\\:`\$1 <\$2>`"],
259 [$type_typedef, "\\:c\\:type\\:`\$1 <\$2>`"],
260 [$type_union, "\\:c\\:type\\:`\$1 <\$2>`"],
Jani Nikulaa7291e72016-05-26 13:57:06 +0300261 # in rst this can refer to any type
Paolo Bonzinidf311752017-01-02 16:22:27 +0100262 [$type_fallback, "\\:c\\:type\\:`\$1`"],
Mauro Carvalho Chehabee2aa752020-04-14 18:48:28 +0200263 [$type_param_ref, "**\$1\$2**"]
Jonathan Corbetc0d1b6e2016-05-12 16:15:37 +0300264 );
265my $blankline_rst = "\n";
266
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267# read arguments
Randy Dunlapb9d973282009-06-09 08:50:38 -0700268if ($#ARGV == -1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 usage();
270}
271
Randy Dunlap8484baa2011-01-05 16:28:43 -0800272my $kernelversion;
Mauro Carvalho Chehabefa444752020-09-24 15:30:37 +0200273my $sphinx_major;
274
Randy Dunlap8484baa2011-01-05 16:28:43 -0800275my $dohighlight = "";
276
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277my $verbose = 0;
Pierre-Louis Bossart2c12c812020-07-28 11:20:40 -0500278my $Werror = 0;
Mauro Carvalho Chehabbdfe2be2017-12-18 10:30:11 -0200279my $output_mode = "rst";
Daniel Santose314ba32012-10-04 17:15:08 -0700280my $output_preformatted = 0;
Johannes Berg4b445952007-10-24 15:08:48 -0700281my $no_doc_sections = 0;
Daniel Vetter0b0f5f292016-06-03 22:21:34 +0200282my $enable_lineno = 0;
Mauro Carvalho Chehabbdfe2be2017-12-18 10:30:11 -0200283my @highlights = @highlights_rst;
284my $blankline = $blankline_rst;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285my $modulename = "Kernel API";
Jani Nikulab6c3f452016-05-29 22:19:35 +0300286
287use constant {
288 OUTPUT_ALL => 0, # output all symbols and doc sections
289 OUTPUT_INCLUDE => 1, # output only specified symbols
Mauro Carvalho Chehabeab795d2020-09-29 14:23:39 +0200290 OUTPUT_EXPORTED => 2, # output exported symbols
291 OUTPUT_INTERNAL => 3, # output non-exported symbols
Jani Nikulab6c3f452016-05-29 22:19:35 +0300292};
293my $output_selection = OUTPUT_ALL;
Jonathan Corbetb0d60bf2019-05-24 14:52:01 -0600294my $show_not_found = 0; # No longer used
Ben Hutchingsb2c41052015-07-08 20:07:16 +0100295
Jani Nikula88c2b572016-06-07 11:00:52 +0300296my @export_file_list;
297
Ben Hutchingsb2c41052015-07-08 20:07:16 +0100298my @build_time;
299if (defined($ENV{'KBUILD_BUILD_TIMESTAMP'}) &&
300 (my $seconds = `date -d"${ENV{'KBUILD_BUILD_TIMESTAMP'}}" +%s`) ne '') {
301 @build_time = gmtime($seconds);
302} else {
303 @build_time = localtime;
304}
305
Randy Dunlap3c3b8092006-02-01 03:06:58 -0800306my $man_date = ('January', 'February', 'March', 'April', 'May', 'June',
307 'July', 'August', 'September', 'October',
Ben Hutchingsb2c41052015-07-08 20:07:16 +0100308 'November', 'December')[$build_time[4]] .
309 " " . ($build_time[5]+1900);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310
Randy Dunlap8484baa2011-01-05 16:28:43 -0800311# Essentially these are globals.
Randy Dunlapb9d973282009-06-09 08:50:38 -0700312# They probably want to be tidied up, made more localised or something.
313# CAVEAT EMPTOR! Some of the others I localised may not want to be, which
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314# could cause "use of undefined value" or other bugs.
Randy Dunlapb9d973282009-06-09 08:50:38 -0700315my ($function, %function_table, %parametertypes, $declaration_purpose);
Mauro Carvalho Chehabeab795d2020-09-29 14:23:39 +0200316my %nosymbol_table = ();
Daniel Vetter0b0f5f292016-06-03 22:21:34 +0200317my $declaration_start_line;
Randy Dunlapb9d973282009-06-09 08:50:38 -0700318my ($type, $declaration_name, $return_type);
Ilya Dryomov1c32fd02010-02-26 13:06:03 -0800319my ($newsection, $newcontents, $prototype, $brcount, %source_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320
Randy Dunlapbd0e88e2008-03-13 12:32:43 -0700321if (defined($ENV{'KBUILD_VERBOSE'})) {
322 $verbose = "$ENV{'KBUILD_VERBOSE'}";
323}
324
Pierre-Louis Bossart2c12c812020-07-28 11:20:40 -0500325if (defined($ENV{'KDOC_WERROR'})) {
326 $Werror = "$ENV{'KDOC_WERROR'}";
327}
328
329if (defined($ENV{'KCFLAGS'})) {
330 my $kcflags = "$ENV{'KCFLAGS'}";
331
332 if ($kcflags =~ /Werror/) {
333 $Werror = 1;
334 }
335}
336
Randy Dunlap3c3b8092006-02-01 03:06:58 -0800337# Generated docbook code is inserted in a template at a point where
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338# docbook v3.1 requires a non-zero sequence of RefEntry's; see:
Alexander A. Klimov93431e02020-05-26 08:05:44 +0200339# https://www.oasis-open.org/docbook/documentation/reference/html/refentry.html
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340# We keep track of number of generated entries and generate a dummy
341# if needs be to ensure the expanded template can be postprocessed
342# into html.
343my $section_counter = 0;
344
345my $lineprefix="";
346
Jani Nikula48af6062016-05-26 14:56:05 +0300347# Parser states
348use constant {
Mauro Carvalho Chehab0d55d482020-04-14 18:48:29 +0200349 STATE_NORMAL => 0, # normal code
350 STATE_NAME => 1, # looking for function name
351 STATE_BODY_MAYBE => 2, # body - or maybe more description
352 STATE_BODY => 3, # the body of the comment
353 STATE_BODY_WITH_BLANK_LINE => 4, # the body, which has a blank line
354 STATE_PROTO => 5, # scanning prototype
355 STATE_DOCBLOCK => 6, # documentation block
356 STATE_INLINE => 7, # gathering doc outside main block
Jani Nikula48af6062016-05-26 14:56:05 +0300357};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358my $state;
Randy Dunlap850622d2006-06-25 05:48:55 -0700359my $in_doc_sect;
Jonathan Corbetd742f242018-02-05 15:36:05 -0700360my $leading_space;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361
Jani Nikula48af6062016-05-26 14:56:05 +0300362# Inline documentation state
363use constant {
364 STATE_INLINE_NA => 0, # not applicable ($state != STATE_INLINE)
365 STATE_INLINE_NAME => 1, # looking for member name (@foo:)
366 STATE_INLINE_TEXT => 2, # looking for member documentation
367 STATE_INLINE_END => 3, # done
368 STATE_INLINE_ERROR => 4, # error - Comment without header was found.
369 # Spit a warning as it's not
370 # proper kernel-doc and ignore the rest.
371};
372my $inline_doc_state;
Danilo Cesar Lemes de Paulaa4c6ebe2015-08-04 09:04:08 -0300373
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374#declaration types: can be
375# 'function', 'struct', 'union', 'enum', 'typedef'
376my $decl_type;
377
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378my $doc_start = '^/\*\*\s*$'; # Allow whitespace at end of comment start.
379my $doc_end = '\*/';
380my $doc_com = '\s*\*\s*';
Daniel Santos12ae6772012-10-04 17:15:10 -0700381my $doc_com_body = '\s*\* ?';
Randy Dunlapb9d973282009-06-09 08:50:38 -0700382my $doc_decl = $doc_com . '(\w+)';
Jani Nikulaf624ade2016-05-29 11:35:28 +0300383# @params and a strictly limited set of supported section names
Mike Rapoport76dd3e72018-11-07 18:47:12 +0200384my $doc_sect = $doc_com .
Jonathan Corbetef000282016-08-26 07:14:08 -0600385 '\s*(\@[.\w]+|\@\.\.\.|description|context|returns?|notes?|examples?)\s*:(.*)';
Daniel Santos12ae6772012-10-04 17:15:10 -0700386my $doc_content = $doc_com_body . '(.*)';
Randy Dunlapb9d973282009-06-09 08:50:38 -0700387my $doc_block = $doc_com . 'DOC:\s*(.*)?';
Jani Nikula48af6062016-05-26 14:56:05 +0300388my $doc_inline_start = '^\s*/\*\*\s*$';
Mauro Carvalho Chehabfe7bc492018-02-16 11:48:18 -0200389my $doc_inline_sect = '\s*\*\s*(@\s*[\w][\w\.]*\s*):(.*)';
Jani Nikula48af6062016-05-26 14:56:05 +0300390my $doc_inline_end = '^\s*\*/\s*$';
Jani Nikula0c9aa202016-11-16 17:26:16 +0200391my $doc_inline_oneline = '^\s*/\*\*\s*(@[\w\s]+):\s*(.*)\s*\*/\s*$';
Jani Nikula86ae2e32016-01-21 13:05:22 +0200392my $export_symbol = '^\s*EXPORT_SYMBOL(_GPL)?\s*\(\s*(\w+)\s*\)\s*;';
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394my %parameterdescs;
Daniel Vetter0b0f5f292016-06-03 22:21:34 +0200395my %parameterdesc_start_lines;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396my @parameterlist;
397my %sections;
398my @sectionlist;
Daniel Vetter0b0f5f292016-06-03 22:21:34 +0200399my %section_start_lines;
Randy Dunlapa1d94aa2008-12-19 08:49:30 -0800400my $sectcheck;
401my $struct_actual;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402
403my $contents = "";
Daniel Vetter0b0f5f292016-06-03 22:21:34 +0200404my $new_start_line = 0;
Jani Nikulaf624ade2016-05-29 11:35:28 +0300405
406# the canonical section names. see also $doc_sect above.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407my $section_default = "Description"; # default section
408my $section_intro = "Introduction";
409my $section = $section_default;
410my $section_context = "Context";
Yacine Belkadi4092bac2012-11-26 22:22:27 +0100411my $section_return = "Return";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412
413my $undescribed = "-- undescribed --";
414
415reset_state();
416
Mauro Carvalho Chehabb031ac42017-12-18 10:30:10 -0200417while ($ARGV[0] =~ m/^--?(.*)/) {
418 my $cmd = $1;
419 shift @ARGV;
420 if ($cmd eq "man") {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 $output_mode = "man";
Danilo Cesar Lemes de Paula4d732702015-09-07 17:01:59 -0300422 @highlights = @highlights_man;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 $blankline = $blankline_man;
Mauro Carvalho Chehabb031ac42017-12-18 10:30:10 -0200424 } elsif ($cmd eq "rst") {
Jonathan Corbetc0d1b6e2016-05-12 16:15:37 +0300425 $output_mode = "rst";
426 @highlights = @highlights_rst;
427 $blankline = $blankline_rst;
Mauro Carvalho Chehabb031ac42017-12-18 10:30:10 -0200428 } elsif ($cmd eq "none") {
Matthew Wilcox3a025e12017-11-20 10:40:40 -0800429 $output_mode = "none";
Mauro Carvalho Chehabb031ac42017-12-18 10:30:10 -0200430 } elsif ($cmd eq "module") { # not needed for XML, inherits from calling document
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 $modulename = shift @ARGV;
Mauro Carvalho Chehabb031ac42017-12-18 10:30:10 -0200432 } elsif ($cmd eq "function") { # to only output specific functions
Jani Nikulab6c3f452016-05-29 22:19:35 +0300433 $output_selection = OUTPUT_INCLUDE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 $function = shift @ARGV;
435 $function_table{$function} = 1;
Mauro Carvalho Chehabeab795d2020-09-29 14:23:39 +0200436 } elsif ($cmd eq "nosymbol") { # Exclude specific symbols
437 my $symbol = shift @ARGV;
438 $nosymbol_table{$symbol} = 1;
Mauro Carvalho Chehabb031ac42017-12-18 10:30:10 -0200439 } elsif ($cmd eq "export") { # only exported symbols
Jani Nikulab6c3f452016-05-29 22:19:35 +0300440 $output_selection = OUTPUT_EXPORTED;
Jani Nikulada9726e2016-06-07 10:29:59 +0300441 %function_table = ();
Mauro Carvalho Chehabb031ac42017-12-18 10:30:10 -0200442 } elsif ($cmd eq "internal") { # only non-exported symbols
Jani Nikulab6c3f452016-05-29 22:19:35 +0300443 $output_selection = OUTPUT_INTERNAL;
Jani Nikulada9726e2016-06-07 10:29:59 +0300444 %function_table = ();
Mauro Carvalho Chehabb031ac42017-12-18 10:30:10 -0200445 } elsif ($cmd eq "export-file") {
Jani Nikula88c2b572016-06-07 11:00:52 +0300446 my $file = shift @ARGV;
447 push(@export_file_list, $file);
Mauro Carvalho Chehabb031ac42017-12-18 10:30:10 -0200448 } elsif ($cmd eq "v") {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 $verbose = 1;
Pierre-Louis Bossart2c12c812020-07-28 11:20:40 -0500450 } elsif ($cmd eq "Werror") {
451 $Werror = 1;
Mauro Carvalho Chehabb031ac42017-12-18 10:30:10 -0200452 } elsif (($cmd eq "h") || ($cmd eq "help")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 usage();
Mauro Carvalho Chehabb031ac42017-12-18 10:30:10 -0200454 } elsif ($cmd eq 'no-doc-sections') {
Johannes Berg4b445952007-10-24 15:08:48 -0700455 $no_doc_sections = 1;
Mauro Carvalho Chehabb031ac42017-12-18 10:30:10 -0200456 } elsif ($cmd eq 'enable-lineno') {
Daniel Vetter0b0f5f292016-06-03 22:21:34 +0200457 $enable_lineno = 1;
Mauro Carvalho Chehabb031ac42017-12-18 10:30:10 -0200458 } elsif ($cmd eq 'show-not-found') {
Jonathan Corbetb0d60bf2019-05-24 14:52:01 -0600459 $show_not_found = 1; # A no-op but don't fail
Mauro Carvalho Chehabb031ac42017-12-18 10:30:10 -0200460 } else {
461 # Unknown argument
462 usage();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 }
464}
465
Randy Dunlap8484baa2011-01-05 16:28:43 -0800466# continue execution near EOF;
467
Mauro Carvalho Chehabefa444752020-09-24 15:30:37 +0200468# The C domain dialect changed on Sphinx 3. So, we need to check the
469# version in order to produce the right tags.
470sub findprog($)
471{
472 foreach(split(/:/, $ENV{PATH})) {
473 return "$_/$_[0]" if(-x "$_/$_[0]");
474 }
475}
476
477sub get_sphinx_version()
478{
479 my $ver;
480 my $major = 1;
481
482 my $cmd = "sphinx-build";
483 if (!findprog($cmd)) {
484 my $cmd = "sphinx-build3";
485 return $major if (!findprog($cmd));
486 }
487
488 open IN, "$cmd --version 2>&1 |";
489 while (<IN>) {
490 if (m/^\s*sphinx-build\s+([\d]+)\.([\d\.]+)(\+\/[\da-f]+)?$/) {
491 $major=$1;
492 last;
493 }
494 # Sphinx 1.2.x uses a different format
495 if (m/^\s*Sphinx.*\s+([\d]+)\.([\d\.]+)$/) {
496 $major=$1;
497 last;
498 }
499 }
500 close IN;
501
502 return $major;
503}
504
Borislav Petkov53f049f2007-05-08 00:30:54 -0700505# get kernel version from env
506sub get_kernel_version() {
Johannes Berg1b9bc222007-10-24 15:08:48 -0700507 my $version = 'unknown kernel version';
Borislav Petkov53f049f2007-05-08 00:30:54 -0700508
509 if (defined($ENV{'KERNELVERSION'})) {
510 $version = $ENV{'KERNELVERSION'};
511 }
512 return $version;
513}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514
Daniel Vetter0b0f5f292016-06-03 22:21:34 +0200515#
516sub print_lineno {
517 my $lineno = shift;
518 if ($enable_lineno && defined($lineno)) {
519 print "#define LINENO " . $lineno . "\n";
520 }
521}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522##
523# dumps section contents to arrays/hashes intended for that purpose.
524#
525sub dump_section {
Randy Dunlap94dc7ad2008-04-28 02:16:34 -0700526 my $file = shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 my $name = shift;
528 my $contents = join "\n", @_;
529
Jani Nikula13901ef2016-05-26 08:57:29 +0300530 if ($name =~ m/$type_param/) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 $name = $1;
532 $parameterdescs{$name} = $contents;
Randy Dunlapa1d94aa2008-12-19 08:49:30 -0800533 $sectcheck = $sectcheck . $name . " ";
Daniel Vetter0b0f5f292016-06-03 22:21:34 +0200534 $parameterdesc_start_lines{$name} = $new_start_line;
535 $new_start_line = 0;
Randy Dunlapced69092008-12-01 13:14:03 -0800536 } elsif ($name eq "@\.\.\.") {
Randy Dunlapced69092008-12-01 13:14:03 -0800537 $name = "...";
538 $parameterdescs{$name} = $contents;
Randy Dunlapa1d94aa2008-12-19 08:49:30 -0800539 $sectcheck = $sectcheck . $name . " ";
Daniel Vetter0b0f5f292016-06-03 22:21:34 +0200540 $parameterdesc_start_lines{$name} = $new_start_line;
541 $new_start_line = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 } else {
Randy Dunlap94dc7ad2008-04-28 02:16:34 -0700543 if (defined($sections{$name}) && ($sections{$name} ne "")) {
Jani Nikula95b6be92016-06-10 11:14:05 +0300544 # Only warn on user specified duplicate section names.
545 if ($name ne $section_default) {
546 print STDERR "${file}:$.: warning: duplicate section name '$name'\n";
547 ++$warnings;
548 }
Jani Nikula32217762016-05-29 09:40:44 +0300549 $sections{$name} .= $contents;
550 } else {
551 $sections{$name} = $contents;
552 push @sectionlist, $name;
Daniel Vetter0b0f5f292016-06-03 22:21:34 +0200553 $section_start_lines{$name} = $new_start_line;
554 $new_start_line = 0;
Randy Dunlap94dc7ad2008-04-28 02:16:34 -0700555 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 }
557}
558
559##
Johannes Bergb112e0f2007-10-24 15:08:48 -0700560# dump DOC: section after checking that it should go out
561#
562sub dump_doc_section {
Randy Dunlap94dc7ad2008-04-28 02:16:34 -0700563 my $file = shift;
Johannes Bergb112e0f2007-10-24 15:08:48 -0700564 my $name = shift;
565 my $contents = join "\n", @_;
566
Johannes Berg4b445952007-10-24 15:08:48 -0700567 if ($no_doc_sections) {
568 return;
569 }
570
Mauro Carvalho Chehabeab795d2020-09-29 14:23:39 +0200571 return if (defined($nosymbol_table{$name}));
572
Jani Nikulab6c3f452016-05-29 22:19:35 +0300573 if (($output_selection == OUTPUT_ALL) ||
Mauro Carvalho Chehabeab795d2020-09-29 14:23:39 +0200574 (($output_selection == OUTPUT_INCLUDE) &&
575 defined($function_table{$name})))
Johannes Bergb112e0f2007-10-24 15:08:48 -0700576 {
Randy Dunlap94dc7ad2008-04-28 02:16:34 -0700577 dump_section($file, $name, $contents);
Johannes Bergb112e0f2007-10-24 15:08:48 -0700578 output_blockhead({'sectionlist' => \@sectionlist,
579 'sections' => \%sections,
580 'module' => $modulename,
Jani Nikulab6c3f452016-05-29 22:19:35 +0300581 'content-only' => ($output_selection != OUTPUT_ALL), });
Johannes Bergb112e0f2007-10-24 15:08:48 -0700582 }
583}
584
585##
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586# output function
587#
588# parameterdescs, a hash.
589# function => "function name"
590# parameterlist => @list of parameters
591# parameterdescs => %parameter descriptions
592# sectionlist => @list of sections
Randy Dunlapa21217d2007-02-10 01:46:04 -0800593# sections => %section descriptions
Randy Dunlap3c3b8092006-02-01 03:06:58 -0800594#
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595
596sub output_highlight {
597 my $contents = join "\n",@_;
598 my $line;
599
600# DEBUG
601# if (!defined $contents) {
602# use Carp;
603# confess "output_highlight got called with no args?\n";
604# }
605
Randy Dunlap3eb014a2007-05-08 00:29:51 -0700606# print STDERR "contents b4:$contents\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 eval $dohighlight;
608 die $@ if $@;
Randy Dunlap3eb014a2007-05-08 00:29:51 -0700609# print STDERR "contents af:$contents\n";
610
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 foreach $line (split "\n", $contents) {
Daniel Santos12ae6772012-10-04 17:15:10 -0700612 if (! $output_preformatted) {
613 $line =~ s/^\s*//;
614 }
Randy Dunlap3c308792007-05-08 00:24:39 -0700615 if ($line eq ""){
Daniel Santose314ba32012-10-04 17:15:08 -0700616 if (! $output_preformatted) {
Jonathan Corbet0bba9242018-02-05 12:40:15 -0700617 print $lineprefix, $blankline;
Daniel Santose314ba32012-10-04 17:15:08 -0700618 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 } else {
Randy Dunlapcdccb312007-07-19 01:48:25 -0700620 if ($output_mode eq "man" && substr($line, 0, 1) eq ".") {
621 print "\\&$line";
622 } else {
623 print $lineprefix, $line;
624 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 }
626 print "\n";
627 }
628}
629
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630##
631# output function in man
632sub output_function_man(%) {
633 my %args = %{$_[0]};
634 my ($parameter, $section);
635 my $count;
636
637 print ".TH \"$args{'function'}\" 9 \"$args{'function'}\" \"$man_date\" \"Kernel Hacker's Manual\" LINUX\n";
638
639 print ".SH NAME\n";
Randy Dunlapb9d973282009-06-09 08:50:38 -0700640 print $args{'function'} . " \\- " . $args{'purpose'} . "\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641
642 print ".SH SYNOPSIS\n";
Randy Dunlapa21217d2007-02-10 01:46:04 -0800643 if ($args{'functiontype'} ne "") {
Randy Dunlapb9d973282009-06-09 08:50:38 -0700644 print ".B \"" . $args{'functiontype'} . "\" " . $args{'function'} . "\n";
Randy Dunlapa21217d2007-02-10 01:46:04 -0800645 } else {
Randy Dunlapb9d973282009-06-09 08:50:38 -0700646 print ".B \"" . $args{'function'} . "\n";
Randy Dunlapa21217d2007-02-10 01:46:04 -0800647 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 $count = 0;
649 my $parenth = "(";
650 my $post = ",";
651 foreach my $parameter (@{$args{'parameterlist'}}) {
652 if ($count == $#{$args{'parameterlist'}}) {
653 $post = ");";
654 }
655 $type = $args{'parametertypes'}{$parameter};
656 if ($type =~ m/([^\(]*\(\*)\s*\)\s*\(([^\)]*)\)/) {
657 # pointer-to-function
Randy Dunlapb9d973282009-06-09 08:50:38 -0700658 print ".BI \"" . $parenth . $1 . "\" " . $parameter . " \") (" . $2 . ")" . $post . "\"\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 } else {
660 $type =~ s/([^\*])$/$1 /;
Randy Dunlapb9d973282009-06-09 08:50:38 -0700661 print ".BI \"" . $parenth . $type . "\" " . $parameter . " \"" . $post . "\"\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 }
663 $count++;
664 $parenth = "";
665 }
666
667 print ".SH ARGUMENTS\n";
668 foreach $parameter (@{$args{'parameterlist'}}) {
669 my $parameter_name = $parameter;
670 $parameter_name =~ s/\[.*//;
671
Randy Dunlapb9d973282009-06-09 08:50:38 -0700672 print ".IP \"" . $parameter . "\" 12\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 output_highlight($args{'parameterdescs'}{$parameter_name});
674 }
675 foreach $section (@{$args{'sectionlist'}}) {
676 print ".SH \"", uc $section, "\"\n";
677 output_highlight($args{'sections'}{$section});
678 }
679}
680
681##
682# output enum in man
683sub output_enum_man(%) {
684 my %args = %{$_[0]};
685 my ($parameter, $section);
686 my $count;
687
688 print ".TH \"$args{'module'}\" 9 \"enum $args{'enum'}\" \"$man_date\" \"API Manual\" LINUX\n";
689
690 print ".SH NAME\n";
Randy Dunlapb9d973282009-06-09 08:50:38 -0700691 print "enum " . $args{'enum'} . " \\- " . $args{'purpose'} . "\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692
693 print ".SH SYNOPSIS\n";
Randy Dunlapb9d973282009-06-09 08:50:38 -0700694 print "enum " . $args{'enum'} . " {\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 $count = 0;
696 foreach my $parameter (@{$args{'parameterlist'}}) {
Randy Dunlap3c308792007-05-08 00:24:39 -0700697 print ".br\n.BI \" $parameter\"\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 if ($count == $#{$args{'parameterlist'}}) {
699 print "\n};\n";
700 last;
701 }
702 else {
703 print ", \n.br\n";
704 }
705 $count++;
706 }
707
708 print ".SH Constants\n";
709 foreach $parameter (@{$args{'parameterlist'}}) {
710 my $parameter_name = $parameter;
711 $parameter_name =~ s/\[.*//;
712
Randy Dunlapb9d973282009-06-09 08:50:38 -0700713 print ".IP \"" . $parameter . "\" 12\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 output_highlight($args{'parameterdescs'}{$parameter_name});
715 }
716 foreach $section (@{$args{'sectionlist'}}) {
717 print ".SH \"$section\"\n";
718 output_highlight($args{'sections'}{$section});
719 }
720}
721
722##
723# output struct in man
724sub output_struct_man(%) {
725 my %args = %{$_[0]};
726 my ($parameter, $section);
727
Randy Dunlapb9d973282009-06-09 08:50:38 -0700728 print ".TH \"$args{'module'}\" 9 \"" . $args{'type'} . " " . $args{'struct'} . "\" \"$man_date\" \"API Manual\" LINUX\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729
730 print ".SH NAME\n";
Randy Dunlapb9d973282009-06-09 08:50:38 -0700731 print $args{'type'} . " " . $args{'struct'} . " \\- " . $args{'purpose'} . "\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732
Mauro Carvalho Chehab8ad72162017-12-18 10:30:13 -0200733 my $declaration = $args{'definition'};
734 $declaration =~ s/\t/ /g;
735 $declaration =~ s/\n/"\n.br\n.BI \"/g;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 print ".SH SYNOPSIS\n";
Randy Dunlapb9d973282009-06-09 08:50:38 -0700737 print $args{'type'} . " " . $args{'struct'} . " {\n.br\n";
Mauro Carvalho Chehab8ad72162017-12-18 10:30:13 -0200738 print ".BI \"$declaration\n};\n.br\n\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739
Randy Dunlapc51d3dac2006-06-25 05:49:14 -0700740 print ".SH Members\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 foreach $parameter (@{$args{'parameterlist'}}) {
742 ($parameter =~ /^#/) && next;
743
744 my $parameter_name = $parameter;
745 $parameter_name =~ s/\[.*//;
746
Randy Dunlap3c308792007-05-08 00:24:39 -0700747 ($args{'parameterdescs'}{$parameter_name} ne $undescribed) || next;
Randy Dunlapb9d973282009-06-09 08:50:38 -0700748 print ".IP \"" . $parameter . "\" 12\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 output_highlight($args{'parameterdescs'}{$parameter_name});
750 }
751 foreach $section (@{$args{'sectionlist'}}) {
752 print ".SH \"$section\"\n";
753 output_highlight($args{'sections'}{$section});
754 }
755}
756
757##
758# output typedef in man
759sub output_typedef_man(%) {
760 my %args = %{$_[0]};
761 my ($parameter, $section);
762
763 print ".TH \"$args{'module'}\" 9 \"$args{'typedef'}\" \"$man_date\" \"API Manual\" LINUX\n";
764
765 print ".SH NAME\n";
Randy Dunlapb9d973282009-06-09 08:50:38 -0700766 print "typedef " . $args{'typedef'} . " \\- " . $args{'purpose'} . "\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767
768 foreach $section (@{$args{'sectionlist'}}) {
769 print ".SH \"$section\"\n";
770 output_highlight($args{'sections'}{$section});
771 }
772}
773
Johannes Bergb112e0f2007-10-24 15:08:48 -0700774sub output_blockhead_man(%) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 my %args = %{$_[0]};
776 my ($parameter, $section);
777 my $count;
778
779 print ".TH \"$args{'module'}\" 9 \"$args{'module'}\" \"$man_date\" \"API Manual\" LINUX\n";
780
781 foreach $section (@{$args{'sectionlist'}}) {
782 print ".SH \"$section\"\n";
783 output_highlight($args{'sections'}{$section});
784 }
785}
786
787##
Jonathan Corbetc0d1b6e2016-05-12 16:15:37 +0300788# output in restructured text
789#
790
791#
792# This could use some work; it's used to output the DOC: sections, and
793# starts by putting out the name of the doc section itself, but that tends
794# to duplicate a header already in the template file.
795#
796sub output_blockhead_rst(%) {
797 my %args = %{$_[0]};
798 my ($parameter, $section);
799
800 foreach $section (@{$args{'sectionlist'}}) {
Mauro Carvalho Chehabeab795d2020-09-29 14:23:39 +0200801 next if (defined($nosymbol_table{$section}));
802
Jani Nikula9e721842016-05-29 22:27:35 +0300803 if ($output_selection != OUTPUT_INCLUDE) {
804 print "**$section**\n\n";
805 }
Daniel Vetter0b0f5f292016-06-03 22:21:34 +0200806 print_lineno($section_start_lines{$section});
Jonathan Corbetc0d1b6e2016-05-12 16:15:37 +0300807 output_highlight_rst($args{'sections'}{$section});
808 print "\n";
809 }
810}
811
Jonathan Corbetaf250292018-02-06 15:58:45 -0700812#
813# Apply the RST highlights to a sub-block of text.
Mike Rapoport76dd3e72018-11-07 18:47:12 +0200814#
Jonathan Corbetaf250292018-02-06 15:58:45 -0700815sub highlight_block($) {
816 # The dohighlight kludge requires the text be called $contents
817 my $contents = shift;
Jonathan Corbetc0d1b6e2016-05-12 16:15:37 +0300818 eval $dohighlight;
819 die $@ if $@;
Jonathan Corbetaf250292018-02-06 15:58:45 -0700820 return $contents;
821}
Jonathan Corbetc0d1b6e2016-05-12 16:15:37 +0300822
Jonathan Corbetaf250292018-02-06 15:58:45 -0700823#
824# Regexes used only here.
825#
826my $sphinx_literal = '^[^.].*::$';
827my $sphinx_cblock = '^\.\.\ +code-block::';
828
829sub output_highlight_rst {
830 my $input = join "\n",@_;
831 my $output = "";
832 my $line;
833 my $in_literal = 0;
834 my $litprefix;
835 my $block = "";
836
837 foreach $line (split "\n",$input) {
838 #
839 # If we're in a literal block, see if we should drop out
840 # of it. Otherwise pass the line straight through unmunged.
841 #
842 if ($in_literal) {
843 if (! ($line =~ /^\s*$/)) {
844 #
845 # If this is the first non-blank line in a literal
846 # block we need to figure out what the proper indent is.
847 #
848 if ($litprefix eq "") {
849 $line =~ /^(\s*)/;
850 $litprefix = '^' . $1;
851 $output .= $line . "\n";
852 } elsif (! ($line =~ /$litprefix/)) {
853 $in_literal = 0;
854 } else {
855 $output .= $line . "\n";
856 }
857 } else {
858 $output .= $line . "\n";
859 }
860 }
861 #
862 # Not in a literal block (or just dropped out)
863 #
864 if (! $in_literal) {
865 $block .= $line . "\n";
866 if (($line =~ /$sphinx_literal/) || ($line =~ /$sphinx_cblock/)) {
867 $in_literal = 1;
868 $litprefix = "";
869 $output .= highlight_block($block);
870 $block = ""
871 }
872 }
873 }
874
875 if ($block) {
876 $output .= highlight_block($block);
877 }
878 foreach $line (split "\n", $output) {
Jani Nikula830066a2016-05-26 22:04:33 +0300879 print $lineprefix . $line . "\n";
Jonathan Corbetc0d1b6e2016-05-12 16:15:37 +0300880 }
881}
882
883sub output_function_rst(%) {
884 my %args = %{$_[0]};
885 my ($parameter, $section);
Jani Nikulac099ff62016-05-26 17:18:17 +0300886 my $oldprefix = $lineprefix;
Mauro Carvalho Chehab82801d02016-08-30 20:20:58 -0300887 my $start = "";
Jonathan Corbetc0d1b6e2016-05-12 16:15:37 +0300888
Mauro Carvalho Chehabe3ad05f2020-09-25 10:05:40 +0200889 if ($sphinx_major < 3) {
890 if ($args{'typedef'}) {
Mauro Carvalho Chehabefa444752020-09-24 15:30:37 +0200891 print ".. c:type:: ". $args{'function'} . "\n\n";
Mauro Carvalho Chehabe3ad05f2020-09-25 10:05:40 +0200892 print_lineno($declaration_start_line);
893 print " **Typedef**: ";
894 $lineprefix = "";
895 output_highlight_rst($args{'purpose'});
896 $start = "\n\n**Syntax**\n\n ``";
Mauro Carvalho Chehabefa444752020-09-24 15:30:37 +0200897 } else {
Mauro Carvalho Chehabe3ad05f2020-09-25 10:05:40 +0200898 print ".. c:function:: ";
Mauro Carvalho Chehabefa444752020-09-24 15:30:37 +0200899 }
Jonathan Corbetc0d1b6e2016-05-12 16:15:37 +0300900 } else {
Mauro Carvalho Chehabe3ad05f2020-09-25 10:05:40 +0200901 print ".. c:macro:: ". $args{'function'} . "\n\n";
902
903 if ($args{'typedef'}) {
904 print_lineno($declaration_start_line);
905 print " **Typedef**: ";
906 $lineprefix = "";
907 output_highlight_rst($args{'purpose'});
908 $start = "\n\n**Syntax**\n\n ``";
909 } else {
910 print "``";
911 }
Mauro Carvalho Chehab82801d02016-08-30 20:20:58 -0300912 }
913 if ($args{'functiontype'} ne "") {
914 $start .= $args{'functiontype'} . " " . $args{'function'} . " (";
915 } else {
916 $start .= $args{'function'} . " (";
Jonathan Corbetc0d1b6e2016-05-12 16:15:37 +0300917 }
918 print $start;
919
920 my $count = 0;
921 foreach my $parameter (@{$args{'parameterlist'}}) {
922 if ($count ne 0) {
923 print ", ";
924 }
925 $count++;
926 $type = $args{'parametertypes'}{$parameter};
Mauro Carvalho Chehaba88b1672016-07-22 11:46:36 -0300927
Jonathan Corbetc0d1b6e2016-05-12 16:15:37 +0300928 if ($type =~ m/([^\(]*\(\*)\s*\)\s*\(([^\)]*)\)/) {
929 # pointer-to-function
Peter Maydelle8f4ba82020-04-14 15:37:43 +0100930 print $1 . $parameter . ") (" . $2 . ")";
Jonathan Corbetc0d1b6e2016-05-12 16:15:37 +0300931 } else {
932 print $type . " " . $parameter;
933 }
934 }
Mauro Carvalho Chehab82801d02016-08-30 20:20:58 -0300935 if ($args{'typedef'}) {
936 print ");``\n\n";
937 } else {
Mauro Carvalho Chehabe3ad05f2020-09-25 10:05:40 +0200938 if ($sphinx_major < 3) {
939 print ")\n\n";
940 } else {
941 print ")``\n";
942 }
Mauro Carvalho Chehab82801d02016-08-30 20:20:58 -0300943 print_lineno($declaration_start_line);
944 $lineprefix = " ";
945 output_highlight_rst($args{'purpose'});
946 print "\n";
947 }
Jonathan Corbetc0d1b6e2016-05-12 16:15:37 +0300948
Jani Nikulaecbcfba2016-05-26 18:30:27 +0300949 print "**Parameters**\n\n";
950 $lineprefix = " ";
Jonathan Corbetc0d1b6e2016-05-12 16:15:37 +0300951 foreach $parameter (@{$args{'parameterlist'}}) {
952 my $parameter_name = $parameter;
Gabriel Krisman Bertaziada5f442017-01-09 18:11:57 -0200953 $parameter_name =~ s/\[.*//;
Jonathan Corbetc0d1b6e2016-05-12 16:15:37 +0300954 $type = $args{'parametertypes'}{$parameter};
955
956 if ($type ne "") {
Jani Nikulaecbcfba2016-05-26 18:30:27 +0300957 print "``$type $parameter``\n";
Jonathan Corbetc0d1b6e2016-05-12 16:15:37 +0300958 } else {
Jani Nikulaecbcfba2016-05-26 18:30:27 +0300959 print "``$parameter``\n";
Jonathan Corbetc0d1b6e2016-05-12 16:15:37 +0300960 }
Daniel Vetter0b0f5f292016-06-03 22:21:34 +0200961
962 print_lineno($parameterdesc_start_lines{$parameter_name});
963
Jani Nikula5e64fa92016-05-19 20:32:48 +0300964 if (defined($args{'parameterdescs'}{$parameter_name}) &&
965 $args{'parameterdescs'}{$parameter_name} ne $undescribed) {
Jonathan Corbetc0d1b6e2016-05-12 16:15:37 +0300966 output_highlight_rst($args{'parameterdescs'}{$parameter_name});
Jonathan Corbetc0d1b6e2016-05-12 16:15:37 +0300967 } else {
Jani Nikulad4b08e02016-05-28 00:48:17 +0300968 print " *undescribed*\n";
Jonathan Corbetc0d1b6e2016-05-12 16:15:37 +0300969 }
970 print "\n";
971 }
Jani Nikulac099ff62016-05-26 17:18:17 +0300972
973 $lineprefix = $oldprefix;
Jonathan Corbetc0d1b6e2016-05-12 16:15:37 +0300974 output_section_rst(@_);
975}
976
977sub output_section_rst(%) {
978 my %args = %{$_[0]};
979 my $section;
980 my $oldprefix = $lineprefix;
Jani Nikulaecbcfba2016-05-26 18:30:27 +0300981 $lineprefix = "";
Jonathan Corbetc0d1b6e2016-05-12 16:15:37 +0300982
983 foreach $section (@{$args{'sectionlist'}}) {
Jani Nikulaecbcfba2016-05-26 18:30:27 +0300984 print "**$section**\n\n";
Daniel Vetter0b0f5f292016-06-03 22:21:34 +0200985 print_lineno($section_start_lines{$section});
Jonathan Corbetc0d1b6e2016-05-12 16:15:37 +0300986 output_highlight_rst($args{'sections'}{$section});
987 print "\n";
988 }
989 print "\n";
990 $lineprefix = $oldprefix;
991}
992
993sub output_enum_rst(%) {
994 my %args = %{$_[0]};
995 my ($parameter);
Jani Nikulac099ff62016-05-26 17:18:17 +0300996 my $oldprefix = $lineprefix;
Jonathan Corbetc0d1b6e2016-05-12 16:15:37 +0300997 my $count;
Jani Nikula62850972016-05-12 16:15:38 +0300998
Mauro Carvalho Chehabefa444752020-09-24 15:30:37 +0200999 if ($sphinx_major < 3) {
1000 my $name = "enum " . $args{'enum'};
1001 print "\n\n.. c:type:: " . $name . "\n\n";
1002 } else {
1003 my $name = $args{'enum'};
1004 print "\n\n.. c:enum:: " . $name . "\n\n";
1005 }
Daniel Vetter0b0f5f292016-06-03 22:21:34 +02001006 print_lineno($declaration_start_line);
Jani Nikulac099ff62016-05-26 17:18:17 +03001007 $lineprefix = " ";
1008 output_highlight_rst($args{'purpose'});
1009 print "\n";
Jonathan Corbetc0d1b6e2016-05-12 16:15:37 +03001010
Jani Nikulaecbcfba2016-05-26 18:30:27 +03001011 print "**Constants**\n\n";
1012 $lineprefix = " ";
Jonathan Corbetc0d1b6e2016-05-12 16:15:37 +03001013 foreach $parameter (@{$args{'parameterlist'}}) {
Jani Nikulaecbcfba2016-05-26 18:30:27 +03001014 print "``$parameter``\n";
Jonathan Corbetc0d1b6e2016-05-12 16:15:37 +03001015 if ($args{'parameterdescs'}{$parameter} ne $undescribed) {
1016 output_highlight_rst($args{'parameterdescs'}{$parameter});
1017 } else {
Jani Nikulad4b08e02016-05-28 00:48:17 +03001018 print " *undescribed*\n";
Jonathan Corbetc0d1b6e2016-05-12 16:15:37 +03001019 }
1020 print "\n";
1021 }
Jani Nikulac099ff62016-05-26 17:18:17 +03001022
Jonathan Corbetc0d1b6e2016-05-12 16:15:37 +03001023 $lineprefix = $oldprefix;
1024 output_section_rst(@_);
1025}
1026
1027sub output_typedef_rst(%) {
1028 my %args = %{$_[0]};
1029 my ($parameter);
Jani Nikulac099ff62016-05-26 17:18:17 +03001030 my $oldprefix = $lineprefix;
Mauro Carvalho Chehabefa444752020-09-24 15:30:37 +02001031 my $name;
Jonathan Corbetc0d1b6e2016-05-12 16:15:37 +03001032
Mauro Carvalho Chehabefa444752020-09-24 15:30:37 +02001033 if ($sphinx_major < 3) {
1034 $name = "typedef " . $args{'typedef'};
1035 } else {
1036 $name = $args{'typedef'};
1037 }
Jani Nikula62850972016-05-12 16:15:38 +03001038 print "\n\n.. c:type:: " . $name . "\n\n";
Daniel Vetter0b0f5f292016-06-03 22:21:34 +02001039 print_lineno($declaration_start_line);
Jani Nikulac099ff62016-05-26 17:18:17 +03001040 $lineprefix = " ";
1041 output_highlight_rst($args{'purpose'});
1042 print "\n";
Jonathan Corbetc0d1b6e2016-05-12 16:15:37 +03001043
Jani Nikulac099ff62016-05-26 17:18:17 +03001044 $lineprefix = $oldprefix;
Jonathan Corbetc0d1b6e2016-05-12 16:15:37 +03001045 output_section_rst(@_);
1046}
1047
1048sub output_struct_rst(%) {
1049 my %args = %{$_[0]};
1050 my ($parameter);
Jani Nikulac099ff62016-05-26 17:18:17 +03001051 my $oldprefix = $lineprefix;
Jonathan Corbetc0d1b6e2016-05-12 16:15:37 +03001052
Mauro Carvalho Chehabefa444752020-09-24 15:30:37 +02001053 if ($sphinx_major < 3) {
1054 my $name = $args{'type'} . " " . $args{'struct'};
1055 print "\n\n.. c:type:: " . $name . "\n\n";
1056 } else {
1057 my $name = $args{'struct'};
1058 print "\n\n.. c:struct:: " . $name . "\n\n";
1059 }
Daniel Vetter0b0f5f292016-06-03 22:21:34 +02001060 print_lineno($declaration_start_line);
Jani Nikulac099ff62016-05-26 17:18:17 +03001061 $lineprefix = " ";
1062 output_highlight_rst($args{'purpose'});
1063 print "\n";
Jonathan Corbetc0d1b6e2016-05-12 16:15:37 +03001064
Jani Nikulaecbcfba2016-05-26 18:30:27 +03001065 print "**Definition**\n\n";
1066 print "::\n\n";
Mauro Carvalho Chehab8ad72162017-12-18 10:30:13 -02001067 my $declaration = $args{'definition'};
1068 $declaration =~ s/\t/ /g;
1069 print " " . $args{'type'} . " " . $args{'struct'} . " {\n$declaration };\n\n";
Jonathan Corbetc0d1b6e2016-05-12 16:15:37 +03001070
Jani Nikulaecbcfba2016-05-26 18:30:27 +03001071 print "**Members**\n\n";
1072 $lineprefix = " ";
Jonathan Corbetc0d1b6e2016-05-12 16:15:37 +03001073 foreach $parameter (@{$args{'parameterlist'}}) {
1074 ($parameter =~ /^#/) && next;
1075
1076 my $parameter_name = $parameter;
1077 $parameter_name =~ s/\[.*//;
1078
1079 ($args{'parameterdescs'}{$parameter_name} ne $undescribed) || next;
1080 $type = $args{'parametertypes'}{$parameter};
Daniel Vetter0b0f5f292016-06-03 22:21:34 +02001081 print_lineno($parameterdesc_start_lines{$parameter_name});
Mauro Carvalho Chehab6d232c82016-08-22 22:02:57 -03001082 print "``" . $parameter . "``\n";
Jonathan Corbetc0d1b6e2016-05-12 16:15:37 +03001083 output_highlight_rst($args{'parameterdescs'}{$parameter_name});
Jonathan Corbetc0d1b6e2016-05-12 16:15:37 +03001084 print "\n";
1085 }
1086 print "\n";
Jani Nikulac099ff62016-05-26 17:18:17 +03001087
1088 $lineprefix = $oldprefix;
Jonathan Corbetc0d1b6e2016-05-12 16:15:37 +03001089 output_section_rst(@_);
1090}
1091
Matthew Wilcox3a025e12017-11-20 10:40:40 -08001092## none mode output functions
1093
1094sub output_function_none(%) {
1095}
1096
1097sub output_enum_none(%) {
1098}
1099
1100sub output_typedef_none(%) {
1101}
1102
1103sub output_struct_none(%) {
1104}
1105
1106sub output_blockhead_none(%) {
1107}
1108
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109##
Randy Dunlap27205742006-10-11 01:22:12 -07001110# generic output function for all types (function, struct/union, typedef, enum);
1111# calls the generated, variable output_ function name based on
1112# functype and output_mode
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113sub output_declaration {
1114 no strict 'refs';
1115 my $name = shift;
1116 my $functype = shift;
1117 my $func = "output_${functype}_$output_mode";
Mauro Carvalho Chehabeab795d2020-09-29 14:23:39 +02001118
1119 return if (defined($nosymbol_table{$name}));
1120
Jani Nikulab6c3f452016-05-29 22:19:35 +03001121 if (($output_selection == OUTPUT_ALL) ||
1122 (($output_selection == OUTPUT_INCLUDE ||
1123 $output_selection == OUTPUT_EXPORTED) &&
1124 defined($function_table{$name})) ||
Mauro Carvalho Chehabeab795d2020-09-29 14:23:39 +02001125 ($output_selection == OUTPUT_INTERNAL &&
Jani Nikulab6c3f452016-05-29 22:19:35 +03001126 !($functype eq "function" && defined($function_table{$name}))))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127 {
Randy Dunlap3c308792007-05-08 00:24:39 -07001128 &$func(@_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129 $section_counter++;
1130 }
1131}
1132
1133##
Randy Dunlap27205742006-10-11 01:22:12 -07001134# generic output function - calls the right one based on current output mode.
Johannes Bergb112e0f2007-10-24 15:08:48 -07001135sub output_blockhead {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136 no strict 'refs';
Randy Dunlapb9d973282009-06-09 08:50:38 -07001137 my $func = "output_blockhead_" . $output_mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138 &$func(@_);
1139 $section_counter++;
1140}
1141
1142##
Randy Dunlap3c3b8092006-02-01 03:06:58 -08001143# takes a declaration (struct, union, enum, typedef) and
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144# invokes the right handler. NOT called for functions.
1145sub dump_declaration($$) {
1146 no strict 'refs';
1147 my ($prototype, $file) = @_;
Randy Dunlapb9d973282009-06-09 08:50:38 -07001148 my $func = "dump_" . $decl_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149 &$func(@_);
1150}
1151
1152sub dump_union($$) {
1153 dump_struct(@_);
1154}
1155
1156sub dump_struct($$) {
1157 my $x = shift;
1158 my $file = shift;
1159
Jonathan Camerona0709912020-09-10 19:54:15 +01001160 if ($x =~ /(struct|union)\s+(\w+)\s*\{(.*)\}(\s*(__packed|__aligned|____cacheline_aligned_in_smp|____cacheline_aligned|__attribute__\s*\(\([a-z0-9,_\s\(\)]*\)\)))*/) {
Johannes Berg5cb5c312017-09-19 13:08:13 +02001161 my $decl_type = $1;
Randy Dunlap3c308792007-05-08 00:24:39 -07001162 $declaration_name = $2;
1163 my $members = $3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164
Martin Waitzaeec46b2005-11-13 16:08:13 -08001165 # ignore members marked private:
Mauro Carvalho Chehab0d8c39e2015-10-05 09:03:48 -03001166 $members =~ s/\/\*\s*private:.*?\/\*\s*public:.*?\*\///gosi;
1167 $members =~ s/\/\*\s*private:.*//gosi;
Martin Waitzaeec46b2005-11-13 16:08:13 -08001168 # strip comments:
1169 $members =~ s/\/\*.*?\*\///gos;
Randy Dunlapef5da592010-03-23 13:35:14 -07001170 # strip attributes
André Almeida2b5f78e2019-09-17 16:41:45 -03001171 $members =~ s/\s*__attribute__\s*\(\([a-z0-9,_\*\s\(\)]*\)\)/ /gi;
1172 $members =~ s/\s*__aligned\s*\([^;]*\)/ /gos;
1173 $members =~ s/\s*__packed\s*/ /gos;
1174 $members =~ s/\s*CRYPTO_MINALIGN_ATTR/ /gos;
André Almeidaf8615372019-09-17 16:41:46 -03001175 $members =~ s/\s*____cacheline_aligned_in_smp/ /gos;
Jonathan Camerona0709912020-09-10 19:54:15 +01001176 $members =~ s/\s*____cacheline_aligned/ /gos;
Mauro Carvalho Chehab35561082020-06-23 09:09:00 +02001177
Conchúr Navidb22b5a92015-11-08 10:52:00 +01001178 # replace DECLARE_BITMAP
Mauro Carvalho Chehab35561082020-06-23 09:09:00 +02001179 $members =~ s/__ETHTOOL_DECLARE_LINK_MODE_MASK\s*\(([^\)]+)\)/DECLARE_BITMAP($1, __ETHTOOL_LINK_MODE_MASK_NBITS)/gos;
Mauro Carvalho Chehab45005b22017-12-08 09:05:12 -05001180 $members =~ s/DECLARE_BITMAP\s*\(([^,)]+),\s*([^,)]+)\)/unsigned long $1\[BITS_TO_LONGS($2)\]/gos;
Jakub Kicinski1cb566b2017-06-30 19:09:59 -07001181 # replace DECLARE_HASHTABLE
Mauro Carvalho Chehab45005b22017-12-08 09:05:12 -05001182 $members =~ s/DECLARE_HASHTABLE\s*\(([^,)]+),\s*([^,)]+)\)/unsigned long $1\[1 << (($2) - 1)\]/gos;
1183 # replace DECLARE_KFIFO
1184 $members =~ s/DECLARE_KFIFO\s*\(([^,)]+),\s*([^,)]+),\s*([^,)]+)\)/$2 \*$1/gos;
1185 # replace DECLARE_KFIFO_PTR
1186 $members =~ s/DECLARE_KFIFO_PTR\s*\(([^,)]+),\s*([^,)]+)\)/$2 \*$1/gos;
Martin Waitzaeec46b2005-11-13 16:08:13 -08001187
Mauro Carvalho Chehab8ad72162017-12-18 10:30:13 -02001188 my $declaration = $members;
1189
1190 # Split nested struct/union elements as newer ones
Mauro Carvalho Chehab84ce5b92017-12-18 10:30:17 -02001191 while ($members =~ m/(struct|union)([^\{\};]+)\{([^\{\}]*)\}([^\{\}\;]*)\;/) {
1192 my $newmember;
1193 my $maintype = $1;
1194 my $ids = $4;
1195 my $content = $3;
1196 foreach my $id(split /,/, $ids) {
1197 $newmember .= "$maintype $id; ";
1198
Mauro Carvalho Chehab8ad72162017-12-18 10:30:13 -02001199 $id =~ s/[:\[].*//;
Mauro Carvalho Chehab84ce5b92017-12-18 10:30:17 -02001200 $id =~ s/^\s*\**(\S+)\s*/$1/;
Mauro Carvalho Chehab8ad72162017-12-18 10:30:13 -02001201 foreach my $arg (split /;/, $content) {
1202 next if ($arg =~ m/^\s*$/);
Mauro Carvalho Chehab7c0d7e82017-12-18 10:30:16 -02001203 if ($arg =~ m/^([^\(]+\(\*?\s*)([\w\.]*)(\s*\).*)/) {
1204 # pointer-to-function
1205 my $type = $1;
1206 my $name = $2;
1207 my $extra = $3;
1208 next if (!$name);
1209 if ($id =~ m/^\s*$/) {
1210 # anonymous struct/union
Mauro Carvalho Chehab84ce5b92017-12-18 10:30:17 -02001211 $newmember .= "$type$name$extra; ";
Mauro Carvalho Chehab7c0d7e82017-12-18 10:30:16 -02001212 } else {
Mauro Carvalho Chehab84ce5b92017-12-18 10:30:17 -02001213 $newmember .= "$type$id.$name$extra; ";
Mauro Carvalho Chehab7c0d7e82017-12-18 10:30:16 -02001214 }
Mauro Carvalho Chehab8ad72162017-12-18 10:30:13 -02001215 } else {
Mauro Carvalho Chehab84ce5b92017-12-18 10:30:17 -02001216 my $type;
1217 my $names;
1218 $arg =~ s/^\s+//;
1219 $arg =~ s/\s+$//;
1220 # Handle bitmaps
1221 $arg =~ s/:\s*\d+\s*//g;
1222 # Handle arrays
Mauro Carvalho Chehabd404d572018-03-29 10:58:59 -04001223 $arg =~ s/\[.*\]//g;
Mauro Carvalho Chehab84ce5b92017-12-18 10:30:17 -02001224 # The type may have multiple words,
1225 # and multiple IDs can be defined, like:
1226 # const struct foo, *bar, foobar
1227 # So, we remove spaces when parsing the
1228 # names, in order to match just names
1229 # and commas for the names
1230 $arg =~ s/\s*,\s*/,/g;
1231 if ($arg =~ m/(.*)\s+([\S+,]+)/) {
1232 $type = $1;
1233 $names = $2;
Mauro Carvalho Chehab7c0d7e82017-12-18 10:30:16 -02001234 } else {
Mauro Carvalho Chehab84ce5b92017-12-18 10:30:17 -02001235 $newmember .= "$arg; ";
1236 next;
1237 }
1238 foreach my $name (split /,/, $names) {
1239 $name =~ s/^\s*\**(\S+)\s*/$1/;
1240 next if (($name =~ m/^\s*$/));
1241 if ($id =~ m/^\s*$/) {
1242 # anonymous struct/union
1243 $newmember .= "$type $name; ";
1244 } else {
1245 $newmember .= "$type $id.$name; ";
1246 }
Mauro Carvalho Chehab7c0d7e82017-12-18 10:30:16 -02001247 }
Mauro Carvalho Chehab8ad72162017-12-18 10:30:13 -02001248 }
1249 }
Mauro Carvalho Chehab84ce5b92017-12-18 10:30:17 -02001250 }
Ben Hutchings673bb2d2018-08-05 17:41:09 +01001251 $members =~ s/(struct|union)([^\{\};]+)\{([^\{\}]*)\}([^\{\}\;]*)\;/$newmember/;
Mauro Carvalho Chehab84ce5b92017-12-18 10:30:17 -02001252 }
Mauro Carvalho Chehab8ad72162017-12-18 10:30:13 -02001253
1254 # Ignore other nested elements, like enums
Ben Hutchings673bb2d2018-08-05 17:41:09 +01001255 $members =~ s/(\{[^\{\}]*\})//g;
Mauro Carvalho Chehab8ad72162017-12-18 10:30:13 -02001256
Mauro Carvalho Chehab151c4682017-12-18 10:30:15 -02001257 create_parameterlist($members, ';', $file, $declaration_name);
Mauro Carvalho Chehab1081de22017-12-18 10:30:14 -02001258 check_sections($file, $declaration_name, $decl_type, $sectcheck, $struct_actual);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259
Mauro Carvalho Chehab8ad72162017-12-18 10:30:13 -02001260 # Adjust declaration for better display
Ben Hutchings673bb2d2018-08-05 17:41:09 +01001261 $declaration =~ s/([\{;])/$1\n/g;
1262 $declaration =~ s/\}\s+;/};/g;
Mauro Carvalho Chehab8ad72162017-12-18 10:30:13 -02001263 # Better handle inlined enums
Ben Hutchings673bb2d2018-08-05 17:41:09 +01001264 do {} while ($declaration =~ s/(enum\s+\{[^\}]+),([^\n])/$1,\n$2/);
Mauro Carvalho Chehab8ad72162017-12-18 10:30:13 -02001265
1266 my @def_args = split /\n/, $declaration;
1267 my $level = 1;
1268 $declaration = "";
1269 foreach my $clause (@def_args) {
1270 $clause =~ s/^\s+//;
1271 $clause =~ s/\s+$//;
1272 $clause =~ s/\s+/ /;
1273 next if (!$clause);
Ben Hutchings673bb2d2018-08-05 17:41:09 +01001274 $level-- if ($clause =~ m/(\})/ && $level > 1);
Mauro Carvalho Chehab8ad72162017-12-18 10:30:13 -02001275 if (!($clause =~ m/^\s*#/)) {
1276 $declaration .= "\t" x $level;
1277 }
1278 $declaration .= "\t" . $clause . "\n";
Ben Hutchings673bb2d2018-08-05 17:41:09 +01001279 $level++ if ($clause =~ m/(\{)/ && !($clause =~m/\}/));
Mauro Carvalho Chehab8ad72162017-12-18 10:30:13 -02001280 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281 output_declaration($declaration_name,
1282 'struct',
1283 {'struct' => $declaration_name,
1284 'module' => $modulename,
Mauro Carvalho Chehab8ad72162017-12-18 10:30:13 -02001285 'definition' => $declaration,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286 'parameterlist' => \@parameterlist,
1287 'parameterdescs' => \%parameterdescs,
1288 'parametertypes' => \%parametertypes,
1289 'sectionlist' => \@sectionlist,
1290 'sections' => \%sections,
1291 'purpose' => $declaration_purpose,
1292 'type' => $decl_type
1293 });
1294 }
1295 else {
Bart Van Assched40e1e62015-09-04 15:43:21 -07001296 print STDERR "${file}:$.: error: Cannot parse struct or union!\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297 ++$errors;
1298 }
1299}
1300
Mauro Carvalho Chehab85afe602017-12-30 22:32:55 -02001301
1302sub show_warnings($$) {
1303 my $functype = shift;
1304 my $name = shift;
1305
Mauro Carvalho Chehabeab795d2020-09-29 14:23:39 +02001306 return 0 if (defined($nosymbol_table{$name}));
1307
Mauro Carvalho Chehab85afe602017-12-30 22:32:55 -02001308 return 1 if ($output_selection == OUTPUT_ALL);
1309
1310 if ($output_selection == OUTPUT_EXPORTED) {
1311 if (defined($function_table{$name})) {
1312 return 1;
1313 } else {
1314 return 0;
1315 }
1316 }
1317 if ($output_selection == OUTPUT_INTERNAL) {
1318 if (!($functype eq "function" && defined($function_table{$name}))) {
1319 return 1;
1320 } else {
1321 return 0;
1322 }
1323 }
1324 if ($output_selection == OUTPUT_INCLUDE) {
1325 if (defined($function_table{$name})) {
1326 return 1;
1327 } else {
1328 return 0;
1329 }
1330 }
Mauro Carvalho Chehab85afe602017-12-30 22:32:55 -02001331 die("Please add the new output type at show_warnings()");
1332}
1333
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334sub dump_enum($$) {
1335 my $x = shift;
1336 my $file = shift;
Mauro Carvalho Chehabd38c8cf2020-10-01 08:23:52 +02001337 my $members;
1338
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339
Martin Waitzaeec46b2005-11-13 16:08:13 -08001340 $x =~ s@/\*.*?\*/@@gos; # strip comments.
Conchúr Navid4468e212015-11-08 10:48:05 +01001341 # strip #define macros inside enums
1342 $x =~ s@#\s*((define|ifdef)\s+|endif)[^;]*;@@gos;
Randy Dunlapb6d676d2010-08-10 18:02:50 -07001343
Mauro Carvalho Chehabd38c8cf2020-10-01 08:23:52 +02001344 if ($x =~ /typedef\s+enum\s*\{(.*)\}\s*(\w*)\s*;/) {
1345 $declaration_name = $2;
1346 $members = $1;
1347 } elsif ($x =~ /enum\s+(\w*)\s*\{(.*)\}/) {
Randy Dunlap3c308792007-05-08 00:24:39 -07001348 $declaration_name = $1;
Mauro Carvalho Chehabd38c8cf2020-10-01 08:23:52 +02001349 $members = $2;
1350 }
1351
1352 if ($declaration_name) {
Johannes Berg5cb5c312017-09-19 13:08:13 +02001353 my %_members;
1354
Markus Heiser463a0fd2017-06-16 21:27:48 +02001355 $members =~ s/\s+$//;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356
1357 foreach my $arg (split ',', $members) {
1358 $arg =~ s/^\s*(\w+).*/$1/;
1359 push @parameterlist, $arg;
1360 if (!$parameterdescs{$arg}) {
Randy Dunlap3c308792007-05-08 00:24:39 -07001361 $parameterdescs{$arg} = $undescribed;
Mauro Carvalho Chehab85afe602017-12-30 22:32:55 -02001362 if (show_warnings("enum", $declaration_name)) {
Mauro Carvalho Chehab2defb272017-12-18 10:30:18 -02001363 print STDERR "${file}:$.: warning: Enum value '$arg' not described in enum '$declaration_name'\n";
1364 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365 }
Johannes Berg5cb5c312017-09-19 13:08:13 +02001366 $_members{$arg} = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367 }
Randy Dunlap3c3b8092006-02-01 03:06:58 -08001368
Johannes Berg5cb5c312017-09-19 13:08:13 +02001369 while (my ($k, $v) = each %parameterdescs) {
1370 if (!exists($_members{$k})) {
Mauro Carvalho Chehab85afe602017-12-30 22:32:55 -02001371 if (show_warnings("enum", $declaration_name)) {
Mauro Carvalho Chehab2defb272017-12-18 10:30:18 -02001372 print STDERR "${file}:$.: warning: Excess enum value '$k' description in '$declaration_name'\n";
1373 }
Johannes Berg5cb5c312017-09-19 13:08:13 +02001374 }
1375 }
1376
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377 output_declaration($declaration_name,
1378 'enum',
1379 {'enum' => $declaration_name,
1380 'module' => $modulename,
1381 'parameterlist' => \@parameterlist,
1382 'parameterdescs' => \%parameterdescs,
1383 'sectionlist' => \@sectionlist,
1384 'sections' => \%sections,
1385 'purpose' => $declaration_purpose
1386 });
Mauro Carvalho Chehabd38c8cf2020-10-01 08:23:52 +02001387 } else {
Bart Van Assched40e1e62015-09-04 15:43:21 -07001388 print STDERR "${file}:$.: error: Cannot parse enum!\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389 ++$errors;
1390 }
1391}
1392
1393sub dump_typedef($$) {
1394 my $x = shift;
1395 my $file = shift;
1396
Martin Waitzaeec46b2005-11-13 16:08:13 -08001397 $x =~ s@/\*.*?\*/@@gos; # strip comments.
Mauro Carvalho Chehab83766452015-10-08 16:14:45 -03001398
1399 # Parse function prototypes
Mauro Carvalho Chehabd37c43c2016-08-30 20:20:57 -03001400 if ($x =~ /typedef\s+(\w+)\s*\(\*\s*(\w\S+)\s*\)\s*\((.*)\);/ ||
1401 $x =~ /typedef\s+(\w+)\s*(\w\S+)\s*\s*\((.*)\);/) {
1402
Mauro Carvalho Chehab83766452015-10-08 16:14:45 -03001403 # Function typedefs
1404 $return_type = $1;
1405 $declaration_name = $2;
1406 my $args = $3;
1407
Mauro Carvalho Chehab151c4682017-12-18 10:30:15 -02001408 create_parameterlist($args, ',', $file, $declaration_name);
Mauro Carvalho Chehab83766452015-10-08 16:14:45 -03001409
1410 output_declaration($declaration_name,
1411 'function',
1412 {'function' => $declaration_name,
Mauro Carvalho Chehab82801d02016-08-30 20:20:58 -03001413 'typedef' => 1,
Mauro Carvalho Chehab83766452015-10-08 16:14:45 -03001414 'module' => $modulename,
1415 'functiontype' => $return_type,
1416 'parameterlist' => \@parameterlist,
1417 'parameterdescs' => \%parameterdescs,
1418 'parametertypes' => \%parametertypes,
1419 'sectionlist' => \@sectionlist,
1420 'sections' => \%sections,
1421 'purpose' => $declaration_purpose
1422 });
1423 return;
1424 }
1425
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426 while (($x =~ /\(*.\)\s*;$/) || ($x =~ /\[*.\]\s*;$/)) {
Randy Dunlap3c308792007-05-08 00:24:39 -07001427 $x =~ s/\(*.\)\s*;$/;/;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428 $x =~ s/\[*.\]\s*;$/;/;
1429 }
1430
1431 if ($x =~ /typedef.*\s+(\w+)\s*;/) {
Randy Dunlap3c308792007-05-08 00:24:39 -07001432 $declaration_name = $1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433
1434 output_declaration($declaration_name,
1435 'typedef',
1436 {'typedef' => $declaration_name,
1437 'module' => $modulename,
1438 'sectionlist' => \@sectionlist,
1439 'sections' => \%sections,
1440 'purpose' => $declaration_purpose
1441 });
1442 }
1443 else {
Bart Van Assched40e1e62015-09-04 15:43:21 -07001444 print STDERR "${file}:$.: error: Cannot parse typedef!\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445 ++$errors;
1446 }
1447}
1448
Randy Dunlapa1d94aa2008-12-19 08:49:30 -08001449sub save_struct_actual($) {
1450 my $actual = shift;
1451
1452 # strip all spaces from the actual param so that it looks like one string item
1453 $actual =~ s/\s*//g;
1454 $struct_actual = $struct_actual . $actual . " ";
1455}
1456
Mauro Carvalho Chehab151c4682017-12-18 10:30:15 -02001457sub create_parameterlist($$$$) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458 my $args = shift;
1459 my $splitter = shift;
1460 my $file = shift;
Mauro Carvalho Chehab151c4682017-12-18 10:30:15 -02001461 my $declaration_name = shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462 my $type;
1463 my $param;
1464
Martin Waitza6d3fe72006-01-09 20:53:55 -08001465 # temporarily replace commas inside function pointer definition
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466 while ($args =~ /(\([^\),]+),/) {
Randy Dunlap3c308792007-05-08 00:24:39 -07001467 $args =~ s/(\([^\),]+),/$1#/g;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468 }
Randy Dunlap3c3b8092006-02-01 03:06:58 -08001469
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470 foreach my $arg (split($splitter, $args)) {
1471 # strip comments
1472 $arg =~ s/\/\*.*\*\///;
Randy Dunlap3c308792007-05-08 00:24:39 -07001473 # strip leading/trailing spaces
1474 $arg =~ s/^\s*//;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475 $arg =~ s/\s*$//;
1476 $arg =~ s/\s+/ /;
1477
1478 if ($arg =~ /^#/) {
1479 # Treat preprocessor directive as a typeless variable just to fill
1480 # corresponding data structures "correctly". Catch it later in
1481 # output_* subs.
1482 push_parameter($arg, "", $file);
Richard Kennedy00d62962008-02-23 15:24:01 -08001483 } elsif ($arg =~ m/\(.+\)\s*\(/) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484 # pointer-to-function
1485 $arg =~ tr/#/,/;
Mauro Carvalho Chehab7c0d7e82017-12-18 10:30:16 -02001486 $arg =~ m/[^\(]+\(\*?\s*([\w\.]*)\s*\)/;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487 $param = $1;
1488 $type = $arg;
Richard Kennedy00d62962008-02-23 15:24:01 -08001489 $type =~ s/([^\(]+\(\*?)\s*$param/$1/;
Randy Dunlapa1d94aa2008-12-19 08:49:30 -08001490 save_struct_actual($param);
Mauro Carvalho Chehab151c4682017-12-18 10:30:15 -02001491 push_parameter($param, $type, $file, $declaration_name);
Martin Waitzaeec46b2005-11-13 16:08:13 -08001492 } elsif ($arg) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493 $arg =~ s/\s*:\s*/:/g;
1494 $arg =~ s/\s*\[/\[/g;
1495
1496 my @args = split('\s*,\s*', $arg);
1497 if ($args[0] =~ m/\*/) {
1498 $args[0] =~ s/(\*+)\s*/ $1/;
1499 }
Borislav Petkov884f2812007-05-08 00:29:05 -07001500
1501 my @first_arg;
1502 if ($args[0] =~ /^(.*\s+)(.*?\[.*\].*)$/) {
1503 shift @args;
1504 push(@first_arg, split('\s+', $1));
1505 push(@first_arg, $2);
1506 } else {
1507 @first_arg = split('\s+', shift @args);
1508 }
1509
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510 unshift(@args, pop @first_arg);
1511 $type = join " ", @first_arg;
1512
1513 foreach $param (@args) {
1514 if ($param =~ m/^(\*+)\s*(.*)/) {
Randy Dunlapa1d94aa2008-12-19 08:49:30 -08001515 save_struct_actual($2);
Mauro Carvalho Chehab151c4682017-12-18 10:30:15 -02001516 push_parameter($2, "$type $1", $file, $declaration_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517 }
1518 elsif ($param =~ m/(.*?):(\d+)/) {
Randy Dunlap7b978872008-05-16 15:45:52 -07001519 if ($type ne "") { # skip unnamed bit-fields
Randy Dunlapa1d94aa2008-12-19 08:49:30 -08001520 save_struct_actual($1);
Mauro Carvalho Chehab151c4682017-12-18 10:30:15 -02001521 push_parameter($1, "$type:$2", $file, $declaration_name)
Randy Dunlap7b978872008-05-16 15:45:52 -07001522 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523 }
1524 else {
Randy Dunlapa1d94aa2008-12-19 08:49:30 -08001525 save_struct_actual($param);
Mauro Carvalho Chehab151c4682017-12-18 10:30:15 -02001526 push_parameter($param, $type, $file, $declaration_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527 }
1528 }
1529 }
1530 }
1531}
1532
Mauro Carvalho Chehab151c4682017-12-18 10:30:15 -02001533sub push_parameter($$$$) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534 my $param = shift;
1535 my $type = shift;
1536 my $file = shift;
Mauro Carvalho Chehab151c4682017-12-18 10:30:15 -02001537 my $declaration_name = shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538
Randy Dunlap5f8c7c92007-07-19 01:48:24 -07001539 if (($anon_struct_union == 1) && ($type eq "") &&
1540 ($param eq "}")) {
1541 return; # ignore the ending }; from anon. struct/union
1542 }
1543
1544 $anon_struct_union = 0;
Mauro Carvalho Chehabf9b5c532017-03-30 17:11:29 -03001545 $param =~ s/[\[\)].*//;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546
Martin Waitza6d3fe72006-01-09 20:53:55 -08001547 if ($type eq "" && $param =~ /\.\.\.$/)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548 {
Silvio Frickec950a172016-10-28 10:14:08 +02001549 if (!$param =~ /\w\.\.\.$/) {
1550 # handles unnamed variable parameters
1551 $param = "...";
1552 }
Jonathan Neuschäfer43756e32019-11-07 14:41:33 +01001553 elsif ($param =~ /\w\.\.\.$/) {
1554 # for named variable parameters of the form `x...`, remove the dots
1555 $param =~ s/\.\.\.$//;
1556 }
Randy Dunlapced69092008-12-01 13:14:03 -08001557 if (!defined $parameterdescs{$param} || $parameterdescs{$param} eq "") {
1558 $parameterdescs{$param} = "variable arguments";
1559 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560 }
1561 elsif ($type eq "" && ($param eq "" or $param eq "void"))
1562 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563 $param="void";
1564 $parameterdescs{void} = "no arguments";
1565 }
Randy Dunlap134fe012006-12-22 01:10:50 -08001566 elsif ($type eq "" && ($param eq "struct" or $param eq "union"))
1567 # handle unnamed (anonymous) union or struct:
1568 {
1569 $type = $param;
Randy Dunlap5f8c7c92007-07-19 01:48:24 -07001570 $param = "{unnamed_" . $param . "}";
Randy Dunlap134fe012006-12-22 01:10:50 -08001571 $parameterdescs{$param} = "anonymous\n";
Randy Dunlap5f8c7c92007-07-19 01:48:24 -07001572 $anon_struct_union = 1;
Randy Dunlap134fe012006-12-22 01:10:50 -08001573 }
1574
Martin Waitza6d3fe72006-01-09 20:53:55 -08001575 # warn if parameter has no description
Randy Dunlap134fe012006-12-22 01:10:50 -08001576 # (but ignore ones starting with # as these are not parameters
1577 # but inline preprocessor statements);
Mauro Carvalho Chehab151c4682017-12-18 10:30:15 -02001578 # Note: It will also ignore void params and unnamed structs/unions
Mauro Carvalho Chehabf9b5c532017-03-30 17:11:29 -03001579 if (!defined $parameterdescs{$param} && $param !~ /^#/) {
Mauro Carvalho Chehab151c4682017-12-18 10:30:15 -02001580 $parameterdescs{$param} = $undescribed;
Martin Waitza6d3fe72006-01-09 20:53:55 -08001581
Jonathan Corbetbe5cd20c2019-01-11 12:31:39 -07001582 if (show_warnings($type, $declaration_name) && $param !~ /\./) {
Mauro Carvalho Chehab2defb272017-12-18 10:30:18 -02001583 print STDERR
1584 "${file}:$.: warning: Function parameter or member '$param' not described in '$declaration_name'\n";
1585 ++$warnings;
1586 }
Randy Dunlap3c308792007-05-08 00:24:39 -07001587 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001589 # strip spaces from $param so that it is one continuous string
Randy Dunlape34e7db2009-06-17 17:37:47 -07001590 # on @parameterlist;
1591 # this fixes a problem where check_sections() cannot find
1592 # a parameter like "addr[6 + 2]" because it actually appears
1593 # as "addr[6", "+", "2]" on the parameter list;
1594 # but it's better to maintain the param string unchanged for output,
1595 # so just weaken the string compare in check_sections() to ignore
1596 # "[blah" in a parameter string;
1597 ###$param =~ s/\s*//g;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598 push @parameterlist, $param;
Paolo Bonzini02a4f4f2017-01-02 16:22:23 +01001599 $type =~ s/\s\s+/ /g;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600 $parametertypes{$param} = $type;
1601}
1602
Mauro Carvalho Chehab1081de22017-12-18 10:30:14 -02001603sub check_sections($$$$$) {
1604 my ($file, $decl_name, $decl_type, $sectcheck, $prmscheck) = @_;
Randy Dunlapa1d94aa2008-12-19 08:49:30 -08001605 my @sects = split ' ', $sectcheck;
1606 my @prms = split ' ', $prmscheck;
1607 my $err;
1608 my ($px, $sx);
1609 my $prm_clean; # strip trailing "[array size]" and/or beginning "*"
1610
1611 foreach $sx (0 .. $#sects) {
1612 $err = 1;
1613 foreach $px (0 .. $#prms) {
1614 $prm_clean = $prms[$px];
1615 $prm_clean =~ s/\[.*\]//;
Johannes Berg1f3a6682010-09-11 15:55:12 -07001616 $prm_clean =~ s/__attribute__\s*\(\([a-z,_\*\s\(\)]*\)\)//i;
Randy Dunlape34e7db2009-06-17 17:37:47 -07001617 # ignore array size in a parameter string;
1618 # however, the original param string may contain
1619 # spaces, e.g.: addr[6 + 2]
1620 # and this appears in @prms as "addr[6" since the
1621 # parameter list is split at spaces;
1622 # hence just ignore "[..." for the sections check;
1623 $prm_clean =~ s/\[.*//;
1624
Randy Dunlapa1d94aa2008-12-19 08:49:30 -08001625 ##$prm_clean =~ s/^\**//;
1626 if ($prm_clean eq $sects[$sx]) {
1627 $err = 0;
1628 last;
1629 }
1630 }
1631 if ($err) {
1632 if ($decl_type eq "function") {
Bart Van Assched40e1e62015-09-04 15:43:21 -07001633 print STDERR "${file}:$.: warning: " .
Randy Dunlapa1d94aa2008-12-19 08:49:30 -08001634 "Excess function parameter " .
1635 "'$sects[$sx]' " .
1636 "description in '$decl_name'\n";
1637 ++$warnings;
Randy Dunlapa1d94aa2008-12-19 08:49:30 -08001638 }
1639 }
1640 }
1641}
1642
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643##
Yacine Belkadi4092bac2012-11-26 22:22:27 +01001644# Checks the section describing the return value of a function.
1645sub check_return_section {
1646 my $file = shift;
1647 my $declaration_name = shift;
1648 my $return_type = shift;
1649
1650 # Ignore an empty return type (It's a macro)
1651 # Ignore functions with a "void" return type. (But don't ignore "void *")
1652 if (($return_type eq "") || ($return_type =~ /void\s*\w*\s*$/)) {
1653 return;
1654 }
1655
1656 if (!defined($sections{$section_return}) ||
1657 $sections{$section_return} eq "") {
Bart Van Assched40e1e62015-09-04 15:43:21 -07001658 print STDERR "${file}:$.: warning: " .
Yacine Belkadi4092bac2012-11-26 22:22:27 +01001659 "No description found for return value of " .
1660 "'$declaration_name'\n";
1661 ++$warnings;
1662 }
1663}
1664
1665##
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666# takes a function prototype and the name of the current file being
1667# processed and spits out all the details stored in the global
1668# arrays/hashes.
1669sub dump_function($$) {
1670 my $prototype = shift;
1671 my $file = shift;
Horia Geantacbb4d3e2014-07-12 09:55:03 -07001672 my $noret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673
Mauro Carvalho Chehab5eb6b4b2020-09-09 16:10:33 +02001674 print_lineno($.);
1675
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676 $prototype =~ s/^static +//;
1677 $prototype =~ s/^extern +//;
Pavel Pisa4dc3b162005-05-01 08:59:25 -07001678 $prototype =~ s/^asmlinkage +//;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679 $prototype =~ s/^inline +//;
1680 $prototype =~ s/^__inline__ +//;
Randy Dunlap32e79402006-10-11 01:22:10 -07001681 $prototype =~ s/^__inline +//;
1682 $prototype =~ s/^__always_inline +//;
1683 $prototype =~ s/^noinline +//;
Randy Dunlap74fc5c62008-06-19 16:03:29 -07001684 $prototype =~ s/__init +//;
Randy Dunlap20072202010-03-23 13:35:24 -07001685 $prototype =~ s/__init_or_module +//;
Randy Dunlap270a0092014-08-24 18:17:17 -07001686 $prototype =~ s/__meminit +//;
Randy Dunlap70c95b02012-01-21 10:31:54 -08001687 $prototype =~ s/__must_check +//;
Randy Dunlap0df7c0e2012-08-16 16:23:20 -07001688 $prototype =~ s/__weak +//;
Matthew Wilcox0891f952018-03-15 05:06:23 -07001689 $prototype =~ s/__sched +//;
Randy Dunlap95e760c2019-08-05 09:29:50 -07001690 $prototype =~ s/__printf\s*\(\s*\d*\s*,\s*\d*\s*\) +//;
Horia Geantacbb4d3e2014-07-12 09:55:03 -07001691 my $define = $prototype =~ s/^#\s*define\s+//; #ak added
Paolo Bonzinib1aaa542017-01-02 16:22:24 +01001692 $prototype =~ s/__attribute__\s*\(\(
1693 (?:
1694 [\w\s]++ # attribute name
1695 (?:\([^)]*+\))? # attribute arguments
1696 \s*+,? # optional comma at the end
1697 )+
1698 \)\)\s+//x;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699
1700 # Yes, this truly is vile. We are looking for:
1701 # 1. Return type (may be nothing if we're looking at a macro)
1702 # 2. Function name
1703 # 3. Function parameters.
1704 #
1705 # All the while we have to watch out for function pointer parameters
1706 # (which IIRC is what the two sections are for), C types (these
1707 # regexps don't even start to express all the possibilities), and
1708 # so on.
1709 #
1710 # If you mess with these regexps, it's a good idea to check that
1711 # the following functions' documentation still comes out right:
1712 # - parport_register_device (function pointer parameters)
1713 # - atomic_set (macro)
Martin Waitz9598f912006-02-01 03:06:55 -08001714 # - pci_match_device, __copy_to_user (long return type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715
Horia Geantacbb4d3e2014-07-12 09:55:03 -07001716 if ($define && $prototype =~ m/^()([a-zA-Z0-9_~:]+)\s+/) {
1717 # This is an object-like macro, it has no return type and no parameter
1718 # list.
1719 # Function-like macros are not allowed to have spaces between
1720 # declaration_name and opening parenthesis (notice the \s+).
1721 $return_type = $1;
1722 $declaration_name = $2;
1723 $noret = 1;
1724 } elsif ($prototype =~ m/^()([a-zA-Z0-9_~:]+)\s*\(([^\(]*)\)/ ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725 $prototype =~ m/^(\w+)\s+([a-zA-Z0-9_~:]+)\s*\(([^\(]*)\)/ ||
Matthew Wilcox5a0bc572017-01-23 00:18:10 -08001726 $prototype =~ m/^(\w+\s*\*+)\s*([a-zA-Z0-9_~:]+)\s*\(([^\(]*)\)/ ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727 $prototype =~ m/^(\w+\s+\w+)\s+([a-zA-Z0-9_~:]+)\s*\(([^\(]*)\)/ ||
Randy Dunlap94b3e032008-02-07 00:13:26 -08001728 $prototype =~ m/^(\w+\s+\w+\s*\*+)\s*([a-zA-Z0-9_~:]+)\s*\(([^\(]*)\)/ ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729 $prototype =~ m/^(\w+\s+\w+\s+\w+)\s+([a-zA-Z0-9_~:]+)\s*\(([^\(]*)\)/ ||
Matthew Wilcox5a0bc572017-01-23 00:18:10 -08001730 $prototype =~ m/^(\w+\s+\w+\s+\w+\s*\*+)\s*([a-zA-Z0-9_~:]+)\s*\(([^\(]*)\)/ ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731 $prototype =~ m/^()([a-zA-Z0-9_~:]+)\s*\(([^\{]*)\)/ ||
1732 $prototype =~ m/^(\w+)\s+([a-zA-Z0-9_~:]+)\s*\(([^\{]*)\)/ ||
Matthew Wilcox5a0bc572017-01-23 00:18:10 -08001733 $prototype =~ m/^(\w+\s*\*+)\s*([a-zA-Z0-9_~:]+)\s*\(([^\{]*)\)/ ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734 $prototype =~ m/^(\w+\s+\w+)\s+([a-zA-Z0-9_~:]+)\s*\(([^\{]*)\)/ ||
Matthew Wilcox5a0bc572017-01-23 00:18:10 -08001735 $prototype =~ m/^(\w+\s+\w+\s*\*+)\s*([a-zA-Z0-9_~:]+)\s*\(([^\{]*)\)/ ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736 $prototype =~ m/^(\w+\s+\w+\s+\w+)\s+([a-zA-Z0-9_~:]+)\s*\(([^\{]*)\)/ ||
Matthew Wilcox5a0bc572017-01-23 00:18:10 -08001737 $prototype =~ m/^(\w+\s+\w+\s+\w+\s*\*+)\s*([a-zA-Z0-9_~:]+)\s*\(([^\{]*)\)/ ||
Martin Waitz9598f912006-02-01 03:06:55 -08001738 $prototype =~ m/^(\w+\s+\w+\s+\w+\s+\w+)\s+([a-zA-Z0-9_~:]+)\s*\(([^\{]*)\)/ ||
Matthew Wilcox5a0bc572017-01-23 00:18:10 -08001739 $prototype =~ m/^(\w+\s+\w+\s+\w+\s+\w+\s*\*+)\s*([a-zA-Z0-9_~:]+)\s*\(([^\{]*)\)/ ||
1740 $prototype =~ m/^(\w+\s+\w+\s*\*+\s*\w+\s*\*+\s*)\s*([a-zA-Z0-9_~:]+)\s*\(([^\{]*)\)/) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741 $return_type = $1;
1742 $declaration_name = $2;
1743 my $args = $3;
1744
Mauro Carvalho Chehab151c4682017-12-18 10:30:15 -02001745 create_parameterlist($args, ',', $file, $declaration_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746 } else {
Bart Van Assched40e1e62015-09-04 15:43:21 -07001747 print STDERR "${file}:$.: warning: cannot understand function prototype: '$prototype'\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748 return;
1749 }
1750
Mauro Carvalho Chehab47bcacf2020-09-30 09:47:01 +02001751 my $prms = join " ", @parameterlist;
1752 check_sections($file, $declaration_name, "function", $sectcheck, $prms);
Randy Dunlapa1d94aa2008-12-19 08:49:30 -08001753
Mauro Carvalho Chehab47bcacf2020-09-30 09:47:01 +02001754 # This check emits a lot of warnings at the moment, because many
1755 # functions don't have a 'Return' doc section. So until the number
1756 # of warnings goes sufficiently down, the check is only performed in
1757 # verbose mode.
1758 # TODO: always perform the check.
1759 if ($verbose && !$noret) {
1760 check_return_section($file, $declaration_name, $return_type);
1761 }
Yacine Belkadi4092bac2012-11-26 22:22:27 +01001762
Mauro Carvalho Chehab47bcacf2020-09-30 09:47:01 +02001763 # The function parser can be called with a typedef parameter.
1764 # Handle it.
1765 if ($return_type =~ /typedef/) {
1766 output_declaration($declaration_name,
1767 'function',
1768 {'function' => $declaration_name,
1769 'typedef' => 1,
1770 'module' => $modulename,
1771 'functiontype' => $return_type,
1772 'parameterlist' => \@parameterlist,
1773 'parameterdescs' => \%parameterdescs,
1774 'parametertypes' => \%parametertypes,
1775 'sectionlist' => \@sectionlist,
1776 'sections' => \%sections,
1777 'purpose' => $declaration_purpose
1778 });
1779 } else {
1780 output_declaration($declaration_name,
1781 'function',
1782 {'function' => $declaration_name,
1783 'module' => $modulename,
1784 'functiontype' => $return_type,
1785 'parameterlist' => \@parameterlist,
1786 'parameterdescs' => \%parameterdescs,
1787 'parametertypes' => \%parametertypes,
1788 'sectionlist' => \@sectionlist,
1789 'sections' => \%sections,
1790 'purpose' => $declaration_purpose
1791 });
1792 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793}
1794
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795sub reset_state {
1796 $function = "";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797 %parameterdescs = ();
1798 %parametertypes = ();
1799 @parameterlist = ();
1800 %sections = ();
1801 @sectionlist = ();
Randy Dunlapa1d94aa2008-12-19 08:49:30 -08001802 $sectcheck = "";
1803 $struct_actual = "";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804 $prototype = "";
Randy Dunlap3c3b8092006-02-01 03:06:58 -08001805
Jani Nikula48af6062016-05-26 14:56:05 +03001806 $state = STATE_NORMAL;
1807 $inline_doc_state = STATE_INLINE_NA;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808}
1809
Jason Baron56afb0f2009-04-30 13:29:36 -04001810sub tracepoint_munge($) {
1811 my $file = shift;
1812 my $tracepointname = 0;
1813 my $tracepointargs = 0;
1814
Jason Baron3a9089f2009-12-01 12:18:49 -05001815 if ($prototype =~ m/TRACE_EVENT\((.*?),/) {
Jason Baron56afb0f2009-04-30 13:29:36 -04001816 $tracepointname = $1;
1817 }
Jason Baron3a9089f2009-12-01 12:18:49 -05001818 if ($prototype =~ m/DEFINE_SINGLE_EVENT\((.*?),/) {
1819 $tracepointname = $1;
1820 }
1821 if ($prototype =~ m/DEFINE_EVENT\((.*?),(.*?),/) {
1822 $tracepointname = $2;
1823 }
1824 $tracepointname =~ s/^\s+//; #strip leading whitespace
1825 if ($prototype =~ m/TP_PROTO\((.*?)\)/) {
Jason Baron56afb0f2009-04-30 13:29:36 -04001826 $tracepointargs = $1;
1827 }
1828 if (($tracepointname eq 0) || ($tracepointargs eq 0)) {
Bart Van Assched40e1e62015-09-04 15:43:21 -07001829 print STDERR "${file}:$.: warning: Unrecognized tracepoint format: \n".
Jason Baron56afb0f2009-04-30 13:29:36 -04001830 "$prototype\n";
1831 } else {
1832 $prototype = "static inline void trace_$tracepointname($tracepointargs)";
1833 }
1834}
1835
Randy Dunlapb4870bc2009-02-11 13:04:33 -08001836sub syscall_munge() {
1837 my $void = 0;
1838
Mauro Carvalho Chehab7c9aa012017-12-18 10:30:12 -02001839 $prototype =~ s@[\r\n]+@ @gos; # strip newlines/CR's
Randy Dunlapb4870bc2009-02-11 13:04:33 -08001840## if ($prototype =~ m/SYSCALL_DEFINE0\s*\(\s*(a-zA-Z0-9_)*\s*\)/) {
1841 if ($prototype =~ m/SYSCALL_DEFINE0/) {
1842 $void = 1;
1843## $prototype = "long sys_$1(void)";
1844 }
1845
1846 $prototype =~ s/SYSCALL_DEFINE.*\(/long sys_/; # fix return type & func name
1847 if ($prototype =~ m/long (sys_.*?),/) {
1848 $prototype =~ s/,/\(/;
1849 } elsif ($void) {
1850 $prototype =~ s/\)/\(void\)/;
1851 }
1852
1853 # now delete all of the odd-number commas in $prototype
1854 # so that arg types & arg names don't have a comma between them
1855 my $count = 0;
1856 my $len = length($prototype);
1857 if ($void) {
1858 $len = 0; # skip the for-loop
1859 }
1860 for (my $ix = 0; $ix < $len; $ix++) {
1861 if (substr($prototype, $ix, 1) eq ',') {
1862 $count++;
1863 if ($count % 2 == 1) {
1864 substr($prototype, $ix, 1) = ' ';
1865 }
1866 }
1867 }
1868}
1869
Daniel Vetterb7afa922016-06-01 23:46:24 +02001870sub process_proto_function($$) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871 my $x = shift;
1872 my $file = shift;
1873
Randy Dunlap51f5a0c2007-07-19 01:48:24 -07001874 $x =~ s@\/\/.*$@@gos; # strip C99-style comments to end of line
1875
Randy Dunlap890c78c2008-10-25 17:06:43 -07001876 if ($x =~ m#\s*/\*\s+MACDOC\s*#io || ($x =~ /^#/ && $x !~ /^#\s*define/)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877 # do nothing
1878 }
1879 elsif ($x =~ /([^\{]*)/) {
Randy Dunlap3c308792007-05-08 00:24:39 -07001880 $prototype .= $1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881 }
Randy Dunlapb4870bc2009-02-11 13:04:33 -08001882
Randy Dunlap890c78c2008-10-25 17:06:43 -07001883 if (($x =~ /\{/) || ($x =~ /\#\s*define/) || ($x =~ /;/)) {
Randy Dunlap3c308792007-05-08 00:24:39 -07001884 $prototype =~ s@/\*.*?\*/@@gos; # strip comments.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001885 $prototype =~ s@[\r\n]+@ @gos; # strip newlines/cr's.
1886 $prototype =~ s@^\s+@@gos; # strip leading spaces
Mauro Carvalho Chehab7ae281b2020-06-23 09:09:02 +02001887
1888 # Handle prototypes for function pointers like:
1889 # int (*pcs_config)(struct foo)
1890 $prototype =~ s@^(\S+\s+)\(\s*\*(\S+)\)@$1$2@gos;
1891
Randy Dunlapb4870bc2009-02-11 13:04:33 -08001892 if ($prototype =~ /SYSCALL_DEFINE/) {
1893 syscall_munge();
1894 }
Jason Baron3a9089f2009-12-01 12:18:49 -05001895 if ($prototype =~ /TRACE_EVENT/ || $prototype =~ /DEFINE_EVENT/ ||
1896 $prototype =~ /DEFINE_SINGLE_EVENT/)
1897 {
Jason Baron56afb0f2009-04-30 13:29:36 -04001898 tracepoint_munge($file);
1899 }
Randy Dunlapb4870bc2009-02-11 13:04:33 -08001900 dump_function($prototype, $file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901 reset_state();
1902 }
1903}
1904
Daniel Vetterb7afa922016-06-01 23:46:24 +02001905sub process_proto_type($$) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906 my $x = shift;
1907 my $file = shift;
1908
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909 $x =~ s@[\r\n]+@ @gos; # strip newlines/cr's.
1910 $x =~ s@^\s+@@gos; # strip leading spaces
1911 $x =~ s@\s+$@@gos; # strip trailing spaces
Randy Dunlap51f5a0c2007-07-19 01:48:24 -07001912 $x =~ s@\/\/.*$@@gos; # strip C99-style comments to end of line
1913
Linus Torvalds1da177e2005-04-16 15:20:36 -07001914 if ($x =~ /^#/) {
1915 # To distinguish preprocessor directive from regular declaration later.
1916 $x .= ";";
1917 }
1918
1919 while (1) {
Ben Hutchings673bb2d2018-08-05 17:41:09 +01001920 if ( $x =~ /([^\{\};]*)([\{\};])(.*)/ ) {
Markus Heiser463a0fd2017-06-16 21:27:48 +02001921 if( length $prototype ) {
1922 $prototype .= " "
1923 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924 $prototype .= $1 . $2;
1925 ($2 eq '{') && $brcount++;
1926 ($2 eq '}') && $brcount--;
1927 if (($2 eq ';') && ($brcount == 0)) {
Randy Dunlapb9d973282009-06-09 08:50:38 -07001928 dump_declaration($prototype, $file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929 reset_state();
Randy Dunlap3c308792007-05-08 00:24:39 -07001930 last;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931 }
1932 $x = $3;
Randy Dunlap3c308792007-05-08 00:24:39 -07001933 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934 $prototype .= $x;
1935 last;
1936 }
1937 }
1938}
1939
Randy Dunlap6b5b55f2007-10-16 23:31:20 -07001940
Jani Nikula1ad560e2016-06-07 10:53:39 +03001941sub map_filename($) {
1942 my $file;
1943 my ($orig_file) = @_;
1944
1945 if (defined($ENV{'SRCTREE'})) {
1946 $file = "$ENV{'SRCTREE'}" . "/" . $orig_file;
1947 } else {
1948 $file = $orig_file;
1949 }
1950
1951 if (defined($source_map{$file})) {
1952 $file = $source_map{$file};
1953 }
1954
1955 return $file;
1956}
1957
Jani Nikula88c2b572016-06-07 11:00:52 +03001958sub process_export_file($) {
1959 my ($orig_file) = @_;
1960 my $file = map_filename($orig_file);
1961
1962 if (!open(IN,"<$file")) {
1963 print STDERR "Error: Cannot open file $file\n";
1964 ++$errors;
1965 return;
1966 }
1967
1968 while (<IN>) {
1969 if (/$export_symbol/) {
Mauro Carvalho Chehabeab795d2020-09-29 14:23:39 +02001970 next if (defined($nosymbol_table{$2}));
Jani Nikula88c2b572016-06-07 11:00:52 +03001971 $function_table{$2} = 1;
1972 }
1973 }
1974
1975 close(IN);
1976}
1977
Jonathan Corbet07048d12018-02-05 14:15:19 -07001978#
1979# Parsers for the various processing states.
1980#
1981# STATE_NORMAL: looking for the /** to begin everything.
1982#
1983sub process_normal() {
1984 if (/$doc_start/o) {
1985 $state = STATE_NAME; # next line is always the function name
1986 $in_doc_sect = 0;
1987 $declaration_start_line = $. + 1;
1988 }
1989}
1990
Jonathan Corbet3cac2bc2018-02-05 14:36:33 -07001991#
1992# STATE_NAME: Looking for the "name - description" line
1993#
1994sub process_name($$) {
1995 my $file = shift;
1996 my $identifier;
1997 my $descr;
Jonathan Corbet07048d12018-02-05 14:15:19 -07001998
Jonathan Corbet3cac2bc2018-02-05 14:36:33 -07001999 if (/$doc_block/o) {
2000 $state = STATE_DOCBLOCK;
2001 $contents = "";
2002 $new_start_line = $. + 1;
2003
2004 if ( $1 eq "" ) {
2005 $section = $section_intro;
2006 } else {
2007 $section = $1;
2008 }
2009 }
2010 elsif (/$doc_decl/o) {
2011 $identifier = $1;
Jonathan Corbetfcdf1df2018-02-20 12:29:50 -07002012 if (/\s*([\w\s]+?)(\(\))?\s*-/) {
Jonathan Corbet3cac2bc2018-02-05 14:36:33 -07002013 $identifier = $1;
2014 }
2015
2016 $state = STATE_BODY;
2017 # if there's no @param blocks need to set up default section
2018 # here
2019 $contents = "";
2020 $section = $section_default;
2021 $new_start_line = $. + 1;
2022 if (/-(.*)/) {
2023 # strip leading/trailing/multiple spaces
2024 $descr= $1;
2025 $descr =~ s/^\s*//;
2026 $descr =~ s/\s*$//;
2027 $descr =~ s/\s+/ /g;
2028 $declaration_purpose = $descr;
2029 $state = STATE_BODY_MAYBE;
2030 } else {
2031 $declaration_purpose = "";
2032 }
2033
2034 if (($declaration_purpose eq "") && $verbose) {
2035 print STDERR "${file}:$.: warning: missing initial short description on line:\n";
2036 print STDERR $_;
2037 ++$warnings;
2038 }
2039
Randy Dunlapcf419d52018-10-17 21:07:27 -07002040 if ($identifier =~ m/^struct\b/) {
Jonathan Corbet3cac2bc2018-02-05 14:36:33 -07002041 $decl_type = 'struct';
Randy Dunlapcf419d52018-10-17 21:07:27 -07002042 } elsif ($identifier =~ m/^union\b/) {
Jonathan Corbet3cac2bc2018-02-05 14:36:33 -07002043 $decl_type = 'union';
Randy Dunlapcf419d52018-10-17 21:07:27 -07002044 } elsif ($identifier =~ m/^enum\b/) {
Jonathan Corbet3cac2bc2018-02-05 14:36:33 -07002045 $decl_type = 'enum';
Randy Dunlapcf419d52018-10-17 21:07:27 -07002046 } elsif ($identifier =~ m/^typedef\b/) {
Jonathan Corbet3cac2bc2018-02-05 14:36:33 -07002047 $decl_type = 'typedef';
2048 } else {
2049 $decl_type = 'function';
2050 }
2051
2052 if ($verbose) {
2053 print STDERR "${file}:$.: info: Scanning doc for $identifier\n";
2054 }
2055 } else {
2056 print STDERR "${file}:$.: warning: Cannot understand $_ on line $.",
2057 " - I thought it was a doc line\n";
2058 ++$warnings;
2059 $state = STATE_NORMAL;
2060 }
2061}
Jonathan Corbet07048d12018-02-05 14:15:19 -07002062
Jonathan Corbetd742f242018-02-05 15:36:05 -07002063
2064#
2065# STATE_BODY and STATE_BODY_MAYBE: the bulk of a kerneldoc comment.
2066#
2067sub process_body($$) {
2068 my $file = shift;
2069
Jonathan Neuschäfer43756e32019-11-07 14:41:33 +01002070 # Until all named variable macro parameters are
2071 # documented using the bare name (`x`) rather than with
2072 # dots (`x...`), strip the dots:
2073 if ($section =~ /\w\.\.\.$/) {
2074 $section =~ s/\.\.\.$//;
2075
2076 if ($verbose) {
2077 print STDERR "${file}:$.: warning: Variable macro arguments should be documented without dots\n";
2078 ++$warnings;
2079 }
2080 }
2081
Mauro Carvalho Chehab0d55d482020-04-14 18:48:29 +02002082 if ($state == STATE_BODY_WITH_BLANK_LINE && /^\s*\*\s?\S/) {
2083 dump_section($file, $section, $contents);
2084 $section = $section_default;
2085 $contents = "";
2086 }
2087
Jonathan Corbetd742f242018-02-05 15:36:05 -07002088 if (/$doc_sect/i) { # case insensitive for supported section names
2089 $newsection = $1;
2090 $newcontents = $2;
2091
2092 # map the supported section names to the canonical names
2093 if ($newsection =~ m/^description$/i) {
2094 $newsection = $section_default;
2095 } elsif ($newsection =~ m/^context$/i) {
2096 $newsection = $section_context;
2097 } elsif ($newsection =~ m/^returns?$/i) {
2098 $newsection = $section_return;
2099 } elsif ($newsection =~ m/^\@return$/) {
2100 # special: @return is a section, not a param description
2101 $newsection = $section_return;
2102 }
2103
2104 if (($contents ne "") && ($contents ne "\n")) {
2105 if (!$in_doc_sect && $verbose) {
2106 print STDERR "${file}:$.: warning: contents before sections\n";
2107 ++$warnings;
2108 }
2109 dump_section($file, $section, $contents);
2110 $section = $section_default;
2111 }
2112
2113 $in_doc_sect = 1;
2114 $state = STATE_BODY;
2115 $contents = $newcontents;
2116 $new_start_line = $.;
2117 while (substr($contents, 0, 1) eq " ") {
2118 $contents = substr($contents, 1);
2119 }
2120 if ($contents ne "") {
2121 $contents .= "\n";
2122 }
2123 $section = $newsection;
2124 $leading_space = undef;
2125 } elsif (/$doc_end/) {
2126 if (($contents ne "") && ($contents ne "\n")) {
2127 dump_section($file, $section, $contents);
2128 $section = $section_default;
2129 $contents = "";
2130 }
2131 # look for doc_com + <text> + doc_end:
2132 if ($_ =~ m'\s*\*\s*[a-zA-Z_0-9:\.]+\*/') {
2133 print STDERR "${file}:$.: warning: suspicious ending line: $_";
2134 ++$warnings;
2135 }
2136
2137 $prototype = "";
2138 $state = STATE_PROTO;
2139 $brcount = 0;
2140 } elsif (/$doc_content/) {
Jonathan Corbetd742f242018-02-05 15:36:05 -07002141 if ($1 eq "") {
Mauro Carvalho Chehab0d55d482020-04-14 18:48:29 +02002142 if ($section eq $section_context) {
Jonathan Corbetd742f242018-02-05 15:36:05 -07002143 dump_section($file, $section, $contents);
2144 $section = $section_default;
2145 $contents = "";
2146 $new_start_line = $.;
Mauro Carvalho Chehab0d55d482020-04-14 18:48:29 +02002147 $state = STATE_BODY;
Jonathan Corbetd742f242018-02-05 15:36:05 -07002148 } else {
Mauro Carvalho Chehab0d55d482020-04-14 18:48:29 +02002149 if ($section ne $section_default) {
2150 $state = STATE_BODY_WITH_BLANK_LINE;
2151 } else {
2152 $state = STATE_BODY;
2153 }
Jonathan Corbetd742f242018-02-05 15:36:05 -07002154 $contents .= "\n";
2155 }
Jonathan Corbetd742f242018-02-05 15:36:05 -07002156 } elsif ($state == STATE_BODY_MAYBE) {
2157 # Continued declaration purpose
2158 chomp($declaration_purpose);
2159 $declaration_purpose .= " " . $1;
2160 $declaration_purpose =~ s/\s+/ /g;
2161 } else {
2162 my $cont = $1;
2163 if ($section =~ m/^@/ || $section eq $section_context) {
2164 if (!defined $leading_space) {
2165 if ($cont =~ m/^(\s+)/) {
2166 $leading_space = $1;
2167 } else {
2168 $leading_space = "";
2169 }
2170 }
2171 $cont =~ s/^$leading_space//;
2172 }
2173 $contents .= $cont . "\n";
2174 }
2175 } else {
2176 # i dont know - bad line? ignore.
2177 print STDERR "${file}:$.: warning: bad line: $_";
2178 ++$warnings;
2179 }
2180}
2181
2182
Jonathan Corbetcc794812018-02-05 15:46:30 -07002183#
2184# STATE_PROTO: reading a function/whatever prototype.
2185#
2186sub process_proto($$) {
2187 my $file = shift;
2188
2189 if (/$doc_inline_oneline/) {
2190 $section = $1;
2191 $contents = $2;
2192 if ($contents ne "") {
2193 $contents .= "\n";
2194 dump_section($file, $section, $contents);
2195 $section = $section_default;
2196 $contents = "";
2197 }
2198 } elsif (/$doc_inline_start/) {
2199 $state = STATE_INLINE;
2200 $inline_doc_state = STATE_INLINE_NAME;
2201 } elsif ($decl_type eq 'function') {
2202 process_proto_function($_, $file);
2203 } else {
2204 process_proto_type($_, $file);
2205 }
2206}
2207
Jonathan Corbetc17add52018-02-05 16:11:47 -07002208#
2209# STATE_DOCBLOCK: within a DOC: block.
2210#
2211sub process_docblock($$) {
2212 my $file = shift;
2213
2214 if (/$doc_end/) {
2215 dump_doc_section($file, $section, $contents);
2216 $section = $section_default;
2217 $contents = "";
2218 $function = "";
2219 %parameterdescs = ();
2220 %parametertypes = ();
2221 @parameterlist = ();
2222 %sections = ();
2223 @sectionlist = ();
2224 $prototype = "";
2225 $state = STATE_NORMAL;
2226 } elsif (/$doc_content/) {
2227 if ( $1 eq "" ) {
2228 $contents .= $blankline;
2229 } else {
2230 $contents .= $1 . "\n";
2231 }
2232 }
2233}
2234
2235#
2236# STATE_INLINE: docbook comments within a prototype.
2237#
2238sub process_inline($$) {
2239 my $file = shift;
2240
2241 # First line (state 1) needs to be a @parameter
2242 if ($inline_doc_state == STATE_INLINE_NAME && /$doc_inline_sect/o) {
2243 $section = $1;
2244 $contents = $2;
2245 $new_start_line = $.;
2246 if ($contents ne "") {
2247 while (substr($contents, 0, 1) eq " ") {
2248 $contents = substr($contents, 1);
2249 }
2250 $contents .= "\n";
2251 }
2252 $inline_doc_state = STATE_INLINE_TEXT;
2253 # Documentation block end */
2254 } elsif (/$doc_inline_end/) {
2255 if (($contents ne "") && ($contents ne "\n")) {
2256 dump_section($file, $section, $contents);
2257 $section = $section_default;
2258 $contents = "";
2259 }
2260 $state = STATE_PROTO;
2261 $inline_doc_state = STATE_INLINE_NA;
2262 # Regular text
2263 } elsif (/$doc_content/) {
2264 if ($inline_doc_state == STATE_INLINE_TEXT) {
2265 $contents .= $1 . "\n";
2266 # nuke leading blank lines
2267 if ($contents =~ /^\s*$/) {
2268 $contents = "";
2269 }
2270 } elsif ($inline_doc_state == STATE_INLINE_NAME) {
2271 $inline_doc_state = STATE_INLINE_ERROR;
2272 print STDERR "${file}:$.: warning: ";
2273 print STDERR "Incorrect use of kernel-doc format: $_";
2274 ++$warnings;
2275 }
2276 }
2277}
2278
Jonathan Corbetcc794812018-02-05 15:46:30 -07002279
Linus Torvalds1da177e2005-04-16 15:20:36 -07002280sub process_file($) {
Randy Dunlap2283a112005-07-07 15:39:26 -07002281 my $file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002282 my $initial_section_counter = $section_counter;
Ben Hutchings68f86662015-09-01 23:48:49 +01002283 my ($orig_file) = @_;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002284
Jani Nikula1ad560e2016-06-07 10:53:39 +03002285 $file = map_filename($orig_file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002286
Mauro Carvalho Chehabdbe8ba02020-09-28 12:52:34 +02002287 if (!open(IN_FILE,"<$file")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002288 print STDERR "Error: Cannot open file $file\n";
2289 ++$errors;
2290 return;
2291 }
2292
Ilya Dryomova9e73142010-02-26 13:05:47 -08002293 $. = 1;
2294
Linus Torvalds1da177e2005-04-16 15:20:36 -07002295 $section_counter = 0;
Mauro Carvalho Chehabdbe8ba02020-09-28 12:52:34 +02002296 while (<IN_FILE>) {
Daniel Santos65478422012-10-04 17:15:05 -07002297 while (s/\\\s*$//) {
Mauro Carvalho Chehabdbe8ba02020-09-28 12:52:34 +02002298 $_ .= <IN_FILE>;
Daniel Santos65478422012-10-04 17:15:05 -07002299 }
Mauro Carvalho Chehab7c9aa012017-12-18 10:30:12 -02002300 # Replace tabs by spaces
2301 while ($_ =~ s/\t+/' ' x (length($&) * 8 - length($`) % 8)/e) {};
Jonathan Corbetc17add52018-02-05 16:11:47 -07002302 # Hand this line to the appropriate state handler
Jani Nikula48af6062016-05-26 14:56:05 +03002303 if ($state == STATE_NORMAL) {
Jonathan Corbet07048d12018-02-05 14:15:19 -07002304 process_normal();
Jonathan Corbet3cac2bc2018-02-05 14:36:33 -07002305 } elsif ($state == STATE_NAME) {
2306 process_name($file, $_);
Mauro Carvalho Chehab0d55d482020-04-14 18:48:29 +02002307 } elsif ($state == STATE_BODY || $state == STATE_BODY_MAYBE ||
2308 $state == STATE_BODY_WITH_BLANK_LINE) {
Jonathan Corbetd742f242018-02-05 15:36:05 -07002309 process_body($file, $_);
Jani Nikula48af6062016-05-26 14:56:05 +03002310 } elsif ($state == STATE_INLINE) { # scanning for inline parameters
Jonathan Corbetc17add52018-02-05 16:11:47 -07002311 process_inline($file, $_);
Jonathan Corbetcc794812018-02-05 15:46:30 -07002312 } elsif ($state == STATE_PROTO) {
2313 process_proto($file, $_);
Jani Nikula48af6062016-05-26 14:56:05 +03002314 } elsif ($state == STATE_DOCBLOCK) {
Jonathan Corbetc17add52018-02-05 16:11:47 -07002315 process_docblock($file, $_);
Randy Dunlap3c308792007-05-08 00:24:39 -07002316 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002317 }
Jonathan Corbetc17add52018-02-05 16:11:47 -07002318
2319 # Make sure we got something interesting.
Jonathan Corbetb0d60bf2019-05-24 14:52:01 -06002320 if ($initial_section_counter == $section_counter && $
2321 output_mode ne "none") {
2322 if ($output_selection == OUTPUT_INCLUDE) {
2323 print STDERR "${file}:1: warning: '$_' not found\n"
2324 for keys %function_table;
Matthew Wilcox3a025e12017-11-20 10:40:40 -08002325 }
Jonathan Corbetb0d60bf2019-05-24 14:52:01 -06002326 else {
2327 print STDERR "${file}:1: warning: no structured comments found\n";
Johannes Berge946c43a2013-11-12 15:11:12 -08002328 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002329 }
Mauro Carvalho Chehabdbe8ba02020-09-28 12:52:34 +02002330 close IN_FILE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002331}
Randy Dunlap8484baa2011-01-05 16:28:43 -08002332
2333
Mauro Carvalho Chehabefa444752020-09-24 15:30:37 +02002334$sphinx_major = get_sphinx_version();
Randy Dunlap8484baa2011-01-05 16:28:43 -08002335$kernelversion = get_kernel_version();
2336
2337# generate a sequence of code that will splice in highlighting information
2338# using the s// operator.
Mauro Carvalho Chehab1ef06232015-11-17 13:29:49 -02002339for (my $k = 0; $k < @highlights; $k++) {
Danilo Cesar Lemes de Paula4d732702015-09-07 17:01:59 -03002340 my $pattern = $highlights[$k][0];
2341 my $result = $highlights[$k][1];
2342# print STDERR "scanning pattern:$pattern, highlight:($result)\n";
2343 $dohighlight .= "\$contents =~ s:$pattern:$result:gs;\n";
Randy Dunlap8484baa2011-01-05 16:28:43 -08002344}
2345
2346# Read the file that maps relative names to absolute names for
2347# separate source and object directories and for shadow trees.
2348if (open(SOURCE_MAP, "<.tmp_filelist.txt")) {
2349 my ($relname, $absname);
2350 while(<SOURCE_MAP>) {
2351 chop();
2352 ($relname, $absname) = (split())[0..1];
2353 $relname =~ s:^/+::;
2354 $source_map{$relname} = $absname;
2355 }
2356 close(SOURCE_MAP);
2357}
2358
Jani Nikula88c2b572016-06-07 11:00:52 +03002359if ($output_selection == OUTPUT_EXPORTED ||
2360 $output_selection == OUTPUT_INTERNAL) {
Jani Nikulac9b2cfb2016-06-07 11:05:53 +03002361
2362 push(@export_file_list, @ARGV);
2363
Jani Nikula88c2b572016-06-07 11:00:52 +03002364 foreach (@export_file_list) {
2365 chomp;
2366 process_export_file($_);
2367 }
2368}
2369
Randy Dunlap8484baa2011-01-05 16:28:43 -08002370foreach (@ARGV) {
2371 chomp;
2372 process_file($_);
2373}
2374if ($verbose && $errors) {
2375 print STDERR "$errors errors\n";
2376}
2377if ($verbose && $warnings) {
2378 print STDERR "$warnings warnings\n";
2379}
2380
Pierre-Louis Bossart2c12c812020-07-28 11:20:40 -05002381if ($Werror && $warnings) {
2382 print STDERR "$warnings warnings as Errors\n";
2383 exit($warnings);
2384} else {
2385 exit($output_mode eq "none" ? 0 : $errors)
2386}