2 # Generate the Summary of Library Facilities (summary.texi).
4 # Copyright (C) 2017-2019 Free Software Foundation, Inc.
5 # This file is part of the GNU C Library.
6 # Contributed by Rical Jasan <ricaljasan@pacific.net>, 2017.
8 # The GNU C Library is free software; you can redistribute it and/or
9 # modify it under the terms of the GNU Lesser General Public License
10 # as published by the Free Software Foundation; either version 2.1 of
11 # the License, or (at your option) any later version.
13 # The GNU C Library is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 # Lesser General Public License for more details.
18 # You should have received a copy of the GNU Lesser General Public
19 # License along with the GNU C Library; if not, see
20 # <https://www.gnu.org/licenses/>.
22 # Anything declared in a header or defined in a standard should have
23 # its origins annotated using the @standards macro (see macro.texi).
24 # This script checks all such elements in the manual (generally,
25 # @def|item*-commands), ensuring annotations are present and correct.
26 # If any errors are detected, they are all reported at the end and
27 # failure is indicated.
35 my $script = basename
$0;
37 &help
if $ARGV[0] eq "--help"; # Will exit(0).
42 my $nde = qr/^\@node /;
44 my $itm = qr/^\@item /;
45 my $itms = qr/^\@itemx? /; # Don't match @itemize.
46 my $ann = qr/^\@(def\w+|item)x? /; # Annotatable.
47 my $std = qr/^\@standards\{/;
48 my $stx = qr/^\@standardsx\{/;
49 my $stds = qr/^\@standardsx?\{/;
50 my $strict_std = qr/^\@standards\{([^,]+, )[^,\}]+\}$/;
51 my $strict_stx = qr/^\@standardsx\{([^,]+, ){2}[^,\}]+\}$/;
52 my $lcon = qr/([vf]?table|itemize|enumerate)/;
53 my $list = qr/^\@${lcon}/;
54 my $endl = qr/^\@end ${lcon}/;
55 my $ign = qr/^\@ignore/;
56 my $eig = qr/^\@end ignore/;
66 open $input, '<', $texi or die "open $texi: $!";
67 while (my $line = <$input>) {
69 $node = &get_node
($line);
70 } elsif ($line =~ $def) {
71 &process_annotation
($line);
72 } elsif ($line =~ $list) {
73 &process_list
($1); # @items occur in list or table context.
74 } elsif ($line =~ $stds) {
75 &record_error
("Misplaced annotation", ["[$.] ".$line]);
76 } elsif ($line =~ $ign) {
77 while (<$input> !~ $eig) {}
80 close $input or die "close $texi: $!";
83 # Disabled until annotations are complete.
84 &print_errors
() if %errors && 0; # Will exit(1).
86 print("\@c DO NOT EDIT THIS FILE!\n".
87 "\@c This file is generated by $script from the Texinfo sources.\n".
88 "\@c The \@items are \@include'd from a \@table in header.texi.\n\n");
90 &print_entry
($_) for sort keys %entries;
92 # Processes an annotatable element, including any subsequent elements
93 # in an @*x chain, ensuring @standards are present, with valid syntax,
94 # either recording any errors detected or creating Summary entries.
95 # This function is the heart of the script.
97 # Prototypes and standards are gathered into separate lists and used
98 # to evaluate the completeness and correctness of annotations before
99 # generating the Summary entries. "Prototype" is used to refer to an
100 # element's entire definition while avoiding conflation with
101 # @def*-commands. "Element" is strictly used here to refer to the
102 # name extracted from the prototype, as used in @standardsx, for
103 # sorting the Summary.
104 sub process_annotation
107 my (@prototypes, @standards, $i, @tmp);
109 # Gather prototypes and standards.
110 push @prototypes, $line;
111 while ($line = <$input>) {
112 last if $line !~ $ann;
113 push @prototypes, $line;
115 if ($line !~ $stds) { # The fundamental error.
116 return &record_error
('Missing annotation', \
@prototypes);
118 push @standards, $line;
119 push @standards, $line while ($line = <$input>) =~ $stds;
121 # If next line is an @item, seek back to catch it on the next
122 # iteration. This avoids imposing a non-Texinfo syntax
123 # requirement of blank lines between consecutive annotated @items.
125 seek $input, -length($line), 1 or die "seek: $!";
128 # Strict check for syntax errors. Other matches are loose, which
129 # aids error detection and reporting by ensuring things that look
130 # like standards aren't simply passed over, but caught here.
131 for ($i=0; $i<@standards; ++$i) {
132 my $standard = $standards[$i];
133 if ($standard !~ $strict_std && $standard !~ $strict_stx) {
134 push @tmp, $standard;
137 return &record_error
('Invalid syntax', \
@tmp) if @tmp;
139 # @standardsx should not be in non-@*x chains.
140 if (@prototypes == 1) {
141 for ($i=0; $i<@standards; ++$i) {
142 return &record_error
('Misplaced @standardsx', \
@prototypes)
143 if $standards[$i] =~ $stx;
146 # @standards may only occur once in @*x chains, at the beginning.
147 if (@prototypes > 1) {
148 for ($i=1; $i<@standards; ++$i) {
149 return &record_error
('Misplaced @standards', \
@prototypes)
150 if $standards[$i] =~ $std;
154 # The @standards are aligned.
155 &add_entries
(\
@prototypes, \
@standards);
158 # Goes through the prototypes, cleaning them up and extracting the
159 # elements, pairing them with the appropriate annotations to create
163 my ($prototypes, $standards) = @_;
164 my $isx = @
{$prototypes} > 1 ?
1 : 0;
165 my $allx = $standards->[0] =~ $stx ?
1 : 0;
166 my ($defstd, $defhdr, %standardsx, $i, $j);
168 # Grab the default annotation and index any @standardsx. Take
169 # care in case there is no default.
173 = $standards->[0] =~ /${std}([^,]+), (.*)\}$/;
175 for ($i = $allx ?
0 : 1; $i<@
{$standards}; ++$i) {
177 = $standards->[$i] =~ /${stx}([^,]+), ([^,]+), (.*)\}$/;
178 push @
{$standardsx{$e}{hs
}}, [$h, $s];
182 for ($i=0; $i<@
{$prototypes}; ++$i) {
183 my $e = &get_element
($prototypes->[$i]);
184 my $p = &get_prototype
($prototypes->[$i]);
186 if ($isx && exists $standardsx{$e}) {
187 for ($j=0; $j<@
{$standardsx{$e}{hs
}}; ++$j) {
188 $h = $standardsx{$e}{hs
}[$j]->[0];
189 $s = $standardsx{$e}{hs
}[$j]->[1];
190 &record_entry
($e, $p, $h, $s, $node);
191 ++$standardsx{$e}{seen
};
193 } elsif ($isx && $allx) {
194 &record_error
('Missing annotation', [$prototypes->[$i]]);
196 &record_entry
($e, $p, $defhdr, $defstd, $node);
198 for ($j=0; $j<@
{$standards}; ++$j) {
199 ($s, $h) = $standards->[$j] =~ /${std}([^,]+), ([^,\}]+)\}$/;
200 &record_entry
($e, $p, $h, $s, $node);
205 # Check if there were any unmatched @standardsx.
206 for my $e (keys %standardsx) {
207 if (!exists $standardsx{$e}{seen
}) {
208 &record_error
('Spurious @standardsx', [$e."\n"])
213 # Stores a Summary entry in %entries. May be called multiple times
214 # per element if multiple header and standard annotations exist. Also
215 # keys on prototypes, as some elements have multiple prototypes. See
216 # isnan in arith.texi for one example.
219 my ($ele, $proto, $hdr, $std, $node) = @_;
220 push @
{$entries{$ele}{$proto}}, [$hdr, $std, $node];
223 # Processes list or table contexts, with nesting.
227 my $in_vtbl = $type eq "vtable" ?
1 : 0;
229 while (my $line = <$input>) {
230 if ($line =~ $itms) {
231 next if ! $in_vtbl; # Not an annotatable context.
232 &process_annotation
($line);
233 } elsif ($line =~ $def) {
234 &process_annotation
($line);
235 } elsif ($line =~ $stds) {
236 &record_error
('Misplaced annotation', ["[$.] ".$line]);
237 } elsif ($line =~ $endl) {
239 } elsif ($line =~ $list) {
240 &process_list
($1); # Nested list.
245 # Returns the current node from an @node line. Used for referencing
252 my ($n) = split ',', $line;
256 # Returns the cleaned up prototype from @def|item* lines.
261 $dfn =~ s/\s+/ /g; # Collapse whitespace.
262 $dfn =~ s/ \{([^\}]*)\} / $1 /g; # Remove grouping braces.
263 $dfn =~ s/^\@\S+ //; # Remove @-command.
264 $dfn =~ s/^Macro //i; # Scrape off cruft...
265 $dfn =~ s/^Data Type //i;
266 $dfn =~ s/^Variable //i;
267 $dfn =~ s/^Deprecated Function //i;
268 $dfn =~ s/^SVID Macro //i;
269 $dfn =~ s/^Obsolete function //i;
270 $dfn =~ s/^Constant //i;
272 $dfn =~ s/^Function //i;
273 $dfn =~ s/^\{(.*)\}$/$1/; # Debrace yourself.
274 $dfn =~ s/^\{([^\}]*)\} /$1 /; # These ones too.
278 # Returns an annotated element's name.
280 # Takes a line defining an annotatable element (e.g., @def|item*),
281 # splitting it on whitespace. The element is generally detected as
282 # the member immediately preceding the first parenthesized expression
283 # (e.g., a function), or the last token in the list. Some additional
284 # cleanup is applied to the element before returning it.
288 my @toks = split /\s+/, shift;
289 # tzname array uses '['; don't match function pointers.
290 ++$i while $toks[$i] && $toks[$i] !~ /^[\(\[](?!\*)/;
291 $toks[$i-1] =~ s/^\*//; # Strip pointer type syntax.
292 $toks[$i-1] =~ s/^\{?([^\}]+)\}?$/$1/; # Strip braces.
293 $toks[$i-1] =~ s/^\(\*([^\)]+)\)$/$1/; # Function pointers.
297 # Records syntax errors detected in the manual related to @standards.
298 # The @def|item*s are grouped by file, then errors, to make it easier
299 # to track down exactly where and what the problems are.
302 my ($err, $list) = @_;
303 push @
{$errors{$texi}{$err}}, $_ for (@
{$list});
307 # Reports all detected errors and exits with failure. Indentation is
308 # used for readability, and "ERROR" is used for visibility.
311 for $texi (sort keys %errors) {
312 print STDERR
"ERRORS in $texi:\n";
313 for my $err (sort keys %{$errors{$texi}}) {
314 print STDERR
" $err:\n";
315 print STDERR
" $_" for (@
{$errors{$texi}{$err}});
318 print(STDERR
"\nFor a description of expected syntax, see ".
319 "\`$script --help'\n\n");
323 # Prints an entry in the Summary.
325 # All the blank lines in summary.texi may seem strange at first, but
326 # they have significant impact on how Texinfo renders the output.
327 # Essentially, each line is its own paragraph. There is a @comment
328 # with the element name, arguably unnecessary, but useful for seeing
329 # the sorting order and extracted element names, and maintains the
330 # format established by summary.awk. Each @item in the @table is the
331 # prototype, which may be anything from just a variable name to a
332 # function declaration. The body of each @item contains lines
333 # annotating the headers and standards each element is declared
334 # in/comes from, with a reference to the @node documenting the element
335 # wrt. each header and standard combination.
339 for my $prototype (sort keys %{$entries{$element}}) {
340 print "\@comment $element\n\@item $prototype\n\n";
341 for (@
{$entries{$element}{$prototype}}) {
342 my ($header, $standard, $node)
343 = ($_->[0], $_->[1], $_->[2]);
344 if ($header =~ /^\(none\)$/i) {
345 $header = "\@emph{no header}";
346 } elsif ($header =~ /\(optional\)$/) {
347 $header =~ s/^(\S+) \((.*)\)$/\@file{$1} \@emph{$2}/;
348 } elsif ($header ne '???') {
349 $header = "\@file{$header}";
351 print "$header ($standard): \@ref{$node}.\n\n";
356 # Document the syntax of @standards.
361 generates the Summary of Library Facilities (summary.texi)
362 from @standards and @standardsx macros in the Texinfo sources (see
363 macros.texi). While generating the Summary, it also checks that
364 @standards are used, correctly.
366 In general, any @def*-command or @item in a @vtable is considered
367 annotatable. "Misplaced annotation" refers to @standards macros
368 detected outside an annotatable context. "Missing annotation" refers
369 to annotatable elements without @standards. @standards are expected
370 to immediately follow the elements being annotated. In @*x lists,
371 @standards sets the default annotation and may only occur as the first
372 annotation ("Misplaced @standards"). @standardsx may not be used
373 outside @*x lists ("Misplaced @standardsx"). "Spurious @standardsx"
374 refers to otherwise valid @standardsx macros that were not matched to
375 an element in an @*x list. "Invalid syntax" means just that.
377 The syntax of @standards annotations is designed to accomodate
378 multiple header and standards annotations, as necessary.
386 @standards{STD, HDR1}
387 @standards{STD, HDR2}
395 @standardsx{bar, STD1, HDR1}
396 @standardsx{baz, STD1, HDR1}
397 @standardsx{baz, STD2, HDR2}
399 Note that @standardsx deviates from the usual Texinfo syntax in that
400 it is optional and may be used without @standards.