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