xref: /linux/scripts/kernel-doc (revision 56b0f453db74207633019f83758b4c11c66b75d0)
1cb77f0d6SKamil Rytarowski#!/usr/bin/env perl
238476378SJonathan Corbet# SPDX-License-Identifier: GPL-2.0
31da177e4SLinus Torvalds
4cb77f0d6SKamil Rytarowskiuse warnings;
51da177e4SLinus Torvaldsuse strict;
61da177e4SLinus Torvalds
71da177e4SLinus Torvalds## Copyright (c) 1998 Michael Zucchi, All Rights Reserved        ##
81da177e4SLinus Torvalds## Copyright (C) 2000, 1  Tim Waugh <twaugh@redhat.com>          ##
91da177e4SLinus Torvalds## Copyright (C) 2001  Simon Huggins                             ##
1070c95b00SRandy Dunlap## Copyright (C) 2005-2012  Randy Dunlap                         ##
111b40c194SDan Luedtke## Copyright (C) 2012  Dan Luedtke                               ##
121da177e4SLinus Torvalds## 								 ##
131da177e4SLinus Torvalds## #define enhancements by Armin Kuster <akuster@mvista.com>	 ##
141da177e4SLinus Torvalds## Copyright (c) 2000 MontaVista Software, Inc.			 ##
152b306ecaSTomasz Warniełło#
162b306ecaSTomasz Warniełło# Copyright (C) 2022 Tomasz Warniełło (POD)
171da177e4SLinus Torvalds
1843caf1a6STomasz Warniełłouse Pod::Usage qw/pod2usage/;
1943caf1a6STomasz Warniełło
20a5cdaea5STomasz Warniełło=head1 NAME
21a5cdaea5STomasz Warniełło
22a5cdaea5STomasz Warniełłokernel-doc - Print formatted kernel documentation to stdout
23a5cdaea5STomasz Warniełło
24a5cdaea5STomasz Warniełło=head1 SYNOPSIS
25a5cdaea5STomasz Warniełło
26*56b0f453SJohannes Berg kernel-doc [-h] [-v] [-Werror] [-Wall] [-Wreturn] [-Wshort-description] [-Wcontents-before-sections]
27a5cdaea5STomasz Warniełło   [ -man |
28a5cdaea5STomasz Warniełło     -rst [-sphinx-version VERSION] [-enable-lineno] |
29a5cdaea5STomasz Warniełło     -none
30a5cdaea5STomasz Warniełło   ]
31a5cdaea5STomasz Warniełło   [
32a5cdaea5STomasz Warniełło     -export |
33a5cdaea5STomasz Warniełło     -internal |
34a5cdaea5STomasz Warniełło     [-function NAME] ... |
35a5cdaea5STomasz Warniełło     [-nosymbol NAME] ...
36a5cdaea5STomasz Warniełło   ]
37a5cdaea5STomasz Warniełło   [-no-doc-sections]
38a5cdaea5STomasz Warniełło   [-export-file FILE] ...
39a5cdaea5STomasz Warniełło   FILE ...
40a5cdaea5STomasz Warniełło
41a5cdaea5STomasz WarniełłoRun `kernel-doc -h` for details.
42a5cdaea5STomasz Warniełło
43f1583922STomasz Warniełło=head1 DESCRIPTION
44f1583922STomasz Warniełło
45f1583922STomasz WarniełłoRead C language source or header FILEs, extract embedded documentation comments,
46f1583922STomasz Warniełłoand print formatted documentation to standard output.
47f1583922STomasz Warniełło
48f1583922STomasz WarniełłoThe documentation comments are identified by the "/**" opening comment mark.
49f1583922STomasz Warniełło
50f1583922STomasz WarniełłoSee Documentation/doc-guide/kernel-doc.rst for the documentation comment syntax.
51f1583922STomasz Warniełło
52a5cdaea5STomasz Warniełło=cut
53a5cdaea5STomasz Warniełło
542875f787STomasz Warniełło# more perldoc at the end of the file
552875f787STomasz Warniełło
568484baaaSRandy Dunlap## init lots of data
578484baaaSRandy Dunlap
581da177e4SLinus Torvaldsmy $errors = 0;
591da177e4SLinus Torvaldsmy $warnings = 0;
605f8c7c98SRandy Dunlapmy $anon_struct_union = 0;
611da177e4SLinus Torvalds
621da177e4SLinus Torvalds# match expressions used to find embedded type information
63b97f193aSMauro Carvalho Chehabmy $type_constant = '\b``([^\`]+)``\b';
64b97f193aSMauro Carvalho Chehabmy $type_constant2 = '\%([-_\w]+)';
651da177e4SLinus Torvaldsmy $type_func = '(\w+)\(\)';
66bfd228c7SMike Rapoportmy $type_param = '\@(\w*((\.\w+)|(->\w+))*(\.\.\.)?)';
67ee2aa759SMauro Carvalho Chehabmy $type_param_ref = '([\!]?)\@(\w*((\.\w+)|(->\w+))*(\.\.\.)?)';
685219f18aSJonathan Corbetmy $type_fp_param = '\@(\w+)\(\)';  # Special RST handling for func ptr params
69346282dbSMauro Carvalho Chehabmy $type_fp_param2 = '\@(\w+->\S+)\(\)';  # Special RST handling for structs with func ptr params
701da177e4SLinus Torvaldsmy $type_env = '(\$\w+)';
71df31175bSPaolo Bonzinimy $type_enum = '\&(enum\s*([_\w]+))';
72df31175bSPaolo Bonzinimy $type_struct = '\&(struct\s*([_\w]+))';
73df31175bSPaolo Bonzinimy $type_typedef = '\&(typedef\s*([_\w]+))';
74df31175bSPaolo Bonzinimy $type_union = '\&(union\s*([_\w]+))';
755267dd35SPaolo Bonzinimy $type_member = '\&([_\w]+)(\.|->)([_\w]+)';
76df31175bSPaolo Bonzinimy $type_fallback = '\&([_\w]+)';
77f3341dcfSJani Nikulamy $type_member_func = $type_member . '\(\)';
781da177e4SLinus Torvalds
791da177e4SLinus Torvalds# Output conversion substitutions.
801da177e4SLinus Torvalds#  One for each output format
811da177e4SLinus Torvalds
821da177e4SLinus Torvalds# these are pretty rough
834d732701SDanilo Cesar Lemes de Paulamy @highlights_man = (
844d732701SDanilo Cesar Lemes de Paula                      [$type_constant, "\$1"],
85b97f193aSMauro Carvalho Chehab                      [$type_constant2, "\$1"],
864d732701SDanilo Cesar Lemes de Paula                      [$type_func, "\\\\fB\$1\\\\fP"],
87df31175bSPaolo Bonzini                      [$type_enum, "\\\\fI\$1\\\\fP"],
884d732701SDanilo Cesar Lemes de Paula                      [$type_struct, "\\\\fI\$1\\\\fP"],
89df31175bSPaolo Bonzini                      [$type_typedef, "\\\\fI\$1\\\\fP"],
90df31175bSPaolo Bonzini                      [$type_union, "\\\\fI\$1\\\\fP"],
915267dd35SPaolo Bonzini                      [$type_param, "\\\\fI\$1\\\\fP"],
92ee2aa759SMauro Carvalho Chehab                      [$type_param_ref, "\\\\fI\$1\$2\\\\fP"],
93df31175bSPaolo Bonzini                      [$type_member, "\\\\fI\$1\$2\$3\\\\fP"],
94df31175bSPaolo Bonzini                      [$type_fallback, "\\\\fI\$1\\\\fP"]
954d732701SDanilo Cesar Lemes de Paula		     );
961da177e4SLinus Torvaldsmy $blankline_man = "";
971da177e4SLinus Torvalds
98c0d1b6eeSJonathan Corbet# rst-mode
99c0d1b6eeSJonathan Corbetmy @highlights_rst = (
100c0d1b6eeSJonathan Corbet                       [$type_constant, "``\$1``"],
101b97f193aSMauro Carvalho Chehab                       [$type_constant2, "``\$1``"],
102f3341dcfSJani Nikula                       # Note: need to escape () to avoid func matching later
1035267dd35SPaolo Bonzini                       [$type_member_func, "\\:c\\:type\\:`\$1\$2\$3\\\\(\\\\) <\$1>`"],
1045267dd35SPaolo Bonzini                       [$type_member, "\\:c\\:type\\:`\$1\$2\$3 <\$1>`"],
1055219f18aSJonathan Corbet		       [$type_fp_param, "**\$1\\\\(\\\\)**"],
106346282dbSMauro Carvalho Chehab		       [$type_fp_param2, "**\$1\\\\(\\\\)**"],
107344fdb28SJonathan Corbet                       [$type_func, "\$1()"],
108df31175bSPaolo Bonzini                       [$type_enum, "\\:c\\:type\\:`\$1 <\$2>`"],
109df31175bSPaolo Bonzini                       [$type_struct, "\\:c\\:type\\:`\$1 <\$2>`"],
110df31175bSPaolo Bonzini                       [$type_typedef, "\\:c\\:type\\:`\$1 <\$2>`"],
111df31175bSPaolo Bonzini                       [$type_union, "\\:c\\:type\\:`\$1 <\$2>`"],
112a7291e7eSJani Nikula                       # in rst this can refer to any type
113df31175bSPaolo Bonzini                       [$type_fallback, "\\:c\\:type\\:`\$1`"],
114ee2aa759SMauro Carvalho Chehab                       [$type_param_ref, "**\$1\$2**"]
115c0d1b6eeSJonathan Corbet		      );
116c0d1b6eeSJonathan Corbetmy $blankline_rst = "\n";
117c0d1b6eeSJonathan Corbet
1181da177e4SLinus Torvalds# read arguments
1191da177e4SLinus Torvaldsif ($#ARGV == -1) {
12043caf1a6STomasz Warniełło	pod2usage(
12143caf1a6STomasz Warniełło		-message => "No arguments!\n",
12243caf1a6STomasz Warniełło		-exitval => 1,
12343caf1a6STomasz Warniełło		-verbose => 99,
12443caf1a6STomasz Warniełło		-sections => 'SYNOPSIS',
12543caf1a6STomasz Warniełło		-output => \*STDERR,
12643caf1a6STomasz Warniełło	);
1271da177e4SLinus Torvalds}
1281da177e4SLinus Torvalds
1298484baaaSRandy Dunlapmy $kernelversion;
13093351d41SMauro Carvalho Chehabmy ($sphinx_major, $sphinx_minor, $sphinx_patch);
131efa44475SMauro Carvalho Chehab
1328484baaaSRandy Dunlapmy $dohighlight = "";
1338484baaaSRandy Dunlap
1341da177e4SLinus Torvaldsmy $verbose = 0;
1352c12c810SPierre-Louis Bossartmy $Werror = 0;
136*56b0f453SJohannes Bergmy $Wreturn = 0;
137*56b0f453SJohannes Bergmy $Wshort_desc = 0;
138*56b0f453SJohannes Bergmy $Wcontents_before_sections = 0;
139bdfe2be3SMauro Carvalho Chehabmy $output_mode = "rst";
140e314ba31SDaniel Santosmy $output_preformatted = 0;
1414b44595aSJohannes Bergmy $no_doc_sections = 0;
1420b0f5f29SDaniel Vettermy $enable_lineno = 0;
143bdfe2be3SMauro Carvalho Chehabmy @highlights = @highlights_rst;
144bdfe2be3SMauro Carvalho Chehabmy $blankline = $blankline_rst;
1451da177e4SLinus Torvaldsmy $modulename = "Kernel API";
146b6c3f456SJani Nikula
147b6c3f456SJani Nikulause constant {
148b6c3f456SJani Nikula    OUTPUT_ALL          => 0, # output all symbols and doc sections
149b6c3f456SJani Nikula    OUTPUT_INCLUDE      => 1, # output only specified symbols
150eab795ddSMauro Carvalho Chehab    OUTPUT_EXPORTED     => 2, # output exported symbols
151eab795ddSMauro Carvalho Chehab    OUTPUT_INTERNAL     => 3, # output non-exported symbols
152b6c3f456SJani Nikula};
153b6c3f456SJani Nikulamy $output_selection = OUTPUT_ALL;
154b0d60bfbSJonathan Corbetmy $show_not_found = 0;	# No longer used
155b2c4105bSBen Hutchings
15688c2b57dSJani Nikulamy @export_file_list;
15788c2b57dSJani Nikula
158b2c4105bSBen Hutchingsmy @build_time;
159b2c4105bSBen Hutchingsif (defined($ENV{'KBUILD_BUILD_TIMESTAMP'}) &&
160b2c4105bSBen Hutchings    (my $seconds = `date -d"${ENV{'KBUILD_BUILD_TIMESTAMP'}}" +%s`) ne '') {
161b2c4105bSBen Hutchings    @build_time = gmtime($seconds);
162b2c4105bSBen Hutchings} else {
163b2c4105bSBen Hutchings    @build_time = localtime;
164b2c4105bSBen Hutchings}
165b2c4105bSBen Hutchings
1661da177e4SLinus Torvaldsmy $man_date = ('January', 'February', 'March', 'April', 'May', 'June',
1671da177e4SLinus Torvalds		'July', 'August', 'September', 'October',
168b2c4105bSBen Hutchings		'November', 'December')[$build_time[4]] .
169b2c4105bSBen Hutchings  " " . ($build_time[5]+1900);
1701da177e4SLinus Torvalds
1718484baaaSRandy Dunlap# Essentially these are globals.
172b9d97328SRandy Dunlap# They probably want to be tidied up, made more localised or something.
173b9d97328SRandy Dunlap# CAVEAT EMPTOR!  Some of the others I localised may not want to be, which
1741da177e4SLinus Torvalds# could cause "use of undefined value" or other bugs.
1751da177e4SLinus Torvaldsmy ($function, %function_table, %parametertypes, $declaration_purpose);
176eab795ddSMauro Carvalho Chehabmy %nosymbol_table = ();
1770b0f5f29SDaniel Vettermy $declaration_start_line;
1781da177e4SLinus Torvaldsmy ($type, $declaration_name, $return_type);
1791c32fd0cSIlya Dryomovmy ($newsection, $newcontents, $prototype, $brcount, %source_map);
1801da177e4SLinus Torvalds
181c0d3b831SMasahiro Yamadaif (defined($ENV{'KBUILD_VERBOSE'}) && $ENV{'KBUILD_VERBOSE'} =~ '1') {
182c0d3b831SMasahiro Yamada	$verbose = 1;
183bd0e88e5SRandy Dunlap}
184bd0e88e5SRandy Dunlap
1852c12c810SPierre-Louis Bossartif (defined($ENV{'KCFLAGS'})) {
1862c12c810SPierre-Louis Bossart	my $kcflags = "$ENV{'KCFLAGS'}";
1872c12c810SPierre-Louis Bossart
1882c12c810SPierre-Louis Bossart	if ($kcflags =~ /Werror/) {
1892c12c810SPierre-Louis Bossart		$Werror = 1;
1902c12c810SPierre-Louis Bossart	}
1912c12c810SPierre-Louis Bossart}
1922c12c810SPierre-Louis Bossart
193*56b0f453SJohannes Berg# reading this variable is for backwards compat just in case
194*56b0f453SJohannes Berg# someone was calling it with the variable from outside the
195*56b0f453SJohannes Berg# kernel's build system
196bed4ed30SLaurent Pinchartif (defined($ENV{'KDOC_WERROR'})) {
197bed4ed30SLaurent Pinchart	$Werror = "$ENV{'KDOC_WERROR'}";
198bed4ed30SLaurent Pinchart}
199*56b0f453SJohannes Berg# other environment variables are converted to command-line
200*56b0f453SJohannes Berg# arguments in cmd_checkdoc in the build system
201bed4ed30SLaurent Pinchart
2021da177e4SLinus Torvalds# Generated docbook code is inserted in a template at a point where
2031da177e4SLinus Torvalds# docbook v3.1 requires a non-zero sequence of RefEntry's; see:
20493431e06SAlexander A. Klimov# https://www.oasis-open.org/docbook/documentation/reference/html/refentry.html
2051da177e4SLinus Torvalds# We keep track of number of generated entries and generate a dummy
2061da177e4SLinus Torvalds# if needs be to ensure the expanded template can be postprocessed
2071da177e4SLinus Torvalds# into html.
2081da177e4SLinus Torvaldsmy $section_counter = 0;
2091da177e4SLinus Torvalds
2101da177e4SLinus Torvaldsmy $lineprefix="";
2111da177e4SLinus Torvalds
21248af606aSJani Nikula# Parser states
21348af606aSJani Nikulause constant {
21448af606aSJani Nikula    STATE_NORMAL        => 0,        # normal code
21548af606aSJani Nikula    STATE_NAME          => 1,        # looking for function name
21617b78717SJonathan Corbet    STATE_BODY_MAYBE    => 2,        # body - or maybe more description
21717b78717SJonathan Corbet    STATE_BODY          => 3,        # the body of the comment
2180d55d48bSMauro Carvalho Chehab    STATE_BODY_WITH_BLANK_LINE => 4, # the body, which has a blank line
2190d55d48bSMauro Carvalho Chehab    STATE_PROTO         => 5,        # scanning prototype
2200d55d48bSMauro Carvalho Chehab    STATE_DOCBLOCK      => 6,        # documentation block
2210d55d48bSMauro Carvalho Chehab    STATE_INLINE        => 7,        # gathering doc outside main block
22248af606aSJani Nikula};
2231da177e4SLinus Torvaldsmy $state;
224850622dfSRandy Dunlapmy $in_doc_sect;
225d742f24dSJonathan Corbetmy $leading_space;
2261da177e4SLinus Torvalds
22748af606aSJani Nikula# Inline documentation state
22848af606aSJani Nikulause constant {
22948af606aSJani Nikula    STATE_INLINE_NA     => 0, # not applicable ($state != STATE_INLINE)
23048af606aSJani Nikula    STATE_INLINE_NAME   => 1, # looking for member name (@foo:)
23148af606aSJani Nikula    STATE_INLINE_TEXT   => 2, # looking for member documentation
23248af606aSJani Nikula    STATE_INLINE_END    => 3, # done
23348af606aSJani Nikula    STATE_INLINE_ERROR  => 4, # error - Comment without header was found.
23448af606aSJani Nikula                              # Spit a warning as it's not
235a4c6ebedSDanilo Cesar Lemes de Paula                              # proper kernel-doc and ignore the rest.
23648af606aSJani Nikula};
23748af606aSJani Nikulamy $inline_doc_state;
238a4c6ebedSDanilo Cesar Lemes de Paula
2391da177e4SLinus Torvalds#declaration types: can be
2401da177e4SLinus Torvalds# 'function', 'struct', 'union', 'enum', 'typedef'
2411da177e4SLinus Torvaldsmy $decl_type;
2421da177e4SLinus Torvalds
24352042e2dSMauro Carvalho Chehab# Name of the kernel-doc identifier for non-DOC markups
24452042e2dSMauro Carvalho Chehabmy $identifier;
24552042e2dSMauro Carvalho Chehab
2461da177e4SLinus Torvaldsmy $doc_start = '^/\*\*\s*$'; # Allow whitespace at end of comment start.
2471da177e4SLinus Torvaldsmy $doc_end = '\*/';
2481da177e4SLinus Torvaldsmy $doc_com = '\s*\*\s*';
24912ae6779SDaniel Santosmy $doc_com_body = '\s*\* ?';
2501da177e4SLinus Torvaldsmy $doc_decl = $doc_com . '(\w+)';
251f624adefSJani Nikula# @params and a strictly limited set of supported section names
252212209cfSJonathan Corbet# Specifically:
253212209cfSJonathan Corbet#   Match @word:
254212209cfSJonathan Corbet#	  @...:
255212209cfSJonathan Corbet#         @{section-name}:
256212209cfSJonathan Corbet# while trying to not match literal block starts like "example::"
257212209cfSJonathan Corbet#
2588569de68SJonathan Corbetmy $doc_sect = $doc_com .
259212209cfSJonathan Corbet    '\s*(\@[.\w]+|\@\.\.\.|description|context|returns?|notes?|examples?)\s*:([^:].*)?$';
26012ae6779SDaniel Santosmy $doc_content = $doc_com_body . '(.*)';
2611da177e4SLinus Torvaldsmy $doc_block = $doc_com . 'DOC:\s*(.*)?';
26248af606aSJani Nikulamy $doc_inline_start = '^\s*/\*\*\s*$';
263fe7bc493SMauro Carvalho Chehabmy $doc_inline_sect = '\s*\*\s*(@\s*[\w][\w\.]*\s*):(.*)';
26448af606aSJani Nikulamy $doc_inline_end = '^\s*\*/\s*$';
2650c9aa209SJani Nikulamy $doc_inline_oneline = '^\s*/\*\*\s*(@[\w\s]+):\s*(.*)\s*\*/\s*$';
26686ae2e38SJani Nikulamy $export_symbol = '^\s*EXPORT_SYMBOL(_GPL)?\s*\(\s*(\w+)\s*\)\s*;';
267632ce137SJason Gunthorpemy $export_symbol_ns = '^\s*EXPORT_SYMBOL_NS(_GPL)?\s*\(\s*(\w+)\s*,\s*\w+\)\s*;';
268e86bdb24SAditya Srivastavamy $function_pointer = qr{([^\(]*\(\*)\s*\)\s*\(([^\)]*)\)};
269e86bdb24SAditya Srivastavamy $attribute = qr{__attribute__\s*\(\([a-z0-9,_\*\s\(\)]*\)\)}i;
2701da177e4SLinus Torvalds
2711da177e4SLinus Torvaldsmy %parameterdescs;
2720b0f5f29SDaniel Vettermy %parameterdesc_start_lines;
2731da177e4SLinus Torvaldsmy @parameterlist;
2741da177e4SLinus Torvaldsmy %sections;
2751da177e4SLinus Torvaldsmy @sectionlist;
2760b0f5f29SDaniel Vettermy %section_start_lines;
277a1d94aa5SRandy Dunlapmy $sectcheck;
278a1d94aa5SRandy Dunlapmy $struct_actual;
2791da177e4SLinus Torvalds
2801da177e4SLinus Torvaldsmy $contents = "";
2810b0f5f29SDaniel Vettermy $new_start_line = 0;
282f624adefSJani Nikula
283f624adefSJani Nikula# the canonical section names. see also $doc_sect above.
2841da177e4SLinus Torvaldsmy $section_default = "Description";	# default section
2851da177e4SLinus Torvaldsmy $section_intro = "Introduction";
2861da177e4SLinus Torvaldsmy $section = $section_default;
2871da177e4SLinus Torvaldsmy $section_context = "Context";
2884092bac7SYacine Belkadimy $section_return = "Return";
2891da177e4SLinus Torvalds
2901da177e4SLinus Torvaldsmy $undescribed = "-- undescribed --";
2911da177e4SLinus Torvalds
2921da177e4SLinus Torvaldsreset_state();
2931da177e4SLinus Torvalds
294b031ac4eSMauro Carvalho Chehabwhile ($ARGV[0] =~ m/^--?(.*)/) {
295b031ac4eSMauro Carvalho Chehab    my $cmd = $1;
296b031ac4eSMauro Carvalho Chehab    shift @ARGV;
297b031ac4eSMauro Carvalho Chehab    if ($cmd eq "man") {
2981da177e4SLinus Torvalds	$output_mode = "man";
2994d732701SDanilo Cesar Lemes de Paula	@highlights = @highlights_man;
3001da177e4SLinus Torvalds	$blankline = $blankline_man;
301b031ac4eSMauro Carvalho Chehab    } elsif ($cmd eq "rst") {
302c0d1b6eeSJonathan Corbet	$output_mode = "rst";
303c0d1b6eeSJonathan Corbet	@highlights = @highlights_rst;
304c0d1b6eeSJonathan Corbet	$blankline = $blankline_rst;
305b031ac4eSMauro Carvalho Chehab    } elsif ($cmd eq "none") {
3063a025e1dSMatthew Wilcox	$output_mode = "none";
307b031ac4eSMauro Carvalho Chehab    } elsif ($cmd eq "module") { # not needed for XML, inherits from calling document
3081da177e4SLinus Torvalds	$modulename = shift @ARGV;
309b031ac4eSMauro Carvalho Chehab    } elsif ($cmd eq "function") { # to only output specific functions
310b6c3f456SJani Nikula	$output_selection = OUTPUT_INCLUDE;
3111da177e4SLinus Torvalds	$function = shift @ARGV;
3121da177e4SLinus Torvalds	$function_table{$function} = 1;
313eab795ddSMauro Carvalho Chehab    } elsif ($cmd eq "nosymbol") { # Exclude specific symbols
314eab795ddSMauro Carvalho Chehab	my $symbol = shift @ARGV;
315eab795ddSMauro Carvalho Chehab	$nosymbol_table{$symbol} = 1;
316b031ac4eSMauro Carvalho Chehab    } elsif ($cmd eq "export") { # only exported symbols
317b6c3f456SJani Nikula	$output_selection = OUTPUT_EXPORTED;
318da9726ecSJani Nikula	%function_table = ();
319b031ac4eSMauro Carvalho Chehab    } elsif ($cmd eq "internal") { # only non-exported symbols
320b6c3f456SJani Nikula	$output_selection = OUTPUT_INTERNAL;
321da9726ecSJani Nikula	%function_table = ();
322b031ac4eSMauro Carvalho Chehab    } elsif ($cmd eq "export-file") {
32388c2b57dSJani Nikula	my $file = shift @ARGV;
32488c2b57dSJani Nikula	push(@export_file_list, $file);
325b031ac4eSMauro Carvalho Chehab    } elsif ($cmd eq "v") {
3261da177e4SLinus Torvalds	$verbose = 1;
3272c12c810SPierre-Louis Bossart    } elsif ($cmd eq "Werror") {
3282c12c810SPierre-Louis Bossart	$Werror = 1;
329*56b0f453SJohannes Berg    } elsif ($cmd eq "Wreturn") {
330*56b0f453SJohannes Berg	$Wreturn = 1;
331*56b0f453SJohannes Berg    } elsif ($cmd eq "Wshort-desc") {
332*56b0f453SJohannes Berg	$Wshort_desc = 1;
333*56b0f453SJohannes Berg    } elsif ($cmd eq "Wcontents-before-sections") {
334*56b0f453SJohannes Berg	$Wcontents_before_sections = 1;
335*56b0f453SJohannes Berg    } elsif ($cmd eq "Wall") {
336*56b0f453SJohannes Berg        $Wreturn = 1;
337*56b0f453SJohannes Berg        $Wshort_desc = 1;
338*56b0f453SJohannes Berg        $Wcontents_before_sections = 1;
339b031ac4eSMauro Carvalho Chehab    } elsif (($cmd eq "h") || ($cmd eq "help")) {
340252b47daSTomasz Warniełło		pod2usage(-exitval => 0, -verbose => 2);
341b031ac4eSMauro Carvalho Chehab    } elsif ($cmd eq 'no-doc-sections') {
3424b44595aSJohannes Berg	    $no_doc_sections = 1;
343b031ac4eSMauro Carvalho Chehab    } elsif ($cmd eq 'enable-lineno') {
3440b0f5f29SDaniel Vetter	    $enable_lineno = 1;
345b031ac4eSMauro Carvalho Chehab    } elsif ($cmd eq 'show-not-found') {
346b0d60bfbSJonathan Corbet	$show_not_found = 1;  # A no-op but don't fail
34793351d41SMauro Carvalho Chehab    } elsif ($cmd eq "sphinx-version") {
34893351d41SMauro Carvalho Chehab	my $ver_string = shift @ARGV;
34993351d41SMauro Carvalho Chehab	if ($ver_string =~ m/^(\d+)(\.\d+)?(\.\d+)?/) {
35093351d41SMauro Carvalho Chehab	    $sphinx_major = $1;
35193351d41SMauro Carvalho Chehab	    if (defined($2)) {
35293351d41SMauro Carvalho Chehab		$sphinx_minor = substr($2,1);
35393351d41SMauro Carvalho Chehab	    } else {
35493351d41SMauro Carvalho Chehab		$sphinx_minor = 0;
35593351d41SMauro Carvalho Chehab	    }
35693351d41SMauro Carvalho Chehab	    if (defined($3)) {
35793351d41SMauro Carvalho Chehab		$sphinx_patch = substr($3,1)
35893351d41SMauro Carvalho Chehab	    } else {
35993351d41SMauro Carvalho Chehab		$sphinx_patch = 0;
36093351d41SMauro Carvalho Chehab	    }
36193351d41SMauro Carvalho Chehab	} else {
36293351d41SMauro Carvalho Chehab	    die "Sphinx version should either major.minor or major.minor.patch format\n";
36393351d41SMauro Carvalho Chehab	}
364b031ac4eSMauro Carvalho Chehab    } else {
365b031ac4eSMauro Carvalho Chehab	# Unknown argument
36643caf1a6STomasz Warniełło	pod2usage(
36743caf1a6STomasz Warniełło	    -message => "Argument unknown!\n",
36843caf1a6STomasz Warniełło	    -exitval => 1,
36943caf1a6STomasz Warniełło	    -verbose => 99,
37043caf1a6STomasz Warniełło	    -sections => 'SYNOPSIS',
37143caf1a6STomasz Warniełło	    -output => \*STDERR,
37243caf1a6STomasz Warniełło	    );
373e334f873SAkira Yokosawa    }
374e334f873SAkira Yokosawa    if ($#ARGV < 0){
375e334f873SAkira Yokosawa	pod2usage(
376e334f873SAkira Yokosawa	    -message => "FILE argument missing\n",
377e334f873SAkira Yokosawa	    -exitval => 1,
378e334f873SAkira Yokosawa	    -verbose => 99,
379e334f873SAkira Yokosawa	    -sections => 'SYNOPSIS',
380e334f873SAkira Yokosawa	    -output => \*STDERR,
381e334f873SAkira Yokosawa	    );
3821da177e4SLinus Torvalds    }
3831da177e4SLinus Torvalds}
3841da177e4SLinus Torvalds
3858484baaaSRandy Dunlap# continue execution near EOF;
3868484baaaSRandy Dunlap
387efa44475SMauro Carvalho Chehab# The C domain dialect changed on Sphinx 3. So, we need to check the
388efa44475SMauro Carvalho Chehab# version in order to produce the right tags.
389efa44475SMauro Carvalho Chehabsub findprog($)
390efa44475SMauro Carvalho Chehab{
391efa44475SMauro Carvalho Chehab	foreach(split(/:/, $ENV{PATH})) {
392efa44475SMauro Carvalho Chehab		return "$_/$_[0]" if(-x "$_/$_[0]");
393efa44475SMauro Carvalho Chehab	}
394efa44475SMauro Carvalho Chehab}
395efa44475SMauro Carvalho Chehab
396efa44475SMauro Carvalho Chehabsub get_sphinx_version()
397efa44475SMauro Carvalho Chehab{
398efa44475SMauro Carvalho Chehab	my $ver;
399efa44475SMauro Carvalho Chehab
400efa44475SMauro Carvalho Chehab	my $cmd = "sphinx-build";
401efa44475SMauro Carvalho Chehab	if (!findprog($cmd)) {
402efa44475SMauro Carvalho Chehab		my $cmd = "sphinx-build3";
40393351d41SMauro Carvalho Chehab		if (!findprog($cmd)) {
40493351d41SMauro Carvalho Chehab			$sphinx_major = 1;
40593351d41SMauro Carvalho Chehab			$sphinx_minor = 2;
40693351d41SMauro Carvalho Chehab			$sphinx_patch = 0;
40793351d41SMauro Carvalho Chehab			printf STDERR "Warning: Sphinx version not found. Using default (Sphinx version %d.%d.%d)\n",
40893351d41SMauro Carvalho Chehab			       $sphinx_major, $sphinx_minor, $sphinx_patch;
40993351d41SMauro Carvalho Chehab			return;
41093351d41SMauro Carvalho Chehab		}
411efa44475SMauro Carvalho Chehab	}
412efa44475SMauro Carvalho Chehab
413efa44475SMauro Carvalho Chehab	open IN, "$cmd --version 2>&1 |";
414efa44475SMauro Carvalho Chehab	while (<IN>) {
415efa44475SMauro Carvalho Chehab		if (m/^\s*sphinx-build\s+([\d]+)\.([\d\.]+)(\+\/[\da-f]+)?$/) {
41693351d41SMauro Carvalho Chehab			$sphinx_major = $1;
41793351d41SMauro Carvalho Chehab			$sphinx_minor = $2;
41893351d41SMauro Carvalho Chehab			$sphinx_patch = $3;
419efa44475SMauro Carvalho Chehab			last;
420efa44475SMauro Carvalho Chehab		}
421efa44475SMauro Carvalho Chehab		# Sphinx 1.2.x uses a different format
422efa44475SMauro Carvalho Chehab		if (m/^\s*Sphinx.*\s+([\d]+)\.([\d\.]+)$/) {
42393351d41SMauro Carvalho Chehab			$sphinx_major = $1;
42493351d41SMauro Carvalho Chehab			$sphinx_minor = $2;
42593351d41SMauro Carvalho Chehab			$sphinx_patch = $3;
426efa44475SMauro Carvalho Chehab			last;
427efa44475SMauro Carvalho Chehab		}
428efa44475SMauro Carvalho Chehab	}
429efa44475SMauro Carvalho Chehab	close IN;
430efa44475SMauro Carvalho Chehab}
431efa44475SMauro Carvalho Chehab
43253f049faSBorislav Petkov# get kernel version from env
43353f049faSBorislav Petkovsub get_kernel_version() {
4341b9bc22dSJohannes Berg    my $version = 'unknown kernel version';
43553f049faSBorislav Petkov
43653f049faSBorislav Petkov    if (defined($ENV{'KERNELVERSION'})) {
43753f049faSBorislav Petkov	$version = $ENV{'KERNELVERSION'};
43853f049faSBorislav Petkov    }
43953f049faSBorislav Petkov    return $version;
44053f049faSBorislav Petkov}
4411da177e4SLinus Torvalds
4420b0f5f29SDaniel Vetter#
4430b0f5f29SDaniel Vettersub print_lineno {
4440b0f5f29SDaniel Vetter    my $lineno = shift;
4450b0f5f29SDaniel Vetter    if ($enable_lineno && defined($lineno)) {
446b79dfef0SMauro Carvalho Chehab        print ".. LINENO " . $lineno . "\n";
4470b0f5f29SDaniel Vetter    }
4480b0f5f29SDaniel Vetter}
449df4bf98eSNiklas Söderlund
450df4bf98eSNiklas Söderlundsub emit_warning {
451df4bf98eSNiklas Söderlund    my $location = shift;
452df4bf98eSNiklas Söderlund    my $msg = shift;
453df4bf98eSNiklas Söderlund    print STDERR "$location: warning: $msg";
454df4bf98eSNiklas Söderlund    ++$warnings;
455df4bf98eSNiklas Söderlund}
4561da177e4SLinus Torvalds##
4571da177e4SLinus Torvalds# dumps section contents to arrays/hashes intended for that purpose.
4581da177e4SLinus Torvalds#
4591da177e4SLinus Torvaldssub dump_section {
46094dc7ad5SRandy Dunlap    my $file = shift;
4611da177e4SLinus Torvalds    my $name = shift;
4621da177e4SLinus Torvalds    my $contents = join "\n", @_;
4631da177e4SLinus Torvalds
46413901ef2SJani Nikula    if ($name =~ m/$type_param/) {
4651da177e4SLinus Torvalds	$name = $1;
4661da177e4SLinus Torvalds	$parameterdescs{$name} = $contents;
467a1d94aa5SRandy Dunlap	$sectcheck = $sectcheck . $name . " ";
4680b0f5f29SDaniel Vetter        $parameterdesc_start_lines{$name} = $new_start_line;
4690b0f5f29SDaniel Vetter        $new_start_line = 0;
470ced69090SRandy Dunlap    } elsif ($name eq "@\.\.\.") {
471ced69090SRandy Dunlap	$name = "...";
472ced69090SRandy Dunlap	$parameterdescs{$name} = $contents;
473a1d94aa5SRandy Dunlap	$sectcheck = $sectcheck . $name . " ";
4740b0f5f29SDaniel Vetter        $parameterdesc_start_lines{$name} = $new_start_line;
4750b0f5f29SDaniel Vetter        $new_start_line = 0;
4761da177e4SLinus Torvalds    } else {
47794dc7ad5SRandy Dunlap	if (defined($sections{$name}) && ($sections{$name} ne "")) {
47895b6be9dSJani Nikula	    # Only warn on user specified duplicate section names.
47995b6be9dSJani Nikula	    if ($name ne $section_default) {
480df4bf98eSNiklas Söderlund		emit_warning("${file}:$.", "duplicate section name '$name'\n");
48195b6be9dSJani Nikula	    }
48232217761SJani Nikula	    $sections{$name} .= $contents;
48332217761SJani Nikula	} else {
4841da177e4SLinus Torvalds	    $sections{$name} = $contents;
4851da177e4SLinus Torvalds	    push @sectionlist, $name;
4860b0f5f29SDaniel Vetter            $section_start_lines{$name} = $new_start_line;
4870b0f5f29SDaniel Vetter            $new_start_line = 0;
4881da177e4SLinus Torvalds	}
4891da177e4SLinus Torvalds    }
49032217761SJani Nikula}
4911da177e4SLinus Torvalds
4921da177e4SLinus Torvalds##
493b112e0f7SJohannes Berg# dump DOC: section after checking that it should go out
494b112e0f7SJohannes Berg#
495b112e0f7SJohannes Bergsub dump_doc_section {
49694dc7ad5SRandy Dunlap    my $file = shift;
497b112e0f7SJohannes Berg    my $name = shift;
498b112e0f7SJohannes Berg    my $contents = join "\n", @_;
499b112e0f7SJohannes Berg
5004b44595aSJohannes Berg    if ($no_doc_sections) {
5014b44595aSJohannes Berg        return;
5024b44595aSJohannes Berg    }
5034b44595aSJohannes Berg
504eab795ddSMauro Carvalho Chehab    return if (defined($nosymbol_table{$name}));
505eab795ddSMauro Carvalho Chehab
506b6c3f456SJani Nikula    if (($output_selection == OUTPUT_ALL) ||
507eab795ddSMauro Carvalho Chehab	(($output_selection == OUTPUT_INCLUDE) &&
508eab795ddSMauro Carvalho Chehab	 defined($function_table{$name})))
509b112e0f7SJohannes Berg    {
51094dc7ad5SRandy Dunlap	dump_section($file, $name, $contents);
511b112e0f7SJohannes Berg	output_blockhead({'sectionlist' => \@sectionlist,
512b112e0f7SJohannes Berg			  'sections' => \%sections,
513b112e0f7SJohannes Berg			  'module' => $modulename,
514b6c3f456SJani Nikula			  'content-only' => ($output_selection != OUTPUT_ALL), });
515b112e0f7SJohannes Berg    }
516b112e0f7SJohannes Berg}
517b112e0f7SJohannes Berg
518b112e0f7SJohannes Berg##
5191da177e4SLinus Torvalds# output function
5201da177e4SLinus Torvalds#
5211da177e4SLinus Torvalds# parameterdescs, a hash.
5221da177e4SLinus Torvalds#  function => "function name"
5231da177e4SLinus Torvalds#  parameterlist => @list of parameters
5241da177e4SLinus Torvalds#  parameterdescs => %parameter descriptions
5251da177e4SLinus Torvalds#  sectionlist => @list of sections
526a21217daSRandy Dunlap#  sections => %section descriptions
5271da177e4SLinus Torvalds#
5281da177e4SLinus Torvalds
5291da177e4SLinus Torvaldssub output_highlight {
5301da177e4SLinus Torvalds    my $contents = join "\n",@_;
5311da177e4SLinus Torvalds    my $line;
5321da177e4SLinus Torvalds
5331da177e4SLinus Torvalds#   DEBUG
5341da177e4SLinus Torvalds#   if (!defined $contents) {
5351da177e4SLinus Torvalds#	use Carp;
5361da177e4SLinus Torvalds#	confess "output_highlight got called with no args?\n";
5371da177e4SLinus Torvalds#   }
5381da177e4SLinus Torvalds
5393eb014a1SRandy Dunlap#   print STDERR "contents b4:$contents\n";
5401da177e4SLinus Torvalds    eval $dohighlight;
5411da177e4SLinus Torvalds    die $@ if $@;
5423eb014a1SRandy Dunlap#   print STDERR "contents af:$contents\n";
5433eb014a1SRandy Dunlap
5441da177e4SLinus Torvalds    foreach $line (split "\n", $contents) {
54512ae6779SDaniel Santos	if (! $output_preformatted) {
54612ae6779SDaniel Santos	    $line =~ s/^\s*//;
54712ae6779SDaniel Santos	}
5481da177e4SLinus Torvalds	if ($line eq ""){
549e314ba31SDaniel Santos	    if (! $output_preformatted) {
5500bba924cSJonathan Corbet		print $lineprefix, $blankline;
551e314ba31SDaniel Santos	    }
5521da177e4SLinus Torvalds	} else {
553cdccb316SRandy Dunlap	    if ($output_mode eq "man" && substr($line, 0, 1) eq ".") {
554cdccb316SRandy Dunlap		print "\\&$line";
555cdccb316SRandy Dunlap	    } else {
5561da177e4SLinus Torvalds		print $lineprefix, $line;
5571da177e4SLinus Torvalds	    }
558cdccb316SRandy Dunlap	}
5591da177e4SLinus Torvalds	print "\n";
5601da177e4SLinus Torvalds    }
5611da177e4SLinus Torvalds}
5621da177e4SLinus Torvalds
5631da177e4SLinus Torvalds##
5641da177e4SLinus Torvalds# output function in man
5651da177e4SLinus Torvaldssub output_function_man(%) {
5661da177e4SLinus Torvalds    my %args = %{$_[0]};
5671da177e4SLinus Torvalds    my ($parameter, $section);
5681da177e4SLinus Torvalds    my $count;
5691da177e4SLinus Torvalds
5701da177e4SLinus Torvalds    print ".TH \"$args{'function'}\" 9 \"$args{'function'}\" \"$man_date\" \"Kernel Hacker's Manual\" LINUX\n";
5711da177e4SLinus Torvalds
5721da177e4SLinus Torvalds    print ".SH NAME\n";
5731da177e4SLinus Torvalds    print $args{'function'} . " \\- " . $args{'purpose'} . "\n";
5741da177e4SLinus Torvalds
5751da177e4SLinus Torvalds    print ".SH SYNOPSIS\n";
576a21217daSRandy Dunlap    if ($args{'functiontype'} ne "") {
5771da177e4SLinus Torvalds	print ".B \"" . $args{'functiontype'} . "\" " . $args{'function'} . "\n";
578a21217daSRandy Dunlap    } else {
579a21217daSRandy Dunlap	print ".B \"" . $args{'function'} . "\n";
580a21217daSRandy Dunlap    }
5811da177e4SLinus Torvalds    $count = 0;
5821da177e4SLinus Torvalds    my $parenth = "(";
5831da177e4SLinus Torvalds    my $post = ",";
5841da177e4SLinus Torvalds    foreach my $parameter (@{$args{'parameterlist'}}) {
5851da177e4SLinus Torvalds	if ($count == $#{$args{'parameterlist'}}) {
5861da177e4SLinus Torvalds	    $post = ");";
5871da177e4SLinus Torvalds	}
5881da177e4SLinus Torvalds	$type = $args{'parametertypes'}{$parameter};
589e86bdb24SAditya Srivastava	if ($type =~ m/$function_pointer/) {
5901da177e4SLinus Torvalds	    # pointer-to-function
591ed8348e2SMauro Carvalho Chehab	    print ".BI \"" . $parenth . $1 . "\" " . " \") (" . $2 . ")" . $post . "\"\n";
5921da177e4SLinus Torvalds	} else {
5931da177e4SLinus Torvalds	    $type =~ s/([^\*])$/$1 /;
594ed8348e2SMauro Carvalho Chehab	    print ".BI \"" . $parenth . $type . "\" " . " \"" . $post . "\"\n";
5951da177e4SLinus Torvalds	}
5961da177e4SLinus Torvalds	$count++;
5971da177e4SLinus Torvalds	$parenth = "";
5981da177e4SLinus Torvalds    }
5991da177e4SLinus Torvalds
6001da177e4SLinus Torvalds    print ".SH ARGUMENTS\n";
6011da177e4SLinus Torvalds    foreach $parameter (@{$args{'parameterlist'}}) {
6021da177e4SLinus Torvalds	my $parameter_name = $parameter;
6031da177e4SLinus Torvalds	$parameter_name =~ s/\[.*//;
6041da177e4SLinus Torvalds
6051da177e4SLinus Torvalds	print ".IP \"" . $parameter . "\" 12\n";
6061da177e4SLinus Torvalds	output_highlight($args{'parameterdescs'}{$parameter_name});
6071da177e4SLinus Torvalds    }
6081da177e4SLinus Torvalds    foreach $section (@{$args{'sectionlist'}}) {
6091da177e4SLinus Torvalds	print ".SH \"", uc $section, "\"\n";
6101da177e4SLinus Torvalds	output_highlight($args{'sections'}{$section});
6111da177e4SLinus Torvalds    }
6121da177e4SLinus Torvalds}
6131da177e4SLinus Torvalds
6141da177e4SLinus Torvalds##
6151da177e4SLinus Torvalds# output enum in man
6161da177e4SLinus Torvaldssub output_enum_man(%) {
6171da177e4SLinus Torvalds    my %args = %{$_[0]};
6181da177e4SLinus Torvalds    my ($parameter, $section);
6191da177e4SLinus Torvalds    my $count;
6201da177e4SLinus Torvalds
6211da177e4SLinus Torvalds    print ".TH \"$args{'module'}\" 9 \"enum $args{'enum'}\" \"$man_date\" \"API Manual\" LINUX\n";
6221da177e4SLinus Torvalds
6231da177e4SLinus Torvalds    print ".SH NAME\n";
6241da177e4SLinus Torvalds    print "enum " . $args{'enum'} . " \\- " . $args{'purpose'} . "\n";
6251da177e4SLinus Torvalds
6261da177e4SLinus Torvalds    print ".SH SYNOPSIS\n";
6271da177e4SLinus Torvalds    print "enum " . $args{'enum'} . " {\n";
6281da177e4SLinus Torvalds    $count = 0;
6291da177e4SLinus Torvalds    foreach my $parameter (@{$args{'parameterlist'}}) {
6301da177e4SLinus Torvalds	print ".br\n.BI \"    $parameter\"\n";
6311da177e4SLinus Torvalds	if ($count == $#{$args{'parameterlist'}}) {
6321da177e4SLinus Torvalds	    print "\n};\n";
6331da177e4SLinus Torvalds	    last;
6341da177e4SLinus Torvalds	}
6351da177e4SLinus Torvalds	else {
6361da177e4SLinus Torvalds	    print ", \n.br\n";
6371da177e4SLinus Torvalds	}
6381da177e4SLinus Torvalds	$count++;
6391da177e4SLinus Torvalds    }
6401da177e4SLinus Torvalds
6411da177e4SLinus Torvalds    print ".SH Constants\n";
6421da177e4SLinus Torvalds    foreach $parameter (@{$args{'parameterlist'}}) {
6431da177e4SLinus Torvalds	my $parameter_name = $parameter;
6441da177e4SLinus Torvalds	$parameter_name =~ s/\[.*//;
6451da177e4SLinus Torvalds
6461da177e4SLinus Torvalds	print ".IP \"" . $parameter . "\" 12\n";
6471da177e4SLinus Torvalds	output_highlight($args{'parameterdescs'}{$parameter_name});
6481da177e4SLinus Torvalds    }
6491da177e4SLinus Torvalds    foreach $section (@{$args{'sectionlist'}}) {
6501da177e4SLinus Torvalds	print ".SH \"$section\"\n";
6511da177e4SLinus Torvalds	output_highlight($args{'sections'}{$section});
6521da177e4SLinus Torvalds    }
6531da177e4SLinus Torvalds}
6541da177e4SLinus Torvalds
6551da177e4SLinus Torvalds##
6561da177e4SLinus Torvalds# output struct in man
6571da177e4SLinus Torvaldssub output_struct_man(%) {
6581da177e4SLinus Torvalds    my %args = %{$_[0]};
6591da177e4SLinus Torvalds    my ($parameter, $section);
6601da177e4SLinus Torvalds
6611da177e4SLinus Torvalds    print ".TH \"$args{'module'}\" 9 \"" . $args{'type'} . " " . $args{'struct'} . "\" \"$man_date\" \"API Manual\" LINUX\n";
6621da177e4SLinus Torvalds
6631da177e4SLinus Torvalds    print ".SH NAME\n";
6641da177e4SLinus Torvalds    print $args{'type'} . " " . $args{'struct'} . " \\- " . $args{'purpose'} . "\n";
6651da177e4SLinus Torvalds
6668ad72163SMauro Carvalho Chehab    my $declaration = $args{'definition'};
6678ad72163SMauro Carvalho Chehab    $declaration =~ s/\t/  /g;
6688ad72163SMauro Carvalho Chehab    $declaration =~ s/\n/"\n.br\n.BI \"/g;
6691da177e4SLinus Torvalds    print ".SH SYNOPSIS\n";
6701da177e4SLinus Torvalds    print $args{'type'} . " " . $args{'struct'} . " {\n.br\n";
6718ad72163SMauro Carvalho Chehab    print ".BI \"$declaration\n};\n.br\n\n";
6721da177e4SLinus Torvalds
673c51d3dacSRandy Dunlap    print ".SH Members\n";
6741da177e4SLinus Torvalds    foreach $parameter (@{$args{'parameterlist'}}) {
6751da177e4SLinus Torvalds	($parameter =~ /^#/) && next;
6761da177e4SLinus Torvalds
6771da177e4SLinus Torvalds	my $parameter_name = $parameter;
6781da177e4SLinus Torvalds	$parameter_name =~ s/\[.*//;
6791da177e4SLinus Torvalds
6801da177e4SLinus Torvalds	($args{'parameterdescs'}{$parameter_name} ne $undescribed) || next;
6811da177e4SLinus Torvalds	print ".IP \"" . $parameter . "\" 12\n";
6821da177e4SLinus Torvalds	output_highlight($args{'parameterdescs'}{$parameter_name});
6831da177e4SLinus Torvalds    }
6841da177e4SLinus Torvalds    foreach $section (@{$args{'sectionlist'}}) {
6851da177e4SLinus Torvalds	print ".SH \"$section\"\n";
6861da177e4SLinus Torvalds	output_highlight($args{'sections'}{$section});
6871da177e4SLinus Torvalds    }
6881da177e4SLinus Torvalds}
6891da177e4SLinus Torvalds
6901da177e4SLinus Torvalds##
6911da177e4SLinus Torvalds# output typedef in man
6921da177e4SLinus Torvaldssub output_typedef_man(%) {
6931da177e4SLinus Torvalds    my %args = %{$_[0]};
6941da177e4SLinus Torvalds    my ($parameter, $section);
6951da177e4SLinus Torvalds
6961da177e4SLinus Torvalds    print ".TH \"$args{'module'}\" 9 \"$args{'typedef'}\" \"$man_date\" \"API Manual\" LINUX\n";
6971da177e4SLinus Torvalds
6981da177e4SLinus Torvalds    print ".SH NAME\n";
6991da177e4SLinus Torvalds    print "typedef " . $args{'typedef'} . " \\- " . $args{'purpose'} . "\n";
7001da177e4SLinus Torvalds
7011da177e4SLinus Torvalds    foreach $section (@{$args{'sectionlist'}}) {
7021da177e4SLinus Torvalds	print ".SH \"$section\"\n";
7031da177e4SLinus Torvalds	output_highlight($args{'sections'}{$section});
7041da177e4SLinus Torvalds    }
7051da177e4SLinus Torvalds}
7061da177e4SLinus Torvalds
707b112e0f7SJohannes Bergsub output_blockhead_man(%) {
7081da177e4SLinus Torvalds    my %args = %{$_[0]};
7091da177e4SLinus Torvalds    my ($parameter, $section);
7101da177e4SLinus Torvalds    my $count;
7111da177e4SLinus Torvalds
7121da177e4SLinus Torvalds    print ".TH \"$args{'module'}\" 9 \"$args{'module'}\" \"$man_date\" \"API Manual\" LINUX\n";
7131da177e4SLinus Torvalds
7141da177e4SLinus Torvalds    foreach $section (@{$args{'sectionlist'}}) {
7151da177e4SLinus Torvalds	print ".SH \"$section\"\n";
7161da177e4SLinus Torvalds	output_highlight($args{'sections'}{$section});
7171da177e4SLinus Torvalds    }
7181da177e4SLinus Torvalds}
7191da177e4SLinus Torvalds
7201da177e4SLinus Torvalds##
721c0d1b6eeSJonathan Corbet# output in restructured text
722c0d1b6eeSJonathan Corbet#
723c0d1b6eeSJonathan Corbet
724c0d1b6eeSJonathan Corbet#
725c0d1b6eeSJonathan Corbet# This could use some work; it's used to output the DOC: sections, and
726c0d1b6eeSJonathan Corbet# starts by putting out the name of the doc section itself, but that tends
727c0d1b6eeSJonathan Corbet# to duplicate a header already in the template file.
728c0d1b6eeSJonathan Corbet#
729c0d1b6eeSJonathan Corbetsub output_blockhead_rst(%) {
730c0d1b6eeSJonathan Corbet    my %args = %{$_[0]};
731c0d1b6eeSJonathan Corbet    my ($parameter, $section);
732c0d1b6eeSJonathan Corbet
733c0d1b6eeSJonathan Corbet    foreach $section (@{$args{'sectionlist'}}) {
734eab795ddSMauro Carvalho Chehab	next if (defined($nosymbol_table{$section}));
735eab795ddSMauro Carvalho Chehab
7369e72184bSJani Nikula	if ($output_selection != OUTPUT_INCLUDE) {
73706a755d6SMichal Wajdeczko	    print ".. _$section:\n\n";
738c0d1b6eeSJonathan Corbet	    print "**$section**\n\n";
7399e72184bSJani Nikula	}
7400b0f5f29SDaniel Vetter        print_lineno($section_start_lines{$section});
741c0d1b6eeSJonathan Corbet	output_highlight_rst($args{'sections'}{$section});
742c0d1b6eeSJonathan Corbet	print "\n";
743c0d1b6eeSJonathan Corbet    }
744c0d1b6eeSJonathan Corbet}
745c0d1b6eeSJonathan Corbet
746af250290SJonathan Corbet#
747af250290SJonathan Corbet# Apply the RST highlights to a sub-block of text.
748af250290SJonathan Corbet#
749af250290SJonathan Corbetsub highlight_block($) {
750af250290SJonathan Corbet    # The dohighlight kludge requires the text be called $contents
751af250290SJonathan Corbet    my $contents = shift;
752c0d1b6eeSJonathan Corbet    eval $dohighlight;
753c0d1b6eeSJonathan Corbet    die $@ if $@;
754af250290SJonathan Corbet    return $contents;
755af250290SJonathan Corbet}
756c0d1b6eeSJonathan Corbet
757af250290SJonathan Corbet#
758af250290SJonathan Corbet# Regexes used only here.
759af250290SJonathan Corbet#
760af250290SJonathan Corbetmy $sphinx_literal = '^[^.].*::$';
761af250290SJonathan Corbetmy $sphinx_cblock = '^\.\.\ +code-block::';
762af250290SJonathan Corbet
763af250290SJonathan Corbetsub output_highlight_rst {
764af250290SJonathan Corbet    my $input = join "\n",@_;
765af250290SJonathan Corbet    my $output = "";
766af250290SJonathan Corbet    my $line;
767af250290SJonathan Corbet    my $in_literal = 0;
768af250290SJonathan Corbet    my $litprefix;
769af250290SJonathan Corbet    my $block = "";
770af250290SJonathan Corbet
771af250290SJonathan Corbet    foreach $line (split "\n",$input) {
772af250290SJonathan Corbet	#
773af250290SJonathan Corbet	# If we're in a literal block, see if we should drop out
774af250290SJonathan Corbet	# of it.  Otherwise pass the line straight through unmunged.
775af250290SJonathan Corbet	#
776af250290SJonathan Corbet	if ($in_literal) {
777af250290SJonathan Corbet	    if (! ($line =~ /^\s*$/)) {
778af250290SJonathan Corbet		#
779af250290SJonathan Corbet		# If this is the first non-blank line in a literal
780af250290SJonathan Corbet		# block we need to figure out what the proper indent is.
781af250290SJonathan Corbet		#
782af250290SJonathan Corbet		if ($litprefix eq "") {
783af250290SJonathan Corbet		    $line =~ /^(\s*)/;
784af250290SJonathan Corbet		    $litprefix = '^' . $1;
785af250290SJonathan Corbet		    $output .= $line . "\n";
786af250290SJonathan Corbet		} elsif (! ($line =~ /$litprefix/)) {
787af250290SJonathan Corbet		    $in_literal = 0;
788af250290SJonathan Corbet		} else {
789af250290SJonathan Corbet		    $output .= $line . "\n";
790af250290SJonathan Corbet		}
791af250290SJonathan Corbet	    } else {
792af250290SJonathan Corbet		$output .= $line . "\n";
793af250290SJonathan Corbet	    }
794af250290SJonathan Corbet	}
795af250290SJonathan Corbet	#
796af250290SJonathan Corbet	# Not in a literal block (or just dropped out)
797af250290SJonathan Corbet	#
798af250290SJonathan Corbet	if (! $in_literal) {
799af250290SJonathan Corbet	    $block .= $line . "\n";
800af250290SJonathan Corbet	    if (($line =~ /$sphinx_literal/) || ($line =~ /$sphinx_cblock/)) {
801af250290SJonathan Corbet		$in_literal = 1;
802af250290SJonathan Corbet		$litprefix = "";
803af250290SJonathan Corbet		$output .= highlight_block($block);
804af250290SJonathan Corbet		$block = ""
805af250290SJonathan Corbet	    }
806af250290SJonathan Corbet	}
807af250290SJonathan Corbet    }
808af250290SJonathan Corbet
809af250290SJonathan Corbet    if ($block) {
810af250290SJonathan Corbet	$output .= highlight_block($block);
811af250290SJonathan Corbet    }
812af250290SJonathan Corbet    foreach $line (split "\n", $output) {
813830066a7SJani Nikula	print $lineprefix . $line . "\n";
814c0d1b6eeSJonathan Corbet    }
815c0d1b6eeSJonathan Corbet}
816c0d1b6eeSJonathan Corbet
817c0d1b6eeSJonathan Corbetsub output_function_rst(%) {
818c0d1b6eeSJonathan Corbet    my %args = %{$_[0]};
819c0d1b6eeSJonathan Corbet    my ($parameter, $section);
820c099ff69SJani Nikula    my $oldprefix = $lineprefix;
82182801d06SMauro Carvalho Chehab    my $start = "";
8226e9e4158SMauro Carvalho Chehab    my $is_macro = 0;
823c0d1b6eeSJonathan Corbet
824efa44475SMauro Carvalho Chehab    if ($sphinx_major < 3) {
825e3ad05feSMauro Carvalho Chehab	if ($args{'typedef'}) {
82682801d06SMauro Carvalho Chehab	    print ".. c:type:: ". $args{'function'} . "\n\n";
82782801d06SMauro Carvalho Chehab	    print_lineno($declaration_start_line);
82882801d06SMauro Carvalho Chehab	    print "   **Typedef**: ";
82982801d06SMauro Carvalho Chehab	    $lineprefix = "";
83082801d06SMauro Carvalho Chehab	    output_highlight_rst($args{'purpose'});
83182801d06SMauro Carvalho Chehab	    $start = "\n\n**Syntax**\n\n  ``";
8326e9e4158SMauro Carvalho Chehab	    $is_macro = 1;
833c0d1b6eeSJonathan Corbet	} else {
83482801d06SMauro Carvalho Chehab	    print ".. c:function:: ";
83582801d06SMauro Carvalho Chehab	}
836e3ad05feSMauro Carvalho Chehab    } else {
8376e9e4158SMauro Carvalho Chehab	if ($args{'typedef'} || $args{'functiontype'} eq "") {
8386e9e4158SMauro Carvalho Chehab	    $is_macro = 1;
839e3ad05feSMauro Carvalho Chehab	    print ".. c:macro:: ". $args{'function'} . "\n\n";
8406e9e4158SMauro Carvalho Chehab	} else {
8416e9e4158SMauro Carvalho Chehab	    print ".. c:function:: ";
8426e9e4158SMauro Carvalho Chehab	}
843e3ad05feSMauro Carvalho Chehab
844e3ad05feSMauro Carvalho Chehab	if ($args{'typedef'}) {
845e3ad05feSMauro Carvalho Chehab	    print_lineno($declaration_start_line);
846e3ad05feSMauro Carvalho Chehab	    print "   **Typedef**: ";
847e3ad05feSMauro Carvalho Chehab	    $lineprefix = "";
848e3ad05feSMauro Carvalho Chehab	    output_highlight_rst($args{'purpose'});
849e3ad05feSMauro Carvalho Chehab	    $start = "\n\n**Syntax**\n\n  ``";
850e3ad05feSMauro Carvalho Chehab	} else {
8516e9e4158SMauro Carvalho Chehab	    print "``" if ($is_macro);
852e3ad05feSMauro Carvalho Chehab	}
853e3ad05feSMauro Carvalho Chehab    }
85482801d06SMauro Carvalho Chehab    if ($args{'functiontype'} ne "") {
85582801d06SMauro Carvalho Chehab	$start .= $args{'functiontype'} . " " . $args{'function'} . " (";
85682801d06SMauro Carvalho Chehab    } else {
85782801d06SMauro Carvalho Chehab	$start .= $args{'function'} . " (";
858c0d1b6eeSJonathan Corbet    }
859c0d1b6eeSJonathan Corbet    print $start;
860c0d1b6eeSJonathan Corbet
861c0d1b6eeSJonathan Corbet    my $count = 0;
862c0d1b6eeSJonathan Corbet    foreach my $parameter (@{$args{'parameterlist'}}) {
863c0d1b6eeSJonathan Corbet	if ($count ne 0) {
864c0d1b6eeSJonathan Corbet	    print ", ";
865c0d1b6eeSJonathan Corbet	}
866c0d1b6eeSJonathan Corbet	$count++;
867c0d1b6eeSJonathan Corbet	$type = $args{'parametertypes'}{$parameter};
868a88b1672SMauro Carvalho Chehab
869e86bdb24SAditya Srivastava	if ($type =~ m/$function_pointer/) {
870c0d1b6eeSJonathan Corbet	    # pointer-to-function
871e8f4ba83SPeter Maydell	    print $1 . $parameter . ") (" . $2 . ")";
872c0d1b6eeSJonathan Corbet	} else {
873ed8348e2SMauro Carvalho Chehab	    print $type;
874c0d1b6eeSJonathan Corbet	}
875c0d1b6eeSJonathan Corbet    }
8766e9e4158SMauro Carvalho Chehab    if ($is_macro) {
8776e9e4158SMauro Carvalho Chehab	print ")``\n\n";
87882801d06SMauro Carvalho Chehab    } else {
879c099ff69SJani Nikula	print ")\n\n";
880e3ad05feSMauro Carvalho Chehab    }
8816e9e4158SMauro Carvalho Chehab    if (!$args{'typedef'}) {
8820b0f5f29SDaniel Vetter	print_lineno($declaration_start_line);
883c099ff69SJani Nikula	$lineprefix = "   ";
884c099ff69SJani Nikula	output_highlight_rst($args{'purpose'});
885c099ff69SJani Nikula	print "\n";
88682801d06SMauro Carvalho Chehab    }
887c0d1b6eeSJonathan Corbet
888eaf710ceSJonathan Corbet    #
889eaf710ceSJonathan Corbet    # Put our descriptive text into a container (thus an HTML <div>) to help
890eaf710ceSJonathan Corbet    # set the function prototypes apart.
891eaf710ceSJonathan Corbet    #
892eaf710ceSJonathan Corbet    print ".. container:: kernelindent\n\n";
893c099ff69SJani Nikula    $lineprefix = "  ";
894eaf710ceSJonathan Corbet    print $lineprefix . "**Parameters**\n\n";
895c0d1b6eeSJonathan Corbet    foreach $parameter (@{$args{'parameterlist'}}) {
896c0d1b6eeSJonathan Corbet	my $parameter_name = $parameter;
897ada5f446SGabriel Krisman Bertazi	$parameter_name =~ s/\[.*//;
898c0d1b6eeSJonathan Corbet	$type = $args{'parametertypes'}{$parameter};
899c0d1b6eeSJonathan Corbet
900c0d1b6eeSJonathan Corbet	if ($type ne "") {
901eaf710ceSJonathan Corbet	    print $lineprefix . "``$type``\n";
902c0d1b6eeSJonathan Corbet	} else {
903eaf710ceSJonathan Corbet	    print $lineprefix . "``$parameter``\n";
904c0d1b6eeSJonathan Corbet	}
9050b0f5f29SDaniel Vetter
9060b0f5f29SDaniel Vetter        print_lineno($parameterdesc_start_lines{$parameter_name});
9070b0f5f29SDaniel Vetter
908eaf710ceSJonathan Corbet	$lineprefix = "    ";
9095e64fa9cSJani Nikula	if (defined($args{'parameterdescs'}{$parameter_name}) &&
9105e64fa9cSJani Nikula	    $args{'parameterdescs'}{$parameter_name} ne $undescribed) {
911c0d1b6eeSJonathan Corbet	    output_highlight_rst($args{'parameterdescs'}{$parameter_name});
912c0d1b6eeSJonathan Corbet	} else {
913eaf710ceSJonathan Corbet	    print $lineprefix . "*undescribed*\n";
914c0d1b6eeSJonathan Corbet	}
915eaf710ceSJonathan Corbet	$lineprefix = "  ";
916c0d1b6eeSJonathan Corbet	print "\n";
917c0d1b6eeSJonathan Corbet    }
918c099ff69SJani Nikula
919c0d1b6eeSJonathan Corbet    output_section_rst(@_);
920eaf710ceSJonathan Corbet    $lineprefix = $oldprefix;
921c0d1b6eeSJonathan Corbet}
922c0d1b6eeSJonathan Corbet
923c0d1b6eeSJonathan Corbetsub output_section_rst(%) {
924c0d1b6eeSJonathan Corbet    my %args = %{$_[0]};
925c0d1b6eeSJonathan Corbet    my $section;
926c0d1b6eeSJonathan Corbet    my $oldprefix = $lineprefix;
927c0d1b6eeSJonathan Corbet
928c0d1b6eeSJonathan Corbet    foreach $section (@{$args{'sectionlist'}}) {
929eaf710ceSJonathan Corbet	print $lineprefix . "**$section**\n\n";
9300b0f5f29SDaniel Vetter        print_lineno($section_start_lines{$section});
931c0d1b6eeSJonathan Corbet	output_highlight_rst($args{'sections'}{$section});
932c0d1b6eeSJonathan Corbet	print "\n";
933c0d1b6eeSJonathan Corbet    }
934c0d1b6eeSJonathan Corbet    print "\n";
935c0d1b6eeSJonathan Corbet}
936c0d1b6eeSJonathan Corbet
937c0d1b6eeSJonathan Corbetsub output_enum_rst(%) {
938c0d1b6eeSJonathan Corbet    my %args = %{$_[0]};
939c0d1b6eeSJonathan Corbet    my ($parameter);
940c099ff69SJani Nikula    my $oldprefix = $lineprefix;
941c0d1b6eeSJonathan Corbet    my $count;
942eaf710ceSJonathan Corbet    my $outer;
94362850976SJani Nikula
944efa44475SMauro Carvalho Chehab    if ($sphinx_major < 3) {
945efa44475SMauro Carvalho Chehab	my $name = "enum " . $args{'enum'};
94662850976SJani Nikula	print "\n\n.. c:type:: " . $name . "\n\n";
947efa44475SMauro Carvalho Chehab    } else {
948efa44475SMauro Carvalho Chehab	my $name = $args{'enum'};
949efa44475SMauro Carvalho Chehab	print "\n\n.. c:enum:: " . $name . "\n\n";
950efa44475SMauro Carvalho Chehab    }
9510b0f5f29SDaniel Vetter    print_lineno($declaration_start_line);
952c099ff69SJani Nikula    $lineprefix = "  ";
953c099ff69SJani Nikula    output_highlight_rst($args{'purpose'});
954c099ff69SJani Nikula    print "\n";
955c0d1b6eeSJonathan Corbet
956eaf710ceSJonathan Corbet    print ".. container:: kernelindent\n\n";
957eaf710ceSJonathan Corbet    $outer = $lineprefix . "  ";
958eaf710ceSJonathan Corbet    $lineprefix = $outer . "  ";
959eaf710ceSJonathan Corbet    print $outer . "**Constants**\n\n";
960c0d1b6eeSJonathan Corbet    foreach $parameter (@{$args{'parameterlist'}}) {
961eaf710ceSJonathan Corbet	print $outer . "``$parameter``\n";
962eaf710ceSJonathan Corbet
963c0d1b6eeSJonathan Corbet	if ($args{'parameterdescs'}{$parameter} ne $undescribed) {
964c0d1b6eeSJonathan Corbet	    output_highlight_rst($args{'parameterdescs'}{$parameter});
965c0d1b6eeSJonathan Corbet	} else {
966eaf710ceSJonathan Corbet	    print $lineprefix . "*undescribed*\n";
967c0d1b6eeSJonathan Corbet	}
968c0d1b6eeSJonathan Corbet	print "\n";
969c0d1b6eeSJonathan Corbet    }
970eaf710ceSJonathan Corbet    print "\n";
971c0d1b6eeSJonathan Corbet    $lineprefix = $oldprefix;
972c0d1b6eeSJonathan Corbet    output_section_rst(@_);
973c0d1b6eeSJonathan Corbet}
974c0d1b6eeSJonathan Corbet
975c0d1b6eeSJonathan Corbetsub output_typedef_rst(%) {
976c0d1b6eeSJonathan Corbet    my %args = %{$_[0]};
977c0d1b6eeSJonathan Corbet    my ($parameter);
978c099ff69SJani Nikula    my $oldprefix = $lineprefix;
979efa44475SMauro Carvalho Chehab    my $name;
980c0d1b6eeSJonathan Corbet
981efa44475SMauro Carvalho Chehab    if ($sphinx_major < 3) {
982efa44475SMauro Carvalho Chehab	$name = "typedef " . $args{'typedef'};
983efa44475SMauro Carvalho Chehab    } else {
984efa44475SMauro Carvalho Chehab	$name = $args{'typedef'};
985efa44475SMauro Carvalho Chehab    }
98662850976SJani Nikula    print "\n\n.. c:type:: " . $name . "\n\n";
9870b0f5f29SDaniel Vetter    print_lineno($declaration_start_line);
988c099ff69SJani Nikula    $lineprefix = "   ";
989c099ff69SJani Nikula    output_highlight_rst($args{'purpose'});
990c099ff69SJani Nikula    print "\n";
991c0d1b6eeSJonathan Corbet
992c099ff69SJani Nikula    $lineprefix = $oldprefix;
993c0d1b6eeSJonathan Corbet    output_section_rst(@_);
994c0d1b6eeSJonathan Corbet}
995c0d1b6eeSJonathan Corbet
996c0d1b6eeSJonathan Corbetsub output_struct_rst(%) {
997c0d1b6eeSJonathan Corbet    my %args = %{$_[0]};
998c0d1b6eeSJonathan Corbet    my ($parameter);
999c099ff69SJani Nikula    my $oldprefix = $lineprefix;
1000c0d1b6eeSJonathan Corbet
1001efa44475SMauro Carvalho Chehab    if ($sphinx_major < 3) {
1002efa44475SMauro Carvalho Chehab	my $name = $args{'type'} . " " . $args{'struct'};
100362850976SJani Nikula	print "\n\n.. c:type:: " . $name . "\n\n";
1004efa44475SMauro Carvalho Chehab    } else {
1005efa44475SMauro Carvalho Chehab	my $name = $args{'struct'};
100672b97d0bSMauro Carvalho Chehab	if ($args{'type'} eq 'union') {
100772b97d0bSMauro Carvalho Chehab	    print "\n\n.. c:union:: " . $name . "\n\n";
100872b97d0bSMauro Carvalho Chehab	} else {
1009efa44475SMauro Carvalho Chehab	    print "\n\n.. c:struct:: " . $name . "\n\n";
1010efa44475SMauro Carvalho Chehab	}
101172b97d0bSMauro Carvalho Chehab    }
10120b0f5f29SDaniel Vetter    print_lineno($declaration_start_line);
1013c099ff69SJani Nikula    $lineprefix = "  ";
1014c099ff69SJani Nikula    output_highlight_rst($args{'purpose'});
1015c099ff69SJani Nikula    print "\n";
1016c0d1b6eeSJonathan Corbet
1017eaf710ceSJonathan Corbet    print ".. container:: kernelindent\n\n";
1018eaf710ceSJonathan Corbet    print $lineprefix . "**Definition**::\n\n";
10198ad72163SMauro Carvalho Chehab    my $declaration = $args{'definition'};
1020eaf710ceSJonathan Corbet    $lineprefix = $lineprefix . "  ";
1021eaf710ceSJonathan Corbet    $declaration =~ s/\t/$lineprefix/g;
1022eaf710ceSJonathan Corbet    print $lineprefix . $args{'type'} . " " . $args{'struct'} . " {\n$declaration" . $lineprefix . "};\n\n";
1023c0d1b6eeSJonathan Corbet
1024c099ff69SJani Nikula    $lineprefix = "  ";
1025eaf710ceSJonathan Corbet    print $lineprefix . "**Members**\n\n";
1026c0d1b6eeSJonathan Corbet    foreach $parameter (@{$args{'parameterlist'}}) {
1027c0d1b6eeSJonathan Corbet	($parameter =~ /^#/) && next;
1028c0d1b6eeSJonathan Corbet
1029c0d1b6eeSJonathan Corbet	my $parameter_name = $parameter;
1030c0d1b6eeSJonathan Corbet	$parameter_name =~ s/\[.*//;
1031c0d1b6eeSJonathan Corbet
1032c0d1b6eeSJonathan Corbet	($args{'parameterdescs'}{$parameter_name} ne $undescribed) || next;
1033c0d1b6eeSJonathan Corbet	$type = $args{'parametertypes'}{$parameter};
10340b0f5f29SDaniel Vetter        print_lineno($parameterdesc_start_lines{$parameter_name});
1035eaf710ceSJonathan Corbet	print $lineprefix . "``" . $parameter . "``\n";
1036eaf710ceSJonathan Corbet	$lineprefix = "    ";
1037c0d1b6eeSJonathan Corbet	output_highlight_rst($args{'parameterdescs'}{$parameter_name});
1038eaf710ceSJonathan Corbet	$lineprefix = "  ";
1039c0d1b6eeSJonathan Corbet	print "\n";
1040c0d1b6eeSJonathan Corbet    }
1041c0d1b6eeSJonathan Corbet    print "\n";
1042c099ff69SJani Nikula
1043c099ff69SJani Nikula    $lineprefix = $oldprefix;
1044c0d1b6eeSJonathan Corbet    output_section_rst(@_);
1045c0d1b6eeSJonathan Corbet}
1046c0d1b6eeSJonathan Corbet
10473a025e1dSMatthew Wilcox## none mode output functions
10483a025e1dSMatthew Wilcox
10493a025e1dSMatthew Wilcoxsub output_function_none(%) {
10503a025e1dSMatthew Wilcox}
10513a025e1dSMatthew Wilcox
10523a025e1dSMatthew Wilcoxsub output_enum_none(%) {
10533a025e1dSMatthew Wilcox}
10543a025e1dSMatthew Wilcox
10553a025e1dSMatthew Wilcoxsub output_typedef_none(%) {
10563a025e1dSMatthew Wilcox}
10573a025e1dSMatthew Wilcox
10583a025e1dSMatthew Wilcoxsub output_struct_none(%) {
10593a025e1dSMatthew Wilcox}
10603a025e1dSMatthew Wilcox
10613a025e1dSMatthew Wilcoxsub output_blockhead_none(%) {
10623a025e1dSMatthew Wilcox}
10633a025e1dSMatthew Wilcox
10641da177e4SLinus Torvalds##
106527205744SRandy Dunlap# generic output function for all types (function, struct/union, typedef, enum);
106627205744SRandy Dunlap# calls the generated, variable output_ function name based on
106727205744SRandy Dunlap# functype and output_mode
10681da177e4SLinus Torvaldssub output_declaration {
10691da177e4SLinus Torvalds    no strict 'refs';
10701da177e4SLinus Torvalds    my $name = shift;
10711da177e4SLinus Torvalds    my $functype = shift;
10721da177e4SLinus Torvalds    my $func = "output_${functype}_$output_mode";
1073eab795ddSMauro Carvalho Chehab
1074eab795ddSMauro Carvalho Chehab    return if (defined($nosymbol_table{$name}));
1075eab795ddSMauro Carvalho Chehab
1076b6c3f456SJani Nikula    if (($output_selection == OUTPUT_ALL) ||
1077b6c3f456SJani Nikula	(($output_selection == OUTPUT_INCLUDE ||
1078b6c3f456SJani Nikula	  $output_selection == OUTPUT_EXPORTED) &&
107986ae2e38SJani Nikula	 defined($function_table{$name})) ||
1080eab795ddSMauro Carvalho Chehab	($output_selection == OUTPUT_INTERNAL &&
108186ae2e38SJani Nikula	 !($functype eq "function" && defined($function_table{$name}))))
10821da177e4SLinus Torvalds    {
10831da177e4SLinus Torvalds	&$func(@_);
10841da177e4SLinus Torvalds	$section_counter++;
10851da177e4SLinus Torvalds    }
10861da177e4SLinus Torvalds}
10871da177e4SLinus Torvalds
10881da177e4SLinus Torvalds##
108927205744SRandy Dunlap# generic output function - calls the right one based on current output mode.
1090b112e0f7SJohannes Bergsub output_blockhead {
10911da177e4SLinus Torvalds    no strict 'refs';
1092b112e0f7SJohannes Berg    my $func = "output_blockhead_" . $output_mode;
10931da177e4SLinus Torvalds    &$func(@_);
10941da177e4SLinus Torvalds    $section_counter++;
10951da177e4SLinus Torvalds}
10961da177e4SLinus Torvalds
10971da177e4SLinus Torvalds##
10981da177e4SLinus Torvalds# takes a declaration (struct, union, enum, typedef) and
10991da177e4SLinus Torvalds# invokes the right handler. NOT called for functions.
11001da177e4SLinus Torvaldssub dump_declaration($$) {
11011da177e4SLinus Torvalds    no strict 'refs';
11021da177e4SLinus Torvalds    my ($prototype, $file) = @_;
11031da177e4SLinus Torvalds    my $func = "dump_" . $decl_type;
11041da177e4SLinus Torvalds    &$func(@_);
11051da177e4SLinus Torvalds}
11061da177e4SLinus Torvalds
11071da177e4SLinus Torvaldssub dump_union($$) {
11081da177e4SLinus Torvalds    dump_struct(@_);
11091da177e4SLinus Torvalds}
11101da177e4SLinus Torvalds
11111da177e4SLinus Torvaldssub dump_struct($$) {
11121da177e4SLinus Torvalds    my $x = shift;
11131da177e4SLinus Torvalds    my $file = shift;
1114a746fe32SAditya Srivastava    my $decl_type;
1115a746fe32SAditya Srivastava    my $members;
1116a746fe32SAditya Srivastava    my $type = qr{struct|union};
1117a746fe32SAditya Srivastava    # For capturing struct/union definition body, i.e. "{members*}qualifiers*"
1118e86bdb24SAditya Srivastava    my $qualifiers = qr{$attribute|__packed|__aligned|____cacheline_aligned_in_smp|____cacheline_aligned};
1119e86bdb24SAditya Srivastava    my $definition_body = qr{\{(.*)\}\s*$qualifiers*};
1120e86bdb24SAditya Srivastava    my $struct_members = qr{($type)([^\{\};]+)\{([^\{\}]*)\}([^\{\}\;]*)\;};
11211da177e4SLinus Torvalds
1122a746fe32SAditya Srivastava    if ($x =~ /($type)\s+(\w+)\s*$definition_body/) {
1123a746fe32SAditya Srivastava	$decl_type = $1;
11241da177e4SLinus Torvalds	$declaration_name = $2;
1125a746fe32SAditya Srivastava	$members = $3;
1126a746fe32SAditya Srivastava    } elsif ($x =~ /typedef\s+($type)\s*$definition_body\s*(\w+)\s*;/) {
1127a746fe32SAditya Srivastava	$decl_type = $1;
1128a746fe32SAditya Srivastava	$declaration_name = $3;
1129a746fe32SAditya Srivastava	$members = $2;
1130a746fe32SAditya Srivastava    }
11311da177e4SLinus Torvalds
1132a746fe32SAditya Srivastava    if ($members) {
113352042e2dSMauro Carvalho Chehab	if ($identifier ne $declaration_name) {
1134df4bf98eSNiklas Söderlund	    emit_warning("${file}:$.", "expecting prototype for $decl_type $identifier. Prototype was for $decl_type $declaration_name instead\n");
113552042e2dSMauro Carvalho Chehab	    return;
113652042e2dSMauro Carvalho Chehab	}
113752042e2dSMauro Carvalho Chehab
1138aeec46b9SMartin Waitz	# ignore members marked private:
11390d8c39e6SMauro Carvalho Chehab	$members =~ s/\/\*\s*private:.*?\/\*\s*public:.*?\*\///gosi;
11400d8c39e6SMauro Carvalho Chehab	$members =~ s/\/\*\s*private:.*//gosi;
1141aeec46b9SMartin Waitz	# strip comments:
1142aeec46b9SMartin Waitz	$members =~ s/\/\*.*?\*\///gos;
1143ef5da59fSRandy Dunlap	# strip attributes
1144e86bdb24SAditya Srivastava	$members =~ s/\s*$attribute/ /gi;
11453d9bfb19SSakari Ailus	$members =~ s/\s*__aligned\s*\([^;]*\)/ /gos;
11463d9bfb19SSakari Ailus	$members =~ s/\s*__packed\s*/ /gos;
1147f0074929SJonathan Corbet	$members =~ s/\s*CRYPTO_MINALIGN_ATTR/ /gos;
1148f861537dSAndré Almeida	$members =~ s/\s*____cacheline_aligned_in_smp/ /gos;
1149a070991fSJonathan Cameron	$members =~ s/\s*____cacheline_aligned/ /gos;
115050d7bd38SKees Cook	# unwrap struct_group():
115150d7bd38SKees Cook	# - first eat non-declaration parameters and rewrite for final match
115250d7bd38SKees Cook	# - then remove macro, outer parens, and trailing semicolon
115350d7bd38SKees Cook	$members =~ s/\bstruct_group\s*\(([^,]*,)/STRUCT_GROUP(/gos;
115450d7bd38SKees Cook	$members =~ s/\bstruct_group_(attr|tagged)\s*\(([^,]*,){2}/STRUCT_GROUP(/gos;
115550d7bd38SKees Cook	$members =~ s/\b__struct_group\s*\(([^,]*,){3}/STRUCT_GROUP(/gos;
115650d7bd38SKees Cook	$members =~ s/\bSTRUCT_GROUP(\(((?:(?>[^)(]+)|(?1))*)\))[^;]*;/$2/gos;
11573556108eSMauro Carvalho Chehab
1158e86bdb24SAditya Srivastava	my $args = qr{([^,)]+)};
1159b22b5a9eSConchúr Navid	# replace DECLARE_BITMAP
11603556108eSMauro Carvalho Chehab	$members =~ s/__ETHTOOL_DECLARE_LINK_MODE_MASK\s*\(([^\)]+)\)/DECLARE_BITMAP($1, __ETHTOOL_LINK_MODE_MASK_NBITS)/gos;
1161603bdf5dSRandy Dunlap	$members =~ s/DECLARE_PHY_INTERFACE_MASK\s*\(([^\)]+)\)/DECLARE_BITMAP($1, PHY_INTERFACE_MODE_MAX)/gos;
1162e86bdb24SAditya Srivastava	$members =~ s/DECLARE_BITMAP\s*\($args,\s*$args\)/unsigned long $1\[BITS_TO_LONGS($2)\]/gos;
11631cb566baSJakub Kicinski	# replace DECLARE_HASHTABLE
1164e86bdb24SAditya Srivastava	$members =~ s/DECLARE_HASHTABLE\s*\($args,\s*$args\)/unsigned long $1\[1 << (($2) - 1)\]/gos;
116545005b27SMauro Carvalho Chehab	# replace DECLARE_KFIFO
1166e86bdb24SAditya Srivastava	$members =~ s/DECLARE_KFIFO\s*\($args,\s*$args,\s*$args\)/$2 \*$1/gos;
116745005b27SMauro Carvalho Chehab	# replace DECLARE_KFIFO_PTR
1168e86bdb24SAditya Srivastava	$members =~ s/DECLARE_KFIFO_PTR\s*\($args,\s*$args\)/$2 \*$1/gos;
11693080ea55SKees Cook	# replace DECLARE_FLEX_ARRAY
11703080ea55SKees Cook	$members =~ s/(?:__)?DECLARE_FLEX_ARRAY\s*\($args,\s*$args\)/$1 $2\[\]/gos;
11718ad72163SMauro Carvalho Chehab	my $declaration = $members;
11728ad72163SMauro Carvalho Chehab
11738ad72163SMauro Carvalho Chehab	# Split nested struct/union elements as newer ones
1174e86bdb24SAditya Srivastava	while ($members =~ m/$struct_members/) {
117584ce5b98SMauro Carvalho Chehab		my $newmember;
117684ce5b98SMauro Carvalho Chehab		my $maintype = $1;
117784ce5b98SMauro Carvalho Chehab		my $ids = $4;
11788ad72163SMauro Carvalho Chehab		my $content = $3;
117984ce5b98SMauro Carvalho Chehab		foreach my $id(split /,/, $ids) {
118084ce5b98SMauro Carvalho Chehab			$newmember .= "$maintype $id; ";
118184ce5b98SMauro Carvalho Chehab
11828ad72163SMauro Carvalho Chehab			$id =~ s/[:\[].*//;
118384ce5b98SMauro Carvalho Chehab			$id =~ s/^\s*\**(\S+)\s*/$1/;
11848ad72163SMauro Carvalho Chehab			foreach my $arg (split /;/, $content) {
11858ad72163SMauro Carvalho Chehab				next if ($arg =~ m/^\s*$/);
11867c0d7e87SMauro Carvalho Chehab				if ($arg =~ m/^([^\(]+\(\*?\s*)([\w\.]*)(\s*\).*)/) {
11877c0d7e87SMauro Carvalho Chehab					# pointer-to-function
11887c0d7e87SMauro Carvalho Chehab					my $type = $1;
11897c0d7e87SMauro Carvalho Chehab					my $name = $2;
11907c0d7e87SMauro Carvalho Chehab					my $extra = $3;
11917c0d7e87SMauro Carvalho Chehab					next if (!$name);
11927c0d7e87SMauro Carvalho Chehab					if ($id =~ m/^\s*$/) {
11937c0d7e87SMauro Carvalho Chehab						# anonymous struct/union
11947c0d7e87SMauro Carvalho Chehab						$newmember .= "$type$name$extra; ";
11957c0d7e87SMauro Carvalho Chehab					} else {
11967c0d7e87SMauro Carvalho Chehab						$newmember .= "$type$id.$name$extra; ";
11977c0d7e87SMauro Carvalho Chehab					}
11987c0d7e87SMauro Carvalho Chehab				} else {
119984ce5b98SMauro Carvalho Chehab					my $type;
120084ce5b98SMauro Carvalho Chehab					my $names;
120184ce5b98SMauro Carvalho Chehab					$arg =~ s/^\s+//;
120284ce5b98SMauro Carvalho Chehab					$arg =~ s/\s+$//;
120384ce5b98SMauro Carvalho Chehab					# Handle bitmaps
120484ce5b98SMauro Carvalho Chehab					$arg =~ s/:\s*\d+\s*//g;
120584ce5b98SMauro Carvalho Chehab					# Handle arrays
1206d404d579SMauro Carvalho Chehab					$arg =~ s/\[.*\]//g;
120784ce5b98SMauro Carvalho Chehab					# The type may have multiple words,
120884ce5b98SMauro Carvalho Chehab					# and multiple IDs can be defined, like:
120984ce5b98SMauro Carvalho Chehab					#	const struct foo, *bar, foobar
121084ce5b98SMauro Carvalho Chehab					# So, we remove spaces when parsing the
121184ce5b98SMauro Carvalho Chehab					# names, in order to match just names
121284ce5b98SMauro Carvalho Chehab					# and commas for the names
121384ce5b98SMauro Carvalho Chehab					$arg =~ s/\s*,\s*/,/g;
121484ce5b98SMauro Carvalho Chehab					if ($arg =~ m/(.*)\s+([\S+,]+)/) {
121584ce5b98SMauro Carvalho Chehab						$type = $1;
121684ce5b98SMauro Carvalho Chehab						$names = $2;
121784ce5b98SMauro Carvalho Chehab					} else {
121884ce5b98SMauro Carvalho Chehab						$newmember .= "$arg; ";
121984ce5b98SMauro Carvalho Chehab						next;
122084ce5b98SMauro Carvalho Chehab					}
122184ce5b98SMauro Carvalho Chehab					foreach my $name (split /,/, $names) {
122284ce5b98SMauro Carvalho Chehab						$name =~ s/^\s*\**(\S+)\s*/$1/;
12238ad72163SMauro Carvalho Chehab						next if (($name =~ m/^\s*$/));
12248ad72163SMauro Carvalho Chehab						if ($id =~ m/^\s*$/) {
12258ad72163SMauro Carvalho Chehab							# anonymous struct/union
12268ad72163SMauro Carvalho Chehab							$newmember .= "$type $name; ";
12278ad72163SMauro Carvalho Chehab						} else {
12288ad72163SMauro Carvalho Chehab							$newmember .= "$type $id.$name; ";
12298ad72163SMauro Carvalho Chehab						}
12308ad72163SMauro Carvalho Chehab					}
12317c0d7e87SMauro Carvalho Chehab				}
123284ce5b98SMauro Carvalho Chehab			}
123384ce5b98SMauro Carvalho Chehab		}
1234e86bdb24SAditya Srivastava		$members =~ s/$struct_members/$newmember/;
123584ce5b98SMauro Carvalho Chehab	}
12368ad72163SMauro Carvalho Chehab
12378ad72163SMauro Carvalho Chehab	# Ignore other nested elements, like enums
1238673bb2dfSBen Hutchings	$members =~ s/(\{[^\{\}]*\})//g;
12398ad72163SMauro Carvalho Chehab
1240151c468bSMauro Carvalho Chehab	create_parameterlist($members, ';', $file, $declaration_name);
12411081de2dSMauro Carvalho Chehab	check_sections($file, $declaration_name, $decl_type, $sectcheck, $struct_actual);
12421da177e4SLinus Torvalds
12438ad72163SMauro Carvalho Chehab	# Adjust declaration for better display
1244673bb2dfSBen Hutchings	$declaration =~ s/([\{;])/$1\n/g;
1245673bb2dfSBen Hutchings	$declaration =~ s/\}\s+;/};/g;
12468ad72163SMauro Carvalho Chehab	# Better handle inlined enums
1247673bb2dfSBen Hutchings	do {} while ($declaration =~ s/(enum\s+\{[^\}]+),([^\n])/$1,\n$2/);
12488ad72163SMauro Carvalho Chehab
12498ad72163SMauro Carvalho Chehab	my @def_args = split /\n/, $declaration;
12508ad72163SMauro Carvalho Chehab	my $level = 1;
12518ad72163SMauro Carvalho Chehab	$declaration = "";
12528ad72163SMauro Carvalho Chehab	foreach my $clause (@def_args) {
12538ad72163SMauro Carvalho Chehab		$clause =~ s/^\s+//;
12548ad72163SMauro Carvalho Chehab		$clause =~ s/\s+$//;
12558ad72163SMauro Carvalho Chehab		$clause =~ s/\s+/ /;
12568ad72163SMauro Carvalho Chehab		next if (!$clause);
1257673bb2dfSBen Hutchings		$level-- if ($clause =~ m/(\})/ && $level > 1);
12588ad72163SMauro Carvalho Chehab		if (!($clause =~ m/^\s*#/)) {
12598ad72163SMauro Carvalho Chehab			$declaration .= "\t" x $level;
12608ad72163SMauro Carvalho Chehab		}
12618ad72163SMauro Carvalho Chehab		$declaration .= "\t" . $clause . "\n";
1262673bb2dfSBen Hutchings		$level++ if ($clause =~ m/(\{)/ && !($clause =~m/\}/));
12638ad72163SMauro Carvalho Chehab	}
12641da177e4SLinus Torvalds	output_declaration($declaration_name,
12651da177e4SLinus Torvalds			   'struct',
12661da177e4SLinus Torvalds			   {'struct' => $declaration_name,
12671da177e4SLinus Torvalds			    'module' => $modulename,
12688ad72163SMauro Carvalho Chehab			    'definition' => $declaration,
12691da177e4SLinus Torvalds			    'parameterlist' => \@parameterlist,
12701da177e4SLinus Torvalds			    'parameterdescs' => \%parameterdescs,
12711da177e4SLinus Torvalds			    'parametertypes' => \%parametertypes,
12721da177e4SLinus Torvalds			    'sectionlist' => \@sectionlist,
12731da177e4SLinus Torvalds			    'sections' => \%sections,
12741da177e4SLinus Torvalds			    'purpose' => $declaration_purpose,
12751da177e4SLinus Torvalds			    'type' => $decl_type
12761da177e4SLinus Torvalds			   });
12771da177e4SLinus Torvalds    }
12781da177e4SLinus Torvalds    else {
1279d40e1e65SBart Van Assche	print STDERR "${file}:$.: error: Cannot parse struct or union!\n";
12801da177e4SLinus Torvalds	++$errors;
12811da177e4SLinus Torvalds    }
12821da177e4SLinus Torvalds}
12831da177e4SLinus Torvalds
128485afe608SMauro Carvalho Chehab
128585afe608SMauro Carvalho Chehabsub show_warnings($$) {
128685afe608SMauro Carvalho Chehab	my $functype = shift;
128785afe608SMauro Carvalho Chehab	my $name = shift;
128885afe608SMauro Carvalho Chehab
1289eab795ddSMauro Carvalho Chehab	return 0 if (defined($nosymbol_table{$name}));
1290eab795ddSMauro Carvalho Chehab
129185afe608SMauro Carvalho Chehab	return 1 if ($output_selection == OUTPUT_ALL);
129285afe608SMauro Carvalho Chehab
129385afe608SMauro Carvalho Chehab	if ($output_selection == OUTPUT_EXPORTED) {
129485afe608SMauro Carvalho Chehab		if (defined($function_table{$name})) {
129585afe608SMauro Carvalho Chehab			return 1;
129685afe608SMauro Carvalho Chehab		} else {
129785afe608SMauro Carvalho Chehab			return 0;
129885afe608SMauro Carvalho Chehab		}
129985afe608SMauro Carvalho Chehab	}
130085afe608SMauro Carvalho Chehab        if ($output_selection == OUTPUT_INTERNAL) {
130185afe608SMauro Carvalho Chehab		if (!($functype eq "function" && defined($function_table{$name}))) {
130285afe608SMauro Carvalho Chehab			return 1;
130385afe608SMauro Carvalho Chehab		} else {
130485afe608SMauro Carvalho Chehab			return 0;
130585afe608SMauro Carvalho Chehab		}
130685afe608SMauro Carvalho Chehab	}
130785afe608SMauro Carvalho Chehab	if ($output_selection == OUTPUT_INCLUDE) {
130885afe608SMauro Carvalho Chehab		if (defined($function_table{$name})) {
130985afe608SMauro Carvalho Chehab			return 1;
131085afe608SMauro Carvalho Chehab		} else {
131185afe608SMauro Carvalho Chehab			return 0;
131285afe608SMauro Carvalho Chehab		}
131385afe608SMauro Carvalho Chehab	}
131485afe608SMauro Carvalho Chehab	die("Please add the new output type at show_warnings()");
131585afe608SMauro Carvalho Chehab}
131685afe608SMauro Carvalho Chehab
13171da177e4SLinus Torvaldssub dump_enum($$) {
13181da177e4SLinus Torvalds    my $x = shift;
13191da177e4SLinus Torvalds    my $file = shift;
1320d38c8cfbSMauro Carvalho Chehab    my $members;
1321d38c8cfbSMauro Carvalho Chehab
13221da177e4SLinus Torvalds
1323aeec46b9SMartin Waitz    $x =~ s@/\*.*?\*/@@gos;	# strip comments.
13244468e21eSConchúr Navid    # strip #define macros inside enums
13254468e21eSConchúr Navid    $x =~ s@#\s*((define|ifdef)\s+|endif)[^;]*;@@gos;
1326b6d676dbSRandy Dunlap
1327d38c8cfbSMauro Carvalho Chehab    if ($x =~ /typedef\s+enum\s*\{(.*)\}\s*(\w*)\s*;/) {
1328d38c8cfbSMauro Carvalho Chehab	$declaration_name = $2;
1329d38c8cfbSMauro Carvalho Chehab	$members = $1;
1330d38c8cfbSMauro Carvalho Chehab    } elsif ($x =~ /enum\s+(\w*)\s*\{(.*)\}/) {
13311da177e4SLinus Torvalds	$declaration_name = $1;
1332d38c8cfbSMauro Carvalho Chehab	$members = $2;
1333d38c8cfbSMauro Carvalho Chehab    }
1334d38c8cfbSMauro Carvalho Chehab
1335ae5b17e4SAndy Shevchenko    if ($members) {
133652042e2dSMauro Carvalho Chehab	if ($identifier ne $declaration_name) {
13370b54c2e3SMauro Carvalho Chehab	    if ($identifier eq "") {
1338df4bf98eSNiklas Söderlund		emit_warning("${file}:$.", "wrong kernel-doc identifier on line:\n");
13390b54c2e3SMauro Carvalho Chehab	    } else {
1340df4bf98eSNiklas Söderlund		emit_warning("${file}:$.", "expecting prototype for enum $identifier. Prototype was for enum $declaration_name instead\n");
13410b54c2e3SMauro Carvalho Chehab	    }
134252042e2dSMauro Carvalho Chehab	    return;
134352042e2dSMauro Carvalho Chehab	}
13440b54c2e3SMauro Carvalho Chehab	$declaration_name = "(anonymous)" if ($declaration_name eq "");
134552042e2dSMauro Carvalho Chehab
13465cb5c31cSJohannes Berg	my %_members;
13475cb5c31cSJohannes Berg
1348463a0fdcSMarkus Heiser	$members =~ s/\s+$//;
13491da177e4SLinus Torvalds
13501da177e4SLinus Torvalds	foreach my $arg (split ',', $members) {
13511da177e4SLinus Torvalds	    $arg =~ s/^\s*(\w+).*/$1/;
13521da177e4SLinus Torvalds	    push @parameterlist, $arg;
13531da177e4SLinus Torvalds	    if (!$parameterdescs{$arg}) {
13541da177e4SLinus Torvalds		$parameterdescs{$arg} = $undescribed;
135585afe608SMauro Carvalho Chehab	        if (show_warnings("enum", $declaration_name)) {
1356df4bf98eSNiklas Söderlund			emit_warning("${file}:$.", "Enum value '$arg' not described in enum '$declaration_name'\n");
13572defb272SMauro Carvalho Chehab		}
13581da177e4SLinus Torvalds	    }
13595cb5c31cSJohannes Berg	    $_members{$arg} = 1;
13605cb5c31cSJohannes Berg	}
13611da177e4SLinus Torvalds
13625cb5c31cSJohannes Berg	while (my ($k, $v) = each %parameterdescs) {
13635cb5c31cSJohannes Berg	    if (!exists($_members{$k})) {
136485afe608SMauro Carvalho Chehab	        if (show_warnings("enum", $declaration_name)) {
1365df4bf98eSNiklas Söderlund		     emit_warning("${file}:$.", "Excess enum value '$k' description in '$declaration_name'\n");
13662defb272SMauro Carvalho Chehab		}
13675cb5c31cSJohannes Berg	    }
13681da177e4SLinus Torvalds        }
13691da177e4SLinus Torvalds
13701da177e4SLinus Torvalds	output_declaration($declaration_name,
13711da177e4SLinus Torvalds			   'enum',
13721da177e4SLinus Torvalds			   {'enum' => $declaration_name,
13731da177e4SLinus Torvalds			    'module' => $modulename,
13741da177e4SLinus Torvalds			    'parameterlist' => \@parameterlist,
13751da177e4SLinus Torvalds			    'parameterdescs' => \%parameterdescs,
13761da177e4SLinus Torvalds			    'sectionlist' => \@sectionlist,
13771da177e4SLinus Torvalds			    'sections' => \%sections,
13781da177e4SLinus Torvalds			    'purpose' => $declaration_purpose
13791da177e4SLinus Torvalds			   });
1380d38c8cfbSMauro Carvalho Chehab    } else {
1381d40e1e65SBart Van Assche	print STDERR "${file}:$.: error: Cannot parse enum!\n";
13821da177e4SLinus Torvalds	++$errors;
13831da177e4SLinus Torvalds    }
13841da177e4SLinus Torvalds}
13851da177e4SLinus Torvalds
13867d2c6b1eSMauro Carvalho Chehabmy $typedef_type = qr { ((?:\s+[\w\*]+\b){1,8})\s* }x;
13877efc6c42SMauro Carvalho Chehabmy $typedef_ident = qr { \*?\s*(\w\S+)\s* }x;
13887efc6c42SMauro Carvalho Chehabmy $typedef_args = qr { \s*\((.*)\); }x;
13897efc6c42SMauro Carvalho Chehab
13907efc6c42SMauro Carvalho Chehabmy $typedef1 = qr { typedef$typedef_type\($typedef_ident\)$typedef_args }x;
13917efc6c42SMauro Carvalho Chehabmy $typedef2 = qr { typedef$typedef_type$typedef_ident$typedef_args }x;
13927efc6c42SMauro Carvalho Chehab
13931da177e4SLinus Torvaldssub dump_typedef($$) {
13941da177e4SLinus Torvalds    my $x = shift;
13951da177e4SLinus Torvalds    my $file = shift;
13961da177e4SLinus Torvalds
1397aeec46b9SMartin Waitz    $x =~ s@/\*.*?\*/@@gos;	# strip comments.
139883766452SMauro Carvalho Chehab
13997efc6c42SMauro Carvalho Chehab    # Parse function typedef prototypes
14007efc6c42SMauro Carvalho Chehab    if ($x =~ $typedef1 || $x =~ $typedef2) {
140183766452SMauro Carvalho Chehab	$return_type = $1;
140283766452SMauro Carvalho Chehab	$declaration_name = $2;
140383766452SMauro Carvalho Chehab	my $args = $3;
14046b80975cSMauro Carvalho Chehab	$return_type =~ s/^\s+//;
140583766452SMauro Carvalho Chehab
140652042e2dSMauro Carvalho Chehab	if ($identifier ne $declaration_name) {
1407df4bf98eSNiklas Söderlund	    emit_warning("${file}:$.", "expecting prototype for typedef $identifier. Prototype was for typedef $declaration_name instead\n");
140852042e2dSMauro Carvalho Chehab	    return;
140952042e2dSMauro Carvalho Chehab	}
141052042e2dSMauro Carvalho Chehab
1411151c468bSMauro Carvalho Chehab	create_parameterlist($args, ',', $file, $declaration_name);
141283766452SMauro Carvalho Chehab
141383766452SMauro Carvalho Chehab	output_declaration($declaration_name,
141483766452SMauro Carvalho Chehab			   'function',
141583766452SMauro Carvalho Chehab			   {'function' => $declaration_name,
141682801d06SMauro Carvalho Chehab			    'typedef' => 1,
141783766452SMauro Carvalho Chehab			    'module' => $modulename,
141883766452SMauro Carvalho Chehab			    'functiontype' => $return_type,
141983766452SMauro Carvalho Chehab			    'parameterlist' => \@parameterlist,
142083766452SMauro Carvalho Chehab			    'parameterdescs' => \%parameterdescs,
142183766452SMauro Carvalho Chehab			    'parametertypes' => \%parametertypes,
142283766452SMauro Carvalho Chehab			    'sectionlist' => \@sectionlist,
142383766452SMauro Carvalho Chehab			    'sections' => \%sections,
142483766452SMauro Carvalho Chehab			    'purpose' => $declaration_purpose
142583766452SMauro Carvalho Chehab			   });
142683766452SMauro Carvalho Chehab	return;
142783766452SMauro Carvalho Chehab    }
142883766452SMauro Carvalho Chehab
14291da177e4SLinus Torvalds    while (($x =~ /\(*.\)\s*;$/) || ($x =~ /\[*.\]\s*;$/)) {
14301da177e4SLinus Torvalds	$x =~ s/\(*.\)\s*;$/;/;
14311da177e4SLinus Torvalds	$x =~ s/\[*.\]\s*;$/;/;
14321da177e4SLinus Torvalds    }
14331da177e4SLinus Torvalds
14341da177e4SLinus Torvalds    if ($x =~ /typedef.*\s+(\w+)\s*;/) {
14351da177e4SLinus Torvalds	$declaration_name = $1;
14361da177e4SLinus Torvalds
143752042e2dSMauro Carvalho Chehab	if ($identifier ne $declaration_name) {
1438df4bf98eSNiklas Söderlund	    emit_warning("${file}:$.", "expecting prototype for typedef $identifier. Prototype was for typedef $declaration_name instead\n");
143952042e2dSMauro Carvalho Chehab	    return;
144052042e2dSMauro Carvalho Chehab	}
144152042e2dSMauro Carvalho Chehab
14421da177e4SLinus Torvalds	output_declaration($declaration_name,
14431da177e4SLinus Torvalds			   'typedef',
14441da177e4SLinus Torvalds			   {'typedef' => $declaration_name,
14451da177e4SLinus Torvalds			    'module' => $modulename,
14461da177e4SLinus Torvalds			    'sectionlist' => \@sectionlist,
14471da177e4SLinus Torvalds			    'sections' => \%sections,
14481da177e4SLinus Torvalds			    'purpose' => $declaration_purpose
14491da177e4SLinus Torvalds			   });
14501da177e4SLinus Torvalds    }
14511da177e4SLinus Torvalds    else {
1452d40e1e65SBart Van Assche	print STDERR "${file}:$.: error: Cannot parse typedef!\n";
14531da177e4SLinus Torvalds	++$errors;
14541da177e4SLinus Torvalds    }
14551da177e4SLinus Torvalds}
14561da177e4SLinus Torvalds
1457a1d94aa5SRandy Dunlapsub save_struct_actual($) {
1458a1d94aa5SRandy Dunlap    my $actual = shift;
1459a1d94aa5SRandy Dunlap
1460a1d94aa5SRandy Dunlap    # strip all spaces from the actual param so that it looks like one string item
1461a1d94aa5SRandy Dunlap    $actual =~ s/\s*//g;
1462a1d94aa5SRandy Dunlap    $struct_actual = $struct_actual . $actual . " ";
1463a1d94aa5SRandy Dunlap}
1464a1d94aa5SRandy Dunlap
1465151c468bSMauro Carvalho Chehabsub create_parameterlist($$$$) {
14661da177e4SLinus Torvalds    my $args = shift;
14671da177e4SLinus Torvalds    my $splitter = shift;
14681da177e4SLinus Torvalds    my $file = shift;
1469151c468bSMauro Carvalho Chehab    my $declaration_name = shift;
14701da177e4SLinus Torvalds    my $type;
14711da177e4SLinus Torvalds    my $param;
14721da177e4SLinus Torvalds
1473a6d3fe77SMartin Waitz    # temporarily replace commas inside function pointer definition
1474e86bdb24SAditya Srivastava    my $arg_expr = qr{\([^\),]+};
1475e86bdb24SAditya Srivastava    while ($args =~ /$arg_expr,/) {
1476e86bdb24SAditya Srivastava	$args =~ s/($arg_expr),/$1#/g;
14771da177e4SLinus Torvalds    }
14781da177e4SLinus Torvalds
14791da177e4SLinus Torvalds    foreach my $arg (split($splitter, $args)) {
14801da177e4SLinus Torvalds	# strip comments
14811da177e4SLinus Torvalds	$arg =~ s/\/\*.*\*\///;
148203699f27SKees Cook	# ignore argument attributes
148303699f27SKees Cook	$arg =~ s/\sPOS0?\s/ /;
14841da177e4SLinus Torvalds	# strip leading/trailing spaces
14851da177e4SLinus Torvalds	$arg =~ s/^\s*//;
14861da177e4SLinus Torvalds	$arg =~ s/\s*$//;
14871da177e4SLinus Torvalds	$arg =~ s/\s+/ /;
14881da177e4SLinus Torvalds
14891da177e4SLinus Torvalds	if ($arg =~ /^#/) {
14901da177e4SLinus Torvalds	    # Treat preprocessor directive as a typeless variable just to fill
14911da177e4SLinus Torvalds	    # corresponding data structures "correctly". Catch it later in
14921da177e4SLinus Torvalds	    # output_* subs.
1493ed8348e2SMauro Carvalho Chehab	    push_parameter($arg, "", "", $file);
149400d62961SRichard Kennedy	} elsif ($arg =~ m/\(.+\)\s*\(/) {
14951da177e4SLinus Torvalds	    # pointer-to-function
14961da177e4SLinus Torvalds	    $arg =~ tr/#/,/;
1497336ced2dSAditya Srivastava	    $arg =~ m/[^\(]+\(\*?\s*([\w\[\]\.]*)\s*\)/;
14981da177e4SLinus Torvalds	    $param = $1;
14991da177e4SLinus Torvalds	    $type = $arg;
150000d62961SRichard Kennedy	    $type =~ s/([^\(]+\(\*?)\s*$param/$1/;
1501a1d94aa5SRandy Dunlap	    save_struct_actual($param);
1502ed8348e2SMauro Carvalho Chehab	    push_parameter($param, $type, $arg, $file, $declaration_name);
1503aeec46b9SMartin Waitz	} elsif ($arg) {
15041da177e4SLinus Torvalds	    $arg =~ s/\s*:\s*/:/g;
15051da177e4SLinus Torvalds	    $arg =~ s/\s*\[/\[/g;
15061da177e4SLinus Torvalds
15071da177e4SLinus Torvalds	    my @args = split('\s*,\s*', $arg);
15081da177e4SLinus Torvalds	    if ($args[0] =~ m/\*/) {
15091da177e4SLinus Torvalds		$args[0] =~ s/(\*+)\s*/ $1/;
15101da177e4SLinus Torvalds	    }
1511884f2810SBorislav Petkov
1512884f2810SBorislav Petkov	    my @first_arg;
1513884f2810SBorislav Petkov	    if ($args[0] =~ /^(.*\s+)(.*?\[.*\].*)$/) {
1514884f2810SBorislav Petkov		    shift @args;
1515884f2810SBorislav Petkov		    push(@first_arg, split('\s+', $1));
1516884f2810SBorislav Petkov		    push(@first_arg, $2);
1517884f2810SBorislav Petkov	    } else {
1518884f2810SBorislav Petkov		    @first_arg = split('\s+', shift @args);
1519884f2810SBorislav Petkov	    }
1520884f2810SBorislav Petkov
15211da177e4SLinus Torvalds	    unshift(@args, pop @first_arg);
15221da177e4SLinus Torvalds	    $type = join " ", @first_arg;
15231da177e4SLinus Torvalds
15241da177e4SLinus Torvalds	    foreach $param (@args) {
15251da177e4SLinus Torvalds		if ($param =~ m/^(\*+)\s*(.*)/) {
1526a1d94aa5SRandy Dunlap		    save_struct_actual($2);
1527ed8348e2SMauro Carvalho Chehab
1528ed8348e2SMauro Carvalho Chehab		    push_parameter($2, "$type $1", $arg, $file, $declaration_name);
15291da177e4SLinus Torvalds		}
15301da177e4SLinus Torvalds		elsif ($param =~ m/(.*?):(\d+)/) {
15317b97887eSRandy Dunlap		    if ($type ne "") { # skip unnamed bit-fields
1532a1d94aa5SRandy Dunlap			save_struct_actual($1);
1533ed8348e2SMauro Carvalho Chehab			push_parameter($1, "$type:$2", $arg, $file, $declaration_name)
15341da177e4SLinus Torvalds		    }
15357b97887eSRandy Dunlap		}
15361da177e4SLinus Torvalds		else {
1537a1d94aa5SRandy Dunlap		    save_struct_actual($param);
1538ed8348e2SMauro Carvalho Chehab		    push_parameter($param, $type, $arg, $file, $declaration_name);
15391da177e4SLinus Torvalds		}
15401da177e4SLinus Torvalds	    }
15411da177e4SLinus Torvalds	}
15421da177e4SLinus Torvalds    }
15431da177e4SLinus Torvalds}
15441da177e4SLinus Torvalds
1545ed8348e2SMauro Carvalho Chehabsub push_parameter($$$$$) {
15461da177e4SLinus Torvalds	my $param = shift;
15471da177e4SLinus Torvalds	my $type = shift;
1548ed8348e2SMauro Carvalho Chehab	my $org_arg = shift;
15491da177e4SLinus Torvalds	my $file = shift;
1550151c468bSMauro Carvalho Chehab	my $declaration_name = shift;
15511da177e4SLinus Torvalds
15525f8c7c98SRandy Dunlap	if (($anon_struct_union == 1) && ($type eq "") &&
15535f8c7c98SRandy Dunlap	    ($param eq "}")) {
15545f8c7c98SRandy Dunlap		return;		# ignore the ending }; from anon. struct/union
15555f8c7c98SRandy Dunlap	}
15565f8c7c98SRandy Dunlap
15575f8c7c98SRandy Dunlap	$anon_struct_union = 0;
1558f9b5c530SMauro Carvalho Chehab	$param =~ s/[\[\)].*//;
15591da177e4SLinus Torvalds
1560a6d3fe77SMartin Waitz	if ($type eq "" && $param =~ /\.\.\.$/)
15611da177e4SLinus Torvalds	{
1562c950a173SSilvio Fricke	    if (!$param =~ /\w\.\.\.$/) {
1563c950a173SSilvio Fricke	      # handles unnamed variable parameters
1564ef00028bSJonathan Corbet	      $param = "...";
1565c950a173SSilvio Fricke	    }
156643756e34SJonathan Neuschäfer	    elsif ($param =~ /\w\.\.\.$/) {
156743756e34SJonathan Neuschäfer	      # for named variable parameters of the form `x...`, remove the dots
156843756e34SJonathan Neuschäfer	      $param =~ s/\.\.\.$//;
156943756e34SJonathan Neuschäfer	    }
1570ced69090SRandy Dunlap	    if (!defined $parameterdescs{$param} || $parameterdescs{$param} eq "") {
1571a6d3fe77SMartin Waitz		$parameterdescs{$param} = "variable arguments";
15721da177e4SLinus Torvalds	    }
1573ced69090SRandy Dunlap	}
15741da177e4SLinus Torvalds	elsif ($type eq "" && ($param eq "" or $param eq "void"))
15751da177e4SLinus Torvalds	{
15761da177e4SLinus Torvalds	    $param="void";
15771da177e4SLinus Torvalds	    $parameterdescs{void} = "no arguments";
15781da177e4SLinus Torvalds	}
1579134fe01bSRandy Dunlap	elsif ($type eq "" && ($param eq "struct" or $param eq "union"))
1580134fe01bSRandy Dunlap	# handle unnamed (anonymous) union or struct:
1581134fe01bSRandy Dunlap	{
1582134fe01bSRandy Dunlap		$type = $param;
1583134fe01bSRandy Dunlap		$param = "{unnamed_" . $param . "}";
1584134fe01bSRandy Dunlap		$parameterdescs{$param} = "anonymous\n";
15855f8c7c98SRandy Dunlap		$anon_struct_union = 1;
1586134fe01bSRandy Dunlap	}
1587134fe01bSRandy Dunlap
1588a6d3fe77SMartin Waitz	# warn if parameter has no description
1589134fe01bSRandy Dunlap	# (but ignore ones starting with # as these are not parameters
1590134fe01bSRandy Dunlap	# but inline preprocessor statements);
1591151c468bSMauro Carvalho Chehab	# Note: It will also ignore void params and unnamed structs/unions
1592f9b5c530SMauro Carvalho Chehab	if (!defined $parameterdescs{$param} && $param !~ /^#/) {
1593f9b5c530SMauro Carvalho Chehab		$parameterdescs{$param} = $undescribed;
15941da177e4SLinus Torvalds
1595be5cd20cSJonathan Corbet	        if (show_warnings($type, $declaration_name) && $param !~ /\./) {
1596df4bf98eSNiklas Söderlund			emit_warning("${file}:$.", "Function parameter or member '$param' not described in '$declaration_name'\n");
15971da177e4SLinus Torvalds		}
15982defb272SMauro Carvalho Chehab	}
15991da177e4SLinus Torvalds
160025985edcSLucas De Marchi	# strip spaces from $param so that it is one continuous string
1601e34e7dbbSRandy Dunlap	# on @parameterlist;
1602e34e7dbbSRandy Dunlap	# this fixes a problem where check_sections() cannot find
1603e34e7dbbSRandy Dunlap	# a parameter like "addr[6 + 2]" because it actually appears
1604e34e7dbbSRandy Dunlap	# as "addr[6", "+", "2]" on the parameter list;
1605e34e7dbbSRandy Dunlap	# but it's better to maintain the param string unchanged for output,
1606e34e7dbbSRandy Dunlap	# so just weaken the string compare in check_sections() to ignore
1607e34e7dbbSRandy Dunlap	# "[blah" in a parameter string;
1608e34e7dbbSRandy Dunlap	###$param =~ s/\s*//g;
16091da177e4SLinus Torvalds	push @parameterlist, $param;
1610ed8348e2SMauro Carvalho Chehab	$org_arg =~ s/\s\s+/ /g;
1611ed8348e2SMauro Carvalho Chehab	$parametertypes{$param} = $org_arg;
16121da177e4SLinus Torvalds}
16131da177e4SLinus Torvalds
16141081de2dSMauro Carvalho Chehabsub check_sections($$$$$) {
16151081de2dSMauro Carvalho Chehab	my ($file, $decl_name, $decl_type, $sectcheck, $prmscheck) = @_;
1616a1d94aa5SRandy Dunlap	my @sects = split ' ', $sectcheck;
1617a1d94aa5SRandy Dunlap	my @prms = split ' ', $prmscheck;
1618a1d94aa5SRandy Dunlap	my $err;
1619a1d94aa5SRandy Dunlap	my ($px, $sx);
1620a1d94aa5SRandy Dunlap	my $prm_clean;		# strip trailing "[array size]" and/or beginning "*"
1621a1d94aa5SRandy Dunlap
1622a1d94aa5SRandy Dunlap	foreach $sx (0 .. $#sects) {
1623a1d94aa5SRandy Dunlap		$err = 1;
1624a1d94aa5SRandy Dunlap		foreach $px (0 .. $#prms) {
1625a1d94aa5SRandy Dunlap			$prm_clean = $prms[$px];
1626a1d94aa5SRandy Dunlap			$prm_clean =~ s/\[.*\]//;
1627e86bdb24SAditya Srivastava			$prm_clean =~ s/$attribute//i;
1628e34e7dbbSRandy Dunlap			# ignore array size in a parameter string;
1629e34e7dbbSRandy Dunlap			# however, the original param string may contain
1630e34e7dbbSRandy Dunlap			# spaces, e.g.:  addr[6 + 2]
1631e34e7dbbSRandy Dunlap			# and this appears in @prms as "addr[6" since the
1632e34e7dbbSRandy Dunlap			# parameter list is split at spaces;
1633e34e7dbbSRandy Dunlap			# hence just ignore "[..." for the sections check;
1634e34e7dbbSRandy Dunlap			$prm_clean =~ s/\[.*//;
1635e34e7dbbSRandy Dunlap
1636a1d94aa5SRandy Dunlap			##$prm_clean =~ s/^\**//;
1637a1d94aa5SRandy Dunlap			if ($prm_clean eq $sects[$sx]) {
1638a1d94aa5SRandy Dunlap				$err = 0;
1639a1d94aa5SRandy Dunlap				last;
1640a1d94aa5SRandy Dunlap			}
1641a1d94aa5SRandy Dunlap		}
1642a1d94aa5SRandy Dunlap		if ($err) {
1643a1d94aa5SRandy Dunlap			if ($decl_type eq "function") {
1644df4bf98eSNiklas Söderlund				emit_warning("${file}:$.",
1645a1d94aa5SRandy Dunlap					"Excess function parameter " .
1646a1d94aa5SRandy Dunlap					"'$sects[$sx]' " .
1647df4bf98eSNiklas Söderlund					"description in '$decl_name'\n");
1648a1d94aa5SRandy Dunlap			}
1649a1d94aa5SRandy Dunlap		}
1650a1d94aa5SRandy Dunlap	}
1651a1d94aa5SRandy Dunlap}
1652a1d94aa5SRandy Dunlap
16531da177e4SLinus Torvalds##
16544092bac7SYacine Belkadi# Checks the section describing the return value of a function.
16554092bac7SYacine Belkadisub check_return_section {
16564092bac7SYacine Belkadi        my $file = shift;
16574092bac7SYacine Belkadi        my $declaration_name = shift;
16584092bac7SYacine Belkadi        my $return_type = shift;
16594092bac7SYacine Belkadi
16604092bac7SYacine Belkadi        # Ignore an empty return type (It's a macro)
16614092bac7SYacine Belkadi        # Ignore functions with a "void" return type. (But don't ignore "void *")
16624092bac7SYacine Belkadi        if (($return_type eq "") || ($return_type =~ /void\s*\w*\s*$/)) {
16634092bac7SYacine Belkadi                return;
16644092bac7SYacine Belkadi        }
16654092bac7SYacine Belkadi
16664092bac7SYacine Belkadi        if (!defined($sections{$section_return}) ||
16674092bac7SYacine Belkadi            $sections{$section_return} eq "") {
1668df4bf98eSNiklas Söderlund                emit_warning("${file}:$.",
16694092bac7SYacine Belkadi                        "No description found for return value of " .
1670df4bf98eSNiklas Söderlund                        "'$declaration_name'\n");
16714092bac7SYacine Belkadi        }
16724092bac7SYacine Belkadi}
16734092bac7SYacine Belkadi
16744092bac7SYacine Belkadi##
16751da177e4SLinus Torvalds# takes a function prototype and the name of the current file being
16761da177e4SLinus Torvalds# processed and spits out all the details stored in the global
16771da177e4SLinus Torvalds# arrays/hashes.
16781da177e4SLinus Torvaldssub dump_function($$) {
16791da177e4SLinus Torvalds    my $prototype = shift;
16801da177e4SLinus Torvalds    my $file = shift;
1681cbb4d3e6SHoria Geanta    my $noret = 0;
16821da177e4SLinus Torvalds
16835ef09c96SMauro Carvalho Chehab    print_lineno($new_start_line);
16845eb6b4b3SMauro Carvalho Chehab
16851da177e4SLinus Torvalds    $prototype =~ s/^static +//;
16861da177e4SLinus Torvalds    $prototype =~ s/^extern +//;
16874dc3b16bSPavel Pisa    $prototype =~ s/^asmlinkage +//;
16881da177e4SLinus Torvalds    $prototype =~ s/^inline +//;
16891da177e4SLinus Torvalds    $prototype =~ s/^__inline__ +//;
169032e79401SRandy Dunlap    $prototype =~ s/^__inline +//;
169132e79401SRandy Dunlap    $prototype =~ s/^__always_inline +//;
169232e79401SRandy Dunlap    $prototype =~ s/^noinline +//;
169303699f27SKees Cook    $prototype =~ s/^__FORTIFY_INLINE +//;
169474fc5c65SRandy Dunlap    $prototype =~ s/__init +//;
169520072205SRandy Dunlap    $prototype =~ s/__init_or_module +//;
169680342d48SMatthew Wilcox    $prototype =~ s/__deprecated +//;
1697084aa001SAditya Srivastava    $prototype =~ s/__flatten +//;
1698270a0096SRandy Dunlap    $prototype =~ s/__meminit +//;
169970c95b00SRandy Dunlap    $prototype =~ s/__must_check +//;
17000df7c0e3SRandy Dunlap    $prototype =~ s/__weak +//;
17010891f959SMatthew Wilcox    $prototype =~ s/__sched +//;
170295e760cbSRandy Dunlap    $prototype =~ s/__printf\s*\(\s*\d*\s*,\s*\d*\s*\) +//;
170303699f27SKees Cook    $prototype =~ s/__(?:re)?alloc_size\s*\(\s*\d+\s*(?:,\s*\d+\s*)?\) +//;
170403699f27SKees Cook    $prototype =~ s/__diagnose_as\s*\(\s*\S+\s*(?:,\s*\d+\s*)*\) +//;
1705cbb4d3e6SHoria Geanta    my $define = $prototype =~ s/^#\s*define\s+//; #ak added
1706084aa001SAditya Srivastava    $prototype =~ s/__attribute_const__ +//;
1707b1aaa546SPaolo Bonzini    $prototype =~ s/__attribute__\s*\(\(
1708b1aaa546SPaolo Bonzini            (?:
1709b1aaa546SPaolo Bonzini                 [\w\s]++          # attribute name
1710b1aaa546SPaolo Bonzini                 (?:\([^)]*+\))?   # attribute arguments
1711b1aaa546SPaolo Bonzini                 \s*+,?            # optional comma at the end
1712b1aaa546SPaolo Bonzini            )+
1713b1aaa546SPaolo Bonzini          \)\)\s+//x;
17141da177e4SLinus Torvalds
17151da177e4SLinus Torvalds    # Yes, this truly is vile.  We are looking for:
17161da177e4SLinus Torvalds    # 1. Return type (may be nothing if we're looking at a macro)
17171da177e4SLinus Torvalds    # 2. Function name
17181da177e4SLinus Torvalds    # 3. Function parameters.
17191da177e4SLinus Torvalds    #
17201da177e4SLinus Torvalds    # All the while we have to watch out for function pointer parameters
17211da177e4SLinus Torvalds    # (which IIRC is what the two sections are for), C types (these
17221da177e4SLinus Torvalds    # regexps don't even start to express all the possibilities), and
17231da177e4SLinus Torvalds    # so on.
17241da177e4SLinus Torvalds    #
17251da177e4SLinus Torvalds    # If you mess with these regexps, it's a good idea to check that
17261da177e4SLinus Torvalds    # the following functions' documentation still comes out right:
17271da177e4SLinus Torvalds    # - parport_register_device (function pointer parameters)
17281da177e4SLinus Torvalds    # - atomic_set (macro)
17299598f91fSMartin Waitz    # - pci_match_device, __copy_to_user (long return type)
1730e86bdb24SAditya Srivastava    my $name = qr{[a-zA-Z0-9_~:]+};
1731e86bdb24SAditya Srivastava    my $prototype_end1 = qr{[^\(]*};
1732e86bdb24SAditya Srivastava    my $prototype_end2 = qr{[^\{]*};
1733e86bdb24SAditya Srivastava    my $prototype_end = qr{\(($prototype_end1|$prototype_end2)\)};
1734e86bdb24SAditya Srivastava    my $type1 = qr{[\w\s]+};
1735e86bdb24SAditya Srivastava    my $type2 = qr{$type1\*+};
17361da177e4SLinus Torvalds
1737e86bdb24SAditya Srivastava    if ($define && $prototype =~ m/^()($name)\s+/) {
1738cbb4d3e6SHoria Geanta        # This is an object-like macro, it has no return type and no parameter
1739cbb4d3e6SHoria Geanta        # list.
1740cbb4d3e6SHoria Geanta        # Function-like macros are not allowed to have spaces between
1741cbb4d3e6SHoria Geanta        # declaration_name and opening parenthesis (notice the \s+).
1742cbb4d3e6SHoria Geanta        $return_type = $1;
1743cbb4d3e6SHoria Geanta        $declaration_name = $2;
1744cbb4d3e6SHoria Geanta        $noret = 1;
1745e86bdb24SAditya Srivastava    } elsif ($prototype =~ m/^()($name)\s*$prototype_end/ ||
1746e86bdb24SAditya Srivastava	$prototype =~ m/^($type1)\s+($name)\s*$prototype_end/ ||
1747e86bdb24SAditya Srivastava	$prototype =~ m/^($type2+)\s*($name)\s*$prototype_end/)  {
17481da177e4SLinus Torvalds	$return_type = $1;
17491da177e4SLinus Torvalds	$declaration_name = $2;
17501da177e4SLinus Torvalds	my $args = $3;
17511da177e4SLinus Torvalds
1752151c468bSMauro Carvalho Chehab	create_parameterlist($args, ',', $file, $declaration_name);
17531da177e4SLinus Torvalds    } else {
1754df4bf98eSNiklas Söderlund	emit_warning("${file}:$.", "cannot understand function prototype: '$prototype'\n");
17551da177e4SLinus Torvalds	return;
17561da177e4SLinus Torvalds    }
17571da177e4SLinus Torvalds
175852042e2dSMauro Carvalho Chehab    if ($identifier ne $declaration_name) {
1759df4bf98eSNiklas Söderlund	emit_warning("${file}:$.", "expecting prototype for $identifier(). Prototype was for $declaration_name() instead\n");
176052042e2dSMauro Carvalho Chehab	return;
176152042e2dSMauro Carvalho Chehab    }
176252042e2dSMauro Carvalho Chehab
1763a1d94aa5SRandy Dunlap    my $prms = join " ", @parameterlist;
17641081de2dSMauro Carvalho Chehab    check_sections($file, $declaration_name, "function", $sectcheck, $prms);
1765a1d94aa5SRandy Dunlap
17664092bac7SYacine Belkadi    # This check emits a lot of warnings at the moment, because many
17674092bac7SYacine Belkadi    # functions don't have a 'Return' doc section. So until the number
17684092bac7SYacine Belkadi    # of warnings goes sufficiently down, the check is only performed in
1769*56b0f453SJohannes Berg    # -Wreturn mode.
17704092bac7SYacine Belkadi    # TODO: always perform the check.
1771*56b0f453SJohannes Berg    if ($Wreturn && !$noret) {
17724092bac7SYacine Belkadi	    check_return_section($file, $declaration_name, $return_type);
17734092bac7SYacine Belkadi    }
17744092bac7SYacine Belkadi
177547bcacfdSMauro Carvalho Chehab    # The function parser can be called with a typedef parameter.
177647bcacfdSMauro Carvalho Chehab    # Handle it.
177747bcacfdSMauro Carvalho Chehab    if ($return_type =~ /typedef/) {
177847bcacfdSMauro Carvalho Chehab	output_declaration($declaration_name,
177947bcacfdSMauro Carvalho Chehab			   'function',
178047bcacfdSMauro Carvalho Chehab			   {'function' => $declaration_name,
178147bcacfdSMauro Carvalho Chehab			    'typedef' => 1,
178247bcacfdSMauro Carvalho Chehab			    'module' => $modulename,
178347bcacfdSMauro Carvalho Chehab			    'functiontype' => $return_type,
178447bcacfdSMauro Carvalho Chehab			    'parameterlist' => \@parameterlist,
178547bcacfdSMauro Carvalho Chehab			    'parameterdescs' => \%parameterdescs,
178647bcacfdSMauro Carvalho Chehab			    'parametertypes' => \%parametertypes,
178747bcacfdSMauro Carvalho Chehab			    'sectionlist' => \@sectionlist,
178847bcacfdSMauro Carvalho Chehab			    'sections' => \%sections,
178947bcacfdSMauro Carvalho Chehab			    'purpose' => $declaration_purpose
179047bcacfdSMauro Carvalho Chehab			   });
179147bcacfdSMauro Carvalho Chehab    } else {
17921da177e4SLinus Torvalds	output_declaration($declaration_name,
17931da177e4SLinus Torvalds			   'function',
17941da177e4SLinus Torvalds			   {'function' => $declaration_name,
17951da177e4SLinus Torvalds			    'module' => $modulename,
17961da177e4SLinus Torvalds			    'functiontype' => $return_type,
17971da177e4SLinus Torvalds			    'parameterlist' => \@parameterlist,
17981da177e4SLinus Torvalds			    'parameterdescs' => \%parameterdescs,
17991da177e4SLinus Torvalds			    'parametertypes' => \%parametertypes,
18001da177e4SLinus Torvalds			    'sectionlist' => \@sectionlist,
18011da177e4SLinus Torvalds			    'sections' => \%sections,
18021da177e4SLinus Torvalds			    'purpose' => $declaration_purpose
18031da177e4SLinus Torvalds			   });
18041da177e4SLinus Torvalds    }
180547bcacfdSMauro Carvalho Chehab}
18061da177e4SLinus Torvalds
18071da177e4SLinus Torvaldssub reset_state {
18081da177e4SLinus Torvalds    $function = "";
18091da177e4SLinus Torvalds    %parameterdescs = ();
18101da177e4SLinus Torvalds    %parametertypes = ();
18111da177e4SLinus Torvalds    @parameterlist = ();
18121da177e4SLinus Torvalds    %sections = ();
18131da177e4SLinus Torvalds    @sectionlist = ();
1814a1d94aa5SRandy Dunlap    $sectcheck = "";
1815a1d94aa5SRandy Dunlap    $struct_actual = "";
18161da177e4SLinus Torvalds    $prototype = "";
18171da177e4SLinus Torvalds
181848af606aSJani Nikula    $state = STATE_NORMAL;
181948af606aSJani Nikula    $inline_doc_state = STATE_INLINE_NA;
18201da177e4SLinus Torvalds}
18211da177e4SLinus Torvalds
182256afb0f8SJason Baronsub tracepoint_munge($) {
182356afb0f8SJason Baron	my $file = shift;
182456afb0f8SJason Baron	my $tracepointname = 0;
182556afb0f8SJason Baron	my $tracepointargs = 0;
182656afb0f8SJason Baron
182756afb0f8SJason Baron	if ($prototype =~ m/TRACE_EVENT\((.*?),/) {
182856afb0f8SJason Baron		$tracepointname = $1;
182956afb0f8SJason Baron	}
18303a9089fdSJason Baron	if ($prototype =~ m/DEFINE_SINGLE_EVENT\((.*?),/) {
18313a9089fdSJason Baron		$tracepointname = $1;
18323a9089fdSJason Baron	}
18333a9089fdSJason Baron	if ($prototype =~ m/DEFINE_EVENT\((.*?),(.*?),/) {
18343a9089fdSJason Baron		$tracepointname = $2;
18353a9089fdSJason Baron	}
18363a9089fdSJason Baron	$tracepointname =~ s/^\s+//; #strip leading whitespace
183756afb0f8SJason Baron	if ($prototype =~ m/TP_PROTO\((.*?)\)/) {
183856afb0f8SJason Baron		$tracepointargs = $1;
183956afb0f8SJason Baron	}
184056afb0f8SJason Baron	if (($tracepointname eq 0) || ($tracepointargs eq 0)) {
1841df4bf98eSNiklas Söderlund		emit_warning("${file}:$.", "Unrecognized tracepoint format: \n".
1842df4bf98eSNiklas Söderlund			     "$prototype\n");
184356afb0f8SJason Baron	} else {
184456afb0f8SJason Baron		$prototype = "static inline void trace_$tracepointname($tracepointargs)";
184552042e2dSMauro Carvalho Chehab		$identifier = "trace_$identifier";
184656afb0f8SJason Baron	}
184756afb0f8SJason Baron}
184856afb0f8SJason Baron
1849b4870bc5SRandy Dunlapsub syscall_munge() {
1850b4870bc5SRandy Dunlap	my $void = 0;
1851b4870bc5SRandy Dunlap
18527c9aa015SMauro Carvalho Chehab	$prototype =~ s@[\r\n]+@ @gos; # strip newlines/CR's
1853b4870bc5SRandy Dunlap##	if ($prototype =~ m/SYSCALL_DEFINE0\s*\(\s*(a-zA-Z0-9_)*\s*\)/) {
1854b4870bc5SRandy Dunlap	if ($prototype =~ m/SYSCALL_DEFINE0/) {
1855b4870bc5SRandy Dunlap		$void = 1;
1856b4870bc5SRandy Dunlap##		$prototype = "long sys_$1(void)";
1857b4870bc5SRandy Dunlap	}
1858b4870bc5SRandy Dunlap
1859b4870bc5SRandy Dunlap	$prototype =~ s/SYSCALL_DEFINE.*\(/long sys_/; # fix return type & func name
1860b4870bc5SRandy Dunlap	if ($prototype =~ m/long (sys_.*?),/) {
1861b4870bc5SRandy Dunlap		$prototype =~ s/,/\(/;
1862b4870bc5SRandy Dunlap	} elsif ($void) {
1863b4870bc5SRandy Dunlap		$prototype =~ s/\)/\(void\)/;
1864b4870bc5SRandy Dunlap	}
1865b4870bc5SRandy Dunlap
1866b4870bc5SRandy Dunlap	# now delete all of the odd-number commas in $prototype
1867b4870bc5SRandy Dunlap	# so that arg types & arg names don't have a comma between them
1868b4870bc5SRandy Dunlap	my $count = 0;
1869b4870bc5SRandy Dunlap	my $len = length($prototype);
1870b4870bc5SRandy Dunlap	if ($void) {
1871b4870bc5SRandy Dunlap		$len = 0;	# skip the for-loop
1872b4870bc5SRandy Dunlap	}
1873b4870bc5SRandy Dunlap	for (my $ix = 0; $ix < $len; $ix++) {
1874b4870bc5SRandy Dunlap		if (substr($prototype, $ix, 1) eq ',') {
1875b4870bc5SRandy Dunlap			$count++;
1876b4870bc5SRandy Dunlap			if ($count % 2 == 1) {
1877b4870bc5SRandy Dunlap				substr($prototype, $ix, 1) = ' ';
1878b4870bc5SRandy Dunlap			}
1879b4870bc5SRandy Dunlap		}
1880b4870bc5SRandy Dunlap	}
1881b4870bc5SRandy Dunlap}
1882b4870bc5SRandy Dunlap
1883b7afa92bSDaniel Vettersub process_proto_function($$) {
18841da177e4SLinus Torvalds    my $x = shift;
18851da177e4SLinus Torvalds    my $file = shift;
18861da177e4SLinus Torvalds
188751f5a0c8SRandy Dunlap    $x =~ s@\/\/.*$@@gos; # strip C99-style comments to end of line
188851f5a0c8SRandy Dunlap
1889890c78c2SRandy Dunlap    if ($x =~ m#\s*/\*\s+MACDOC\s*#io || ($x =~ /^#/ && $x !~ /^#\s*define/)) {
18901da177e4SLinus Torvalds	# do nothing
18911da177e4SLinus Torvalds    }
18921da177e4SLinus Torvalds    elsif ($x =~ /([^\{]*)/) {
18931da177e4SLinus Torvalds	$prototype .= $1;
18941da177e4SLinus Torvalds    }
1895b4870bc5SRandy Dunlap
1896890c78c2SRandy Dunlap    if (($x =~ /\{/) || ($x =~ /\#\s*define/) || ($x =~ /;/)) {
18971da177e4SLinus Torvalds	$prototype =~ s@/\*.*?\*/@@gos;	# strip comments.
18981da177e4SLinus Torvalds	$prototype =~ s@[\r\n]+@ @gos; # strip newlines/cr's.
18991da177e4SLinus Torvalds	$prototype =~ s@^\s+@@gos; # strip leading spaces
19007ae281b0SMauro Carvalho Chehab
19017ae281b0SMauro Carvalho Chehab	 # Handle prototypes for function pointers like:
19027ae281b0SMauro Carvalho Chehab	 # int (*pcs_config)(struct foo)
19037ae281b0SMauro Carvalho Chehab	$prototype =~ s@^(\S+\s+)\(\s*\*(\S+)\)@$1$2@gos;
19047ae281b0SMauro Carvalho Chehab
1905b4870bc5SRandy Dunlap	if ($prototype =~ /SYSCALL_DEFINE/) {
1906b4870bc5SRandy Dunlap		syscall_munge();
1907b4870bc5SRandy Dunlap	}
19083a9089fdSJason Baron	if ($prototype =~ /TRACE_EVENT/ || $prototype =~ /DEFINE_EVENT/ ||
19093a9089fdSJason Baron	    $prototype =~ /DEFINE_SINGLE_EVENT/)
19103a9089fdSJason Baron	{
191156afb0f8SJason Baron		tracepoint_munge($file);
191256afb0f8SJason Baron	}
19131da177e4SLinus Torvalds	dump_function($prototype, $file);
19141da177e4SLinus Torvalds	reset_state();
19151da177e4SLinus Torvalds    }
19161da177e4SLinus Torvalds}
19171da177e4SLinus Torvalds
1918b7afa92bSDaniel Vettersub process_proto_type($$) {
19191da177e4SLinus Torvalds    my $x = shift;
19201da177e4SLinus Torvalds    my $file = shift;
19211da177e4SLinus Torvalds
19221da177e4SLinus Torvalds    $x =~ s@[\r\n]+@ @gos; # strip newlines/cr's.
19231da177e4SLinus Torvalds    $x =~ s@^\s+@@gos; # strip leading spaces
19241da177e4SLinus Torvalds    $x =~ s@\s+$@@gos; # strip trailing spaces
192551f5a0c8SRandy Dunlap    $x =~ s@\/\/.*$@@gos; # strip C99-style comments to end of line
192651f5a0c8SRandy Dunlap
19271da177e4SLinus Torvalds    if ($x =~ /^#/) {
19281da177e4SLinus Torvalds	# To distinguish preprocessor directive from regular declaration later.
19291da177e4SLinus Torvalds	$x .= ";";
19301da177e4SLinus Torvalds    }
19311da177e4SLinus Torvalds
19321da177e4SLinus Torvalds    while (1) {
1933673bb2dfSBen Hutchings	if ( $x =~ /([^\{\};]*)([\{\};])(.*)/ ) {
1934463a0fdcSMarkus Heiser            if( length $prototype ) {
1935463a0fdcSMarkus Heiser                $prototype .= " "
1936463a0fdcSMarkus Heiser            }
19371da177e4SLinus Torvalds	    $prototype .= $1 . $2;
19381da177e4SLinus Torvalds	    ($2 eq '{') && $brcount++;
19391da177e4SLinus Torvalds	    ($2 eq '}') && $brcount--;
19401da177e4SLinus Torvalds	    if (($2 eq ';') && ($brcount == 0)) {
19411da177e4SLinus Torvalds		dump_declaration($prototype, $file);
19421da177e4SLinus Torvalds		reset_state();
19431da177e4SLinus Torvalds		last;
19441da177e4SLinus Torvalds	    }
19451da177e4SLinus Torvalds	    $x = $3;
19461da177e4SLinus Torvalds	} else {
19471da177e4SLinus Torvalds	    $prototype .= $x;
19481da177e4SLinus Torvalds	    last;
19491da177e4SLinus Torvalds	}
19501da177e4SLinus Torvalds    }
19511da177e4SLinus Torvalds}
19521da177e4SLinus Torvalds
19536b5b55f6SRandy Dunlap
19541ad560e4SJani Nikulasub map_filename($) {
19551ad560e4SJani Nikula    my $file;
19561ad560e4SJani Nikula    my ($orig_file) = @_;
19571ad560e4SJani Nikula
19581ad560e4SJani Nikula    if (defined($ENV{'SRCTREE'})) {
19591ad560e4SJani Nikula	$file = "$ENV{'SRCTREE'}" . "/" . $orig_file;
19601ad560e4SJani Nikula    } else {
19611ad560e4SJani Nikula	$file = $orig_file;
19621ad560e4SJani Nikula    }
19631ad560e4SJani Nikula
19641ad560e4SJani Nikula    if (defined($source_map{$file})) {
19651ad560e4SJani Nikula	$file = $source_map{$file};
19661ad560e4SJani Nikula    }
19671ad560e4SJani Nikula
19681ad560e4SJani Nikula    return $file;
19691ad560e4SJani Nikula}
19701ad560e4SJani Nikula
197188c2b57dSJani Nikulasub process_export_file($) {
197288c2b57dSJani Nikula    my ($orig_file) = @_;
197388c2b57dSJani Nikula    my $file = map_filename($orig_file);
197488c2b57dSJani Nikula
197588c2b57dSJani Nikula    if (!open(IN,"<$file")) {
197688c2b57dSJani Nikula	print STDERR "Error: Cannot open file $file\n";
197788c2b57dSJani Nikula	++$errors;
197888c2b57dSJani Nikula	return;
197988c2b57dSJani Nikula    }
198088c2b57dSJani Nikula
198188c2b57dSJani Nikula    while (<IN>) {
198288c2b57dSJani Nikula	if (/$export_symbol/) {
1983eab795ddSMauro Carvalho Chehab	    next if (defined($nosymbol_table{$2}));
198488c2b57dSJani Nikula	    $function_table{$2} = 1;
198588c2b57dSJani Nikula	}
1986632ce137SJason Gunthorpe	if (/$export_symbol_ns/) {
1987632ce137SJason Gunthorpe	    next if (defined($nosymbol_table{$2}));
1988632ce137SJason Gunthorpe	    $function_table{$2} = 1;
1989632ce137SJason Gunthorpe	}
199088c2b57dSJani Nikula    }
199188c2b57dSJani Nikula
199288c2b57dSJani Nikula    close(IN);
199388c2b57dSJani Nikula}
199488c2b57dSJani Nikula
199507048d13SJonathan Corbet#
199607048d13SJonathan Corbet# Parsers for the various processing states.
199707048d13SJonathan Corbet#
199807048d13SJonathan Corbet# STATE_NORMAL: looking for the /** to begin everything.
199907048d13SJonathan Corbet#
200007048d13SJonathan Corbetsub process_normal() {
20011da177e4SLinus Torvalds    if (/$doc_start/o) {
200248af606aSJani Nikula	$state = STATE_NAME;	# next line is always the function name
2003850622dfSRandy Dunlap	$in_doc_sect = 0;
20040b0f5f29SDaniel Vetter	$declaration_start_line = $. + 1;
20051da177e4SLinus Torvalds    }
200607048d13SJonathan Corbet}
200707048d13SJonathan Corbet
20083cac2bc4SJonathan Corbet#
20093cac2bc4SJonathan Corbet# STATE_NAME: Looking for the "name - description" line
20103cac2bc4SJonathan Corbet#
20113cac2bc4SJonathan Corbetsub process_name($$) {
20123cac2bc4SJonathan Corbet    my $file = shift;
20131da177e4SLinus Torvalds    my $descr;
20141da177e4SLinus Torvalds
20151da177e4SLinus Torvalds    if (/$doc_block/o) {
201648af606aSJani Nikula	$state = STATE_DOCBLOCK;
20171da177e4SLinus Torvalds	$contents = "";
20185ef09c96SMauro Carvalho Chehab	$new_start_line = $.;
20190b0f5f29SDaniel Vetter
20201da177e4SLinus Torvalds	if ( $1 eq "" ) {
20211da177e4SLinus Torvalds	    $section = $section_intro;
20221da177e4SLinus Torvalds	} else {
20231da177e4SLinus Torvalds	    $section = $1;
20241da177e4SLinus Torvalds	}
202552042e2dSMauro Carvalho Chehab    } elsif (/$doc_decl/o) {
20261da177e4SLinus Torvalds	$identifier = $1;
20273e58e839SAditya Srivastava	my $is_kernel_comment = 0;
2028e86bdb24SAditya Srivastava	my $decl_start = qr{$doc_com};
2029f9bbc12cSAditya Srivastava	# test for pointer declaration type, foo * bar() - desc
2030f9bbc12cSAditya Srivastava	my $fn_type = qr{\w+\s*\*\s*};
2031f9bbc12cSAditya Srivastava	my $parenthesis = qr{\(\w*\)};
2032f9bbc12cSAditya Srivastava	my $decl_end = qr{[-:].*};
2033e86bdb24SAditya Srivastava	if (/^$decl_start([\w\s]+?)$parenthesis?\s*$decl_end?$/) {
20341da177e4SLinus Torvalds	    $identifier = $1;
20351da177e4SLinus Torvalds	}
203652042e2dSMauro Carvalho Chehab	if ($identifier =~ m/^(struct|union|enum|typedef)\b\s*(\S*)/) {
203752042e2dSMauro Carvalho Chehab	    $decl_type = $1;
203852042e2dSMauro Carvalho Chehab	    $identifier = $2;
20393e58e839SAditya Srivastava	    $is_kernel_comment = 1;
204052042e2dSMauro Carvalho Chehab	}
2041f9bbc12cSAditya Srivastava	# Look for foo() or static void foo() - description; or misspelt
2042f9bbc12cSAditya Srivastava	# identifier
2043e86bdb24SAditya Srivastava	elsif (/^$decl_start$fn_type?(\w+)\s*$parenthesis?\s*$decl_end?$/ ||
2044e86bdb24SAditya Srivastava	    /^$decl_start$fn_type?(\w+.*)$parenthesis?\s*$decl_end$/) {
2045f9bbc12cSAditya Srivastava	    $identifier = $1;
2046f9bbc12cSAditya Srivastava	    $decl_type = 'function';
2047f9bbc12cSAditya Srivastava	    $identifier =~ s/^define\s+//;
2048f9bbc12cSAditya Srivastava	    $is_kernel_comment = 1;
2049f9bbc12cSAditya Srivastava	}
205052042e2dSMauro Carvalho Chehab	$identifier =~ s/\s+$//;
20511da177e4SLinus Torvalds
205217b78717SJonathan Corbet	$state = STATE_BODY;
20530b0f5f29SDaniel Vetter	# if there's no @param blocks need to set up default section
20540b0f5f29SDaniel Vetter	# here
20552f4ad40aSJani Nikula	$contents = "";
20562f4ad40aSJani Nikula	$section = $section_default;
20570b0f5f29SDaniel Vetter	$new_start_line = $. + 1;
205852042e2dSMauro Carvalho Chehab	if (/[-:](.*)/) {
205951f5a0c8SRandy Dunlap	    # strip leading/trailing/multiple spaces
2060a21217daSRandy Dunlap	    $descr= $1;
2061a21217daSRandy Dunlap	    $descr =~ s/^\s*//;
2062a21217daSRandy Dunlap	    $descr =~ s/\s*$//;
206312ae6779SDaniel Santos	    $descr =~ s/\s+/ /g;
20640bba924cSJonathan Corbet	    $declaration_purpose = $descr;
206517b78717SJonathan Corbet	    $state = STATE_BODY_MAYBE;
20661da177e4SLinus Torvalds	} else {
20671da177e4SLinus Torvalds	    $declaration_purpose = "";
20681da177e4SLinus Torvalds	}
206977cc23b8SRandy Dunlap
20703e58e839SAditya Srivastava	if (!$is_kernel_comment) {
2071df4bf98eSNiklas Söderlund	    emit_warning("${file}:$.", "This comment starts with '/**', but isn't a kernel-doc comment. Refer Documentation/doc-guide/kernel-doc.rst\n$_");
20723e58e839SAditya Srivastava	    $state = STATE_NORMAL;
20733e58e839SAditya Srivastava	}
20743e58e839SAditya Srivastava
2075*56b0f453SJohannes Berg	if (($declaration_purpose eq "") && $Wshort_desc) {
2076df4bf98eSNiklas Söderlund	    emit_warning("${file}:$.", "missing initial short description on line:\n$_");
207777cc23b8SRandy Dunlap	}
207877cc23b8SRandy Dunlap
20790b54c2e3SMauro Carvalho Chehab	if ($identifier eq "" && $decl_type ne "enum") {
2080df4bf98eSNiklas Söderlund	    emit_warning("${file}:$.", "wrong kernel-doc identifier on line:\n$_");
208152042e2dSMauro Carvalho Chehab	    $state = STATE_NORMAL;
20821da177e4SLinus Torvalds	}
20831da177e4SLinus Torvalds
20841da177e4SLinus Torvalds	if ($verbose) {
208552042e2dSMauro Carvalho Chehab	    print STDERR "${file}:$.: info: Scanning doc for $decl_type $identifier\n";
20861da177e4SLinus Torvalds	}
20871da177e4SLinus Torvalds    } else {
2088df4bf98eSNiklas Söderlund	emit_warning("${file}:$.", "Cannot understand $_ on line $. - I thought it was a doc line\n");
208948af606aSJani Nikula	$state = STATE_NORMAL;
20901da177e4SLinus Torvalds    }
20913cac2bc4SJonathan Corbet}
20923cac2bc4SJonathan Corbet
20933cac2bc4SJonathan Corbet
2094d742f24dSJonathan Corbet#
2095d742f24dSJonathan Corbet# STATE_BODY and STATE_BODY_MAYBE: the bulk of a kerneldoc comment.
2096d742f24dSJonathan Corbet#
2097d742f24dSJonathan Corbetsub process_body($$) {
2098d742f24dSJonathan Corbet    my $file = shift;
20993cac2bc4SJonathan Corbet
21000d55d48bSMauro Carvalho Chehab    if ($state == STATE_BODY_WITH_BLANK_LINE && /^\s*\*\s?\S/) {
21010d55d48bSMauro Carvalho Chehab	dump_section($file, $section, $contents);
21020d55d48bSMauro Carvalho Chehab	$section = $section_default;
21035ef09c96SMauro Carvalho Chehab	$new_start_line = $.;
21040d55d48bSMauro Carvalho Chehab	$contents = "";
21050d55d48bSMauro Carvalho Chehab    }
21060d55d48bSMauro Carvalho Chehab
2107f624adefSJani Nikula    if (/$doc_sect/i) { # case insensitive for supported section names
21081da177e4SLinus Torvalds	$newsection = $1;
21091da177e4SLinus Torvalds	$newcontents = $2;
21101da177e4SLinus Torvalds
2111f624adefSJani Nikula	# map the supported section names to the canonical names
2112f624adefSJani Nikula	if ($newsection =~ m/^description$/i) {
2113f624adefSJani Nikula	    $newsection = $section_default;
2114f624adefSJani Nikula	} elsif ($newsection =~ m/^context$/i) {
2115f624adefSJani Nikula	    $newsection = $section_context;
2116f624adefSJani Nikula	} elsif ($newsection =~ m/^returns?$/i) {
2117f624adefSJani Nikula	    $newsection = $section_return;
2118f624adefSJani Nikula	} elsif ($newsection =~ m/^\@return$/) {
2119f624adefSJani Nikula	    # special: @return is a section, not a param description
2120f624adefSJani Nikula	    $newsection = $section_return;
2121f624adefSJani Nikula	}
2122f624adefSJani Nikula
2123792aa2f2SRandy Dunlap	if (($contents ne "") && ($contents ne "\n")) {
2124*56b0f453SJohannes Berg	    if (!$in_doc_sect && $Wcontents_before_sections) {
2125df4bf98eSNiklas Söderlund		emit_warning("${file}:$.", "contents before sections\n");
2126850622dfSRandy Dunlap	    }
21270bba924cSJonathan Corbet	    dump_section($file, $section, $contents);
21281da177e4SLinus Torvalds	    $section = $section_default;
21291da177e4SLinus Torvalds	}
21301da177e4SLinus Torvalds
2131850622dfSRandy Dunlap	$in_doc_sect = 1;
213217b78717SJonathan Corbet	$state = STATE_BODY;
21331da177e4SLinus Torvalds	$contents = $newcontents;
21340b0f5f29SDaniel Vetter	$new_start_line = $.;
21357c9aa015SMauro Carvalho Chehab	while (substr($contents, 0, 1) eq " ") {
213605189497SRandy Dunlap	    $contents = substr($contents, 1);
213705189497SRandy Dunlap	}
21380a726301SJani Nikula	if ($contents ne "") {
21391da177e4SLinus Torvalds	    $contents .= "\n";
21401da177e4SLinus Torvalds	}
21411da177e4SLinus Torvalds	$section = $newsection;
2142b7886de4SJani Nikula	$leading_space = undef;
21431da177e4SLinus Torvalds    } elsif (/$doc_end/) {
21444c98ecafSRandy Dunlap	if (($contents ne "") && ($contents ne "\n")) {
21450bba924cSJonathan Corbet	    dump_section($file, $section, $contents);
21461da177e4SLinus Torvalds	    $section = $section_default;
21471da177e4SLinus Torvalds	    $contents = "";
21481da177e4SLinus Torvalds	}
214946b958ebSRandy Dunlap	# look for doc_com + <text> + doc_end:
215046b958ebSRandy Dunlap	if ($_ =~ m'\s*\*\s*[a-zA-Z_0-9:\.]+\*/') {
2151df4bf98eSNiklas Söderlund	    emit_warning("${file}:$.", "suspicious ending line: $_");
215246b958ebSRandy Dunlap	}
21531da177e4SLinus Torvalds
21541da177e4SLinus Torvalds	$prototype = "";
215548af606aSJani Nikula	$state = STATE_PROTO;
21561da177e4SLinus Torvalds	$brcount = 0;
21575ef09c96SMauro Carvalho Chehab        $new_start_line = $. + 1;
21581da177e4SLinus Torvalds    } elsif (/$doc_content/) {
21596423133bSJohannes Weiner	if ($1 eq "") {
21600d55d48bSMauro Carvalho Chehab	    if ($section eq $section_context) {
21610bba924cSJonathan Corbet		dump_section($file, $section, $contents);
21621da177e4SLinus Torvalds		$section = $section_default;
21631da177e4SLinus Torvalds		$contents = "";
21640b0f5f29SDaniel Vetter		$new_start_line = $.;
21650d55d48bSMauro Carvalho Chehab		$state = STATE_BODY;
21661da177e4SLinus Torvalds	    } else {
21670d55d48bSMauro Carvalho Chehab		if ($section ne $section_default) {
21680d55d48bSMauro Carvalho Chehab		    $state = STATE_BODY_WITH_BLANK_LINE;
21690d55d48bSMauro Carvalho Chehab		} else {
21700d55d48bSMauro Carvalho Chehab		    $state = STATE_BODY;
21710d55d48bSMauro Carvalho Chehab		}
21726423133bSJohannes Weiner		$contents .= "\n";
21736423133bSJohannes Weiner	    }
217417b78717SJonathan Corbet	} elsif ($state == STATE_BODY_MAYBE) {
21756423133bSJohannes Weiner	    # Continued declaration purpose
21766423133bSJohannes Weiner	    chomp($declaration_purpose);
21770bba924cSJonathan Corbet	    $declaration_purpose .= " " . $1;
217812ae6779SDaniel Santos	    $declaration_purpose =~ s/\s+/ /g;
21796423133bSJohannes Weiner	} else {
2180b7886de4SJani Nikula	    my $cont = $1;
2181b7886de4SJani Nikula	    if ($section =~ m/^@/ || $section eq $section_context) {
2182b7886de4SJani Nikula		if (!defined $leading_space) {
2183b7886de4SJani Nikula		    if ($cont =~ m/^(\s+)/) {
2184b7886de4SJani Nikula			$leading_space = $1;
2185b7886de4SJani Nikula		    } else {
2186b7886de4SJani Nikula			$leading_space = "";
2187b7886de4SJani Nikula		    }
2188b7886de4SJani Nikula		}
2189b7886de4SJani Nikula		$cont =~ s/^$leading_space//;
2190b7886de4SJani Nikula	    }
2191b7886de4SJani Nikula	    $contents .= $cont . "\n";
21921da177e4SLinus Torvalds	}
21931da177e4SLinus Torvalds    } else {
21941da177e4SLinus Torvalds	# i dont know - bad line?  ignore.
2195df4bf98eSNiklas Söderlund	emit_warning("${file}:$.", "bad line: $_");
21961da177e4SLinus Torvalds    }
2197d742f24dSJonathan Corbet}
2198d742f24dSJonathan Corbet
2199d742f24dSJonathan Corbet
2200cc794812SJonathan Corbet#
2201cc794812SJonathan Corbet# STATE_PROTO: reading a function/whatever prototype.
2202cc794812SJonathan Corbet#
2203cc794812SJonathan Corbetsub process_proto($$) {
2204cc794812SJonathan Corbet    my $file = shift;
2205cc794812SJonathan Corbet
2206cc794812SJonathan Corbet    if (/$doc_inline_oneline/) {
2207cc794812SJonathan Corbet	$section = $1;
2208cc794812SJonathan Corbet	$contents = $2;
2209cc794812SJonathan Corbet	if ($contents ne "") {
2210cc794812SJonathan Corbet	    $contents .= "\n";
2211cc794812SJonathan Corbet	    dump_section($file, $section, $contents);
2212cc794812SJonathan Corbet	    $section = $section_default;
2213cc794812SJonathan Corbet	    $contents = "";
2214cc794812SJonathan Corbet	}
2215cc794812SJonathan Corbet    } elsif (/$doc_inline_start/) {
2216cc794812SJonathan Corbet	$state = STATE_INLINE;
2217cc794812SJonathan Corbet	$inline_doc_state = STATE_INLINE_NAME;
2218cc794812SJonathan Corbet    } elsif ($decl_type eq 'function') {
2219cc794812SJonathan Corbet	process_proto_function($_, $file);
2220cc794812SJonathan Corbet    } else {
2221cc794812SJonathan Corbet	process_proto_type($_, $file);
2222cc794812SJonathan Corbet    }
2223cc794812SJonathan Corbet}
2224cc794812SJonathan Corbet
2225c17add56SJonathan Corbet#
2226c17add56SJonathan Corbet# STATE_DOCBLOCK: within a DOC: block.
2227c17add56SJonathan Corbet#
2228c17add56SJonathan Corbetsub process_docblock($$) {
2229c17add56SJonathan Corbet    my $file = shift;
2230cc794812SJonathan Corbet
2231c17add56SJonathan Corbet    if (/$doc_end/) {
2232c17add56SJonathan Corbet	dump_doc_section($file, $section, $contents);
2233c17add56SJonathan Corbet	$section = $section_default;
2234c17add56SJonathan Corbet	$contents = "";
2235c17add56SJonathan Corbet	$function = "";
2236c17add56SJonathan Corbet	%parameterdescs = ();
2237c17add56SJonathan Corbet	%parametertypes = ();
2238c17add56SJonathan Corbet	@parameterlist = ();
2239c17add56SJonathan Corbet	%sections = ();
2240c17add56SJonathan Corbet	@sectionlist = ();
2241c17add56SJonathan Corbet	$prototype = "";
2242c17add56SJonathan Corbet	$state = STATE_NORMAL;
2243c17add56SJonathan Corbet    } elsif (/$doc_content/) {
2244c17add56SJonathan Corbet	if ( $1 eq "" )	{
2245c17add56SJonathan Corbet	    $contents .= $blankline;
2246c17add56SJonathan Corbet	} else {
2247c17add56SJonathan Corbet	    $contents .= $1 . "\n";
2248c17add56SJonathan Corbet	}
2249c17add56SJonathan Corbet    }
2250d742f24dSJonathan Corbet}
2251d742f24dSJonathan Corbet
2252c17add56SJonathan Corbet#
2253c17add56SJonathan Corbet# STATE_INLINE: docbook comments within a prototype.
2254c17add56SJonathan Corbet#
2255c17add56SJonathan Corbetsub process_inline($$) {
2256c17add56SJonathan Corbet    my $file = shift;
2257d742f24dSJonathan Corbet
2258a4c6ebedSDanilo Cesar Lemes de Paula    # First line (state 1) needs to be a @parameter
225948af606aSJani Nikula    if ($inline_doc_state == STATE_INLINE_NAME && /$doc_inline_sect/o) {
2260a4c6ebedSDanilo Cesar Lemes de Paula	$section = $1;
2261a4c6ebedSDanilo Cesar Lemes de Paula	$contents = $2;
22620b0f5f29SDaniel Vetter	$new_start_line = $.;
2263a4c6ebedSDanilo Cesar Lemes de Paula	if ($contents ne "") {
22647c9aa015SMauro Carvalho Chehab	    while (substr($contents, 0, 1) eq " ") {
2265a4c6ebedSDanilo Cesar Lemes de Paula		$contents = substr($contents, 1);
2266a4c6ebedSDanilo Cesar Lemes de Paula	    }
2267a4c6ebedSDanilo Cesar Lemes de Paula	    $contents .= "\n";
2268a4c6ebedSDanilo Cesar Lemes de Paula	}
226948af606aSJani Nikula	$inline_doc_state = STATE_INLINE_TEXT;
2270a4c6ebedSDanilo Cesar Lemes de Paula	# Documentation block end */
227148af606aSJani Nikula    } elsif (/$doc_inline_end/) {
2272a4c6ebedSDanilo Cesar Lemes de Paula	if (($contents ne "") && ($contents ne "\n")) {
22730bba924cSJonathan Corbet	    dump_section($file, $section, $contents);
2274a4c6ebedSDanilo Cesar Lemes de Paula	    $section = $section_default;
2275a4c6ebedSDanilo Cesar Lemes de Paula	    $contents = "";
2276a4c6ebedSDanilo Cesar Lemes de Paula	}
227748af606aSJani Nikula	$state = STATE_PROTO;
227848af606aSJani Nikula	$inline_doc_state = STATE_INLINE_NA;
2279a4c6ebedSDanilo Cesar Lemes de Paula	# Regular text
2280a4c6ebedSDanilo Cesar Lemes de Paula    } elsif (/$doc_content/) {
228148af606aSJani Nikula	if ($inline_doc_state == STATE_INLINE_TEXT) {
2282a4c6ebedSDanilo Cesar Lemes de Paula	    $contents .= $1 . "\n";
22836450c895SJani Nikula	    # nuke leading blank lines
22846450c895SJani Nikula	    if ($contents =~ /^\s*$/) {
22856450c895SJani Nikula		$contents = "";
22866450c895SJani Nikula	    }
228748af606aSJani Nikula	} elsif ($inline_doc_state == STATE_INLINE_NAME) {
228848af606aSJani Nikula	    $inline_doc_state = STATE_INLINE_ERROR;
2289df4bf98eSNiklas Söderlund	    emit_warning("${file}:$.", "Incorrect use of kernel-doc format: $_");
2290a4c6ebedSDanilo Cesar Lemes de Paula	}
2291a4c6ebedSDanilo Cesar Lemes de Paula    }
22920c9aa209SJani Nikula}
2293c17add56SJonathan Corbet
2294c17add56SJonathan Corbet
2295c17add56SJonathan Corbetsub process_file($) {
2296c17add56SJonathan Corbet    my $file;
2297c17add56SJonathan Corbet    my $initial_section_counter = $section_counter;
2298c17add56SJonathan Corbet    my ($orig_file) = @_;
2299c17add56SJonathan Corbet
2300c17add56SJonathan Corbet    $file = map_filename($orig_file);
2301c17add56SJonathan Corbet
2302dbe8ba00SMauro Carvalho Chehab    if (!open(IN_FILE,"<$file")) {
2303c17add56SJonathan Corbet	print STDERR "Error: Cannot open file $file\n";
2304c17add56SJonathan Corbet	++$errors;
2305c17add56SJonathan Corbet	return;
23061da177e4SLinus Torvalds    }
2307c17add56SJonathan Corbet
2308c17add56SJonathan Corbet    $. = 1;
2309c17add56SJonathan Corbet
2310c17add56SJonathan Corbet    $section_counter = 0;
2311dbe8ba00SMauro Carvalho Chehab    while (<IN_FILE>) {
2312c17add56SJonathan Corbet	while (s/\\\s*$//) {
2313dbe8ba00SMauro Carvalho Chehab	    $_ .= <IN_FILE>;
2314c17add56SJonathan Corbet	}
2315c17add56SJonathan Corbet	# Replace tabs by spaces
2316c17add56SJonathan Corbet        while ($_ =~ s/\t+/' ' x (length($&) * 8 - length($`) % 8)/e) {};
2317c17add56SJonathan Corbet	# Hand this line to the appropriate state handler
2318c17add56SJonathan Corbet	if ($state == STATE_NORMAL) {
2319c17add56SJonathan Corbet	    process_normal();
2320c17add56SJonathan Corbet	} elsif ($state == STATE_NAME) {
2321c17add56SJonathan Corbet	    process_name($file, $_);
23220d55d48bSMauro Carvalho Chehab	} elsif ($state == STATE_BODY || $state == STATE_BODY_MAYBE ||
23230d55d48bSMauro Carvalho Chehab		 $state == STATE_BODY_WITH_BLANK_LINE) {
2324c17add56SJonathan Corbet	    process_body($file, $_);
2325c17add56SJonathan Corbet	} elsif ($state == STATE_INLINE) { # scanning for inline parameters
2326c17add56SJonathan Corbet	    process_inline($file, $_);
2327cc794812SJonathan Corbet	} elsif ($state == STATE_PROTO) {
2328cc794812SJonathan Corbet	    process_proto($file, $_);
232948af606aSJani Nikula	} elsif ($state == STATE_DOCBLOCK) {
2330c17add56SJonathan Corbet	    process_docblock($file, $_);
23311da177e4SLinus Torvalds	}
23321da177e4SLinus Torvalds    }
2333c17add56SJonathan Corbet
2334c17add56SJonathan Corbet    # Make sure we got something interesting.
2335b0d60bfbSJonathan Corbet    if ($initial_section_counter == $section_counter && $
2336b0d60bfbSJonathan Corbet	output_mode ne "none") {
2337b0d60bfbSJonathan Corbet	if ($output_selection == OUTPUT_INCLUDE) {
2338df4bf98eSNiklas Söderlund	    emit_warning("${file}:1", "'$_' not found\n")
2339b0d60bfbSJonathan Corbet		for keys %function_table;
23403a025e1dSMatthew Wilcox	}
2341b0d60bfbSJonathan Corbet	else {
2342df4bf98eSNiklas Söderlund	    emit_warning("${file}:1", "no structured comments found\n");
2343e946c43aSJohannes Berg	}
23441da177e4SLinus Torvalds    }
2345dbe8ba00SMauro Carvalho Chehab    close IN_FILE;
23461da177e4SLinus Torvalds}
23478484baaaSRandy Dunlap
23488484baaaSRandy Dunlap
234993351d41SMauro Carvalho Chehabif ($output_mode eq "rst") {
235093351d41SMauro Carvalho Chehab	get_sphinx_version() if (!$sphinx_major);
235193351d41SMauro Carvalho Chehab}
235293351d41SMauro Carvalho Chehab
23538484baaaSRandy Dunlap$kernelversion = get_kernel_version();
23548484baaaSRandy Dunlap
23558484baaaSRandy Dunlap# generate a sequence of code that will splice in highlighting information
23568484baaaSRandy Dunlap# using the s// operator.
23571ef06233SMauro Carvalho Chehabfor (my $k = 0; $k < @highlights; $k++) {
23584d732701SDanilo Cesar Lemes de Paula    my $pattern = $highlights[$k][0];
23594d732701SDanilo Cesar Lemes de Paula    my $result = $highlights[$k][1];
23604d732701SDanilo Cesar Lemes de Paula#   print STDERR "scanning pattern:$pattern, highlight:($result)\n";
23614d732701SDanilo Cesar Lemes de Paula    $dohighlight .=  "\$contents =~ s:$pattern:$result:gs;\n";
23628484baaaSRandy Dunlap}
23638484baaaSRandy Dunlap
23648484baaaSRandy Dunlap# Read the file that maps relative names to absolute names for
23658484baaaSRandy Dunlap# separate source and object directories and for shadow trees.
23668484baaaSRandy Dunlapif (open(SOURCE_MAP, "<.tmp_filelist.txt")) {
23678484baaaSRandy Dunlap	my ($relname, $absname);
23688484baaaSRandy Dunlap	while(<SOURCE_MAP>) {
23698484baaaSRandy Dunlap		chop();
23708484baaaSRandy Dunlap		($relname, $absname) = (split())[0..1];
23718484baaaSRandy Dunlap		$relname =~ s:^/+::;
23728484baaaSRandy Dunlap		$source_map{$relname} = $absname;
23738484baaaSRandy Dunlap	}
23748484baaaSRandy Dunlap	close(SOURCE_MAP);
23758484baaaSRandy Dunlap}
23768484baaaSRandy Dunlap
237788c2b57dSJani Nikulaif ($output_selection == OUTPUT_EXPORTED ||
237888c2b57dSJani Nikula    $output_selection == OUTPUT_INTERNAL) {
2379c9b2cfb3SJani Nikula
2380c9b2cfb3SJani Nikula    push(@export_file_list, @ARGV);
2381c9b2cfb3SJani Nikula
238288c2b57dSJani Nikula    foreach (@export_file_list) {
238388c2b57dSJani Nikula	chomp;
238488c2b57dSJani Nikula	process_export_file($_);
238588c2b57dSJani Nikula    }
238688c2b57dSJani Nikula}
238788c2b57dSJani Nikula
23888484baaaSRandy Dunlapforeach (@ARGV) {
23898484baaaSRandy Dunlap    chomp;
23908484baaaSRandy Dunlap    process_file($_);
23918484baaaSRandy Dunlap}
23928484baaaSRandy Dunlapif ($verbose && $errors) {
23938484baaaSRandy Dunlap  print STDERR "$errors errors\n";
23948484baaaSRandy Dunlap}
23958484baaaSRandy Dunlapif ($verbose && $warnings) {
23968484baaaSRandy Dunlap  print STDERR "$warnings warnings\n";
23978484baaaSRandy Dunlap}
23988484baaaSRandy Dunlap
23992c12c810SPierre-Louis Bossartif ($Werror && $warnings) {
24002c12c810SPierre-Louis Bossart    print STDERR "$warnings warnings as Errors\n";
24012c12c810SPierre-Louis Bossart    exit($warnings);
24022c12c810SPierre-Louis Bossart} else {
24032c12c810SPierre-Louis Bossart    exit($output_mode eq "none" ? 0 : $errors)
24042c12c810SPierre-Louis Bossart}
24052875f787STomasz Warniełło
24062875f787STomasz Warniełło__END__
24072875f787STomasz Warniełło
24082875f787STomasz Warniełło=head1 OPTIONS
24092875f787STomasz Warniełło
24102875f787STomasz Warniełło=head2 Output format selection (mutually exclusive):
24112875f787STomasz Warniełło
24122875f787STomasz Warniełło=over 8
24132875f787STomasz Warniełło
24142875f787STomasz Warniełło=item -man
24152875f787STomasz Warniełło
24162875f787STomasz WarniełłoOutput troff manual page format.
24172875f787STomasz Warniełło
24182875f787STomasz Warniełło=item -rst
24192875f787STomasz Warniełło
24202875f787STomasz WarniełłoOutput reStructuredText format. This is the default.
24212875f787STomasz Warniełło
24222875f787STomasz Warniełło=item -none
24232875f787STomasz Warniełło
24242875f787STomasz WarniełłoDo not output documentation, only warnings.
24252875f787STomasz Warniełło
24262875f787STomasz Warniełło=back
24272875f787STomasz Warniełło
2428dd803b04STomasz Warniełło=head2 Output format modifiers
2429dd803b04STomasz Warniełło
2430dd803b04STomasz Warniełło=head3 reStructuredText only
2431dd803b04STomasz Warniełło
2432dd803b04STomasz Warniełło=over 8
2433dd803b04STomasz Warniełło
2434dd803b04STomasz Warniełło=item -sphinx-version VERSION
2435dd803b04STomasz Warniełło
2436dd803b04STomasz WarniełłoUse the ReST C domain dialect compatible with a specific Sphinx Version.
2437dd803b04STomasz Warniełło
2438dd803b04STomasz WarniełłoIf not specified, kernel-doc will auto-detect using the sphinx-build version
2439dd803b04STomasz Warniełłofound on PATH.
2440dd803b04STomasz Warniełło
2441dd803b04STomasz Warniełło=back
2442dd803b04STomasz Warniełło
24439c77f108STomasz Warniełło=head2 Output selection (mutually exclusive):
24449c77f108STomasz Warniełło
24459c77f108STomasz Warniełło=over 8
24469c77f108STomasz Warniełło
24479c77f108STomasz Warniełło=item -export
24489c77f108STomasz Warniełło
24499c77f108STomasz WarniełłoOnly output documentation for the symbols that have been exported using
2450632ce137SJason GunthorpeEXPORT_SYMBOL() and related macros in any input FILE or -export-file FILE.
24519c77f108STomasz Warniełło
24529c77f108STomasz Warniełło=item -internal
24539c77f108STomasz Warniełło
24549c77f108STomasz WarniełłoOnly output documentation for the symbols that have NOT been exported using
2455632ce137SJason GunthorpeEXPORT_SYMBOL() and related macros in any input FILE or -export-file FILE.
24569c77f108STomasz Warniełło
24579c77f108STomasz Warniełło=item -function NAME
24589c77f108STomasz Warniełło
24599c77f108STomasz WarniełłoOnly output documentation for the given function or DOC: section title.
24609c77f108STomasz WarniełłoAll other functions and DOC: sections are ignored.
24619c77f108STomasz Warniełło
24629c77f108STomasz WarniełłoMay be specified multiple times.
24639c77f108STomasz Warniełło
24649c77f108STomasz Warniełło=item -nosymbol NAME
24659c77f108STomasz Warniełło
24669c77f108STomasz WarniełłoExclude the specified symbol from the output documentation.
24679c77f108STomasz Warniełło
24689c77f108STomasz WarniełłoMay be specified multiple times.
24699c77f108STomasz Warniełło
24709c77f108STomasz Warniełło=back
24719c77f108STomasz Warniełło
2472c15de5a1STomasz Warniełło=head2 Output selection modifiers:
2473c15de5a1STomasz Warniełło
2474c15de5a1STomasz Warniełło=over 8
2475c15de5a1STomasz Warniełło
2476c15de5a1STomasz Warniełło=item -no-doc-sections
2477c15de5a1STomasz Warniełło
2478c15de5a1STomasz WarniełłoDo not output DOC: sections.
2479c15de5a1STomasz Warniełło
2480c15de5a1STomasz Warniełło=item -export-file FILE
2481c15de5a1STomasz Warniełło
2482632ce137SJason GunthorpeSpecify an additional FILE in which to look for EXPORT_SYMBOL information.
2483c15de5a1STomasz Warniełło
2484c15de5a1STomasz WarniełłoTo be used with -export or -internal.
2485c15de5a1STomasz Warniełło
2486c15de5a1STomasz WarniełłoMay be specified multiple times.
2487c15de5a1STomasz Warniełło
2488c15de5a1STomasz Warniełło=back
2489c15de5a1STomasz Warniełło
2490c15de5a1STomasz Warniełło=head3 reStructuredText only
2491c15de5a1STomasz Warniełło
2492c15de5a1STomasz Warniełło=over 8
2493c15de5a1STomasz Warniełło
2494c15de5a1STomasz Warniełło=item -enable-lineno
2495c15de5a1STomasz Warniełło
2496b79dfef0SMauro Carvalho ChehabEnable output of .. LINENO lines.
2497c15de5a1STomasz Warniełło
2498c15de5a1STomasz Warniełło=back
2499c15de5a1STomasz Warniełło
2500834cf6b9STomasz Warniełło=head2 Other parameters:
2501834cf6b9STomasz Warniełło
2502834cf6b9STomasz Warniełło=over 8
2503834cf6b9STomasz Warniełło
2504834cf6b9STomasz Warniełło=item -h, -help
2505834cf6b9STomasz Warniełło
2506834cf6b9STomasz WarniełłoPrint this help.
2507834cf6b9STomasz Warniełło
2508834cf6b9STomasz Warniełło=item -v
2509834cf6b9STomasz Warniełło
2510834cf6b9STomasz WarniełłoVerbose output, more warnings and other information.
2511834cf6b9STomasz Warniełło
2512834cf6b9STomasz Warniełło=item -Werror
2513834cf6b9STomasz Warniełło
2514834cf6b9STomasz WarniełłoTreat warnings as errors.
2515834cf6b9STomasz Warniełło
2516834cf6b9STomasz Warniełło=back
2517834cf6b9STomasz Warniełło
25182875f787STomasz Warniełło=cut
2519