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