kernel-doc 60 KB

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