kernel-doc 58 KB

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