kernel-doc 59 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162
  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*(\.\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 = "rst";
  246. my $output_preformatted = 0;
  247. my $no_doc_sections = 0;
  248. my $enable_lineno = 0;
  249. my @highlights = @highlights_rst;
  250. my $blankline = $blankline_rst;
  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_BODY_MAYBE => 2, # body - or maybe more description
  297. STATE_BODY => 3, # the body of the comment
  298. STATE_PROTO => 4, # scanning prototype
  299. STATE_DOCBLOCK => 5, # documentation block
  300. STATE_INLINE => 6, # gathering documentation outside main block
  301. };
  302. my $state;
  303. my $in_doc_sect;
  304. my $leading_space;
  305. # Inline documentation state
  306. use constant {
  307. STATE_INLINE_NA => 0, # not applicable ($state != STATE_INLINE)
  308. STATE_INLINE_NAME => 1, # looking for member name (@foo:)
  309. STATE_INLINE_TEXT => 2, # looking for member documentation
  310. STATE_INLINE_END => 3, # done
  311. STATE_INLINE_ERROR => 4, # error - Comment without header was found.
  312. # Spit a warning as it's not
  313. # proper kernel-doc and ignore the rest.
  314. };
  315. my $inline_doc_state;
  316. #declaration types: can be
  317. # 'function', 'struct', 'union', 'enum', 'typedef'
  318. my $decl_type;
  319. my $doc_start = '^/\*\*\s*$'; # Allow whitespace at end of comment start.
  320. my $doc_end = '\*/';
  321. my $doc_com = '\s*\*\s*';
  322. my $doc_com_body = '\s*\* ?';
  323. my $doc_decl = $doc_com . '(\w+)';
  324. # @params and a strictly limited set of supported section names
  325. my $doc_sect = $doc_com .
  326. '\s*(\@[.\w]+|\@\.\.\.|description|context|returns?|notes?|examples?)\s*:(.*)';
  327. my $doc_content = $doc_com_body . '(.*)';
  328. my $doc_block = $doc_com . 'DOC:\s*(.*)?';
  329. my $doc_inline_start = '^\s*/\*\*\s*$';
  330. my $doc_inline_sect = '\s*\*\s*(@[\w\s]+):(.*)';
  331. my $doc_inline_end = '^\s*\*/\s*$';
  332. my $doc_inline_oneline = '^\s*/\*\*\s*(@[\w\s]+):\s*(.*)\s*\*/\s*$';
  333. my $export_symbol = '^\s*EXPORT_SYMBOL(_GPL)?\s*\(\s*(\w+)\s*\)\s*;';
  334. my %parameterdescs;
  335. my %parameterdesc_start_lines;
  336. my @parameterlist;
  337. my %sections;
  338. my @sectionlist;
  339. my %section_start_lines;
  340. my $sectcheck;
  341. my $struct_actual;
  342. my $contents = "";
  343. my $new_start_line = 0;
  344. # the canonical section names. see also $doc_sect above.
  345. my $section_default = "Description"; # default section
  346. my $section_intro = "Introduction";
  347. my $section = $section_default;
  348. my $section_context = "Context";
  349. my $section_return = "Return";
  350. my $undescribed = "-- undescribed --";
  351. reset_state();
  352. while ($ARGV[0] =~ m/^--?(.*)/) {
  353. my $cmd = $1;
  354. shift @ARGV;
  355. if ($cmd eq "man") {
  356. $output_mode = "man";
  357. @highlights = @highlights_man;
  358. $blankline = $blankline_man;
  359. } elsif ($cmd eq "rst") {
  360. $output_mode = "rst";
  361. @highlights = @highlights_rst;
  362. $blankline = $blankline_rst;
  363. } elsif ($cmd eq "none") {
  364. $output_mode = "none";
  365. } elsif ($cmd eq "module") { # not needed for XML, inherits from calling document
  366. $modulename = shift @ARGV;
  367. } elsif ($cmd eq "function") { # to only output specific functions
  368. $output_selection = OUTPUT_INCLUDE;
  369. $function = shift @ARGV;
  370. $function_table{$function} = 1;
  371. } elsif ($cmd eq "nofunction") { # output all except specific functions
  372. $output_selection = OUTPUT_EXCLUDE;
  373. $function = shift @ARGV;
  374. $function_table{$function} = 1;
  375. } elsif ($cmd eq "export") { # only exported symbols
  376. $output_selection = OUTPUT_EXPORTED;
  377. %function_table = ();
  378. } elsif ($cmd eq "internal") { # only non-exported symbols
  379. $output_selection = OUTPUT_INTERNAL;
  380. %function_table = ();
  381. } elsif ($cmd eq "export-file") {
  382. my $file = shift @ARGV;
  383. push(@export_file_list, $file);
  384. } elsif ($cmd eq "v") {
  385. $verbose = 1;
  386. } elsif (($cmd eq "h") || ($cmd eq "help")) {
  387. usage();
  388. } elsif ($cmd eq 'no-doc-sections') {
  389. $no_doc_sections = 1;
  390. } elsif ($cmd eq 'enable-lineno') {
  391. $enable_lineno = 1;
  392. } elsif ($cmd eq 'show-not-found') {
  393. $show_not_found = 1;
  394. } else {
  395. # Unknown argument
  396. usage();
  397. }
  398. }
  399. # continue execution near EOF;
  400. # get kernel version from env
  401. sub get_kernel_version() {
  402. my $version = 'unknown kernel version';
  403. if (defined($ENV{'KERNELVERSION'})) {
  404. $version = $ENV{'KERNELVERSION'};
  405. }
  406. return $version;
  407. }
  408. #
  409. sub print_lineno {
  410. my $lineno = shift;
  411. if ($enable_lineno && defined($lineno)) {
  412. print "#define LINENO " . $lineno . "\n";
  413. }
  414. }
  415. ##
  416. # dumps section contents to arrays/hashes intended for that purpose.
  417. #
  418. sub dump_section {
  419. my $file = shift;
  420. my $name = shift;
  421. my $contents = join "\n", @_;
  422. if ($name =~ m/$type_param/) {
  423. $name = $1;
  424. $parameterdescs{$name} = $contents;
  425. $sectcheck = $sectcheck . $name . " ";
  426. $parameterdesc_start_lines{$name} = $new_start_line;
  427. $new_start_line = 0;
  428. } elsif ($name eq "@\.\.\.") {
  429. $name = "...";
  430. $parameterdescs{$name} = $contents;
  431. $sectcheck = $sectcheck . $name . " ";
  432. $parameterdesc_start_lines{$name} = $new_start_line;
  433. $new_start_line = 0;
  434. } else {
  435. if (defined($sections{$name}) && ($sections{$name} ne "")) {
  436. # Only warn on user specified duplicate section names.
  437. if ($name ne $section_default) {
  438. print STDERR "${file}:$.: warning: duplicate section name '$name'\n";
  439. ++$warnings;
  440. }
  441. $sections{$name} .= $contents;
  442. } else {
  443. $sections{$name} = $contents;
  444. push @sectionlist, $name;
  445. $section_start_lines{$name} = $new_start_line;
  446. $new_start_line = 0;
  447. }
  448. }
  449. }
  450. ##
  451. # dump DOC: section after checking that it should go out
  452. #
  453. sub dump_doc_section {
  454. my $file = shift;
  455. my $name = shift;
  456. my $contents = join "\n", @_;
  457. if ($no_doc_sections) {
  458. return;
  459. }
  460. if (($output_selection == OUTPUT_ALL) ||
  461. ($output_selection == OUTPUT_INCLUDE &&
  462. defined($function_table{$name})) ||
  463. ($output_selection == OUTPUT_EXCLUDE &&
  464. !defined($function_table{$name})))
  465. {
  466. dump_section($file, $name, $contents);
  467. output_blockhead({'sectionlist' => \@sectionlist,
  468. 'sections' => \%sections,
  469. 'module' => $modulename,
  470. 'content-only' => ($output_selection != OUTPUT_ALL), });
  471. }
  472. }
  473. ##
  474. # output function
  475. #
  476. # parameterdescs, a hash.
  477. # function => "function name"
  478. # parameterlist => @list of parameters
  479. # parameterdescs => %parameter descriptions
  480. # sectionlist => @list of sections
  481. # sections => %section descriptions
  482. #
  483. sub output_highlight {
  484. my $contents = join "\n",@_;
  485. my $line;
  486. # DEBUG
  487. # if (!defined $contents) {
  488. # use Carp;
  489. # confess "output_highlight got called with no args?\n";
  490. # }
  491. # print STDERR "contents b4:$contents\n";
  492. eval $dohighlight;
  493. die $@ if $@;
  494. # print STDERR "contents af:$contents\n";
  495. foreach $line (split "\n", $contents) {
  496. if (! $output_preformatted) {
  497. $line =~ s/^\s*//;
  498. }
  499. if ($line eq ""){
  500. if (! $output_preformatted) {
  501. print $lineprefix, $blankline;
  502. }
  503. } else {
  504. if ($output_mode eq "man" && substr($line, 0, 1) eq ".") {
  505. print "\\&$line";
  506. } else {
  507. print $lineprefix, $line;
  508. }
  509. }
  510. print "\n";
  511. }
  512. }
  513. ##
  514. # output function in man
  515. sub output_function_man(%) {
  516. my %args = %{$_[0]};
  517. my ($parameter, $section);
  518. my $count;
  519. print ".TH \"$args{'function'}\" 9 \"$args{'function'}\" \"$man_date\" \"Kernel Hacker's Manual\" LINUX\n";
  520. print ".SH NAME\n";
  521. print $args{'function'} . " \\- " . $args{'purpose'} . "\n";
  522. print ".SH SYNOPSIS\n";
  523. if ($args{'functiontype'} ne "") {
  524. print ".B \"" . $args{'functiontype'} . "\" " . $args{'function'} . "\n";
  525. } else {
  526. print ".B \"" . $args{'function'} . "\n";
  527. }
  528. $count = 0;
  529. my $parenth = "(";
  530. my $post = ",";
  531. foreach my $parameter (@{$args{'parameterlist'}}) {
  532. if ($count == $#{$args{'parameterlist'}}) {
  533. $post = ");";
  534. }
  535. $type = $args{'parametertypes'}{$parameter};
  536. if ($type =~ m/([^\(]*\(\*)\s*\)\s*\(([^\)]*)\)/) {
  537. # pointer-to-function
  538. print ".BI \"" . $parenth . $1 . "\" " . $parameter . " \") (" . $2 . ")" . $post . "\"\n";
  539. } else {
  540. $type =~ s/([^\*])$/$1 /;
  541. print ".BI \"" . $parenth . $type . "\" " . $parameter . " \"" . $post . "\"\n";
  542. }
  543. $count++;
  544. $parenth = "";
  545. }
  546. print ".SH ARGUMENTS\n";
  547. foreach $parameter (@{$args{'parameterlist'}}) {
  548. my $parameter_name = $parameter;
  549. $parameter_name =~ s/\[.*//;
  550. print ".IP \"" . $parameter . "\" 12\n";
  551. output_highlight($args{'parameterdescs'}{$parameter_name});
  552. }
  553. foreach $section (@{$args{'sectionlist'}}) {
  554. print ".SH \"", uc $section, "\"\n";
  555. output_highlight($args{'sections'}{$section});
  556. }
  557. }
  558. ##
  559. # output enum in man
  560. sub output_enum_man(%) {
  561. my %args = %{$_[0]};
  562. my ($parameter, $section);
  563. my $count;
  564. print ".TH \"$args{'module'}\" 9 \"enum $args{'enum'}\" \"$man_date\" \"API Manual\" LINUX\n";
  565. print ".SH NAME\n";
  566. print "enum " . $args{'enum'} . " \\- " . $args{'purpose'} . "\n";
  567. print ".SH SYNOPSIS\n";
  568. print "enum " . $args{'enum'} . " {\n";
  569. $count = 0;
  570. foreach my $parameter (@{$args{'parameterlist'}}) {
  571. print ".br\n.BI \" $parameter\"\n";
  572. if ($count == $#{$args{'parameterlist'}}) {
  573. print "\n};\n";
  574. last;
  575. }
  576. else {
  577. print ", \n.br\n";
  578. }
  579. $count++;
  580. }
  581. print ".SH Constants\n";
  582. foreach $parameter (@{$args{'parameterlist'}}) {
  583. my $parameter_name = $parameter;
  584. $parameter_name =~ s/\[.*//;
  585. print ".IP \"" . $parameter . "\" 12\n";
  586. output_highlight($args{'parameterdescs'}{$parameter_name});
  587. }
  588. foreach $section (@{$args{'sectionlist'}}) {
  589. print ".SH \"$section\"\n";
  590. output_highlight($args{'sections'}{$section});
  591. }
  592. }
  593. ##
  594. # output struct in man
  595. sub output_struct_man(%) {
  596. my %args = %{$_[0]};
  597. my ($parameter, $section);
  598. print ".TH \"$args{'module'}\" 9 \"" . $args{'type'} . " " . $args{'struct'} . "\" \"$man_date\" \"API Manual\" LINUX\n";
  599. print ".SH NAME\n";
  600. print $args{'type'} . " " . $args{'struct'} . " \\- " . $args{'purpose'} . "\n";
  601. my $declaration = $args{'definition'};
  602. $declaration =~ s/\t/ /g;
  603. $declaration =~ s/\n/"\n.br\n.BI \"/g;
  604. print ".SH SYNOPSIS\n";
  605. print $args{'type'} . " " . $args{'struct'} . " {\n.br\n";
  606. print ".BI \"$declaration\n};\n.br\n\n";
  607. print ".SH Members\n";
  608. foreach $parameter (@{$args{'parameterlist'}}) {
  609. ($parameter =~ /^#/) && next;
  610. my $parameter_name = $parameter;
  611. $parameter_name =~ s/\[.*//;
  612. ($args{'parameterdescs'}{$parameter_name} ne $undescribed) || next;
  613. print ".IP \"" . $parameter . "\" 12\n";
  614. output_highlight($args{'parameterdescs'}{$parameter_name});
  615. }
  616. foreach $section (@{$args{'sectionlist'}}) {
  617. print ".SH \"$section\"\n";
  618. output_highlight($args{'sections'}{$section});
  619. }
  620. }
  621. ##
  622. # output typedef in man
  623. sub output_typedef_man(%) {
  624. my %args = %{$_[0]};
  625. my ($parameter, $section);
  626. print ".TH \"$args{'module'}\" 9 \"$args{'typedef'}\" \"$man_date\" \"API Manual\" LINUX\n";
  627. print ".SH NAME\n";
  628. print "typedef " . $args{'typedef'} . " \\- " . $args{'purpose'} . "\n";
  629. foreach $section (@{$args{'sectionlist'}}) {
  630. print ".SH \"$section\"\n";
  631. output_highlight($args{'sections'}{$section});
  632. }
  633. }
  634. sub output_blockhead_man(%) {
  635. my %args = %{$_[0]};
  636. my ($parameter, $section);
  637. my $count;
  638. print ".TH \"$args{'module'}\" 9 \"$args{'module'}\" \"$man_date\" \"API Manual\" LINUX\n";
  639. foreach $section (@{$args{'sectionlist'}}) {
  640. print ".SH \"$section\"\n";
  641. output_highlight($args{'sections'}{$section});
  642. }
  643. }
  644. ##
  645. # output in restructured text
  646. #
  647. #
  648. # This could use some work; it's used to output the DOC: sections, and
  649. # starts by putting out the name of the doc section itself, but that tends
  650. # to duplicate a header already in the template file.
  651. #
  652. sub output_blockhead_rst(%) {
  653. my %args = %{$_[0]};
  654. my ($parameter, $section);
  655. foreach $section (@{$args{'sectionlist'}}) {
  656. if ($output_selection != OUTPUT_INCLUDE) {
  657. print "**$section**\n\n";
  658. }
  659. print_lineno($section_start_lines{$section});
  660. output_highlight_rst($args{'sections'}{$section});
  661. print "\n";
  662. }
  663. }
  664. sub output_highlight_rst {
  665. my $contents = join "\n",@_;
  666. my $line;
  667. eval $dohighlight;
  668. die $@ if $@;
  669. foreach $line (split "\n", $contents) {
  670. print $lineprefix . $line . "\n";
  671. }
  672. }
  673. sub output_function_rst(%) {
  674. my %args = %{$_[0]};
  675. my ($parameter, $section);
  676. my $oldprefix = $lineprefix;
  677. my $start = "";
  678. if ($args{'typedef'}) {
  679. print ".. c:type:: ". $args{'function'} . "\n\n";
  680. print_lineno($declaration_start_line);
  681. print " **Typedef**: ";
  682. $lineprefix = "";
  683. output_highlight_rst($args{'purpose'});
  684. $start = "\n\n**Syntax**\n\n ``";
  685. } else {
  686. print ".. c:function:: ";
  687. }
  688. if ($args{'functiontype'} ne "") {
  689. $start .= $args{'functiontype'} . " " . $args{'function'} . " (";
  690. } else {
  691. $start .= $args{'function'} . " (";
  692. }
  693. print $start;
  694. my $count = 0;
  695. foreach my $parameter (@{$args{'parameterlist'}}) {
  696. if ($count ne 0) {
  697. print ", ";
  698. }
  699. $count++;
  700. $type = $args{'parametertypes'}{$parameter};
  701. if ($type =~ m/([^\(]*\(\*)\s*\)\s*\(([^\)]*)\)/) {
  702. # pointer-to-function
  703. print $1 . $parameter . ") (" . $2;
  704. } else {
  705. print $type . " " . $parameter;
  706. }
  707. }
  708. if ($args{'typedef'}) {
  709. print ");``\n\n";
  710. } else {
  711. print ")\n\n";
  712. print_lineno($declaration_start_line);
  713. $lineprefix = " ";
  714. output_highlight_rst($args{'purpose'});
  715. print "\n";
  716. }
  717. print "**Parameters**\n\n";
  718. $lineprefix = " ";
  719. foreach $parameter (@{$args{'parameterlist'}}) {
  720. my $parameter_name = $parameter;
  721. $parameter_name =~ s/\[.*//;
  722. $type = $args{'parametertypes'}{$parameter};
  723. if ($type ne "") {
  724. print "``$type $parameter``\n";
  725. } else {
  726. print "``$parameter``\n";
  727. }
  728. print_lineno($parameterdesc_start_lines{$parameter_name});
  729. if (defined($args{'parameterdescs'}{$parameter_name}) &&
  730. $args{'parameterdescs'}{$parameter_name} ne $undescribed) {
  731. output_highlight_rst($args{'parameterdescs'}{$parameter_name});
  732. } else {
  733. print " *undescribed*\n";
  734. }
  735. print "\n";
  736. }
  737. $lineprefix = $oldprefix;
  738. output_section_rst(@_);
  739. }
  740. sub output_section_rst(%) {
  741. my %args = %{$_[0]};
  742. my $section;
  743. my $oldprefix = $lineprefix;
  744. $lineprefix = "";
  745. foreach $section (@{$args{'sectionlist'}}) {
  746. print "**$section**\n\n";
  747. print_lineno($section_start_lines{$section});
  748. output_highlight_rst($args{'sections'}{$section});
  749. print "\n";
  750. }
  751. print "\n";
  752. $lineprefix = $oldprefix;
  753. }
  754. sub output_enum_rst(%) {
  755. my %args = %{$_[0]};
  756. my ($parameter);
  757. my $oldprefix = $lineprefix;
  758. my $count;
  759. my $name = "enum " . $args{'enum'};
  760. print "\n\n.. c:type:: " . $name . "\n\n";
  761. print_lineno($declaration_start_line);
  762. $lineprefix = " ";
  763. output_highlight_rst($args{'purpose'});
  764. print "\n";
  765. print "**Constants**\n\n";
  766. $lineprefix = " ";
  767. foreach $parameter (@{$args{'parameterlist'}}) {
  768. print "``$parameter``\n";
  769. if ($args{'parameterdescs'}{$parameter} ne $undescribed) {
  770. output_highlight_rst($args{'parameterdescs'}{$parameter});
  771. } else {
  772. print " *undescribed*\n";
  773. }
  774. print "\n";
  775. }
  776. $lineprefix = $oldprefix;
  777. output_section_rst(@_);
  778. }
  779. sub output_typedef_rst(%) {
  780. my %args = %{$_[0]};
  781. my ($parameter);
  782. my $oldprefix = $lineprefix;
  783. my $name = "typedef " . $args{'typedef'};
  784. print "\n\n.. c:type:: " . $name . "\n\n";
  785. print_lineno($declaration_start_line);
  786. $lineprefix = " ";
  787. output_highlight_rst($args{'purpose'});
  788. print "\n";
  789. $lineprefix = $oldprefix;
  790. output_section_rst(@_);
  791. }
  792. sub output_struct_rst(%) {
  793. my %args = %{$_[0]};
  794. my ($parameter);
  795. my $oldprefix = $lineprefix;
  796. my $name = $args{'type'} . " " . $args{'struct'};
  797. print "\n\n.. c:type:: " . $name . "\n\n";
  798. print_lineno($declaration_start_line);
  799. $lineprefix = " ";
  800. output_highlight_rst($args{'purpose'});
  801. print "\n";
  802. print "**Definition**\n\n";
  803. print "::\n\n";
  804. my $declaration = $args{'definition'};
  805. $declaration =~ s/\t/ /g;
  806. print " " . $args{'type'} . " " . $args{'struct'} . " {\n$declaration };\n\n";
  807. print "**Members**\n\n";
  808. $lineprefix = " ";
  809. foreach $parameter (@{$args{'parameterlist'}}) {
  810. ($parameter =~ /^#/) && next;
  811. my $parameter_name = $parameter;
  812. $parameter_name =~ s/\[.*//;
  813. ($args{'parameterdescs'}{$parameter_name} ne $undescribed) || next;
  814. $type = $args{'parametertypes'}{$parameter};
  815. print_lineno($parameterdesc_start_lines{$parameter_name});
  816. print "``" . $parameter . "``\n";
  817. output_highlight_rst($args{'parameterdescs'}{$parameter_name});
  818. print "\n";
  819. }
  820. print "\n";
  821. $lineprefix = $oldprefix;
  822. output_section_rst(@_);
  823. }
  824. ## none mode output functions
  825. sub output_function_none(%) {
  826. }
  827. sub output_enum_none(%) {
  828. }
  829. sub output_typedef_none(%) {
  830. }
  831. sub output_struct_none(%) {
  832. }
  833. sub output_blockhead_none(%) {
  834. }
  835. ##
  836. # generic output function for all types (function, struct/union, typedef, enum);
  837. # calls the generated, variable output_ function name based on
  838. # functype and output_mode
  839. sub output_declaration {
  840. no strict 'refs';
  841. my $name = shift;
  842. my $functype = shift;
  843. my $func = "output_${functype}_$output_mode";
  844. if (($output_selection == OUTPUT_ALL) ||
  845. (($output_selection == OUTPUT_INCLUDE ||
  846. $output_selection == OUTPUT_EXPORTED) &&
  847. defined($function_table{$name})) ||
  848. (($output_selection == OUTPUT_EXCLUDE ||
  849. $output_selection == OUTPUT_INTERNAL) &&
  850. !($functype eq "function" && defined($function_table{$name}))))
  851. {
  852. &$func(@_);
  853. $section_counter++;
  854. }
  855. }
  856. ##
  857. # generic output function - calls the right one based on current output mode.
  858. sub output_blockhead {
  859. no strict 'refs';
  860. my $func = "output_blockhead_" . $output_mode;
  861. &$func(@_);
  862. $section_counter++;
  863. }
  864. ##
  865. # takes a declaration (struct, union, enum, typedef) and
  866. # invokes the right handler. NOT called for functions.
  867. sub dump_declaration($$) {
  868. no strict 'refs';
  869. my ($prototype, $file) = @_;
  870. my $func = "dump_" . $decl_type;
  871. &$func(@_);
  872. }
  873. sub dump_union($$) {
  874. dump_struct(@_);
  875. }
  876. sub dump_struct($$) {
  877. my $x = shift;
  878. my $file = shift;
  879. if ($x =~ /(struct|union)\s+(\w+)\s*{(.*)}/) {
  880. my $decl_type = $1;
  881. $declaration_name = $2;
  882. my $members = $3;
  883. # ignore members marked private:
  884. $members =~ s/\/\*\s*private:.*?\/\*\s*public:.*?\*\///gosi;
  885. $members =~ s/\/\*\s*private:.*//gosi;
  886. # strip comments:
  887. $members =~ s/\/\*.*?\*\///gos;
  888. # strip attributes
  889. $members =~ s/__attribute__\s*\(\([a-z,_\*\s\(\)]*\)\)//i;
  890. $members =~ s/__aligned\s*\([^;]*\)//gos;
  891. $members =~ s/\s*CRYPTO_MINALIGN_ATTR//gos;
  892. # replace DECLARE_BITMAP
  893. $members =~ s/DECLARE_BITMAP\s*\(([^,)]+),\s*([^,)]+)\)/unsigned long $1\[BITS_TO_LONGS($2)\]/gos;
  894. # replace DECLARE_HASHTABLE
  895. $members =~ s/DECLARE_HASHTABLE\s*\(([^,)]+),\s*([^,)]+)\)/unsigned long $1\[1 << (($2) - 1)\]/gos;
  896. # replace DECLARE_KFIFO
  897. $members =~ s/DECLARE_KFIFO\s*\(([^,)]+),\s*([^,)]+),\s*([^,)]+)\)/$2 \*$1/gos;
  898. # replace DECLARE_KFIFO_PTR
  899. $members =~ s/DECLARE_KFIFO_PTR\s*\(([^,)]+),\s*([^,)]+)\)/$2 \*$1/gos;
  900. my $declaration = $members;
  901. # Split nested struct/union elements as newer ones
  902. while ($members =~ m/(struct|union)([^\{\};]+)\{([^\{\}]*)\}([^\{\}\;]*)\;/) {
  903. my $newmember;
  904. my $maintype = $1;
  905. my $ids = $4;
  906. my $content = $3;
  907. foreach my $id(split /,/, $ids) {
  908. $newmember .= "$maintype $id; ";
  909. $id =~ s/[:\[].*//;
  910. $id =~ s/^\s*\**(\S+)\s*/$1/;
  911. foreach my $arg (split /;/, $content) {
  912. next if ($arg =~ m/^\s*$/);
  913. if ($arg =~ m/^([^\(]+\(\*?\s*)([\w\.]*)(\s*\).*)/) {
  914. # pointer-to-function
  915. my $type = $1;
  916. my $name = $2;
  917. my $extra = $3;
  918. next if (!$name);
  919. if ($id =~ m/^\s*$/) {
  920. # anonymous struct/union
  921. $newmember .= "$type$name$extra; ";
  922. } else {
  923. $newmember .= "$type$id.$name$extra; ";
  924. }
  925. } else {
  926. my $type;
  927. my $names;
  928. $arg =~ s/^\s+//;
  929. $arg =~ s/\s+$//;
  930. # Handle bitmaps
  931. $arg =~ s/:\s*\d+\s*//g;
  932. # Handle arrays
  933. $arg =~ s/\[\S+\]//g;
  934. # The type may have multiple words,
  935. # and multiple IDs can be defined, like:
  936. # const struct foo, *bar, foobar
  937. # So, we remove spaces when parsing the
  938. # names, in order to match just names
  939. # and commas for the names
  940. $arg =~ s/\s*,\s*/,/g;
  941. if ($arg =~ m/(.*)\s+([\S+,]+)/) {
  942. $type = $1;
  943. $names = $2;
  944. } else {
  945. $newmember .= "$arg; ";
  946. next;
  947. }
  948. foreach my $name (split /,/, $names) {
  949. $name =~ s/^\s*\**(\S+)\s*/$1/;
  950. next if (($name =~ m/^\s*$/));
  951. if ($id =~ m/^\s*$/) {
  952. # anonymous struct/union
  953. $newmember .= "$type $name; ";
  954. } else {
  955. $newmember .= "$type $id.$name; ";
  956. }
  957. }
  958. }
  959. }
  960. }
  961. $members =~ s/(struct|union)([^\{\};]+)\{([^\{\}]*)}([^\{\}\;]*)\;/$newmember/;
  962. }
  963. # Ignore other nested elements, like enums
  964. $members =~ s/({[^\{\}]*})//g;
  965. create_parameterlist($members, ';', $file, $declaration_name);
  966. check_sections($file, $declaration_name, $decl_type, $sectcheck, $struct_actual);
  967. # Adjust declaration for better display
  968. $declaration =~ s/([{;])/$1\n/g;
  969. $declaration =~ s/}\s+;/};/g;
  970. # Better handle inlined enums
  971. do {} while ($declaration =~ s/(enum\s+{[^}]+),([^\n])/$1,\n$2/);
  972. my @def_args = split /\n/, $declaration;
  973. my $level = 1;
  974. $declaration = "";
  975. foreach my $clause (@def_args) {
  976. $clause =~ s/^\s+//;
  977. $clause =~ s/\s+$//;
  978. $clause =~ s/\s+/ /;
  979. next if (!$clause);
  980. $level-- if ($clause =~ m/(})/ && $level > 1);
  981. if (!($clause =~ m/^\s*#/)) {
  982. $declaration .= "\t" x $level;
  983. }
  984. $declaration .= "\t" . $clause . "\n";
  985. $level++ if ($clause =~ m/({)/ && !($clause =~m/}/));
  986. }
  987. output_declaration($declaration_name,
  988. 'struct',
  989. {'struct' => $declaration_name,
  990. 'module' => $modulename,
  991. 'definition' => $declaration,
  992. 'parameterlist' => \@parameterlist,
  993. 'parameterdescs' => \%parameterdescs,
  994. 'parametertypes' => \%parametertypes,
  995. 'sectionlist' => \@sectionlist,
  996. 'sections' => \%sections,
  997. 'purpose' => $declaration_purpose,
  998. 'type' => $decl_type
  999. });
  1000. }
  1001. else {
  1002. print STDERR "${file}:$.: error: Cannot parse struct or union!\n";
  1003. ++$errors;
  1004. }
  1005. }
  1006. sub show_warnings($$) {
  1007. my $functype = shift;
  1008. my $name = shift;
  1009. return 1 if ($output_selection == OUTPUT_ALL);
  1010. if ($output_selection == OUTPUT_EXPORTED) {
  1011. if (defined($function_table{$name})) {
  1012. return 1;
  1013. } else {
  1014. return 0;
  1015. }
  1016. }
  1017. if ($output_selection == OUTPUT_INTERNAL) {
  1018. if (!($functype eq "function" && defined($function_table{$name}))) {
  1019. return 1;
  1020. } else {
  1021. return 0;
  1022. }
  1023. }
  1024. if ($output_selection == OUTPUT_INCLUDE) {
  1025. if (defined($function_table{$name})) {
  1026. return 1;
  1027. } else {
  1028. return 0;
  1029. }
  1030. }
  1031. if ($output_selection == OUTPUT_EXCLUDE) {
  1032. if (!defined($function_table{$name})) {
  1033. return 1;
  1034. } else {
  1035. return 0;
  1036. }
  1037. }
  1038. die("Please add the new output type at show_warnings()");
  1039. }
  1040. sub dump_enum($$) {
  1041. my $x = shift;
  1042. my $file = shift;
  1043. $x =~ s@/\*.*?\*/@@gos; # strip comments.
  1044. # strip #define macros inside enums
  1045. $x =~ s@#\s*((define|ifdef)\s+|endif)[^;]*;@@gos;
  1046. if ($x =~ /enum\s+(\w+)\s*{(.*)}/) {
  1047. $declaration_name = $1;
  1048. my $members = $2;
  1049. my %_members;
  1050. $members =~ s/\s+$//;
  1051. foreach my $arg (split ',', $members) {
  1052. $arg =~ s/^\s*(\w+).*/$1/;
  1053. push @parameterlist, $arg;
  1054. if (!$parameterdescs{$arg}) {
  1055. $parameterdescs{$arg} = $undescribed;
  1056. if (show_warnings("enum", $declaration_name)) {
  1057. print STDERR "${file}:$.: warning: Enum value '$arg' not described in enum '$declaration_name'\n";
  1058. }
  1059. }
  1060. $_members{$arg} = 1;
  1061. }
  1062. while (my ($k, $v) = each %parameterdescs) {
  1063. if (!exists($_members{$k})) {
  1064. if (show_warnings("enum", $declaration_name)) {
  1065. print STDERR "${file}:$.: warning: Excess enum value '$k' description in '$declaration_name'\n";
  1066. }
  1067. }
  1068. }
  1069. output_declaration($declaration_name,
  1070. 'enum',
  1071. {'enum' => $declaration_name,
  1072. 'module' => $modulename,
  1073. 'parameterlist' => \@parameterlist,
  1074. 'parameterdescs' => \%parameterdescs,
  1075. 'sectionlist' => \@sectionlist,
  1076. 'sections' => \%sections,
  1077. 'purpose' => $declaration_purpose
  1078. });
  1079. }
  1080. else {
  1081. print STDERR "${file}:$.: error: Cannot parse enum!\n";
  1082. ++$errors;
  1083. }
  1084. }
  1085. sub dump_typedef($$) {
  1086. my $x = shift;
  1087. my $file = shift;
  1088. $x =~ s@/\*.*?\*/@@gos; # strip comments.
  1089. # Parse function prototypes
  1090. if ($x =~ /typedef\s+(\w+)\s*\(\*\s*(\w\S+)\s*\)\s*\((.*)\);/ ||
  1091. $x =~ /typedef\s+(\w+)\s*(\w\S+)\s*\s*\((.*)\);/) {
  1092. # Function typedefs
  1093. $return_type = $1;
  1094. $declaration_name = $2;
  1095. my $args = $3;
  1096. create_parameterlist($args, ',', $file, $declaration_name);
  1097. output_declaration($declaration_name,
  1098. 'function',
  1099. {'function' => $declaration_name,
  1100. 'typedef' => 1,
  1101. 'module' => $modulename,
  1102. 'functiontype' => $return_type,
  1103. 'parameterlist' => \@parameterlist,
  1104. 'parameterdescs' => \%parameterdescs,
  1105. 'parametertypes' => \%parametertypes,
  1106. 'sectionlist' => \@sectionlist,
  1107. 'sections' => \%sections,
  1108. 'purpose' => $declaration_purpose
  1109. });
  1110. return;
  1111. }
  1112. while (($x =~ /\(*.\)\s*;$/) || ($x =~ /\[*.\]\s*;$/)) {
  1113. $x =~ s/\(*.\)\s*;$/;/;
  1114. $x =~ s/\[*.\]\s*;$/;/;
  1115. }
  1116. if ($x =~ /typedef.*\s+(\w+)\s*;/) {
  1117. $declaration_name = $1;
  1118. output_declaration($declaration_name,
  1119. 'typedef',
  1120. {'typedef' => $declaration_name,
  1121. 'module' => $modulename,
  1122. 'sectionlist' => \@sectionlist,
  1123. 'sections' => \%sections,
  1124. 'purpose' => $declaration_purpose
  1125. });
  1126. }
  1127. else {
  1128. print STDERR "${file}:$.: error: Cannot parse typedef!\n";
  1129. ++$errors;
  1130. }
  1131. }
  1132. sub save_struct_actual($) {
  1133. my $actual = shift;
  1134. # strip all spaces from the actual param so that it looks like one string item
  1135. $actual =~ s/\s*//g;
  1136. $struct_actual = $struct_actual . $actual . " ";
  1137. }
  1138. sub create_parameterlist($$$$) {
  1139. my $args = shift;
  1140. my $splitter = shift;
  1141. my $file = shift;
  1142. my $declaration_name = shift;
  1143. my $type;
  1144. my $param;
  1145. # temporarily replace commas inside function pointer definition
  1146. while ($args =~ /(\([^\),]+),/) {
  1147. $args =~ s/(\([^\),]+),/$1#/g;
  1148. }
  1149. foreach my $arg (split($splitter, $args)) {
  1150. # strip comments
  1151. $arg =~ s/\/\*.*\*\///;
  1152. # strip leading/trailing spaces
  1153. $arg =~ s/^\s*//;
  1154. $arg =~ s/\s*$//;
  1155. $arg =~ s/\s+/ /;
  1156. if ($arg =~ /^#/) {
  1157. # Treat preprocessor directive as a typeless variable just to fill
  1158. # corresponding data structures "correctly". Catch it later in
  1159. # output_* subs.
  1160. push_parameter($arg, "", $file);
  1161. } elsif ($arg =~ m/\(.+\)\s*\(/) {
  1162. # pointer-to-function
  1163. $arg =~ tr/#/,/;
  1164. $arg =~ m/[^\(]+\(\*?\s*([\w\.]*)\s*\)/;
  1165. $param = $1;
  1166. $type = $arg;
  1167. $type =~ s/([^\(]+\(\*?)\s*$param/$1/;
  1168. save_struct_actual($param);
  1169. push_parameter($param, $type, $file, $declaration_name);
  1170. } elsif ($arg) {
  1171. $arg =~ s/\s*:\s*/:/g;
  1172. $arg =~ s/\s*\[/\[/g;
  1173. my @args = split('\s*,\s*', $arg);
  1174. if ($args[0] =~ m/\*/) {
  1175. $args[0] =~ s/(\*+)\s*/ $1/;
  1176. }
  1177. my @first_arg;
  1178. if ($args[0] =~ /^(.*\s+)(.*?\[.*\].*)$/) {
  1179. shift @args;
  1180. push(@first_arg, split('\s+', $1));
  1181. push(@first_arg, $2);
  1182. } else {
  1183. @first_arg = split('\s+', shift @args);
  1184. }
  1185. unshift(@args, pop @first_arg);
  1186. $type = join " ", @first_arg;
  1187. foreach $param (@args) {
  1188. if ($param =~ m/^(\*+)\s*(.*)/) {
  1189. save_struct_actual($2);
  1190. push_parameter($2, "$type $1", $file, $declaration_name);
  1191. }
  1192. elsif ($param =~ m/(.*?):(\d+)/) {
  1193. if ($type ne "") { # skip unnamed bit-fields
  1194. save_struct_actual($1);
  1195. push_parameter($1, "$type:$2", $file, $declaration_name)
  1196. }
  1197. }
  1198. else {
  1199. save_struct_actual($param);
  1200. push_parameter($param, $type, $file, $declaration_name);
  1201. }
  1202. }
  1203. }
  1204. }
  1205. }
  1206. sub push_parameter($$$$) {
  1207. my $param = shift;
  1208. my $type = shift;
  1209. my $file = shift;
  1210. my $declaration_name = shift;
  1211. if (($anon_struct_union == 1) && ($type eq "") &&
  1212. ($param eq "}")) {
  1213. return; # ignore the ending }; from anon. struct/union
  1214. }
  1215. $anon_struct_union = 0;
  1216. $param =~ s/[\[\)].*//;
  1217. if ($type eq "" && $param =~ /\.\.\.$/)
  1218. {
  1219. if (!$param =~ /\w\.\.\.$/) {
  1220. # handles unnamed variable parameters
  1221. $param = "...";
  1222. }
  1223. if (!defined $parameterdescs{$param} || $parameterdescs{$param} eq "") {
  1224. $parameterdescs{$param} = "variable arguments";
  1225. }
  1226. }
  1227. elsif ($type eq "" && ($param eq "" or $param eq "void"))
  1228. {
  1229. $param="void";
  1230. $parameterdescs{void} = "no arguments";
  1231. }
  1232. elsif ($type eq "" && ($param eq "struct" or $param eq "union"))
  1233. # handle unnamed (anonymous) union or struct:
  1234. {
  1235. $type = $param;
  1236. $param = "{unnamed_" . $param . "}";
  1237. $parameterdescs{$param} = "anonymous\n";
  1238. $anon_struct_union = 1;
  1239. }
  1240. # warn if parameter has no description
  1241. # (but ignore ones starting with # as these are not parameters
  1242. # but inline preprocessor statements);
  1243. # Note: It will also ignore void params and unnamed structs/unions
  1244. if (!defined $parameterdescs{$param} && $param !~ /^#/) {
  1245. $parameterdescs{$param} = $undescribed;
  1246. if (show_warnings($type, $declaration_name)) {
  1247. print STDERR
  1248. "${file}:$.: warning: Function parameter or member '$param' not described in '$declaration_name'\n";
  1249. ++$warnings;
  1250. }
  1251. }
  1252. # strip spaces from $param so that it is one continuous string
  1253. # on @parameterlist;
  1254. # this fixes a problem where check_sections() cannot find
  1255. # a parameter like "addr[6 + 2]" because it actually appears
  1256. # as "addr[6", "+", "2]" on the parameter list;
  1257. # but it's better to maintain the param string unchanged for output,
  1258. # so just weaken the string compare in check_sections() to ignore
  1259. # "[blah" in a parameter string;
  1260. ###$param =~ s/\s*//g;
  1261. push @parameterlist, $param;
  1262. $type =~ s/\s\s+/ /g;
  1263. $parametertypes{$param} = $type;
  1264. }
  1265. sub check_sections($$$$$) {
  1266. my ($file, $decl_name, $decl_type, $sectcheck, $prmscheck) = @_;
  1267. my @sects = split ' ', $sectcheck;
  1268. my @prms = split ' ', $prmscheck;
  1269. my $err;
  1270. my ($px, $sx);
  1271. my $prm_clean; # strip trailing "[array size]" and/or beginning "*"
  1272. foreach $sx (0 .. $#sects) {
  1273. $err = 1;
  1274. foreach $px (0 .. $#prms) {
  1275. $prm_clean = $prms[$px];
  1276. $prm_clean =~ s/\[.*\]//;
  1277. $prm_clean =~ s/__attribute__\s*\(\([a-z,_\*\s\(\)]*\)\)//i;
  1278. # ignore array size in a parameter string;
  1279. # however, the original param string may contain
  1280. # spaces, e.g.: addr[6 + 2]
  1281. # and this appears in @prms as "addr[6" since the
  1282. # parameter list is split at spaces;
  1283. # hence just ignore "[..." for the sections check;
  1284. $prm_clean =~ s/\[.*//;
  1285. ##$prm_clean =~ s/^\**//;
  1286. if ($prm_clean eq $sects[$sx]) {
  1287. $err = 0;
  1288. last;
  1289. }
  1290. }
  1291. if ($err) {
  1292. if ($decl_type eq "function") {
  1293. print STDERR "${file}:$.: warning: " .
  1294. "Excess function parameter " .
  1295. "'$sects[$sx]' " .
  1296. "description in '$decl_name'\n";
  1297. ++$warnings;
  1298. }
  1299. }
  1300. }
  1301. }
  1302. ##
  1303. # Checks the section describing the return value of a function.
  1304. sub check_return_section {
  1305. my $file = shift;
  1306. my $declaration_name = shift;
  1307. my $return_type = shift;
  1308. # Ignore an empty return type (It's a macro)
  1309. # Ignore functions with a "void" return type. (But don't ignore "void *")
  1310. if (($return_type eq "") || ($return_type =~ /void\s*\w*\s*$/)) {
  1311. return;
  1312. }
  1313. if (!defined($sections{$section_return}) ||
  1314. $sections{$section_return} eq "") {
  1315. print STDERR "${file}:$.: warning: " .
  1316. "No description found for return value of " .
  1317. "'$declaration_name'\n";
  1318. ++$warnings;
  1319. }
  1320. }
  1321. ##
  1322. # takes a function prototype and the name of the current file being
  1323. # processed and spits out all the details stored in the global
  1324. # arrays/hashes.
  1325. sub dump_function($$) {
  1326. my $prototype = shift;
  1327. my $file = shift;
  1328. my $noret = 0;
  1329. $prototype =~ s/^static +//;
  1330. $prototype =~ s/^extern +//;
  1331. $prototype =~ s/^asmlinkage +//;
  1332. $prototype =~ s/^inline +//;
  1333. $prototype =~ s/^__inline__ +//;
  1334. $prototype =~ s/^__inline +//;
  1335. $prototype =~ s/^__always_inline +//;
  1336. $prototype =~ s/^noinline +//;
  1337. $prototype =~ s/__init +//;
  1338. $prototype =~ s/__init_or_module +//;
  1339. $prototype =~ s/__meminit +//;
  1340. $prototype =~ s/__must_check +//;
  1341. $prototype =~ s/__weak +//;
  1342. my $define = $prototype =~ s/^#\s*define\s+//; #ak added
  1343. $prototype =~ s/__attribute__\s*\(\(
  1344. (?:
  1345. [\w\s]++ # attribute name
  1346. (?:\([^)]*+\))? # attribute arguments
  1347. \s*+,? # optional comma at the end
  1348. )+
  1349. \)\)\s+//x;
  1350. # Yes, this truly is vile. We are looking for:
  1351. # 1. Return type (may be nothing if we're looking at a macro)
  1352. # 2. Function name
  1353. # 3. Function parameters.
  1354. #
  1355. # All the while we have to watch out for function pointer parameters
  1356. # (which IIRC is what the two sections are for), C types (these
  1357. # regexps don't even start to express all the possibilities), and
  1358. # so on.
  1359. #
  1360. # If you mess with these regexps, it's a good idea to check that
  1361. # the following functions' documentation still comes out right:
  1362. # - parport_register_device (function pointer parameters)
  1363. # - atomic_set (macro)
  1364. # - pci_match_device, __copy_to_user (long return type)
  1365. if ($define && $prototype =~ m/^()([a-zA-Z0-9_~:]+)\s+/) {
  1366. # This is an object-like macro, it has no return type and no parameter
  1367. # list.
  1368. # Function-like macros are not allowed to have spaces between
  1369. # declaration_name and opening parenthesis (notice the \s+).
  1370. $return_type = $1;
  1371. $declaration_name = $2;
  1372. $noret = 1;
  1373. } elsif ($prototype =~ m/^()([a-zA-Z0-9_~:]+)\s*\(([^\(]*)\)/ ||
  1374. $prototype =~ m/^(\w+)\s+([a-zA-Z0-9_~:]+)\s*\(([^\(]*)\)/ ||
  1375. $prototype =~ m/^(\w+\s*\*+)\s*([a-zA-Z0-9_~:]+)\s*\(([^\(]*)\)/ ||
  1376. $prototype =~ m/^(\w+\s+\w+)\s+([a-zA-Z0-9_~:]+)\s*\(([^\(]*)\)/ ||
  1377. $prototype =~ m/^(\w+\s+\w+\s*\*+)\s*([a-zA-Z0-9_~:]+)\s*\(([^\(]*)\)/ ||
  1378. $prototype =~ m/^(\w+\s+\w+\s+\w+)\s+([a-zA-Z0-9_~:]+)\s*\(([^\(]*)\)/ ||
  1379. $prototype =~ m/^(\w+\s+\w+\s+\w+\s*\*+)\s*([a-zA-Z0-9_~:]+)\s*\(([^\(]*)\)/ ||
  1380. $prototype =~ m/^()([a-zA-Z0-9_~:]+)\s*\(([^\{]*)\)/ ||
  1381. $prototype =~ m/^(\w+)\s+([a-zA-Z0-9_~:]+)\s*\(([^\{]*)\)/ ||
  1382. $prototype =~ m/^(\w+\s*\*+)\s*([a-zA-Z0-9_~:]+)\s*\(([^\{]*)\)/ ||
  1383. $prototype =~ m/^(\w+\s+\w+)\s+([a-zA-Z0-9_~:]+)\s*\(([^\{]*)\)/ ||
  1384. $prototype =~ m/^(\w+\s+\w+\s*\*+)\s*([a-zA-Z0-9_~:]+)\s*\(([^\{]*)\)/ ||
  1385. $prototype =~ m/^(\w+\s+\w+\s+\w+)\s+([a-zA-Z0-9_~:]+)\s*\(([^\{]*)\)/ ||
  1386. $prototype =~ m/^(\w+\s+\w+\s+\w+\s*\*+)\s*([a-zA-Z0-9_~:]+)\s*\(([^\{]*)\)/ ||
  1387. $prototype =~ m/^(\w+\s+\w+\s+\w+\s+\w+)\s+([a-zA-Z0-9_~:]+)\s*\(([^\{]*)\)/ ||
  1388. $prototype =~ m/^(\w+\s+\w+\s+\w+\s+\w+\s*\*+)\s*([a-zA-Z0-9_~:]+)\s*\(([^\{]*)\)/ ||
  1389. $prototype =~ m/^(\w+\s+\w+\s*\*+\s*\w+\s*\*+\s*)\s*([a-zA-Z0-9_~:]+)\s*\(([^\{]*)\)/) {
  1390. $return_type = $1;
  1391. $declaration_name = $2;
  1392. my $args = $3;
  1393. create_parameterlist($args, ',', $file, $declaration_name);
  1394. } else {
  1395. print STDERR "${file}:$.: warning: cannot understand function prototype: '$prototype'\n";
  1396. return;
  1397. }
  1398. my $prms = join " ", @parameterlist;
  1399. check_sections($file, $declaration_name, "function", $sectcheck, $prms);
  1400. # This check emits a lot of warnings at the moment, because many
  1401. # functions don't have a 'Return' doc section. So until the number
  1402. # of warnings goes sufficiently down, the check is only performed in
  1403. # verbose mode.
  1404. # TODO: always perform the check.
  1405. if ($verbose && !$noret) {
  1406. check_return_section($file, $declaration_name, $return_type);
  1407. }
  1408. output_declaration($declaration_name,
  1409. 'function',
  1410. {'function' => $declaration_name,
  1411. 'module' => $modulename,
  1412. 'functiontype' => $return_type,
  1413. 'parameterlist' => \@parameterlist,
  1414. 'parameterdescs' => \%parameterdescs,
  1415. 'parametertypes' => \%parametertypes,
  1416. 'sectionlist' => \@sectionlist,
  1417. 'sections' => \%sections,
  1418. 'purpose' => $declaration_purpose
  1419. });
  1420. }
  1421. sub reset_state {
  1422. $function = "";
  1423. %parameterdescs = ();
  1424. %parametertypes = ();
  1425. @parameterlist = ();
  1426. %sections = ();
  1427. @sectionlist = ();
  1428. $sectcheck = "";
  1429. $struct_actual = "";
  1430. $prototype = "";
  1431. $state = STATE_NORMAL;
  1432. $inline_doc_state = STATE_INLINE_NA;
  1433. }
  1434. sub tracepoint_munge($) {
  1435. my $file = shift;
  1436. my $tracepointname = 0;
  1437. my $tracepointargs = 0;
  1438. if ($prototype =~ m/TRACE_EVENT\((.*?),/) {
  1439. $tracepointname = $1;
  1440. }
  1441. if ($prototype =~ m/DEFINE_SINGLE_EVENT\((.*?),/) {
  1442. $tracepointname = $1;
  1443. }
  1444. if ($prototype =~ m/DEFINE_EVENT\((.*?),(.*?),/) {
  1445. $tracepointname = $2;
  1446. }
  1447. $tracepointname =~ s/^\s+//; #strip leading whitespace
  1448. if ($prototype =~ m/TP_PROTO\((.*?)\)/) {
  1449. $tracepointargs = $1;
  1450. }
  1451. if (($tracepointname eq 0) || ($tracepointargs eq 0)) {
  1452. print STDERR "${file}:$.: warning: Unrecognized tracepoint format: \n".
  1453. "$prototype\n";
  1454. } else {
  1455. $prototype = "static inline void trace_$tracepointname($tracepointargs)";
  1456. }
  1457. }
  1458. sub syscall_munge() {
  1459. my $void = 0;
  1460. $prototype =~ s@[\r\n]+@ @gos; # strip newlines/CR's
  1461. ## if ($prototype =~ m/SYSCALL_DEFINE0\s*\(\s*(a-zA-Z0-9_)*\s*\)/) {
  1462. if ($prototype =~ m/SYSCALL_DEFINE0/) {
  1463. $void = 1;
  1464. ## $prototype = "long sys_$1(void)";
  1465. }
  1466. $prototype =~ s/SYSCALL_DEFINE.*\(/long sys_/; # fix return type & func name
  1467. if ($prototype =~ m/long (sys_.*?),/) {
  1468. $prototype =~ s/,/\(/;
  1469. } elsif ($void) {
  1470. $prototype =~ s/\)/\(void\)/;
  1471. }
  1472. # now delete all of the odd-number commas in $prototype
  1473. # so that arg types & arg names don't have a comma between them
  1474. my $count = 0;
  1475. my $len = length($prototype);
  1476. if ($void) {
  1477. $len = 0; # skip the for-loop
  1478. }
  1479. for (my $ix = 0; $ix < $len; $ix++) {
  1480. if (substr($prototype, $ix, 1) eq ',') {
  1481. $count++;
  1482. if ($count % 2 == 1) {
  1483. substr($prototype, $ix, 1) = ' ';
  1484. }
  1485. }
  1486. }
  1487. }
  1488. sub process_proto_function($$) {
  1489. my $x = shift;
  1490. my $file = shift;
  1491. $x =~ s@\/\/.*$@@gos; # strip C99-style comments to end of line
  1492. if ($x =~ m#\s*/\*\s+MACDOC\s*#io || ($x =~ /^#/ && $x !~ /^#\s*define/)) {
  1493. # do nothing
  1494. }
  1495. elsif ($x =~ /([^\{]*)/) {
  1496. $prototype .= $1;
  1497. }
  1498. if (($x =~ /\{/) || ($x =~ /\#\s*define/) || ($x =~ /;/)) {
  1499. $prototype =~ s@/\*.*?\*/@@gos; # strip comments.
  1500. $prototype =~ s@[\r\n]+@ @gos; # strip newlines/cr's.
  1501. $prototype =~ s@^\s+@@gos; # strip leading spaces
  1502. if ($prototype =~ /SYSCALL_DEFINE/) {
  1503. syscall_munge();
  1504. }
  1505. if ($prototype =~ /TRACE_EVENT/ || $prototype =~ /DEFINE_EVENT/ ||
  1506. $prototype =~ /DEFINE_SINGLE_EVENT/)
  1507. {
  1508. tracepoint_munge($file);
  1509. }
  1510. dump_function($prototype, $file);
  1511. reset_state();
  1512. }
  1513. }
  1514. sub process_proto_type($$) {
  1515. my $x = shift;
  1516. my $file = shift;
  1517. $x =~ s@[\r\n]+@ @gos; # strip newlines/cr's.
  1518. $x =~ s@^\s+@@gos; # strip leading spaces
  1519. $x =~ s@\s+$@@gos; # strip trailing spaces
  1520. $x =~ s@\/\/.*$@@gos; # strip C99-style comments to end of line
  1521. if ($x =~ /^#/) {
  1522. # To distinguish preprocessor directive from regular declaration later.
  1523. $x .= ";";
  1524. }
  1525. while (1) {
  1526. if ( $x =~ /([^{};]*)([{};])(.*)/ ) {
  1527. if( length $prototype ) {
  1528. $prototype .= " "
  1529. }
  1530. $prototype .= $1 . $2;
  1531. ($2 eq '{') && $brcount++;
  1532. ($2 eq '}') && $brcount--;
  1533. if (($2 eq ';') && ($brcount == 0)) {
  1534. dump_declaration($prototype, $file);
  1535. reset_state();
  1536. last;
  1537. }
  1538. $x = $3;
  1539. } else {
  1540. $prototype .= $x;
  1541. last;
  1542. }
  1543. }
  1544. }
  1545. sub map_filename($) {
  1546. my $file;
  1547. my ($orig_file) = @_;
  1548. if (defined($ENV{'SRCTREE'})) {
  1549. $file = "$ENV{'SRCTREE'}" . "/" . $orig_file;
  1550. } else {
  1551. $file = $orig_file;
  1552. }
  1553. if (defined($source_map{$file})) {
  1554. $file = $source_map{$file};
  1555. }
  1556. return $file;
  1557. }
  1558. sub process_export_file($) {
  1559. my ($orig_file) = @_;
  1560. my $file = map_filename($orig_file);
  1561. if (!open(IN,"<$file")) {
  1562. print STDERR "Error: Cannot open file $file\n";
  1563. ++$errors;
  1564. return;
  1565. }
  1566. while (<IN>) {
  1567. if (/$export_symbol/) {
  1568. $function_table{$2} = 1;
  1569. }
  1570. }
  1571. close(IN);
  1572. }
  1573. #
  1574. # Parsers for the various processing states.
  1575. #
  1576. # STATE_NORMAL: looking for the /** to begin everything.
  1577. #
  1578. sub process_normal() {
  1579. if (/$doc_start/o) {
  1580. $state = STATE_NAME; # next line is always the function name
  1581. $in_doc_sect = 0;
  1582. $declaration_start_line = $. + 1;
  1583. }
  1584. }
  1585. #
  1586. # STATE_NAME: Looking for the "name - description" line
  1587. #
  1588. sub process_name($$) {
  1589. my $file = shift;
  1590. my $identifier;
  1591. my $descr;
  1592. if (/$doc_block/o) {
  1593. $state = STATE_DOCBLOCK;
  1594. $contents = "";
  1595. $new_start_line = $. + 1;
  1596. if ( $1 eq "" ) {
  1597. $section = $section_intro;
  1598. } else {
  1599. $section = $1;
  1600. }
  1601. }
  1602. elsif (/$doc_decl/o) {
  1603. $identifier = $1;
  1604. if (/\s*([\w\s]+?)\s*-/) {
  1605. $identifier = $1;
  1606. }
  1607. $state = STATE_BODY;
  1608. # if there's no @param blocks need to set up default section
  1609. # here
  1610. $contents = "";
  1611. $section = $section_default;
  1612. $new_start_line = $. + 1;
  1613. if (/-(.*)/) {
  1614. # strip leading/trailing/multiple spaces
  1615. $descr= $1;
  1616. $descr =~ s/^\s*//;
  1617. $descr =~ s/\s*$//;
  1618. $descr =~ s/\s+/ /g;
  1619. $declaration_purpose = $descr;
  1620. $state = STATE_BODY_MAYBE;
  1621. } else {
  1622. $declaration_purpose = "";
  1623. }
  1624. if (($declaration_purpose eq "") && $verbose) {
  1625. print STDERR "${file}:$.: warning: missing initial short description on line:\n";
  1626. print STDERR $_;
  1627. ++$warnings;
  1628. }
  1629. if ($identifier =~ m/^struct/) {
  1630. $decl_type = 'struct';
  1631. } elsif ($identifier =~ m/^union/) {
  1632. $decl_type = 'union';
  1633. } elsif ($identifier =~ m/^enum/) {
  1634. $decl_type = 'enum';
  1635. } elsif ($identifier =~ m/^typedef/) {
  1636. $decl_type = 'typedef';
  1637. } else {
  1638. $decl_type = 'function';
  1639. }
  1640. if ($verbose) {
  1641. print STDERR "${file}:$.: info: Scanning doc for $identifier\n";
  1642. }
  1643. } else {
  1644. print STDERR "${file}:$.: warning: Cannot understand $_ on line $.",
  1645. " - I thought it was a doc line\n";
  1646. ++$warnings;
  1647. $state = STATE_NORMAL;
  1648. }
  1649. }
  1650. #
  1651. # STATE_BODY and STATE_BODY_MAYBE: the bulk of a kerneldoc comment.
  1652. #
  1653. sub process_body($$) {
  1654. my $file = shift;
  1655. if (/$doc_sect/i) { # case insensitive for supported section names
  1656. $newsection = $1;
  1657. $newcontents = $2;
  1658. # map the supported section names to the canonical names
  1659. if ($newsection =~ m/^description$/i) {
  1660. $newsection = $section_default;
  1661. } elsif ($newsection =~ m/^context$/i) {
  1662. $newsection = $section_context;
  1663. } elsif ($newsection =~ m/^returns?$/i) {
  1664. $newsection = $section_return;
  1665. } elsif ($newsection =~ m/^\@return$/) {
  1666. # special: @return is a section, not a param description
  1667. $newsection = $section_return;
  1668. }
  1669. if (($contents ne "") && ($contents ne "\n")) {
  1670. if (!$in_doc_sect && $verbose) {
  1671. print STDERR "${file}:$.: warning: contents before sections\n";
  1672. ++$warnings;
  1673. }
  1674. dump_section($file, $section, $contents);
  1675. $section = $section_default;
  1676. }
  1677. $in_doc_sect = 1;
  1678. $state = STATE_BODY;
  1679. $contents = $newcontents;
  1680. $new_start_line = $.;
  1681. while (substr($contents, 0, 1) eq " ") {
  1682. $contents = substr($contents, 1);
  1683. }
  1684. if ($contents ne "") {
  1685. $contents .= "\n";
  1686. }
  1687. $section = $newsection;
  1688. $leading_space = undef;
  1689. } elsif (/$doc_end/) {
  1690. if (($contents ne "") && ($contents ne "\n")) {
  1691. dump_section($file, $section, $contents);
  1692. $section = $section_default;
  1693. $contents = "";
  1694. }
  1695. # look for doc_com + <text> + doc_end:
  1696. if ($_ =~ m'\s*\*\s*[a-zA-Z_0-9:\.]+\*/') {
  1697. print STDERR "${file}:$.: warning: suspicious ending line: $_";
  1698. ++$warnings;
  1699. }
  1700. $prototype = "";
  1701. $state = STATE_PROTO;
  1702. $brcount = 0;
  1703. } elsif (/$doc_content/) {
  1704. # miguel-style comment kludge, look for blank lines after
  1705. # @parameter line to signify start of description
  1706. if ($1 eq "") {
  1707. if ($section =~ m/^@/ || $section eq $section_context) {
  1708. dump_section($file, $section, $contents);
  1709. $section = $section_default;
  1710. $contents = "";
  1711. $new_start_line = $.;
  1712. } else {
  1713. $contents .= "\n";
  1714. }
  1715. $state = STATE_BODY;
  1716. } elsif ($state == STATE_BODY_MAYBE) {
  1717. # Continued declaration purpose
  1718. chomp($declaration_purpose);
  1719. $declaration_purpose .= " " . $1;
  1720. $declaration_purpose =~ s/\s+/ /g;
  1721. } else {
  1722. my $cont = $1;
  1723. if ($section =~ m/^@/ || $section eq $section_context) {
  1724. if (!defined $leading_space) {
  1725. if ($cont =~ m/^(\s+)/) {
  1726. $leading_space = $1;
  1727. } else {
  1728. $leading_space = "";
  1729. }
  1730. }
  1731. $cont =~ s/^$leading_space//;
  1732. }
  1733. $contents .= $cont . "\n";
  1734. }
  1735. } else {
  1736. # i dont know - bad line? ignore.
  1737. print STDERR "${file}:$.: warning: bad line: $_";
  1738. ++$warnings;
  1739. }
  1740. }
  1741. #
  1742. # STATE_PROTO: reading a function/whatever prototype.
  1743. #
  1744. sub process_proto($$) {
  1745. my $file = shift;
  1746. if (/$doc_inline_oneline/) {
  1747. $section = $1;
  1748. $contents = $2;
  1749. if ($contents ne "") {
  1750. $contents .= "\n";
  1751. dump_section($file, $section, $contents);
  1752. $section = $section_default;
  1753. $contents = "";
  1754. }
  1755. } elsif (/$doc_inline_start/) {
  1756. $state = STATE_INLINE;
  1757. $inline_doc_state = STATE_INLINE_NAME;
  1758. } elsif ($decl_type eq 'function') {
  1759. process_proto_function($_, $file);
  1760. } else {
  1761. process_proto_type($_, $file);
  1762. }
  1763. }
  1764. #
  1765. # STATE_DOCBLOCK: within a DOC: block.
  1766. #
  1767. sub process_docblock($$) {
  1768. my $file = shift;
  1769. if (/$doc_end/) {
  1770. dump_doc_section($file, $section, $contents);
  1771. $section = $section_default;
  1772. $contents = "";
  1773. $function = "";
  1774. %parameterdescs = ();
  1775. %parametertypes = ();
  1776. @parameterlist = ();
  1777. %sections = ();
  1778. @sectionlist = ();
  1779. $prototype = "";
  1780. $state = STATE_NORMAL;
  1781. } elsif (/$doc_content/) {
  1782. if ( $1 eq "" ) {
  1783. $contents .= $blankline;
  1784. } else {
  1785. $contents .= $1 . "\n";
  1786. }
  1787. }
  1788. }
  1789. #
  1790. # STATE_INLINE: docbook comments within a prototype.
  1791. #
  1792. sub process_inline($$) {
  1793. my $file = shift;
  1794. # First line (state 1) needs to be a @parameter
  1795. if ($inline_doc_state == STATE_INLINE_NAME && /$doc_inline_sect/o) {
  1796. $section = $1;
  1797. $contents = $2;
  1798. $new_start_line = $.;
  1799. if ($contents ne "") {
  1800. while (substr($contents, 0, 1) eq " ") {
  1801. $contents = substr($contents, 1);
  1802. }
  1803. $contents .= "\n";
  1804. }
  1805. $inline_doc_state = STATE_INLINE_TEXT;
  1806. # Documentation block end */
  1807. } elsif (/$doc_inline_end/) {
  1808. if (($contents ne "") && ($contents ne "\n")) {
  1809. dump_section($file, $section, $contents);
  1810. $section = $section_default;
  1811. $contents = "";
  1812. }
  1813. $state = STATE_PROTO;
  1814. $inline_doc_state = STATE_INLINE_NA;
  1815. # Regular text
  1816. } elsif (/$doc_content/) {
  1817. if ($inline_doc_state == STATE_INLINE_TEXT) {
  1818. $contents .= $1 . "\n";
  1819. # nuke leading blank lines
  1820. if ($contents =~ /^\s*$/) {
  1821. $contents = "";
  1822. }
  1823. } elsif ($inline_doc_state == STATE_INLINE_NAME) {
  1824. $inline_doc_state = STATE_INLINE_ERROR;
  1825. print STDERR "${file}:$.: warning: ";
  1826. print STDERR "Incorrect use of kernel-doc format: $_";
  1827. ++$warnings;
  1828. }
  1829. }
  1830. }
  1831. sub process_file($) {
  1832. my $file;
  1833. my $initial_section_counter = $section_counter;
  1834. my ($orig_file) = @_;
  1835. $file = map_filename($orig_file);
  1836. if (!open(IN,"<$file")) {
  1837. print STDERR "Error: Cannot open file $file\n";
  1838. ++$errors;
  1839. return;
  1840. }
  1841. $. = 1;
  1842. $section_counter = 0;
  1843. while (<IN>) {
  1844. while (s/\\\s*$//) {
  1845. $_ .= <IN>;
  1846. }
  1847. # Replace tabs by spaces
  1848. while ($_ =~ s/\t+/' ' x (length($&) * 8 - length($`) % 8)/e) {};
  1849. # Hand this line to the appropriate state handler
  1850. if ($state == STATE_NORMAL) {
  1851. process_normal();
  1852. } elsif ($state == STATE_NAME) {
  1853. process_name($file, $_);
  1854. } elsif ($state == STATE_BODY || $state == STATE_BODY_MAYBE) {
  1855. process_body($file, $_);
  1856. } elsif ($state == STATE_INLINE) { # scanning for inline parameters
  1857. process_inline($file, $_);
  1858. } elsif ($state == STATE_PROTO) {
  1859. process_proto($file, $_);
  1860. } elsif ($state == STATE_DOCBLOCK) {
  1861. process_docblock($file, $_);
  1862. }
  1863. }
  1864. # Make sure we got something interesting.
  1865. if ($initial_section_counter == $section_counter) {
  1866. if ($output_mode ne "none") {
  1867. print STDERR "${file}:1: warning: no structured comments found\n";
  1868. }
  1869. if (($output_selection == OUTPUT_INCLUDE) && ($show_not_found == 1)) {
  1870. print STDERR " Was looking for '$_'.\n" for keys %function_table;
  1871. }
  1872. }
  1873. }
  1874. $kernelversion = get_kernel_version();
  1875. # generate a sequence of code that will splice in highlighting information
  1876. # using the s// operator.
  1877. for (my $k = 0; $k < @highlights; $k++) {
  1878. my $pattern = $highlights[$k][0];
  1879. my $result = $highlights[$k][1];
  1880. # print STDERR "scanning pattern:$pattern, highlight:($result)\n";
  1881. $dohighlight .= "\$contents =~ s:$pattern:$result:gs;\n";
  1882. }
  1883. # Read the file that maps relative names to absolute names for
  1884. # separate source and object directories and for shadow trees.
  1885. if (open(SOURCE_MAP, "<.tmp_filelist.txt")) {
  1886. my ($relname, $absname);
  1887. while(<SOURCE_MAP>) {
  1888. chop();
  1889. ($relname, $absname) = (split())[0..1];
  1890. $relname =~ s:^/+::;
  1891. $source_map{$relname} = $absname;
  1892. }
  1893. close(SOURCE_MAP);
  1894. }
  1895. if ($output_selection == OUTPUT_EXPORTED ||
  1896. $output_selection == OUTPUT_INTERNAL) {
  1897. push(@export_file_list, @ARGV);
  1898. foreach (@export_file_list) {
  1899. chomp;
  1900. process_export_file($_);
  1901. }
  1902. }
  1903. foreach (@ARGV) {
  1904. chomp;
  1905. process_file($_);
  1906. }
  1907. if ($verbose && $errors) {
  1908. print STDERR "$errors errors\n";
  1909. }
  1910. if ($verbose && $warnings) {
  1911. print STDERR "$warnings warnings\n";
  1912. }
  1913. exit($output_mode eq "none" ? 0 : $errors);