]> git.proxmox.com Git - mirror_qemu.git/blobdiff - scripts/kernel-doc
hw/arm/aspeed: Init CPU defaults in a common helper
[mirror_qemu.git] / scripts / kernel-doc
index 35d60af834df8defea1cfc3ce5496d367352689e..240923d509a6fcdf2a53b7e458739867174f637b 100755 (executable)
@@ -56,6 +56,13 @@ Output format selection (mutually exclusive):
   -rst                 Output reStructuredText format.
   -none                        Do not output documentation, only warnings.
 
+Output format selection modifier (affects only ReST output):
+
+  -sphinx-version      Use the ReST C domain dialect compatible with an
+                       specific Sphinx Version.
+                       If not specified, kernel-doc will auto-detect using
+                       the sphinx-build version found on PATH.
+
 Output selection (mutually exclusive):
   -export              Only output documentation for symbols that have been
                        exported using EXPORT_SYMBOL() or EXPORT_SYMBOL_GPL()
@@ -270,7 +277,7 @@ if ($#ARGV == -1) {
 }
 
 my $kernelversion;
-my $sphinx_major;
+my ($sphinx_major, $sphinx_minor, $sphinx_patch);
 
 my $dohighlight = "";
 
@@ -457,6 +464,23 @@ while ($ARGV[0] =~ m/^--?(.*)/) {
            $enable_lineno = 1;
     } elsif ($cmd eq 'show-not-found') {
        $show_not_found = 1;  # A no-op but don't fail
+    } elsif ($cmd eq "sphinx-version") {
+       my $ver_string = shift @ARGV;
+       if ($ver_string =~ m/^(\d+)(\.\d+)?(\.\d+)?/) {
+           $sphinx_major = $1;
+           if (defined($2)) {
+               $sphinx_minor = substr($2,1);
+           } else {
+               $sphinx_minor = 0;
+           }
+           if (defined($3)) {
+               $sphinx_patch = substr($3,1)
+           } else {
+               $sphinx_patch = 0;
+           }
+       } else {
+           die "Sphinx version should either major.minor or major.minor.patch format\n";
+       }
     } else {
        # Unknown argument
         usage();
@@ -477,29 +501,37 @@ sub findprog($)
 sub get_sphinx_version()
 {
        my $ver;
-       my $major = 1;
 
        my $cmd = "sphinx-build";
        if (!findprog($cmd)) {
                my $cmd = "sphinx-build3";
-               return $major if (!findprog($cmd));
+               if (!findprog($cmd)) {
+                       $sphinx_major = 1;
+                       $sphinx_minor = 2;
+                       $sphinx_patch = 0;
+                       printf STDERR "Warning: Sphinx version not found. Using default (Sphinx version %d.%d.%d)\n",
+                              $sphinx_major, $sphinx_minor, $sphinx_patch;
+                       return;
+               }
        }
 
        open IN, "$cmd --version 2>&1 |";
        while (<IN>) {
                if (m/^\s*sphinx-build\s+([\d]+)\.([\d\.]+)(\+\/[\da-f]+)?$/) {
-                       $major=$1;
+                       $sphinx_major = $1;
+                       $sphinx_minor = $2;
+                       $sphinx_patch = $3;
                        last;
                }
                # Sphinx 1.2.x uses a different format
                if (m/^\s*Sphinx.*\s+([\d]+)\.([\d\.]+)$/) {
-                       $major=$1;
+                       $sphinx_major = $1;
+                       $sphinx_minor = $2;
+                       $sphinx_patch = $3;
                        last;
                }
        }
        close IN;
-
-       return $major;
 }
 
 # get kernel version from env
@@ -655,10 +687,10 @@ sub output_function_man(%) {
        $type = $args{'parametertypes'}{$parameter};
        if ($type =~ m/([^\(]*\(\*)\s*\)\s*\(([^\)]*)\)/) {
            # pointer-to-function
-           print ".BI \"" . $parenth . $1 . "\" " . $parameter . " \") (" . $2 . ")" . $post . "\"\n";
+           print ".BI \"" . $parenth . $1 . "\" " . " \") (" . $2 . ")" . $post . "\"\n";
        } else {
            $type =~ s/([^\*])$/$1 /;
-           print ".BI \"" . $parenth . $type . "\" " . $parameter . " \"" . $post . "\"\n";
+           print ".BI \"" . $parenth . $type . "\" " . " \"" . $post . "\"\n";
        }
        $count++;
        $parenth = "";
@@ -885,6 +917,7 @@ sub output_function_rst(%) {
     my ($parameter, $section);
     my $oldprefix = $lineprefix;
     my $start = "";
+    my $is_macro = 0;
 
     if ($sphinx_major < 3) {
        if ($args{'typedef'}) {
@@ -894,11 +927,17 @@ sub output_function_rst(%) {
            $lineprefix = "";
            output_highlight_rst($args{'purpose'});
            $start = "\n\n**Syntax**\n\n  ``";
+           $is_macro = 1;
        } else {
            print ".. c:function:: ";
        }
     } else {
-       print ".. c:macro:: ". $args{'function'} . "\n\n";
+       if ($args{'typedef'} || $args{'functiontype'} eq "") {
+           $is_macro = 1;
+           print ".. c:macro:: ". $args{'function'} . "\n\n";
+       } else {
+           print ".. c:function:: ";
+       }
 
        if ($args{'typedef'}) {
            print_lineno($declaration_start_line);
@@ -907,7 +946,7 @@ sub output_function_rst(%) {
            output_highlight_rst($args{'purpose'});
            $start = "\n\n**Syntax**\n\n  ``";
        } else {
-           print "``";
+           print "``" if ($is_macro);
        }
     }
     if ($args{'functiontype'} ne "") {
@@ -929,17 +968,15 @@ sub output_function_rst(%) {
            # pointer-to-function
            print $1 . $parameter . ") (" . $2 . ")";
        } else {
-           print $type . " " . $parameter;
+           print $type;
        }
     }
-    if ($args{'typedef'}) {
-       print ");``\n\n";
+    if ($is_macro) {
+       print ")``\n\n";
     } else {
-       if ($sphinx_major < 3) {
-           print ")\n\n";
-       } else {
-           print ")``\n";
-       }
+       print ")\n\n";
+    }
+    if (!$args{'typedef'}) {
        print_lineno($declaration_start_line);
        $lineprefix = "   ";
        output_highlight_rst($args{'purpose'});
@@ -954,7 +991,7 @@ sub output_function_rst(%) {
        $type = $args{'parametertypes'}{$parameter};
 
        if ($type ne "") {
-           print "``$type $parameter``\n";
+           print "``$type``\n";
        } else {
            print "``$parameter``\n";
        }
@@ -1055,7 +1092,11 @@ sub output_struct_rst(%) {
        print "\n\n.. c:type:: " . $name . "\n\n";
     } else {
        my $name = $args{'struct'};
-       print "\n\n.. c:struct:: " . $name . "\n\n";
+       if ($args{'type'} eq 'union') {
+           print "\n\n.. c:union:: " . $name . "\n\n";
+       } else {
+           print "\n\n.. c:struct:: " . $name . "\n\n";
+       }
     }
     print_lineno($declaration_start_line);
     $lineprefix = "   ";
@@ -1390,20 +1431,25 @@ sub dump_enum($$) {
     }
 }
 
+my $typedef_type = qr { ((?:\s+[\w\*]+){1,8})\s* }x;
+my $typedef_ident = qr { \*?\s*(\w\S+)\s* }x;
+my $typedef_args = qr { \s*\((.*)\); }x;
+
+my $typedef1 = qr { typedef$typedef_type\($typedef_ident\)$typedef_args }x;
+my $typedef2 = qr { typedef$typedef_type$typedef_ident$typedef_args }x;
+
 sub dump_typedef($$) {
     my $x = shift;
     my $file = shift;
 
     $x =~ s@/\*.*?\*/@@gos;    # strip comments.
 
-    # Parse function prototypes
-    if ($x =~ /typedef\s+(\w+\s*\**)\s*\(\*?\s*(\w\S+)\s*\)\s*\((.*)\);/ ||
-       $x =~ /typedef\s+(\w+\s*\**)\s*(\w\S+)\s*\s*\((.*)\);/) {
-
-       # Function typedefs
+    # Parse function typedef prototypes
+    if ($x =~ $typedef1 || $x =~ $typedef2) {
        $return_type = $1;
        $declaration_name = $2;
        my $args = $3;
+       $return_type =~ s/^\s+//;
 
        create_parameterlist($args, ',', $file, $declaration_name);
 
@@ -1479,7 +1525,7 @@ sub create_parameterlist($$$$) {
            # Treat preprocessor directive as a typeless variable just to fill
            # corresponding data structures "correctly". Catch it later in
            # output_* subs.
-           push_parameter($arg, "", $file);
+           push_parameter($arg, "", "", $file);
        } elsif ($arg =~ m/\(.+\)\s*\(/) {
            # pointer-to-function
            $arg =~ tr/#/,/;
@@ -1488,7 +1534,7 @@ sub create_parameterlist($$$$) {
            $type = $arg;
            $type =~ s/([^\(]+\(\*?)\s*$param/$1/;
            save_struct_actual($param);
-           push_parameter($param, $type, $file, $declaration_name);
+           push_parameter($param, $type, $arg, $file, $declaration_name);
        } elsif ($arg) {
            $arg =~ s/\s*:\s*/:/g;
            $arg =~ s/\s*\[/\[/g;
@@ -1513,26 +1559,28 @@ sub create_parameterlist($$$$) {
            foreach $param (@args) {
                if ($param =~ m/^(\*+)\s*(.*)/) {
                    save_struct_actual($2);
-                   push_parameter($2, "$type $1", $file, $declaration_name);
+
+                   push_parameter($2, "$type $1", $arg, $file, $declaration_name);
                }
                elsif ($param =~ m/(.*?):(\d+)/) {
                    if ($type ne "") { # skip unnamed bit-fields
                        save_struct_actual($1);
-                       push_parameter($1, "$type:$2", $file, $declaration_name)
+                       push_parameter($1, "$type:$2", $arg, $file, $declaration_name)
                    }
                }
                else {
                    save_struct_actual($param);
-                   push_parameter($param, $type, $file, $declaration_name);
+                   push_parameter($param, $type, $arg, $file, $declaration_name);
                }
            }
        }
     }
 }
 
-sub push_parameter($$$$) {
+sub push_parameter($$$$$) {
        my $param = shift;
        my $type = shift;
+       my $org_arg = shift;
        my $file = shift;
        my $declaration_name = shift;
 
@@ -1596,8 +1644,8 @@ sub push_parameter($$$$) {
        # "[blah" in a parameter string;
        ###$param =~ s/\s*//g;
        push @parameterlist, $param;
-       $type =~ s/\s\s+/ /g;
-       $parametertypes{$param} = $type;
+       $org_arg =~ s/\s\s+/ /g;
+       $parametertypes{$param} = $org_arg;
 }
 
 sub check_sections($$$$$) {
@@ -1671,7 +1719,7 @@ sub dump_function($$) {
     my $file = shift;
     my $noret = 0;
 
-    print_lineno($.);
+    print_lineno($new_start_line);
 
     $prototype =~ s/^static +//;
     $prototype =~ s/^extern +//;
@@ -1697,6 +1745,9 @@ sub dump_function($$) {
             )+
           \)\)\s+//x;
 
+    # Strip QEMU specific compiler annotations
+    $prototype =~ s/QEMU_[A-Z_]+ +//;
+
     # Yes, this truly is vile.  We are looking for:
     # 1. Return type (may be nothing if we're looking at a macro)
     # 2. Function name
@@ -1710,7 +1761,7 @@ sub dump_function($$) {
     # If you mess with these regexps, it's a good idea to check that
     # the following functions' documentation still comes out right:
     # - parport_register_device (function pointer parameters)
-    # - qatomic_set (macro)
+    # - atomic_set (macro)
     # - pci_match_device, __copy_to_user (long return type)
 
     if ($define && $prototype =~ m/^()([a-zA-Z0-9_~:]+)\s+/) {
@@ -1748,30 +1799,48 @@ sub dump_function($$) {
        return;
     }
 
-       my $prms = join " ", @parameterlist;
-       check_sections($file, $declaration_name, "function", $sectcheck, $prms);
+    my $prms = join " ", @parameterlist;
+    check_sections($file, $declaration_name, "function", $sectcheck, $prms);
 
-        # This check emits a lot of warnings at the moment, because many
-        # functions don't have a 'Return' doc section. So until the number
-        # of warnings goes sufficiently down, the check is only performed in
-        # verbose mode.
-        # TODO: always perform the check.
-        if ($verbose && !$noret) {
-                check_return_section($file, $declaration_name, $return_type);
-        }
+    # This check emits a lot of warnings at the moment, because many
+    # functions don't have a 'Return' doc section. So until the number
+    # of warnings goes sufficiently down, the check is only performed in
+    # verbose mode.
+    # TODO: always perform the check.
+    if ($verbose && !$noret) {
+           check_return_section($file, $declaration_name, $return_type);
+    }
 
-    output_declaration($declaration_name,
-                      'function',
-                      {'function' => $declaration_name,
-                       'module' => $modulename,
-                       'functiontype' => $return_type,
-                       'parameterlist' => \@parameterlist,
-                       'parameterdescs' => \%parameterdescs,
-                       'parametertypes' => \%parametertypes,
-                       'sectionlist' => \@sectionlist,
-                       'sections' => \%sections,
-                       'purpose' => $declaration_purpose
-                      });
+    # The function parser can be called with a typedef parameter.
+    # Handle it.
+    if ($return_type =~ /typedef/) {
+       output_declaration($declaration_name,
+                          'function',
+                          {'function' => $declaration_name,
+                           'typedef' => 1,
+                           'module' => $modulename,
+                           'functiontype' => $return_type,
+                           'parameterlist' => \@parameterlist,
+                           'parameterdescs' => \%parameterdescs,
+                           'parametertypes' => \%parametertypes,
+                           'sectionlist' => \@sectionlist,
+                           'sections' => \%sections,
+                           'purpose' => $declaration_purpose
+                          });
+    } else {
+       output_declaration($declaration_name,
+                          'function',
+                          {'function' => $declaration_name,
+                           'module' => $modulename,
+                           'functiontype' => $return_type,
+                           'parameterlist' => \@parameterlist,
+                           'parameterdescs' => \%parameterdescs,
+                           'parametertypes' => \%parametertypes,
+                           'sectionlist' => \@sectionlist,
+                           'sections' => \%sections,
+                           'purpose' => $declaration_purpose
+                          });
+    }
 }
 
 sub reset_state {
@@ -1981,7 +2050,7 @@ sub process_name($$) {
     if (/$doc_block/o) {
        $state = STATE_DOCBLOCK;
        $contents = "";
-       $new_start_line = $. + 1;
+       $new_start_line = $.;
 
        if ( $1 eq "" ) {
            $section = $section_intro;
@@ -2064,6 +2133,7 @@ sub process_body($$) {
     if ($state == STATE_BODY_WITH_BLANK_LINE && /^\s*\*\s?\S/) {
        dump_section($file, $section, $contents);
        $section = $section_default;
+       $new_start_line = $.;
        $contents = "";
     }
 
@@ -2119,6 +2189,7 @@ sub process_body($$) {
        $prototype = "";
        $state = STATE_PROTO;
        $brcount = 0;
+        $new_start_line = $. + 1;
     } elsif (/$doc_content/) {
        if ($1 eq "") {
            if ($section eq $section_context) {
@@ -2313,7 +2384,10 @@ sub process_file($) {
 }
 
 
-$sphinx_major = get_sphinx_version();
+if ($output_mode eq "rst") {
+       get_sphinx_version() if (!$sphinx_major);
+}
+
 $kernelversion = get_kernel_version();
 
 # generate a sequence of code that will splice in highlighting information