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