kernel-doc 59 KB

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