This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
autodoc.pl: Use bold font for 'deprecated', 'experimental'
[perl5.git] / autodoc.pl
1 #!/usr/bin/perl -w
2 #
3 # Unconditionally regenerate:
4 #
5 #    pod/perlintern.pod
6 #    pod/perlapi.pod
7 #
8 # from information stored in
9 #
10 #    embed.fnc
11 #    plus all the .c and .h files listed in MANIFEST
12 #
13 # Has an optional arg, which is the directory to chdir to before reading
14 # MANIFEST and *.[ch].
15 #
16 # This script is invoked as part of 'make all'
17 #
18 # '=head1' are the only headings looked for.  If the first non-blank line after
19 # the heading begins with a word character, it is considered to be the first 
20 # line of documentation that applies to the heading itself.  That is, it is 
21 # output immediately after the heading, before the first function, and not 
22 # indented. The next input line that is a pod directive terminates this 
23 # heading-level documentation.
24
25 # The meanings of the flags fields in embed.fnc and the source code is
26 # documented at the top of embed.fnc.
27
28 use strict;
29 use warnings;
30
31 if (@ARGV) {
32     my $workdir = shift;
33     chdir $workdir
34         or die "Couldn't chdir to '$workdir': $!";
35 }
36 require './regen/regen_lib.pl';
37 require './regen/embed_lib.pl';
38
39 my %described_elsewhere;
40
41 #
42 # See database of global and static function prototypes in embed.fnc
43 # This is used to generate prototype headers under various configurations,
44 # export symbols lists for different platforms, and macros to provide an
45 # implicit interpreter context argument.
46 #
47
48 my %docs;
49 my %seen;
50 my %funcflags;
51 my %missing;
52
53 my $section = "Unknown section";
54 # Somewhat loose match for an apidoc line so we can catch minor typos.
55 # Parentheses are used to capture portions so that below we verify
56 # that things are the actual correct syntax.
57 my $apidoc_re = qr/ ^ (\s*)            # $1
58                       (=?)             # $2
59                       (\s*)            # $3
60                       for (\s*)        # $4
61                       apidoc (_item)?  # $5
62                       (\s*)            # $6
63                       (.*?)            # $7
64                       \s* \n /x;
65
66 sub check_api_doc_line ($$) {
67     my ($file, $in) = @_;
68
69     return unless $in =~ $apidoc_re;
70
71     my $is_item = defined $5;
72     my $is_in_proper_form = length $1 == 0
73                          && length $2 > 0
74                          && length $3 == 0
75                          && length $4 > 0
76                          && length $6 > 0
77                          && length $7 > 0;
78     my $proto_in_file = $7;
79     my $proto = $proto_in_file;
80     $proto = "||$proto" if $proto !~ /\|/;
81     my ($flags, $ret_type, $name, @args) = split /\s*\|\s*/, $proto;
82
83     $name && $is_in_proper_form or die <<EOS;
84 Bad apidoc at $file line $.:
85   $in
86 Expected:
87   =for apidoc flags|returntype|name|arg|arg|...
88   =for apidoc flags|returntype|name
89   =for apidoc name
90 (or 'apidoc_item')
91 EOS
92
93     return ($name, $flags, $ret_type, $is_item, $proto_in_file, @args);
94 }
95
96 sub autodoc ($$) { # parse a file and extract documentation info
97     my($fh,$file) = @_;
98     my($in, $line_num, $header);
99
100     my $file_is_C = $file =~ / \. [ch] $ /x;
101
102     # Count lines easier
103     my $get_next_line = sub { $line_num++; return <$fh> };
104
105 FUNC:
106     while (defined($in = $get_next_line->())) {
107
108         if ($in=~ /^=for apidoc_section\s*(.*)/) {
109             $section = $1;
110             next FUNC;
111         }
112         elsif ($file_is_C && $in=~ /^=head1 (.*)/) {
113             # =head1 lines only have effect in C files
114
115             $section = $1;
116
117             # If the next non-space line begins with a word char, then it is
118             # the start of heading-level documentation.
119             if (defined($in = $get_next_line->())) {
120                 # Skip over empty lines
121                 while ($in =~ /^\s+$/) {
122                     if (! defined($in = $get_next_line->())) {
123                         next FUNC;
124                     }
125                 }
126
127                 if ($in !~ /^\w/) {
128                     redo FUNC;
129                 }
130                 $header = $in;
131
132                 # Continue getting the heading-level documentation until read
133                 # in any pod directive (or as a fail-safe, find a closing
134                 # comment to this pod in a C language file
135 HDR_DOC:
136                 while (defined($in = $get_next_line->())) {
137                     if ($in =~ /^=\w/) {
138                         redo FUNC;
139                     }
140
141                     if ($file_is_C && $in =~ m:^\s*\*/$:) {
142                         warn "=cut missing? $file:$line_num:$in";;
143                         last HDR_DOC;
144                     }
145                     $header .= $in;
146                 }
147             }
148             next FUNC;
149         }
150
151         if ($in =~ /^=for comment/) {
152             $in = $get_next_line->();
153             if ($in =~ /skip apidoc/) {   # Skips the next apidoc-like line
154                 while (defined($in = $get_next_line->())) {
155                     last if $in =~ $apidoc_re;
156                 }
157             }
158             next FUNC;
159         }
160
161         my ($element_name, $flags, $ret_type, $is_item, $proto_in_file, @args)
162                                             = check_api_doc_line($file, $in);
163         next unless defined $element_name;
164         die "Unexpected apidoc_item '$in' in $file near line $." if $is_item;
165
166         # If the entry is also in embed.fnc, it should be defined completely
167         # there, but not here
168         my $embed_docref = delete $funcflags{$element_name};
169         if ($embed_docref and %$embed_docref) {
170             warn "embed.fnc entry overrides redundant information in"
171                 . " '$proto_in_file' in $file" if $flags || $ret_type || @args;
172             $flags = $embed_docref->{'flags'};
173             warn "embed.fnc entry '$element_name' missing 'd' flag"
174                                                         unless $flags =~ /d/;
175             $ret_type = $embed_docref->{'ret_type'};
176             @args = @{$embed_docref->{args}};
177         } elsif ($flags !~ /m/)  { # Not in embed.fnc, is missing if not a
178                                     # macro
179             $missing{$element_name} = $file;
180         }
181
182         die "flag $1 is not legal (for function $element_name (from $file))"
183                     if $flags =~ / ( [^AabCDdEeFfhiMmNnTOoPpRrSsUuWXx] ) /x;
184
185
186         die "'u' flag must also have 'm' flag' for $element_name" if $flags =~ /u/ && $flags !~ /m/;
187         warn ("'$element_name' not \\w+ in '$proto_in_file' in $file")
188                     if $flags !~ /N/ && $element_name !~ / ^ [_[:alpha:]] \w* $ /x;
189
190         if (exists $seen{$element_name} && $flags !~ /h/) {
191             # Temporarily ignore
192             #die ("'$element_name' in $file was already documented in $seen{$element_name}");
193         }
194         else {
195             $seen{$element_name} = $file;
196         }
197
198         my $text = "";
199         my $is_link_only = ($flags =~ /h/);
200         if ($is_link_only) {    # Don't put meat of entry in perlapi
201             next FUNC if $file_is_C;    # Don't put anything if C source
202
203             # Here, is an 'h' flag in pod.  We add a reference to the pod (and
204             # nothing else) to perlapi/intern.  (It would be better to add a
205             # reference to the correct =item,=header, but something that makes
206             # it harder is that it that might be a duplicate, like '=item *';
207             # so that is a future enhancement XXX.  Another complication is
208             # there might be more than one deserving candidates.)
209             undef $header;
210             my $podname = $file =~ s!.*/!!r;    # Rmv directory name(s)
211             $podname =~ s/\.pod//;
212             $text .= "Described in L<$podname>.\n\n";
213
214             # Keep track of all the pod files that we refer to.
215             push $described_elsewhere{$podname}->@*, $podname;
216         }
217         else {
218             DOC:
219             while (defined($in = $get_next_line->())) {
220
221                 # Other pod commands are considered part of the current
222                 # function's docs, so can have lists, etc.
223                 last DOC if $in =~ /^=(cut|for\s+apidoc|head)/;
224                 if ($in =~ m:^\*/$:) {
225                     warn "=cut missing? $file:$line_num:$in";;
226                     last DOC;
227                 }
228                 $text .= $in;
229             }
230         }
231         $text = "\n$text" if $text and $text !~ /^\n/;
232
233         my $where = $flags =~ /A/ ? 'api' : 'guts';
234
235         if (exists $docs{$where}{$section}{$element_name}) {
236             warn "$0: duplicate API entry for '$element_name' in $where/$section\n";
237             next;
238         }
239         $docs{$where}{$section}{$element_name}
240             = [$flags, $text, $ret_type, $file, @args];
241
242         # Create a special entry with an empty-string name for the
243         # heading-level documentation.
244         if (defined $header) {
245             $docs{$where}{$section}{""} = $header;
246             undef $header;
247         }
248
249         if (defined $in) {
250             if ($in =~ /^=(?:for|head)/) {
251                 redo FUNC;
252             }
253         } elsif (! $is_link_only) {
254             warn "No doc for $file:$line_num:$in";
255         }
256     }
257 }
258
259 sub docout ($$$) { # output the docs for one function
260     my($fh, $element_name, $docref) = @_;
261     my($flags, $pod, $ret_type, $file, @args) = @$docref;
262     $element_name =~ s/\s*$//;
263
264     warn("Empty pod for $element_name (from $file)") unless $pod =~ /\S/;
265
266     if ($flags =~ /D/) {
267         my $function = $flags =~ /n/ ? 'definition' : 'function';
268         $pod = "\n\nC<B<DEPRECATED!>>  It is planned to remove this $function from a
269 future release of Perl.  Do not use it for new code; remove it from
270 existing code.\n\n$pod";
271     }
272     else {
273         $pod = "\n\nNOTE: this function is B<experimental> and may change or be
274 removed without notice.\n\n$pod" if $flags =~ /x/;
275     }
276
277     # Is Perl_, but no #define foo # Perl_foo
278     my $p = (($flags =~ /p/ && $flags =~ /o/ && $flags !~ /M/)
279           || ($flags =~ /f/ && $flags !~ /T/));  # Can't handle threaded varargs
280
281     $pod .= "NOTE: the C<perl_> form of this function is B<deprecated>.\n\n"
282          if $flags =~ /O/;
283     if ($p) {
284         $pod .= "NOTE: this function must be explicitly called as C<Perl_$element_name>";
285         $pod .= " with an C<aTHX_> parameter" if $flags !~ /T/;
286         $pod .= ".\n\n"
287     }
288
289     print $fh "=item $element_name\n";
290
291     # If we're printing only a link to an element, this isn't the major entry,
292     # so no X<> here.
293     print $fh "X<$element_name>\n" unless $flags =~ /h/;
294
295     print $fh $pod;
296
297     if ($flags =~ /U/) { # no usage
298         warn("U and s flags are incompatible") if $flags =~ /s/;
299         # nothing
300     } else {
301         if ($flags =~ /n/) { # no args
302             warn("$file: $element_name: n flag without m") unless $flags =~ /m/;
303             warn("$file: $element_name: n flag but apparently has args") if @args;
304             print $fh "\t$ret_type\t$element_name";
305         } else { # full usage
306             my $n            = "Perl_"x$p . $element_name;
307             my $large_ret    = length $ret_type > 7;
308             my $indent_size  = 7+8 # nroff: 7 under =head + 8 under =item
309                             +8+($large_ret ? 1 + length $ret_type : 8)
310                             +length($n) + 1;
311             my $indent;
312             print $fh "\t$ret_type" . ($large_ret ? ' ' : "\t") . "$n(";
313             my $long_args;
314             for (@args) {
315                 if ($indent_size + 2 + length > 79) {
316                     $long_args=1;
317                     $indent_size -= length($n) - 3;
318                     last;
319                 }
320             }
321             my $args = '';
322             if ($flags !~ /T/ && ($p || ($flags =~ /m/ && $element_name =~ /^Perl_/))) {
323                 $args = @args ? "pTHX_ " : "pTHX";
324                 if ($long_args) { print $fh $args; $args = '' }
325             }
326             $long_args and print $fh "\n";
327             my $first = !$long_args;
328             while () {
329                 if (!@args or
330                     length $args
331                     && $indent_size + 3 + length($args[0]) + length $args > 79
332                 ) {
333                     print $fh
334                     $first ? '' : (
335                         $indent //=
336                         "\t".($large_ret ? " " x (1+length $ret_type) : "\t")
337                         ." "x($long_args ? 4 : 1 + length $n)
338                     ),
339                     $args, (","x($args ne 'pTHX_ ') . "\n")x!!@args;
340                     $args = $first = '';
341                 }
342                 @args or last;
343                 $args .= ", "x!!(length $args && $args ne 'pTHX_ ')
344                     . shift @args;
345             }
346             if ($long_args) { print $fh "\n", substr $indent, 0, -4 }
347             print $fh ")";
348         }
349         print $fh ";" if $flags =~ /s/; # semicolon "dTHR;"
350         print $fh "\n\n";
351     }
352     print $fh "=for hackers\nFound in file $file\n\n";
353 }
354
355 sub sort_helper {
356     # Do a case-insensitive dictionary sort, with only alphabetics
357     # significant, falling back to using everything for determinancy
358     return (uc($a =~ s/[[:^alpha:]]//r) cmp uc($b =~ s/[[:^alpha:]]//r))
359            || uc($a) cmp uc($b)
360            || $a cmp $b;
361 }
362
363 sub output {
364     my ($podname, $header, $dochash, $missing, $footer) = @_;
365     #
366     # strip leading '|' from each line which had been used to hide
367     # pod from pod checkers.
368     s/^\|//gm for $header, $footer;
369
370     my $fh = open_new("pod/$podname.pod", undef,
371                       {by => "$0 extracting documentation",
372                        from => 'the C source files'}, 1);
373
374     print $fh $header;
375
376     for my $section_name (sort sort_helper keys %$dochash) {
377         my $section_info = $dochash->{$section_name};
378         next unless keys %$section_info;     # Skip empty
379         print $fh "\n=head1 $section_name\n\n";
380
381         # Output any heading-level documentation and delete so won't get in
382         # the way later
383         if (exists $section_info->{""}) {
384             print $fh $section_info->{""} . "\n";
385             delete $section_info->{""};
386         }
387         next unless keys %$section_info;     # Skip empty
388         print $fh "=over 8\n\n";
389
390         for my $function_name (sort sort_helper keys %$section_info) {
391             docout($fh, $function_name, $section_info->{$function_name});
392         }
393         print $fh "\n=back\n";
394     }
395
396     if (@$missing) {
397         print $fh "\n=head1 Undocumented functions\n\n";
398     print $fh $podname eq 'perlapi' ? <<'_EOB_' : <<'_EOB_';
399 The following functions have been flagged as part of the public API,
400 but are currently undocumented.  Use them at your own risk, as the
401 interfaces are subject to change.  Functions that are not listed in this
402 document are not intended for public use, and should NOT be used under any
403 circumstances.
404
405 If you feel you need to use one of these functions, first send email to
406 L<perl5-porters@perl.org|mailto:perl5-porters@perl.org>.  It may be
407 that there is a good reason for the function not being documented, and it
408 should be removed from this list; or it may just be that no one has gotten
409 around to documenting it.  In the latter case, you will be asked to submit a
410 patch to document the function.  Once your patch is accepted, it will indicate
411 that the interface is stable (unless it is explicitly marked otherwise) and
412 usable by you.
413 _EOB_
414 The following functions are currently undocumented.  If you use one of
415 them, you may wish to consider creating and submitting documentation for
416 it.
417 _EOB_
418     print $fh "\n=over\n\n";
419
420     for my $missing (sort sort_helper @$missing) {
421         print $fh "=item C<$missing>\nX<$missing>\n\n";
422     }
423     print $fh "=back\n\n";
424 }
425     print $fh $footer, "=cut\n";
426
427     read_only_bottom_close_and_rename($fh);
428 }
429
430 foreach (@{(setup_embed())[0]}) {
431     next if @$_ < 2;
432     my ($flags, $ret_type, $func, @args) = @$_;
433     s/\b(?:NN|NULLOK)\b\s+//g for @args;
434
435     $funcflags{$func} = {
436                          flags => $flags,
437                          ret_type => $ret_type,
438                          args => \@args,
439                         };
440 }
441
442 # glob() picks up docs from extra .c or .h files that may be in unclean
443 # development trees.
444 open my $fh, '<', 'MANIFEST'
445     or die "Can't open MANIFEST: $!";
446 while (my $line = <$fh>) {
447     next unless my ($file) = $line =~ /^(\S+\.(?:[ch]|pod))\t/;
448
449     # Don't pick up pods from these.  (We may pick up generated stuff from
450     # /lib though)
451     next if $file =~ m! ^ ( cpan | dist | ext ) / !x;
452
453     open F, '<', $file or die "Cannot open $file for docs: $!\n";
454     $section = "Functions in file $file\n";
455     autodoc(\*F,$file);
456     close F or die "Error closing $file: $!\n";
457 }
458 close $fh or die "Error whilst reading MANIFEST: $!";
459
460 for (sort keys %funcflags) {
461     next unless $funcflags{$_}{flags} =~ /d/;
462     next if $funcflags{$_}{flags} =~ /h/;
463     warn "no docs for $_\n"
464 }
465
466 foreach (sort keys %missing) {
467     warn "Function '$_', documented in $missing{$_}, not listed in embed.fnc";
468 }
469
470 # walk table providing an array of components in each line to
471 # subroutine, printing the result
472
473 # List of funcs in the public API that aren't also marked as core-only,
474 # experimental nor deprecated.
475 my @missing_api = grep $funcflags{$_}{flags} =~ /A/
476                     && $funcflags{$_}{flags} !~ /[xD]/
477                     && !$docs{api}{$_}, keys %funcflags;
478
479 my $other_places = join ", ", map { "L<$_>" } sort sort_helper qw( perlclib perlxs),
480                                                                keys %described_elsewhere;
481
482 output('perlapi', <<"_EOB_", $docs{api}, \@missing_api, <<"_EOE_");
483 |=encoding UTF-8
484 |
485 |=head1 NAME
486 |
487 |perlapi - autogenerated documentation for the perl public API
488 |
489 |=head1 DESCRIPTION
490 |X<Perl API> X<API> X<api>
491 |
492 |This file contains most of the documentation of the perl public API, as
493 |generated by F<embed.pl>.  Specifically, it is a listing of functions,
494 |macros, flags, and variables that may be used by extension writers.  Besides
495 |L<perlintern> and F<config.h>, some items are listed here as being actually
496 |documented in another pod.
497 |
498 |L<At the end|/Undocumented functions> is a list of functions which have yet
499 |to be documented.  Patches welcome!  The interfaces of these are subject to
500 |change without notice.
501 |
502 |Anything not listed here or in the other mentioned pods is not part of the
503 |public API, and should not be used by extension writers at all.  For these
504 |reasons, blindly using functions listed in F<proto.h> is to be avoided when
505 |writing extensions.
506 |
507 |In Perl, unlike C, a string of characters may generally contain embedded
508 |C<NUL> characters.  Sometimes in the documentation a Perl string is referred
509 |to as a "buffer" to distinguish it from a C string, but sometimes they are
510 |both just referred to as strings.
511 |
512 |Note that all Perl API global variables must be referenced with the C<PL_>
513 |prefix.  Again, those not listed here are not to be used by extension writers,
514 |and can be changed or removed without notice; same with macros.
515 |Some macros are provided for compatibility with the older,
516 |unadorned names, but this support may be disabled in a future release.
517 |
518 |Perl was originally written to handle US-ASCII only (that is characters
519 |whose ordinal numbers are in the range 0 - 127).
520 |And documentation and comments may still use the term ASCII, when
521 |sometimes in fact the entire range from 0 - 255 is meant.
522 |
523 |The non-ASCII characters below 256 can have various meanings, depending on
524 |various things.  (See, most notably, L<perllocale>.)  But usually the whole
525 |range can be referred to as ISO-8859-1.  Often, the term "Latin-1" (or
526 |"Latin1") is used as an equivalent for ISO-8859-1.  But some people treat
527 |"Latin1" as referring just to the characters in the range 128 through 255, or
528 |sometimes from 160 through 255.
529 |This documentation uses "Latin1" and "Latin-1" to refer to all 256 characters.
530 |
531 |Note that Perl can be compiled and run under either ASCII or EBCDIC (See
532 |L<perlebcdic>).  Most of the documentation (and even comments in the code)
533 |ignore the EBCDIC possibility.
534 |For almost all purposes the differences are transparent.
535 |As an example, under EBCDIC,
536 |instead of UTF-8, UTF-EBCDIC is used to encode Unicode strings, and so
537 |whenever this documentation refers to C<utf8>
538 |(and variants of that name, including in function names),
539 |it also (essentially transparently) means C<UTF-EBCDIC>.
540 |But the ordinals of characters differ between ASCII, EBCDIC, and
541 |the UTF- encodings, and a string encoded in UTF-EBCDIC may occupy a different
542 |number of bytes than in UTF-8.
543 |
544 |The listing below is alphabetical, case insensitive.
545 |
546 _EOB_
547 |
548 |=head1 AUTHORS
549 |
550 |Until May 1997, this document was maintained by Jeff Okamoto
551 |<okamoto\@corp.hp.com>.  It is now maintained as part of Perl itself.
552 |
553 |With lots of help and suggestions from Dean Roehrich, Malcolm Beattie,
554 |Andreas Koenig, Paul Hudson, Ilya Zakharevich, Paul Marquess, Neil
555 |Bowers, Matthew Green, Tim Bunce, Spider Boardman, Ulrich Pfeifer,
556 |Stephen McCamant, and Gurusamy Sarathy.
557 |
558 |API Listing originally by Dean Roehrich <roehrich\@cray.com>.
559 |
560 |Updated to be autogenerated from comments in the source by Benjamin Stuhl.
561 |
562 |=head1 SEE ALSO
563 |
564 |F<config.h>, L<perlintern>, $other_places
565 _EOE_
566
567 # List of non-static internal functions
568 my @missing_guts =
569  grep $funcflags{$_}{flags} !~ /[AS]/ && !$docs{guts}{$_}, keys %funcflags;
570
571 output('perlintern', <<'_EOB_', $docs{guts}, \@missing_guts, <<"_EOE_");
572 |=head1 NAME
573 |
574 |perlintern - autogenerated documentation of purely B<internal>
575 |Perl functions
576 |
577 |=head1 DESCRIPTION
578 |X<internal Perl functions> X<interpreter functions>
579 |
580 |This file is the autogenerated documentation of functions in the
581 |Perl interpreter that are documented using Perl's internal documentation
582 |format but are not marked as part of the Perl API.  In other words,
583 |B<they are not for use in extensions>!
584 |
585 _EOB_
586 |
587 |=head1 AUTHORS
588 |
589 |The autodocumentation system was originally added to the Perl core by
590 |Benjamin Stuhl.  Documentation is by whoever was kind enough to
591 |document their functions.
592 |
593 |=head1 SEE ALSO
594 |
595 |F<config.h>, L<perlapi>, $other_places
596 _EOE_