2
0

kernel-doc 61 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240
  1. #!/usr/bin/env perl
  2. # SPDX-License-Identifier: GPL-2.0
  3. use warnings;
  4. use strict;
  5. ## Copyright (c) 1998 Michael Zucchi, All Rights Reserved ##
  6. ## Copyright (C) 2000, 1 Tim Waugh <twaugh@redhat.com> ##
  7. ## Copyright (C) 2001 Simon Huggins ##
  8. ## Copyright (C) 2005-2012 Randy Dunlap ##
  9. ## Copyright (C) 2012 Dan Luedtke ##
  10. ## ##
  11. ## #define enhancements by Armin Kuster <akuster@mvista.com> ##
  12. ## Copyright (c) 2000 MontaVista Software, Inc. ##
  13. ## ##
  14. ## This software falls under the GNU General Public License. ##
  15. ## Please read the COPYING file for more information ##
  16. # 18/01/2001 - Cleanups
  17. # Functions prototyped as foo(void) same as foo()
  18. # Stop eval'ing where we don't need to.
  19. # -- huggie@earth.li
  20. # 27/06/2001 - Allowed whitespace after initial "/**" and
  21. # allowed comments before function declarations.
  22. # -- Christian Kreibich <ck@whoop.org>
  23. # Still to do:
  24. # - add perldoc documentation
  25. # - Look more closely at some of the scarier bits :)
  26. # 26/05/2001 - Support for separate source and object trees.
  27. # Return error code.
  28. # Keith Owens <kaos@ocs.com.au>
  29. # 23/09/2001 - Added support for typedefs, structs, enums and unions
  30. # Support for Context section; can be terminated using empty line
  31. # Small fixes (like spaces vs. \s in regex)
  32. # -- Tim Jansen <tim@tjansen.de>
  33. # 25/07/2012 - Added support for HTML5
  34. # -- Dan Luedtke <mail@danrl.de>
  35. sub usage {
  36. my $message = <<"EOF";
  37. Usage: $0 [OPTION ...] FILE ...
  38. Read C language source or header FILEs, extract embedded documentation comments,
  39. and print formatted documentation to standard output.
  40. The documentation comments are identified by "/**" opening comment mark. See
  41. Documentation/doc-guide/kernel-doc.rst for the documentation comment syntax.
  42. Output format selection (mutually exclusive):
  43. -man Output troff manual page format. This is the default.
  44. -rst Output reStructuredText format.
  45. -none Do not output documentation, only warnings.
  46. Output selection (mutually exclusive):
  47. -export Only output documentation for symbols that have been
  48. exported using EXPORT_SYMBOL() or EXPORT_SYMBOL_GPL()
  49. in any input FILE or -export-file FILE.
  50. -internal Only output documentation for symbols that have NOT been
  51. exported using EXPORT_SYMBOL() or EXPORT_SYMBOL_GPL()
  52. in any input FILE or -export-file FILE.
  53. -function NAME Only output documentation for the given function(s)
  54. or DOC: section title(s). All other functions and DOC:
  55. sections are ignored. May be specified multiple times.
  56. -nofunction NAME Do NOT output documentation for the given function(s);
  57. only output documentation for the other functions and
  58. DOC: sections. May be specified multiple times.
  59. Output selection modifiers:
  60. -sphinx-version VER Generate rST syntax for the specified Sphinx version.
  61. Only works with reStructuredTextFormat.
  62. -no-doc-sections Do not output DOC: sections.
  63. -enable-lineno Enable output of #define LINENO lines. Only works with
  64. reStructuredText format.
  65. -export-file FILE Specify an additional FILE in which to look for
  66. EXPORT_SYMBOL() and EXPORT_SYMBOL_GPL(). To be used with
  67. -export or -internal. May be specified multiple times.
  68. Other parameters:
  69. -v Verbose output, more warnings and other information.
  70. -h Print this help.
  71. EOF
  72. print $message;
  73. exit 1;
  74. }
  75. #
  76. # format of comments.
  77. # In the following table, (...)? signifies optional structure.
  78. # (...)* signifies 0 or more structure elements
  79. # /**
  80. # * function_name(:)? (- short description)?
  81. # (* @parameterx: (description of parameter x)?)*
  82. # (* a blank line)?
  83. # * (Description:)? (Description of function)?
  84. # * (section header: (section description)? )*
  85. # (*)?*/
  86. #
  87. # So .. the trivial example would be:
  88. #
  89. # /**
  90. # * my_function
  91. # */
  92. #
  93. # If the Description: header tag is omitted, then there must be a blank line
  94. # after the last parameter specification.
  95. # e.g.
  96. # /**
  97. # * my_function - does my stuff
  98. # * @my_arg: its mine damnit
  99. # *
  100. # * Does my stuff explained.
  101. # */
  102. #
  103. # or, could also use:
  104. # /**
  105. # * my_function - does my stuff
  106. # * @my_arg: its mine damnit
  107. # * Description: Does my stuff explained.
  108. # */
  109. # etc.
  110. #
  111. # Besides functions you can also write documentation for structs, unions,
  112. # enums and typedefs. Instead of the function name you must write the name
  113. # of the declaration; the struct/union/enum/typedef must always precede
  114. # the name. Nesting of declarations is not supported.
  115. # Use the argument mechanism to document members or constants.
  116. # e.g.
  117. # /**
  118. # * struct my_struct - short description
  119. # * @a: first member
  120. # * @b: second member
  121. # *
  122. # * Longer description
  123. # */
  124. # struct my_struct {
  125. # int a;
  126. # int b;
  127. # /* private: */
  128. # int c;
  129. # };
  130. #
  131. # All descriptions can be multiline, except the short function description.
  132. #
  133. # For really longs structs, you can also describe arguments inside the
  134. # body of the struct.
  135. # eg.
  136. # /**
  137. # * struct my_struct - short description
  138. # * @a: first member
  139. # * @b: second member
  140. # *
  141. # * Longer description
  142. # */
  143. # struct my_struct {
  144. # int a;
  145. # int b;
  146. # /**
  147. # * @c: This is longer description of C
  148. # *
  149. # * You can use paragraphs to describe arguments
  150. # * using this method.
  151. # */
  152. # int c;
  153. # };
  154. #
  155. # This should be use only for struct/enum members.
  156. #
  157. # You can also add additional sections. When documenting kernel functions you
  158. # should document the "Context:" of the function, e.g. whether the functions
  159. # can be called form interrupts. Unlike other sections you can end it with an
  160. # empty line.
  161. # A non-void function should have a "Return:" section describing the return
  162. # value(s).
  163. # Example-sections should contain the string EXAMPLE so that they are marked
  164. # appropriately in DocBook.
  165. #
  166. # Example:
  167. # /**
  168. # * user_function - function that can only be called in user context
  169. # * @a: some argument
  170. # * Context: !in_interrupt()
  171. # *
  172. # * Some description
  173. # * Example:
  174. # * user_function(22);
  175. # */
  176. # ...
  177. #
  178. #
  179. # All descriptive text is further processed, scanning for the following special
  180. # patterns, which are highlighted appropriately.
  181. #
  182. # 'funcname()' - function
  183. # '$ENVVAR' - environmental variable
  184. # '&struct_name' - name of a structure (up to two words including 'struct')
  185. # '&struct_name.member' - name of a structure member
  186. # '@parameter' - name of a parameter
  187. # '%CONST' - name of a constant.
  188. # '``LITERAL``' - literal string without any spaces on it.
  189. ## init lots of data
  190. my $errors = 0;
  191. my $warnings = 0;
  192. my $anon_struct_union = 0;
  193. # match expressions used to find embedded type information
  194. my $type_constant = '\b``([^\`]+)``\b';
  195. my $type_constant2 = '\%([-_\w]+)';
  196. my $type_func = '(\w+)\(\)';
  197. my $type_param = '\@(\w*((\.\w+)|(->\w+))*(\.\.\.)?)';
  198. my $type_fp_param = '\@(\w+)\(\)'; # Special RST handling for func ptr params
  199. my $type_env = '(\$\w+)';
  200. my $type_enum = '#(enum\s*([_\w]+))';
  201. my $type_struct = '#(struct\s*([_\w]+))';
  202. my $type_typedef = '#(([A-Z][_\w]*))';
  203. my $type_union = '#(union\s*([_\w]+))';
  204. my $type_member = '#([_\w]+)(\.|->)([_\w]+)';
  205. my $type_fallback = '(?!)'; # this never matches
  206. my $type_member_func = $type_member . '\(\)';
  207. # Output conversion substitutions.
  208. # One for each output format
  209. # these are pretty rough
  210. my @highlights_man = (
  211. [$type_constant, "\$1"],
  212. [$type_constant2, "\$1"],
  213. [$type_func, "\\\\fB\$1\\\\fP"],
  214. [$type_enum, "\\\\fI\$1\\\\fP"],
  215. [$type_struct, "\\\\fI\$1\\\\fP"],
  216. [$type_typedef, "\\\\fI\$1\\\\fP"],
  217. [$type_union, "\\\\fI\$1\\\\fP"],
  218. [$type_param, "\\\\fI\$1\\\\fP"],
  219. [$type_member, "\\\\fI\$1\$2\$3\\\\fP"],
  220. [$type_fallback, "\\\\fI\$1\\\\fP"]
  221. );
  222. my $blankline_man = "";
  223. # rst-mode
  224. my @highlights_rst = (
  225. [$type_constant, "``\$1``"],
  226. [$type_constant2, "``\$1``"],
  227. # Note: need to escape () to avoid func matching later
  228. [$type_member_func, "\\:c\\:type\\:`\$1\$2\$3\\\\(\\\\) <\$1>`"],
  229. [$type_member, "\\:c\\:type\\:`\$1\$2\$3 <\$1>`"],
  230. [$type_fp_param, "**\$1\\\\(\\\\)**"],
  231. [$type_func, "\$1()"],
  232. [$type_enum, "\\:c\\:type\\:`\$1 <\$2>`"],
  233. [$type_struct, "\\:c\\:type\\:`\$1 <\$2>`"],
  234. [$type_typedef, "\\:c\\:type\\:`\$1 <\$2>`"],
  235. [$type_union, "\\:c\\:type\\:`\$1 <\$2>`"],
  236. # in rst this can refer to any type
  237. [$type_fallback, "\\:c\\:type\\:`\$1`"],
  238. [$type_param, "**\$1**"]
  239. );
  240. my $blankline_rst = "\n";
  241. # read arguments
  242. if ($#ARGV == -1) {
  243. usage();
  244. }
  245. my $kernelversion;
  246. my $dohighlight = "";
  247. my $verbose = 0;
  248. my $output_mode = "rst";
  249. my $output_preformatted = 0;
  250. my $no_doc_sections = 0;
  251. my $enable_lineno = 0;
  252. my @highlights = @highlights_rst;
  253. my $blankline = $blankline_rst;
  254. my $modulename = "Kernel API";
  255. use constant {
  256. OUTPUT_ALL => 0, # output all symbols and doc sections
  257. OUTPUT_INCLUDE => 1, # output only specified symbols
  258. OUTPUT_EXCLUDE => 2, # output everything except specified symbols
  259. OUTPUT_EXPORTED => 3, # output exported symbols
  260. OUTPUT_INTERNAL => 4, # output non-exported symbols
  261. };
  262. my $output_selection = OUTPUT_ALL;
  263. my $show_not_found = 0; # No longer used
  264. my $sphinx_version = "0.0"; # if not specified, assume old
  265. my @export_file_list;
  266. my @build_time;
  267. if (defined($ENV{'KBUILD_BUILD_TIMESTAMP'}) &&
  268. (my $seconds = `date -d"${ENV{'KBUILD_BUILD_TIMESTAMP'}}" +%s`) ne '') {
  269. @build_time = gmtime($seconds);
  270. } else {
  271. @build_time = localtime;
  272. }
  273. my $man_date = ('January', 'February', 'March', 'April', 'May', 'June',
  274. 'July', 'August', 'September', 'October',
  275. 'November', 'December')[$build_time[4]] .
  276. " " . ($build_time[5]+1900);
  277. # Essentially these are globals.
  278. # They probably want to be tidied up, made more localised or something.
  279. # CAVEAT EMPTOR! Some of the others I localised may not want to be, which
  280. # could cause "use of undefined value" or other bugs.
  281. my ($function, %function_table, %parametertypes, $declaration_purpose);
  282. my $declaration_start_line;
  283. my ($type, $declaration_name, $return_type);
  284. my ($newsection, $newcontents, $prototype, $brcount, %source_map);
  285. if (defined($ENV{'KBUILD_VERBOSE'})) {
  286. $verbose = "$ENV{'KBUILD_VERBOSE'}";
  287. }
  288. # Generated docbook code is inserted in a template at a point where
  289. # docbook v3.1 requires a non-zero sequence of RefEntry's; see:
  290. # http://www.oasis-open.org/docbook/documentation/reference/html/refentry.html
  291. # We keep track of number of generated entries and generate a dummy
  292. # if needs be to ensure the expanded template can be postprocessed
  293. # into html.
  294. my $section_counter = 0;
  295. my $lineprefix="";
  296. # Parser states
  297. use constant {
  298. STATE_NORMAL => 0, # normal code
  299. STATE_NAME => 1, # looking for function name
  300. STATE_BODY_MAYBE => 2, # body - or maybe more description
  301. STATE_BODY => 3, # the body of the comment
  302. STATE_PROTO => 4, # scanning prototype
  303. STATE_DOCBLOCK => 5, # documentation block
  304. STATE_INLINE => 6, # gathering documentation outside main block
  305. };
  306. my $state;
  307. my $in_doc_sect;
  308. my $leading_space;
  309. # Inline documentation state
  310. use constant {
  311. STATE_INLINE_NA => 0, # not applicable ($state != STATE_INLINE)
  312. STATE_INLINE_NAME => 1, # looking for member name (@foo:)
  313. STATE_INLINE_TEXT => 2, # looking for member documentation
  314. STATE_INLINE_END => 3, # done
  315. STATE_INLINE_ERROR => 4, # error - Comment without header was found.
  316. # Spit a warning as it's not
  317. # proper kernel-doc and ignore the rest.
  318. };
  319. my $inline_doc_state;
  320. #declaration types: can be
  321. # 'function', 'struct', 'union', 'enum', 'typedef'
  322. my $decl_type;
  323. my $doc_start = '^/\*\*\s*$'; # Allow whitespace at end of comment start.
  324. my $doc_end = '\*/';
  325. my $doc_com = '\s*\*\s*';
  326. my $doc_com_body = '\s*\* ?';
  327. my $doc_decl = $doc_com . '(\w+)';
  328. # @params and a strictly limited set of supported section names
  329. my $doc_sect = $doc_com .
  330. '\s*(\@[.\w]+|\@\.\.\.|description|context|returns?|notes?|examples?)\s*:(.*)';
  331. my $doc_content = $doc_com_body . '(.*)';
  332. my $doc_block = $doc_com . 'DOC:\s*(.*)?';
  333. my $doc_inline_start = '^\s*/\*\*\s*$';
  334. my $doc_inline_sect = '\s*\*\s*(@\s*[\w][\w\.]*\s*):(.*)';
  335. my $doc_inline_end = '^\s*\*/\s*$';
  336. my $doc_inline_oneline = '^\s*/\*\*\s*(@[\w\s]+):\s*(.*)\s*\*/\s*$';
  337. my $export_symbol = '^\s*EXPORT_SYMBOL(_GPL)?\s*\(\s*(\w+)\s*\)\s*;';
  338. my %parameterdescs;
  339. my %parameterdesc_start_lines;
  340. my @parameterlist;
  341. my %sections;
  342. my @sectionlist;
  343. my %section_start_lines;
  344. my $sectcheck;
  345. my $struct_actual;
  346. my $contents = "";
  347. my $new_start_line = 0;
  348. # the canonical section names. see also $doc_sect above.
  349. my $section_default = "Description"; # default section
  350. my $section_intro = "Introduction";
  351. my $section = $section_default;
  352. my $section_context = "Context";
  353. my $section_return = "Return";
  354. my $undescribed = "-- undescribed --";
  355. reset_state();
  356. while ($ARGV[0] =~ m/^--?(.*)/) {
  357. my $cmd = $1;
  358. shift @ARGV;
  359. if ($cmd eq "man") {
  360. $output_mode = "man";
  361. @highlights = @highlights_man;
  362. $blankline = $blankline_man;
  363. } elsif ($cmd eq "rst") {
  364. $output_mode = "rst";
  365. @highlights = @highlights_rst;
  366. $blankline = $blankline_rst;
  367. } elsif ($cmd eq "none") {
  368. $output_mode = "none";
  369. } elsif ($cmd eq "module") { # not needed for XML, inherits from calling document
  370. $modulename = shift @ARGV;
  371. } elsif ($cmd eq "function") { # to only output specific functions
  372. $output_selection = OUTPUT_INCLUDE;
  373. $function = shift @ARGV;
  374. $function_table{$function} = 1;
  375. } elsif ($cmd eq "nofunction") { # output all except specific functions
  376. $output_selection = OUTPUT_EXCLUDE;
  377. $function = shift @ARGV;
  378. $function_table{$function} = 1;
  379. } elsif ($cmd eq "export") { # only exported symbols
  380. $output_selection = OUTPUT_EXPORTED;
  381. %function_table = ();
  382. } elsif ($cmd eq "internal") { # only non-exported symbols
  383. $output_selection = OUTPUT_INTERNAL;
  384. %function_table = ();
  385. } elsif ($cmd eq "export-file") {
  386. my $file = shift @ARGV;
  387. push(@export_file_list, $file);
  388. } elsif ($cmd eq "v") {
  389. $verbose = 1;
  390. } elsif (($cmd eq "h") || ($cmd eq "help")) {
  391. usage();
  392. } elsif ($cmd eq 'no-doc-sections') {
  393. $no_doc_sections = 1;
  394. } elsif ($cmd eq 'enable-lineno') {
  395. $enable_lineno = 1;
  396. } elsif ($cmd eq 'show-not-found') {
  397. $show_not_found = 1; # A no-op but don't fail
  398. } elsif ($cmd eq 'sphinx-version') {
  399. $sphinx_version = shift @ARGV;
  400. } else {
  401. # Unknown argument
  402. usage();
  403. }
  404. }
  405. # continue execution near EOF;
  406. # get kernel version from env
  407. sub get_kernel_version() {
  408. my $version = 'unknown kernel version';
  409. if (defined($ENV{'KERNELVERSION'})) {
  410. $version = $ENV{'KERNELVERSION'};
  411. }
  412. return $version;
  413. }
  414. #
  415. sub print_lineno {
  416. my $lineno = shift;
  417. if ($enable_lineno && defined($lineno)) {
  418. print "#define LINENO " . $lineno . "\n";
  419. }
  420. }
  421. ##
  422. # dumps section contents to arrays/hashes intended for that purpose.
  423. #
  424. sub dump_section {
  425. my $file = shift;
  426. my $name = shift;
  427. my $contents = join "\n", @_;
  428. if ($name =~ m/$type_param/) {
  429. $name = $1;
  430. $parameterdescs{$name} = $contents;
  431. $sectcheck = $sectcheck . $name . " ";
  432. $parameterdesc_start_lines{$name} = $new_start_line;
  433. $new_start_line = 0;
  434. } elsif ($name eq "@\.\.\.") {
  435. $name = "...";
  436. $parameterdescs{$name} = $contents;
  437. $sectcheck = $sectcheck . $name . " ";
  438. $parameterdesc_start_lines{$name} = $new_start_line;
  439. $new_start_line = 0;
  440. } else {
  441. if (defined($sections{$name}) && ($sections{$name} ne "")) {
  442. # Only warn on user specified duplicate section names.
  443. if ($name ne $section_default) {
  444. print STDERR "${file}:$.: warning: duplicate section name '$name'\n";
  445. ++$warnings;
  446. }
  447. $sections{$name} .= $contents;
  448. } else {
  449. $sections{$name} = $contents;
  450. push @sectionlist, $name;
  451. $section_start_lines{$name} = $new_start_line;
  452. $new_start_line = 0;
  453. }
  454. }
  455. }
  456. ##
  457. # dump DOC: section after checking that it should go out
  458. #
  459. sub dump_doc_section {
  460. my $file = shift;
  461. my $name = shift;
  462. my $contents = join "\n", @_;
  463. if ($no_doc_sections) {
  464. return;
  465. }
  466. if (($output_selection == OUTPUT_ALL) ||
  467. ($output_selection == OUTPUT_INCLUDE &&
  468. defined($function_table{$name})) ||
  469. ($output_selection == OUTPUT_EXCLUDE &&
  470. !defined($function_table{$name})))
  471. {
  472. dump_section($file, $name, $contents);
  473. output_blockhead({'sectionlist' => \@sectionlist,
  474. 'sections' => \%sections,
  475. 'module' => $modulename,
  476. 'content-only' => ($output_selection != OUTPUT_ALL), });
  477. }
  478. }
  479. ##
  480. # output function
  481. #
  482. # parameterdescs, a hash.
  483. # function => "function name"
  484. # parameterlist => @list of parameters
  485. # parameterdescs => %parameter descriptions
  486. # sectionlist => @list of sections
  487. # sections => %section descriptions
  488. #
  489. sub output_highlight {
  490. my $contents = join "\n",@_;
  491. my $line;
  492. # DEBUG
  493. # if (!defined $contents) {
  494. # use Carp;
  495. # confess "output_highlight got called with no args?\n";
  496. # }
  497. # print STDERR "contents b4:$contents\n";
  498. eval $dohighlight;
  499. die $@ if $@;
  500. # print STDERR "contents af:$contents\n";
  501. foreach $line (split "\n", $contents) {
  502. if (! $output_preformatted) {
  503. $line =~ s/^\s*//;
  504. }
  505. if ($line eq ""){
  506. if (! $output_preformatted) {
  507. print $lineprefix, $blankline;
  508. }
  509. } else {
  510. if ($output_mode eq "man" && substr($line, 0, 1) eq ".") {
  511. print "\\&$line";
  512. } else {
  513. print $lineprefix, $line;
  514. }
  515. }
  516. print "\n";
  517. }
  518. }
  519. ##
  520. # output function in man
  521. sub output_function_man(%) {
  522. my %args = %{$_[0]};
  523. my ($parameter, $section);
  524. my $count;
  525. print ".TH \"$args{'function'}\" 9 \"$args{'function'}\" \"$man_date\" \"Kernel Hacker's Manual\" LINUX\n";
  526. print ".SH NAME\n";
  527. print $args{'function'} . " \\- " . $args{'purpose'} . "\n";
  528. print ".SH SYNOPSIS\n";
  529. if ($args{'functiontype'} ne "") {
  530. print ".B \"" . $args{'functiontype'} . "\" " . $args{'function'} . "\n";
  531. } else {
  532. print ".B \"" . $args{'function'} . "\n";
  533. }
  534. $count = 0;
  535. my $parenth = "(";
  536. my $post = ",";
  537. foreach my $parameter (@{$args{'parameterlist'}}) {
  538. if ($count == $#{$args{'parameterlist'}}) {
  539. $post = ");";
  540. }
  541. $type = $args{'parametertypes'}{$parameter};
  542. if ($type =~ m/([^\(]*\(\*)\s*\)\s*\(([^\)]*)\)/) {
  543. # pointer-to-function
  544. print ".BI \"" . $parenth . $1 . "\" " . $parameter . " \") (" . $2 . ")" . $post . "\"\n";
  545. } else {
  546. $type =~ s/([^\*])$/$1 /;
  547. print ".BI \"" . $parenth . $type . "\" " . $parameter . " \"" . $post . "\"\n";
  548. }
  549. $count++;
  550. $parenth = "";
  551. }
  552. print ".SH ARGUMENTS\n";
  553. foreach $parameter (@{$args{'parameterlist'}}) {
  554. my $parameter_name = $parameter;
  555. $parameter_name =~ s/\[.*//;
  556. print ".IP \"" . $parameter . "\" 12\n";
  557. output_highlight($args{'parameterdescs'}{$parameter_name});
  558. }
  559. foreach $section (@{$args{'sectionlist'}}) {
  560. print ".SH \"", uc $section, "\"\n";
  561. output_highlight($args{'sections'}{$section});
  562. }
  563. }
  564. ##
  565. # output enum in man
  566. sub output_enum_man(%) {
  567. my %args = %{$_[0]};
  568. my ($parameter, $section);
  569. my $count;
  570. print ".TH \"$args{'module'}\" 9 \"enum $args{'enum'}\" \"$man_date\" \"API Manual\" LINUX\n";
  571. print ".SH NAME\n";
  572. print "enum " . $args{'enum'} . " \\- " . $args{'purpose'} . "\n";
  573. print ".SH SYNOPSIS\n";
  574. print "enum " . $args{'enum'} . " {\n";
  575. $count = 0;
  576. foreach my $parameter (@{$args{'parameterlist'}}) {
  577. print ".br\n.BI \" $parameter\"\n";
  578. if ($count == $#{$args{'parameterlist'}}) {
  579. print "\n};\n";
  580. last;
  581. }
  582. else {
  583. print ", \n.br\n";
  584. }
  585. $count++;
  586. }
  587. print ".SH Constants\n";
  588. foreach $parameter (@{$args{'parameterlist'}}) {
  589. my $parameter_name = $parameter;
  590. $parameter_name =~ s/\[.*//;
  591. print ".IP \"" . $parameter . "\" 12\n";
  592. output_highlight($args{'parameterdescs'}{$parameter_name});
  593. }
  594. foreach $section (@{$args{'sectionlist'}}) {
  595. print ".SH \"$section\"\n";
  596. output_highlight($args{'sections'}{$section});
  597. }
  598. }
  599. ##
  600. # output struct in man
  601. sub output_struct_man(%) {
  602. my %args = %{$_[0]};
  603. my ($parameter, $section);
  604. print ".TH \"$args{'module'}\" 9 \"" . $args{'type'} . " " . $args{'struct'} . "\" \"$man_date\" \"API Manual\" LINUX\n";
  605. print ".SH NAME\n";
  606. print $args{'type'} . " " . $args{'struct'} . " \\- " . $args{'purpose'} . "\n";
  607. my $declaration = $args{'definition'};
  608. $declaration =~ s/\t/ /g;
  609. $declaration =~ s/\n/"\n.br\n.BI \"/g;
  610. print ".SH SYNOPSIS\n";
  611. print $args{'type'} . " " . $args{'struct'} . " {\n.br\n";
  612. print ".BI \"$declaration\n};\n.br\n\n";
  613. print ".SH Members\n";
  614. foreach $parameter (@{$args{'parameterlist'}}) {
  615. ($parameter =~ /^#/) && next;
  616. my $parameter_name = $parameter;
  617. $parameter_name =~ s/\[.*//;
  618. ($args{'parameterdescs'}{$parameter_name} ne $undescribed) || next;
  619. print ".IP \"" . $parameter . "\" 12\n";
  620. output_highlight($args{'parameterdescs'}{$parameter_name});
  621. }
  622. foreach $section (@{$args{'sectionlist'}}) {
  623. print ".SH \"$section\"\n";
  624. output_highlight($args{'sections'}{$section});
  625. }
  626. }
  627. ##
  628. # output typedef in man
  629. sub output_typedef_man(%) {
  630. my %args = %{$_[0]};
  631. my ($parameter, $section);
  632. print ".TH \"$args{'module'}\" 9 \"$args{'typedef'}\" \"$man_date\" \"API Manual\" LINUX\n";
  633. print ".SH NAME\n";
  634. print "typedef " . $args{'typedef'} . " \\- " . $args{'purpose'} . "\n";
  635. foreach $section (@{$args{'sectionlist'}}) {
  636. print ".SH \"$section\"\n";
  637. output_highlight($args{'sections'}{$section});
  638. }
  639. }
  640. sub output_blockhead_man(%) {
  641. my %args = %{$_[0]};
  642. my ($parameter, $section);
  643. my $count;
  644. print ".TH \"$args{'module'}\" 9 \"$args{'module'}\" \"$man_date\" \"API Manual\" LINUX\n";
  645. foreach $section (@{$args{'sectionlist'}}) {
  646. print ".SH \"$section\"\n";
  647. output_highlight($args{'sections'}{$section});
  648. }
  649. }
  650. ##
  651. # output in restructured text
  652. #
  653. #
  654. # This could use some work; it's used to output the DOC: sections, and
  655. # starts by putting out the name of the doc section itself, but that tends
  656. # to duplicate a header already in the template file.
  657. #
  658. sub output_blockhead_rst(%) {
  659. my %args = %{$_[0]};
  660. my ($parameter, $section);
  661. foreach $section (@{$args{'sectionlist'}}) {
  662. if ($output_selection != OUTPUT_INCLUDE) {
  663. print "**$section**\n\n";
  664. }
  665. print_lineno($section_start_lines{$section});
  666. output_highlight_rst($args{'sections'}{$section});
  667. print "\n";
  668. }
  669. }
  670. #
  671. # Apply the RST highlights to a sub-block of text.
  672. #
  673. sub highlight_block($) {
  674. # The dohighlight kludge requires the text be called $contents
  675. my $contents = shift;
  676. eval $dohighlight;
  677. die $@ if $@;
  678. return $contents;
  679. }
  680. #
  681. # Regexes used only here.
  682. #
  683. my $sphinx_literal = '^[^.].*::$';
  684. my $sphinx_cblock = '^\.\.\ +code-block::';
  685. sub output_highlight_rst {
  686. my $input = join "\n",@_;
  687. my $output = "";
  688. my $line;
  689. my $in_literal = 0;
  690. my $litprefix;
  691. my $block = "";
  692. foreach $line (split "\n",$input) {
  693. #
  694. # If we're in a literal block, see if we should drop out
  695. # of it. Otherwise pass the line straight through unmunged.
  696. #
  697. if ($in_literal) {
  698. if (! ($line =~ /^\s*$/)) {
  699. #
  700. # If this is the first non-blank line in a literal
  701. # block we need to figure out what the proper indent is.
  702. #
  703. if ($litprefix eq "") {
  704. $line =~ /^(\s*)/;
  705. $litprefix = '^' . $1;
  706. $output .= $line . "\n";
  707. } elsif (! ($line =~ /$litprefix/)) {
  708. $in_literal = 0;
  709. } else {
  710. $output .= $line . "\n";
  711. }
  712. } else {
  713. $output .= $line . "\n";
  714. }
  715. }
  716. #
  717. # Not in a literal block (or just dropped out)
  718. #
  719. if (! $in_literal) {
  720. $block .= $line . "\n";
  721. if (($line =~ /$sphinx_literal/) || ($line =~ /$sphinx_cblock/)) {
  722. $in_literal = 1;
  723. $litprefix = "";
  724. $output .= highlight_block($block);
  725. $block = ""
  726. }
  727. }
  728. }
  729. if ($block) {
  730. $output .= highlight_block($block);
  731. }
  732. foreach $line (split "\n", $output) {
  733. print $lineprefix . $line . "\n";
  734. }
  735. }
  736. sub output_function_rst(%) {
  737. my %args = %{$_[0]};
  738. my ($parameter, $section);
  739. my $oldprefix = $lineprefix;
  740. my $start = "";
  741. if ($args{'typedef'}) {
  742. print ".. c:type:: ". $args{'function'} . "\n\n";
  743. print_lineno($declaration_start_line);
  744. print " **Typedef**: ";
  745. $lineprefix = "";
  746. output_highlight_rst($args{'purpose'});
  747. $start = "\n\n**Syntax**\n\n ``";
  748. } else {
  749. print ".. c:function:: ";
  750. }
  751. if ($args{'functiontype'} ne "") {
  752. $start .= $args{'functiontype'} . " " . $args{'function'} . " (";
  753. } else {
  754. $start .= $args{'function'} . " (";
  755. }
  756. print $start;
  757. my $count = 0;
  758. foreach my $parameter (@{$args{'parameterlist'}}) {
  759. if ($count ne 0) {
  760. print ", ";
  761. }
  762. $count++;
  763. $type = $args{'parametertypes'}{$parameter};
  764. if ($type =~ m/([^\(]*\(\*)\s*\)\s*\(([^\)]*)\)/) {
  765. # pointer-to-function
  766. print $1 . $parameter . ") (" . $2 . ")";
  767. } else {
  768. print $type . " " . $parameter;
  769. }
  770. }
  771. if ($args{'typedef'}) {
  772. print ");``\n\n";
  773. } else {
  774. print ")\n\n";
  775. print_lineno($declaration_start_line);
  776. $lineprefix = " ";
  777. output_highlight_rst($args{'purpose'});
  778. print "\n";
  779. }
  780. print "**Parameters**\n\n";
  781. $lineprefix = " ";
  782. foreach $parameter (@{$args{'parameterlist'}}) {
  783. my $parameter_name = $parameter;
  784. $parameter_name =~ s/\[.*//;
  785. $type = $args{'parametertypes'}{$parameter};
  786. if ($type ne "") {
  787. print "``$type $parameter``\n";
  788. } else {
  789. print "``$parameter``\n";
  790. }
  791. print_lineno($parameterdesc_start_lines{$parameter_name});
  792. if (defined($args{'parameterdescs'}{$parameter_name}) &&
  793. $args{'parameterdescs'}{$parameter_name} ne $undescribed) {
  794. output_highlight_rst($args{'parameterdescs'}{$parameter_name});
  795. } else {
  796. print " *undescribed*\n";
  797. }
  798. print "\n";
  799. }
  800. $lineprefix = $oldprefix;
  801. output_section_rst(@_);
  802. }
  803. sub output_section_rst(%) {
  804. my %args = %{$_[0]};
  805. my $section;
  806. my $oldprefix = $lineprefix;
  807. $lineprefix = "";
  808. foreach $section (@{$args{'sectionlist'}}) {
  809. print "**$section**\n\n";
  810. print_lineno($section_start_lines{$section});
  811. output_highlight_rst($args{'sections'}{$section});
  812. print "\n";
  813. }
  814. print "\n";
  815. $lineprefix = $oldprefix;
  816. }
  817. sub output_enum_rst(%) {
  818. my %args = %{$_[0]};
  819. my ($parameter);
  820. my $oldprefix = $lineprefix;
  821. my $count;
  822. my $name = "enum " . $args{'enum'};
  823. print "\n\n.. c:type:: " . $name . "\n\n";
  824. print_lineno($declaration_start_line);
  825. $lineprefix = " ";
  826. output_highlight_rst($args{'purpose'});
  827. print "\n";
  828. print "**Constants**\n\n";
  829. $lineprefix = " ";
  830. foreach $parameter (@{$args{'parameterlist'}}) {
  831. print "``$parameter``\n";
  832. if ($args{'parameterdescs'}{$parameter} ne $undescribed) {
  833. output_highlight_rst($args{'parameterdescs'}{$parameter});
  834. } else {
  835. print " *undescribed*\n";
  836. }
  837. print "\n";
  838. }
  839. $lineprefix = $oldprefix;
  840. output_section_rst(@_);
  841. }
  842. sub output_typedef_rst(%) {
  843. my %args = %{$_[0]};
  844. my ($parameter);
  845. my $oldprefix = $lineprefix;
  846. my $name = "typedef " . $args{'typedef'};
  847. print "\n\n.. c:type:: " . $name . "\n\n";
  848. print_lineno($declaration_start_line);
  849. $lineprefix = " ";
  850. output_highlight_rst($args{'purpose'});
  851. print "\n";
  852. $lineprefix = $oldprefix;
  853. output_section_rst(@_);
  854. }
  855. sub output_struct_rst(%) {
  856. my %args = %{$_[0]};
  857. my ($parameter);
  858. my $oldprefix = $lineprefix;
  859. my $name = $args{'type'} . " " . $args{'struct'};
  860. # Sphinx 3.0 and up will emit warnings for "c:type:: struct Foo".
  861. # It wants to see "c:struct:: Foo" (and will add the word 'struct' in
  862. # the rendered output).
  863. if ((split(/\./, $sphinx_version))[0] >= 3) {
  864. my $sname = $name;
  865. $sname =~ s/^struct //;
  866. print "\n\n.. c:struct:: " . $sname . "\n\n";
  867. } else {
  868. print "\n\n.. c:type:: " . $name . "\n\n";
  869. }
  870. print_lineno($declaration_start_line);
  871. $lineprefix = " ";
  872. output_highlight_rst($args{'purpose'});
  873. print "\n";
  874. print "**Definition**\n\n";
  875. print "::\n\n";
  876. my $declaration = $args{'definition'};
  877. $declaration =~ s/\t/ /g;
  878. print " " . $args{'type'} . " " . $args{'struct'} . " {\n$declaration };\n\n";
  879. print "**Members**\n\n";
  880. $lineprefix = " ";
  881. foreach $parameter (@{$args{'parameterlist'}}) {
  882. ($parameter =~ /^#/) && next;
  883. my $parameter_name = $parameter;
  884. $parameter_name =~ s/\[.*//;
  885. ($args{'parameterdescs'}{$parameter_name} ne $undescribed) || next;
  886. $type = $args{'parametertypes'}{$parameter};
  887. print_lineno($parameterdesc_start_lines{$parameter_name});
  888. print "``" . $parameter . "``\n";
  889. output_highlight_rst($args{'parameterdescs'}{$parameter_name});
  890. print "\n";
  891. }
  892. print "\n";
  893. $lineprefix = $oldprefix;
  894. output_section_rst(@_);
  895. }
  896. ## none mode output functions
  897. sub output_function_none(%) {
  898. }
  899. sub output_enum_none(%) {
  900. }
  901. sub output_typedef_none(%) {
  902. }
  903. sub output_struct_none(%) {
  904. }
  905. sub output_blockhead_none(%) {
  906. }
  907. ##
  908. # generic output function for all types (function, struct/union, typedef, enum);
  909. # calls the generated, variable output_ function name based on
  910. # functype and output_mode
  911. sub output_declaration {
  912. no strict 'refs';
  913. my $name = shift;
  914. my $functype = shift;
  915. my $func = "output_${functype}_$output_mode";
  916. if (($output_selection == OUTPUT_ALL) ||
  917. (($output_selection == OUTPUT_INCLUDE ||
  918. $output_selection == OUTPUT_EXPORTED) &&
  919. defined($function_table{$name})) ||
  920. (($output_selection == OUTPUT_EXCLUDE ||
  921. $output_selection == OUTPUT_INTERNAL) &&
  922. !($functype eq "function" && defined($function_table{$name}))))
  923. {
  924. &$func(@_);
  925. $section_counter++;
  926. }
  927. }
  928. ##
  929. # generic output function - calls the right one based on current output mode.
  930. sub output_blockhead {
  931. no strict 'refs';
  932. my $func = "output_blockhead_" . $output_mode;
  933. &$func(@_);
  934. $section_counter++;
  935. }
  936. ##
  937. # takes a declaration (struct, union, enum, typedef) and
  938. # invokes the right handler. NOT called for functions.
  939. sub dump_declaration($$) {
  940. no strict 'refs';
  941. my ($prototype, $file) = @_;
  942. my $func = "dump_" . $decl_type;
  943. &$func(@_);
  944. }
  945. sub dump_union($$) {
  946. dump_struct(@_);
  947. }
  948. sub dump_struct($$) {
  949. my $x = shift;
  950. my $file = shift;
  951. if ($x =~ /(struct|union)\s+(\w+)\s*\{(.*)\}(\s*(__packed|__aligned|__attribute__\s*\(\([a-z0-9,_\s\(\)]*\)\)))*/) {
  952. my $decl_type = $1;
  953. $declaration_name = $2;
  954. my $members = $3;
  955. # ignore members marked private:
  956. $members =~ s/\/\*\s*private:.*?\/\*\s*public:.*?\*\///gosi;
  957. $members =~ s/\/\*\s*private:.*//gosi;
  958. # strip comments:
  959. $members =~ s/\/\*.*?\*\///gos;
  960. # strip attributes
  961. $members =~ s/\s*__attribute__\s*\(\([a-z0-9,_\*\s\(\)]*\)\)//gi;
  962. $members =~ s/\s*__aligned\s*\([^;]*\)//gos;
  963. $members =~ s/\s*__packed\s*//gos;
  964. $members =~ s/\s*CRYPTO_MINALIGN_ATTR//gos;
  965. # replace DECLARE_BITMAP
  966. $members =~ s/DECLARE_BITMAP\s*\(([^,)]+),\s*([^,)]+)\)/unsigned long $1\[BITS_TO_LONGS($2)\]/gos;
  967. # replace DECLARE_HASHTABLE
  968. $members =~ s/DECLARE_HASHTABLE\s*\(([^,)]+),\s*([^,)]+)\)/unsigned long $1\[1 << (($2) - 1)\]/gos;
  969. # replace DECLARE_KFIFO
  970. $members =~ s/DECLARE_KFIFO\s*\(([^,)]+),\s*([^,)]+),\s*([^,)]+)\)/$2 \*$1/gos;
  971. # replace DECLARE_KFIFO_PTR
  972. $members =~ s/DECLARE_KFIFO_PTR\s*\(([^,)]+),\s*([^,)]+)\)/$2 \*$1/gos;
  973. my $declaration = $members;
  974. # Split nested struct/union elements as newer ones
  975. while ($members =~ m/(struct|union)([^\{\};]+)\{([^\{\}]*)\}([^\{\}\;]*)\;/) {
  976. my $newmember;
  977. my $maintype = $1;
  978. my $ids = $4;
  979. my $content = $3;
  980. foreach my $id(split /,/, $ids) {
  981. $newmember .= "$maintype $id; ";
  982. $id =~ s/[:\[].*//;
  983. $id =~ s/^\s*\**(\S+)\s*/$1/;
  984. foreach my $arg (split /;/, $content) {
  985. next if ($arg =~ m/^\s*$/);
  986. if ($arg =~ m/^([^\(]+\(\*?\s*)([\w\.]*)(\s*\).*)/) {
  987. # pointer-to-function
  988. my $type = $1;
  989. my $name = $2;
  990. my $extra = $3;
  991. next if (!$name);
  992. if ($id =~ m/^\s*$/) {
  993. # anonymous struct/union
  994. $newmember .= "$type$name$extra; ";
  995. } else {
  996. $newmember .= "$type$id.$name$extra; ";
  997. }
  998. } else {
  999. my $type;
  1000. my $names;
  1001. $arg =~ s/^\s+//;
  1002. $arg =~ s/\s+$//;
  1003. # Handle bitmaps
  1004. $arg =~ s/:\s*\d+\s*//g;
  1005. # Handle arrays
  1006. $arg =~ s/\[.*\]//g;
  1007. # The type may have multiple words,
  1008. # and multiple IDs can be defined, like:
  1009. # const struct foo, *bar, foobar
  1010. # So, we remove spaces when parsing the
  1011. # names, in order to match just names
  1012. # and commas for the names
  1013. $arg =~ s/\s*,\s*/,/g;
  1014. if ($arg =~ m/(.*)\s+([\S+,]+)/) {
  1015. $type = $1;
  1016. $names = $2;
  1017. } else {
  1018. $newmember .= "$arg; ";
  1019. next;
  1020. }
  1021. foreach my $name (split /,/, $names) {
  1022. $name =~ s/^\s*\**(\S+)\s*/$1/;
  1023. next if (($name =~ m/^\s*$/));
  1024. if ($id =~ m/^\s*$/) {
  1025. # anonymous struct/union
  1026. $newmember .= "$type $name; ";
  1027. } else {
  1028. $newmember .= "$type $id.$name; ";
  1029. }
  1030. }
  1031. }
  1032. }
  1033. }
  1034. $members =~ s/(struct|union)([^\{\};]+)\{([^\{\}]*)\}([^\{\}\;]*)\;/$newmember/;
  1035. }
  1036. # Ignore other nested elements, like enums
  1037. $members =~ s/(\{[^\{\}]*\})//g;
  1038. create_parameterlist($members, ';', $file, $declaration_name);
  1039. check_sections($file, $declaration_name, $decl_type, $sectcheck, $struct_actual);
  1040. # Adjust declaration for better display
  1041. $declaration =~ s/([\{;])/$1\n/g;
  1042. $declaration =~ s/\}\s+;/};/g;
  1043. # Better handle inlined enums
  1044. do {} while ($declaration =~ s/(enum\s+\{[^\}]+),([^\n])/$1,\n$2/);
  1045. my @def_args = split /\n/, $declaration;
  1046. my $level = 1;
  1047. $declaration = "";
  1048. foreach my $clause (@def_args) {
  1049. $clause =~ s/^\s+//;
  1050. $clause =~ s/\s+$//;
  1051. $clause =~ s/\s+/ /;
  1052. next if (!$clause);
  1053. $level-- if ($clause =~ m/(\})/ && $level > 1);
  1054. if (!($clause =~ m/^\s*#/)) {
  1055. $declaration .= "\t" x $level;
  1056. }
  1057. $declaration .= "\t" . $clause . "\n";
  1058. $level++ if ($clause =~ m/(\{)/ && !($clause =~m/\}/));
  1059. }
  1060. output_declaration($declaration_name,
  1061. 'struct',
  1062. {'struct' => $declaration_name,
  1063. 'module' => $modulename,
  1064. 'definition' => $declaration,
  1065. 'parameterlist' => \@parameterlist,
  1066. 'parameterdescs' => \%parameterdescs,
  1067. 'parametertypes' => \%parametertypes,
  1068. 'sectionlist' => \@sectionlist,
  1069. 'sections' => \%sections,
  1070. 'purpose' => $declaration_purpose,
  1071. 'type' => $decl_type
  1072. });
  1073. }
  1074. else {
  1075. print STDERR "${file}:$.: error: Cannot parse struct or union!\n";
  1076. ++$errors;
  1077. }
  1078. }
  1079. sub show_warnings($$) {
  1080. my $functype = shift;
  1081. my $name = shift;
  1082. return 1 if ($output_selection == OUTPUT_ALL);
  1083. if ($output_selection == OUTPUT_EXPORTED) {
  1084. if (defined($function_table{$name})) {
  1085. return 1;
  1086. } else {
  1087. return 0;
  1088. }
  1089. }
  1090. if ($output_selection == OUTPUT_INTERNAL) {
  1091. if (!($functype eq "function" && defined($function_table{$name}))) {
  1092. return 1;
  1093. } else {
  1094. return 0;
  1095. }
  1096. }
  1097. if ($output_selection == OUTPUT_INCLUDE) {
  1098. if (defined($function_table{$name})) {
  1099. return 1;
  1100. } else {
  1101. return 0;
  1102. }
  1103. }
  1104. if ($output_selection == OUTPUT_EXCLUDE) {
  1105. if (!defined($function_table{$name})) {
  1106. return 1;
  1107. } else {
  1108. return 0;
  1109. }
  1110. }
  1111. die("Please add the new output type at show_warnings()");
  1112. }
  1113. sub dump_enum($$) {
  1114. my $x = shift;
  1115. my $file = shift;
  1116. $x =~ s@/\*.*?\*/@@gos; # strip comments.
  1117. # strip #define macros inside enums
  1118. $x =~ s@#\s*((define|ifdef)\s+|endif)[^;]*;@@gos;
  1119. if ($x =~ /enum\s+(\w*)\s*\{(.*)\}/) {
  1120. $declaration_name = $1;
  1121. my $members = $2;
  1122. my %_members;
  1123. $members =~ s/\s+$//;
  1124. foreach my $arg (split ',', $members) {
  1125. $arg =~ s/^\s*(\w+).*/$1/;
  1126. push @parameterlist, $arg;
  1127. if (!$parameterdescs{$arg}) {
  1128. $parameterdescs{$arg} = $undescribed;
  1129. if (show_warnings("enum", $declaration_name)) {
  1130. print STDERR "${file}:$.: warning: Enum value '$arg' not described in enum '$declaration_name'\n";
  1131. }
  1132. }
  1133. $_members{$arg} = 1;
  1134. }
  1135. while (my ($k, $v) = each %parameterdescs) {
  1136. if (!exists($_members{$k})) {
  1137. if (show_warnings("enum", $declaration_name)) {
  1138. print STDERR "${file}:$.: warning: Excess enum value '$k' description in '$declaration_name'\n";
  1139. }
  1140. }
  1141. }
  1142. output_declaration($declaration_name,
  1143. 'enum',
  1144. {'enum' => $declaration_name,
  1145. 'module' => $modulename,
  1146. 'parameterlist' => \@parameterlist,
  1147. 'parameterdescs' => \%parameterdescs,
  1148. 'sectionlist' => \@sectionlist,
  1149. 'sections' => \%sections,
  1150. 'purpose' => $declaration_purpose
  1151. });
  1152. }
  1153. else {
  1154. print STDERR "${file}:$.: error: Cannot parse enum!\n";
  1155. ++$errors;
  1156. }
  1157. }
  1158. sub dump_typedef($$) {
  1159. my $x = shift;
  1160. my $file = shift;
  1161. $x =~ s@/\*.*?\*/@@gos; # strip comments.
  1162. # Parse function prototypes
  1163. if ($x =~ /typedef\s+(\w+\s*\**)\s*\(\*?\s*(\w\S+)\s*\)\s*\((.*)\);/ ||
  1164. $x =~ /typedef\s+(\w+\s*\**)\s*(\w\S+)\s*\s*\((.*)\);/) {
  1165. # Function typedefs
  1166. $return_type = $1;
  1167. $declaration_name = $2;
  1168. my $args = $3;
  1169. create_parameterlist($args, ',', $file, $declaration_name);
  1170. output_declaration($declaration_name,
  1171. 'function',
  1172. {'function' => $declaration_name,
  1173. 'typedef' => 1,
  1174. 'module' => $modulename,
  1175. 'functiontype' => $return_type,
  1176. 'parameterlist' => \@parameterlist,
  1177. 'parameterdescs' => \%parameterdescs,
  1178. 'parametertypes' => \%parametertypes,
  1179. 'sectionlist' => \@sectionlist,
  1180. 'sections' => \%sections,
  1181. 'purpose' => $declaration_purpose
  1182. });
  1183. return;
  1184. }
  1185. while (($x =~ /\(*.\)\s*;$/) || ($x =~ /\[*.\]\s*;$/)) {
  1186. $x =~ s/\(*.\)\s*;$/;/;
  1187. $x =~ s/\[*.\]\s*;$/;/;
  1188. }
  1189. if ($x =~ /typedef.*\s+(\w+)\s*;/) {
  1190. $declaration_name = $1;
  1191. output_declaration($declaration_name,
  1192. 'typedef',
  1193. {'typedef' => $declaration_name,
  1194. 'module' => $modulename,
  1195. 'sectionlist' => \@sectionlist,
  1196. 'sections' => \%sections,
  1197. 'purpose' => $declaration_purpose
  1198. });
  1199. }
  1200. else {
  1201. print STDERR "${file}:$.: error: Cannot parse typedef!\n";
  1202. ++$errors;
  1203. }
  1204. }
  1205. sub save_struct_actual($) {
  1206. my $actual = shift;
  1207. # strip all spaces from the actual param so that it looks like one string item
  1208. $actual =~ s/\s*//g;
  1209. $struct_actual = $struct_actual . $actual . " ";
  1210. }
  1211. sub create_parameterlist($$$$) {
  1212. my $args = shift;
  1213. my $splitter = shift;
  1214. my $file = shift;
  1215. my $declaration_name = shift;
  1216. my $type;
  1217. my $param;
  1218. # temporarily replace commas inside function pointer definition
  1219. while ($args =~ /(\([^\),]+),/) {
  1220. $args =~ s/(\([^\),]+),/$1#/g;
  1221. }
  1222. foreach my $arg (split($splitter, $args)) {
  1223. # strip comments
  1224. $arg =~ s/\/\*.*\*\///;
  1225. # strip leading/trailing spaces
  1226. $arg =~ s/^\s*//;
  1227. $arg =~ s/\s*$//;
  1228. $arg =~ s/\s+/ /;
  1229. if ($arg =~ /^#/) {
  1230. # Treat preprocessor directive as a typeless variable just to fill
  1231. # corresponding data structures "correctly". Catch it later in
  1232. # output_* subs.
  1233. push_parameter($arg, "", $file);
  1234. } elsif ($arg =~ m/\(.+\)\s*\(/) {
  1235. # pointer-to-function
  1236. $arg =~ tr/#/,/;
  1237. $arg =~ m/[^\(]+\(\*?\s*([\w\.]*)\s*\)/;
  1238. $param = $1;
  1239. $type = $arg;
  1240. $type =~ s/([^\(]+\(\*?)\s*$param/$1/;
  1241. save_struct_actual($param);
  1242. push_parameter($param, $type, $file, $declaration_name);
  1243. } elsif ($arg) {
  1244. $arg =~ s/\s*:\s*/:/g;
  1245. $arg =~ s/\s*\[/\[/g;
  1246. my @args = split('\s*,\s*', $arg);
  1247. if ($args[0] =~ m/\*/) {
  1248. $args[0] =~ s/(\*+)\s*/ $1/;
  1249. }
  1250. my @first_arg;
  1251. if ($args[0] =~ /^(.*\s+)(.*?\[.*\].*)$/) {
  1252. shift @args;
  1253. push(@first_arg, split('\s+', $1));
  1254. push(@first_arg, $2);
  1255. } else {
  1256. @first_arg = split('\s+', shift @args);
  1257. }
  1258. unshift(@args, pop @first_arg);
  1259. $type = join " ", @first_arg;
  1260. foreach $param (@args) {
  1261. if ($param =~ m/^(\*+)\s*(.*)/) {
  1262. save_struct_actual($2);
  1263. push_parameter($2, "$type $1", $file, $declaration_name);
  1264. }
  1265. elsif ($param =~ m/(.*?):(\d+)/) {
  1266. if ($type ne "") { # skip unnamed bit-fields
  1267. save_struct_actual($1);
  1268. push_parameter($1, "$type:$2", $file, $declaration_name)
  1269. }
  1270. }
  1271. else {
  1272. save_struct_actual($param);
  1273. push_parameter($param, $type, $file, $declaration_name);
  1274. }
  1275. }
  1276. }
  1277. }
  1278. }
  1279. sub push_parameter($$$$) {
  1280. my $param = shift;
  1281. my $type = shift;
  1282. my $file = shift;
  1283. my $declaration_name = shift;
  1284. if (($anon_struct_union == 1) && ($type eq "") &&
  1285. ($param eq "}")) {
  1286. return; # ignore the ending }; from anon. struct/union
  1287. }
  1288. $anon_struct_union = 0;
  1289. $param =~ s/[\[\)].*//;
  1290. if ($type eq "" && $param =~ /\.\.\.$/)
  1291. {
  1292. if (!$param =~ /\w\.\.\.$/) {
  1293. # handles unnamed variable parameters
  1294. $param = "...";
  1295. }
  1296. if (!defined $parameterdescs{$param} || $parameterdescs{$param} eq "") {
  1297. $parameterdescs{$param} = "variable arguments";
  1298. }
  1299. }
  1300. elsif ($type eq "" && ($param eq "" or $param eq "void"))
  1301. {
  1302. $param="void";
  1303. $parameterdescs{void} = "no arguments";
  1304. }
  1305. elsif ($type eq "" && ($param eq "struct" or $param eq "union"))
  1306. # handle unnamed (anonymous) union or struct:
  1307. {
  1308. $type = $param;
  1309. $param = "{unnamed_" . $param . "}";
  1310. $parameterdescs{$param} = "anonymous\n";
  1311. $anon_struct_union = 1;
  1312. }
  1313. # warn if parameter has no description
  1314. # (but ignore ones starting with # as these are not parameters
  1315. # but inline preprocessor statements);
  1316. # Note: It will also ignore void params and unnamed structs/unions
  1317. if (!defined $parameterdescs{$param} && $param !~ /^#/) {
  1318. $parameterdescs{$param} = $undescribed;
  1319. if (show_warnings($type, $declaration_name) && $param !~ /\./) {
  1320. print STDERR
  1321. "${file}:$.: warning: Function parameter or member '$param' not described in '$declaration_name'\n";
  1322. ++$warnings;
  1323. }
  1324. }
  1325. # strip spaces from $param so that it is one continuous string
  1326. # on @parameterlist;
  1327. # this fixes a problem where check_sections() cannot find
  1328. # a parameter like "addr[6 + 2]" because it actually appears
  1329. # as "addr[6", "+", "2]" on the parameter list;
  1330. # but it's better to maintain the param string unchanged for output,
  1331. # so just weaken the string compare in check_sections() to ignore
  1332. # "[blah" in a parameter string;
  1333. ###$param =~ s/\s*//g;
  1334. push @parameterlist, $param;
  1335. $type =~ s/\s\s+/ /g;
  1336. $parametertypes{$param} = $type;
  1337. }
  1338. sub check_sections($$$$$) {
  1339. my ($file, $decl_name, $decl_type, $sectcheck, $prmscheck) = @_;
  1340. my @sects = split ' ', $sectcheck;
  1341. my @prms = split ' ', $prmscheck;
  1342. my $err;
  1343. my ($px, $sx);
  1344. my $prm_clean; # strip trailing "[array size]" and/or beginning "*"
  1345. foreach $sx (0 .. $#sects) {
  1346. $err = 1;
  1347. foreach $px (0 .. $#prms) {
  1348. $prm_clean = $prms[$px];
  1349. $prm_clean =~ s/\[.*\]//;
  1350. $prm_clean =~ s/__attribute__\s*\(\([a-z,_\*\s\(\)]*\)\)//i;
  1351. # ignore array size in a parameter string;
  1352. # however, the original param string may contain
  1353. # spaces, e.g.: addr[6 + 2]
  1354. # and this appears in @prms as "addr[6" since the
  1355. # parameter list is split at spaces;
  1356. # hence just ignore "[..." for the sections check;
  1357. $prm_clean =~ s/\[.*//;
  1358. ##$prm_clean =~ s/^\**//;
  1359. if ($prm_clean eq $sects[$sx]) {
  1360. $err = 0;
  1361. last;
  1362. }
  1363. }
  1364. if ($err) {
  1365. if ($decl_type eq "function") {
  1366. print STDERR "${file}:$.: warning: " .
  1367. "Excess function parameter " .
  1368. "'$sects[$sx]' " .
  1369. "description in '$decl_name'\n";
  1370. ++$warnings;
  1371. }
  1372. }
  1373. }
  1374. }
  1375. ##
  1376. # Checks the section describing the return value of a function.
  1377. sub check_return_section {
  1378. my $file = shift;
  1379. my $declaration_name = shift;
  1380. my $return_type = shift;
  1381. # Ignore an empty return type (It's a macro)
  1382. # Ignore functions with a "void" return type. (But don't ignore "void *")
  1383. if (($return_type eq "") || ($return_type =~ /void\s*\w*\s*$/)) {
  1384. return;
  1385. }
  1386. if (!defined($sections{$section_return}) ||
  1387. $sections{$section_return} eq "") {
  1388. print STDERR "${file}:$.: warning: " .
  1389. "No description found for return value of " .
  1390. "'$declaration_name'\n";
  1391. ++$warnings;
  1392. }
  1393. }
  1394. ##
  1395. # takes a function prototype and the name of the current file being
  1396. # processed and spits out all the details stored in the global
  1397. # arrays/hashes.
  1398. sub dump_function($$) {
  1399. my $prototype = shift;
  1400. my $file = shift;
  1401. my $noret = 0;
  1402. $prototype =~ s/^static +//;
  1403. $prototype =~ s/^extern +//;
  1404. $prototype =~ s/^asmlinkage +//;
  1405. $prototype =~ s/^inline +//;
  1406. $prototype =~ s/^__inline__ +//;
  1407. $prototype =~ s/^__inline +//;
  1408. $prototype =~ s/^__always_inline +//;
  1409. $prototype =~ s/^noinline +//;
  1410. $prototype =~ s/__init +//;
  1411. $prototype =~ s/__init_or_module +//;
  1412. $prototype =~ s/__meminit +//;
  1413. $prototype =~ s/__must_check +//;
  1414. $prototype =~ s/__weak +//;
  1415. $prototype =~ s/__sched +//;
  1416. $prototype =~ s/__printf\s*\(\s*\d*\s*,\s*\d*\s*\) +//;
  1417. my $define = $prototype =~ s/^#\s*define\s+//; #ak added
  1418. $prototype =~ s/__attribute__\s*\(\(
  1419. (?:
  1420. [\w\s]++ # attribute name
  1421. (?:\([^)]*+\))? # attribute arguments
  1422. \s*+,? # optional comma at the end
  1423. )+
  1424. \)\)\s+//x;
  1425. # Yes, this truly is vile. We are looking for:
  1426. # 1. Return type (may be nothing if we're looking at a macro)
  1427. # 2. Function name
  1428. # 3. Function parameters.
  1429. #
  1430. # All the while we have to watch out for function pointer parameters
  1431. # (which IIRC is what the two sections are for), C types (these
  1432. # regexps don't even start to express all the possibilities), and
  1433. # so on.
  1434. #
  1435. # If you mess with these regexps, it's a good idea to check that
  1436. # the following functions' documentation still comes out right:
  1437. # - parport_register_device (function pointer parameters)
  1438. # - qatomic_set (macro)
  1439. # - pci_match_device, __copy_to_user (long return type)
  1440. if ($define && $prototype =~ m/^()([a-zA-Z0-9_~:]+)\s+/) {
  1441. # This is an object-like macro, it has no return type and no parameter
  1442. # list.
  1443. # Function-like macros are not allowed to have spaces between
  1444. # declaration_name and opening parenthesis (notice the \s+).
  1445. $return_type = $1;
  1446. $declaration_name = $2;
  1447. $noret = 1;
  1448. } elsif ($prototype =~ m/^()([a-zA-Z0-9_~:]+)\s*\(([^\(]*)\)/ ||
  1449. $prototype =~ m/^(\w+)\s+([a-zA-Z0-9_~:]+)\s*\(([^\(]*)\)/ ||
  1450. $prototype =~ m/^(\w+\s*\*+)\s*([a-zA-Z0-9_~:]+)\s*\(([^\(]*)\)/ ||
  1451. $prototype =~ m/^(\w+\s+\w+)\s+([a-zA-Z0-9_~:]+)\s*\(([^\(]*)\)/ ||
  1452. $prototype =~ m/^(\w+\s+\w+\s*\*+)\s*([a-zA-Z0-9_~:]+)\s*\(([^\(]*)\)/ ||
  1453. $prototype =~ m/^(\w+\s+\w+\s+\w+)\s+([a-zA-Z0-9_~:]+)\s*\(([^\(]*)\)/ ||
  1454. $prototype =~ m/^(\w+\s+\w+\s+\w+\s*\*+)\s*([a-zA-Z0-9_~:]+)\s*\(([^\(]*)\)/ ||
  1455. $prototype =~ m/^()([a-zA-Z0-9_~:]+)\s*\(([^\{]*)\)/ ||
  1456. $prototype =~ m/^(\w+)\s+([a-zA-Z0-9_~:]+)\s*\(([^\{]*)\)/ ||
  1457. $prototype =~ m/^(\w+\s*\*+)\s*([a-zA-Z0-9_~:]+)\s*\(([^\{]*)\)/ ||
  1458. $prototype =~ m/^(\w+\s+\w+)\s+([a-zA-Z0-9_~:]+)\s*\(([^\{]*)\)/ ||
  1459. $prototype =~ m/^(\w+\s+\w+\s*\*+)\s*([a-zA-Z0-9_~:]+)\s*\(([^\{]*)\)/ ||
  1460. $prototype =~ m/^(\w+\s+\w+\s+\w+)\s+([a-zA-Z0-9_~:]+)\s*\(([^\{]*)\)/ ||
  1461. $prototype =~ m/^(\w+\s+\w+\s+\w+\s*\*+)\s*([a-zA-Z0-9_~:]+)\s*\(([^\{]*)\)/ ||
  1462. $prototype =~ m/^(\w+\s+\w+\s+\w+\s+\w+)\s+([a-zA-Z0-9_~:]+)\s*\(([^\{]*)\)/ ||
  1463. $prototype =~ m/^(\w+\s+\w+\s+\w+\s+\w+\s*\*+)\s*([a-zA-Z0-9_~:]+)\s*\(([^\{]*)\)/ ||
  1464. $prototype =~ m/^(\w+\s+\w+\s*\*+\s*\w+\s*\*+\s*)\s*([a-zA-Z0-9_~:]+)\s*\(([^\{]*)\)/) {
  1465. $return_type = $1;
  1466. $declaration_name = $2;
  1467. my $args = $3;
  1468. create_parameterlist($args, ',', $file, $declaration_name);
  1469. } else {
  1470. print STDERR "${file}:$.: warning: cannot understand function prototype: '$prototype'\n";
  1471. return;
  1472. }
  1473. my $prms = join " ", @parameterlist;
  1474. check_sections($file, $declaration_name, "function", $sectcheck, $prms);
  1475. # This check emits a lot of warnings at the moment, because many
  1476. # functions don't have a 'Return' doc section. So until the number
  1477. # of warnings goes sufficiently down, the check is only performed in
  1478. # verbose mode.
  1479. # TODO: always perform the check.
  1480. if ($verbose && !$noret) {
  1481. check_return_section($file, $declaration_name, $return_type);
  1482. }
  1483. output_declaration($declaration_name,
  1484. 'function',
  1485. {'function' => $declaration_name,
  1486. 'module' => $modulename,
  1487. 'functiontype' => $return_type,
  1488. 'parameterlist' => \@parameterlist,
  1489. 'parameterdescs' => \%parameterdescs,
  1490. 'parametertypes' => \%parametertypes,
  1491. 'sectionlist' => \@sectionlist,
  1492. 'sections' => \%sections,
  1493. 'purpose' => $declaration_purpose
  1494. });
  1495. }
  1496. sub reset_state {
  1497. $function = "";
  1498. %parameterdescs = ();
  1499. %parametertypes = ();
  1500. @parameterlist = ();
  1501. %sections = ();
  1502. @sectionlist = ();
  1503. $sectcheck = "";
  1504. $struct_actual = "";
  1505. $prototype = "";
  1506. $state = STATE_NORMAL;
  1507. $inline_doc_state = STATE_INLINE_NA;
  1508. }
  1509. sub tracepoint_munge($) {
  1510. my $file = shift;
  1511. my $tracepointname = 0;
  1512. my $tracepointargs = 0;
  1513. if ($prototype =~ m/TRACE_EVENT\((.*?),/) {
  1514. $tracepointname = $1;
  1515. }
  1516. if ($prototype =~ m/DEFINE_SINGLE_EVENT\((.*?),/) {
  1517. $tracepointname = $1;
  1518. }
  1519. if ($prototype =~ m/DEFINE_EVENT\((.*?),(.*?),/) {
  1520. $tracepointname = $2;
  1521. }
  1522. $tracepointname =~ s/^\s+//; #strip leading whitespace
  1523. if ($prototype =~ m/TP_PROTO\((.*?)\)/) {
  1524. $tracepointargs = $1;
  1525. }
  1526. if (($tracepointname eq 0) || ($tracepointargs eq 0)) {
  1527. print STDERR "${file}:$.: warning: Unrecognized tracepoint format: \n".
  1528. "$prototype\n";
  1529. } else {
  1530. $prototype = "static inline void trace_$tracepointname($tracepointargs)";
  1531. }
  1532. }
  1533. sub syscall_munge() {
  1534. my $void = 0;
  1535. $prototype =~ s@[\r\n]+@ @gos; # strip newlines/CR's
  1536. ## if ($prototype =~ m/SYSCALL_DEFINE0\s*\(\s*(a-zA-Z0-9_)*\s*\)/) {
  1537. if ($prototype =~ m/SYSCALL_DEFINE0/) {
  1538. $void = 1;
  1539. ## $prototype = "long sys_$1(void)";
  1540. }
  1541. $prototype =~ s/SYSCALL_DEFINE.*\(/long sys_/; # fix return type & func name
  1542. if ($prototype =~ m/long (sys_.*?),/) {
  1543. $prototype =~ s/,/\(/;
  1544. } elsif ($void) {
  1545. $prototype =~ s/\)/\(void\)/;
  1546. }
  1547. # now delete all of the odd-number commas in $prototype
  1548. # so that arg types & arg names don't have a comma between them
  1549. my $count = 0;
  1550. my $len = length($prototype);
  1551. if ($void) {
  1552. $len = 0; # skip the for-loop
  1553. }
  1554. for (my $ix = 0; $ix < $len; $ix++) {
  1555. if (substr($prototype, $ix, 1) eq ',') {
  1556. $count++;
  1557. if ($count % 2 == 1) {
  1558. substr($prototype, $ix, 1) = ' ';
  1559. }
  1560. }
  1561. }
  1562. }
  1563. sub process_proto_function($$) {
  1564. my $x = shift;
  1565. my $file = shift;
  1566. $x =~ s@\/\/.*$@@gos; # strip C99-style comments to end of line
  1567. if ($x =~ m#\s*/\*\s+MACDOC\s*#io || ($x =~ /^#/ && $x !~ /^#\s*define/)) {
  1568. # do nothing
  1569. }
  1570. elsif ($x =~ /([^\{]*)/) {
  1571. $prototype .= $1;
  1572. }
  1573. if (($x =~ /\{/) || ($x =~ /\#\s*define/) || ($x =~ /;/)) {
  1574. $prototype =~ s@/\*.*?\*/@@gos; # strip comments.
  1575. $prototype =~ s@[\r\n]+@ @gos; # strip newlines/cr's.
  1576. $prototype =~ s@^\s+@@gos; # strip leading spaces
  1577. if ($prototype =~ /SYSCALL_DEFINE/) {
  1578. syscall_munge();
  1579. }
  1580. if ($prototype =~ /TRACE_EVENT/ || $prototype =~ /DEFINE_EVENT/ ||
  1581. $prototype =~ /DEFINE_SINGLE_EVENT/)
  1582. {
  1583. tracepoint_munge($file);
  1584. }
  1585. dump_function($prototype, $file);
  1586. reset_state();
  1587. }
  1588. }
  1589. sub process_proto_type($$) {
  1590. my $x = shift;
  1591. my $file = shift;
  1592. $x =~ s@[\r\n]+@ @gos; # strip newlines/cr's.
  1593. $x =~ s@^\s+@@gos; # strip leading spaces
  1594. $x =~ s@\s+$@@gos; # strip trailing spaces
  1595. $x =~ s@\/\/.*$@@gos; # strip C99-style comments to end of line
  1596. if ($x =~ /^#/) {
  1597. # To distinguish preprocessor directive from regular declaration later.
  1598. $x .= ";";
  1599. }
  1600. while (1) {
  1601. if ( $x =~ /([^\{\};]*)([\{\};])(.*)/ ) {
  1602. if( length $prototype ) {
  1603. $prototype .= " "
  1604. }
  1605. $prototype .= $1 . $2;
  1606. ($2 eq '{') && $brcount++;
  1607. ($2 eq '}') && $brcount--;
  1608. if (($2 eq ';') && ($brcount == 0)) {
  1609. dump_declaration($prototype, $file);
  1610. reset_state();
  1611. last;
  1612. }
  1613. $x = $3;
  1614. } else {
  1615. $prototype .= $x;
  1616. last;
  1617. }
  1618. }
  1619. }
  1620. sub map_filename($) {
  1621. my $file;
  1622. my ($orig_file) = @_;
  1623. if (defined($ENV{'SRCTREE'})) {
  1624. $file = "$ENV{'SRCTREE'}" . "/" . $orig_file;
  1625. } else {
  1626. $file = $orig_file;
  1627. }
  1628. if (defined($source_map{$file})) {
  1629. $file = $source_map{$file};
  1630. }
  1631. return $file;
  1632. }
  1633. sub process_export_file($) {
  1634. my ($orig_file) = @_;
  1635. my $file = map_filename($orig_file);
  1636. if (!open(IN,"<$file")) {
  1637. print STDERR "Error: Cannot open file $file\n";
  1638. ++$errors;
  1639. return;
  1640. }
  1641. while (<IN>) {
  1642. if (/$export_symbol/) {
  1643. $function_table{$2} = 1;
  1644. }
  1645. }
  1646. close(IN);
  1647. }
  1648. #
  1649. # Parsers for the various processing states.
  1650. #
  1651. # STATE_NORMAL: looking for the /** to begin everything.
  1652. #
  1653. sub process_normal() {
  1654. if (/$doc_start/o) {
  1655. $state = STATE_NAME; # next line is always the function name
  1656. $in_doc_sect = 0;
  1657. $declaration_start_line = $. + 1;
  1658. }
  1659. }
  1660. #
  1661. # STATE_NAME: Looking for the "name - description" line
  1662. #
  1663. sub process_name($$) {
  1664. my $file = shift;
  1665. my $identifier;
  1666. my $descr;
  1667. if (/$doc_block/o) {
  1668. $state = STATE_DOCBLOCK;
  1669. $contents = "";
  1670. $new_start_line = $. + 1;
  1671. if ( $1 eq "" ) {
  1672. $section = $section_intro;
  1673. } else {
  1674. $section = $1;
  1675. }
  1676. }
  1677. elsif (/$doc_decl/o) {
  1678. $identifier = $1;
  1679. if (/\s*([\w\s]+?)(\s*-|:)/) {
  1680. $identifier = $1;
  1681. }
  1682. $state = STATE_BODY;
  1683. # if there's no @param blocks need to set up default section
  1684. # here
  1685. $contents = "";
  1686. $section = $section_default;
  1687. $new_start_line = $. + 1;
  1688. if (/[-:](.*)/) {
  1689. # strip leading/trailing/multiple spaces
  1690. $descr= $1;
  1691. $descr =~ s/^\s*//;
  1692. $descr =~ s/\s*$//;
  1693. $descr =~ s/\s+/ /g;
  1694. $declaration_purpose = $descr;
  1695. $state = STATE_BODY_MAYBE;
  1696. } else {
  1697. $declaration_purpose = "";
  1698. }
  1699. if (($declaration_purpose eq "") && $verbose) {
  1700. print STDERR "${file}:$.: warning: missing initial short description on line:\n";
  1701. print STDERR $_;
  1702. ++$warnings;
  1703. }
  1704. if ($identifier =~ m/^struct\b/) {
  1705. $decl_type = 'struct';
  1706. } elsif ($identifier =~ m/^union\b/) {
  1707. $decl_type = 'union';
  1708. } elsif ($identifier =~ m/^enum\b/) {
  1709. $decl_type = 'enum';
  1710. } elsif ($identifier =~ m/^typedef\b/) {
  1711. $decl_type = 'typedef';
  1712. } else {
  1713. $decl_type = 'function';
  1714. }
  1715. if ($verbose) {
  1716. print STDERR "${file}:$.: info: Scanning doc for $identifier\n";
  1717. }
  1718. } else {
  1719. print STDERR "${file}:$.: warning: Cannot understand $_ on line $.",
  1720. " - I thought it was a doc line\n";
  1721. ++$warnings;
  1722. $state = STATE_NORMAL;
  1723. }
  1724. }
  1725. #
  1726. # STATE_BODY and STATE_BODY_MAYBE: the bulk of a kerneldoc comment.
  1727. #
  1728. sub process_body($$) {
  1729. my $file = shift;
  1730. if (/$doc_sect/i) { # case insensitive for supported section names
  1731. $newsection = $1;
  1732. $newcontents = $2;
  1733. # map the supported section names to the canonical names
  1734. if ($newsection =~ m/^description$/i) {
  1735. $newsection = $section_default;
  1736. } elsif ($newsection =~ m/^context$/i) {
  1737. $newsection = $section_context;
  1738. } elsif ($newsection =~ m/^returns?$/i) {
  1739. $newsection = $section_return;
  1740. } elsif ($newsection =~ m/^\@return$/) {
  1741. # special: @return is a section, not a param description
  1742. $newsection = $section_return;
  1743. }
  1744. if (($contents ne "") && ($contents ne "\n")) {
  1745. if (!$in_doc_sect && $verbose) {
  1746. print STDERR "${file}:$.: warning: contents before sections\n";
  1747. ++$warnings;
  1748. }
  1749. dump_section($file, $section, $contents);
  1750. $section = $section_default;
  1751. }
  1752. $in_doc_sect = 1;
  1753. $state = STATE_BODY;
  1754. $contents = $newcontents;
  1755. $new_start_line = $.;
  1756. while (substr($contents, 0, 1) eq " ") {
  1757. $contents = substr($contents, 1);
  1758. }
  1759. if ($contents ne "") {
  1760. $contents .= "\n";
  1761. }
  1762. $section = $newsection;
  1763. $leading_space = undef;
  1764. } elsif (/$doc_end/) {
  1765. if (($contents ne "") && ($contents ne "\n")) {
  1766. dump_section($file, $section, $contents);
  1767. $section = $section_default;
  1768. $contents = "";
  1769. }
  1770. # look for doc_com + <text> + doc_end:
  1771. if ($_ =~ m'\s*\*\s*[a-zA-Z_0-9:\.]+\*/') {
  1772. print STDERR "${file}:$.: warning: suspicious ending line: $_";
  1773. ++$warnings;
  1774. }
  1775. $prototype = "";
  1776. $state = STATE_PROTO;
  1777. $brcount = 0;
  1778. } elsif (/$doc_content/) {
  1779. # miguel-style comment kludge, look for blank lines after
  1780. # @parameter line to signify start of description
  1781. if ($1 eq "") {
  1782. if ($section =~ m/^@/ || $section eq $section_context) {
  1783. dump_section($file, $section, $contents);
  1784. $section = $section_default;
  1785. $contents = "";
  1786. $new_start_line = $.;
  1787. } else {
  1788. $contents .= "\n";
  1789. }
  1790. $state = STATE_BODY;
  1791. } elsif ($state == STATE_BODY_MAYBE) {
  1792. # Continued declaration purpose
  1793. chomp($declaration_purpose);
  1794. $declaration_purpose .= " " . $1;
  1795. $declaration_purpose =~ s/\s+/ /g;
  1796. } else {
  1797. my $cont = $1;
  1798. if ($section =~ m/^@/ || $section eq $section_context) {
  1799. if (!defined $leading_space) {
  1800. if ($cont =~ m/^(\s+)/) {
  1801. $leading_space = $1;
  1802. } else {
  1803. $leading_space = "";
  1804. }
  1805. }
  1806. $cont =~ s/^$leading_space//;
  1807. }
  1808. $contents .= $cont . "\n";
  1809. }
  1810. } else {
  1811. # i dont know - bad line? ignore.
  1812. print STDERR "${file}:$.: warning: bad line: $_";
  1813. ++$warnings;
  1814. }
  1815. }
  1816. #
  1817. # STATE_PROTO: reading a function/whatever prototype.
  1818. #
  1819. sub process_proto($$) {
  1820. my $file = shift;
  1821. if (/$doc_inline_oneline/) {
  1822. $section = $1;
  1823. $contents = $2;
  1824. if ($contents ne "") {
  1825. $contents .= "\n";
  1826. dump_section($file, $section, $contents);
  1827. $section = $section_default;
  1828. $contents = "";
  1829. }
  1830. } elsif (/$doc_inline_start/) {
  1831. $state = STATE_INLINE;
  1832. $inline_doc_state = STATE_INLINE_NAME;
  1833. } elsif ($decl_type eq 'function') {
  1834. process_proto_function($_, $file);
  1835. } else {
  1836. process_proto_type($_, $file);
  1837. }
  1838. }
  1839. #
  1840. # STATE_DOCBLOCK: within a DOC: block.
  1841. #
  1842. sub process_docblock($$) {
  1843. my $file = shift;
  1844. if (/$doc_end/) {
  1845. dump_doc_section($file, $section, $contents);
  1846. $section = $section_default;
  1847. $contents = "";
  1848. $function = "";
  1849. %parameterdescs = ();
  1850. %parametertypes = ();
  1851. @parameterlist = ();
  1852. %sections = ();
  1853. @sectionlist = ();
  1854. $prototype = "";
  1855. $state = STATE_NORMAL;
  1856. } elsif (/$doc_content/) {
  1857. if ( $1 eq "" ) {
  1858. $contents .= $blankline;
  1859. } else {
  1860. $contents .= $1 . "\n";
  1861. }
  1862. }
  1863. }
  1864. #
  1865. # STATE_INLINE: docbook comments within a prototype.
  1866. #
  1867. sub process_inline($$) {
  1868. my $file = shift;
  1869. # First line (state 1) needs to be a @parameter
  1870. if ($inline_doc_state == STATE_INLINE_NAME && /$doc_inline_sect/o) {
  1871. $section = $1;
  1872. $contents = $2;
  1873. $new_start_line = $.;
  1874. if ($contents ne "") {
  1875. while (substr($contents, 0, 1) eq " ") {
  1876. $contents = substr($contents, 1);
  1877. }
  1878. $contents .= "\n";
  1879. }
  1880. $inline_doc_state = STATE_INLINE_TEXT;
  1881. # Documentation block end */
  1882. } elsif (/$doc_inline_end/) {
  1883. if (($contents ne "") && ($contents ne "\n")) {
  1884. dump_section($file, $section, $contents);
  1885. $section = $section_default;
  1886. $contents = "";
  1887. }
  1888. $state = STATE_PROTO;
  1889. $inline_doc_state = STATE_INLINE_NA;
  1890. # Regular text
  1891. } elsif (/$doc_content/) {
  1892. if ($inline_doc_state == STATE_INLINE_TEXT) {
  1893. $contents .= $1 . "\n";
  1894. # nuke leading blank lines
  1895. if ($contents =~ /^\s*$/) {
  1896. $contents = "";
  1897. }
  1898. } elsif ($inline_doc_state == STATE_INLINE_NAME) {
  1899. $inline_doc_state = STATE_INLINE_ERROR;
  1900. print STDERR "${file}:$.: warning: ";
  1901. print STDERR "Incorrect use of kernel-doc format: $_";
  1902. ++$warnings;
  1903. }
  1904. }
  1905. }
  1906. sub process_file($) {
  1907. my $file;
  1908. my $initial_section_counter = $section_counter;
  1909. my ($orig_file) = @_;
  1910. $file = map_filename($orig_file);
  1911. if (!open(IN,"<$file")) {
  1912. print STDERR "Error: Cannot open file $file\n";
  1913. ++$errors;
  1914. return;
  1915. }
  1916. $. = 1;
  1917. $section_counter = 0;
  1918. while (<IN>) {
  1919. while (s/\\\s*$//) {
  1920. $_ .= <IN>;
  1921. }
  1922. # Replace tabs by spaces
  1923. while ($_ =~ s/\t+/' ' x (length($&) * 8 - length($`) % 8)/e) {};
  1924. # Hand this line to the appropriate state handler
  1925. if ($state == STATE_NORMAL) {
  1926. process_normal();
  1927. } elsif ($state == STATE_NAME) {
  1928. process_name($file, $_);
  1929. } elsif ($state == STATE_BODY || $state == STATE_BODY_MAYBE) {
  1930. process_body($file, $_);
  1931. } elsif ($state == STATE_INLINE) { # scanning for inline parameters
  1932. process_inline($file, $_);
  1933. } elsif ($state == STATE_PROTO) {
  1934. process_proto($file, $_);
  1935. } elsif ($state == STATE_DOCBLOCK) {
  1936. process_docblock($file, $_);
  1937. }
  1938. }
  1939. # Make sure we got something interesting.
  1940. if ($initial_section_counter == $section_counter && $
  1941. output_mode ne "none") {
  1942. if ($output_selection == OUTPUT_INCLUDE) {
  1943. print STDERR "${file}:1: warning: '$_' not found\n"
  1944. for keys %function_table;
  1945. }
  1946. else {
  1947. print STDERR "${file}:1: warning: no structured comments found\n";
  1948. }
  1949. }
  1950. }
  1951. $kernelversion = get_kernel_version();
  1952. # generate a sequence of code that will splice in highlighting information
  1953. # using the s// operator.
  1954. for (my $k = 0; $k < @highlights; $k++) {
  1955. my $pattern = $highlights[$k][0];
  1956. my $result = $highlights[$k][1];
  1957. # print STDERR "scanning pattern:$pattern, highlight:($result)\n";
  1958. $dohighlight .= "\$contents =~ s:$pattern:$result:gs;\n";
  1959. }
  1960. # Read the file that maps relative names to absolute names for
  1961. # separate source and object directories and for shadow trees.
  1962. if (open(SOURCE_MAP, "<.tmp_filelist.txt")) {
  1963. my ($relname, $absname);
  1964. while(<SOURCE_MAP>) {
  1965. chop();
  1966. ($relname, $absname) = (split())[0..1];
  1967. $relname =~ s:^/+::;
  1968. $source_map{$relname} = $absname;
  1969. }
  1970. close(SOURCE_MAP);
  1971. }
  1972. if ($output_selection == OUTPUT_EXPORTED ||
  1973. $output_selection == OUTPUT_INTERNAL) {
  1974. push(@export_file_list, @ARGV);
  1975. foreach (@export_file_list) {
  1976. chomp;
  1977. process_export_file($_);
  1978. }
  1979. }
  1980. foreach (@ARGV) {
  1981. chomp;
  1982. process_file($_);
  1983. }
  1984. if ($verbose && $errors) {
  1985. print STDERR "$errors errors\n";
  1986. }
  1987. if ($verbose && $warnings) {
  1988. print STDERR "$warnings warnings\n";
  1989. }
  1990. exit($output_mode eq "none" ? 0 : $errors);