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