1cb77f0d6SKamil Rytarowski#!/usr/bin/env perl 238476378SJonathan Corbet# SPDX-License-Identifier: GPL-2.0 31da177e4SLinus Torvalds 4cb77f0d6SKamil Rytarowskiuse warnings; 51da177e4SLinus Torvaldsuse strict; 61da177e4SLinus Torvalds 71da177e4SLinus Torvalds## Copyright (c) 1998 Michael Zucchi, All Rights Reserved ## 81da177e4SLinus Torvalds## Copyright (C) 2000, 1 Tim Waugh <twaugh@redhat.com> ## 91da177e4SLinus Torvalds## Copyright (C) 2001 Simon Huggins ## 1070c95b00SRandy Dunlap## Copyright (C) 2005-2012 Randy Dunlap ## 111b40c194SDan Luedtke## Copyright (C) 2012 Dan Luedtke ## 121da177e4SLinus Torvalds## ## 131da177e4SLinus Torvalds## #define enhancements by Armin Kuster <akuster@mvista.com> ## 141da177e4SLinus Torvalds## Copyright (c) 2000 MontaVista Software, Inc. ## 151da177e4SLinus Torvalds## ## 161da177e4SLinus Torvalds## This software falls under the GNU General Public License. ## 171da177e4SLinus Torvalds## Please read the COPYING file for more information ## 181da177e4SLinus Torvalds 191da177e4SLinus Torvalds# 18/01/2001 - Cleanups 201da177e4SLinus Torvalds# Functions prototyped as foo(void) same as foo() 211da177e4SLinus Torvalds# Stop eval'ing where we don't need to. 221da177e4SLinus Torvalds# -- huggie@earth.li 231da177e4SLinus Torvalds 241da177e4SLinus Torvalds# 27/06/2001 - Allowed whitespace after initial "/**" and 251da177e4SLinus Torvalds# allowed comments before function declarations. 261da177e4SLinus Torvalds# -- Christian Kreibich <ck@whoop.org> 271da177e4SLinus Torvalds 281da177e4SLinus Torvalds# Still to do: 291da177e4SLinus Torvalds# - add perldoc documentation 301da177e4SLinus Torvalds# - Look more closely at some of the scarier bits :) 311da177e4SLinus Torvalds 321da177e4SLinus Torvalds# 26/05/2001 - Support for separate source and object trees. 331da177e4SLinus Torvalds# Return error code. 341da177e4SLinus Torvalds# Keith Owens <kaos@ocs.com.au> 351da177e4SLinus Torvalds 361da177e4SLinus Torvalds# 23/09/2001 - Added support for typedefs, structs, enums and unions 371da177e4SLinus Torvalds# Support for Context section; can be terminated using empty line 381da177e4SLinus Torvalds# Small fixes (like spaces vs. \s in regex) 391da177e4SLinus Torvalds# -- Tim Jansen <tim@tjansen.de> 401da177e4SLinus Torvalds 411b40c194SDan Luedtke# 25/07/2012 - Added support for HTML5 421b40c194SDan Luedtke# -- Dan Luedtke <mail@danrl.de> 431da177e4SLinus Torvalds 44fadc0b31SJani Nikulasub usage { 45fadc0b31SJani Nikula my $message = <<"EOF"; 46fadc0b31SJani NikulaUsage: $0 [OPTION ...] FILE ... 471da177e4SLinus Torvalds 48fadc0b31SJani NikulaRead C language source or header FILEs, extract embedded documentation comments, 49fadc0b31SJani Nikulaand print formatted documentation to standard output. 501da177e4SLinus Torvalds 51fadc0b31SJani NikulaThe documentation comments are identified by "/**" opening comment mark. See 52857af3b7SMauro Carvalho ChehabDocumentation/doc-guide/kernel-doc.rst for the documentation comment syntax. 53fadc0b31SJani Nikula 54fadc0b31SJani NikulaOutput format selection (mutually exclusive): 55fadc0b31SJani Nikula -man Output troff manual page format. This is the default. 56c0d1b6eeSJonathan Corbet -rst Output reStructuredText format. 573a025e1dSMatthew Wilcox -none Do not output documentation, only warnings. 58fadc0b31SJani Nikula 59fadc0b31SJani NikulaOutput selection (mutually exclusive): 6086ae2e38SJani Nikula -export Only output documentation for symbols that have been 6186ae2e38SJani Nikula exported using EXPORT_SYMBOL() or EXPORT_SYMBOL_GPL() 62c9b2cfb3SJani Nikula in any input FILE or -export-file FILE. 6386ae2e38SJani Nikula -internal Only output documentation for symbols that have NOT been 6486ae2e38SJani Nikula exported using EXPORT_SYMBOL() or EXPORT_SYMBOL_GPL() 65c9b2cfb3SJani Nikula in any input FILE or -export-file FILE. 66fadc0b31SJani Nikula -function NAME Only output documentation for the given function(s) 67fadc0b31SJani Nikula or DOC: section title(s). All other functions and DOC: 68fadc0b31SJani Nikula sections are ignored. May be specified multiple times. 69fadc0b31SJani Nikula -nofunction NAME Do NOT output documentation for the given function(s); 70fadc0b31SJani Nikula only output documentation for the other functions and 71fadc0b31SJani Nikula DOC: sections. May be specified multiple times. 72fadc0b31SJani Nikula 73fadc0b31SJani NikulaOutput selection modifiers: 74fadc0b31SJani Nikula -no-doc-sections Do not output DOC: sections. 750b0f5f29SDaniel Vetter -enable-lineno Enable output of #define LINENO lines. Only works with 760b0f5f29SDaniel Vetter reStructuredText format. 7788c2b57dSJani Nikula -export-file FILE Specify an additional FILE in which to look for 7888c2b57dSJani Nikula EXPORT_SYMBOL() and EXPORT_SYMBOL_GPL(). To be used with 7988c2b57dSJani Nikula -export or -internal. May be specified multiple times. 80fadc0b31SJani Nikula 81fadc0b31SJani NikulaOther parameters: 82fadc0b31SJani Nikula -v Verbose output, more warnings and other information. 83fadc0b31SJani Nikula -h Print this help. 842c12c810SPierre-Louis Bossart -Werror Treat warnings as errors. 85fadc0b31SJani Nikula 86fadc0b31SJani NikulaEOF 87fadc0b31SJani Nikula print $message; 88fadc0b31SJani Nikula exit 1; 89fadc0b31SJani Nikula} 901da177e4SLinus Torvalds 911da177e4SLinus Torvalds# 921da177e4SLinus Torvalds# format of comments. 931da177e4SLinus Torvalds# In the following table, (...)? signifies optional structure. 941da177e4SLinus Torvalds# (...)* signifies 0 or more structure elements 951da177e4SLinus Torvalds# /** 961da177e4SLinus Torvalds# * function_name(:)? (- short description)? 971da177e4SLinus Torvalds# (* @parameterx: (description of parameter x)?)* 981da177e4SLinus Torvalds# (* a blank line)? 991da177e4SLinus Torvalds# * (Description:)? (Description of function)? 1001da177e4SLinus Torvalds# * (section header: (section description)? )* 1011da177e4SLinus Torvalds# (*)?*/ 1021da177e4SLinus Torvalds# 1031da177e4SLinus Torvalds# So .. the trivial example would be: 1041da177e4SLinus Torvalds# 1051da177e4SLinus Torvalds# /** 1061da177e4SLinus Torvalds# * my_function 107b9d97328SRandy Dunlap# */ 1081da177e4SLinus Torvalds# 109891dcd2fSRandy Dunlap# If the Description: header tag is omitted, then there must be a blank line 1101da177e4SLinus Torvalds# after the last parameter specification. 1111da177e4SLinus Torvalds# e.g. 1121da177e4SLinus Torvalds# /** 1131da177e4SLinus Torvalds# * my_function - does my stuff 1141da177e4SLinus Torvalds# * @my_arg: its mine damnit 1151da177e4SLinus Torvalds# * 1161da177e4SLinus Torvalds# * Does my stuff explained. 1171da177e4SLinus Torvalds# */ 1181da177e4SLinus Torvalds# 1191da177e4SLinus Torvalds# or, could also use: 1201da177e4SLinus Torvalds# /** 1211da177e4SLinus Torvalds# * my_function - does my stuff 1221da177e4SLinus Torvalds# * @my_arg: its mine damnit 1231da177e4SLinus Torvalds# * Description: Does my stuff explained. 1241da177e4SLinus Torvalds# */ 1251da177e4SLinus Torvalds# etc. 1261da177e4SLinus Torvalds# 127b9d97328SRandy Dunlap# Besides functions you can also write documentation for structs, unions, 1281da177e4SLinus Torvalds# enums and typedefs. Instead of the function name you must write the name 1291da177e4SLinus Torvalds# of the declaration; the struct/union/enum/typedef must always precede 1301da177e4SLinus Torvalds# the name. Nesting of declarations is not supported. 1311da177e4SLinus Torvalds# Use the argument mechanism to document members or constants. 1321da177e4SLinus Torvalds# e.g. 1331da177e4SLinus Torvalds# /** 1341da177e4SLinus Torvalds# * struct my_struct - short description 1351da177e4SLinus Torvalds# * @a: first member 1361da177e4SLinus Torvalds# * @b: second member 1371da177e4SLinus Torvalds# * 1381da177e4SLinus Torvalds# * Longer description 1391da177e4SLinus Torvalds# */ 1401da177e4SLinus Torvalds# struct my_struct { 1411da177e4SLinus Torvalds# int a; 1421da177e4SLinus Torvalds# int b; 143aeec46b9SMartin Waitz# /* private: */ 144aeec46b9SMartin Waitz# int c; 1451da177e4SLinus Torvalds# }; 1461da177e4SLinus Torvalds# 1471da177e4SLinus Torvalds# All descriptions can be multiline, except the short function description. 1481da177e4SLinus Torvalds# 149a4c6ebedSDanilo Cesar Lemes de Paula# For really longs structs, you can also describe arguments inside the 150a4c6ebedSDanilo Cesar Lemes de Paula# body of the struct. 151a4c6ebedSDanilo Cesar Lemes de Paula# eg. 152a4c6ebedSDanilo Cesar Lemes de Paula# /** 153a4c6ebedSDanilo Cesar Lemes de Paula# * struct my_struct - short description 154a4c6ebedSDanilo Cesar Lemes de Paula# * @a: first member 155a4c6ebedSDanilo Cesar Lemes de Paula# * @b: second member 156a4c6ebedSDanilo Cesar Lemes de Paula# * 157a4c6ebedSDanilo Cesar Lemes de Paula# * Longer description 158a4c6ebedSDanilo Cesar Lemes de Paula# */ 159a4c6ebedSDanilo Cesar Lemes de Paula# struct my_struct { 160a4c6ebedSDanilo Cesar Lemes de Paula# int a; 161a4c6ebedSDanilo Cesar Lemes de Paula# int b; 162a4c6ebedSDanilo Cesar Lemes de Paula# /** 163a4c6ebedSDanilo Cesar Lemes de Paula# * @c: This is longer description of C 164a4c6ebedSDanilo Cesar Lemes de Paula# * 165a4c6ebedSDanilo Cesar Lemes de Paula# * You can use paragraphs to describe arguments 166a4c6ebedSDanilo Cesar Lemes de Paula# * using this method. 167a4c6ebedSDanilo Cesar Lemes de Paula# */ 168a4c6ebedSDanilo Cesar Lemes de Paula# int c; 169a4c6ebedSDanilo Cesar Lemes de Paula# }; 170a4c6ebedSDanilo Cesar Lemes de Paula# 171a4c6ebedSDanilo Cesar Lemes de Paula# This should be use only for struct/enum members. 172a4c6ebedSDanilo Cesar Lemes de Paula# 1731da177e4SLinus Torvalds# You can also add additional sections. When documenting kernel functions you 1741da177e4SLinus Torvalds# should document the "Context:" of the function, e.g. whether the functions 1751da177e4SLinus Torvalds# can be called form interrupts. Unlike other sections you can end it with an 1761da177e4SLinus Torvalds# empty line. 1774092bac7SYacine Belkadi# A non-void function should have a "Return:" section describing the return 1784092bac7SYacine Belkadi# value(s). 1791da177e4SLinus Torvalds# Example-sections should contain the string EXAMPLE so that they are marked 1801da177e4SLinus Torvalds# appropriately in DocBook. 1811da177e4SLinus Torvalds# 1821da177e4SLinus Torvalds# Example: 1831da177e4SLinus Torvalds# /** 1841da177e4SLinus Torvalds# * user_function - function that can only be called in user context 1851da177e4SLinus Torvalds# * @a: some argument 1861da177e4SLinus Torvalds# * Context: !in_interrupt() 1871da177e4SLinus Torvalds# * 1881da177e4SLinus Torvalds# * Some description 1891da177e4SLinus Torvalds# * Example: 1901da177e4SLinus Torvalds# * user_function(22); 1911da177e4SLinus Torvalds# */ 1921da177e4SLinus Torvalds# ... 1931da177e4SLinus Torvalds# 1941da177e4SLinus Torvalds# 1951da177e4SLinus Torvalds# All descriptive text is further processed, scanning for the following special 1961da177e4SLinus Torvalds# patterns, which are highlighted appropriately. 1971da177e4SLinus Torvalds# 1981da177e4SLinus Torvalds# 'funcname()' - function 1991da177e4SLinus Torvalds# '$ENVVAR' - environmental variable 2001da177e4SLinus Torvalds# '&struct_name' - name of a structure (up to two words including 'struct') 2015267dd35SPaolo Bonzini# '&struct_name.member' - name of a structure member 2021da177e4SLinus Torvalds# '@parameter' - name of a parameter 2031da177e4SLinus Torvalds# '%CONST' - name of a constant. 204b97f193aSMauro Carvalho Chehab# '``LITERAL``' - literal string without any spaces on it. 2051da177e4SLinus Torvalds 2068484baaaSRandy Dunlap## init lots of data 2078484baaaSRandy Dunlap 2081da177e4SLinus Torvaldsmy $errors = 0; 2091da177e4SLinus Torvaldsmy $warnings = 0; 2105f8c7c98SRandy Dunlapmy $anon_struct_union = 0; 2111da177e4SLinus Torvalds 2121da177e4SLinus Torvalds# match expressions used to find embedded type information 213b97f193aSMauro Carvalho Chehabmy $type_constant = '\b``([^\`]+)``\b'; 214b97f193aSMauro Carvalho Chehabmy $type_constant2 = '\%([-_\w]+)'; 2151da177e4SLinus Torvaldsmy $type_func = '(\w+)\(\)'; 216bfd228c7SMike Rapoportmy $type_param = '\@(\w*((\.\w+)|(->\w+))*(\.\.\.)?)'; 217ee2aa759SMauro Carvalho Chehabmy $type_param_ref = '([\!]?)\@(\w*((\.\w+)|(->\w+))*(\.\.\.)?)'; 2185219f18aSJonathan Corbetmy $type_fp_param = '\@(\w+)\(\)'; # Special RST handling for func ptr params 219346282dbSMauro Carvalho Chehabmy $type_fp_param2 = '\@(\w+->\S+)\(\)'; # Special RST handling for structs with func ptr params 2201da177e4SLinus Torvaldsmy $type_env = '(\$\w+)'; 221df31175bSPaolo Bonzinimy $type_enum = '\&(enum\s*([_\w]+))'; 222df31175bSPaolo Bonzinimy $type_struct = '\&(struct\s*([_\w]+))'; 223df31175bSPaolo Bonzinimy $type_typedef = '\&(typedef\s*([_\w]+))'; 224df31175bSPaolo Bonzinimy $type_union = '\&(union\s*([_\w]+))'; 2255267dd35SPaolo Bonzinimy $type_member = '\&([_\w]+)(\.|->)([_\w]+)'; 226df31175bSPaolo Bonzinimy $type_fallback = '\&([_\w]+)'; 227f3341dcfSJani Nikulamy $type_member_func = $type_member . '\(\)'; 2281da177e4SLinus Torvalds 2291da177e4SLinus Torvalds# Output conversion substitutions. 2301da177e4SLinus Torvalds# One for each output format 2311da177e4SLinus Torvalds 2321da177e4SLinus Torvalds# these are pretty rough 2334d732701SDanilo Cesar Lemes de Paulamy @highlights_man = ( 2344d732701SDanilo Cesar Lemes de Paula [$type_constant, "\$1"], 235b97f193aSMauro Carvalho Chehab [$type_constant2, "\$1"], 2364d732701SDanilo Cesar Lemes de Paula [$type_func, "\\\\fB\$1\\\\fP"], 237df31175bSPaolo Bonzini [$type_enum, "\\\\fI\$1\\\\fP"], 2384d732701SDanilo Cesar Lemes de Paula [$type_struct, "\\\\fI\$1\\\\fP"], 239df31175bSPaolo Bonzini [$type_typedef, "\\\\fI\$1\\\\fP"], 240df31175bSPaolo Bonzini [$type_union, "\\\\fI\$1\\\\fP"], 2415267dd35SPaolo Bonzini [$type_param, "\\\\fI\$1\\\\fP"], 242ee2aa759SMauro Carvalho Chehab [$type_param_ref, "\\\\fI\$1\$2\\\\fP"], 243df31175bSPaolo Bonzini [$type_member, "\\\\fI\$1\$2\$3\\\\fP"], 244df31175bSPaolo Bonzini [$type_fallback, "\\\\fI\$1\\\\fP"] 2454d732701SDanilo Cesar Lemes de Paula ); 2461da177e4SLinus Torvaldsmy $blankline_man = ""; 2471da177e4SLinus Torvalds 248c0d1b6eeSJonathan Corbet# rst-mode 249c0d1b6eeSJonathan Corbetmy @highlights_rst = ( 250c0d1b6eeSJonathan Corbet [$type_constant, "``\$1``"], 251b97f193aSMauro Carvalho Chehab [$type_constant2, "``\$1``"], 252f3341dcfSJani Nikula # Note: need to escape () to avoid func matching later 2535267dd35SPaolo Bonzini [$type_member_func, "\\:c\\:type\\:`\$1\$2\$3\\\\(\\\\) <\$1>`"], 2545267dd35SPaolo Bonzini [$type_member, "\\:c\\:type\\:`\$1\$2\$3 <\$1>`"], 2555219f18aSJonathan Corbet [$type_fp_param, "**\$1\\\\(\\\\)**"], 256346282dbSMauro Carvalho Chehab [$type_fp_param2, "**\$1\\\\(\\\\)**"], 257344fdb28SJonathan Corbet [$type_func, "\$1()"], 258df31175bSPaolo Bonzini [$type_enum, "\\:c\\:type\\:`\$1 <\$2>`"], 259df31175bSPaolo Bonzini [$type_struct, "\\:c\\:type\\:`\$1 <\$2>`"], 260df31175bSPaolo Bonzini [$type_typedef, "\\:c\\:type\\:`\$1 <\$2>`"], 261df31175bSPaolo Bonzini [$type_union, "\\:c\\:type\\:`\$1 <\$2>`"], 262a7291e7eSJani Nikula # in rst this can refer to any type 263df31175bSPaolo Bonzini [$type_fallback, "\\:c\\:type\\:`\$1`"], 264ee2aa759SMauro Carvalho Chehab [$type_param_ref, "**\$1\$2**"] 265c0d1b6eeSJonathan Corbet ); 266c0d1b6eeSJonathan Corbetmy $blankline_rst = "\n"; 267c0d1b6eeSJonathan Corbet 2681da177e4SLinus Torvalds# read arguments 2691da177e4SLinus Torvaldsif ($#ARGV == -1) { 2701da177e4SLinus Torvalds usage(); 2711da177e4SLinus Torvalds} 2721da177e4SLinus Torvalds 2738484baaaSRandy Dunlapmy $kernelversion; 2748484baaaSRandy Dunlapmy $dohighlight = ""; 2758484baaaSRandy Dunlap 2761da177e4SLinus Torvaldsmy $verbose = 0; 2772c12c810SPierre-Louis Bossartmy $Werror = 0; 278bdfe2be3SMauro Carvalho Chehabmy $output_mode = "rst"; 279e314ba31SDaniel Santosmy $output_preformatted = 0; 2804b44595aSJohannes Bergmy $no_doc_sections = 0; 2810b0f5f29SDaniel Vettermy $enable_lineno = 0; 282bdfe2be3SMauro Carvalho Chehabmy @highlights = @highlights_rst; 283bdfe2be3SMauro Carvalho Chehabmy $blankline = $blankline_rst; 2841da177e4SLinus Torvaldsmy $modulename = "Kernel API"; 285b6c3f456SJani Nikula 286b6c3f456SJani Nikulause constant { 287b6c3f456SJani Nikula OUTPUT_ALL => 0, # output all symbols and doc sections 288b6c3f456SJani Nikula OUTPUT_INCLUDE => 1, # output only specified symbols 289b6c3f456SJani Nikula OUTPUT_EXCLUDE => 2, # output everything except specified symbols 290b6c3f456SJani Nikula OUTPUT_EXPORTED => 3, # output exported symbols 291b6c3f456SJani Nikula OUTPUT_INTERNAL => 4, # output non-exported symbols 292b6c3f456SJani Nikula}; 293b6c3f456SJani Nikulamy $output_selection = OUTPUT_ALL; 294b0d60bfbSJonathan Corbetmy $show_not_found = 0; # No longer used 295b2c4105bSBen Hutchings 29688c2b57dSJani Nikulamy @export_file_list; 29788c2b57dSJani Nikula 298b2c4105bSBen Hutchingsmy @build_time; 299b2c4105bSBen Hutchingsif (defined($ENV{'KBUILD_BUILD_TIMESTAMP'}) && 300b2c4105bSBen Hutchings (my $seconds = `date -d"${ENV{'KBUILD_BUILD_TIMESTAMP'}}" +%s`) ne '') { 301b2c4105bSBen Hutchings @build_time = gmtime($seconds); 302b2c4105bSBen Hutchings} else { 303b2c4105bSBen Hutchings @build_time = localtime; 304b2c4105bSBen Hutchings} 305b2c4105bSBen Hutchings 3061da177e4SLinus Torvaldsmy $man_date = ('January', 'February', 'March', 'April', 'May', 'June', 3071da177e4SLinus Torvalds 'July', 'August', 'September', 'October', 308b2c4105bSBen Hutchings 'November', 'December')[$build_time[4]] . 309b2c4105bSBen Hutchings " " . ($build_time[5]+1900); 3101da177e4SLinus Torvalds 3118484baaaSRandy Dunlap# Essentially these are globals. 312b9d97328SRandy Dunlap# They probably want to be tidied up, made more localised or something. 313b9d97328SRandy Dunlap# CAVEAT EMPTOR! Some of the others I localised may not want to be, which 3141da177e4SLinus Torvalds# could cause "use of undefined value" or other bugs. 3151da177e4SLinus Torvaldsmy ($function, %function_table, %parametertypes, $declaration_purpose); 3160b0f5f29SDaniel Vettermy $declaration_start_line; 3171da177e4SLinus Torvaldsmy ($type, $declaration_name, $return_type); 3181c32fd0cSIlya Dryomovmy ($newsection, $newcontents, $prototype, $brcount, %source_map); 3191da177e4SLinus Torvalds 320bd0e88e5SRandy Dunlapif (defined($ENV{'KBUILD_VERBOSE'})) { 321bd0e88e5SRandy Dunlap $verbose = "$ENV{'KBUILD_VERBOSE'}"; 322bd0e88e5SRandy Dunlap} 323bd0e88e5SRandy Dunlap 3242c12c810SPierre-Louis Bossartif (defined($ENV{'KDOC_WERROR'})) { 3252c12c810SPierre-Louis Bossart $Werror = "$ENV{'KDOC_WERROR'}"; 3262c12c810SPierre-Louis Bossart} 3272c12c810SPierre-Louis Bossart 3282c12c810SPierre-Louis Bossartif (defined($ENV{'KCFLAGS'})) { 3292c12c810SPierre-Louis Bossart my $kcflags = "$ENV{'KCFLAGS'}"; 3302c12c810SPierre-Louis Bossart 3312c12c810SPierre-Louis Bossart if ($kcflags =~ /Werror/) { 3322c12c810SPierre-Louis Bossart $Werror = 1; 3332c12c810SPierre-Louis Bossart } 3342c12c810SPierre-Louis Bossart} 3352c12c810SPierre-Louis Bossart 3361da177e4SLinus Torvalds# Generated docbook code is inserted in a template at a point where 3371da177e4SLinus Torvalds# docbook v3.1 requires a non-zero sequence of RefEntry's; see: 33893431e06SAlexander A. Klimov# https://www.oasis-open.org/docbook/documentation/reference/html/refentry.html 3391da177e4SLinus Torvalds# We keep track of number of generated entries and generate a dummy 3401da177e4SLinus Torvalds# if needs be to ensure the expanded template can be postprocessed 3411da177e4SLinus Torvalds# into html. 3421da177e4SLinus Torvaldsmy $section_counter = 0; 3431da177e4SLinus Torvalds 3441da177e4SLinus Torvaldsmy $lineprefix=""; 3451da177e4SLinus Torvalds 34648af606aSJani Nikula# Parser states 34748af606aSJani Nikulause constant { 34848af606aSJani Nikula STATE_NORMAL => 0, # normal code 34948af606aSJani Nikula STATE_NAME => 1, # looking for function name 35017b78717SJonathan Corbet STATE_BODY_MAYBE => 2, # body - or maybe more description 35117b78717SJonathan Corbet STATE_BODY => 3, # the body of the comment 3520d55d48bSMauro Carvalho Chehab STATE_BODY_WITH_BLANK_LINE => 4, # the body, which has a blank line 3530d55d48bSMauro Carvalho Chehab STATE_PROTO => 5, # scanning prototype 3540d55d48bSMauro Carvalho Chehab STATE_DOCBLOCK => 6, # documentation block 3550d55d48bSMauro Carvalho Chehab STATE_INLINE => 7, # gathering doc outside main block 35648af606aSJani Nikula}; 3571da177e4SLinus Torvaldsmy $state; 358850622dfSRandy Dunlapmy $in_doc_sect; 359d742f24dSJonathan Corbetmy $leading_space; 3601da177e4SLinus Torvalds 36148af606aSJani Nikula# Inline documentation state 36248af606aSJani Nikulause constant { 36348af606aSJani Nikula STATE_INLINE_NA => 0, # not applicable ($state != STATE_INLINE) 36448af606aSJani Nikula STATE_INLINE_NAME => 1, # looking for member name (@foo:) 36548af606aSJani Nikula STATE_INLINE_TEXT => 2, # looking for member documentation 36648af606aSJani Nikula STATE_INLINE_END => 3, # done 36748af606aSJani Nikula STATE_INLINE_ERROR => 4, # error - Comment without header was found. 36848af606aSJani Nikula # Spit a warning as it's not 369a4c6ebedSDanilo Cesar Lemes de Paula # proper kernel-doc and ignore the rest. 37048af606aSJani Nikula}; 37148af606aSJani Nikulamy $inline_doc_state; 372a4c6ebedSDanilo Cesar Lemes de Paula 3731da177e4SLinus Torvalds#declaration types: can be 3741da177e4SLinus Torvalds# 'function', 'struct', 'union', 'enum', 'typedef' 3751da177e4SLinus Torvaldsmy $decl_type; 3761da177e4SLinus Torvalds 3771da177e4SLinus Torvaldsmy $doc_start = '^/\*\*\s*$'; # Allow whitespace at end of comment start. 3781da177e4SLinus Torvaldsmy $doc_end = '\*/'; 3791da177e4SLinus Torvaldsmy $doc_com = '\s*\*\s*'; 38012ae6779SDaniel Santosmy $doc_com_body = '\s*\* ?'; 3811da177e4SLinus Torvaldsmy $doc_decl = $doc_com . '(\w+)'; 382f624adefSJani Nikula# @params and a strictly limited set of supported section names 3838569de68SJonathan Corbetmy $doc_sect = $doc_com . 384ef00028bSJonathan Corbet '\s*(\@[.\w]+|\@\.\.\.|description|context|returns?|notes?|examples?)\s*:(.*)'; 38512ae6779SDaniel Santosmy $doc_content = $doc_com_body . '(.*)'; 3861da177e4SLinus Torvaldsmy $doc_block = $doc_com . 'DOC:\s*(.*)?'; 38748af606aSJani Nikulamy $doc_inline_start = '^\s*/\*\*\s*$'; 388fe7bc493SMauro Carvalho Chehabmy $doc_inline_sect = '\s*\*\s*(@\s*[\w][\w\.]*\s*):(.*)'; 38948af606aSJani Nikulamy $doc_inline_end = '^\s*\*/\s*$'; 3900c9aa209SJani Nikulamy $doc_inline_oneline = '^\s*/\*\*\s*(@[\w\s]+):\s*(.*)\s*\*/\s*$'; 39186ae2e38SJani Nikulamy $export_symbol = '^\s*EXPORT_SYMBOL(_GPL)?\s*\(\s*(\w+)\s*\)\s*;'; 3921da177e4SLinus Torvalds 3931da177e4SLinus Torvaldsmy %parameterdescs; 3940b0f5f29SDaniel Vettermy %parameterdesc_start_lines; 3951da177e4SLinus Torvaldsmy @parameterlist; 3961da177e4SLinus Torvaldsmy %sections; 3971da177e4SLinus Torvaldsmy @sectionlist; 3980b0f5f29SDaniel Vettermy %section_start_lines; 399a1d94aa5SRandy Dunlapmy $sectcheck; 400a1d94aa5SRandy Dunlapmy $struct_actual; 4011da177e4SLinus Torvalds 4021da177e4SLinus Torvaldsmy $contents = ""; 4030b0f5f29SDaniel Vettermy $new_start_line = 0; 404f624adefSJani Nikula 405f624adefSJani Nikula# the canonical section names. see also $doc_sect above. 4061da177e4SLinus Torvaldsmy $section_default = "Description"; # default section 4071da177e4SLinus Torvaldsmy $section_intro = "Introduction"; 4081da177e4SLinus Torvaldsmy $section = $section_default; 4091da177e4SLinus Torvaldsmy $section_context = "Context"; 4104092bac7SYacine Belkadimy $section_return = "Return"; 4111da177e4SLinus Torvalds 4121da177e4SLinus Torvaldsmy $undescribed = "-- undescribed --"; 4131da177e4SLinus Torvalds 4141da177e4SLinus Torvaldsreset_state(); 4151da177e4SLinus Torvalds 416b031ac4eSMauro Carvalho Chehabwhile ($ARGV[0] =~ m/^--?(.*)/) { 417b031ac4eSMauro Carvalho Chehab my $cmd = $1; 418b031ac4eSMauro Carvalho Chehab shift @ARGV; 419b031ac4eSMauro Carvalho Chehab if ($cmd eq "man") { 4201da177e4SLinus Torvalds $output_mode = "man"; 4214d732701SDanilo Cesar Lemes de Paula @highlights = @highlights_man; 4221da177e4SLinus Torvalds $blankline = $blankline_man; 423b031ac4eSMauro Carvalho Chehab } elsif ($cmd eq "rst") { 424c0d1b6eeSJonathan Corbet $output_mode = "rst"; 425c0d1b6eeSJonathan Corbet @highlights = @highlights_rst; 426c0d1b6eeSJonathan Corbet $blankline = $blankline_rst; 427b031ac4eSMauro Carvalho Chehab } elsif ($cmd eq "none") { 4283a025e1dSMatthew Wilcox $output_mode = "none"; 429b031ac4eSMauro Carvalho Chehab } elsif ($cmd eq "module") { # not needed for XML, inherits from calling document 4301da177e4SLinus Torvalds $modulename = shift @ARGV; 431b031ac4eSMauro Carvalho Chehab } elsif ($cmd eq "function") { # to only output specific functions 432b6c3f456SJani Nikula $output_selection = OUTPUT_INCLUDE; 4331da177e4SLinus Torvalds $function = shift @ARGV; 4341da177e4SLinus Torvalds $function_table{$function} = 1; 435b031ac4eSMauro Carvalho Chehab } elsif ($cmd eq "nofunction") { # output all except specific functions 436b6c3f456SJani Nikula $output_selection = OUTPUT_EXCLUDE; 4371da177e4SLinus Torvalds $function = shift @ARGV; 4381da177e4SLinus Torvalds $function_table{$function} = 1; 439b031ac4eSMauro Carvalho Chehab } elsif ($cmd eq "export") { # only exported symbols 440b6c3f456SJani Nikula $output_selection = OUTPUT_EXPORTED; 441da9726ecSJani Nikula %function_table = (); 442b031ac4eSMauro Carvalho Chehab } elsif ($cmd eq "internal") { # only non-exported symbols 443b6c3f456SJani Nikula $output_selection = OUTPUT_INTERNAL; 444da9726ecSJani Nikula %function_table = (); 445b031ac4eSMauro Carvalho Chehab } elsif ($cmd eq "export-file") { 44688c2b57dSJani Nikula my $file = shift @ARGV; 44788c2b57dSJani Nikula push(@export_file_list, $file); 448b031ac4eSMauro Carvalho Chehab } elsif ($cmd eq "v") { 4491da177e4SLinus Torvalds $verbose = 1; 4502c12c810SPierre-Louis Bossart } elsif ($cmd eq "Werror") { 4512c12c810SPierre-Louis Bossart $Werror = 1; 452b031ac4eSMauro Carvalho Chehab } elsif (($cmd eq "h") || ($cmd eq "help")) { 4531da177e4SLinus Torvalds usage(); 454b031ac4eSMauro Carvalho Chehab } elsif ($cmd eq 'no-doc-sections') { 4554b44595aSJohannes Berg $no_doc_sections = 1; 456b031ac4eSMauro Carvalho Chehab } elsif ($cmd eq 'enable-lineno') { 4570b0f5f29SDaniel Vetter $enable_lineno = 1; 458b031ac4eSMauro Carvalho Chehab } elsif ($cmd eq 'show-not-found') { 459b0d60bfbSJonathan Corbet $show_not_found = 1; # A no-op but don't fail 460b031ac4eSMauro Carvalho Chehab } else { 461b031ac4eSMauro Carvalho Chehab # Unknown argument 462b031ac4eSMauro Carvalho Chehab usage(); 4631da177e4SLinus Torvalds } 4641da177e4SLinus Torvalds} 4651da177e4SLinus Torvalds 4668484baaaSRandy Dunlap# continue execution near EOF; 4678484baaaSRandy Dunlap 46853f049faSBorislav Petkov# get kernel version from env 46953f049faSBorislav Petkovsub get_kernel_version() { 4701b9bc22dSJohannes Berg my $version = 'unknown kernel version'; 47153f049faSBorislav Petkov 47253f049faSBorislav Petkov if (defined($ENV{'KERNELVERSION'})) { 47353f049faSBorislav Petkov $version = $ENV{'KERNELVERSION'}; 47453f049faSBorislav Petkov } 47553f049faSBorislav Petkov return $version; 47653f049faSBorislav Petkov} 4771da177e4SLinus Torvalds 4780b0f5f29SDaniel Vetter# 4790b0f5f29SDaniel Vettersub print_lineno { 4800b0f5f29SDaniel Vetter my $lineno = shift; 4810b0f5f29SDaniel Vetter if ($enable_lineno && defined($lineno)) { 4820b0f5f29SDaniel Vetter print "#define LINENO " . $lineno . "\n"; 4830b0f5f29SDaniel Vetter } 4840b0f5f29SDaniel Vetter} 4851da177e4SLinus Torvalds## 4861da177e4SLinus Torvalds# dumps section contents to arrays/hashes intended for that purpose. 4871da177e4SLinus Torvalds# 4881da177e4SLinus Torvaldssub dump_section { 48994dc7ad5SRandy Dunlap my $file = shift; 4901da177e4SLinus Torvalds my $name = shift; 4911da177e4SLinus Torvalds my $contents = join "\n", @_; 4921da177e4SLinus Torvalds 49313901ef2SJani Nikula if ($name =~ m/$type_param/) { 4941da177e4SLinus Torvalds $name = $1; 4951da177e4SLinus Torvalds $parameterdescs{$name} = $contents; 496a1d94aa5SRandy Dunlap $sectcheck = $sectcheck . $name . " "; 4970b0f5f29SDaniel Vetter $parameterdesc_start_lines{$name} = $new_start_line; 4980b0f5f29SDaniel Vetter $new_start_line = 0; 499ced69090SRandy Dunlap } elsif ($name eq "@\.\.\.") { 500ced69090SRandy Dunlap $name = "..."; 501ced69090SRandy Dunlap $parameterdescs{$name} = $contents; 502a1d94aa5SRandy Dunlap $sectcheck = $sectcheck . $name . " "; 5030b0f5f29SDaniel Vetter $parameterdesc_start_lines{$name} = $new_start_line; 5040b0f5f29SDaniel Vetter $new_start_line = 0; 5051da177e4SLinus Torvalds } else { 50694dc7ad5SRandy Dunlap if (defined($sections{$name}) && ($sections{$name} ne "")) { 50795b6be9dSJani Nikula # Only warn on user specified duplicate section names. 50895b6be9dSJani Nikula if ($name ne $section_default) { 50932217761SJani Nikula print STDERR "${file}:$.: warning: duplicate section name '$name'\n"; 51032217761SJani Nikula ++$warnings; 51195b6be9dSJani Nikula } 51232217761SJani Nikula $sections{$name} .= $contents; 51332217761SJani Nikula } else { 5141da177e4SLinus Torvalds $sections{$name} = $contents; 5151da177e4SLinus Torvalds push @sectionlist, $name; 5160b0f5f29SDaniel Vetter $section_start_lines{$name} = $new_start_line; 5170b0f5f29SDaniel Vetter $new_start_line = 0; 5181da177e4SLinus Torvalds } 5191da177e4SLinus Torvalds } 52032217761SJani Nikula} 5211da177e4SLinus Torvalds 5221da177e4SLinus Torvalds## 523b112e0f7SJohannes Berg# dump DOC: section after checking that it should go out 524b112e0f7SJohannes Berg# 525b112e0f7SJohannes Bergsub dump_doc_section { 52694dc7ad5SRandy Dunlap my $file = shift; 527b112e0f7SJohannes Berg my $name = shift; 528b112e0f7SJohannes Berg my $contents = join "\n", @_; 529b112e0f7SJohannes Berg 5304b44595aSJohannes Berg if ($no_doc_sections) { 5314b44595aSJohannes Berg return; 5324b44595aSJohannes Berg } 5334b44595aSJohannes Berg 534b6c3f456SJani Nikula if (($output_selection == OUTPUT_ALL) || 535b6c3f456SJani Nikula ($output_selection == OUTPUT_INCLUDE && 536b6c3f456SJani Nikula defined($function_table{$name})) || 537b6c3f456SJani Nikula ($output_selection == OUTPUT_EXCLUDE && 538b6c3f456SJani Nikula !defined($function_table{$name}))) 539b112e0f7SJohannes Berg { 54094dc7ad5SRandy Dunlap dump_section($file, $name, $contents); 541b112e0f7SJohannes Berg output_blockhead({'sectionlist' => \@sectionlist, 542b112e0f7SJohannes Berg 'sections' => \%sections, 543b112e0f7SJohannes Berg 'module' => $modulename, 544b6c3f456SJani Nikula 'content-only' => ($output_selection != OUTPUT_ALL), }); 545b112e0f7SJohannes Berg } 546b112e0f7SJohannes Berg} 547b112e0f7SJohannes Berg 548b112e0f7SJohannes Berg## 5491da177e4SLinus Torvalds# output function 5501da177e4SLinus Torvalds# 5511da177e4SLinus Torvalds# parameterdescs, a hash. 5521da177e4SLinus Torvalds# function => "function name" 5531da177e4SLinus Torvalds# parameterlist => @list of parameters 5541da177e4SLinus Torvalds# parameterdescs => %parameter descriptions 5551da177e4SLinus Torvalds# sectionlist => @list of sections 556a21217daSRandy Dunlap# sections => %section descriptions 5571da177e4SLinus Torvalds# 5581da177e4SLinus Torvalds 5591da177e4SLinus Torvaldssub output_highlight { 5601da177e4SLinus Torvalds my $contents = join "\n",@_; 5611da177e4SLinus Torvalds my $line; 5621da177e4SLinus Torvalds 5631da177e4SLinus Torvalds# DEBUG 5641da177e4SLinus Torvalds# if (!defined $contents) { 5651da177e4SLinus Torvalds# use Carp; 5661da177e4SLinus Torvalds# confess "output_highlight got called with no args?\n"; 5671da177e4SLinus Torvalds# } 5681da177e4SLinus Torvalds 5693eb014a1SRandy Dunlap# print STDERR "contents b4:$contents\n"; 5701da177e4SLinus Torvalds eval $dohighlight; 5711da177e4SLinus Torvalds die $@ if $@; 5723eb014a1SRandy Dunlap# print STDERR "contents af:$contents\n"; 5733eb014a1SRandy Dunlap 5741da177e4SLinus Torvalds foreach $line (split "\n", $contents) { 57512ae6779SDaniel Santos if (! $output_preformatted) { 57612ae6779SDaniel Santos $line =~ s/^\s*//; 57712ae6779SDaniel Santos } 5781da177e4SLinus Torvalds if ($line eq ""){ 579e314ba31SDaniel Santos if (! $output_preformatted) { 5800bba924cSJonathan Corbet print $lineprefix, $blankline; 581e314ba31SDaniel Santos } 5821da177e4SLinus Torvalds } else { 583cdccb316SRandy Dunlap if ($output_mode eq "man" && substr($line, 0, 1) eq ".") { 584cdccb316SRandy Dunlap print "\\&$line"; 585cdccb316SRandy Dunlap } else { 5861da177e4SLinus Torvalds print $lineprefix, $line; 5871da177e4SLinus Torvalds } 588cdccb316SRandy Dunlap } 5891da177e4SLinus Torvalds print "\n"; 5901da177e4SLinus Torvalds } 5911da177e4SLinus Torvalds} 5921da177e4SLinus Torvalds 5931da177e4SLinus Torvalds## 5941da177e4SLinus Torvalds# output function in man 5951da177e4SLinus Torvaldssub output_function_man(%) { 5961da177e4SLinus Torvalds my %args = %{$_[0]}; 5971da177e4SLinus Torvalds my ($parameter, $section); 5981da177e4SLinus Torvalds my $count; 5991da177e4SLinus Torvalds 6001da177e4SLinus Torvalds print ".TH \"$args{'function'}\" 9 \"$args{'function'}\" \"$man_date\" \"Kernel Hacker's Manual\" LINUX\n"; 6011da177e4SLinus Torvalds 6021da177e4SLinus Torvalds print ".SH NAME\n"; 6031da177e4SLinus Torvalds print $args{'function'} . " \\- " . $args{'purpose'} . "\n"; 6041da177e4SLinus Torvalds 6051da177e4SLinus Torvalds print ".SH SYNOPSIS\n"; 606a21217daSRandy Dunlap if ($args{'functiontype'} ne "") { 6071da177e4SLinus Torvalds print ".B \"" . $args{'functiontype'} . "\" " . $args{'function'} . "\n"; 608a21217daSRandy Dunlap } else { 609a21217daSRandy Dunlap print ".B \"" . $args{'function'} . "\n"; 610a21217daSRandy Dunlap } 6111da177e4SLinus Torvalds $count = 0; 6121da177e4SLinus Torvalds my $parenth = "("; 6131da177e4SLinus Torvalds my $post = ","; 6141da177e4SLinus Torvalds foreach my $parameter (@{$args{'parameterlist'}}) { 6151da177e4SLinus Torvalds if ($count == $#{$args{'parameterlist'}}) { 6161da177e4SLinus Torvalds $post = ");"; 6171da177e4SLinus Torvalds } 6181da177e4SLinus Torvalds $type = $args{'parametertypes'}{$parameter}; 6191da177e4SLinus Torvalds if ($type =~ m/([^\(]*\(\*)\s*\)\s*\(([^\)]*)\)/) { 6201da177e4SLinus Torvalds # pointer-to-function 6211da177e4SLinus Torvalds print ".BI \"" . $parenth . $1 . "\" " . $parameter . " \") (" . $2 . ")" . $post . "\"\n"; 6221da177e4SLinus Torvalds } else { 6231da177e4SLinus Torvalds $type =~ s/([^\*])$/$1 /; 6241da177e4SLinus Torvalds print ".BI \"" . $parenth . $type . "\" " . $parameter . " \"" . $post . "\"\n"; 6251da177e4SLinus Torvalds } 6261da177e4SLinus Torvalds $count++; 6271da177e4SLinus Torvalds $parenth = ""; 6281da177e4SLinus Torvalds } 6291da177e4SLinus Torvalds 6301da177e4SLinus Torvalds print ".SH ARGUMENTS\n"; 6311da177e4SLinus Torvalds foreach $parameter (@{$args{'parameterlist'}}) { 6321da177e4SLinus Torvalds my $parameter_name = $parameter; 6331da177e4SLinus Torvalds $parameter_name =~ s/\[.*//; 6341da177e4SLinus Torvalds 6351da177e4SLinus Torvalds print ".IP \"" . $parameter . "\" 12\n"; 6361da177e4SLinus Torvalds output_highlight($args{'parameterdescs'}{$parameter_name}); 6371da177e4SLinus Torvalds } 6381da177e4SLinus Torvalds foreach $section (@{$args{'sectionlist'}}) { 6391da177e4SLinus Torvalds print ".SH \"", uc $section, "\"\n"; 6401da177e4SLinus Torvalds output_highlight($args{'sections'}{$section}); 6411da177e4SLinus Torvalds } 6421da177e4SLinus Torvalds} 6431da177e4SLinus Torvalds 6441da177e4SLinus Torvalds## 6451da177e4SLinus Torvalds# output enum in man 6461da177e4SLinus Torvaldssub output_enum_man(%) { 6471da177e4SLinus Torvalds my %args = %{$_[0]}; 6481da177e4SLinus Torvalds my ($parameter, $section); 6491da177e4SLinus Torvalds my $count; 6501da177e4SLinus Torvalds 6511da177e4SLinus Torvalds print ".TH \"$args{'module'}\" 9 \"enum $args{'enum'}\" \"$man_date\" \"API Manual\" LINUX\n"; 6521da177e4SLinus Torvalds 6531da177e4SLinus Torvalds print ".SH NAME\n"; 6541da177e4SLinus Torvalds print "enum " . $args{'enum'} . " \\- " . $args{'purpose'} . "\n"; 6551da177e4SLinus Torvalds 6561da177e4SLinus Torvalds print ".SH SYNOPSIS\n"; 6571da177e4SLinus Torvalds print "enum " . $args{'enum'} . " {\n"; 6581da177e4SLinus Torvalds $count = 0; 6591da177e4SLinus Torvalds foreach my $parameter (@{$args{'parameterlist'}}) { 6601da177e4SLinus Torvalds print ".br\n.BI \" $parameter\"\n"; 6611da177e4SLinus Torvalds if ($count == $#{$args{'parameterlist'}}) { 6621da177e4SLinus Torvalds print "\n};\n"; 6631da177e4SLinus Torvalds last; 6641da177e4SLinus Torvalds } 6651da177e4SLinus Torvalds else { 6661da177e4SLinus Torvalds print ", \n.br\n"; 6671da177e4SLinus Torvalds } 6681da177e4SLinus Torvalds $count++; 6691da177e4SLinus Torvalds } 6701da177e4SLinus Torvalds 6711da177e4SLinus Torvalds print ".SH Constants\n"; 6721da177e4SLinus Torvalds foreach $parameter (@{$args{'parameterlist'}}) { 6731da177e4SLinus Torvalds my $parameter_name = $parameter; 6741da177e4SLinus Torvalds $parameter_name =~ s/\[.*//; 6751da177e4SLinus Torvalds 6761da177e4SLinus Torvalds print ".IP \"" . $parameter . "\" 12\n"; 6771da177e4SLinus Torvalds output_highlight($args{'parameterdescs'}{$parameter_name}); 6781da177e4SLinus Torvalds } 6791da177e4SLinus Torvalds foreach $section (@{$args{'sectionlist'}}) { 6801da177e4SLinus Torvalds print ".SH \"$section\"\n"; 6811da177e4SLinus Torvalds output_highlight($args{'sections'}{$section}); 6821da177e4SLinus Torvalds } 6831da177e4SLinus Torvalds} 6841da177e4SLinus Torvalds 6851da177e4SLinus Torvalds## 6861da177e4SLinus Torvalds# output struct in man 6871da177e4SLinus Torvaldssub output_struct_man(%) { 6881da177e4SLinus Torvalds my %args = %{$_[0]}; 6891da177e4SLinus Torvalds my ($parameter, $section); 6901da177e4SLinus Torvalds 6911da177e4SLinus Torvalds print ".TH \"$args{'module'}\" 9 \"" . $args{'type'} . " " . $args{'struct'} . "\" \"$man_date\" \"API Manual\" LINUX\n"; 6921da177e4SLinus Torvalds 6931da177e4SLinus Torvalds print ".SH NAME\n"; 6941da177e4SLinus Torvalds print $args{'type'} . " " . $args{'struct'} . " \\- " . $args{'purpose'} . "\n"; 6951da177e4SLinus Torvalds 6968ad72163SMauro Carvalho Chehab my $declaration = $args{'definition'}; 6978ad72163SMauro Carvalho Chehab $declaration =~ s/\t/ /g; 6988ad72163SMauro Carvalho Chehab $declaration =~ s/\n/"\n.br\n.BI \"/g; 6991da177e4SLinus Torvalds print ".SH SYNOPSIS\n"; 7001da177e4SLinus Torvalds print $args{'type'} . " " . $args{'struct'} . " {\n.br\n"; 7018ad72163SMauro Carvalho Chehab print ".BI \"$declaration\n};\n.br\n\n"; 7021da177e4SLinus Torvalds 703c51d3dacSRandy Dunlap print ".SH Members\n"; 7041da177e4SLinus Torvalds foreach $parameter (@{$args{'parameterlist'}}) { 7051da177e4SLinus Torvalds ($parameter =~ /^#/) && next; 7061da177e4SLinus Torvalds 7071da177e4SLinus Torvalds my $parameter_name = $parameter; 7081da177e4SLinus Torvalds $parameter_name =~ s/\[.*//; 7091da177e4SLinus Torvalds 7101da177e4SLinus Torvalds ($args{'parameterdescs'}{$parameter_name} ne $undescribed) || next; 7111da177e4SLinus Torvalds print ".IP \"" . $parameter . "\" 12\n"; 7121da177e4SLinus Torvalds output_highlight($args{'parameterdescs'}{$parameter_name}); 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## 7211da177e4SLinus Torvalds# output typedef in man 7221da177e4SLinus Torvaldssub output_typedef_man(%) { 7231da177e4SLinus Torvalds my %args = %{$_[0]}; 7241da177e4SLinus Torvalds my ($parameter, $section); 7251da177e4SLinus Torvalds 7261da177e4SLinus Torvalds print ".TH \"$args{'module'}\" 9 \"$args{'typedef'}\" \"$man_date\" \"API Manual\" LINUX\n"; 7271da177e4SLinus Torvalds 7281da177e4SLinus Torvalds print ".SH NAME\n"; 7291da177e4SLinus Torvalds print "typedef " . $args{'typedef'} . " \\- " . $args{'purpose'} . "\n"; 7301da177e4SLinus Torvalds 7311da177e4SLinus Torvalds foreach $section (@{$args{'sectionlist'}}) { 7321da177e4SLinus Torvalds print ".SH \"$section\"\n"; 7331da177e4SLinus Torvalds output_highlight($args{'sections'}{$section}); 7341da177e4SLinus Torvalds } 7351da177e4SLinus Torvalds} 7361da177e4SLinus Torvalds 737b112e0f7SJohannes Bergsub output_blockhead_man(%) { 7381da177e4SLinus Torvalds my %args = %{$_[0]}; 7391da177e4SLinus Torvalds my ($parameter, $section); 7401da177e4SLinus Torvalds my $count; 7411da177e4SLinus Torvalds 7421da177e4SLinus Torvalds print ".TH \"$args{'module'}\" 9 \"$args{'module'}\" \"$man_date\" \"API Manual\" LINUX\n"; 7431da177e4SLinus Torvalds 7441da177e4SLinus Torvalds foreach $section (@{$args{'sectionlist'}}) { 7451da177e4SLinus Torvalds print ".SH \"$section\"\n"; 7461da177e4SLinus Torvalds output_highlight($args{'sections'}{$section}); 7471da177e4SLinus Torvalds } 7481da177e4SLinus Torvalds} 7491da177e4SLinus Torvalds 7501da177e4SLinus Torvalds## 751c0d1b6eeSJonathan Corbet# output in restructured text 752c0d1b6eeSJonathan Corbet# 753c0d1b6eeSJonathan Corbet 754c0d1b6eeSJonathan Corbet# 755c0d1b6eeSJonathan Corbet# This could use some work; it's used to output the DOC: sections, and 756c0d1b6eeSJonathan Corbet# starts by putting out the name of the doc section itself, but that tends 757c0d1b6eeSJonathan Corbet# to duplicate a header already in the template file. 758c0d1b6eeSJonathan Corbet# 759c0d1b6eeSJonathan Corbetsub output_blockhead_rst(%) { 760c0d1b6eeSJonathan Corbet my %args = %{$_[0]}; 761c0d1b6eeSJonathan Corbet my ($parameter, $section); 762c0d1b6eeSJonathan Corbet 763c0d1b6eeSJonathan Corbet foreach $section (@{$args{'sectionlist'}}) { 7649e72184bSJani Nikula if ($output_selection != OUTPUT_INCLUDE) { 765c0d1b6eeSJonathan Corbet print "**$section**\n\n"; 7669e72184bSJani Nikula } 7670b0f5f29SDaniel Vetter print_lineno($section_start_lines{$section}); 768c0d1b6eeSJonathan Corbet output_highlight_rst($args{'sections'}{$section}); 769c0d1b6eeSJonathan Corbet print "\n"; 770c0d1b6eeSJonathan Corbet } 771c0d1b6eeSJonathan Corbet} 772c0d1b6eeSJonathan Corbet 773af250290SJonathan Corbet# 774af250290SJonathan Corbet# Apply the RST highlights to a sub-block of text. 775af250290SJonathan Corbet# 776af250290SJonathan Corbetsub highlight_block($) { 777af250290SJonathan Corbet # The dohighlight kludge requires the text be called $contents 778af250290SJonathan Corbet my $contents = shift; 779c0d1b6eeSJonathan Corbet eval $dohighlight; 780c0d1b6eeSJonathan Corbet die $@ if $@; 781af250290SJonathan Corbet return $contents; 782af250290SJonathan Corbet} 783c0d1b6eeSJonathan Corbet 784af250290SJonathan Corbet# 785af250290SJonathan Corbet# Regexes used only here. 786af250290SJonathan Corbet# 787af250290SJonathan Corbetmy $sphinx_literal = '^[^.].*::$'; 788af250290SJonathan Corbetmy $sphinx_cblock = '^\.\.\ +code-block::'; 789af250290SJonathan Corbet 790af250290SJonathan Corbetsub output_highlight_rst { 791af250290SJonathan Corbet my $input = join "\n",@_; 792af250290SJonathan Corbet my $output = ""; 793af250290SJonathan Corbet my $line; 794af250290SJonathan Corbet my $in_literal = 0; 795af250290SJonathan Corbet my $litprefix; 796af250290SJonathan Corbet my $block = ""; 797af250290SJonathan Corbet 798af250290SJonathan Corbet foreach $line (split "\n",$input) { 799af250290SJonathan Corbet # 800af250290SJonathan Corbet # If we're in a literal block, see if we should drop out 801af250290SJonathan Corbet # of it. Otherwise pass the line straight through unmunged. 802af250290SJonathan Corbet # 803af250290SJonathan Corbet if ($in_literal) { 804af250290SJonathan Corbet if (! ($line =~ /^\s*$/)) { 805af250290SJonathan Corbet # 806af250290SJonathan Corbet # If this is the first non-blank line in a literal 807af250290SJonathan Corbet # block we need to figure out what the proper indent is. 808af250290SJonathan Corbet # 809af250290SJonathan Corbet if ($litprefix eq "") { 810af250290SJonathan Corbet $line =~ /^(\s*)/; 811af250290SJonathan Corbet $litprefix = '^' . $1; 812af250290SJonathan Corbet $output .= $line . "\n"; 813af250290SJonathan Corbet } elsif (! ($line =~ /$litprefix/)) { 814af250290SJonathan Corbet $in_literal = 0; 815af250290SJonathan Corbet } else { 816af250290SJonathan Corbet $output .= $line . "\n"; 817af250290SJonathan Corbet } 818af250290SJonathan Corbet } else { 819af250290SJonathan Corbet $output .= $line . "\n"; 820af250290SJonathan Corbet } 821af250290SJonathan Corbet } 822af250290SJonathan Corbet # 823af250290SJonathan Corbet # Not in a literal block (or just dropped out) 824af250290SJonathan Corbet # 825af250290SJonathan Corbet if (! $in_literal) { 826af250290SJonathan Corbet $block .= $line . "\n"; 827af250290SJonathan Corbet if (($line =~ /$sphinx_literal/) || ($line =~ /$sphinx_cblock/)) { 828af250290SJonathan Corbet $in_literal = 1; 829af250290SJonathan Corbet $litprefix = ""; 830af250290SJonathan Corbet $output .= highlight_block($block); 831af250290SJonathan Corbet $block = "" 832af250290SJonathan Corbet } 833af250290SJonathan Corbet } 834af250290SJonathan Corbet } 835af250290SJonathan Corbet 836af250290SJonathan Corbet if ($block) { 837af250290SJonathan Corbet $output .= highlight_block($block); 838af250290SJonathan Corbet } 839af250290SJonathan Corbet foreach $line (split "\n", $output) { 840830066a7SJani Nikula print $lineprefix . $line . "\n"; 841c0d1b6eeSJonathan Corbet } 842c0d1b6eeSJonathan Corbet} 843c0d1b6eeSJonathan Corbet 844c0d1b6eeSJonathan Corbetsub output_function_rst(%) { 845c0d1b6eeSJonathan Corbet my %args = %{$_[0]}; 846c0d1b6eeSJonathan Corbet my ($parameter, $section); 847c099ff69SJani Nikula my $oldprefix = $lineprefix; 84882801d06SMauro Carvalho Chehab my $start = ""; 849c0d1b6eeSJonathan Corbet 85082801d06SMauro Carvalho Chehab if ($args{'typedef'}) { 85182801d06SMauro Carvalho Chehab print ".. c:type:: ". $args{'function'} . "\n\n"; 85282801d06SMauro Carvalho Chehab print_lineno($declaration_start_line); 85382801d06SMauro Carvalho Chehab print " **Typedef**: "; 85482801d06SMauro Carvalho Chehab $lineprefix = ""; 85582801d06SMauro Carvalho Chehab output_highlight_rst($args{'purpose'}); 85682801d06SMauro Carvalho Chehab $start = "\n\n**Syntax**\n\n ``"; 857c0d1b6eeSJonathan Corbet } else { 85882801d06SMauro Carvalho Chehab print ".. c:function:: "; 85982801d06SMauro Carvalho Chehab } 86082801d06SMauro Carvalho Chehab if ($args{'functiontype'} ne "") { 86182801d06SMauro Carvalho Chehab $start .= $args{'functiontype'} . " " . $args{'function'} . " ("; 86282801d06SMauro Carvalho Chehab } else { 86382801d06SMauro Carvalho Chehab $start .= $args{'function'} . " ("; 864c0d1b6eeSJonathan Corbet } 865c0d1b6eeSJonathan Corbet print $start; 866c0d1b6eeSJonathan Corbet 867c0d1b6eeSJonathan Corbet my $count = 0; 868c0d1b6eeSJonathan Corbet foreach my $parameter (@{$args{'parameterlist'}}) { 869c0d1b6eeSJonathan Corbet if ($count ne 0) { 870c0d1b6eeSJonathan Corbet print ", "; 871c0d1b6eeSJonathan Corbet } 872c0d1b6eeSJonathan Corbet $count++; 873c0d1b6eeSJonathan Corbet $type = $args{'parametertypes'}{$parameter}; 874a88b1672SMauro Carvalho Chehab 875c0d1b6eeSJonathan Corbet if ($type =~ m/([^\(]*\(\*)\s*\)\s*\(([^\)]*)\)/) { 876c0d1b6eeSJonathan Corbet # pointer-to-function 877e8f4ba83SPeter Maydell print $1 . $parameter . ") (" . $2 . ")"; 878c0d1b6eeSJonathan Corbet } else { 879c0d1b6eeSJonathan Corbet print $type . " " . $parameter; 880c0d1b6eeSJonathan Corbet } 881c0d1b6eeSJonathan Corbet } 88282801d06SMauro Carvalho Chehab if ($args{'typedef'}) { 88382801d06SMauro Carvalho Chehab print ");``\n\n"; 88482801d06SMauro Carvalho Chehab } else { 885c099ff69SJani Nikula print ")\n\n"; 8860b0f5f29SDaniel Vetter print_lineno($declaration_start_line); 887c099ff69SJani Nikula $lineprefix = " "; 888c099ff69SJani Nikula output_highlight_rst($args{'purpose'}); 889c099ff69SJani Nikula print "\n"; 89082801d06SMauro Carvalho Chehab } 891c0d1b6eeSJonathan Corbet 892ecbcfba1SJani Nikula print "**Parameters**\n\n"; 893c099ff69SJani Nikula $lineprefix = " "; 894c0d1b6eeSJonathan Corbet foreach $parameter (@{$args{'parameterlist'}}) { 895c0d1b6eeSJonathan Corbet my $parameter_name = $parameter; 896ada5f446SGabriel Krisman Bertazi $parameter_name =~ s/\[.*//; 897c0d1b6eeSJonathan Corbet $type = $args{'parametertypes'}{$parameter}; 898c0d1b6eeSJonathan Corbet 899c0d1b6eeSJonathan Corbet if ($type ne "") { 900c0d1b6eeSJonathan Corbet print "``$type $parameter``\n"; 901c0d1b6eeSJonathan Corbet } else { 902c0d1b6eeSJonathan Corbet print "``$parameter``\n"; 903c0d1b6eeSJonathan Corbet } 9040b0f5f29SDaniel Vetter 9050b0f5f29SDaniel Vetter print_lineno($parameterdesc_start_lines{$parameter_name}); 9060b0f5f29SDaniel Vetter 9075e64fa9cSJani Nikula if (defined($args{'parameterdescs'}{$parameter_name}) && 9085e64fa9cSJani Nikula $args{'parameterdescs'}{$parameter_name} ne $undescribed) { 909c0d1b6eeSJonathan Corbet output_highlight_rst($args{'parameterdescs'}{$parameter_name}); 910c0d1b6eeSJonathan Corbet } else { 911d4b08e0cSJani Nikula print " *undescribed*\n"; 912c0d1b6eeSJonathan Corbet } 913c0d1b6eeSJonathan Corbet print "\n"; 914c0d1b6eeSJonathan Corbet } 915c099ff69SJani Nikula 916c099ff69SJani Nikula $lineprefix = $oldprefix; 917c0d1b6eeSJonathan Corbet output_section_rst(@_); 918c0d1b6eeSJonathan Corbet} 919c0d1b6eeSJonathan Corbet 920c0d1b6eeSJonathan Corbetsub output_section_rst(%) { 921c0d1b6eeSJonathan Corbet my %args = %{$_[0]}; 922c0d1b6eeSJonathan Corbet my $section; 923c0d1b6eeSJonathan Corbet my $oldprefix = $lineprefix; 924c0d1b6eeSJonathan Corbet $lineprefix = ""; 925c0d1b6eeSJonathan Corbet 926c0d1b6eeSJonathan Corbet foreach $section (@{$args{'sectionlist'}}) { 927ecbcfba1SJani Nikula print "**$section**\n\n"; 9280b0f5f29SDaniel Vetter print_lineno($section_start_lines{$section}); 929c0d1b6eeSJonathan Corbet output_highlight_rst($args{'sections'}{$section}); 930c0d1b6eeSJonathan Corbet print "\n"; 931c0d1b6eeSJonathan Corbet } 932c0d1b6eeSJonathan Corbet print "\n"; 933c0d1b6eeSJonathan Corbet $lineprefix = $oldprefix; 934c0d1b6eeSJonathan Corbet} 935c0d1b6eeSJonathan Corbet 936c0d1b6eeSJonathan Corbetsub output_enum_rst(%) { 937c0d1b6eeSJonathan Corbet my %args = %{$_[0]}; 938c0d1b6eeSJonathan Corbet my ($parameter); 939c099ff69SJani Nikula my $oldprefix = $lineprefix; 940c0d1b6eeSJonathan Corbet my $count; 941c0d1b6eeSJonathan Corbet my $name = "enum " . $args{'enum'}; 94262850976SJani Nikula 94362850976SJani Nikula print "\n\n.. c:type:: " . $name . "\n\n"; 9440b0f5f29SDaniel Vetter print_lineno($declaration_start_line); 945c099ff69SJani Nikula $lineprefix = " "; 946c099ff69SJani Nikula output_highlight_rst($args{'purpose'}); 947c099ff69SJani Nikula print "\n"; 948c0d1b6eeSJonathan Corbet 949ecbcfba1SJani Nikula print "**Constants**\n\n"; 950c0d1b6eeSJonathan Corbet $lineprefix = " "; 951c0d1b6eeSJonathan Corbet foreach $parameter (@{$args{'parameterlist'}}) { 952ecbcfba1SJani Nikula print "``$parameter``\n"; 953c0d1b6eeSJonathan Corbet if ($args{'parameterdescs'}{$parameter} ne $undescribed) { 954c0d1b6eeSJonathan Corbet output_highlight_rst($args{'parameterdescs'}{$parameter}); 955c0d1b6eeSJonathan Corbet } else { 956d4b08e0cSJani Nikula print " *undescribed*\n"; 957c0d1b6eeSJonathan Corbet } 958c0d1b6eeSJonathan Corbet print "\n"; 959c0d1b6eeSJonathan Corbet } 960c099ff69SJani Nikula 961c0d1b6eeSJonathan Corbet $lineprefix = $oldprefix; 962c0d1b6eeSJonathan Corbet output_section_rst(@_); 963c0d1b6eeSJonathan Corbet} 964c0d1b6eeSJonathan Corbet 965c0d1b6eeSJonathan Corbetsub output_typedef_rst(%) { 966c0d1b6eeSJonathan Corbet my %args = %{$_[0]}; 967c0d1b6eeSJonathan Corbet my ($parameter); 968c099ff69SJani Nikula my $oldprefix = $lineprefix; 969c0d1b6eeSJonathan Corbet my $name = "typedef " . $args{'typedef'}; 970c0d1b6eeSJonathan Corbet 97162850976SJani Nikula print "\n\n.. c:type:: " . $name . "\n\n"; 9720b0f5f29SDaniel Vetter print_lineno($declaration_start_line); 973c099ff69SJani Nikula $lineprefix = " "; 974c099ff69SJani Nikula output_highlight_rst($args{'purpose'}); 975c099ff69SJani Nikula print "\n"; 976c0d1b6eeSJonathan Corbet 977c099ff69SJani Nikula $lineprefix = $oldprefix; 978c0d1b6eeSJonathan Corbet output_section_rst(@_); 979c0d1b6eeSJonathan Corbet} 980c0d1b6eeSJonathan Corbet 981c0d1b6eeSJonathan Corbetsub output_struct_rst(%) { 982c0d1b6eeSJonathan Corbet my %args = %{$_[0]}; 983c0d1b6eeSJonathan Corbet my ($parameter); 984c099ff69SJani Nikula my $oldprefix = $lineprefix; 985c0d1b6eeSJonathan Corbet my $name = $args{'type'} . " " . $args{'struct'}; 986c0d1b6eeSJonathan Corbet 98762850976SJani Nikula print "\n\n.. c:type:: " . $name . "\n\n"; 9880b0f5f29SDaniel Vetter print_lineno($declaration_start_line); 989c099ff69SJani Nikula $lineprefix = " "; 990c099ff69SJani Nikula output_highlight_rst($args{'purpose'}); 991c099ff69SJani Nikula print "\n"; 992c0d1b6eeSJonathan Corbet 993ecbcfba1SJani Nikula print "**Definition**\n\n"; 994c0d1b6eeSJonathan Corbet print "::\n\n"; 9958ad72163SMauro Carvalho Chehab my $declaration = $args{'definition'}; 9968ad72163SMauro Carvalho Chehab $declaration =~ s/\t/ /g; 9978ad72163SMauro Carvalho Chehab print " " . $args{'type'} . " " . $args{'struct'} . " {\n$declaration };\n\n"; 998c0d1b6eeSJonathan Corbet 999ecbcfba1SJani Nikula print "**Members**\n\n"; 1000c099ff69SJani Nikula $lineprefix = " "; 1001c0d1b6eeSJonathan Corbet foreach $parameter (@{$args{'parameterlist'}}) { 1002c0d1b6eeSJonathan Corbet ($parameter =~ /^#/) && next; 1003c0d1b6eeSJonathan Corbet 1004c0d1b6eeSJonathan Corbet my $parameter_name = $parameter; 1005c0d1b6eeSJonathan Corbet $parameter_name =~ s/\[.*//; 1006c0d1b6eeSJonathan Corbet 1007c0d1b6eeSJonathan Corbet ($args{'parameterdescs'}{$parameter_name} ne $undescribed) || next; 1008c0d1b6eeSJonathan Corbet $type = $args{'parametertypes'}{$parameter}; 10090b0f5f29SDaniel Vetter print_lineno($parameterdesc_start_lines{$parameter_name}); 10106d232c80SMauro Carvalho Chehab print "``" . $parameter . "``\n"; 1011c0d1b6eeSJonathan Corbet output_highlight_rst($args{'parameterdescs'}{$parameter_name}); 1012c0d1b6eeSJonathan Corbet print "\n"; 1013c0d1b6eeSJonathan Corbet } 1014c0d1b6eeSJonathan Corbet print "\n"; 1015c099ff69SJani Nikula 1016c099ff69SJani Nikula $lineprefix = $oldprefix; 1017c0d1b6eeSJonathan Corbet output_section_rst(@_); 1018c0d1b6eeSJonathan Corbet} 1019c0d1b6eeSJonathan Corbet 10203a025e1dSMatthew Wilcox## none mode output functions 10213a025e1dSMatthew Wilcox 10223a025e1dSMatthew Wilcoxsub output_function_none(%) { 10233a025e1dSMatthew Wilcox} 10243a025e1dSMatthew Wilcox 10253a025e1dSMatthew Wilcoxsub output_enum_none(%) { 10263a025e1dSMatthew Wilcox} 10273a025e1dSMatthew Wilcox 10283a025e1dSMatthew Wilcoxsub output_typedef_none(%) { 10293a025e1dSMatthew Wilcox} 10303a025e1dSMatthew Wilcox 10313a025e1dSMatthew Wilcoxsub output_struct_none(%) { 10323a025e1dSMatthew Wilcox} 10333a025e1dSMatthew Wilcox 10343a025e1dSMatthew Wilcoxsub output_blockhead_none(%) { 10353a025e1dSMatthew Wilcox} 10363a025e1dSMatthew Wilcox 10371da177e4SLinus Torvalds## 103827205744SRandy Dunlap# generic output function for all types (function, struct/union, typedef, enum); 103927205744SRandy Dunlap# calls the generated, variable output_ function name based on 104027205744SRandy Dunlap# functype and output_mode 10411da177e4SLinus Torvaldssub output_declaration { 10421da177e4SLinus Torvalds no strict 'refs'; 10431da177e4SLinus Torvalds my $name = shift; 10441da177e4SLinus Torvalds my $functype = shift; 10451da177e4SLinus Torvalds my $func = "output_${functype}_$output_mode"; 1046b6c3f456SJani Nikula if (($output_selection == OUTPUT_ALL) || 1047b6c3f456SJani Nikula (($output_selection == OUTPUT_INCLUDE || 1048b6c3f456SJani Nikula $output_selection == OUTPUT_EXPORTED) && 104986ae2e38SJani Nikula defined($function_table{$name})) || 1050b6c3f456SJani Nikula (($output_selection == OUTPUT_EXCLUDE || 1051b6c3f456SJani Nikula $output_selection == OUTPUT_INTERNAL) && 105286ae2e38SJani Nikula !($functype eq "function" && defined($function_table{$name})))) 10531da177e4SLinus Torvalds { 10541da177e4SLinus Torvalds &$func(@_); 10551da177e4SLinus Torvalds $section_counter++; 10561da177e4SLinus Torvalds } 10571da177e4SLinus Torvalds} 10581da177e4SLinus Torvalds 10591da177e4SLinus Torvalds## 106027205744SRandy Dunlap# generic output function - calls the right one based on current output mode. 1061b112e0f7SJohannes Bergsub output_blockhead { 10621da177e4SLinus Torvalds no strict 'refs'; 1063b112e0f7SJohannes Berg my $func = "output_blockhead_" . $output_mode; 10641da177e4SLinus Torvalds &$func(@_); 10651da177e4SLinus Torvalds $section_counter++; 10661da177e4SLinus Torvalds} 10671da177e4SLinus Torvalds 10681da177e4SLinus Torvalds## 10691da177e4SLinus Torvalds# takes a declaration (struct, union, enum, typedef) and 10701da177e4SLinus Torvalds# invokes the right handler. NOT called for functions. 10711da177e4SLinus Torvaldssub dump_declaration($$) { 10721da177e4SLinus Torvalds no strict 'refs'; 10731da177e4SLinus Torvalds my ($prototype, $file) = @_; 10741da177e4SLinus Torvalds my $func = "dump_" . $decl_type; 10751da177e4SLinus Torvalds &$func(@_); 10761da177e4SLinus Torvalds} 10771da177e4SLinus Torvalds 10781da177e4SLinus Torvaldssub dump_union($$) { 10791da177e4SLinus Torvalds dump_struct(@_); 10801da177e4SLinus Torvalds} 10811da177e4SLinus Torvalds 10821da177e4SLinus Torvaldssub dump_struct($$) { 10831da177e4SLinus Torvalds my $x = shift; 10841da177e4SLinus Torvalds my $file = shift; 10851da177e4SLinus Torvalds 1086*a070991fSJonathan Cameron if ($x =~ /(struct|union)\s+(\w+)\s*\{(.*)\}(\s*(__packed|__aligned|____cacheline_aligned_in_smp|____cacheline_aligned|__attribute__\s*\(\([a-z0-9,_\s\(\)]*\)\)))*/) { 10875cb5c31cSJohannes Berg my $decl_type = $1; 10881da177e4SLinus Torvalds $declaration_name = $2; 10891da177e4SLinus Torvalds my $members = $3; 10901da177e4SLinus Torvalds 1091aeec46b9SMartin Waitz # ignore members marked private: 10920d8c39e6SMauro Carvalho Chehab $members =~ s/\/\*\s*private:.*?\/\*\s*public:.*?\*\///gosi; 10930d8c39e6SMauro Carvalho Chehab $members =~ s/\/\*\s*private:.*//gosi; 1094aeec46b9SMartin Waitz # strip comments: 1095aeec46b9SMartin Waitz $members =~ s/\/\*.*?\*\///gos; 1096ef5da59fSRandy Dunlap # strip attributes 10973d9bfb19SSakari Ailus $members =~ s/\s*__attribute__\s*\(\([a-z0-9,_\*\s\(\)]*\)\)/ /gi; 10983d9bfb19SSakari Ailus $members =~ s/\s*__aligned\s*\([^;]*\)/ /gos; 10993d9bfb19SSakari Ailus $members =~ s/\s*__packed\s*/ /gos; 1100f0074929SJonathan Corbet $members =~ s/\s*CRYPTO_MINALIGN_ATTR/ /gos; 1101f861537dSAndré Almeida $members =~ s/\s*____cacheline_aligned_in_smp/ /gos; 1102*a070991fSJonathan Cameron $members =~ s/\s*____cacheline_aligned/ /gos; 11033556108eSMauro Carvalho Chehab 1104b22b5a9eSConchúr Navid # replace DECLARE_BITMAP 11053556108eSMauro Carvalho Chehab $members =~ s/__ETHTOOL_DECLARE_LINK_MODE_MASK\s*\(([^\)]+)\)/DECLARE_BITMAP($1, __ETHTOOL_LINK_MODE_MASK_NBITS)/gos; 110645005b27SMauro Carvalho Chehab $members =~ s/DECLARE_BITMAP\s*\(([^,)]+),\s*([^,)]+)\)/unsigned long $1\[BITS_TO_LONGS($2)\]/gos; 11071cb566baSJakub Kicinski # replace DECLARE_HASHTABLE 110845005b27SMauro Carvalho Chehab $members =~ s/DECLARE_HASHTABLE\s*\(([^,)]+),\s*([^,)]+)\)/unsigned long $1\[1 << (($2) - 1)\]/gos; 110945005b27SMauro Carvalho Chehab # replace DECLARE_KFIFO 111045005b27SMauro Carvalho Chehab $members =~ s/DECLARE_KFIFO\s*\(([^,)]+),\s*([^,)]+),\s*([^,)]+)\)/$2 \*$1/gos; 111145005b27SMauro Carvalho Chehab # replace DECLARE_KFIFO_PTR 111245005b27SMauro Carvalho Chehab $members =~ s/DECLARE_KFIFO_PTR\s*\(([^,)]+),\s*([^,)]+)\)/$2 \*$1/gos; 1113aeec46b9SMartin Waitz 11148ad72163SMauro Carvalho Chehab my $declaration = $members; 11158ad72163SMauro Carvalho Chehab 11168ad72163SMauro Carvalho Chehab # Split nested struct/union elements as newer ones 111784ce5b98SMauro Carvalho Chehab while ($members =~ m/(struct|union)([^\{\};]+)\{([^\{\}]*)\}([^\{\}\;]*)\;/) { 111884ce5b98SMauro Carvalho Chehab my $newmember; 111984ce5b98SMauro Carvalho Chehab my $maintype = $1; 112084ce5b98SMauro Carvalho Chehab my $ids = $4; 11218ad72163SMauro Carvalho Chehab my $content = $3; 112284ce5b98SMauro Carvalho Chehab foreach my $id(split /,/, $ids) { 112384ce5b98SMauro Carvalho Chehab $newmember .= "$maintype $id; "; 112484ce5b98SMauro Carvalho Chehab 11258ad72163SMauro Carvalho Chehab $id =~ s/[:\[].*//; 112684ce5b98SMauro Carvalho Chehab $id =~ s/^\s*\**(\S+)\s*/$1/; 11278ad72163SMauro Carvalho Chehab foreach my $arg (split /;/, $content) { 11288ad72163SMauro Carvalho Chehab next if ($arg =~ m/^\s*$/); 11297c0d7e87SMauro Carvalho Chehab if ($arg =~ m/^([^\(]+\(\*?\s*)([\w\.]*)(\s*\).*)/) { 11307c0d7e87SMauro Carvalho Chehab # pointer-to-function 11317c0d7e87SMauro Carvalho Chehab my $type = $1; 11327c0d7e87SMauro Carvalho Chehab my $name = $2; 11337c0d7e87SMauro Carvalho Chehab my $extra = $3; 11347c0d7e87SMauro Carvalho Chehab next if (!$name); 11357c0d7e87SMauro Carvalho Chehab if ($id =~ m/^\s*$/) { 11367c0d7e87SMauro Carvalho Chehab # anonymous struct/union 11377c0d7e87SMauro Carvalho Chehab $newmember .= "$type$name$extra; "; 11387c0d7e87SMauro Carvalho Chehab } else { 11397c0d7e87SMauro Carvalho Chehab $newmember .= "$type$id.$name$extra; "; 11407c0d7e87SMauro Carvalho Chehab } 11417c0d7e87SMauro Carvalho Chehab } else { 114284ce5b98SMauro Carvalho Chehab my $type; 114384ce5b98SMauro Carvalho Chehab my $names; 114484ce5b98SMauro Carvalho Chehab $arg =~ s/^\s+//; 114584ce5b98SMauro Carvalho Chehab $arg =~ s/\s+$//; 114684ce5b98SMauro Carvalho Chehab # Handle bitmaps 114784ce5b98SMauro Carvalho Chehab $arg =~ s/:\s*\d+\s*//g; 114884ce5b98SMauro Carvalho Chehab # Handle arrays 1149d404d579SMauro Carvalho Chehab $arg =~ s/\[.*\]//g; 115084ce5b98SMauro Carvalho Chehab # The type may have multiple words, 115184ce5b98SMauro Carvalho Chehab # and multiple IDs can be defined, like: 115284ce5b98SMauro Carvalho Chehab # const struct foo, *bar, foobar 115384ce5b98SMauro Carvalho Chehab # So, we remove spaces when parsing the 115484ce5b98SMauro Carvalho Chehab # names, in order to match just names 115584ce5b98SMauro Carvalho Chehab # and commas for the names 115684ce5b98SMauro Carvalho Chehab $arg =~ s/\s*,\s*/,/g; 115784ce5b98SMauro Carvalho Chehab if ($arg =~ m/(.*)\s+([\S+,]+)/) { 115884ce5b98SMauro Carvalho Chehab $type = $1; 115984ce5b98SMauro Carvalho Chehab $names = $2; 116084ce5b98SMauro Carvalho Chehab } else { 116184ce5b98SMauro Carvalho Chehab $newmember .= "$arg; "; 116284ce5b98SMauro Carvalho Chehab next; 116384ce5b98SMauro Carvalho Chehab } 116484ce5b98SMauro Carvalho Chehab foreach my $name (split /,/, $names) { 116584ce5b98SMauro Carvalho Chehab $name =~ s/^\s*\**(\S+)\s*/$1/; 11668ad72163SMauro Carvalho Chehab next if (($name =~ m/^\s*$/)); 11678ad72163SMauro Carvalho Chehab if ($id =~ m/^\s*$/) { 11688ad72163SMauro Carvalho Chehab # anonymous struct/union 11698ad72163SMauro Carvalho Chehab $newmember .= "$type $name; "; 11708ad72163SMauro Carvalho Chehab } else { 11718ad72163SMauro Carvalho Chehab $newmember .= "$type $id.$name; "; 11728ad72163SMauro Carvalho Chehab } 11738ad72163SMauro Carvalho Chehab } 11747c0d7e87SMauro Carvalho Chehab } 117584ce5b98SMauro Carvalho Chehab } 117684ce5b98SMauro Carvalho Chehab } 1177673bb2dfSBen Hutchings $members =~ s/(struct|union)([^\{\};]+)\{([^\{\}]*)\}([^\{\}\;]*)\;/$newmember/; 117884ce5b98SMauro Carvalho Chehab } 11798ad72163SMauro Carvalho Chehab 11808ad72163SMauro Carvalho Chehab # Ignore other nested elements, like enums 1181673bb2dfSBen Hutchings $members =~ s/(\{[^\{\}]*\})//g; 11828ad72163SMauro Carvalho Chehab 1183151c468bSMauro Carvalho Chehab create_parameterlist($members, ';', $file, $declaration_name); 11841081de2dSMauro Carvalho Chehab check_sections($file, $declaration_name, $decl_type, $sectcheck, $struct_actual); 11851da177e4SLinus Torvalds 11868ad72163SMauro Carvalho Chehab # Adjust declaration for better display 1187673bb2dfSBen Hutchings $declaration =~ s/([\{;])/$1\n/g; 1188673bb2dfSBen Hutchings $declaration =~ s/\}\s+;/};/g; 11898ad72163SMauro Carvalho Chehab # Better handle inlined enums 1190673bb2dfSBen Hutchings do {} while ($declaration =~ s/(enum\s+\{[^\}]+),([^\n])/$1,\n$2/); 11918ad72163SMauro Carvalho Chehab 11928ad72163SMauro Carvalho Chehab my @def_args = split /\n/, $declaration; 11938ad72163SMauro Carvalho Chehab my $level = 1; 11948ad72163SMauro Carvalho Chehab $declaration = ""; 11958ad72163SMauro Carvalho Chehab foreach my $clause (@def_args) { 11968ad72163SMauro Carvalho Chehab $clause =~ s/^\s+//; 11978ad72163SMauro Carvalho Chehab $clause =~ s/\s+$//; 11988ad72163SMauro Carvalho Chehab $clause =~ s/\s+/ /; 11998ad72163SMauro Carvalho Chehab next if (!$clause); 1200673bb2dfSBen Hutchings $level-- if ($clause =~ m/(\})/ && $level > 1); 12018ad72163SMauro Carvalho Chehab if (!($clause =~ m/^\s*#/)) { 12028ad72163SMauro Carvalho Chehab $declaration .= "\t" x $level; 12038ad72163SMauro Carvalho Chehab } 12048ad72163SMauro Carvalho Chehab $declaration .= "\t" . $clause . "\n"; 1205673bb2dfSBen Hutchings $level++ if ($clause =~ m/(\{)/ && !($clause =~m/\}/)); 12068ad72163SMauro Carvalho Chehab } 12071da177e4SLinus Torvalds output_declaration($declaration_name, 12081da177e4SLinus Torvalds 'struct', 12091da177e4SLinus Torvalds {'struct' => $declaration_name, 12101da177e4SLinus Torvalds 'module' => $modulename, 12118ad72163SMauro Carvalho Chehab 'definition' => $declaration, 12121da177e4SLinus Torvalds 'parameterlist' => \@parameterlist, 12131da177e4SLinus Torvalds 'parameterdescs' => \%parameterdescs, 12141da177e4SLinus Torvalds 'parametertypes' => \%parametertypes, 12151da177e4SLinus Torvalds 'sectionlist' => \@sectionlist, 12161da177e4SLinus Torvalds 'sections' => \%sections, 12171da177e4SLinus Torvalds 'purpose' => $declaration_purpose, 12181da177e4SLinus Torvalds 'type' => $decl_type 12191da177e4SLinus Torvalds }); 12201da177e4SLinus Torvalds } 12211da177e4SLinus Torvalds else { 1222d40e1e65SBart Van Assche print STDERR "${file}:$.: error: Cannot parse struct or union!\n"; 12231da177e4SLinus Torvalds ++$errors; 12241da177e4SLinus Torvalds } 12251da177e4SLinus Torvalds} 12261da177e4SLinus Torvalds 122785afe608SMauro Carvalho Chehab 122885afe608SMauro Carvalho Chehabsub show_warnings($$) { 122985afe608SMauro Carvalho Chehab my $functype = shift; 123085afe608SMauro Carvalho Chehab my $name = shift; 123185afe608SMauro Carvalho Chehab 123285afe608SMauro Carvalho Chehab return 1 if ($output_selection == OUTPUT_ALL); 123385afe608SMauro Carvalho Chehab 123485afe608SMauro Carvalho Chehab if ($output_selection == OUTPUT_EXPORTED) { 123585afe608SMauro Carvalho Chehab if (defined($function_table{$name})) { 123685afe608SMauro Carvalho Chehab return 1; 123785afe608SMauro Carvalho Chehab } else { 123885afe608SMauro Carvalho Chehab return 0; 123985afe608SMauro Carvalho Chehab } 124085afe608SMauro Carvalho Chehab } 124185afe608SMauro Carvalho Chehab if ($output_selection == OUTPUT_INTERNAL) { 124285afe608SMauro Carvalho Chehab if (!($functype eq "function" && defined($function_table{$name}))) { 124385afe608SMauro Carvalho Chehab return 1; 124485afe608SMauro Carvalho Chehab } else { 124585afe608SMauro Carvalho Chehab return 0; 124685afe608SMauro Carvalho Chehab } 124785afe608SMauro Carvalho Chehab } 124885afe608SMauro Carvalho Chehab if ($output_selection == OUTPUT_INCLUDE) { 124985afe608SMauro Carvalho Chehab if (defined($function_table{$name})) { 125085afe608SMauro Carvalho Chehab return 1; 125185afe608SMauro Carvalho Chehab } else { 125285afe608SMauro Carvalho Chehab return 0; 125385afe608SMauro Carvalho Chehab } 125485afe608SMauro Carvalho Chehab } 125585afe608SMauro Carvalho Chehab if ($output_selection == OUTPUT_EXCLUDE) { 125685afe608SMauro Carvalho Chehab if (!defined($function_table{$name})) { 125785afe608SMauro Carvalho Chehab return 1; 125885afe608SMauro Carvalho Chehab } else { 125985afe608SMauro Carvalho Chehab return 0; 126085afe608SMauro Carvalho Chehab } 126185afe608SMauro Carvalho Chehab } 126285afe608SMauro Carvalho Chehab die("Please add the new output type at show_warnings()"); 126385afe608SMauro Carvalho Chehab} 126485afe608SMauro Carvalho Chehab 12651da177e4SLinus Torvaldssub dump_enum($$) { 12661da177e4SLinus Torvalds my $x = shift; 12671da177e4SLinus Torvalds my $file = shift; 12681da177e4SLinus Torvalds 1269aeec46b9SMartin Waitz $x =~ s@/\*.*?\*/@@gos; # strip comments. 12704468e21eSConchúr Navid # strip #define macros inside enums 12714468e21eSConchúr Navid $x =~ s@#\s*((define|ifdef)\s+|endif)[^;]*;@@gos; 1272b6d676dbSRandy Dunlap 127315e2544eSAndy Shevchenko if ($x =~ /enum\s+(\w*)\s*\{(.*)\}/) { 12741da177e4SLinus Torvalds $declaration_name = $1; 12751da177e4SLinus Torvalds my $members = $2; 12765cb5c31cSJohannes Berg my %_members; 12775cb5c31cSJohannes Berg 1278463a0fdcSMarkus Heiser $members =~ s/\s+$//; 12791da177e4SLinus Torvalds 12801da177e4SLinus Torvalds foreach my $arg (split ',', $members) { 12811da177e4SLinus Torvalds $arg =~ s/^\s*(\w+).*/$1/; 12821da177e4SLinus Torvalds push @parameterlist, $arg; 12831da177e4SLinus Torvalds if (!$parameterdescs{$arg}) { 12841da177e4SLinus Torvalds $parameterdescs{$arg} = $undescribed; 128585afe608SMauro Carvalho Chehab if (show_warnings("enum", $declaration_name)) { 12862defb272SMauro Carvalho Chehab print STDERR "${file}:$.: warning: Enum value '$arg' not described in enum '$declaration_name'\n"; 12872defb272SMauro Carvalho Chehab } 12881da177e4SLinus Torvalds } 12895cb5c31cSJohannes Berg $_members{$arg} = 1; 12905cb5c31cSJohannes Berg } 12911da177e4SLinus Torvalds 12925cb5c31cSJohannes Berg while (my ($k, $v) = each %parameterdescs) { 12935cb5c31cSJohannes Berg if (!exists($_members{$k})) { 129485afe608SMauro Carvalho Chehab if (show_warnings("enum", $declaration_name)) { 12952defb272SMauro Carvalho Chehab print STDERR "${file}:$.: warning: Excess enum value '$k' description in '$declaration_name'\n"; 12962defb272SMauro Carvalho Chehab } 12975cb5c31cSJohannes Berg } 12981da177e4SLinus Torvalds } 12991da177e4SLinus Torvalds 13001da177e4SLinus Torvalds output_declaration($declaration_name, 13011da177e4SLinus Torvalds 'enum', 13021da177e4SLinus Torvalds {'enum' => $declaration_name, 13031da177e4SLinus Torvalds 'module' => $modulename, 13041da177e4SLinus Torvalds 'parameterlist' => \@parameterlist, 13051da177e4SLinus Torvalds 'parameterdescs' => \%parameterdescs, 13061da177e4SLinus Torvalds 'sectionlist' => \@sectionlist, 13071da177e4SLinus Torvalds 'sections' => \%sections, 13081da177e4SLinus Torvalds 'purpose' => $declaration_purpose 13091da177e4SLinus Torvalds }); 13101da177e4SLinus Torvalds } 13111da177e4SLinus Torvalds else { 1312d40e1e65SBart Van Assche print STDERR "${file}:$.: error: Cannot parse enum!\n"; 13131da177e4SLinus Torvalds ++$errors; 13141da177e4SLinus Torvalds } 13151da177e4SLinus Torvalds} 13161da177e4SLinus Torvalds 13171da177e4SLinus Torvaldssub dump_typedef($$) { 13181da177e4SLinus Torvalds my $x = shift; 13191da177e4SLinus Torvalds my $file = shift; 13201da177e4SLinus Torvalds 1321aeec46b9SMartin Waitz $x =~ s@/\*.*?\*/@@gos; # strip comments. 132283766452SMauro Carvalho Chehab 132383766452SMauro Carvalho Chehab # Parse function prototypes 1324d37c43ceSMauro Carvalho Chehab if ($x =~ /typedef\s+(\w+)\s*\(\*\s*(\w\S+)\s*\)\s*\((.*)\);/ || 1325d37c43ceSMauro Carvalho Chehab $x =~ /typedef\s+(\w+)\s*(\w\S+)\s*\s*\((.*)\);/) { 1326d37c43ceSMauro Carvalho Chehab 132783766452SMauro Carvalho Chehab # Function typedefs 132883766452SMauro Carvalho Chehab $return_type = $1; 132983766452SMauro Carvalho Chehab $declaration_name = $2; 133083766452SMauro Carvalho Chehab my $args = $3; 133183766452SMauro Carvalho Chehab 1332151c468bSMauro Carvalho Chehab create_parameterlist($args, ',', $file, $declaration_name); 133383766452SMauro Carvalho Chehab 133483766452SMauro Carvalho Chehab output_declaration($declaration_name, 133583766452SMauro Carvalho Chehab 'function', 133683766452SMauro Carvalho Chehab {'function' => $declaration_name, 133782801d06SMauro Carvalho Chehab 'typedef' => 1, 133883766452SMauro Carvalho Chehab 'module' => $modulename, 133983766452SMauro Carvalho Chehab 'functiontype' => $return_type, 134083766452SMauro Carvalho Chehab 'parameterlist' => \@parameterlist, 134183766452SMauro Carvalho Chehab 'parameterdescs' => \%parameterdescs, 134283766452SMauro Carvalho Chehab 'parametertypes' => \%parametertypes, 134383766452SMauro Carvalho Chehab 'sectionlist' => \@sectionlist, 134483766452SMauro Carvalho Chehab 'sections' => \%sections, 134583766452SMauro Carvalho Chehab 'purpose' => $declaration_purpose 134683766452SMauro Carvalho Chehab }); 134783766452SMauro Carvalho Chehab return; 134883766452SMauro Carvalho Chehab } 134983766452SMauro Carvalho Chehab 13501da177e4SLinus Torvalds while (($x =~ /\(*.\)\s*;$/) || ($x =~ /\[*.\]\s*;$/)) { 13511da177e4SLinus Torvalds $x =~ s/\(*.\)\s*;$/;/; 13521da177e4SLinus Torvalds $x =~ s/\[*.\]\s*;$/;/; 13531da177e4SLinus Torvalds } 13541da177e4SLinus Torvalds 13551da177e4SLinus Torvalds if ($x =~ /typedef.*\s+(\w+)\s*;/) { 13561da177e4SLinus Torvalds $declaration_name = $1; 13571da177e4SLinus Torvalds 13581da177e4SLinus Torvalds output_declaration($declaration_name, 13591da177e4SLinus Torvalds 'typedef', 13601da177e4SLinus Torvalds {'typedef' => $declaration_name, 13611da177e4SLinus Torvalds 'module' => $modulename, 13621da177e4SLinus Torvalds 'sectionlist' => \@sectionlist, 13631da177e4SLinus Torvalds 'sections' => \%sections, 13641da177e4SLinus Torvalds 'purpose' => $declaration_purpose 13651da177e4SLinus Torvalds }); 13661da177e4SLinus Torvalds } 13671da177e4SLinus Torvalds else { 1368d40e1e65SBart Van Assche print STDERR "${file}:$.: error: Cannot parse typedef!\n"; 13691da177e4SLinus Torvalds ++$errors; 13701da177e4SLinus Torvalds } 13711da177e4SLinus Torvalds} 13721da177e4SLinus Torvalds 1373a1d94aa5SRandy Dunlapsub save_struct_actual($) { 1374a1d94aa5SRandy Dunlap my $actual = shift; 1375a1d94aa5SRandy Dunlap 1376a1d94aa5SRandy Dunlap # strip all spaces from the actual param so that it looks like one string item 1377a1d94aa5SRandy Dunlap $actual =~ s/\s*//g; 1378a1d94aa5SRandy Dunlap $struct_actual = $struct_actual . $actual . " "; 1379a1d94aa5SRandy Dunlap} 1380a1d94aa5SRandy Dunlap 1381151c468bSMauro Carvalho Chehabsub create_parameterlist($$$$) { 13821da177e4SLinus Torvalds my $args = shift; 13831da177e4SLinus Torvalds my $splitter = shift; 13841da177e4SLinus Torvalds my $file = shift; 1385151c468bSMauro Carvalho Chehab my $declaration_name = shift; 13861da177e4SLinus Torvalds my $type; 13871da177e4SLinus Torvalds my $param; 13881da177e4SLinus Torvalds 1389a6d3fe77SMartin Waitz # temporarily replace commas inside function pointer definition 13901da177e4SLinus Torvalds while ($args =~ /(\([^\),]+),/) { 13911da177e4SLinus Torvalds $args =~ s/(\([^\),]+),/$1#/g; 13921da177e4SLinus Torvalds } 13931da177e4SLinus Torvalds 13941da177e4SLinus Torvalds foreach my $arg (split($splitter, $args)) { 13951da177e4SLinus Torvalds # strip comments 13961da177e4SLinus Torvalds $arg =~ s/\/\*.*\*\///; 13971da177e4SLinus Torvalds # strip leading/trailing spaces 13981da177e4SLinus Torvalds $arg =~ s/^\s*//; 13991da177e4SLinus Torvalds $arg =~ s/\s*$//; 14001da177e4SLinus Torvalds $arg =~ s/\s+/ /; 14011da177e4SLinus Torvalds 14021da177e4SLinus Torvalds if ($arg =~ /^#/) { 14031da177e4SLinus Torvalds # Treat preprocessor directive as a typeless variable just to fill 14041da177e4SLinus Torvalds # corresponding data structures "correctly". Catch it later in 14051da177e4SLinus Torvalds # output_* subs. 14061da177e4SLinus Torvalds push_parameter($arg, "", $file); 140700d62961SRichard Kennedy } elsif ($arg =~ m/\(.+\)\s*\(/) { 14081da177e4SLinus Torvalds # pointer-to-function 14091da177e4SLinus Torvalds $arg =~ tr/#/,/; 14107c0d7e87SMauro Carvalho Chehab $arg =~ m/[^\(]+\(\*?\s*([\w\.]*)\s*\)/; 14111da177e4SLinus Torvalds $param = $1; 14121da177e4SLinus Torvalds $type = $arg; 141300d62961SRichard Kennedy $type =~ s/([^\(]+\(\*?)\s*$param/$1/; 1414a1d94aa5SRandy Dunlap save_struct_actual($param); 1415151c468bSMauro Carvalho Chehab push_parameter($param, $type, $file, $declaration_name); 1416aeec46b9SMartin Waitz } elsif ($arg) { 14171da177e4SLinus Torvalds $arg =~ s/\s*:\s*/:/g; 14181da177e4SLinus Torvalds $arg =~ s/\s*\[/\[/g; 14191da177e4SLinus Torvalds 14201da177e4SLinus Torvalds my @args = split('\s*,\s*', $arg); 14211da177e4SLinus Torvalds if ($args[0] =~ m/\*/) { 14221da177e4SLinus Torvalds $args[0] =~ s/(\*+)\s*/ $1/; 14231da177e4SLinus Torvalds } 1424884f2810SBorislav Petkov 1425884f2810SBorislav Petkov my @first_arg; 1426884f2810SBorislav Petkov if ($args[0] =~ /^(.*\s+)(.*?\[.*\].*)$/) { 1427884f2810SBorislav Petkov shift @args; 1428884f2810SBorislav Petkov push(@first_arg, split('\s+', $1)); 1429884f2810SBorislav Petkov push(@first_arg, $2); 1430884f2810SBorislav Petkov } else { 1431884f2810SBorislav Petkov @first_arg = split('\s+', shift @args); 1432884f2810SBorislav Petkov } 1433884f2810SBorislav Petkov 14341da177e4SLinus Torvalds unshift(@args, pop @first_arg); 14351da177e4SLinus Torvalds $type = join " ", @first_arg; 14361da177e4SLinus Torvalds 14371da177e4SLinus Torvalds foreach $param (@args) { 14381da177e4SLinus Torvalds if ($param =~ m/^(\*+)\s*(.*)/) { 1439a1d94aa5SRandy Dunlap save_struct_actual($2); 1440151c468bSMauro Carvalho Chehab push_parameter($2, "$type $1", $file, $declaration_name); 14411da177e4SLinus Torvalds } 14421da177e4SLinus Torvalds elsif ($param =~ m/(.*?):(\d+)/) { 14437b97887eSRandy Dunlap if ($type ne "") { # skip unnamed bit-fields 1444a1d94aa5SRandy Dunlap save_struct_actual($1); 1445151c468bSMauro Carvalho Chehab push_parameter($1, "$type:$2", $file, $declaration_name) 14461da177e4SLinus Torvalds } 14477b97887eSRandy Dunlap } 14481da177e4SLinus Torvalds else { 1449a1d94aa5SRandy Dunlap save_struct_actual($param); 1450151c468bSMauro Carvalho Chehab push_parameter($param, $type, $file, $declaration_name); 14511da177e4SLinus Torvalds } 14521da177e4SLinus Torvalds } 14531da177e4SLinus Torvalds } 14541da177e4SLinus Torvalds } 14551da177e4SLinus Torvalds} 14561da177e4SLinus Torvalds 1457151c468bSMauro Carvalho Chehabsub push_parameter($$$$) { 14581da177e4SLinus Torvalds my $param = shift; 14591da177e4SLinus Torvalds my $type = shift; 14601da177e4SLinus Torvalds my $file = shift; 1461151c468bSMauro Carvalho Chehab my $declaration_name = shift; 14621da177e4SLinus Torvalds 14635f8c7c98SRandy Dunlap if (($anon_struct_union == 1) && ($type eq "") && 14645f8c7c98SRandy Dunlap ($param eq "}")) { 14655f8c7c98SRandy Dunlap return; # ignore the ending }; from anon. struct/union 14665f8c7c98SRandy Dunlap } 14675f8c7c98SRandy Dunlap 14685f8c7c98SRandy Dunlap $anon_struct_union = 0; 1469f9b5c530SMauro Carvalho Chehab $param =~ s/[\[\)].*//; 14701da177e4SLinus Torvalds 1471a6d3fe77SMartin Waitz if ($type eq "" && $param =~ /\.\.\.$/) 14721da177e4SLinus Torvalds { 1473c950a173SSilvio Fricke if (!$param =~ /\w\.\.\.$/) { 1474c950a173SSilvio Fricke # handles unnamed variable parameters 1475ef00028bSJonathan Corbet $param = "..."; 1476c950a173SSilvio Fricke } 147743756e34SJonathan Neuschäfer elsif ($param =~ /\w\.\.\.$/) { 147843756e34SJonathan Neuschäfer # for named variable parameters of the form `x...`, remove the dots 147943756e34SJonathan Neuschäfer $param =~ s/\.\.\.$//; 148043756e34SJonathan Neuschäfer } 1481ced69090SRandy Dunlap if (!defined $parameterdescs{$param} || $parameterdescs{$param} eq "") { 1482a6d3fe77SMartin Waitz $parameterdescs{$param} = "variable arguments"; 14831da177e4SLinus Torvalds } 1484ced69090SRandy Dunlap } 14851da177e4SLinus Torvalds elsif ($type eq "" && ($param eq "" or $param eq "void")) 14861da177e4SLinus Torvalds { 14871da177e4SLinus Torvalds $param="void"; 14881da177e4SLinus Torvalds $parameterdescs{void} = "no arguments"; 14891da177e4SLinus Torvalds } 1490134fe01bSRandy Dunlap elsif ($type eq "" && ($param eq "struct" or $param eq "union")) 1491134fe01bSRandy Dunlap # handle unnamed (anonymous) union or struct: 1492134fe01bSRandy Dunlap { 1493134fe01bSRandy Dunlap $type = $param; 1494134fe01bSRandy Dunlap $param = "{unnamed_" . $param . "}"; 1495134fe01bSRandy Dunlap $parameterdescs{$param} = "anonymous\n"; 14965f8c7c98SRandy Dunlap $anon_struct_union = 1; 1497134fe01bSRandy Dunlap } 1498134fe01bSRandy Dunlap 1499a6d3fe77SMartin Waitz # warn if parameter has no description 1500134fe01bSRandy Dunlap # (but ignore ones starting with # as these are not parameters 1501134fe01bSRandy Dunlap # but inline preprocessor statements); 1502151c468bSMauro Carvalho Chehab # Note: It will also ignore void params and unnamed structs/unions 1503f9b5c530SMauro Carvalho Chehab if (!defined $parameterdescs{$param} && $param !~ /^#/) { 1504f9b5c530SMauro Carvalho Chehab $parameterdescs{$param} = $undescribed; 15051da177e4SLinus Torvalds 1506be5cd20cSJonathan Corbet if (show_warnings($type, $declaration_name) && $param !~ /\./) { 1507151c468bSMauro Carvalho Chehab print STDERR 1508151c468bSMauro Carvalho Chehab "${file}:$.: warning: Function parameter or member '$param' not described in '$declaration_name'\n"; 15091da177e4SLinus Torvalds ++$warnings; 15101da177e4SLinus Torvalds } 15112defb272SMauro Carvalho Chehab } 15121da177e4SLinus Torvalds 151325985edcSLucas De Marchi # strip spaces from $param so that it is one continuous string 1514e34e7dbbSRandy Dunlap # on @parameterlist; 1515e34e7dbbSRandy Dunlap # this fixes a problem where check_sections() cannot find 1516e34e7dbbSRandy Dunlap # a parameter like "addr[6 + 2]" because it actually appears 1517e34e7dbbSRandy Dunlap # as "addr[6", "+", "2]" on the parameter list; 1518e34e7dbbSRandy Dunlap # but it's better to maintain the param string unchanged for output, 1519e34e7dbbSRandy Dunlap # so just weaken the string compare in check_sections() to ignore 1520e34e7dbbSRandy Dunlap # "[blah" in a parameter string; 1521e34e7dbbSRandy Dunlap ###$param =~ s/\s*//g; 15221da177e4SLinus Torvalds push @parameterlist, $param; 152302a4f4feSPaolo Bonzini $type =~ s/\s\s+/ /g; 15241da177e4SLinus Torvalds $parametertypes{$param} = $type; 15251da177e4SLinus Torvalds} 15261da177e4SLinus Torvalds 15271081de2dSMauro Carvalho Chehabsub check_sections($$$$$) { 15281081de2dSMauro Carvalho Chehab my ($file, $decl_name, $decl_type, $sectcheck, $prmscheck) = @_; 1529a1d94aa5SRandy Dunlap my @sects = split ' ', $sectcheck; 1530a1d94aa5SRandy Dunlap my @prms = split ' ', $prmscheck; 1531a1d94aa5SRandy Dunlap my $err; 1532a1d94aa5SRandy Dunlap my ($px, $sx); 1533a1d94aa5SRandy Dunlap my $prm_clean; # strip trailing "[array size]" and/or beginning "*" 1534a1d94aa5SRandy Dunlap 1535a1d94aa5SRandy Dunlap foreach $sx (0 .. $#sects) { 1536a1d94aa5SRandy Dunlap $err = 1; 1537a1d94aa5SRandy Dunlap foreach $px (0 .. $#prms) { 1538a1d94aa5SRandy Dunlap $prm_clean = $prms[$px]; 1539a1d94aa5SRandy Dunlap $prm_clean =~ s/\[.*\]//; 15401f3a6688SJohannes Berg $prm_clean =~ s/__attribute__\s*\(\([a-z,_\*\s\(\)]*\)\)//i; 1541e34e7dbbSRandy Dunlap # ignore array size in a parameter string; 1542e34e7dbbSRandy Dunlap # however, the original param string may contain 1543e34e7dbbSRandy Dunlap # spaces, e.g.: addr[6 + 2] 1544e34e7dbbSRandy Dunlap # and this appears in @prms as "addr[6" since the 1545e34e7dbbSRandy Dunlap # parameter list is split at spaces; 1546e34e7dbbSRandy Dunlap # hence just ignore "[..." for the sections check; 1547e34e7dbbSRandy Dunlap $prm_clean =~ s/\[.*//; 1548e34e7dbbSRandy Dunlap 1549a1d94aa5SRandy Dunlap ##$prm_clean =~ s/^\**//; 1550a1d94aa5SRandy Dunlap if ($prm_clean eq $sects[$sx]) { 1551a1d94aa5SRandy Dunlap $err = 0; 1552a1d94aa5SRandy Dunlap last; 1553a1d94aa5SRandy Dunlap } 1554a1d94aa5SRandy Dunlap } 1555a1d94aa5SRandy Dunlap if ($err) { 1556a1d94aa5SRandy Dunlap if ($decl_type eq "function") { 1557d40e1e65SBart Van Assche print STDERR "${file}:$.: warning: " . 1558a1d94aa5SRandy Dunlap "Excess function parameter " . 1559a1d94aa5SRandy Dunlap "'$sects[$sx]' " . 1560a1d94aa5SRandy Dunlap "description in '$decl_name'\n"; 1561a1d94aa5SRandy Dunlap ++$warnings; 1562a1d94aa5SRandy Dunlap } 1563a1d94aa5SRandy Dunlap } 1564a1d94aa5SRandy Dunlap } 1565a1d94aa5SRandy Dunlap} 1566a1d94aa5SRandy Dunlap 15671da177e4SLinus Torvalds## 15684092bac7SYacine Belkadi# Checks the section describing the return value of a function. 15694092bac7SYacine Belkadisub check_return_section { 15704092bac7SYacine Belkadi my $file = shift; 15714092bac7SYacine Belkadi my $declaration_name = shift; 15724092bac7SYacine Belkadi my $return_type = shift; 15734092bac7SYacine Belkadi 15744092bac7SYacine Belkadi # Ignore an empty return type (It's a macro) 15754092bac7SYacine Belkadi # Ignore functions with a "void" return type. (But don't ignore "void *") 15764092bac7SYacine Belkadi if (($return_type eq "") || ($return_type =~ /void\s*\w*\s*$/)) { 15774092bac7SYacine Belkadi return; 15784092bac7SYacine Belkadi } 15794092bac7SYacine Belkadi 15804092bac7SYacine Belkadi if (!defined($sections{$section_return}) || 15814092bac7SYacine Belkadi $sections{$section_return} eq "") { 1582d40e1e65SBart Van Assche print STDERR "${file}:$.: warning: " . 15834092bac7SYacine Belkadi "No description found for return value of " . 15844092bac7SYacine Belkadi "'$declaration_name'\n"; 15854092bac7SYacine Belkadi ++$warnings; 15864092bac7SYacine Belkadi } 15874092bac7SYacine Belkadi} 15884092bac7SYacine Belkadi 15894092bac7SYacine Belkadi## 15901da177e4SLinus Torvalds# takes a function prototype and the name of the current file being 15911da177e4SLinus Torvalds# processed and spits out all the details stored in the global 15921da177e4SLinus Torvalds# arrays/hashes. 15931da177e4SLinus Torvaldssub dump_function($$) { 15941da177e4SLinus Torvalds my $prototype = shift; 15951da177e4SLinus Torvalds my $file = shift; 1596cbb4d3e6SHoria Geanta my $noret = 0; 15971da177e4SLinus Torvalds 15985eb6b4b3SMauro Carvalho Chehab print_lineno($.); 15995eb6b4b3SMauro Carvalho Chehab 16001da177e4SLinus Torvalds $prototype =~ s/^static +//; 16011da177e4SLinus Torvalds $prototype =~ s/^extern +//; 16024dc3b16bSPavel Pisa $prototype =~ s/^asmlinkage +//; 16031da177e4SLinus Torvalds $prototype =~ s/^inline +//; 16041da177e4SLinus Torvalds $prototype =~ s/^__inline__ +//; 160532e79401SRandy Dunlap $prototype =~ s/^__inline +//; 160632e79401SRandy Dunlap $prototype =~ s/^__always_inline +//; 160732e79401SRandy Dunlap $prototype =~ s/^noinline +//; 160874fc5c65SRandy Dunlap $prototype =~ s/__init +//; 160920072205SRandy Dunlap $prototype =~ s/__init_or_module +//; 1610270a0096SRandy Dunlap $prototype =~ s/__meminit +//; 161170c95b00SRandy Dunlap $prototype =~ s/__must_check +//; 16120df7c0e3SRandy Dunlap $prototype =~ s/__weak +//; 16130891f959SMatthew Wilcox $prototype =~ s/__sched +//; 161495e760cbSRandy Dunlap $prototype =~ s/__printf\s*\(\s*\d*\s*,\s*\d*\s*\) +//; 1615cbb4d3e6SHoria Geanta my $define = $prototype =~ s/^#\s*define\s+//; #ak added 1616b1aaa546SPaolo Bonzini $prototype =~ s/__attribute__\s*\(\( 1617b1aaa546SPaolo Bonzini (?: 1618b1aaa546SPaolo Bonzini [\w\s]++ # attribute name 1619b1aaa546SPaolo Bonzini (?:\([^)]*+\))? # attribute arguments 1620b1aaa546SPaolo Bonzini \s*+,? # optional comma at the end 1621b1aaa546SPaolo Bonzini )+ 1622b1aaa546SPaolo Bonzini \)\)\s+//x; 16231da177e4SLinus Torvalds 16241da177e4SLinus Torvalds # Yes, this truly is vile. We are looking for: 16251da177e4SLinus Torvalds # 1. Return type (may be nothing if we're looking at a macro) 16261da177e4SLinus Torvalds # 2. Function name 16271da177e4SLinus Torvalds # 3. Function parameters. 16281da177e4SLinus Torvalds # 16291da177e4SLinus Torvalds # All the while we have to watch out for function pointer parameters 16301da177e4SLinus Torvalds # (which IIRC is what the two sections are for), C types (these 16311da177e4SLinus Torvalds # regexps don't even start to express all the possibilities), and 16321da177e4SLinus Torvalds # so on. 16331da177e4SLinus Torvalds # 16341da177e4SLinus Torvalds # If you mess with these regexps, it's a good idea to check that 16351da177e4SLinus Torvalds # the following functions' documentation still comes out right: 16361da177e4SLinus Torvalds # - parport_register_device (function pointer parameters) 16371da177e4SLinus Torvalds # - atomic_set (macro) 16389598f91fSMartin Waitz # - pci_match_device, __copy_to_user (long return type) 16391da177e4SLinus Torvalds 1640cbb4d3e6SHoria Geanta if ($define && $prototype =~ m/^()([a-zA-Z0-9_~:]+)\s+/) { 1641cbb4d3e6SHoria Geanta # This is an object-like macro, it has no return type and no parameter 1642cbb4d3e6SHoria Geanta # list. 1643cbb4d3e6SHoria Geanta # Function-like macros are not allowed to have spaces between 1644cbb4d3e6SHoria Geanta # declaration_name and opening parenthesis (notice the \s+). 1645cbb4d3e6SHoria Geanta $return_type = $1; 1646cbb4d3e6SHoria Geanta $declaration_name = $2; 1647cbb4d3e6SHoria Geanta $noret = 1; 1648cbb4d3e6SHoria Geanta } elsif ($prototype =~ m/^()([a-zA-Z0-9_~:]+)\s*\(([^\(]*)\)/ || 16491da177e4SLinus Torvalds $prototype =~ m/^(\w+)\s+([a-zA-Z0-9_~:]+)\s*\(([^\(]*)\)/ || 16505a0bc578SMatthew Wilcox $prototype =~ m/^(\w+\s*\*+)\s*([a-zA-Z0-9_~:]+)\s*\(([^\(]*)\)/ || 16511da177e4SLinus Torvalds $prototype =~ m/^(\w+\s+\w+)\s+([a-zA-Z0-9_~:]+)\s*\(([^\(]*)\)/ || 165294b3e03cSRandy Dunlap $prototype =~ m/^(\w+\s+\w+\s*\*+)\s*([a-zA-Z0-9_~:]+)\s*\(([^\(]*)\)/ || 16531da177e4SLinus Torvalds $prototype =~ m/^(\w+\s+\w+\s+\w+)\s+([a-zA-Z0-9_~:]+)\s*\(([^\(]*)\)/ || 16545a0bc578SMatthew Wilcox $prototype =~ m/^(\w+\s+\w+\s+\w+\s*\*+)\s*([a-zA-Z0-9_~:]+)\s*\(([^\(]*)\)/ || 16551da177e4SLinus Torvalds $prototype =~ m/^()([a-zA-Z0-9_~:]+)\s*\(([^\{]*)\)/ || 16561da177e4SLinus Torvalds $prototype =~ m/^(\w+)\s+([a-zA-Z0-9_~:]+)\s*\(([^\{]*)\)/ || 16575a0bc578SMatthew Wilcox $prototype =~ m/^(\w+\s*\*+)\s*([a-zA-Z0-9_~:]+)\s*\(([^\{]*)\)/ || 16581da177e4SLinus Torvalds $prototype =~ m/^(\w+\s+\w+)\s+([a-zA-Z0-9_~:]+)\s*\(([^\{]*)\)/ || 16595a0bc578SMatthew Wilcox $prototype =~ m/^(\w+\s+\w+\s*\*+)\s*([a-zA-Z0-9_~:]+)\s*\(([^\{]*)\)/ || 16601da177e4SLinus Torvalds $prototype =~ m/^(\w+\s+\w+\s+\w+)\s+([a-zA-Z0-9_~:]+)\s*\(([^\{]*)\)/ || 16615a0bc578SMatthew Wilcox $prototype =~ m/^(\w+\s+\w+\s+\w+\s*\*+)\s*([a-zA-Z0-9_~:]+)\s*\(([^\{]*)\)/ || 16629598f91fSMartin Waitz $prototype =~ m/^(\w+\s+\w+\s+\w+\s+\w+)\s+([a-zA-Z0-9_~:]+)\s*\(([^\{]*)\)/ || 16635a0bc578SMatthew Wilcox $prototype =~ m/^(\w+\s+\w+\s+\w+\s+\w+\s*\*+)\s*([a-zA-Z0-9_~:]+)\s*\(([^\{]*)\)/ || 16645a0bc578SMatthew Wilcox $prototype =~ m/^(\w+\s+\w+\s*\*+\s*\w+\s*\*+\s*)\s*([a-zA-Z0-9_~:]+)\s*\(([^\{]*)\)/) { 16651da177e4SLinus Torvalds $return_type = $1; 16661da177e4SLinus Torvalds $declaration_name = $2; 16671da177e4SLinus Torvalds my $args = $3; 16681da177e4SLinus Torvalds 1669151c468bSMauro Carvalho Chehab create_parameterlist($args, ',', $file, $declaration_name); 16701da177e4SLinus Torvalds } else { 1671d40e1e65SBart Van Assche print STDERR "${file}:$.: warning: cannot understand function prototype: '$prototype'\n"; 16721da177e4SLinus Torvalds return; 16731da177e4SLinus Torvalds } 16741da177e4SLinus Torvalds 1675a1d94aa5SRandy Dunlap my $prms = join " ", @parameterlist; 16761081de2dSMauro Carvalho Chehab check_sections($file, $declaration_name, "function", $sectcheck, $prms); 1677a1d94aa5SRandy Dunlap 16784092bac7SYacine Belkadi # This check emits a lot of warnings at the moment, because many 16794092bac7SYacine Belkadi # functions don't have a 'Return' doc section. So until the number 16804092bac7SYacine Belkadi # of warnings goes sufficiently down, the check is only performed in 16814092bac7SYacine Belkadi # verbose mode. 16824092bac7SYacine Belkadi # TODO: always perform the check. 1683cbb4d3e6SHoria Geanta if ($verbose && !$noret) { 16844092bac7SYacine Belkadi check_return_section($file, $declaration_name, $return_type); 16854092bac7SYacine Belkadi } 16864092bac7SYacine Belkadi 16871da177e4SLinus Torvalds output_declaration($declaration_name, 16881da177e4SLinus Torvalds 'function', 16891da177e4SLinus Torvalds {'function' => $declaration_name, 16901da177e4SLinus Torvalds 'module' => $modulename, 16911da177e4SLinus Torvalds 'functiontype' => $return_type, 16921da177e4SLinus Torvalds 'parameterlist' => \@parameterlist, 16931da177e4SLinus Torvalds 'parameterdescs' => \%parameterdescs, 16941da177e4SLinus Torvalds 'parametertypes' => \%parametertypes, 16951da177e4SLinus Torvalds 'sectionlist' => \@sectionlist, 16961da177e4SLinus Torvalds 'sections' => \%sections, 16971da177e4SLinus Torvalds 'purpose' => $declaration_purpose 16981da177e4SLinus Torvalds }); 16991da177e4SLinus Torvalds} 17001da177e4SLinus Torvalds 17011da177e4SLinus Torvaldssub reset_state { 17021da177e4SLinus Torvalds $function = ""; 17031da177e4SLinus Torvalds %parameterdescs = (); 17041da177e4SLinus Torvalds %parametertypes = (); 17051da177e4SLinus Torvalds @parameterlist = (); 17061da177e4SLinus Torvalds %sections = (); 17071da177e4SLinus Torvalds @sectionlist = (); 1708a1d94aa5SRandy Dunlap $sectcheck = ""; 1709a1d94aa5SRandy Dunlap $struct_actual = ""; 17101da177e4SLinus Torvalds $prototype = ""; 17111da177e4SLinus Torvalds 171248af606aSJani Nikula $state = STATE_NORMAL; 171348af606aSJani Nikula $inline_doc_state = STATE_INLINE_NA; 17141da177e4SLinus Torvalds} 17151da177e4SLinus Torvalds 171656afb0f8SJason Baronsub tracepoint_munge($) { 171756afb0f8SJason Baron my $file = shift; 171856afb0f8SJason Baron my $tracepointname = 0; 171956afb0f8SJason Baron my $tracepointargs = 0; 172056afb0f8SJason Baron 172156afb0f8SJason Baron if ($prototype =~ m/TRACE_EVENT\((.*?),/) { 172256afb0f8SJason Baron $tracepointname = $1; 172356afb0f8SJason Baron } 17243a9089fdSJason Baron if ($prototype =~ m/DEFINE_SINGLE_EVENT\((.*?),/) { 17253a9089fdSJason Baron $tracepointname = $1; 17263a9089fdSJason Baron } 17273a9089fdSJason Baron if ($prototype =~ m/DEFINE_EVENT\((.*?),(.*?),/) { 17283a9089fdSJason Baron $tracepointname = $2; 17293a9089fdSJason Baron } 17303a9089fdSJason Baron $tracepointname =~ s/^\s+//; #strip leading whitespace 173156afb0f8SJason Baron if ($prototype =~ m/TP_PROTO\((.*?)\)/) { 173256afb0f8SJason Baron $tracepointargs = $1; 173356afb0f8SJason Baron } 173456afb0f8SJason Baron if (($tracepointname eq 0) || ($tracepointargs eq 0)) { 1735d40e1e65SBart Van Assche print STDERR "${file}:$.: warning: Unrecognized tracepoint format: \n". 173656afb0f8SJason Baron "$prototype\n"; 173756afb0f8SJason Baron } else { 173856afb0f8SJason Baron $prototype = "static inline void trace_$tracepointname($tracepointargs)"; 173956afb0f8SJason Baron } 174056afb0f8SJason Baron} 174156afb0f8SJason Baron 1742b4870bc5SRandy Dunlapsub syscall_munge() { 1743b4870bc5SRandy Dunlap my $void = 0; 1744b4870bc5SRandy Dunlap 17457c9aa015SMauro Carvalho Chehab $prototype =~ s@[\r\n]+@ @gos; # strip newlines/CR's 1746b4870bc5SRandy Dunlap## if ($prototype =~ m/SYSCALL_DEFINE0\s*\(\s*(a-zA-Z0-9_)*\s*\)/) { 1747b4870bc5SRandy Dunlap if ($prototype =~ m/SYSCALL_DEFINE0/) { 1748b4870bc5SRandy Dunlap $void = 1; 1749b4870bc5SRandy Dunlap## $prototype = "long sys_$1(void)"; 1750b4870bc5SRandy Dunlap } 1751b4870bc5SRandy Dunlap 1752b4870bc5SRandy Dunlap $prototype =~ s/SYSCALL_DEFINE.*\(/long sys_/; # fix return type & func name 1753b4870bc5SRandy Dunlap if ($prototype =~ m/long (sys_.*?),/) { 1754b4870bc5SRandy Dunlap $prototype =~ s/,/\(/; 1755b4870bc5SRandy Dunlap } elsif ($void) { 1756b4870bc5SRandy Dunlap $prototype =~ s/\)/\(void\)/; 1757b4870bc5SRandy Dunlap } 1758b4870bc5SRandy Dunlap 1759b4870bc5SRandy Dunlap # now delete all of the odd-number commas in $prototype 1760b4870bc5SRandy Dunlap # so that arg types & arg names don't have a comma between them 1761b4870bc5SRandy Dunlap my $count = 0; 1762b4870bc5SRandy Dunlap my $len = length($prototype); 1763b4870bc5SRandy Dunlap if ($void) { 1764b4870bc5SRandy Dunlap $len = 0; # skip the for-loop 1765b4870bc5SRandy Dunlap } 1766b4870bc5SRandy Dunlap for (my $ix = 0; $ix < $len; $ix++) { 1767b4870bc5SRandy Dunlap if (substr($prototype, $ix, 1) eq ',') { 1768b4870bc5SRandy Dunlap $count++; 1769b4870bc5SRandy Dunlap if ($count % 2 == 1) { 1770b4870bc5SRandy Dunlap substr($prototype, $ix, 1) = ' '; 1771b4870bc5SRandy Dunlap } 1772b4870bc5SRandy Dunlap } 1773b4870bc5SRandy Dunlap } 1774b4870bc5SRandy Dunlap} 1775b4870bc5SRandy Dunlap 1776b7afa92bSDaniel Vettersub process_proto_function($$) { 17771da177e4SLinus Torvalds my $x = shift; 17781da177e4SLinus Torvalds my $file = shift; 17791da177e4SLinus Torvalds 178051f5a0c8SRandy Dunlap $x =~ s@\/\/.*$@@gos; # strip C99-style comments to end of line 178151f5a0c8SRandy Dunlap 1782890c78c2SRandy Dunlap if ($x =~ m#\s*/\*\s+MACDOC\s*#io || ($x =~ /^#/ && $x !~ /^#\s*define/)) { 17831da177e4SLinus Torvalds # do nothing 17841da177e4SLinus Torvalds } 17851da177e4SLinus Torvalds elsif ($x =~ /([^\{]*)/) { 17861da177e4SLinus Torvalds $prototype .= $1; 17871da177e4SLinus Torvalds } 1788b4870bc5SRandy Dunlap 1789890c78c2SRandy Dunlap if (($x =~ /\{/) || ($x =~ /\#\s*define/) || ($x =~ /;/)) { 17901da177e4SLinus Torvalds $prototype =~ s@/\*.*?\*/@@gos; # strip comments. 17911da177e4SLinus Torvalds $prototype =~ s@[\r\n]+@ @gos; # strip newlines/cr's. 17921da177e4SLinus Torvalds $prototype =~ s@^\s+@@gos; # strip leading spaces 17937ae281b0SMauro Carvalho Chehab 17947ae281b0SMauro Carvalho Chehab # Handle prototypes for function pointers like: 17957ae281b0SMauro Carvalho Chehab # int (*pcs_config)(struct foo) 17967ae281b0SMauro Carvalho Chehab $prototype =~ s@^(\S+\s+)\(\s*\*(\S+)\)@$1$2@gos; 17977ae281b0SMauro Carvalho Chehab 1798b4870bc5SRandy Dunlap if ($prototype =~ /SYSCALL_DEFINE/) { 1799b4870bc5SRandy Dunlap syscall_munge(); 1800b4870bc5SRandy Dunlap } 18013a9089fdSJason Baron if ($prototype =~ /TRACE_EVENT/ || $prototype =~ /DEFINE_EVENT/ || 18023a9089fdSJason Baron $prototype =~ /DEFINE_SINGLE_EVENT/) 18033a9089fdSJason Baron { 180456afb0f8SJason Baron tracepoint_munge($file); 180556afb0f8SJason Baron } 18061da177e4SLinus Torvalds dump_function($prototype, $file); 18071da177e4SLinus Torvalds reset_state(); 18081da177e4SLinus Torvalds } 18091da177e4SLinus Torvalds} 18101da177e4SLinus Torvalds 1811b7afa92bSDaniel Vettersub process_proto_type($$) { 18121da177e4SLinus Torvalds my $x = shift; 18131da177e4SLinus Torvalds my $file = shift; 18141da177e4SLinus Torvalds 18151da177e4SLinus Torvalds $x =~ s@[\r\n]+@ @gos; # strip newlines/cr's. 18161da177e4SLinus Torvalds $x =~ s@^\s+@@gos; # strip leading spaces 18171da177e4SLinus Torvalds $x =~ s@\s+$@@gos; # strip trailing spaces 181851f5a0c8SRandy Dunlap $x =~ s@\/\/.*$@@gos; # strip C99-style comments to end of line 181951f5a0c8SRandy Dunlap 18201da177e4SLinus Torvalds if ($x =~ /^#/) { 18211da177e4SLinus Torvalds # To distinguish preprocessor directive from regular declaration later. 18221da177e4SLinus Torvalds $x .= ";"; 18231da177e4SLinus Torvalds } 18241da177e4SLinus Torvalds 18251da177e4SLinus Torvalds while (1) { 1826673bb2dfSBen Hutchings if ( $x =~ /([^\{\};]*)([\{\};])(.*)/ ) { 1827463a0fdcSMarkus Heiser if( length $prototype ) { 1828463a0fdcSMarkus Heiser $prototype .= " " 1829463a0fdcSMarkus Heiser } 18301da177e4SLinus Torvalds $prototype .= $1 . $2; 18311da177e4SLinus Torvalds ($2 eq '{') && $brcount++; 18321da177e4SLinus Torvalds ($2 eq '}') && $brcount--; 18331da177e4SLinus Torvalds if (($2 eq ';') && ($brcount == 0)) { 18341da177e4SLinus Torvalds dump_declaration($prototype, $file); 18351da177e4SLinus Torvalds reset_state(); 18361da177e4SLinus Torvalds last; 18371da177e4SLinus Torvalds } 18381da177e4SLinus Torvalds $x = $3; 18391da177e4SLinus Torvalds } else { 18401da177e4SLinus Torvalds $prototype .= $x; 18411da177e4SLinus Torvalds last; 18421da177e4SLinus Torvalds } 18431da177e4SLinus Torvalds } 18441da177e4SLinus Torvalds} 18451da177e4SLinus Torvalds 18466b5b55f6SRandy Dunlap 18471ad560e4SJani Nikulasub map_filename($) { 18481ad560e4SJani Nikula my $file; 18491ad560e4SJani Nikula my ($orig_file) = @_; 18501ad560e4SJani Nikula 18511ad560e4SJani Nikula if (defined($ENV{'SRCTREE'})) { 18521ad560e4SJani Nikula $file = "$ENV{'SRCTREE'}" . "/" . $orig_file; 18531ad560e4SJani Nikula } else { 18541ad560e4SJani Nikula $file = $orig_file; 18551ad560e4SJani Nikula } 18561ad560e4SJani Nikula 18571ad560e4SJani Nikula if (defined($source_map{$file})) { 18581ad560e4SJani Nikula $file = $source_map{$file}; 18591ad560e4SJani Nikula } 18601ad560e4SJani Nikula 18611ad560e4SJani Nikula return $file; 18621ad560e4SJani Nikula} 18631ad560e4SJani Nikula 186488c2b57dSJani Nikulasub process_export_file($) { 186588c2b57dSJani Nikula my ($orig_file) = @_; 186688c2b57dSJani Nikula my $file = map_filename($orig_file); 186788c2b57dSJani Nikula 186888c2b57dSJani Nikula if (!open(IN,"<$file")) { 186988c2b57dSJani Nikula print STDERR "Error: Cannot open file $file\n"; 187088c2b57dSJani Nikula ++$errors; 187188c2b57dSJani Nikula return; 187288c2b57dSJani Nikula } 187388c2b57dSJani Nikula 187488c2b57dSJani Nikula while (<IN>) { 187588c2b57dSJani Nikula if (/$export_symbol/) { 187688c2b57dSJani Nikula $function_table{$2} = 1; 187788c2b57dSJani Nikula } 187888c2b57dSJani Nikula } 187988c2b57dSJani Nikula 188088c2b57dSJani Nikula close(IN); 188188c2b57dSJani Nikula} 188288c2b57dSJani Nikula 188307048d13SJonathan Corbet# 188407048d13SJonathan Corbet# Parsers for the various processing states. 188507048d13SJonathan Corbet# 188607048d13SJonathan Corbet# STATE_NORMAL: looking for the /** to begin everything. 188707048d13SJonathan Corbet# 188807048d13SJonathan Corbetsub process_normal() { 18891da177e4SLinus Torvalds if (/$doc_start/o) { 189048af606aSJani Nikula $state = STATE_NAME; # next line is always the function name 1891850622dfSRandy Dunlap $in_doc_sect = 0; 18920b0f5f29SDaniel Vetter $declaration_start_line = $. + 1; 18931da177e4SLinus Torvalds } 189407048d13SJonathan Corbet} 189507048d13SJonathan Corbet 18963cac2bc4SJonathan Corbet# 18973cac2bc4SJonathan Corbet# STATE_NAME: Looking for the "name - description" line 18983cac2bc4SJonathan Corbet# 18993cac2bc4SJonathan Corbetsub process_name($$) { 19003cac2bc4SJonathan Corbet my $file = shift; 19011da177e4SLinus Torvalds my $identifier; 19021da177e4SLinus Torvalds my $descr; 19031da177e4SLinus Torvalds 19041da177e4SLinus Torvalds if (/$doc_block/o) { 190548af606aSJani Nikula $state = STATE_DOCBLOCK; 19061da177e4SLinus Torvalds $contents = ""; 19070b0f5f29SDaniel Vetter $new_start_line = $. + 1; 19080b0f5f29SDaniel Vetter 19091da177e4SLinus Torvalds if ( $1 eq "" ) { 19101da177e4SLinus Torvalds $section = $section_intro; 19111da177e4SLinus Torvalds } else { 19121da177e4SLinus Torvalds $section = $1; 19131da177e4SLinus Torvalds } 19141da177e4SLinus Torvalds } 19151da177e4SLinus Torvalds elsif (/$doc_decl/o) { 19161da177e4SLinus Torvalds $identifier = $1; 1917a8dae20bSMike Rapoport if (/\s*([\w\s]+?)(\(\))?\s*-/) { 19181da177e4SLinus Torvalds $identifier = $1; 19191da177e4SLinus Torvalds } 19201da177e4SLinus Torvalds 192117b78717SJonathan Corbet $state = STATE_BODY; 19220b0f5f29SDaniel Vetter # if there's no @param blocks need to set up default section 19230b0f5f29SDaniel Vetter # here 19242f4ad40aSJani Nikula $contents = ""; 19252f4ad40aSJani Nikula $section = $section_default; 19260b0f5f29SDaniel Vetter $new_start_line = $. + 1; 19271da177e4SLinus Torvalds if (/-(.*)/) { 192851f5a0c8SRandy Dunlap # strip leading/trailing/multiple spaces 1929a21217daSRandy Dunlap $descr= $1; 1930a21217daSRandy Dunlap $descr =~ s/^\s*//; 1931a21217daSRandy Dunlap $descr =~ s/\s*$//; 193212ae6779SDaniel Santos $descr =~ s/\s+/ /g; 19330bba924cSJonathan Corbet $declaration_purpose = $descr; 193417b78717SJonathan Corbet $state = STATE_BODY_MAYBE; 19351da177e4SLinus Torvalds } else { 19361da177e4SLinus Torvalds $declaration_purpose = ""; 19371da177e4SLinus Torvalds } 193877cc23b8SRandy Dunlap 193977cc23b8SRandy Dunlap if (($declaration_purpose eq "") && $verbose) { 1940d40e1e65SBart Van Assche print STDERR "${file}:$.: warning: missing initial short description on line:\n"; 194177cc23b8SRandy Dunlap print STDERR $_; 194277cc23b8SRandy Dunlap ++$warnings; 194377cc23b8SRandy Dunlap } 194477cc23b8SRandy Dunlap 1945cf419d54SRandy Dunlap if ($identifier =~ m/^struct\b/) { 19461da177e4SLinus Torvalds $decl_type = 'struct'; 1947cf419d54SRandy Dunlap } elsif ($identifier =~ m/^union\b/) { 19481da177e4SLinus Torvalds $decl_type = 'union'; 1949cf419d54SRandy Dunlap } elsif ($identifier =~ m/^enum\b/) { 19501da177e4SLinus Torvalds $decl_type = 'enum'; 1951cf419d54SRandy Dunlap } elsif ($identifier =~ m/^typedef\b/) { 19521da177e4SLinus Torvalds $decl_type = 'typedef'; 19531da177e4SLinus Torvalds } else { 19541da177e4SLinus Torvalds $decl_type = 'function'; 19551da177e4SLinus Torvalds } 19561da177e4SLinus Torvalds 19571da177e4SLinus Torvalds if ($verbose) { 1958d40e1e65SBart Van Assche print STDERR "${file}:$.: info: Scanning doc for $identifier\n"; 19591da177e4SLinus Torvalds } 19601da177e4SLinus Torvalds } else { 1961d40e1e65SBart Van Assche print STDERR "${file}:$.: warning: Cannot understand $_ on line $.", 19621da177e4SLinus Torvalds " - I thought it was a doc line\n"; 19631da177e4SLinus Torvalds ++$warnings; 196448af606aSJani Nikula $state = STATE_NORMAL; 19651da177e4SLinus Torvalds } 19663cac2bc4SJonathan Corbet} 19673cac2bc4SJonathan Corbet 19683cac2bc4SJonathan Corbet 1969d742f24dSJonathan Corbet# 1970d742f24dSJonathan Corbet# STATE_BODY and STATE_BODY_MAYBE: the bulk of a kerneldoc comment. 1971d742f24dSJonathan Corbet# 1972d742f24dSJonathan Corbetsub process_body($$) { 1973d742f24dSJonathan Corbet my $file = shift; 19743cac2bc4SJonathan Corbet 197543756e34SJonathan Neuschäfer # Until all named variable macro parameters are 197643756e34SJonathan Neuschäfer # documented using the bare name (`x`) rather than with 197743756e34SJonathan Neuschäfer # dots (`x...`), strip the dots: 197843756e34SJonathan Neuschäfer if ($section =~ /\w\.\.\.$/) { 197943756e34SJonathan Neuschäfer $section =~ s/\.\.\.$//; 198043756e34SJonathan Neuschäfer 198143756e34SJonathan Neuschäfer if ($verbose) { 198243756e34SJonathan Neuschäfer print STDERR "${file}:$.: warning: Variable macro arguments should be documented without dots\n"; 198343756e34SJonathan Neuschäfer ++$warnings; 198443756e34SJonathan Neuschäfer } 198543756e34SJonathan Neuschäfer } 198643756e34SJonathan Neuschäfer 19870d55d48bSMauro Carvalho Chehab if ($state == STATE_BODY_WITH_BLANK_LINE && /^\s*\*\s?\S/) { 19880d55d48bSMauro Carvalho Chehab dump_section($file, $section, $contents); 19890d55d48bSMauro Carvalho Chehab $section = $section_default; 19900d55d48bSMauro Carvalho Chehab $contents = ""; 19910d55d48bSMauro Carvalho Chehab } 19920d55d48bSMauro Carvalho Chehab 1993f624adefSJani Nikula if (/$doc_sect/i) { # case insensitive for supported section names 19941da177e4SLinus Torvalds $newsection = $1; 19951da177e4SLinus Torvalds $newcontents = $2; 19961da177e4SLinus Torvalds 1997f624adefSJani Nikula # map the supported section names to the canonical names 1998f624adefSJani Nikula if ($newsection =~ m/^description$/i) { 1999f624adefSJani Nikula $newsection = $section_default; 2000f624adefSJani Nikula } elsif ($newsection =~ m/^context$/i) { 2001f624adefSJani Nikula $newsection = $section_context; 2002f624adefSJani Nikula } elsif ($newsection =~ m/^returns?$/i) { 2003f624adefSJani Nikula $newsection = $section_return; 2004f624adefSJani Nikula } elsif ($newsection =~ m/^\@return$/) { 2005f624adefSJani Nikula # special: @return is a section, not a param description 2006f624adefSJani Nikula $newsection = $section_return; 2007f624adefSJani Nikula } 2008f624adefSJani Nikula 2009792aa2f2SRandy Dunlap if (($contents ne "") && ($contents ne "\n")) { 2010850622dfSRandy Dunlap if (!$in_doc_sect && $verbose) { 2011d40e1e65SBart Van Assche print STDERR "${file}:$.: warning: contents before sections\n"; 2012850622dfSRandy Dunlap ++$warnings; 2013850622dfSRandy Dunlap } 20140bba924cSJonathan Corbet dump_section($file, $section, $contents); 20151da177e4SLinus Torvalds $section = $section_default; 20161da177e4SLinus Torvalds } 20171da177e4SLinus Torvalds 2018850622dfSRandy Dunlap $in_doc_sect = 1; 201917b78717SJonathan Corbet $state = STATE_BODY; 20201da177e4SLinus Torvalds $contents = $newcontents; 20210b0f5f29SDaniel Vetter $new_start_line = $.; 20227c9aa015SMauro Carvalho Chehab while (substr($contents, 0, 1) eq " ") { 202305189497SRandy Dunlap $contents = substr($contents, 1); 202405189497SRandy Dunlap } 20250a726301SJani Nikula if ($contents ne "") { 20261da177e4SLinus Torvalds $contents .= "\n"; 20271da177e4SLinus Torvalds } 20281da177e4SLinus Torvalds $section = $newsection; 2029b7886de4SJani Nikula $leading_space = undef; 20301da177e4SLinus Torvalds } elsif (/$doc_end/) { 20314c98ecafSRandy Dunlap if (($contents ne "") && ($contents ne "\n")) { 20320bba924cSJonathan Corbet dump_section($file, $section, $contents); 20331da177e4SLinus Torvalds $section = $section_default; 20341da177e4SLinus Torvalds $contents = ""; 20351da177e4SLinus Torvalds } 203646b958ebSRandy Dunlap # look for doc_com + <text> + doc_end: 203746b958ebSRandy Dunlap if ($_ =~ m'\s*\*\s*[a-zA-Z_0-9:\.]+\*/') { 2038d40e1e65SBart Van Assche print STDERR "${file}:$.: warning: suspicious ending line: $_"; 203946b958ebSRandy Dunlap ++$warnings; 204046b958ebSRandy Dunlap } 20411da177e4SLinus Torvalds 20421da177e4SLinus Torvalds $prototype = ""; 204348af606aSJani Nikula $state = STATE_PROTO; 20441da177e4SLinus Torvalds $brcount = 0; 20451da177e4SLinus Torvalds } elsif (/$doc_content/) { 20466423133bSJohannes Weiner if ($1 eq "") { 20470d55d48bSMauro Carvalho Chehab if ($section eq $section_context) { 20480bba924cSJonathan Corbet dump_section($file, $section, $contents); 20491da177e4SLinus Torvalds $section = $section_default; 20501da177e4SLinus Torvalds $contents = ""; 20510b0f5f29SDaniel Vetter $new_start_line = $.; 20520d55d48bSMauro Carvalho Chehab $state = STATE_BODY; 20531da177e4SLinus Torvalds } else { 20540d55d48bSMauro Carvalho Chehab if ($section ne $section_default) { 20550d55d48bSMauro Carvalho Chehab $state = STATE_BODY_WITH_BLANK_LINE; 20560d55d48bSMauro Carvalho Chehab } else { 20570d55d48bSMauro Carvalho Chehab $state = STATE_BODY; 20580d55d48bSMauro Carvalho Chehab } 20596423133bSJohannes Weiner $contents .= "\n"; 20606423133bSJohannes Weiner } 206117b78717SJonathan Corbet } elsif ($state == STATE_BODY_MAYBE) { 20626423133bSJohannes Weiner # Continued declaration purpose 20636423133bSJohannes Weiner chomp($declaration_purpose); 20640bba924cSJonathan Corbet $declaration_purpose .= " " . $1; 206512ae6779SDaniel Santos $declaration_purpose =~ s/\s+/ /g; 20666423133bSJohannes Weiner } else { 2067b7886de4SJani Nikula my $cont = $1; 2068b7886de4SJani Nikula if ($section =~ m/^@/ || $section eq $section_context) { 2069b7886de4SJani Nikula if (!defined $leading_space) { 2070b7886de4SJani Nikula if ($cont =~ m/^(\s+)/) { 2071b7886de4SJani Nikula $leading_space = $1; 2072b7886de4SJani Nikula } else { 2073b7886de4SJani Nikula $leading_space = ""; 2074b7886de4SJani Nikula } 2075b7886de4SJani Nikula } 2076b7886de4SJani Nikula $cont =~ s/^$leading_space//; 2077b7886de4SJani Nikula } 2078b7886de4SJani Nikula $contents .= $cont . "\n"; 20791da177e4SLinus Torvalds } 20801da177e4SLinus Torvalds } else { 20811da177e4SLinus Torvalds # i dont know - bad line? ignore. 2082d40e1e65SBart Van Assche print STDERR "${file}:$.: warning: bad line: $_"; 20831da177e4SLinus Torvalds ++$warnings; 20841da177e4SLinus Torvalds } 2085d742f24dSJonathan Corbet} 2086d742f24dSJonathan Corbet 2087d742f24dSJonathan Corbet 2088cc794812SJonathan Corbet# 2089cc794812SJonathan Corbet# STATE_PROTO: reading a function/whatever prototype. 2090cc794812SJonathan Corbet# 2091cc794812SJonathan Corbetsub process_proto($$) { 2092cc794812SJonathan Corbet my $file = shift; 2093cc794812SJonathan Corbet 2094cc794812SJonathan Corbet if (/$doc_inline_oneline/) { 2095cc794812SJonathan Corbet $section = $1; 2096cc794812SJonathan Corbet $contents = $2; 2097cc794812SJonathan Corbet if ($contents ne "") { 2098cc794812SJonathan Corbet $contents .= "\n"; 2099cc794812SJonathan Corbet dump_section($file, $section, $contents); 2100cc794812SJonathan Corbet $section = $section_default; 2101cc794812SJonathan Corbet $contents = ""; 2102cc794812SJonathan Corbet } 2103cc794812SJonathan Corbet } elsif (/$doc_inline_start/) { 2104cc794812SJonathan Corbet $state = STATE_INLINE; 2105cc794812SJonathan Corbet $inline_doc_state = STATE_INLINE_NAME; 2106cc794812SJonathan Corbet } elsif ($decl_type eq 'function') { 2107cc794812SJonathan Corbet process_proto_function($_, $file); 2108cc794812SJonathan Corbet } else { 2109cc794812SJonathan Corbet process_proto_type($_, $file); 2110cc794812SJonathan Corbet } 2111cc794812SJonathan Corbet} 2112cc794812SJonathan Corbet 2113c17add56SJonathan Corbet# 2114c17add56SJonathan Corbet# STATE_DOCBLOCK: within a DOC: block. 2115c17add56SJonathan Corbet# 2116c17add56SJonathan Corbetsub process_docblock($$) { 2117c17add56SJonathan Corbet my $file = shift; 2118cc794812SJonathan Corbet 2119c17add56SJonathan Corbet if (/$doc_end/) { 2120c17add56SJonathan Corbet dump_doc_section($file, $section, $contents); 2121c17add56SJonathan Corbet $section = $section_default; 2122c17add56SJonathan Corbet $contents = ""; 2123c17add56SJonathan Corbet $function = ""; 2124c17add56SJonathan Corbet %parameterdescs = (); 2125c17add56SJonathan Corbet %parametertypes = (); 2126c17add56SJonathan Corbet @parameterlist = (); 2127c17add56SJonathan Corbet %sections = (); 2128c17add56SJonathan Corbet @sectionlist = (); 2129c17add56SJonathan Corbet $prototype = ""; 2130c17add56SJonathan Corbet $state = STATE_NORMAL; 2131c17add56SJonathan Corbet } elsif (/$doc_content/) { 2132c17add56SJonathan Corbet if ( $1 eq "" ) { 2133c17add56SJonathan Corbet $contents .= $blankline; 2134c17add56SJonathan Corbet } else { 2135c17add56SJonathan Corbet $contents .= $1 . "\n"; 2136c17add56SJonathan Corbet } 2137c17add56SJonathan Corbet } 2138d742f24dSJonathan Corbet} 2139d742f24dSJonathan Corbet 2140c17add56SJonathan Corbet# 2141c17add56SJonathan Corbet# STATE_INLINE: docbook comments within a prototype. 2142c17add56SJonathan Corbet# 2143c17add56SJonathan Corbetsub process_inline($$) { 2144c17add56SJonathan Corbet my $file = shift; 2145d742f24dSJonathan Corbet 2146a4c6ebedSDanilo Cesar Lemes de Paula # First line (state 1) needs to be a @parameter 214748af606aSJani Nikula if ($inline_doc_state == STATE_INLINE_NAME && /$doc_inline_sect/o) { 2148a4c6ebedSDanilo Cesar Lemes de Paula $section = $1; 2149a4c6ebedSDanilo Cesar Lemes de Paula $contents = $2; 21500b0f5f29SDaniel Vetter $new_start_line = $.; 2151a4c6ebedSDanilo Cesar Lemes de Paula if ($contents ne "") { 21527c9aa015SMauro Carvalho Chehab while (substr($contents, 0, 1) eq " ") { 2153a4c6ebedSDanilo Cesar Lemes de Paula $contents = substr($contents, 1); 2154a4c6ebedSDanilo Cesar Lemes de Paula } 2155a4c6ebedSDanilo Cesar Lemes de Paula $contents .= "\n"; 2156a4c6ebedSDanilo Cesar Lemes de Paula } 215748af606aSJani Nikula $inline_doc_state = STATE_INLINE_TEXT; 2158a4c6ebedSDanilo Cesar Lemes de Paula # Documentation block end */ 215948af606aSJani Nikula } elsif (/$doc_inline_end/) { 2160a4c6ebedSDanilo Cesar Lemes de Paula if (($contents ne "") && ($contents ne "\n")) { 21610bba924cSJonathan Corbet dump_section($file, $section, $contents); 2162a4c6ebedSDanilo Cesar Lemes de Paula $section = $section_default; 2163a4c6ebedSDanilo Cesar Lemes de Paula $contents = ""; 2164a4c6ebedSDanilo Cesar Lemes de Paula } 216548af606aSJani Nikula $state = STATE_PROTO; 216648af606aSJani Nikula $inline_doc_state = STATE_INLINE_NA; 2167a4c6ebedSDanilo Cesar Lemes de Paula # Regular text 2168a4c6ebedSDanilo Cesar Lemes de Paula } elsif (/$doc_content/) { 216948af606aSJani Nikula if ($inline_doc_state == STATE_INLINE_TEXT) { 2170a4c6ebedSDanilo Cesar Lemes de Paula $contents .= $1 . "\n"; 21716450c895SJani Nikula # nuke leading blank lines 21726450c895SJani Nikula if ($contents =~ /^\s*$/) { 21736450c895SJani Nikula $contents = ""; 21746450c895SJani Nikula } 217548af606aSJani Nikula } elsif ($inline_doc_state == STATE_INLINE_NAME) { 217648af606aSJani Nikula $inline_doc_state = STATE_INLINE_ERROR; 2177e7ca311eSDaniel Vetter print STDERR "${file}:$.: warning: "; 2178a4c6ebedSDanilo Cesar Lemes de Paula print STDERR "Incorrect use of kernel-doc format: $_"; 2179a4c6ebedSDanilo Cesar Lemes de Paula ++$warnings; 2180a4c6ebedSDanilo Cesar Lemes de Paula } 2181a4c6ebedSDanilo Cesar Lemes de Paula } 21820c9aa209SJani Nikula} 2183c17add56SJonathan Corbet 2184c17add56SJonathan Corbet 2185c17add56SJonathan Corbetsub process_file($) { 2186c17add56SJonathan Corbet my $file; 2187c17add56SJonathan Corbet my $initial_section_counter = $section_counter; 2188c17add56SJonathan Corbet my ($orig_file) = @_; 2189c17add56SJonathan Corbet 2190c17add56SJonathan Corbet $file = map_filename($orig_file); 2191c17add56SJonathan Corbet 2192c17add56SJonathan Corbet if (!open(IN,"<$file")) { 2193c17add56SJonathan Corbet print STDERR "Error: Cannot open file $file\n"; 2194c17add56SJonathan Corbet ++$errors; 2195c17add56SJonathan Corbet return; 21961da177e4SLinus Torvalds } 2197c17add56SJonathan Corbet 2198c17add56SJonathan Corbet $. = 1; 2199c17add56SJonathan Corbet 2200c17add56SJonathan Corbet $section_counter = 0; 2201c17add56SJonathan Corbet while (<IN>) { 2202c17add56SJonathan Corbet while (s/\\\s*$//) { 2203c17add56SJonathan Corbet $_ .= <IN>; 2204c17add56SJonathan Corbet } 2205c17add56SJonathan Corbet # Replace tabs by spaces 2206c17add56SJonathan Corbet while ($_ =~ s/\t+/' ' x (length($&) * 8 - length($`) % 8)/e) {}; 2207c17add56SJonathan Corbet # Hand this line to the appropriate state handler 2208c17add56SJonathan Corbet if ($state == STATE_NORMAL) { 2209c17add56SJonathan Corbet process_normal(); 2210c17add56SJonathan Corbet } elsif ($state == STATE_NAME) { 2211c17add56SJonathan Corbet process_name($file, $_); 22120d55d48bSMauro Carvalho Chehab } elsif ($state == STATE_BODY || $state == STATE_BODY_MAYBE || 22130d55d48bSMauro Carvalho Chehab $state == STATE_BODY_WITH_BLANK_LINE) { 2214c17add56SJonathan Corbet process_body($file, $_); 2215c17add56SJonathan Corbet } elsif ($state == STATE_INLINE) { # scanning for inline parameters 2216c17add56SJonathan Corbet process_inline($file, $_); 2217cc794812SJonathan Corbet } elsif ($state == STATE_PROTO) { 2218cc794812SJonathan Corbet process_proto($file, $_); 221948af606aSJani Nikula } elsif ($state == STATE_DOCBLOCK) { 2220c17add56SJonathan Corbet process_docblock($file, $_); 22211da177e4SLinus Torvalds } 22221da177e4SLinus Torvalds } 2223c17add56SJonathan Corbet 2224c17add56SJonathan Corbet # Make sure we got something interesting. 2225b0d60bfbSJonathan Corbet if ($initial_section_counter == $section_counter && $ 2226b0d60bfbSJonathan Corbet output_mode ne "none") { 2227b0d60bfbSJonathan Corbet if ($output_selection == OUTPUT_INCLUDE) { 2228b0d60bfbSJonathan Corbet print STDERR "${file}:1: warning: '$_' not found\n" 2229b0d60bfbSJonathan Corbet for keys %function_table; 22303a025e1dSMatthew Wilcox } 2231b0d60bfbSJonathan Corbet else { 2232b0d60bfbSJonathan Corbet print STDERR "${file}:1: warning: no structured comments found\n"; 2233e946c43aSJohannes Berg } 22341da177e4SLinus Torvalds } 22351da177e4SLinus Torvalds} 22368484baaaSRandy Dunlap 22378484baaaSRandy Dunlap 22388484baaaSRandy Dunlap$kernelversion = get_kernel_version(); 22398484baaaSRandy Dunlap 22408484baaaSRandy Dunlap# generate a sequence of code that will splice in highlighting information 22418484baaaSRandy Dunlap# using the s// operator. 22421ef06233SMauro Carvalho Chehabfor (my $k = 0; $k < @highlights; $k++) { 22434d732701SDanilo Cesar Lemes de Paula my $pattern = $highlights[$k][0]; 22444d732701SDanilo Cesar Lemes de Paula my $result = $highlights[$k][1]; 22454d732701SDanilo Cesar Lemes de Paula# print STDERR "scanning pattern:$pattern, highlight:($result)\n"; 22464d732701SDanilo Cesar Lemes de Paula $dohighlight .= "\$contents =~ s:$pattern:$result:gs;\n"; 22478484baaaSRandy Dunlap} 22488484baaaSRandy Dunlap 22498484baaaSRandy Dunlap# Read the file that maps relative names to absolute names for 22508484baaaSRandy Dunlap# separate source and object directories and for shadow trees. 22518484baaaSRandy Dunlapif (open(SOURCE_MAP, "<.tmp_filelist.txt")) { 22528484baaaSRandy Dunlap my ($relname, $absname); 22538484baaaSRandy Dunlap while(<SOURCE_MAP>) { 22548484baaaSRandy Dunlap chop(); 22558484baaaSRandy Dunlap ($relname, $absname) = (split())[0..1]; 22568484baaaSRandy Dunlap $relname =~ s:^/+::; 22578484baaaSRandy Dunlap $source_map{$relname} = $absname; 22588484baaaSRandy Dunlap } 22598484baaaSRandy Dunlap close(SOURCE_MAP); 22608484baaaSRandy Dunlap} 22618484baaaSRandy Dunlap 226288c2b57dSJani Nikulaif ($output_selection == OUTPUT_EXPORTED || 226388c2b57dSJani Nikula $output_selection == OUTPUT_INTERNAL) { 2264c9b2cfb3SJani Nikula 2265c9b2cfb3SJani Nikula push(@export_file_list, @ARGV); 2266c9b2cfb3SJani Nikula 226788c2b57dSJani Nikula foreach (@export_file_list) { 226888c2b57dSJani Nikula chomp; 226988c2b57dSJani Nikula process_export_file($_); 227088c2b57dSJani Nikula } 227188c2b57dSJani Nikula} 227288c2b57dSJani Nikula 22738484baaaSRandy Dunlapforeach (@ARGV) { 22748484baaaSRandy Dunlap chomp; 22758484baaaSRandy Dunlap process_file($_); 22768484baaaSRandy Dunlap} 22778484baaaSRandy Dunlapif ($verbose && $errors) { 22788484baaaSRandy Dunlap print STDERR "$errors errors\n"; 22798484baaaSRandy Dunlap} 22808484baaaSRandy Dunlapif ($verbose && $warnings) { 22818484baaaSRandy Dunlap print STDERR "$warnings warnings\n"; 22828484baaaSRandy Dunlap} 22838484baaaSRandy Dunlap 22842c12c810SPierre-Louis Bossartif ($Werror && $warnings) { 22852c12c810SPierre-Louis Bossart print STDERR "$warnings warnings as Errors\n"; 22862c12c810SPierre-Louis Bossart exit($warnings); 22872c12c810SPierre-Louis Bossart} else { 22882c12c810SPierre-Louis Bossart exit($output_mode eq "none" ? 0 : $errors) 22892c12c810SPierre-Louis Bossart} 2290