1cb77f0d6SKamil Rytarowski#!/usr/bin/env perl 21da177e4SLinus Torvalds 3cb77f0d6SKamil Rytarowskiuse warnings; 41da177e4SLinus Torvaldsuse strict; 51da177e4SLinus Torvalds 61da177e4SLinus Torvalds## Copyright (c) 1998 Michael Zucchi, All Rights Reserved ## 71da177e4SLinus Torvalds## Copyright (C) 2000, 1 Tim Waugh <twaugh@redhat.com> ## 81da177e4SLinus Torvalds## Copyright (C) 2001 Simon Huggins ## 970c95b00SRandy Dunlap## Copyright (C) 2005-2012 Randy Dunlap ## 101b40c194SDan Luedtke## Copyright (C) 2012 Dan Luedtke ## 111da177e4SLinus Torvalds## ## 121da177e4SLinus Torvalds## #define enhancements by Armin Kuster <akuster@mvista.com> ## 131da177e4SLinus Torvalds## Copyright (c) 2000 MontaVista Software, Inc. ## 141da177e4SLinus Torvalds## ## 151da177e4SLinus Torvalds## This software falls under the GNU General Public License. ## 161da177e4SLinus Torvalds## Please read the COPYING file for more information ## 171da177e4SLinus Torvalds 181da177e4SLinus Torvalds# 18/01/2001 - Cleanups 191da177e4SLinus Torvalds# Functions prototyped as foo(void) same as foo() 201da177e4SLinus Torvalds# Stop eval'ing where we don't need to. 211da177e4SLinus Torvalds# -- huggie@earth.li 221da177e4SLinus Torvalds 231da177e4SLinus Torvalds# 27/06/2001 - Allowed whitespace after initial "/**" and 241da177e4SLinus Torvalds# allowed comments before function declarations. 251da177e4SLinus Torvalds# -- Christian Kreibich <ck@whoop.org> 261da177e4SLinus Torvalds 271da177e4SLinus Torvalds# Still to do: 281da177e4SLinus Torvalds# - add perldoc documentation 291da177e4SLinus Torvalds# - Look more closely at some of the scarier bits :) 301da177e4SLinus Torvalds 311da177e4SLinus Torvalds# 26/05/2001 - Support for separate source and object trees. 321da177e4SLinus Torvalds# Return error code. 331da177e4SLinus Torvalds# Keith Owens <kaos@ocs.com.au> 341da177e4SLinus Torvalds 351da177e4SLinus Torvalds# 23/09/2001 - Added support for typedefs, structs, enums and unions 361da177e4SLinus Torvalds# Support for Context section; can be terminated using empty line 371da177e4SLinus Torvalds# Small fixes (like spaces vs. \s in regex) 381da177e4SLinus Torvalds# -- Tim Jansen <tim@tjansen.de> 391da177e4SLinus Torvalds 401b40c194SDan Luedtke# 25/07/2012 - Added support for HTML5 411b40c194SDan Luedtke# -- Dan Luedtke <mail@danrl.de> 421da177e4SLinus Torvalds 43fadc0b31SJani Nikulasub usage { 44fadc0b31SJani Nikula my $message = <<"EOF"; 45fadc0b31SJani NikulaUsage: $0 [OPTION ...] FILE ... 461da177e4SLinus Torvalds 47fadc0b31SJani NikulaRead C language source or header FILEs, extract embedded documentation comments, 48fadc0b31SJani Nikulaand print formatted documentation to standard output. 491da177e4SLinus Torvalds 50fadc0b31SJani NikulaThe documentation comments are identified by "/**" opening comment mark. See 51857af3b7SMauro Carvalho ChehabDocumentation/doc-guide/kernel-doc.rst for the documentation comment syntax. 52fadc0b31SJani Nikula 53fadc0b31SJani NikulaOutput format selection (mutually exclusive): 54fadc0b31SJani Nikula -man Output troff manual page format. This is the default. 55c0d1b6eeSJonathan Corbet -rst Output reStructuredText format. 563a025e1dSMatthew Wilcox -none Do not output documentation, only warnings. 57fadc0b31SJani Nikula 58fadc0b31SJani NikulaOutput selection (mutually exclusive): 5986ae2e38SJani Nikula -export Only output documentation for symbols that have been 6086ae2e38SJani Nikula exported using EXPORT_SYMBOL() or EXPORT_SYMBOL_GPL() 61c9b2cfb3SJani Nikula in any input FILE or -export-file FILE. 6286ae2e38SJani Nikula -internal Only output documentation for symbols that have NOT been 6386ae2e38SJani Nikula exported using EXPORT_SYMBOL() or EXPORT_SYMBOL_GPL() 64c9b2cfb3SJani Nikula in any input FILE or -export-file FILE. 65fadc0b31SJani Nikula -function NAME Only output documentation for the given function(s) 66fadc0b31SJani Nikula or DOC: section title(s). All other functions and DOC: 67fadc0b31SJani Nikula sections are ignored. May be specified multiple times. 68fadc0b31SJani Nikula -nofunction NAME Do NOT output documentation for the given function(s); 69fadc0b31SJani Nikula only output documentation for the other functions and 70fadc0b31SJani Nikula DOC: sections. May be specified multiple times. 71fadc0b31SJani Nikula 72fadc0b31SJani NikulaOutput selection modifiers: 73fadc0b31SJani Nikula -no-doc-sections Do not output DOC: sections. 740b0f5f29SDaniel Vetter -enable-lineno Enable output of #define LINENO lines. Only works with 750b0f5f29SDaniel Vetter reStructuredText format. 7688c2b57dSJani Nikula -export-file FILE Specify an additional FILE in which to look for 7788c2b57dSJani Nikula EXPORT_SYMBOL() and EXPORT_SYMBOL_GPL(). To be used with 7888c2b57dSJani Nikula -export or -internal. May be specified multiple times. 79fadc0b31SJani Nikula 80fadc0b31SJani NikulaOther parameters: 81fadc0b31SJani Nikula -v Verbose output, more warnings and other information. 82fadc0b31SJani Nikula -h Print this help. 83fadc0b31SJani Nikula 84fadc0b31SJani NikulaEOF 85fadc0b31SJani Nikula print $message; 86fadc0b31SJani Nikula exit 1; 87fadc0b31SJani Nikula} 881da177e4SLinus Torvalds 891da177e4SLinus Torvalds# 901da177e4SLinus Torvalds# format of comments. 911da177e4SLinus Torvalds# In the following table, (...)? signifies optional structure. 921da177e4SLinus Torvalds# (...)* signifies 0 or more structure elements 931da177e4SLinus Torvalds# /** 941da177e4SLinus Torvalds# * function_name(:)? (- short description)? 951da177e4SLinus Torvalds# (* @parameterx: (description of parameter x)?)* 961da177e4SLinus Torvalds# (* a blank line)? 971da177e4SLinus Torvalds# * (Description:)? (Description of function)? 981da177e4SLinus Torvalds# * (section header: (section description)? )* 991da177e4SLinus Torvalds# (*)?*/ 1001da177e4SLinus Torvalds# 1011da177e4SLinus Torvalds# So .. the trivial example would be: 1021da177e4SLinus Torvalds# 1031da177e4SLinus Torvalds# /** 1041da177e4SLinus Torvalds# * my_function 105b9d97328SRandy Dunlap# */ 1061da177e4SLinus Torvalds# 107891dcd2fSRandy Dunlap# If the Description: header tag is omitted, then there must be a blank line 1081da177e4SLinus Torvalds# after the last parameter specification. 1091da177e4SLinus Torvalds# e.g. 1101da177e4SLinus Torvalds# /** 1111da177e4SLinus Torvalds# * my_function - does my stuff 1121da177e4SLinus Torvalds# * @my_arg: its mine damnit 1131da177e4SLinus Torvalds# * 1141da177e4SLinus Torvalds# * Does my stuff explained. 1151da177e4SLinus Torvalds# */ 1161da177e4SLinus Torvalds# 1171da177e4SLinus Torvalds# or, could also use: 1181da177e4SLinus Torvalds# /** 1191da177e4SLinus Torvalds# * my_function - does my stuff 1201da177e4SLinus Torvalds# * @my_arg: its mine damnit 1211da177e4SLinus Torvalds# * Description: Does my stuff explained. 1221da177e4SLinus Torvalds# */ 1231da177e4SLinus Torvalds# etc. 1241da177e4SLinus Torvalds# 125b9d97328SRandy Dunlap# Besides functions you can also write documentation for structs, unions, 1261da177e4SLinus Torvalds# enums and typedefs. Instead of the function name you must write the name 1271da177e4SLinus Torvalds# of the declaration; the struct/union/enum/typedef must always precede 1281da177e4SLinus Torvalds# the name. Nesting of declarations is not supported. 1291da177e4SLinus Torvalds# Use the argument mechanism to document members or constants. 1301da177e4SLinus Torvalds# e.g. 1311da177e4SLinus Torvalds# /** 1321da177e4SLinus Torvalds# * struct my_struct - short description 1331da177e4SLinus Torvalds# * @a: first member 1341da177e4SLinus Torvalds# * @b: second member 1351da177e4SLinus Torvalds# * 1361da177e4SLinus Torvalds# * Longer description 1371da177e4SLinus Torvalds# */ 1381da177e4SLinus Torvalds# struct my_struct { 1391da177e4SLinus Torvalds# int a; 1401da177e4SLinus Torvalds# int b; 141aeec46b9SMartin Waitz# /* private: */ 142aeec46b9SMartin Waitz# int c; 1431da177e4SLinus Torvalds# }; 1441da177e4SLinus Torvalds# 1451da177e4SLinus Torvalds# All descriptions can be multiline, except the short function description. 1461da177e4SLinus Torvalds# 147a4c6ebedSDanilo Cesar Lemes de Paula# For really longs structs, you can also describe arguments inside the 148a4c6ebedSDanilo Cesar Lemes de Paula# body of the struct. 149a4c6ebedSDanilo Cesar Lemes de Paula# eg. 150a4c6ebedSDanilo Cesar Lemes de Paula# /** 151a4c6ebedSDanilo Cesar Lemes de Paula# * struct my_struct - short description 152a4c6ebedSDanilo Cesar Lemes de Paula# * @a: first member 153a4c6ebedSDanilo Cesar Lemes de Paula# * @b: second member 154a4c6ebedSDanilo Cesar Lemes de Paula# * 155a4c6ebedSDanilo Cesar Lemes de Paula# * Longer description 156a4c6ebedSDanilo Cesar Lemes de Paula# */ 157a4c6ebedSDanilo Cesar Lemes de Paula# struct my_struct { 158a4c6ebedSDanilo Cesar Lemes de Paula# int a; 159a4c6ebedSDanilo Cesar Lemes de Paula# int b; 160a4c6ebedSDanilo Cesar Lemes de Paula# /** 161a4c6ebedSDanilo Cesar Lemes de Paula# * @c: This is longer description of C 162a4c6ebedSDanilo Cesar Lemes de Paula# * 163a4c6ebedSDanilo Cesar Lemes de Paula# * You can use paragraphs to describe arguments 164a4c6ebedSDanilo Cesar Lemes de Paula# * using this method. 165a4c6ebedSDanilo Cesar Lemes de Paula# */ 166a4c6ebedSDanilo Cesar Lemes de Paula# int c; 167a4c6ebedSDanilo Cesar Lemes de Paula# }; 168a4c6ebedSDanilo Cesar Lemes de Paula# 169a4c6ebedSDanilo Cesar Lemes de Paula# This should be use only for struct/enum members. 170a4c6ebedSDanilo Cesar Lemes de Paula# 1711da177e4SLinus Torvalds# You can also add additional sections. When documenting kernel functions you 1721da177e4SLinus Torvalds# should document the "Context:" of the function, e.g. whether the functions 1731da177e4SLinus Torvalds# can be called form interrupts. Unlike other sections you can end it with an 1741da177e4SLinus Torvalds# empty line. 1754092bac7SYacine Belkadi# A non-void function should have a "Return:" section describing the return 1764092bac7SYacine Belkadi# value(s). 1771da177e4SLinus Torvalds# Example-sections should contain the string EXAMPLE so that they are marked 1781da177e4SLinus Torvalds# appropriately in DocBook. 1791da177e4SLinus Torvalds# 1801da177e4SLinus Torvalds# Example: 1811da177e4SLinus Torvalds# /** 1821da177e4SLinus Torvalds# * user_function - function that can only be called in user context 1831da177e4SLinus Torvalds# * @a: some argument 1841da177e4SLinus Torvalds# * Context: !in_interrupt() 1851da177e4SLinus Torvalds# * 1861da177e4SLinus Torvalds# * Some description 1871da177e4SLinus Torvalds# * Example: 1881da177e4SLinus Torvalds# * user_function(22); 1891da177e4SLinus Torvalds# */ 1901da177e4SLinus Torvalds# ... 1911da177e4SLinus Torvalds# 1921da177e4SLinus Torvalds# 1931da177e4SLinus Torvalds# All descriptive text is further processed, scanning for the following special 1941da177e4SLinus Torvalds# patterns, which are highlighted appropriately. 1951da177e4SLinus Torvalds# 1961da177e4SLinus Torvalds# 'funcname()' - function 1971da177e4SLinus Torvalds# '$ENVVAR' - environmental variable 1981da177e4SLinus Torvalds# '&struct_name' - name of a structure (up to two words including 'struct') 1995267dd35SPaolo Bonzini# '&struct_name.member' - name of a structure member 2001da177e4SLinus Torvalds# '@parameter' - name of a parameter 2011da177e4SLinus Torvalds# '%CONST' - name of a constant. 202b97f193aSMauro Carvalho Chehab# '``LITERAL``' - literal string without any spaces on it. 2031da177e4SLinus Torvalds 2048484baaaSRandy Dunlap## init lots of data 2058484baaaSRandy Dunlap 2061da177e4SLinus Torvaldsmy $errors = 0; 2071da177e4SLinus Torvaldsmy $warnings = 0; 2085f8c7c98SRandy Dunlapmy $anon_struct_union = 0; 2091da177e4SLinus Torvalds 2101da177e4SLinus Torvalds# match expressions used to find embedded type information 211b97f193aSMauro Carvalho Chehabmy $type_constant = '\b``([^\`]+)``\b'; 212b97f193aSMauro Carvalho Chehabmy $type_constant2 = '\%([-_\w]+)'; 2131da177e4SLinus Torvaldsmy $type_func = '(\w+)\(\)'; 2148ad72163SMauro Carvalho Chehabmy $type_param = '\@(\w*(\.\w+)*(\.\.\.)?)'; 2155219f18aSJonathan Corbetmy $type_fp_param = '\@(\w+)\(\)'; # Special RST handling for func ptr params 2161da177e4SLinus Torvaldsmy $type_env = '(\$\w+)'; 217df31175bSPaolo Bonzinimy $type_enum = '\&(enum\s*([_\w]+))'; 218df31175bSPaolo Bonzinimy $type_struct = '\&(struct\s*([_\w]+))'; 219df31175bSPaolo Bonzinimy $type_typedef = '\&(typedef\s*([_\w]+))'; 220df31175bSPaolo Bonzinimy $type_union = '\&(union\s*([_\w]+))'; 2215267dd35SPaolo Bonzinimy $type_member = '\&([_\w]+)(\.|->)([_\w]+)'; 222df31175bSPaolo Bonzinimy $type_fallback = '\&([_\w]+)'; 223f3341dcfSJani Nikulamy $type_member_func = $type_member . '\(\)'; 2241da177e4SLinus Torvalds 2251da177e4SLinus Torvalds# Output conversion substitutions. 2261da177e4SLinus Torvalds# One for each output format 2271da177e4SLinus Torvalds 2281da177e4SLinus Torvalds# these are pretty rough 2294d732701SDanilo Cesar Lemes de Paulamy @highlights_man = ( 2304d732701SDanilo Cesar Lemes de Paula [$type_constant, "\$1"], 231b97f193aSMauro Carvalho Chehab [$type_constant2, "\$1"], 2324d732701SDanilo Cesar Lemes de Paula [$type_func, "\\\\fB\$1\\\\fP"], 233df31175bSPaolo Bonzini [$type_enum, "\\\\fI\$1\\\\fP"], 2344d732701SDanilo Cesar Lemes de Paula [$type_struct, "\\\\fI\$1\\\\fP"], 235df31175bSPaolo Bonzini [$type_typedef, "\\\\fI\$1\\\\fP"], 236df31175bSPaolo Bonzini [$type_union, "\\\\fI\$1\\\\fP"], 2375267dd35SPaolo Bonzini [$type_param, "\\\\fI\$1\\\\fP"], 238df31175bSPaolo Bonzini [$type_member, "\\\\fI\$1\$2\$3\\\\fP"], 239df31175bSPaolo Bonzini [$type_fallback, "\\\\fI\$1\\\\fP"] 2404d732701SDanilo Cesar Lemes de Paula ); 2411da177e4SLinus Torvaldsmy $blankline_man = ""; 2421da177e4SLinus Torvalds 243c0d1b6eeSJonathan Corbet# rst-mode 244c0d1b6eeSJonathan Corbetmy @highlights_rst = ( 245c0d1b6eeSJonathan Corbet [$type_constant, "``\$1``"], 246b97f193aSMauro Carvalho Chehab [$type_constant2, "``\$1``"], 247f3341dcfSJani Nikula # Note: need to escape () to avoid func matching later 2485267dd35SPaolo Bonzini [$type_member_func, "\\:c\\:type\\:`\$1\$2\$3\\\\(\\\\) <\$1>`"], 2495267dd35SPaolo Bonzini [$type_member, "\\:c\\:type\\:`\$1\$2\$3 <\$1>`"], 2505219f18aSJonathan Corbet [$type_fp_param, "**\$1\\\\(\\\\)**"], 251a19bce64SJani Nikula [$type_func, "\\:c\\:func\\:`\$1()`"], 252df31175bSPaolo Bonzini [$type_enum, "\\:c\\:type\\:`\$1 <\$2>`"], 253df31175bSPaolo Bonzini [$type_struct, "\\:c\\:type\\:`\$1 <\$2>`"], 254df31175bSPaolo Bonzini [$type_typedef, "\\:c\\:type\\:`\$1 <\$2>`"], 255df31175bSPaolo Bonzini [$type_union, "\\:c\\:type\\:`\$1 <\$2>`"], 256a7291e7eSJani Nikula # in rst this can refer to any type 257df31175bSPaolo Bonzini [$type_fallback, "\\:c\\:type\\:`\$1`"], 258c0d1b6eeSJonathan Corbet [$type_param, "**\$1**"] 259c0d1b6eeSJonathan Corbet ); 260c0d1b6eeSJonathan Corbetmy $blankline_rst = "\n"; 261c0d1b6eeSJonathan Corbet 2621da177e4SLinus Torvalds# read arguments 2631da177e4SLinus Torvaldsif ($#ARGV == -1) { 2641da177e4SLinus Torvalds usage(); 2651da177e4SLinus Torvalds} 2661da177e4SLinus Torvalds 2678484baaaSRandy Dunlapmy $kernelversion; 2688484baaaSRandy Dunlapmy $dohighlight = ""; 2698484baaaSRandy Dunlap 2701da177e4SLinus Torvaldsmy $verbose = 0; 271bdfe2be3SMauro Carvalho Chehabmy $output_mode = "rst"; 272e314ba31SDaniel Santosmy $output_preformatted = 0; 2734b44595aSJohannes Bergmy $no_doc_sections = 0; 2740b0f5f29SDaniel Vettermy $enable_lineno = 0; 275bdfe2be3SMauro Carvalho Chehabmy @highlights = @highlights_rst; 276bdfe2be3SMauro Carvalho Chehabmy $blankline = $blankline_rst; 2771da177e4SLinus Torvaldsmy $modulename = "Kernel API"; 278b6c3f456SJani Nikula 279b6c3f456SJani Nikulause constant { 280b6c3f456SJani Nikula OUTPUT_ALL => 0, # output all symbols and doc sections 281b6c3f456SJani Nikula OUTPUT_INCLUDE => 1, # output only specified symbols 282b6c3f456SJani Nikula OUTPUT_EXCLUDE => 2, # output everything except specified symbols 283b6c3f456SJani Nikula OUTPUT_EXPORTED => 3, # output exported symbols 284b6c3f456SJani Nikula OUTPUT_INTERNAL => 4, # output non-exported symbols 285b6c3f456SJani Nikula}; 286b6c3f456SJani Nikulamy $output_selection = OUTPUT_ALL; 287b2c4105bSBen Hutchingsmy $show_not_found = 0; 288b2c4105bSBen Hutchings 28988c2b57dSJani Nikulamy @export_file_list; 29088c2b57dSJani Nikula 291b2c4105bSBen Hutchingsmy @build_time; 292b2c4105bSBen Hutchingsif (defined($ENV{'KBUILD_BUILD_TIMESTAMP'}) && 293b2c4105bSBen Hutchings (my $seconds = `date -d"${ENV{'KBUILD_BUILD_TIMESTAMP'}}" +%s`) ne '') { 294b2c4105bSBen Hutchings @build_time = gmtime($seconds); 295b2c4105bSBen Hutchings} else { 296b2c4105bSBen Hutchings @build_time = localtime; 297b2c4105bSBen Hutchings} 298b2c4105bSBen Hutchings 2991da177e4SLinus Torvaldsmy $man_date = ('January', 'February', 'March', 'April', 'May', 'June', 3001da177e4SLinus Torvalds 'July', 'August', 'September', 'October', 301b2c4105bSBen Hutchings 'November', 'December')[$build_time[4]] . 302b2c4105bSBen Hutchings " " . ($build_time[5]+1900); 3031da177e4SLinus Torvalds 3048484baaaSRandy Dunlap# Essentially these are globals. 305b9d97328SRandy Dunlap# They probably want to be tidied up, made more localised or something. 306b9d97328SRandy Dunlap# CAVEAT EMPTOR! Some of the others I localised may not want to be, which 3071da177e4SLinus Torvalds# could cause "use of undefined value" or other bugs. 3081da177e4SLinus Torvaldsmy ($function, %function_table, %parametertypes, $declaration_purpose); 3090b0f5f29SDaniel Vettermy $declaration_start_line; 3101da177e4SLinus Torvaldsmy ($type, $declaration_name, $return_type); 3111c32fd0cSIlya Dryomovmy ($newsection, $newcontents, $prototype, $brcount, %source_map); 3121da177e4SLinus Torvalds 313bd0e88e5SRandy Dunlapif (defined($ENV{'KBUILD_VERBOSE'})) { 314bd0e88e5SRandy Dunlap $verbose = "$ENV{'KBUILD_VERBOSE'}"; 315bd0e88e5SRandy Dunlap} 316bd0e88e5SRandy Dunlap 3171da177e4SLinus Torvalds# Generated docbook code is inserted in a template at a point where 3181da177e4SLinus Torvalds# docbook v3.1 requires a non-zero sequence of RefEntry's; see: 3191da177e4SLinus Torvalds# http://www.oasis-open.org/docbook/documentation/reference/html/refentry.html 3201da177e4SLinus Torvalds# We keep track of number of generated entries and generate a dummy 3211da177e4SLinus Torvalds# if needs be to ensure the expanded template can be postprocessed 3221da177e4SLinus Torvalds# into html. 3231da177e4SLinus Torvaldsmy $section_counter = 0; 3241da177e4SLinus Torvalds 3251da177e4SLinus Torvaldsmy $lineprefix=""; 3261da177e4SLinus Torvalds 32748af606aSJani Nikula# Parser states 32848af606aSJani Nikulause constant { 32948af606aSJani Nikula STATE_NORMAL => 0, # normal code 33048af606aSJani Nikula STATE_NAME => 1, # looking for function name 33148af606aSJani Nikula STATE_FIELD => 2, # scanning field start 33248af606aSJani Nikula STATE_PROTO => 3, # scanning prototype 33348af606aSJani Nikula STATE_DOCBLOCK => 4, # documentation block 33448af606aSJani Nikula STATE_INLINE => 5, # gathering documentation outside main block 33548af606aSJani Nikula}; 3361da177e4SLinus Torvaldsmy $state; 337850622dfSRandy Dunlapmy $in_doc_sect; 3381da177e4SLinus Torvalds 33948af606aSJani Nikula# Inline documentation state 34048af606aSJani Nikulause constant { 34148af606aSJani Nikula STATE_INLINE_NA => 0, # not applicable ($state != STATE_INLINE) 34248af606aSJani Nikula STATE_INLINE_NAME => 1, # looking for member name (@foo:) 34348af606aSJani Nikula STATE_INLINE_TEXT => 2, # looking for member documentation 34448af606aSJani Nikula STATE_INLINE_END => 3, # done 34548af606aSJani Nikula STATE_INLINE_ERROR => 4, # error - Comment without header was found. 34648af606aSJani Nikula # Spit a warning as it's not 347a4c6ebedSDanilo Cesar Lemes de Paula # proper kernel-doc and ignore the rest. 34848af606aSJani Nikula}; 34948af606aSJani Nikulamy $inline_doc_state; 350a4c6ebedSDanilo Cesar Lemes de Paula 3511da177e4SLinus Torvalds#declaration types: can be 3521da177e4SLinus Torvalds# 'function', 'struct', 'union', 'enum', 'typedef' 3531da177e4SLinus Torvaldsmy $decl_type; 3541da177e4SLinus Torvalds 3551da177e4SLinus Torvaldsmy $doc_start = '^/\*\*\s*$'; # Allow whitespace at end of comment start. 3561da177e4SLinus Torvaldsmy $doc_end = '\*/'; 3571da177e4SLinus Torvaldsmy $doc_com = '\s*\*\s*'; 35812ae6779SDaniel Santosmy $doc_com_body = '\s*\* ?'; 3591da177e4SLinus Torvaldsmy $doc_decl = $doc_com . '(\w+)'; 360f624adefSJani Nikula# @params and a strictly limited set of supported section names 3618569de68SJonathan Corbetmy $doc_sect = $doc_com . 362ef00028bSJonathan Corbet '\s*(\@[.\w]+|\@\.\.\.|description|context|returns?|notes?|examples?)\s*:(.*)'; 36312ae6779SDaniel Santosmy $doc_content = $doc_com_body . '(.*)'; 3641da177e4SLinus Torvaldsmy $doc_block = $doc_com . 'DOC:\s*(.*)?'; 36548af606aSJani Nikulamy $doc_inline_start = '^\s*/\*\*\s*$'; 36648af606aSJani Nikulamy $doc_inline_sect = '\s*\*\s*(@[\w\s]+):(.*)'; 36748af606aSJani Nikulamy $doc_inline_end = '^\s*\*/\s*$'; 3680c9aa209SJani Nikulamy $doc_inline_oneline = '^\s*/\*\*\s*(@[\w\s]+):\s*(.*)\s*\*/\s*$'; 36986ae2e38SJani Nikulamy $export_symbol = '^\s*EXPORT_SYMBOL(_GPL)?\s*\(\s*(\w+)\s*\)\s*;'; 3701da177e4SLinus Torvalds 3711da177e4SLinus Torvaldsmy %parameterdescs; 3720b0f5f29SDaniel Vettermy %parameterdesc_start_lines; 3731da177e4SLinus Torvaldsmy @parameterlist; 3741da177e4SLinus Torvaldsmy %sections; 3751da177e4SLinus Torvaldsmy @sectionlist; 3760b0f5f29SDaniel Vettermy %section_start_lines; 377a1d94aa5SRandy Dunlapmy $sectcheck; 378a1d94aa5SRandy Dunlapmy $struct_actual; 3791da177e4SLinus Torvalds 3801da177e4SLinus Torvaldsmy $contents = ""; 3810b0f5f29SDaniel Vettermy $new_start_line = 0; 382f624adefSJani Nikula 383f624adefSJani Nikula# the canonical section names. see also $doc_sect above. 3841da177e4SLinus Torvaldsmy $section_default = "Description"; # default section 3851da177e4SLinus Torvaldsmy $section_intro = "Introduction"; 3861da177e4SLinus Torvaldsmy $section = $section_default; 3871da177e4SLinus Torvaldsmy $section_context = "Context"; 3884092bac7SYacine Belkadimy $section_return = "Return"; 3891da177e4SLinus Torvalds 3901da177e4SLinus Torvaldsmy $undescribed = "-- undescribed --"; 3911da177e4SLinus Torvalds 3921da177e4SLinus Torvaldsreset_state(); 3931da177e4SLinus Torvalds 394b031ac4eSMauro Carvalho Chehabwhile ($ARGV[0] =~ m/^--?(.*)/) { 395b031ac4eSMauro Carvalho Chehab my $cmd = $1; 396b031ac4eSMauro Carvalho Chehab shift @ARGV; 397b031ac4eSMauro Carvalho Chehab if ($cmd eq "man") { 3981da177e4SLinus Torvalds $output_mode = "man"; 3994d732701SDanilo Cesar Lemes de Paula @highlights = @highlights_man; 4001da177e4SLinus Torvalds $blankline = $blankline_man; 401b031ac4eSMauro Carvalho Chehab } elsif ($cmd eq "rst") { 402c0d1b6eeSJonathan Corbet $output_mode = "rst"; 403c0d1b6eeSJonathan Corbet @highlights = @highlights_rst; 404c0d1b6eeSJonathan Corbet $blankline = $blankline_rst; 405b031ac4eSMauro Carvalho Chehab } elsif ($cmd eq "none") { 4063a025e1dSMatthew Wilcox $output_mode = "none"; 407b031ac4eSMauro Carvalho Chehab } elsif ($cmd eq "module") { # not needed for XML, inherits from calling document 4081da177e4SLinus Torvalds $modulename = shift @ARGV; 409b031ac4eSMauro Carvalho Chehab } elsif ($cmd eq "function") { # to only output specific functions 410b6c3f456SJani Nikula $output_selection = OUTPUT_INCLUDE; 4111da177e4SLinus Torvalds $function = shift @ARGV; 4121da177e4SLinus Torvalds $function_table{$function} = 1; 413b031ac4eSMauro Carvalho Chehab } elsif ($cmd eq "nofunction") { # output all except specific functions 414b6c3f456SJani Nikula $output_selection = OUTPUT_EXCLUDE; 4151da177e4SLinus Torvalds $function = shift @ARGV; 4161da177e4SLinus Torvalds $function_table{$function} = 1; 417b031ac4eSMauro Carvalho Chehab } elsif ($cmd eq "export") { # only exported symbols 418b6c3f456SJani Nikula $output_selection = OUTPUT_EXPORTED; 419da9726ecSJani Nikula %function_table = (); 420b031ac4eSMauro Carvalho Chehab } elsif ($cmd eq "internal") { # only non-exported symbols 421b6c3f456SJani Nikula $output_selection = OUTPUT_INTERNAL; 422da9726ecSJani Nikula %function_table = (); 423b031ac4eSMauro Carvalho Chehab } elsif ($cmd eq "export-file") { 42488c2b57dSJani Nikula my $file = shift @ARGV; 42588c2b57dSJani Nikula push(@export_file_list, $file); 426b031ac4eSMauro Carvalho Chehab } elsif ($cmd eq "v") { 4271da177e4SLinus Torvalds $verbose = 1; 428b031ac4eSMauro Carvalho Chehab } elsif (($cmd eq "h") || ($cmd eq "help")) { 4291da177e4SLinus Torvalds usage(); 430b031ac4eSMauro Carvalho Chehab } elsif ($cmd eq 'no-doc-sections') { 4314b44595aSJohannes Berg $no_doc_sections = 1; 432b031ac4eSMauro Carvalho Chehab } elsif ($cmd eq 'enable-lineno') { 4330b0f5f29SDaniel Vetter $enable_lineno = 1; 434b031ac4eSMauro Carvalho Chehab } elsif ($cmd eq 'show-not-found') { 435e946c43aSJohannes Berg $show_not_found = 1; 436b031ac4eSMauro Carvalho Chehab } else { 437b031ac4eSMauro Carvalho Chehab # Unknown argument 438b031ac4eSMauro Carvalho Chehab usage(); 4391da177e4SLinus Torvalds } 4401da177e4SLinus Torvalds} 4411da177e4SLinus Torvalds 4428484baaaSRandy Dunlap# continue execution near EOF; 4438484baaaSRandy Dunlap 44453f049faSBorislav Petkov# get kernel version from env 44553f049faSBorislav Petkovsub get_kernel_version() { 4461b9bc22dSJohannes Berg my $version = 'unknown kernel version'; 44753f049faSBorislav Petkov 44853f049faSBorislav Petkov if (defined($ENV{'KERNELVERSION'})) { 44953f049faSBorislav Petkov $version = $ENV{'KERNELVERSION'}; 45053f049faSBorislav Petkov } 45153f049faSBorislav Petkov return $version; 45253f049faSBorislav Petkov} 4531da177e4SLinus Torvalds 4540b0f5f29SDaniel Vetter# 4550b0f5f29SDaniel Vettersub print_lineno { 4560b0f5f29SDaniel Vetter my $lineno = shift; 4570b0f5f29SDaniel Vetter if ($enable_lineno && defined($lineno)) { 4580b0f5f29SDaniel Vetter print "#define LINENO " . $lineno . "\n"; 4590b0f5f29SDaniel Vetter } 4600b0f5f29SDaniel Vetter} 4611da177e4SLinus Torvalds## 4621da177e4SLinus Torvalds# dumps section contents to arrays/hashes intended for that purpose. 4631da177e4SLinus Torvalds# 4641da177e4SLinus Torvaldssub dump_section { 46594dc7ad5SRandy Dunlap my $file = shift; 4661da177e4SLinus Torvalds my $name = shift; 4671da177e4SLinus Torvalds my $contents = join "\n", @_; 4681da177e4SLinus Torvalds 46913901ef2SJani Nikula if ($name =~ m/$type_param/) { 4701da177e4SLinus Torvalds $name = $1; 4711da177e4SLinus Torvalds $parameterdescs{$name} = $contents; 472a1d94aa5SRandy Dunlap $sectcheck = $sectcheck . $name . " "; 4730b0f5f29SDaniel Vetter $parameterdesc_start_lines{$name} = $new_start_line; 4740b0f5f29SDaniel Vetter $new_start_line = 0; 475ced69090SRandy Dunlap } elsif ($name eq "@\.\.\.") { 476ced69090SRandy Dunlap $name = "..."; 477ced69090SRandy Dunlap $parameterdescs{$name} = $contents; 478a1d94aa5SRandy Dunlap $sectcheck = $sectcheck . $name . " "; 4790b0f5f29SDaniel Vetter $parameterdesc_start_lines{$name} = $new_start_line; 4800b0f5f29SDaniel Vetter $new_start_line = 0; 4811da177e4SLinus Torvalds } else { 48294dc7ad5SRandy Dunlap if (defined($sections{$name}) && ($sections{$name} ne "")) { 48395b6be9dSJani Nikula # Only warn on user specified duplicate section names. 48495b6be9dSJani Nikula if ($name ne $section_default) { 48532217761SJani Nikula print STDERR "${file}:$.: warning: duplicate section name '$name'\n"; 48632217761SJani Nikula ++$warnings; 48795b6be9dSJani Nikula } 48832217761SJani Nikula $sections{$name} .= $contents; 48932217761SJani Nikula } else { 4901da177e4SLinus Torvalds $sections{$name} = $contents; 4911da177e4SLinus Torvalds push @sectionlist, $name; 4920b0f5f29SDaniel Vetter $section_start_lines{$name} = $new_start_line; 4930b0f5f29SDaniel Vetter $new_start_line = 0; 4941da177e4SLinus Torvalds } 4951da177e4SLinus Torvalds } 49632217761SJani Nikula} 4971da177e4SLinus Torvalds 4981da177e4SLinus Torvalds## 499b112e0f7SJohannes Berg# dump DOC: section after checking that it should go out 500b112e0f7SJohannes Berg# 501b112e0f7SJohannes Bergsub dump_doc_section { 50294dc7ad5SRandy Dunlap my $file = shift; 503b112e0f7SJohannes Berg my $name = shift; 504b112e0f7SJohannes Berg my $contents = join "\n", @_; 505b112e0f7SJohannes Berg 5064b44595aSJohannes Berg if ($no_doc_sections) { 5074b44595aSJohannes Berg return; 5084b44595aSJohannes Berg } 5094b44595aSJohannes Berg 510b6c3f456SJani Nikula if (($output_selection == OUTPUT_ALL) || 511b6c3f456SJani Nikula ($output_selection == OUTPUT_INCLUDE && 512b6c3f456SJani Nikula defined($function_table{$name})) || 513b6c3f456SJani Nikula ($output_selection == OUTPUT_EXCLUDE && 514b6c3f456SJani Nikula !defined($function_table{$name}))) 515b112e0f7SJohannes Berg { 51694dc7ad5SRandy Dunlap dump_section($file, $name, $contents); 517b112e0f7SJohannes Berg output_blockhead({'sectionlist' => \@sectionlist, 518b112e0f7SJohannes Berg 'sections' => \%sections, 519b112e0f7SJohannes Berg 'module' => $modulename, 520b6c3f456SJani Nikula 'content-only' => ($output_selection != OUTPUT_ALL), }); 521b112e0f7SJohannes Berg } 522b112e0f7SJohannes Berg} 523b112e0f7SJohannes Berg 524b112e0f7SJohannes Berg## 5251da177e4SLinus Torvalds# output function 5261da177e4SLinus Torvalds# 5271da177e4SLinus Torvalds# parameterdescs, a hash. 5281da177e4SLinus Torvalds# function => "function name" 5291da177e4SLinus Torvalds# parameterlist => @list of parameters 5301da177e4SLinus Torvalds# parameterdescs => %parameter descriptions 5311da177e4SLinus Torvalds# sectionlist => @list of sections 532a21217daSRandy Dunlap# sections => %section descriptions 5331da177e4SLinus Torvalds# 5341da177e4SLinus Torvalds 5351da177e4SLinus Torvaldssub output_highlight { 5361da177e4SLinus Torvalds my $contents = join "\n",@_; 5371da177e4SLinus Torvalds my $line; 5381da177e4SLinus Torvalds 5391da177e4SLinus Torvalds# DEBUG 5401da177e4SLinus Torvalds# if (!defined $contents) { 5411da177e4SLinus Torvalds# use Carp; 5421da177e4SLinus Torvalds# confess "output_highlight got called with no args?\n"; 5431da177e4SLinus Torvalds# } 5441da177e4SLinus Torvalds 5453eb014a1SRandy Dunlap# print STDERR "contents b4:$contents\n"; 5461da177e4SLinus Torvalds eval $dohighlight; 5471da177e4SLinus Torvalds die $@ if $@; 5483eb014a1SRandy Dunlap# print STDERR "contents af:$contents\n"; 5493eb014a1SRandy Dunlap 5501da177e4SLinus Torvalds foreach $line (split "\n", $contents) { 55112ae6779SDaniel Santos if (! $output_preformatted) { 55212ae6779SDaniel Santos $line =~ s/^\s*//; 55312ae6779SDaniel Santos } 5541da177e4SLinus Torvalds if ($line eq ""){ 555e314ba31SDaniel Santos if (! $output_preformatted) { 5566b5b55f6SRandy Dunlap print $lineprefix, local_unescape($blankline); 557e314ba31SDaniel Santos } 5581da177e4SLinus Torvalds } else { 5591da177e4SLinus Torvalds $line =~ s/\\\\\\/\&/g; 560cdccb316SRandy Dunlap if ($output_mode eq "man" && substr($line, 0, 1) eq ".") { 561cdccb316SRandy Dunlap print "\\&$line"; 562cdccb316SRandy Dunlap } else { 5631da177e4SLinus Torvalds print $lineprefix, $line; 5641da177e4SLinus Torvalds } 565cdccb316SRandy Dunlap } 5661da177e4SLinus Torvalds print "\n"; 5671da177e4SLinus Torvalds } 5681da177e4SLinus Torvalds} 5691da177e4SLinus Torvalds 5701da177e4SLinus Torvalds## 5711da177e4SLinus Torvalds# output function in man 5721da177e4SLinus Torvaldssub output_function_man(%) { 5731da177e4SLinus Torvalds my %args = %{$_[0]}; 5741da177e4SLinus Torvalds my ($parameter, $section); 5751da177e4SLinus Torvalds my $count; 5761da177e4SLinus Torvalds 5771da177e4SLinus Torvalds print ".TH \"$args{'function'}\" 9 \"$args{'function'}\" \"$man_date\" \"Kernel Hacker's Manual\" LINUX\n"; 5781da177e4SLinus Torvalds 5791da177e4SLinus Torvalds print ".SH NAME\n"; 5801da177e4SLinus Torvalds print $args{'function'} . " \\- " . $args{'purpose'} . "\n"; 5811da177e4SLinus Torvalds 5821da177e4SLinus Torvalds print ".SH SYNOPSIS\n"; 583a21217daSRandy Dunlap if ($args{'functiontype'} ne "") { 5841da177e4SLinus Torvalds print ".B \"" . $args{'functiontype'} . "\" " . $args{'function'} . "\n"; 585a21217daSRandy Dunlap } else { 586a21217daSRandy Dunlap print ".B \"" . $args{'function'} . "\n"; 587a21217daSRandy Dunlap } 5881da177e4SLinus Torvalds $count = 0; 5891da177e4SLinus Torvalds my $parenth = "("; 5901da177e4SLinus Torvalds my $post = ","; 5911da177e4SLinus Torvalds foreach my $parameter (@{$args{'parameterlist'}}) { 5921da177e4SLinus Torvalds if ($count == $#{$args{'parameterlist'}}) { 5931da177e4SLinus Torvalds $post = ");"; 5941da177e4SLinus Torvalds } 5951da177e4SLinus Torvalds $type = $args{'parametertypes'}{$parameter}; 5961da177e4SLinus Torvalds if ($type =~ m/([^\(]*\(\*)\s*\)\s*\(([^\)]*)\)/) { 5971da177e4SLinus Torvalds # pointer-to-function 5981da177e4SLinus Torvalds print ".BI \"" . $parenth . $1 . "\" " . $parameter . " \") (" . $2 . ")" . $post . "\"\n"; 5991da177e4SLinus Torvalds } else { 6001da177e4SLinus Torvalds $type =~ s/([^\*])$/$1 /; 6011da177e4SLinus Torvalds print ".BI \"" . $parenth . $type . "\" " . $parameter . " \"" . $post . "\"\n"; 6021da177e4SLinus Torvalds } 6031da177e4SLinus Torvalds $count++; 6041da177e4SLinus Torvalds $parenth = ""; 6051da177e4SLinus Torvalds } 6061da177e4SLinus Torvalds 6071da177e4SLinus Torvalds print ".SH ARGUMENTS\n"; 6081da177e4SLinus Torvalds foreach $parameter (@{$args{'parameterlist'}}) { 6091da177e4SLinus Torvalds my $parameter_name = $parameter; 6101da177e4SLinus Torvalds $parameter_name =~ s/\[.*//; 6111da177e4SLinus Torvalds 6121da177e4SLinus Torvalds print ".IP \"" . $parameter . "\" 12\n"; 6131da177e4SLinus Torvalds output_highlight($args{'parameterdescs'}{$parameter_name}); 6141da177e4SLinus Torvalds } 6151da177e4SLinus Torvalds foreach $section (@{$args{'sectionlist'}}) { 6161da177e4SLinus Torvalds print ".SH \"", uc $section, "\"\n"; 6171da177e4SLinus Torvalds output_highlight($args{'sections'}{$section}); 6181da177e4SLinus Torvalds } 6191da177e4SLinus Torvalds} 6201da177e4SLinus Torvalds 6211da177e4SLinus Torvalds## 6221da177e4SLinus Torvalds# output enum in man 6231da177e4SLinus Torvaldssub output_enum_man(%) { 6241da177e4SLinus Torvalds my %args = %{$_[0]}; 6251da177e4SLinus Torvalds my ($parameter, $section); 6261da177e4SLinus Torvalds my $count; 6271da177e4SLinus Torvalds 6281da177e4SLinus Torvalds print ".TH \"$args{'module'}\" 9 \"enum $args{'enum'}\" \"$man_date\" \"API Manual\" LINUX\n"; 6291da177e4SLinus Torvalds 6301da177e4SLinus Torvalds print ".SH NAME\n"; 6311da177e4SLinus Torvalds print "enum " . $args{'enum'} . " \\- " . $args{'purpose'} . "\n"; 6321da177e4SLinus Torvalds 6331da177e4SLinus Torvalds print ".SH SYNOPSIS\n"; 6341da177e4SLinus Torvalds print "enum " . $args{'enum'} . " {\n"; 6351da177e4SLinus Torvalds $count = 0; 6361da177e4SLinus Torvalds foreach my $parameter (@{$args{'parameterlist'}}) { 6371da177e4SLinus Torvalds print ".br\n.BI \" $parameter\"\n"; 6381da177e4SLinus Torvalds if ($count == $#{$args{'parameterlist'}}) { 6391da177e4SLinus Torvalds print "\n};\n"; 6401da177e4SLinus Torvalds last; 6411da177e4SLinus Torvalds } 6421da177e4SLinus Torvalds else { 6431da177e4SLinus Torvalds print ", \n.br\n"; 6441da177e4SLinus Torvalds } 6451da177e4SLinus Torvalds $count++; 6461da177e4SLinus Torvalds } 6471da177e4SLinus Torvalds 6481da177e4SLinus Torvalds print ".SH Constants\n"; 6491da177e4SLinus Torvalds foreach $parameter (@{$args{'parameterlist'}}) { 6501da177e4SLinus Torvalds my $parameter_name = $parameter; 6511da177e4SLinus Torvalds $parameter_name =~ s/\[.*//; 6521da177e4SLinus Torvalds 6531da177e4SLinus Torvalds print ".IP \"" . $parameter . "\" 12\n"; 6541da177e4SLinus Torvalds output_highlight($args{'parameterdescs'}{$parameter_name}); 6551da177e4SLinus Torvalds } 6561da177e4SLinus Torvalds foreach $section (@{$args{'sectionlist'}}) { 6571da177e4SLinus Torvalds print ".SH \"$section\"\n"; 6581da177e4SLinus Torvalds output_highlight($args{'sections'}{$section}); 6591da177e4SLinus Torvalds } 6601da177e4SLinus Torvalds} 6611da177e4SLinus Torvalds 6621da177e4SLinus Torvalds## 6631da177e4SLinus Torvalds# output struct in man 6641da177e4SLinus Torvaldssub output_struct_man(%) { 6651da177e4SLinus Torvalds my %args = %{$_[0]}; 6661da177e4SLinus Torvalds my ($parameter, $section); 6671da177e4SLinus Torvalds 6681da177e4SLinus Torvalds print ".TH \"$args{'module'}\" 9 \"" . $args{'type'} . " " . $args{'struct'} . "\" \"$man_date\" \"API Manual\" LINUX\n"; 6691da177e4SLinus Torvalds 6701da177e4SLinus Torvalds print ".SH NAME\n"; 6711da177e4SLinus Torvalds print $args{'type'} . " " . $args{'struct'} . " \\- " . $args{'purpose'} . "\n"; 6721da177e4SLinus Torvalds 6738ad72163SMauro Carvalho Chehab my $declaration = $args{'definition'}; 6748ad72163SMauro Carvalho Chehab $declaration =~ s/\t/ /g; 6758ad72163SMauro Carvalho Chehab $declaration =~ s/\n/"\n.br\n.BI \"/g; 6761da177e4SLinus Torvalds print ".SH SYNOPSIS\n"; 6771da177e4SLinus Torvalds print $args{'type'} . " " . $args{'struct'} . " {\n.br\n"; 6788ad72163SMauro Carvalho Chehab print ".BI \"$declaration\n};\n.br\n\n"; 6791da177e4SLinus Torvalds 680c51d3dacSRandy Dunlap print ".SH Members\n"; 6811da177e4SLinus Torvalds foreach $parameter (@{$args{'parameterlist'}}) { 6821da177e4SLinus Torvalds ($parameter =~ /^#/) && next; 6831da177e4SLinus Torvalds 6841da177e4SLinus Torvalds my $parameter_name = $parameter; 6851da177e4SLinus Torvalds $parameter_name =~ s/\[.*//; 6861da177e4SLinus Torvalds 6871da177e4SLinus Torvalds ($args{'parameterdescs'}{$parameter_name} ne $undescribed) || next; 6881da177e4SLinus Torvalds print ".IP \"" . $parameter . "\" 12\n"; 6891da177e4SLinus Torvalds output_highlight($args{'parameterdescs'}{$parameter_name}); 6901da177e4SLinus Torvalds } 6911da177e4SLinus Torvalds foreach $section (@{$args{'sectionlist'}}) { 6921da177e4SLinus Torvalds print ".SH \"$section\"\n"; 6931da177e4SLinus Torvalds output_highlight($args{'sections'}{$section}); 6941da177e4SLinus Torvalds } 6951da177e4SLinus Torvalds} 6961da177e4SLinus Torvalds 6971da177e4SLinus Torvalds## 6981da177e4SLinus Torvalds# output typedef in man 6991da177e4SLinus Torvaldssub output_typedef_man(%) { 7001da177e4SLinus Torvalds my %args = %{$_[0]}; 7011da177e4SLinus Torvalds my ($parameter, $section); 7021da177e4SLinus Torvalds 7031da177e4SLinus Torvalds print ".TH \"$args{'module'}\" 9 \"$args{'typedef'}\" \"$man_date\" \"API Manual\" LINUX\n"; 7041da177e4SLinus Torvalds 7051da177e4SLinus Torvalds print ".SH NAME\n"; 7061da177e4SLinus Torvalds print "typedef " . $args{'typedef'} . " \\- " . $args{'purpose'} . "\n"; 7071da177e4SLinus Torvalds 7081da177e4SLinus Torvalds foreach $section (@{$args{'sectionlist'}}) { 7091da177e4SLinus Torvalds print ".SH \"$section\"\n"; 7101da177e4SLinus Torvalds output_highlight($args{'sections'}{$section}); 7111da177e4SLinus Torvalds } 7121da177e4SLinus Torvalds} 7131da177e4SLinus Torvalds 714b112e0f7SJohannes Bergsub output_blockhead_man(%) { 7151da177e4SLinus Torvalds my %args = %{$_[0]}; 7161da177e4SLinus Torvalds my ($parameter, $section); 7171da177e4SLinus Torvalds my $count; 7181da177e4SLinus Torvalds 7191da177e4SLinus Torvalds print ".TH \"$args{'module'}\" 9 \"$args{'module'}\" \"$man_date\" \"API Manual\" LINUX\n"; 7201da177e4SLinus Torvalds 7211da177e4SLinus Torvalds foreach $section (@{$args{'sectionlist'}}) { 7221da177e4SLinus Torvalds print ".SH \"$section\"\n"; 7231da177e4SLinus Torvalds output_highlight($args{'sections'}{$section}); 7241da177e4SLinus Torvalds } 7251da177e4SLinus Torvalds} 7261da177e4SLinus Torvalds 7271da177e4SLinus Torvalds## 728c0d1b6eeSJonathan Corbet# output in restructured text 729c0d1b6eeSJonathan Corbet# 730c0d1b6eeSJonathan Corbet 731c0d1b6eeSJonathan Corbet# 732c0d1b6eeSJonathan Corbet# This could use some work; it's used to output the DOC: sections, and 733c0d1b6eeSJonathan Corbet# starts by putting out the name of the doc section itself, but that tends 734c0d1b6eeSJonathan Corbet# to duplicate a header already in the template file. 735c0d1b6eeSJonathan Corbet# 736c0d1b6eeSJonathan Corbetsub output_blockhead_rst(%) { 737c0d1b6eeSJonathan Corbet my %args = %{$_[0]}; 738c0d1b6eeSJonathan Corbet my ($parameter, $section); 739c0d1b6eeSJonathan Corbet 740c0d1b6eeSJonathan Corbet foreach $section (@{$args{'sectionlist'}}) { 7419e72184bSJani Nikula if ($output_selection != OUTPUT_INCLUDE) { 742c0d1b6eeSJonathan Corbet print "**$section**\n\n"; 7439e72184bSJani Nikula } 7440b0f5f29SDaniel Vetter print_lineno($section_start_lines{$section}); 745c0d1b6eeSJonathan Corbet output_highlight_rst($args{'sections'}{$section}); 746c0d1b6eeSJonathan Corbet print "\n"; 747c0d1b6eeSJonathan Corbet } 748c0d1b6eeSJonathan Corbet} 749c0d1b6eeSJonathan Corbet 750c0d1b6eeSJonathan Corbetsub output_highlight_rst { 751c0d1b6eeSJonathan Corbet my $contents = join "\n",@_; 752c0d1b6eeSJonathan Corbet my $line; 753c0d1b6eeSJonathan Corbet 754c0d1b6eeSJonathan Corbet # undo the evil effects of xml_escape() earlier 755c0d1b6eeSJonathan Corbet $contents = xml_unescape($contents); 756c0d1b6eeSJonathan Corbet 757c0d1b6eeSJonathan Corbet eval $dohighlight; 758c0d1b6eeSJonathan Corbet die $@ if $@; 759c0d1b6eeSJonathan Corbet 760c0d1b6eeSJonathan Corbet foreach $line (split "\n", $contents) { 761830066a7SJani Nikula print $lineprefix . $line . "\n"; 762c0d1b6eeSJonathan Corbet } 763c0d1b6eeSJonathan Corbet} 764c0d1b6eeSJonathan Corbet 765c0d1b6eeSJonathan Corbetsub output_function_rst(%) { 766c0d1b6eeSJonathan Corbet my %args = %{$_[0]}; 767c0d1b6eeSJonathan Corbet my ($parameter, $section); 768c099ff69SJani Nikula my $oldprefix = $lineprefix; 76982801d06SMauro Carvalho Chehab my $start = ""; 770c0d1b6eeSJonathan Corbet 77182801d06SMauro Carvalho Chehab if ($args{'typedef'}) { 77282801d06SMauro Carvalho Chehab print ".. c:type:: ". $args{'function'} . "\n\n"; 77382801d06SMauro Carvalho Chehab print_lineno($declaration_start_line); 77482801d06SMauro Carvalho Chehab print " **Typedef**: "; 77582801d06SMauro Carvalho Chehab $lineprefix = ""; 77682801d06SMauro Carvalho Chehab output_highlight_rst($args{'purpose'}); 77782801d06SMauro Carvalho Chehab $start = "\n\n**Syntax**\n\n ``"; 778c0d1b6eeSJonathan Corbet } else { 77982801d06SMauro Carvalho Chehab print ".. c:function:: "; 78082801d06SMauro Carvalho Chehab } 78182801d06SMauro Carvalho Chehab if ($args{'functiontype'} ne "") { 78282801d06SMauro Carvalho Chehab $start .= $args{'functiontype'} . " " . $args{'function'} . " ("; 78382801d06SMauro Carvalho Chehab } else { 78482801d06SMauro Carvalho Chehab $start .= $args{'function'} . " ("; 785c0d1b6eeSJonathan Corbet } 786c0d1b6eeSJonathan Corbet print $start; 787c0d1b6eeSJonathan Corbet 788c0d1b6eeSJonathan Corbet my $count = 0; 789c0d1b6eeSJonathan Corbet foreach my $parameter (@{$args{'parameterlist'}}) { 790c0d1b6eeSJonathan Corbet if ($count ne 0) { 791c0d1b6eeSJonathan Corbet print ", "; 792c0d1b6eeSJonathan Corbet } 793c0d1b6eeSJonathan Corbet $count++; 794c0d1b6eeSJonathan Corbet $type = $args{'parametertypes'}{$parameter}; 795a88b1672SMauro Carvalho Chehab 796c0d1b6eeSJonathan Corbet if ($type =~ m/([^\(]*\(\*)\s*\)\s*\(([^\)]*)\)/) { 797c0d1b6eeSJonathan Corbet # pointer-to-function 798c0d1b6eeSJonathan Corbet print $1 . $parameter . ") (" . $2; 799c0d1b6eeSJonathan Corbet } else { 800c0d1b6eeSJonathan Corbet print $type . " " . $parameter; 801c0d1b6eeSJonathan Corbet } 802c0d1b6eeSJonathan Corbet } 80382801d06SMauro Carvalho Chehab if ($args{'typedef'}) { 80482801d06SMauro Carvalho Chehab print ");``\n\n"; 80582801d06SMauro Carvalho Chehab } else { 806c099ff69SJani Nikula print ")\n\n"; 8070b0f5f29SDaniel Vetter print_lineno($declaration_start_line); 808c099ff69SJani Nikula $lineprefix = " "; 809c099ff69SJani Nikula output_highlight_rst($args{'purpose'}); 810c099ff69SJani Nikula print "\n"; 81182801d06SMauro Carvalho Chehab } 812c0d1b6eeSJonathan Corbet 813ecbcfba1SJani Nikula print "**Parameters**\n\n"; 814c099ff69SJani Nikula $lineprefix = " "; 815c0d1b6eeSJonathan Corbet foreach $parameter (@{$args{'parameterlist'}}) { 816c0d1b6eeSJonathan Corbet my $parameter_name = $parameter; 817ada5f446SGabriel Krisman Bertazi $parameter_name =~ s/\[.*//; 818c0d1b6eeSJonathan Corbet $type = $args{'parametertypes'}{$parameter}; 819c0d1b6eeSJonathan Corbet 820c0d1b6eeSJonathan Corbet if ($type ne "") { 821c0d1b6eeSJonathan Corbet print "``$type $parameter``\n"; 822c0d1b6eeSJonathan Corbet } else { 823c0d1b6eeSJonathan Corbet print "``$parameter``\n"; 824c0d1b6eeSJonathan Corbet } 8250b0f5f29SDaniel Vetter 8260b0f5f29SDaniel Vetter print_lineno($parameterdesc_start_lines{$parameter_name}); 8270b0f5f29SDaniel Vetter 8285e64fa9cSJani Nikula if (defined($args{'parameterdescs'}{$parameter_name}) && 8295e64fa9cSJani Nikula $args{'parameterdescs'}{$parameter_name} ne $undescribed) { 830c0d1b6eeSJonathan Corbet output_highlight_rst($args{'parameterdescs'}{$parameter_name}); 831c0d1b6eeSJonathan Corbet } else { 832d4b08e0cSJani Nikula print " *undescribed*\n"; 833c0d1b6eeSJonathan Corbet } 834c0d1b6eeSJonathan Corbet print "\n"; 835c0d1b6eeSJonathan Corbet } 836c099ff69SJani Nikula 837c099ff69SJani Nikula $lineprefix = $oldprefix; 838c0d1b6eeSJonathan Corbet output_section_rst(@_); 839c0d1b6eeSJonathan Corbet} 840c0d1b6eeSJonathan Corbet 841c0d1b6eeSJonathan Corbetsub output_section_rst(%) { 842c0d1b6eeSJonathan Corbet my %args = %{$_[0]}; 843c0d1b6eeSJonathan Corbet my $section; 844c0d1b6eeSJonathan Corbet my $oldprefix = $lineprefix; 845c0d1b6eeSJonathan Corbet $lineprefix = ""; 846c0d1b6eeSJonathan Corbet 847c0d1b6eeSJonathan Corbet foreach $section (@{$args{'sectionlist'}}) { 848ecbcfba1SJani Nikula print "**$section**\n\n"; 8490b0f5f29SDaniel Vetter print_lineno($section_start_lines{$section}); 850c0d1b6eeSJonathan Corbet output_highlight_rst($args{'sections'}{$section}); 851c0d1b6eeSJonathan Corbet print "\n"; 852c0d1b6eeSJonathan Corbet } 853c0d1b6eeSJonathan Corbet print "\n"; 854c0d1b6eeSJonathan Corbet $lineprefix = $oldprefix; 855c0d1b6eeSJonathan Corbet} 856c0d1b6eeSJonathan Corbet 857c0d1b6eeSJonathan Corbetsub output_enum_rst(%) { 858c0d1b6eeSJonathan Corbet my %args = %{$_[0]}; 859c0d1b6eeSJonathan Corbet my ($parameter); 860c099ff69SJani Nikula my $oldprefix = $lineprefix; 861c0d1b6eeSJonathan Corbet my $count; 862c0d1b6eeSJonathan Corbet my $name = "enum " . $args{'enum'}; 86362850976SJani Nikula 86462850976SJani Nikula print "\n\n.. c:type:: " . $name . "\n\n"; 8650b0f5f29SDaniel Vetter print_lineno($declaration_start_line); 866c099ff69SJani Nikula $lineprefix = " "; 867c099ff69SJani Nikula output_highlight_rst($args{'purpose'}); 868c099ff69SJani Nikula print "\n"; 869c0d1b6eeSJonathan Corbet 870ecbcfba1SJani Nikula print "**Constants**\n\n"; 871c0d1b6eeSJonathan Corbet $lineprefix = " "; 872c0d1b6eeSJonathan Corbet foreach $parameter (@{$args{'parameterlist'}}) { 873ecbcfba1SJani Nikula print "``$parameter``\n"; 874c0d1b6eeSJonathan Corbet if ($args{'parameterdescs'}{$parameter} ne $undescribed) { 875c0d1b6eeSJonathan Corbet output_highlight_rst($args{'parameterdescs'}{$parameter}); 876c0d1b6eeSJonathan Corbet } else { 877d4b08e0cSJani Nikula print " *undescribed*\n"; 878c0d1b6eeSJonathan Corbet } 879c0d1b6eeSJonathan Corbet print "\n"; 880c0d1b6eeSJonathan Corbet } 881c099ff69SJani Nikula 882c0d1b6eeSJonathan Corbet $lineprefix = $oldprefix; 883c0d1b6eeSJonathan Corbet output_section_rst(@_); 884c0d1b6eeSJonathan Corbet} 885c0d1b6eeSJonathan Corbet 886c0d1b6eeSJonathan Corbetsub output_typedef_rst(%) { 887c0d1b6eeSJonathan Corbet my %args = %{$_[0]}; 888c0d1b6eeSJonathan Corbet my ($parameter); 889c099ff69SJani Nikula my $oldprefix = $lineprefix; 890c0d1b6eeSJonathan Corbet my $name = "typedef " . $args{'typedef'}; 891c0d1b6eeSJonathan Corbet 89262850976SJani Nikula print "\n\n.. c:type:: " . $name . "\n\n"; 8930b0f5f29SDaniel Vetter print_lineno($declaration_start_line); 894c099ff69SJani Nikula $lineprefix = " "; 895c099ff69SJani Nikula output_highlight_rst($args{'purpose'}); 896c099ff69SJani Nikula print "\n"; 897c0d1b6eeSJonathan Corbet 898c099ff69SJani Nikula $lineprefix = $oldprefix; 899c0d1b6eeSJonathan Corbet output_section_rst(@_); 900c0d1b6eeSJonathan Corbet} 901c0d1b6eeSJonathan Corbet 902c0d1b6eeSJonathan Corbetsub output_struct_rst(%) { 903c0d1b6eeSJonathan Corbet my %args = %{$_[0]}; 904c0d1b6eeSJonathan Corbet my ($parameter); 905c099ff69SJani Nikula my $oldprefix = $lineprefix; 906c0d1b6eeSJonathan Corbet my $name = $args{'type'} . " " . $args{'struct'}; 907c0d1b6eeSJonathan Corbet 90862850976SJani Nikula print "\n\n.. c:type:: " . $name . "\n\n"; 9090b0f5f29SDaniel Vetter print_lineno($declaration_start_line); 910c099ff69SJani Nikula $lineprefix = " "; 911c099ff69SJani Nikula output_highlight_rst($args{'purpose'}); 912c099ff69SJani Nikula print "\n"; 913c0d1b6eeSJonathan Corbet 914ecbcfba1SJani Nikula print "**Definition**\n\n"; 915c0d1b6eeSJonathan Corbet print "::\n\n"; 9168ad72163SMauro Carvalho Chehab my $declaration = $args{'definition'}; 9178ad72163SMauro Carvalho Chehab $declaration =~ s/\t/ /g; 9188ad72163SMauro Carvalho Chehab print " " . $args{'type'} . " " . $args{'struct'} . " {\n$declaration };\n\n"; 919c0d1b6eeSJonathan Corbet 920ecbcfba1SJani Nikula print "**Members**\n\n"; 921c099ff69SJani Nikula $lineprefix = " "; 922c0d1b6eeSJonathan Corbet foreach $parameter (@{$args{'parameterlist'}}) { 923c0d1b6eeSJonathan Corbet ($parameter =~ /^#/) && next; 924c0d1b6eeSJonathan Corbet 925c0d1b6eeSJonathan Corbet my $parameter_name = $parameter; 926c0d1b6eeSJonathan Corbet $parameter_name =~ s/\[.*//; 927c0d1b6eeSJonathan Corbet 928c0d1b6eeSJonathan Corbet ($args{'parameterdescs'}{$parameter_name} ne $undescribed) || next; 929c0d1b6eeSJonathan Corbet $type = $args{'parametertypes'}{$parameter}; 9300b0f5f29SDaniel Vetter print_lineno($parameterdesc_start_lines{$parameter_name}); 9316d232c80SMauro Carvalho Chehab print "``" . $parameter . "``\n"; 932c0d1b6eeSJonathan Corbet output_highlight_rst($args{'parameterdescs'}{$parameter_name}); 933c0d1b6eeSJonathan Corbet print "\n"; 934c0d1b6eeSJonathan Corbet } 935c0d1b6eeSJonathan Corbet print "\n"; 936c099ff69SJani Nikula 937c099ff69SJani Nikula $lineprefix = $oldprefix; 938c0d1b6eeSJonathan Corbet output_section_rst(@_); 939c0d1b6eeSJonathan Corbet} 940c0d1b6eeSJonathan Corbet 9413a025e1dSMatthew Wilcox## none mode output functions 9423a025e1dSMatthew Wilcox 9433a025e1dSMatthew Wilcoxsub output_function_none(%) { 9443a025e1dSMatthew Wilcox} 9453a025e1dSMatthew Wilcox 9463a025e1dSMatthew Wilcoxsub output_enum_none(%) { 9473a025e1dSMatthew Wilcox} 9483a025e1dSMatthew Wilcox 9493a025e1dSMatthew Wilcoxsub output_typedef_none(%) { 9503a025e1dSMatthew Wilcox} 9513a025e1dSMatthew Wilcox 9523a025e1dSMatthew Wilcoxsub output_struct_none(%) { 9533a025e1dSMatthew Wilcox} 9543a025e1dSMatthew Wilcox 9553a025e1dSMatthew Wilcoxsub output_blockhead_none(%) { 9563a025e1dSMatthew Wilcox} 9573a025e1dSMatthew Wilcox 9581da177e4SLinus Torvalds## 95927205744SRandy Dunlap# generic output function for all types (function, struct/union, typedef, enum); 96027205744SRandy Dunlap# calls the generated, variable output_ function name based on 96127205744SRandy Dunlap# functype and output_mode 9621da177e4SLinus Torvaldssub output_declaration { 9631da177e4SLinus Torvalds no strict 'refs'; 9641da177e4SLinus Torvalds my $name = shift; 9651da177e4SLinus Torvalds my $functype = shift; 9661da177e4SLinus Torvalds my $func = "output_${functype}_$output_mode"; 967b6c3f456SJani Nikula if (($output_selection == OUTPUT_ALL) || 968b6c3f456SJani Nikula (($output_selection == OUTPUT_INCLUDE || 969b6c3f456SJani Nikula $output_selection == OUTPUT_EXPORTED) && 97086ae2e38SJani Nikula defined($function_table{$name})) || 971b6c3f456SJani Nikula (($output_selection == OUTPUT_EXCLUDE || 972b6c3f456SJani Nikula $output_selection == OUTPUT_INTERNAL) && 97386ae2e38SJani Nikula !($functype eq "function" && defined($function_table{$name})))) 9741da177e4SLinus Torvalds { 9751da177e4SLinus Torvalds &$func(@_); 9761da177e4SLinus Torvalds $section_counter++; 9771da177e4SLinus Torvalds } 9781da177e4SLinus Torvalds} 9791da177e4SLinus Torvalds 9801da177e4SLinus Torvalds## 98127205744SRandy Dunlap# generic output function - calls the right one based on current output mode. 982b112e0f7SJohannes Bergsub output_blockhead { 9831da177e4SLinus Torvalds no strict 'refs'; 984b112e0f7SJohannes Berg my $func = "output_blockhead_" . $output_mode; 9851da177e4SLinus Torvalds &$func(@_); 9861da177e4SLinus Torvalds $section_counter++; 9871da177e4SLinus Torvalds} 9881da177e4SLinus Torvalds 9891da177e4SLinus Torvalds## 9901da177e4SLinus Torvalds# takes a declaration (struct, union, enum, typedef) and 9911da177e4SLinus Torvalds# invokes the right handler. NOT called for functions. 9921da177e4SLinus Torvaldssub dump_declaration($$) { 9931da177e4SLinus Torvalds no strict 'refs'; 9941da177e4SLinus Torvalds my ($prototype, $file) = @_; 9951da177e4SLinus Torvalds my $func = "dump_" . $decl_type; 9961da177e4SLinus Torvalds &$func(@_); 9971da177e4SLinus Torvalds} 9981da177e4SLinus Torvalds 9991da177e4SLinus Torvaldssub dump_union($$) { 10001da177e4SLinus Torvalds dump_struct(@_); 10011da177e4SLinus Torvalds} 10021da177e4SLinus Torvalds 10031da177e4SLinus Torvaldssub dump_struct($$) { 10041da177e4SLinus Torvalds my $x = shift; 10051da177e4SLinus Torvalds my $file = shift; 10061da177e4SLinus Torvalds 10071da177e4SLinus Torvalds if ($x =~ /(struct|union)\s+(\w+)\s*{(.*)}/) { 10085cb5c31cSJohannes Berg my $decl_type = $1; 10091da177e4SLinus Torvalds $declaration_name = $2; 10101da177e4SLinus Torvalds my $members = $3; 10111da177e4SLinus Torvalds 1012aeec46b9SMartin Waitz # ignore members marked private: 10130d8c39e6SMauro Carvalho Chehab $members =~ s/\/\*\s*private:.*?\/\*\s*public:.*?\*\///gosi; 10140d8c39e6SMauro Carvalho Chehab $members =~ s/\/\*\s*private:.*//gosi; 1015aeec46b9SMartin Waitz # strip comments: 1016aeec46b9SMartin Waitz $members =~ s/\/\*.*?\*\///gos; 1017ef5da59fSRandy Dunlap # strip attributes 1018f0074929SJonathan Corbet $members =~ s/__attribute__\s*\(\([a-z,_\*\s\(\)]*\)\)//i; 10197b990789SJohannes Berg $members =~ s/__aligned\s*\([^;]*\)//gos; 1020f0074929SJonathan Corbet $members =~ s/\s*CRYPTO_MINALIGN_ATTR//gos; 1021b22b5a9eSConchúr Navid # replace DECLARE_BITMAP 102245005b27SMauro Carvalho Chehab $members =~ s/DECLARE_BITMAP\s*\(([^,)]+),\s*([^,)]+)\)/unsigned long $1\[BITS_TO_LONGS($2)\]/gos; 10231cb566baSJakub Kicinski # replace DECLARE_HASHTABLE 102445005b27SMauro Carvalho Chehab $members =~ s/DECLARE_HASHTABLE\s*\(([^,)]+),\s*([^,)]+)\)/unsigned long $1\[1 << (($2) - 1)\]/gos; 102545005b27SMauro Carvalho Chehab # replace DECLARE_KFIFO 102645005b27SMauro Carvalho Chehab $members =~ s/DECLARE_KFIFO\s*\(([^,)]+),\s*([^,)]+),\s*([^,)]+)\)/$2 \*$1/gos; 102745005b27SMauro Carvalho Chehab # replace DECLARE_KFIFO_PTR 102845005b27SMauro Carvalho Chehab $members =~ s/DECLARE_KFIFO_PTR\s*\(([^,)]+),\s*([^,)]+)\)/$2 \*$1/gos; 1029aeec46b9SMartin Waitz 10308ad72163SMauro Carvalho Chehab my $declaration = $members; 10318ad72163SMauro Carvalho Chehab 10328ad72163SMauro Carvalho Chehab # Split nested struct/union elements as newer ones 10338ad72163SMauro Carvalho Chehab my $cont = 1; 10348ad72163SMauro Carvalho Chehab while ($cont) { 10358ad72163SMauro Carvalho Chehab $cont = 0; 10368ad72163SMauro Carvalho Chehab while ($members =~ m/(struct|union)([^{};]+){([^{}]*)}([^{}\;]*)\;/) { 10378ad72163SMauro Carvalho Chehab my $newmember = "$1 $4;"; 10388ad72163SMauro Carvalho Chehab my $id = $4; 10398ad72163SMauro Carvalho Chehab my $content = $3; 10408ad72163SMauro Carvalho Chehab $id =~ s/[:\[].*//; 10418ad72163SMauro Carvalho Chehab $id =~ s/^\*+//; 10428ad72163SMauro Carvalho Chehab foreach my $arg (split /;/, $content) { 10438ad72163SMauro Carvalho Chehab next if ($arg =~ m/^\s*$/); 10448ad72163SMauro Carvalho Chehab my $type = $arg; 10458ad72163SMauro Carvalho Chehab my $name = $arg; 10468ad72163SMauro Carvalho Chehab $type =~ s/\s\S+$//; 10478ad72163SMauro Carvalho Chehab $name =~ s/.*\s//; 10488ad72163SMauro Carvalho Chehab $name =~ s/[:\[].*//; 10498ad72163SMauro Carvalho Chehab $name =~ s/^\*+//; 10508ad72163SMauro Carvalho Chehab next if (($name =~ m/^\s*$/)); 10518ad72163SMauro Carvalho Chehab if ($id =~ m/^\s*$/) { 10528ad72163SMauro Carvalho Chehab # anonymous struct/union 10538ad72163SMauro Carvalho Chehab $newmember .= "$type $name;"; 10548ad72163SMauro Carvalho Chehab } else { 10558ad72163SMauro Carvalho Chehab $newmember .= "$type $id.$name;"; 10568ad72163SMauro Carvalho Chehab } 10578ad72163SMauro Carvalho Chehab } 10588ad72163SMauro Carvalho Chehab $members =~ s/(struct|union)([^{};]+){([^{}]*)}([^{}\;]*)\;/$newmember/; 10598ad72163SMauro Carvalho Chehab $cont = 1; 10608ad72163SMauro Carvalho Chehab }; 10618ad72163SMauro Carvalho Chehab }; 10628ad72163SMauro Carvalho Chehab 10638ad72163SMauro Carvalho Chehab # Ignore other nested elements, like enums 10648ad72163SMauro Carvalho Chehab $members =~ s/({[^\{\}]*})//g; 10658ad72163SMauro Carvalho Chehab 1066*151c468bSMauro Carvalho Chehab create_parameterlist($members, ';', $file, $declaration_name); 10671081de2dSMauro Carvalho Chehab check_sections($file, $declaration_name, $decl_type, $sectcheck, $struct_actual); 10681da177e4SLinus Torvalds 10698ad72163SMauro Carvalho Chehab # Adjust declaration for better display 10708ad72163SMauro Carvalho Chehab $declaration =~ s/([{;])/$1\n/g; 10718ad72163SMauro Carvalho Chehab $declaration =~ s/}\s+;/};/g; 10728ad72163SMauro Carvalho Chehab # Better handle inlined enums 10738ad72163SMauro Carvalho Chehab do {} while ($declaration =~ s/(enum\s+{[^}]+),([^\n])/$1,\n$2/); 10748ad72163SMauro Carvalho Chehab 10758ad72163SMauro Carvalho Chehab my @def_args = split /\n/, $declaration; 10768ad72163SMauro Carvalho Chehab my $level = 1; 10778ad72163SMauro Carvalho Chehab $declaration = ""; 10788ad72163SMauro Carvalho Chehab foreach my $clause (@def_args) { 10798ad72163SMauro Carvalho Chehab $clause =~ s/^\s+//; 10808ad72163SMauro Carvalho Chehab $clause =~ s/\s+$//; 10818ad72163SMauro Carvalho Chehab $clause =~ s/\s+/ /; 10828ad72163SMauro Carvalho Chehab next if (!$clause); 10838ad72163SMauro Carvalho Chehab $level-- if ($clause =~ m/(})/ && $level > 1); 10848ad72163SMauro Carvalho Chehab if (!($clause =~ m/^\s*#/)) { 10858ad72163SMauro Carvalho Chehab $declaration .= "\t" x $level; 10868ad72163SMauro Carvalho Chehab } 10878ad72163SMauro Carvalho Chehab $declaration .= "\t" . $clause . "\n"; 10888ad72163SMauro Carvalho Chehab $level++ if ($clause =~ m/({)/ && !($clause =~m/}/)); 10898ad72163SMauro Carvalho Chehab } 10901da177e4SLinus Torvalds output_declaration($declaration_name, 10911da177e4SLinus Torvalds 'struct', 10921da177e4SLinus Torvalds {'struct' => $declaration_name, 10931da177e4SLinus Torvalds 'module' => $modulename, 10948ad72163SMauro Carvalho Chehab 'definition' => $declaration, 10951da177e4SLinus Torvalds 'parameterlist' => \@parameterlist, 10961da177e4SLinus Torvalds 'parameterdescs' => \%parameterdescs, 10971da177e4SLinus Torvalds 'parametertypes' => \%parametertypes, 10981da177e4SLinus Torvalds 'sectionlist' => \@sectionlist, 10991da177e4SLinus Torvalds 'sections' => \%sections, 11001da177e4SLinus Torvalds 'purpose' => $declaration_purpose, 11011da177e4SLinus Torvalds 'type' => $decl_type 11021da177e4SLinus Torvalds }); 11031da177e4SLinus Torvalds } 11041da177e4SLinus Torvalds else { 1105d40e1e65SBart Van Assche print STDERR "${file}:$.: error: Cannot parse struct or union!\n"; 11061da177e4SLinus Torvalds ++$errors; 11071da177e4SLinus Torvalds } 11081da177e4SLinus Torvalds} 11091da177e4SLinus Torvalds 11101da177e4SLinus Torvaldssub dump_enum($$) { 11111da177e4SLinus Torvalds my $x = shift; 11121da177e4SLinus Torvalds my $file = shift; 11131da177e4SLinus Torvalds 1114aeec46b9SMartin Waitz $x =~ s@/\*.*?\*/@@gos; # strip comments. 11154468e21eSConchúr Navid # strip #define macros inside enums 11164468e21eSConchúr Navid $x =~ s@#\s*((define|ifdef)\s+|endif)[^;]*;@@gos; 1117b6d676dbSRandy Dunlap 11181da177e4SLinus Torvalds if ($x =~ /enum\s+(\w+)\s*{(.*)}/) { 11191da177e4SLinus Torvalds $declaration_name = $1; 11201da177e4SLinus Torvalds my $members = $2; 11215cb5c31cSJohannes Berg my %_members; 11225cb5c31cSJohannes Berg 1123463a0fdcSMarkus Heiser $members =~ s/\s+$//; 11241da177e4SLinus Torvalds 11251da177e4SLinus Torvalds foreach my $arg (split ',', $members) { 11261da177e4SLinus Torvalds $arg =~ s/^\s*(\w+).*/$1/; 11271da177e4SLinus Torvalds push @parameterlist, $arg; 11281da177e4SLinus Torvalds if (!$parameterdescs{$arg}) { 11291da177e4SLinus Torvalds $parameterdescs{$arg} = $undescribed; 1130d40e1e65SBart Van Assche print STDERR "${file}:$.: warning: Enum value '$arg' ". 11311da177e4SLinus Torvalds "not described in enum '$declaration_name'\n"; 11321da177e4SLinus Torvalds } 11335cb5c31cSJohannes Berg $_members{$arg} = 1; 11345cb5c31cSJohannes Berg } 11351da177e4SLinus Torvalds 11365cb5c31cSJohannes Berg while (my ($k, $v) = each %parameterdescs) { 11375cb5c31cSJohannes Berg if (!exists($_members{$k})) { 11385cb5c31cSJohannes Berg print STDERR "${file}:$.: warning: Excess enum value " . 11395cb5c31cSJohannes Berg "'$k' description in '$declaration_name'\n"; 11405cb5c31cSJohannes Berg } 11411da177e4SLinus Torvalds } 11421da177e4SLinus Torvalds 11431da177e4SLinus Torvalds output_declaration($declaration_name, 11441da177e4SLinus Torvalds 'enum', 11451da177e4SLinus Torvalds {'enum' => $declaration_name, 11461da177e4SLinus Torvalds 'module' => $modulename, 11471da177e4SLinus Torvalds 'parameterlist' => \@parameterlist, 11481da177e4SLinus Torvalds 'parameterdescs' => \%parameterdescs, 11491da177e4SLinus Torvalds 'sectionlist' => \@sectionlist, 11501da177e4SLinus Torvalds 'sections' => \%sections, 11511da177e4SLinus Torvalds 'purpose' => $declaration_purpose 11521da177e4SLinus Torvalds }); 11531da177e4SLinus Torvalds } 11541da177e4SLinus Torvalds else { 1155d40e1e65SBart Van Assche print STDERR "${file}:$.: error: Cannot parse enum!\n"; 11561da177e4SLinus Torvalds ++$errors; 11571da177e4SLinus Torvalds } 11581da177e4SLinus Torvalds} 11591da177e4SLinus Torvalds 11601da177e4SLinus Torvaldssub dump_typedef($$) { 11611da177e4SLinus Torvalds my $x = shift; 11621da177e4SLinus Torvalds my $file = shift; 11631da177e4SLinus Torvalds 1164aeec46b9SMartin Waitz $x =~ s@/\*.*?\*/@@gos; # strip comments. 116583766452SMauro Carvalho Chehab 116683766452SMauro Carvalho Chehab # Parse function prototypes 1167d37c43ceSMauro Carvalho Chehab if ($x =~ /typedef\s+(\w+)\s*\(\*\s*(\w\S+)\s*\)\s*\((.*)\);/ || 1168d37c43ceSMauro Carvalho Chehab $x =~ /typedef\s+(\w+)\s*(\w\S+)\s*\s*\((.*)\);/) { 1169d37c43ceSMauro Carvalho Chehab 117083766452SMauro Carvalho Chehab # Function typedefs 117183766452SMauro Carvalho Chehab $return_type = $1; 117283766452SMauro Carvalho Chehab $declaration_name = $2; 117383766452SMauro Carvalho Chehab my $args = $3; 117483766452SMauro Carvalho Chehab 1175*151c468bSMauro Carvalho Chehab create_parameterlist($args, ',', $file, $declaration_name); 117683766452SMauro Carvalho Chehab 117783766452SMauro Carvalho Chehab output_declaration($declaration_name, 117883766452SMauro Carvalho Chehab 'function', 117983766452SMauro Carvalho Chehab {'function' => $declaration_name, 118082801d06SMauro Carvalho Chehab 'typedef' => 1, 118183766452SMauro Carvalho Chehab 'module' => $modulename, 118283766452SMauro Carvalho Chehab 'functiontype' => $return_type, 118383766452SMauro Carvalho Chehab 'parameterlist' => \@parameterlist, 118483766452SMauro Carvalho Chehab 'parameterdescs' => \%parameterdescs, 118583766452SMauro Carvalho Chehab 'parametertypes' => \%parametertypes, 118683766452SMauro Carvalho Chehab 'sectionlist' => \@sectionlist, 118783766452SMauro Carvalho Chehab 'sections' => \%sections, 118883766452SMauro Carvalho Chehab 'purpose' => $declaration_purpose 118983766452SMauro Carvalho Chehab }); 119083766452SMauro Carvalho Chehab return; 119183766452SMauro Carvalho Chehab } 119283766452SMauro Carvalho Chehab 11931da177e4SLinus Torvalds while (($x =~ /\(*.\)\s*;$/) || ($x =~ /\[*.\]\s*;$/)) { 11941da177e4SLinus Torvalds $x =~ s/\(*.\)\s*;$/;/; 11951da177e4SLinus Torvalds $x =~ s/\[*.\]\s*;$/;/; 11961da177e4SLinus Torvalds } 11971da177e4SLinus Torvalds 11981da177e4SLinus Torvalds if ($x =~ /typedef.*\s+(\w+)\s*;/) { 11991da177e4SLinus Torvalds $declaration_name = $1; 12001da177e4SLinus Torvalds 12011da177e4SLinus Torvalds output_declaration($declaration_name, 12021da177e4SLinus Torvalds 'typedef', 12031da177e4SLinus Torvalds {'typedef' => $declaration_name, 12041da177e4SLinus Torvalds 'module' => $modulename, 12051da177e4SLinus Torvalds 'sectionlist' => \@sectionlist, 12061da177e4SLinus Torvalds 'sections' => \%sections, 12071da177e4SLinus Torvalds 'purpose' => $declaration_purpose 12081da177e4SLinus Torvalds }); 12091da177e4SLinus Torvalds } 12101da177e4SLinus Torvalds else { 1211d40e1e65SBart Van Assche print STDERR "${file}:$.: error: Cannot parse typedef!\n"; 12121da177e4SLinus Torvalds ++$errors; 12131da177e4SLinus Torvalds } 12141da177e4SLinus Torvalds} 12151da177e4SLinus Torvalds 1216a1d94aa5SRandy Dunlapsub save_struct_actual($) { 1217a1d94aa5SRandy Dunlap my $actual = shift; 1218a1d94aa5SRandy Dunlap 1219a1d94aa5SRandy Dunlap # strip all spaces from the actual param so that it looks like one string item 1220a1d94aa5SRandy Dunlap $actual =~ s/\s*//g; 1221a1d94aa5SRandy Dunlap $struct_actual = $struct_actual . $actual . " "; 1222a1d94aa5SRandy Dunlap} 1223a1d94aa5SRandy Dunlap 1224*151c468bSMauro Carvalho Chehabsub create_parameterlist($$$$) { 12251da177e4SLinus Torvalds my $args = shift; 12261da177e4SLinus Torvalds my $splitter = shift; 12271da177e4SLinus Torvalds my $file = shift; 1228*151c468bSMauro Carvalho Chehab my $declaration_name = shift; 12291da177e4SLinus Torvalds my $type; 12301da177e4SLinus Torvalds my $param; 12311da177e4SLinus Torvalds 1232a6d3fe77SMartin Waitz # temporarily replace commas inside function pointer definition 12331da177e4SLinus Torvalds while ($args =~ /(\([^\),]+),/) { 12341da177e4SLinus Torvalds $args =~ s/(\([^\),]+),/$1#/g; 12351da177e4SLinus Torvalds } 12361da177e4SLinus Torvalds 12371da177e4SLinus Torvalds foreach my $arg (split($splitter, $args)) { 12381da177e4SLinus Torvalds # strip comments 12391da177e4SLinus Torvalds $arg =~ s/\/\*.*\*\///; 12401da177e4SLinus Torvalds # strip leading/trailing spaces 12411da177e4SLinus Torvalds $arg =~ s/^\s*//; 12421da177e4SLinus Torvalds $arg =~ s/\s*$//; 12431da177e4SLinus Torvalds $arg =~ s/\s+/ /; 12441da177e4SLinus Torvalds 12451da177e4SLinus Torvalds if ($arg =~ /^#/) { 12461da177e4SLinus Torvalds # Treat preprocessor directive as a typeless variable just to fill 12471da177e4SLinus Torvalds # corresponding data structures "correctly". Catch it later in 12481da177e4SLinus Torvalds # output_* subs. 12491da177e4SLinus Torvalds push_parameter($arg, "", $file); 125000d62961SRichard Kennedy } elsif ($arg =~ m/\(.+\)\s*\(/) { 12511da177e4SLinus Torvalds # pointer-to-function 12521da177e4SLinus Torvalds $arg =~ tr/#/,/; 125300d62961SRichard Kennedy $arg =~ m/[^\(]+\(\*?\s*(\w*)\s*\)/; 12541da177e4SLinus Torvalds $param = $1; 12551da177e4SLinus Torvalds $type = $arg; 125600d62961SRichard Kennedy $type =~ s/([^\(]+\(\*?)\s*$param/$1/; 1257a1d94aa5SRandy Dunlap save_struct_actual($param); 1258*151c468bSMauro Carvalho Chehab push_parameter($param, $type, $file, $declaration_name); 1259aeec46b9SMartin Waitz } elsif ($arg) { 12601da177e4SLinus Torvalds $arg =~ s/\s*:\s*/:/g; 12611da177e4SLinus Torvalds $arg =~ s/\s*\[/\[/g; 12621da177e4SLinus Torvalds 12631da177e4SLinus Torvalds my @args = split('\s*,\s*', $arg); 12641da177e4SLinus Torvalds if ($args[0] =~ m/\*/) { 12651da177e4SLinus Torvalds $args[0] =~ s/(\*+)\s*/ $1/; 12661da177e4SLinus Torvalds } 1267884f2810SBorislav Petkov 1268884f2810SBorislav Petkov my @first_arg; 1269884f2810SBorislav Petkov if ($args[0] =~ /^(.*\s+)(.*?\[.*\].*)$/) { 1270884f2810SBorislav Petkov shift @args; 1271884f2810SBorislav Petkov push(@first_arg, split('\s+', $1)); 1272884f2810SBorislav Petkov push(@first_arg, $2); 1273884f2810SBorislav Petkov } else { 1274884f2810SBorislav Petkov @first_arg = split('\s+', shift @args); 1275884f2810SBorislav Petkov } 1276884f2810SBorislav Petkov 12771da177e4SLinus Torvalds unshift(@args, pop @first_arg); 12781da177e4SLinus Torvalds $type = join " ", @first_arg; 12791da177e4SLinus Torvalds 12801da177e4SLinus Torvalds foreach $param (@args) { 12811da177e4SLinus Torvalds if ($param =~ m/^(\*+)\s*(.*)/) { 1282a1d94aa5SRandy Dunlap save_struct_actual($2); 1283*151c468bSMauro Carvalho Chehab push_parameter($2, "$type $1", $file, $declaration_name); 12841da177e4SLinus Torvalds } 12851da177e4SLinus Torvalds elsif ($param =~ m/(.*?):(\d+)/) { 12867b97887eSRandy Dunlap if ($type ne "") { # skip unnamed bit-fields 1287a1d94aa5SRandy Dunlap save_struct_actual($1); 1288*151c468bSMauro Carvalho Chehab push_parameter($1, "$type:$2", $file, $declaration_name) 12891da177e4SLinus Torvalds } 12907b97887eSRandy Dunlap } 12911da177e4SLinus Torvalds else { 1292a1d94aa5SRandy Dunlap save_struct_actual($param); 1293*151c468bSMauro Carvalho Chehab push_parameter($param, $type, $file, $declaration_name); 12941da177e4SLinus Torvalds } 12951da177e4SLinus Torvalds } 12961da177e4SLinus Torvalds } 12971da177e4SLinus Torvalds } 12981da177e4SLinus Torvalds} 12991da177e4SLinus Torvalds 1300*151c468bSMauro Carvalho Chehabsub push_parameter($$$$) { 13011da177e4SLinus Torvalds my $param = shift; 13021da177e4SLinus Torvalds my $type = shift; 13031da177e4SLinus Torvalds my $file = shift; 1304*151c468bSMauro Carvalho Chehab my $declaration_name = shift; 13051da177e4SLinus Torvalds 13065f8c7c98SRandy Dunlap if (($anon_struct_union == 1) && ($type eq "") && 13075f8c7c98SRandy Dunlap ($param eq "}")) { 13085f8c7c98SRandy Dunlap return; # ignore the ending }; from anon. struct/union 13095f8c7c98SRandy Dunlap } 13105f8c7c98SRandy Dunlap 13115f8c7c98SRandy Dunlap $anon_struct_union = 0; 1312f9b5c530SMauro Carvalho Chehab $param =~ s/[\[\)].*//; 13131da177e4SLinus Torvalds 1314a6d3fe77SMartin Waitz if ($type eq "" && $param =~ /\.\.\.$/) 13151da177e4SLinus Torvalds { 1316c950a173SSilvio Fricke if (!$param =~ /\w\.\.\.$/) { 1317c950a173SSilvio Fricke # handles unnamed variable parameters 1318ef00028bSJonathan Corbet $param = "..."; 1319c950a173SSilvio Fricke } 1320ced69090SRandy Dunlap if (!defined $parameterdescs{$param} || $parameterdescs{$param} eq "") { 1321a6d3fe77SMartin Waitz $parameterdescs{$param} = "variable arguments"; 13221da177e4SLinus Torvalds } 1323ced69090SRandy Dunlap } 13241da177e4SLinus Torvalds elsif ($type eq "" && ($param eq "" or $param eq "void")) 13251da177e4SLinus Torvalds { 13261da177e4SLinus Torvalds $param="void"; 13271da177e4SLinus Torvalds $parameterdescs{void} = "no arguments"; 13281da177e4SLinus Torvalds } 1329134fe01bSRandy Dunlap elsif ($type eq "" && ($param eq "struct" or $param eq "union")) 1330134fe01bSRandy Dunlap # handle unnamed (anonymous) union or struct: 1331134fe01bSRandy Dunlap { 1332134fe01bSRandy Dunlap $type = $param; 1333134fe01bSRandy Dunlap $param = "{unnamed_" . $param . "}"; 1334134fe01bSRandy Dunlap $parameterdescs{$param} = "anonymous\n"; 13355f8c7c98SRandy Dunlap $anon_struct_union = 1; 1336134fe01bSRandy Dunlap } 1337134fe01bSRandy Dunlap 1338a6d3fe77SMartin Waitz # warn if parameter has no description 1339134fe01bSRandy Dunlap # (but ignore ones starting with # as these are not parameters 1340134fe01bSRandy Dunlap # but inline preprocessor statements); 1341*151c468bSMauro Carvalho Chehab # Note: It will also ignore void params and unnamed structs/unions 1342f9b5c530SMauro Carvalho Chehab if (!defined $parameterdescs{$param} && $param !~ /^#/) { 1343f9b5c530SMauro Carvalho Chehab $parameterdescs{$param} = $undescribed; 13441da177e4SLinus Torvalds 1345*151c468bSMauro Carvalho Chehab print STDERR 1346*151c468bSMauro Carvalho Chehab "${file}:$.: warning: Function parameter or member '$param' not described in '$declaration_name'\n"; 13471da177e4SLinus Torvalds ++$warnings; 13481da177e4SLinus Torvalds } 13491da177e4SLinus Torvalds 13502b35f4d9SRandy Dunlap $param = xml_escape($param); 13512b35f4d9SRandy Dunlap 135225985edcSLucas De Marchi # strip spaces from $param so that it is one continuous string 1353e34e7dbbSRandy Dunlap # on @parameterlist; 1354e34e7dbbSRandy Dunlap # this fixes a problem where check_sections() cannot find 1355e34e7dbbSRandy Dunlap # a parameter like "addr[6 + 2]" because it actually appears 1356e34e7dbbSRandy Dunlap # as "addr[6", "+", "2]" on the parameter list; 1357e34e7dbbSRandy Dunlap # but it's better to maintain the param string unchanged for output, 1358e34e7dbbSRandy Dunlap # so just weaken the string compare in check_sections() to ignore 1359e34e7dbbSRandy Dunlap # "[blah" in a parameter string; 1360e34e7dbbSRandy Dunlap ###$param =~ s/\s*//g; 13611da177e4SLinus Torvalds push @parameterlist, $param; 136202a4f4feSPaolo Bonzini $type =~ s/\s\s+/ /g; 13631da177e4SLinus Torvalds $parametertypes{$param} = $type; 13641da177e4SLinus Torvalds} 13651da177e4SLinus Torvalds 13661081de2dSMauro Carvalho Chehabsub check_sections($$$$$) { 13671081de2dSMauro Carvalho Chehab my ($file, $decl_name, $decl_type, $sectcheck, $prmscheck) = @_; 1368a1d94aa5SRandy Dunlap my @sects = split ' ', $sectcheck; 1369a1d94aa5SRandy Dunlap my @prms = split ' ', $prmscheck; 1370a1d94aa5SRandy Dunlap my $err; 1371a1d94aa5SRandy Dunlap my ($px, $sx); 1372a1d94aa5SRandy Dunlap my $prm_clean; # strip trailing "[array size]" and/or beginning "*" 1373a1d94aa5SRandy Dunlap 1374a1d94aa5SRandy Dunlap foreach $sx (0 .. $#sects) { 1375a1d94aa5SRandy Dunlap $err = 1; 1376a1d94aa5SRandy Dunlap foreach $px (0 .. $#prms) { 1377a1d94aa5SRandy Dunlap $prm_clean = $prms[$px]; 1378a1d94aa5SRandy Dunlap $prm_clean =~ s/\[.*\]//; 13791f3a6688SJohannes Berg $prm_clean =~ s/__attribute__\s*\(\([a-z,_\*\s\(\)]*\)\)//i; 1380e34e7dbbSRandy Dunlap # ignore array size in a parameter string; 1381e34e7dbbSRandy Dunlap # however, the original param string may contain 1382e34e7dbbSRandy Dunlap # spaces, e.g.: addr[6 + 2] 1383e34e7dbbSRandy Dunlap # and this appears in @prms as "addr[6" since the 1384e34e7dbbSRandy Dunlap # parameter list is split at spaces; 1385e34e7dbbSRandy Dunlap # hence just ignore "[..." for the sections check; 1386e34e7dbbSRandy Dunlap $prm_clean =~ s/\[.*//; 1387e34e7dbbSRandy Dunlap 1388a1d94aa5SRandy Dunlap ##$prm_clean =~ s/^\**//; 1389a1d94aa5SRandy Dunlap if ($prm_clean eq $sects[$sx]) { 1390a1d94aa5SRandy Dunlap $err = 0; 1391a1d94aa5SRandy Dunlap last; 1392a1d94aa5SRandy Dunlap } 1393a1d94aa5SRandy Dunlap } 1394a1d94aa5SRandy Dunlap if ($err) { 1395a1d94aa5SRandy Dunlap if ($decl_type eq "function") { 1396d40e1e65SBart Van Assche print STDERR "${file}:$.: warning: " . 1397a1d94aa5SRandy Dunlap "Excess function parameter " . 1398a1d94aa5SRandy Dunlap "'$sects[$sx]' " . 1399a1d94aa5SRandy Dunlap "description in '$decl_name'\n"; 1400a1d94aa5SRandy Dunlap ++$warnings; 1401a1d94aa5SRandy Dunlap } 1402a1d94aa5SRandy Dunlap } 1403a1d94aa5SRandy Dunlap } 1404a1d94aa5SRandy Dunlap} 1405a1d94aa5SRandy Dunlap 14061da177e4SLinus Torvalds## 14074092bac7SYacine Belkadi# Checks the section describing the return value of a function. 14084092bac7SYacine Belkadisub check_return_section { 14094092bac7SYacine Belkadi my $file = shift; 14104092bac7SYacine Belkadi my $declaration_name = shift; 14114092bac7SYacine Belkadi my $return_type = shift; 14124092bac7SYacine Belkadi 14134092bac7SYacine Belkadi # Ignore an empty return type (It's a macro) 14144092bac7SYacine Belkadi # Ignore functions with a "void" return type. (But don't ignore "void *") 14154092bac7SYacine Belkadi if (($return_type eq "") || ($return_type =~ /void\s*\w*\s*$/)) { 14164092bac7SYacine Belkadi return; 14174092bac7SYacine Belkadi } 14184092bac7SYacine Belkadi 14194092bac7SYacine Belkadi if (!defined($sections{$section_return}) || 14204092bac7SYacine Belkadi $sections{$section_return} eq "") { 1421d40e1e65SBart Van Assche print STDERR "${file}:$.: warning: " . 14224092bac7SYacine Belkadi "No description found for return value of " . 14234092bac7SYacine Belkadi "'$declaration_name'\n"; 14244092bac7SYacine Belkadi ++$warnings; 14254092bac7SYacine Belkadi } 14264092bac7SYacine Belkadi} 14274092bac7SYacine Belkadi 14284092bac7SYacine Belkadi## 14291da177e4SLinus Torvalds# takes a function prototype and the name of the current file being 14301da177e4SLinus Torvalds# processed and spits out all the details stored in the global 14311da177e4SLinus Torvalds# arrays/hashes. 14321da177e4SLinus Torvaldssub dump_function($$) { 14331da177e4SLinus Torvalds my $prototype = shift; 14341da177e4SLinus Torvalds my $file = shift; 1435cbb4d3e6SHoria Geanta my $noret = 0; 14361da177e4SLinus Torvalds 14371da177e4SLinus Torvalds $prototype =~ s/^static +//; 14381da177e4SLinus Torvalds $prototype =~ s/^extern +//; 14394dc3b16bSPavel Pisa $prototype =~ s/^asmlinkage +//; 14401da177e4SLinus Torvalds $prototype =~ s/^inline +//; 14411da177e4SLinus Torvalds $prototype =~ s/^__inline__ +//; 144232e79401SRandy Dunlap $prototype =~ s/^__inline +//; 144332e79401SRandy Dunlap $prototype =~ s/^__always_inline +//; 144432e79401SRandy Dunlap $prototype =~ s/^noinline +//; 144574fc5c65SRandy Dunlap $prototype =~ s/__init +//; 144620072205SRandy Dunlap $prototype =~ s/__init_or_module +//; 1447270a0096SRandy Dunlap $prototype =~ s/__meminit +//; 144870c95b00SRandy Dunlap $prototype =~ s/__must_check +//; 14490df7c0e3SRandy Dunlap $prototype =~ s/__weak +//; 1450cbb4d3e6SHoria Geanta my $define = $prototype =~ s/^#\s*define\s+//; #ak added 1451b1aaa546SPaolo Bonzini $prototype =~ s/__attribute__\s*\(\( 1452b1aaa546SPaolo Bonzini (?: 1453b1aaa546SPaolo Bonzini [\w\s]++ # attribute name 1454b1aaa546SPaolo Bonzini (?:\([^)]*+\))? # attribute arguments 1455b1aaa546SPaolo Bonzini \s*+,? # optional comma at the end 1456b1aaa546SPaolo Bonzini )+ 1457b1aaa546SPaolo Bonzini \)\)\s+//x; 14581da177e4SLinus Torvalds 14591da177e4SLinus Torvalds # Yes, this truly is vile. We are looking for: 14601da177e4SLinus Torvalds # 1. Return type (may be nothing if we're looking at a macro) 14611da177e4SLinus Torvalds # 2. Function name 14621da177e4SLinus Torvalds # 3. Function parameters. 14631da177e4SLinus Torvalds # 14641da177e4SLinus Torvalds # All the while we have to watch out for function pointer parameters 14651da177e4SLinus Torvalds # (which IIRC is what the two sections are for), C types (these 14661da177e4SLinus Torvalds # regexps don't even start to express all the possibilities), and 14671da177e4SLinus Torvalds # so on. 14681da177e4SLinus Torvalds # 14691da177e4SLinus Torvalds # If you mess with these regexps, it's a good idea to check that 14701da177e4SLinus Torvalds # the following functions' documentation still comes out right: 14711da177e4SLinus Torvalds # - parport_register_device (function pointer parameters) 14721da177e4SLinus Torvalds # - atomic_set (macro) 14739598f91fSMartin Waitz # - pci_match_device, __copy_to_user (long return type) 14741da177e4SLinus Torvalds 1475cbb4d3e6SHoria Geanta if ($define && $prototype =~ m/^()([a-zA-Z0-9_~:]+)\s+/) { 1476cbb4d3e6SHoria Geanta # This is an object-like macro, it has no return type and no parameter 1477cbb4d3e6SHoria Geanta # list. 1478cbb4d3e6SHoria Geanta # Function-like macros are not allowed to have spaces between 1479cbb4d3e6SHoria Geanta # declaration_name and opening parenthesis (notice the \s+). 1480cbb4d3e6SHoria Geanta $return_type = $1; 1481cbb4d3e6SHoria Geanta $declaration_name = $2; 1482cbb4d3e6SHoria Geanta $noret = 1; 1483cbb4d3e6SHoria Geanta } elsif ($prototype =~ m/^()([a-zA-Z0-9_~:]+)\s*\(([^\(]*)\)/ || 14841da177e4SLinus Torvalds $prototype =~ m/^(\w+)\s+([a-zA-Z0-9_~:]+)\s*\(([^\(]*)\)/ || 14855a0bc578SMatthew Wilcox $prototype =~ m/^(\w+\s*\*+)\s*([a-zA-Z0-9_~:]+)\s*\(([^\(]*)\)/ || 14861da177e4SLinus Torvalds $prototype =~ m/^(\w+\s+\w+)\s+([a-zA-Z0-9_~:]+)\s*\(([^\(]*)\)/ || 148794b3e03cSRandy Dunlap $prototype =~ m/^(\w+\s+\w+\s*\*+)\s*([a-zA-Z0-9_~:]+)\s*\(([^\(]*)\)/ || 14881da177e4SLinus Torvalds $prototype =~ m/^(\w+\s+\w+\s+\w+)\s+([a-zA-Z0-9_~:]+)\s*\(([^\(]*)\)/ || 14895a0bc578SMatthew Wilcox $prototype =~ m/^(\w+\s+\w+\s+\w+\s*\*+)\s*([a-zA-Z0-9_~:]+)\s*\(([^\(]*)\)/ || 14901da177e4SLinus Torvalds $prototype =~ m/^()([a-zA-Z0-9_~:]+)\s*\(([^\{]*)\)/ || 14911da177e4SLinus Torvalds $prototype =~ m/^(\w+)\s+([a-zA-Z0-9_~:]+)\s*\(([^\{]*)\)/ || 14925a0bc578SMatthew Wilcox $prototype =~ m/^(\w+\s*\*+)\s*([a-zA-Z0-9_~:]+)\s*\(([^\{]*)\)/ || 14931da177e4SLinus Torvalds $prototype =~ m/^(\w+\s+\w+)\s+([a-zA-Z0-9_~:]+)\s*\(([^\{]*)\)/ || 14945a0bc578SMatthew Wilcox $prototype =~ m/^(\w+\s+\w+\s*\*+)\s*([a-zA-Z0-9_~:]+)\s*\(([^\{]*)\)/ || 14951da177e4SLinus Torvalds $prototype =~ m/^(\w+\s+\w+\s+\w+)\s+([a-zA-Z0-9_~:]+)\s*\(([^\{]*)\)/ || 14965a0bc578SMatthew Wilcox $prototype =~ m/^(\w+\s+\w+\s+\w+\s*\*+)\s*([a-zA-Z0-9_~:]+)\s*\(([^\{]*)\)/ || 14979598f91fSMartin Waitz $prototype =~ m/^(\w+\s+\w+\s+\w+\s+\w+)\s+([a-zA-Z0-9_~:]+)\s*\(([^\{]*)\)/ || 14985a0bc578SMatthew Wilcox $prototype =~ m/^(\w+\s+\w+\s+\w+\s+\w+\s*\*+)\s*([a-zA-Z0-9_~:]+)\s*\(([^\{]*)\)/ || 14995a0bc578SMatthew Wilcox $prototype =~ m/^(\w+\s+\w+\s*\*+\s*\w+\s*\*+\s*)\s*([a-zA-Z0-9_~:]+)\s*\(([^\{]*)\)/) { 15001da177e4SLinus Torvalds $return_type = $1; 15011da177e4SLinus Torvalds $declaration_name = $2; 15021da177e4SLinus Torvalds my $args = $3; 15031da177e4SLinus Torvalds 1504*151c468bSMauro Carvalho Chehab create_parameterlist($args, ',', $file, $declaration_name); 15051da177e4SLinus Torvalds } else { 1506d40e1e65SBart Van Assche print STDERR "${file}:$.: warning: cannot understand function prototype: '$prototype'\n"; 15071da177e4SLinus Torvalds return; 15081da177e4SLinus Torvalds } 15091da177e4SLinus Torvalds 1510a1d94aa5SRandy Dunlap my $prms = join " ", @parameterlist; 15111081de2dSMauro Carvalho Chehab check_sections($file, $declaration_name, "function", $sectcheck, $prms); 1512a1d94aa5SRandy Dunlap 15134092bac7SYacine Belkadi # This check emits a lot of warnings at the moment, because many 15144092bac7SYacine Belkadi # functions don't have a 'Return' doc section. So until the number 15154092bac7SYacine Belkadi # of warnings goes sufficiently down, the check is only performed in 15164092bac7SYacine Belkadi # verbose mode. 15174092bac7SYacine Belkadi # TODO: always perform the check. 1518cbb4d3e6SHoria Geanta if ($verbose && !$noret) { 15194092bac7SYacine Belkadi check_return_section($file, $declaration_name, $return_type); 15204092bac7SYacine Belkadi } 15214092bac7SYacine Belkadi 15221da177e4SLinus Torvalds output_declaration($declaration_name, 15231da177e4SLinus Torvalds 'function', 15241da177e4SLinus Torvalds {'function' => $declaration_name, 15251da177e4SLinus Torvalds 'module' => $modulename, 15261da177e4SLinus Torvalds 'functiontype' => $return_type, 15271da177e4SLinus Torvalds 'parameterlist' => \@parameterlist, 15281da177e4SLinus Torvalds 'parameterdescs' => \%parameterdescs, 15291da177e4SLinus Torvalds 'parametertypes' => \%parametertypes, 15301da177e4SLinus Torvalds 'sectionlist' => \@sectionlist, 15311da177e4SLinus Torvalds 'sections' => \%sections, 15321da177e4SLinus Torvalds 'purpose' => $declaration_purpose 15331da177e4SLinus Torvalds }); 15341da177e4SLinus Torvalds} 15351da177e4SLinus Torvalds 15361da177e4SLinus Torvaldssub reset_state { 15371da177e4SLinus Torvalds $function = ""; 15381da177e4SLinus Torvalds %parameterdescs = (); 15391da177e4SLinus Torvalds %parametertypes = (); 15401da177e4SLinus Torvalds @parameterlist = (); 15411da177e4SLinus Torvalds %sections = (); 15421da177e4SLinus Torvalds @sectionlist = (); 1543a1d94aa5SRandy Dunlap $sectcheck = ""; 1544a1d94aa5SRandy Dunlap $struct_actual = ""; 15451da177e4SLinus Torvalds $prototype = ""; 15461da177e4SLinus Torvalds 154748af606aSJani Nikula $state = STATE_NORMAL; 154848af606aSJani Nikula $inline_doc_state = STATE_INLINE_NA; 15491da177e4SLinus Torvalds} 15501da177e4SLinus Torvalds 155156afb0f8SJason Baronsub tracepoint_munge($) { 155256afb0f8SJason Baron my $file = shift; 155356afb0f8SJason Baron my $tracepointname = 0; 155456afb0f8SJason Baron my $tracepointargs = 0; 155556afb0f8SJason Baron 155656afb0f8SJason Baron if ($prototype =~ m/TRACE_EVENT\((.*?),/) { 155756afb0f8SJason Baron $tracepointname = $1; 155856afb0f8SJason Baron } 15593a9089fdSJason Baron if ($prototype =~ m/DEFINE_SINGLE_EVENT\((.*?),/) { 15603a9089fdSJason Baron $tracepointname = $1; 15613a9089fdSJason Baron } 15623a9089fdSJason Baron if ($prototype =~ m/DEFINE_EVENT\((.*?),(.*?),/) { 15633a9089fdSJason Baron $tracepointname = $2; 15643a9089fdSJason Baron } 15653a9089fdSJason Baron $tracepointname =~ s/^\s+//; #strip leading whitespace 156656afb0f8SJason Baron if ($prototype =~ m/TP_PROTO\((.*?)\)/) { 156756afb0f8SJason Baron $tracepointargs = $1; 156856afb0f8SJason Baron } 156956afb0f8SJason Baron if (($tracepointname eq 0) || ($tracepointargs eq 0)) { 1570d40e1e65SBart Van Assche print STDERR "${file}:$.: warning: Unrecognized tracepoint format: \n". 157156afb0f8SJason Baron "$prototype\n"; 157256afb0f8SJason Baron } else { 157356afb0f8SJason Baron $prototype = "static inline void trace_$tracepointname($tracepointargs)"; 157456afb0f8SJason Baron } 157556afb0f8SJason Baron} 157656afb0f8SJason Baron 1577b4870bc5SRandy Dunlapsub syscall_munge() { 1578b4870bc5SRandy Dunlap my $void = 0; 1579b4870bc5SRandy Dunlap 15807c9aa015SMauro Carvalho Chehab $prototype =~ s@[\r\n]+@ @gos; # strip newlines/CR's 1581b4870bc5SRandy Dunlap## if ($prototype =~ m/SYSCALL_DEFINE0\s*\(\s*(a-zA-Z0-9_)*\s*\)/) { 1582b4870bc5SRandy Dunlap if ($prototype =~ m/SYSCALL_DEFINE0/) { 1583b4870bc5SRandy Dunlap $void = 1; 1584b4870bc5SRandy Dunlap## $prototype = "long sys_$1(void)"; 1585b4870bc5SRandy Dunlap } 1586b4870bc5SRandy Dunlap 1587b4870bc5SRandy Dunlap $prototype =~ s/SYSCALL_DEFINE.*\(/long sys_/; # fix return type & func name 1588b4870bc5SRandy Dunlap if ($prototype =~ m/long (sys_.*?),/) { 1589b4870bc5SRandy Dunlap $prototype =~ s/,/\(/; 1590b4870bc5SRandy Dunlap } elsif ($void) { 1591b4870bc5SRandy Dunlap $prototype =~ s/\)/\(void\)/; 1592b4870bc5SRandy Dunlap } 1593b4870bc5SRandy Dunlap 1594b4870bc5SRandy Dunlap # now delete all of the odd-number commas in $prototype 1595b4870bc5SRandy Dunlap # so that arg types & arg names don't have a comma between them 1596b4870bc5SRandy Dunlap my $count = 0; 1597b4870bc5SRandy Dunlap my $len = length($prototype); 1598b4870bc5SRandy Dunlap if ($void) { 1599b4870bc5SRandy Dunlap $len = 0; # skip the for-loop 1600b4870bc5SRandy Dunlap } 1601b4870bc5SRandy Dunlap for (my $ix = 0; $ix < $len; $ix++) { 1602b4870bc5SRandy Dunlap if (substr($prototype, $ix, 1) eq ',') { 1603b4870bc5SRandy Dunlap $count++; 1604b4870bc5SRandy Dunlap if ($count % 2 == 1) { 1605b4870bc5SRandy Dunlap substr($prototype, $ix, 1) = ' '; 1606b4870bc5SRandy Dunlap } 1607b4870bc5SRandy Dunlap } 1608b4870bc5SRandy Dunlap } 1609b4870bc5SRandy Dunlap} 1610b4870bc5SRandy Dunlap 1611b7afa92bSDaniel Vettersub process_proto_function($$) { 16121da177e4SLinus Torvalds my $x = shift; 16131da177e4SLinus Torvalds my $file = shift; 16141da177e4SLinus Torvalds 161551f5a0c8SRandy Dunlap $x =~ s@\/\/.*$@@gos; # strip C99-style comments to end of line 161651f5a0c8SRandy Dunlap 1617890c78c2SRandy Dunlap if ($x =~ m#\s*/\*\s+MACDOC\s*#io || ($x =~ /^#/ && $x !~ /^#\s*define/)) { 16181da177e4SLinus Torvalds # do nothing 16191da177e4SLinus Torvalds } 16201da177e4SLinus Torvalds elsif ($x =~ /([^\{]*)/) { 16211da177e4SLinus Torvalds $prototype .= $1; 16221da177e4SLinus Torvalds } 1623b4870bc5SRandy Dunlap 1624890c78c2SRandy Dunlap if (($x =~ /\{/) || ($x =~ /\#\s*define/) || ($x =~ /;/)) { 16251da177e4SLinus Torvalds $prototype =~ s@/\*.*?\*/@@gos; # strip comments. 16261da177e4SLinus Torvalds $prototype =~ s@[\r\n]+@ @gos; # strip newlines/cr's. 16271da177e4SLinus Torvalds $prototype =~ s@^\s+@@gos; # strip leading spaces 1628b4870bc5SRandy Dunlap if ($prototype =~ /SYSCALL_DEFINE/) { 1629b4870bc5SRandy Dunlap syscall_munge(); 1630b4870bc5SRandy Dunlap } 16313a9089fdSJason Baron if ($prototype =~ /TRACE_EVENT/ || $prototype =~ /DEFINE_EVENT/ || 16323a9089fdSJason Baron $prototype =~ /DEFINE_SINGLE_EVENT/) 16333a9089fdSJason Baron { 163456afb0f8SJason Baron tracepoint_munge($file); 163556afb0f8SJason Baron } 16361da177e4SLinus Torvalds dump_function($prototype, $file); 16371da177e4SLinus Torvalds reset_state(); 16381da177e4SLinus Torvalds } 16391da177e4SLinus Torvalds} 16401da177e4SLinus Torvalds 1641b7afa92bSDaniel Vettersub process_proto_type($$) { 16421da177e4SLinus Torvalds my $x = shift; 16431da177e4SLinus Torvalds my $file = shift; 16441da177e4SLinus Torvalds 16451da177e4SLinus Torvalds $x =~ s@[\r\n]+@ @gos; # strip newlines/cr's. 16461da177e4SLinus Torvalds $x =~ s@^\s+@@gos; # strip leading spaces 16471da177e4SLinus Torvalds $x =~ s@\s+$@@gos; # strip trailing spaces 164851f5a0c8SRandy Dunlap $x =~ s@\/\/.*$@@gos; # strip C99-style comments to end of line 164951f5a0c8SRandy Dunlap 16501da177e4SLinus Torvalds if ($x =~ /^#/) { 16511da177e4SLinus Torvalds # To distinguish preprocessor directive from regular declaration later. 16521da177e4SLinus Torvalds $x .= ";"; 16531da177e4SLinus Torvalds } 16541da177e4SLinus Torvalds 16551da177e4SLinus Torvalds while (1) { 16561da177e4SLinus Torvalds if ( $x =~ /([^{};]*)([{};])(.*)/ ) { 1657463a0fdcSMarkus Heiser if( length $prototype ) { 1658463a0fdcSMarkus Heiser $prototype .= " " 1659463a0fdcSMarkus Heiser } 16601da177e4SLinus Torvalds $prototype .= $1 . $2; 16611da177e4SLinus Torvalds ($2 eq '{') && $brcount++; 16621da177e4SLinus Torvalds ($2 eq '}') && $brcount--; 16631da177e4SLinus Torvalds if (($2 eq ';') && ($brcount == 0)) { 16641da177e4SLinus Torvalds dump_declaration($prototype, $file); 16651da177e4SLinus Torvalds reset_state(); 16661da177e4SLinus Torvalds last; 16671da177e4SLinus Torvalds } 16681da177e4SLinus Torvalds $x = $3; 16691da177e4SLinus Torvalds } else { 16701da177e4SLinus Torvalds $prototype .= $x; 16711da177e4SLinus Torvalds last; 16721da177e4SLinus Torvalds } 16731da177e4SLinus Torvalds } 16741da177e4SLinus Torvalds} 16751da177e4SLinus Torvalds 16766b5b55f6SRandy Dunlap# xml_escape: replace <, >, and & in the text stream; 16776b5b55f6SRandy Dunlap# 16786b5b55f6SRandy Dunlap# however, formatting controls that are generated internally/locally in the 16796b5b55f6SRandy Dunlap# kernel-doc script are not escaped here; instead, they begin life like 16806b5b55f6SRandy Dunlap# $blankline_html (4 of '\' followed by a mnemonic + ':'), then these strings 16816b5b55f6SRandy Dunlap# are converted to their mnemonic-expected output, without the 4 * '\' & ':', 16826b5b55f6SRandy Dunlap# just before actual output; (this is done by local_unescape()) 16831da177e4SLinus Torvaldssub xml_escape($) { 16841da177e4SLinus Torvalds my $text = shift; 1685b0514267SMauro Carvalho Chehab if ($output_mode eq "man") { 1686ecfb251aSRandy Dunlap return $text; 1687ecfb251aSRandy Dunlap } 16881da177e4SLinus Torvalds $text =~ s/\&/\\\\\\amp;/g; 16891da177e4SLinus Torvalds $text =~ s/\</\\\\\\lt;/g; 16901da177e4SLinus Torvalds $text =~ s/\>/\\\\\\gt;/g; 16911da177e4SLinus Torvalds return $text; 16921da177e4SLinus Torvalds} 16931da177e4SLinus Torvalds 1694c0d1b6eeSJonathan Corbet# xml_unescape: reverse the effects of xml_escape 1695c0d1b6eeSJonathan Corbetsub xml_unescape($) { 1696c0d1b6eeSJonathan Corbet my $text = shift; 1697b0514267SMauro Carvalho Chehab if ($output_mode eq "man") { 1698c0d1b6eeSJonathan Corbet return $text; 1699c0d1b6eeSJonathan Corbet } 1700c0d1b6eeSJonathan Corbet $text =~ s/\\\\\\amp;/\&/g; 1701c0d1b6eeSJonathan Corbet $text =~ s/\\\\\\lt;/</g; 1702c0d1b6eeSJonathan Corbet $text =~ s/\\\\\\gt;/>/g; 1703c0d1b6eeSJonathan Corbet return $text; 1704c0d1b6eeSJonathan Corbet} 1705c0d1b6eeSJonathan Corbet 17066b5b55f6SRandy Dunlap# convert local escape strings to html 17076b5b55f6SRandy Dunlap# local escape strings look like: '\\\\menmonic:' (that's 4 backslashes) 17086b5b55f6SRandy Dunlapsub local_unescape($) { 17096b5b55f6SRandy Dunlap my $text = shift; 1710b0514267SMauro Carvalho Chehab if ($output_mode eq "man") { 17116b5b55f6SRandy Dunlap return $text; 17126b5b55f6SRandy Dunlap } 17136b5b55f6SRandy Dunlap $text =~ s/\\\\\\\\lt:/</g; 17146b5b55f6SRandy Dunlap $text =~ s/\\\\\\\\gt:/>/g; 17156b5b55f6SRandy Dunlap return $text; 17166b5b55f6SRandy Dunlap} 17176b5b55f6SRandy Dunlap 17181ad560e4SJani Nikulasub map_filename($) { 17191ad560e4SJani Nikula my $file; 17201ad560e4SJani Nikula my ($orig_file) = @_; 17211ad560e4SJani Nikula 17221ad560e4SJani Nikula if (defined($ENV{'SRCTREE'})) { 17231ad560e4SJani Nikula $file = "$ENV{'SRCTREE'}" . "/" . $orig_file; 17241ad560e4SJani Nikula } else { 17251ad560e4SJani Nikula $file = $orig_file; 17261ad560e4SJani Nikula } 17271ad560e4SJani Nikula 17281ad560e4SJani Nikula if (defined($source_map{$file})) { 17291ad560e4SJani Nikula $file = $source_map{$file}; 17301ad560e4SJani Nikula } 17311ad560e4SJani Nikula 17321ad560e4SJani Nikula return $file; 17331ad560e4SJani Nikula} 17341ad560e4SJani Nikula 173588c2b57dSJani Nikulasub process_export_file($) { 173688c2b57dSJani Nikula my ($orig_file) = @_; 173788c2b57dSJani Nikula my $file = map_filename($orig_file); 173888c2b57dSJani Nikula 173988c2b57dSJani Nikula if (!open(IN,"<$file")) { 174088c2b57dSJani Nikula print STDERR "Error: Cannot open file $file\n"; 174188c2b57dSJani Nikula ++$errors; 174288c2b57dSJani Nikula return; 174388c2b57dSJani Nikula } 174488c2b57dSJani Nikula 174588c2b57dSJani Nikula while (<IN>) { 174688c2b57dSJani Nikula if (/$export_symbol/) { 174788c2b57dSJani Nikula $function_table{$2} = 1; 174888c2b57dSJani Nikula } 174988c2b57dSJani Nikula } 175088c2b57dSJani Nikula 175188c2b57dSJani Nikula close(IN); 175288c2b57dSJani Nikula} 175388c2b57dSJani Nikula 17541da177e4SLinus Torvaldssub process_file($) { 17552283a117SRandy Dunlap my $file; 17561da177e4SLinus Torvalds my $identifier; 17571da177e4SLinus Torvalds my $func; 1758a21217daSRandy Dunlap my $descr; 17596423133bSJohannes Weiner my $in_purpose = 0; 17601da177e4SLinus Torvalds my $initial_section_counter = $section_counter; 176168f86662SBen Hutchings my ($orig_file) = @_; 1762b7886de4SJani Nikula my $leading_space; 17631da177e4SLinus Torvalds 17641ad560e4SJani Nikula $file = map_filename($orig_file); 17651da177e4SLinus Torvalds 17661da177e4SLinus Torvalds if (!open(IN,"<$file")) { 17671da177e4SLinus Torvalds print STDERR "Error: Cannot open file $file\n"; 17681da177e4SLinus Torvalds ++$errors; 17691da177e4SLinus Torvalds return; 17701da177e4SLinus Torvalds } 17711da177e4SLinus Torvalds 1772a9e7314bSIlya Dryomov $. = 1; 1773a9e7314bSIlya Dryomov 17741da177e4SLinus Torvalds $section_counter = 0; 17751da177e4SLinus Torvalds while (<IN>) { 177665478428SDaniel Santos while (s/\\\s*$//) { 177765478428SDaniel Santos $_ .= <IN>; 177865478428SDaniel Santos } 17797c9aa015SMauro Carvalho Chehab # Replace tabs by spaces 17807c9aa015SMauro Carvalho Chehab while ($_ =~ s/\t+/' ' x (length($&) * 8 - length($`) % 8)/e) {}; 178148af606aSJani Nikula if ($state == STATE_NORMAL) { 17821da177e4SLinus Torvalds if (/$doc_start/o) { 178348af606aSJani Nikula $state = STATE_NAME; # next line is always the function name 1784850622dfSRandy Dunlap $in_doc_sect = 0; 17850b0f5f29SDaniel Vetter $declaration_start_line = $. + 1; 17861da177e4SLinus Torvalds } 178748af606aSJani Nikula } elsif ($state == STATE_NAME) {# this line is the function name (always) 17881da177e4SLinus Torvalds if (/$doc_block/o) { 178948af606aSJani Nikula $state = STATE_DOCBLOCK; 17901da177e4SLinus Torvalds $contents = ""; 17910b0f5f29SDaniel Vetter $new_start_line = $. + 1; 17920b0f5f29SDaniel Vetter 17931da177e4SLinus Torvalds if ( $1 eq "" ) { 17941da177e4SLinus Torvalds $section = $section_intro; 17951da177e4SLinus Torvalds } else { 17961da177e4SLinus Torvalds $section = $1; 17971da177e4SLinus Torvalds } 17981da177e4SLinus Torvalds } 17991da177e4SLinus Torvalds elsif (/$doc_decl/o) { 18001da177e4SLinus Torvalds $identifier = $1; 18011da177e4SLinus Torvalds if (/\s*([\w\s]+?)\s*-/) { 18021da177e4SLinus Torvalds $identifier = $1; 18031da177e4SLinus Torvalds } 18041da177e4SLinus Torvalds 180548af606aSJani Nikula $state = STATE_FIELD; 18060b0f5f29SDaniel Vetter # if there's no @param blocks need to set up default section 18070b0f5f29SDaniel Vetter # here 18082f4ad40aSJani Nikula $contents = ""; 18092f4ad40aSJani Nikula $section = $section_default; 18100b0f5f29SDaniel Vetter $new_start_line = $. + 1; 18111da177e4SLinus Torvalds if (/-(.*)/) { 181251f5a0c8SRandy Dunlap # strip leading/trailing/multiple spaces 1813a21217daSRandy Dunlap $descr= $1; 1814a21217daSRandy Dunlap $descr =~ s/^\s*//; 1815a21217daSRandy Dunlap $descr =~ s/\s*$//; 181612ae6779SDaniel Santos $descr =~ s/\s+/ /g; 1817a21217daSRandy Dunlap $declaration_purpose = xml_escape($descr); 18186423133bSJohannes Weiner $in_purpose = 1; 18191da177e4SLinus Torvalds } else { 18201da177e4SLinus Torvalds $declaration_purpose = ""; 18211da177e4SLinus Torvalds } 182277cc23b8SRandy Dunlap 182377cc23b8SRandy Dunlap if (($declaration_purpose eq "") && $verbose) { 1824d40e1e65SBart Van Assche print STDERR "${file}:$.: warning: missing initial short description on line:\n"; 182577cc23b8SRandy Dunlap print STDERR $_; 182677cc23b8SRandy Dunlap ++$warnings; 182777cc23b8SRandy Dunlap } 182877cc23b8SRandy Dunlap 18291da177e4SLinus Torvalds if ($identifier =~ m/^struct/) { 18301da177e4SLinus Torvalds $decl_type = 'struct'; 18311da177e4SLinus Torvalds } elsif ($identifier =~ m/^union/) { 18321da177e4SLinus Torvalds $decl_type = 'union'; 18331da177e4SLinus Torvalds } elsif ($identifier =~ m/^enum/) { 18341da177e4SLinus Torvalds $decl_type = 'enum'; 18351da177e4SLinus Torvalds } elsif ($identifier =~ m/^typedef/) { 18361da177e4SLinus Torvalds $decl_type = 'typedef'; 18371da177e4SLinus Torvalds } else { 18381da177e4SLinus Torvalds $decl_type = 'function'; 18391da177e4SLinus Torvalds } 18401da177e4SLinus Torvalds 18411da177e4SLinus Torvalds if ($verbose) { 1842d40e1e65SBart Van Assche print STDERR "${file}:$.: info: Scanning doc for $identifier\n"; 18431da177e4SLinus Torvalds } 18441da177e4SLinus Torvalds } else { 1845d40e1e65SBart Van Assche print STDERR "${file}:$.: warning: Cannot understand $_ on line $.", 18461da177e4SLinus Torvalds " - I thought it was a doc line\n"; 18471da177e4SLinus Torvalds ++$warnings; 184848af606aSJani Nikula $state = STATE_NORMAL; 18491da177e4SLinus Torvalds } 185048af606aSJani Nikula } elsif ($state == STATE_FIELD) { # look for head: lines, and include content 1851f624adefSJani Nikula if (/$doc_sect/i) { # case insensitive for supported section names 18521da177e4SLinus Torvalds $newsection = $1; 18531da177e4SLinus Torvalds $newcontents = $2; 18541da177e4SLinus Torvalds 1855f624adefSJani Nikula # map the supported section names to the canonical names 1856f624adefSJani Nikula if ($newsection =~ m/^description$/i) { 1857f624adefSJani Nikula $newsection = $section_default; 1858f624adefSJani Nikula } elsif ($newsection =~ m/^context$/i) { 1859f624adefSJani Nikula $newsection = $section_context; 1860f624adefSJani Nikula } elsif ($newsection =~ m/^returns?$/i) { 1861f624adefSJani Nikula $newsection = $section_return; 1862f624adefSJani Nikula } elsif ($newsection =~ m/^\@return$/) { 1863f624adefSJani Nikula # special: @return is a section, not a param description 1864f624adefSJani Nikula $newsection = $section_return; 1865f624adefSJani Nikula } 1866f624adefSJani Nikula 1867792aa2f2SRandy Dunlap if (($contents ne "") && ($contents ne "\n")) { 1868850622dfSRandy Dunlap if (!$in_doc_sect && $verbose) { 1869d40e1e65SBart Van Assche print STDERR "${file}:$.: warning: contents before sections\n"; 1870850622dfSRandy Dunlap ++$warnings; 1871850622dfSRandy Dunlap } 187294dc7ad5SRandy Dunlap dump_section($file, $section, xml_escape($contents)); 18731da177e4SLinus Torvalds $section = $section_default; 18741da177e4SLinus Torvalds } 18751da177e4SLinus Torvalds 1876850622dfSRandy Dunlap $in_doc_sect = 1; 18776423133bSJohannes Weiner $in_purpose = 0; 18781da177e4SLinus Torvalds $contents = $newcontents; 18790b0f5f29SDaniel Vetter $new_start_line = $.; 18807c9aa015SMauro Carvalho Chehab while (substr($contents, 0, 1) eq " ") { 188105189497SRandy Dunlap $contents = substr($contents, 1); 188205189497SRandy Dunlap } 18830a726301SJani Nikula if ($contents ne "") { 18841da177e4SLinus Torvalds $contents .= "\n"; 18851da177e4SLinus Torvalds } 18861da177e4SLinus Torvalds $section = $newsection; 1887b7886de4SJani Nikula $leading_space = undef; 18881da177e4SLinus Torvalds } elsif (/$doc_end/) { 18894c98ecafSRandy Dunlap if (($contents ne "") && ($contents ne "\n")) { 189094dc7ad5SRandy Dunlap dump_section($file, $section, xml_escape($contents)); 18911da177e4SLinus Torvalds $section = $section_default; 18921da177e4SLinus Torvalds $contents = ""; 18931da177e4SLinus Torvalds } 189446b958ebSRandy Dunlap # look for doc_com + <text> + doc_end: 189546b958ebSRandy Dunlap if ($_ =~ m'\s*\*\s*[a-zA-Z_0-9:\.]+\*/') { 1896d40e1e65SBart Van Assche print STDERR "${file}:$.: warning: suspicious ending line: $_"; 189746b958ebSRandy Dunlap ++$warnings; 189846b958ebSRandy Dunlap } 18991da177e4SLinus Torvalds 19001da177e4SLinus Torvalds $prototype = ""; 190148af606aSJani Nikula $state = STATE_PROTO; 19021da177e4SLinus Torvalds $brcount = 0; 19031da177e4SLinus Torvalds# print STDERR "end of doc comment, looking for prototype\n"; 19041da177e4SLinus Torvalds } elsif (/$doc_content/) { 19051da177e4SLinus Torvalds # miguel-style comment kludge, look for blank lines after 19061da177e4SLinus Torvalds # @parameter line to signify start of description 19076423133bSJohannes Weiner if ($1 eq "") { 19086423133bSJohannes Weiner if ($section =~ m/^@/ || $section eq $section_context) { 190994dc7ad5SRandy Dunlap dump_section($file, $section, xml_escape($contents)); 19101da177e4SLinus Torvalds $section = $section_default; 19111da177e4SLinus Torvalds $contents = ""; 19120b0f5f29SDaniel Vetter $new_start_line = $.; 19131da177e4SLinus Torvalds } else { 19146423133bSJohannes Weiner $contents .= "\n"; 19156423133bSJohannes Weiner } 19166423133bSJohannes Weiner $in_purpose = 0; 19176423133bSJohannes Weiner } elsif ($in_purpose == 1) { 19186423133bSJohannes Weiner # Continued declaration purpose 19196423133bSJohannes Weiner chomp($declaration_purpose); 19206423133bSJohannes Weiner $declaration_purpose .= " " . xml_escape($1); 192112ae6779SDaniel Santos $declaration_purpose =~ s/\s+/ /g; 19226423133bSJohannes Weiner } else { 1923b7886de4SJani Nikula my $cont = $1; 1924b7886de4SJani Nikula if ($section =~ m/^@/ || $section eq $section_context) { 1925b7886de4SJani Nikula if (!defined $leading_space) { 1926b7886de4SJani Nikula if ($cont =~ m/^(\s+)/) { 1927b7886de4SJani Nikula $leading_space = $1; 1928b7886de4SJani Nikula } else { 1929b7886de4SJani Nikula $leading_space = ""; 1930b7886de4SJani Nikula } 1931b7886de4SJani Nikula } 1932b7886de4SJani Nikula 1933b7886de4SJani Nikula $cont =~ s/^$leading_space//; 1934b7886de4SJani Nikula } 1935b7886de4SJani Nikula $contents .= $cont . "\n"; 19361da177e4SLinus Torvalds } 19371da177e4SLinus Torvalds } else { 19381da177e4SLinus Torvalds # i dont know - bad line? ignore. 1939d40e1e65SBart Van Assche print STDERR "${file}:$.: warning: bad line: $_"; 19401da177e4SLinus Torvalds ++$warnings; 19411da177e4SLinus Torvalds } 194248af606aSJani Nikula } elsif ($state == STATE_INLINE) { # scanning for inline parameters 1943a4c6ebedSDanilo Cesar Lemes de Paula # First line (state 1) needs to be a @parameter 194448af606aSJani Nikula if ($inline_doc_state == STATE_INLINE_NAME && /$doc_inline_sect/o) { 1945a4c6ebedSDanilo Cesar Lemes de Paula $section = $1; 1946a4c6ebedSDanilo Cesar Lemes de Paula $contents = $2; 19470b0f5f29SDaniel Vetter $new_start_line = $.; 1948a4c6ebedSDanilo Cesar Lemes de Paula if ($contents ne "") { 19497c9aa015SMauro Carvalho Chehab while (substr($contents, 0, 1) eq " ") { 1950a4c6ebedSDanilo Cesar Lemes de Paula $contents = substr($contents, 1); 1951a4c6ebedSDanilo Cesar Lemes de Paula } 1952a4c6ebedSDanilo Cesar Lemes de Paula $contents .= "\n"; 1953a4c6ebedSDanilo Cesar Lemes de Paula } 195448af606aSJani Nikula $inline_doc_state = STATE_INLINE_TEXT; 1955a4c6ebedSDanilo Cesar Lemes de Paula # Documentation block end */ 195648af606aSJani Nikula } elsif (/$doc_inline_end/) { 1957a4c6ebedSDanilo Cesar Lemes de Paula if (($contents ne "") && ($contents ne "\n")) { 1958a4c6ebedSDanilo Cesar Lemes de Paula dump_section($file, $section, xml_escape($contents)); 1959a4c6ebedSDanilo Cesar Lemes de Paula $section = $section_default; 1960a4c6ebedSDanilo Cesar Lemes de Paula $contents = ""; 1961a4c6ebedSDanilo Cesar Lemes de Paula } 196248af606aSJani Nikula $state = STATE_PROTO; 196348af606aSJani Nikula $inline_doc_state = STATE_INLINE_NA; 1964a4c6ebedSDanilo Cesar Lemes de Paula # Regular text 1965a4c6ebedSDanilo Cesar Lemes de Paula } elsif (/$doc_content/) { 196648af606aSJani Nikula if ($inline_doc_state == STATE_INLINE_TEXT) { 1967a4c6ebedSDanilo Cesar Lemes de Paula $contents .= $1 . "\n"; 19686450c895SJani Nikula # nuke leading blank lines 19696450c895SJani Nikula if ($contents =~ /^\s*$/) { 19706450c895SJani Nikula $contents = ""; 19716450c895SJani Nikula } 197248af606aSJani Nikula } elsif ($inline_doc_state == STATE_INLINE_NAME) { 197348af606aSJani Nikula $inline_doc_state = STATE_INLINE_ERROR; 1974e7ca311eSDaniel Vetter print STDERR "${file}:$.: warning: "; 1975a4c6ebedSDanilo Cesar Lemes de Paula print STDERR "Incorrect use of kernel-doc format: $_"; 1976a4c6ebedSDanilo Cesar Lemes de Paula ++$warnings; 1977a4c6ebedSDanilo Cesar Lemes de Paula } 1978a4c6ebedSDanilo Cesar Lemes de Paula } 197948af606aSJani Nikula } elsif ($state == STATE_PROTO) { # scanning for function '{' (end of prototype) 19800c9aa209SJani Nikula if (/$doc_inline_oneline/) { 19810c9aa209SJani Nikula $section = $1; 19820c9aa209SJani Nikula $contents = $2; 19830c9aa209SJani Nikula if ($contents ne "") { 19840c9aa209SJani Nikula $contents .= "\n"; 19850c9aa209SJani Nikula dump_section($file, $section, xml_escape($contents)); 19860c9aa209SJani Nikula $section = $section_default; 19870c9aa209SJani Nikula $contents = ""; 19880c9aa209SJani Nikula } 19890c9aa209SJani Nikula } elsif (/$doc_inline_start/) { 199048af606aSJani Nikula $state = STATE_INLINE; 199148af606aSJani Nikula $inline_doc_state = STATE_INLINE_NAME; 1992a4c6ebedSDanilo Cesar Lemes de Paula } elsif ($decl_type eq 'function') { 1993b7afa92bSDaniel Vetter process_proto_function($_, $file); 19941da177e4SLinus Torvalds } else { 1995b7afa92bSDaniel Vetter process_proto_type($_, $file); 19961da177e4SLinus Torvalds } 199748af606aSJani Nikula } elsif ($state == STATE_DOCBLOCK) { 1998ebff7f92SDaniel Vetter if (/$doc_end/) 19991da177e4SLinus Torvalds { 200094dc7ad5SRandy Dunlap dump_doc_section($file, $section, xml_escape($contents)); 20012f4ad40aSJani Nikula $section = $section_default; 20021da177e4SLinus Torvalds $contents = ""; 20031da177e4SLinus Torvalds $function = ""; 20041da177e4SLinus Torvalds %parameterdescs = (); 20051da177e4SLinus Torvalds %parametertypes = (); 20061da177e4SLinus Torvalds @parameterlist = (); 20071da177e4SLinus Torvalds %sections = (); 20081da177e4SLinus Torvalds @sectionlist = (); 20091da177e4SLinus Torvalds $prototype = ""; 201048af606aSJani Nikula $state = STATE_NORMAL; 20111da177e4SLinus Torvalds } 20121da177e4SLinus Torvalds elsif (/$doc_content/) 20131da177e4SLinus Torvalds { 20141da177e4SLinus Torvalds if ( $1 eq "" ) 20151da177e4SLinus Torvalds { 20161da177e4SLinus Torvalds $contents .= $blankline; 20171da177e4SLinus Torvalds } 20181da177e4SLinus Torvalds else 20191da177e4SLinus Torvalds { 20201da177e4SLinus Torvalds $contents .= $1 . "\n"; 20211da177e4SLinus Torvalds } 20221da177e4SLinus Torvalds } 20231da177e4SLinus Torvalds } 20241da177e4SLinus Torvalds } 20251da177e4SLinus Torvalds if ($initial_section_counter == $section_counter) { 20263a025e1dSMatthew Wilcox if ($output_mode ne "none") { 2027d40e1e65SBart Van Assche print STDERR "${file}:1: warning: no structured comments found\n"; 20283a025e1dSMatthew Wilcox } 2029b6c3f456SJani Nikula if (($output_selection == OUTPUT_INCLUDE) && ($show_not_found == 1)) { 2030e946c43aSJohannes Berg print STDERR " Was looking for '$_'.\n" for keys %function_table; 2031e946c43aSJohannes Berg } 20321da177e4SLinus Torvalds } 20331da177e4SLinus Torvalds} 20348484baaaSRandy Dunlap 20358484baaaSRandy Dunlap 20368484baaaSRandy Dunlap$kernelversion = get_kernel_version(); 20378484baaaSRandy Dunlap 20388484baaaSRandy Dunlap# generate a sequence of code that will splice in highlighting information 20398484baaaSRandy Dunlap# using the s// operator. 20401ef06233SMauro Carvalho Chehabfor (my $k = 0; $k < @highlights; $k++) { 20414d732701SDanilo Cesar Lemes de Paula my $pattern = $highlights[$k][0]; 20424d732701SDanilo Cesar Lemes de Paula my $result = $highlights[$k][1]; 20434d732701SDanilo Cesar Lemes de Paula# print STDERR "scanning pattern:$pattern, highlight:($result)\n"; 20444d732701SDanilo Cesar Lemes de Paula $dohighlight .= "\$contents =~ s:$pattern:$result:gs;\n"; 20458484baaaSRandy Dunlap} 20468484baaaSRandy Dunlap 20478484baaaSRandy Dunlap# Read the file that maps relative names to absolute names for 20488484baaaSRandy Dunlap# separate source and object directories and for shadow trees. 20498484baaaSRandy Dunlapif (open(SOURCE_MAP, "<.tmp_filelist.txt")) { 20508484baaaSRandy Dunlap my ($relname, $absname); 20518484baaaSRandy Dunlap while(<SOURCE_MAP>) { 20528484baaaSRandy Dunlap chop(); 20538484baaaSRandy Dunlap ($relname, $absname) = (split())[0..1]; 20548484baaaSRandy Dunlap $relname =~ s:^/+::; 20558484baaaSRandy Dunlap $source_map{$relname} = $absname; 20568484baaaSRandy Dunlap } 20578484baaaSRandy Dunlap close(SOURCE_MAP); 20588484baaaSRandy Dunlap} 20598484baaaSRandy Dunlap 206088c2b57dSJani Nikulaif ($output_selection == OUTPUT_EXPORTED || 206188c2b57dSJani Nikula $output_selection == OUTPUT_INTERNAL) { 2062c9b2cfb3SJani Nikula 2063c9b2cfb3SJani Nikula push(@export_file_list, @ARGV); 2064c9b2cfb3SJani Nikula 206588c2b57dSJani Nikula foreach (@export_file_list) { 206688c2b57dSJani Nikula chomp; 206788c2b57dSJani Nikula process_export_file($_); 206888c2b57dSJani Nikula } 206988c2b57dSJani Nikula} 207088c2b57dSJani Nikula 20718484baaaSRandy Dunlapforeach (@ARGV) { 20728484baaaSRandy Dunlap chomp; 20738484baaaSRandy Dunlap process_file($_); 20748484baaaSRandy Dunlap} 20758484baaaSRandy Dunlapif ($verbose && $errors) { 20768484baaaSRandy Dunlap print STDERR "$errors errors\n"; 20778484baaaSRandy Dunlap} 20788484baaaSRandy Dunlapif ($verbose && $warnings) { 20798484baaaSRandy Dunlap print STDERR "$warnings warnings\n"; 20808484baaaSRandy Dunlap} 20818484baaaSRandy Dunlap 2082e814bccbSWill Deaconexit($output_mode eq "none" ? 0 : $errors); 2083