kernel-doc 62 KB

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