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