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