kernel-doc 59 KB

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