Debian Codegraph @

lib/Autom4te/ChannelDefs.pm

raw
1 # Copyright (C) 2002-2025 Free Software Foundation, Inc.
2
3 # This program is free software; you can redistribute it and/or modify
4 # it under the terms of the GNU General Public License as published by
5 # the Free Software Foundation; either version 2, or (at your option)
6 # any later version.
7
8 # This program is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # GNU General Public License for more details.
12
13 # You should have received a copy of the GNU General Public License
14 # along with this program. If not, see <https://www.gnu.org/licenses/>.
15
16 ##################################################################
17 # The master copy of this file is in Automake's source repository.
18 # Please send updates to [email protected].
19 ##################################################################
20
21 package Autom4te::ChannelDefs;
22
23 =head1 NAME
24
25 Autom4te::ChannelDefs - channel definitions for Automake and helper functions
26
27 =head1 SYNOPSIS
28
29 use Autom4te::ChannelDefs;
30
31 print Autom4te::ChannelDefs::usage (), "\n";
32 prog_error ($MESSAGE, [%OPTIONS]);
33 error ($WHERE, $MESSAGE, [%OPTIONS]);
34 error ($MESSAGE);
35 fatal ($WHERE, $MESSAGE, [%OPTIONS]);
36 fatal ($MESSAGE);
37 verb ($MESSAGE, [%OPTIONS]);
38 switch_warning ($CATEGORY);
39 parse_WARNINGS ();
40 parse_warnings ($OPTION, @ARGUMENT);
41 Autom4te::ChannelDefs::set_strictness ($STRICTNESS_NAME);
42
43 =head1 DESCRIPTION
44
45 This package defines channels that can be used in Automake to
46 output diagnostics and other messages (via C<msg()>). It also defines
47 some helper function to enable or disable these channels, and some
48 shorthand function to output on specific channels.
49
50 =cut
51
52 use 5.006; use strict; use warnings;
53 use Exporter;
54
55 use Autom4te::Channels;
56 use Autom4te::Config;
57 BEGIN
58 {
59 if ($perl_threads)
60 {
61 require threads;
62 import threads;
63 }
64 }
65
66 our @ISA = qw (Exporter);
67 our @EXPORT = qw (&prog_error &error &fatal &verb
68 &switch_warning &parse_WARNINGS &parse_warnings
69 &merge_WARNINGS);
70
71 =head2 CHANNELS
72
73 The following channels can be used as the first argument of
74 C<Autom4te::Channels::msg>. For some of them we list a shorthand
75 function that makes the code more readable.
76
77 =over 4
78
79 =item C<fatal>
80
81 Fatal errors. Use C<&fatal> to send messages over this channel.
82
83 =item C<error>
84
85 Common errors. Use C<&error> to send messages over this channel.
86
87 =item C<error-gnu>
88
89 Errors related to GNU Standards.
90
91 =item C<error-gnu/warn>
92
93 Errors related to GNU Standards that should be warnings in 'foreign' mode.
94
95 =item C<error-gnits>
96
97 Errors related to GNITS Standards (silent by default).
98
99 =item C<automake>
100
101 Internal errors. Use C<&prog_error> to send messages over this channel.
102
103 =item C<cross>
104
105 Constructs compromising the cross-compilation of the package.
106
107 =item C<gnu>
108
109 Warnings related to GNU Coding Standards.
110
111 =item C<obsolete>
112
113 Warnings about obsolete features.
114
115 =item C<override>
116
117 Warnings about user redefinitions of Automake rules or
118 variables (silent by default).
119
120 =item C<portability>
121
122 Warnings about non-portable constructs.
123
124 =item C<portability-recursive>
125
126 Warnings about recursive variable expansions (C<$(foo$(x))>).
127 These are not universally supported, but are more portable than
128 the other non-portable constructs diagnosed by C<-Wportability>.
129 These warnings are turned on by C<-Wportability> but can then be
130 turned off separately by C<-Wno-portability-recursive>.
131
132 =item C<extra-portability>
133
134 Extra warnings about non-portable constructs covering obscure tools.
135
136 =item C<syntax>
137
138 Warnings about weird syntax, unused variables, typos...
139
140 =item C<unsupported>
141
142 Warnings about unsupported (or mis-supported) features.
143
144 =item C<verb>
145
146 Messages output in C<--verbose> mode. Use C<&verb> to send such messages.
147
148 =item C<note>
149
150 Informative messages.
151
152 =back
153
154 =cut
155
156 # Initialize our list of error/warning channels.
157 # Do not forget to update &usage and the manual
158 # if you add or change a warning channel.
159
160 register_channel 'fatal', type => 'fatal', uniq_part => UP_NONE, ordered => 0;
161 register_channel 'error', type => 'error';
162 register_channel 'error-gnu', type => 'error';
163 register_channel 'error-gnu/warn', type => 'error';
164 register_channel 'error-gnits', type => 'error', silent => 1;
165 register_channel 'automake', type => 'fatal', backtrace => 1,
166 header => ("####################\n" .
167 "## Internal Error ##\n" .
168 "####################\n"),
169 footer => "\nPlease contact <$PACKAGE_BUGREPORT>.",
170 uniq_part => UP_NONE, ordered => 0;
171
172 register_channel 'cross', type => 'warning', silent => 1;
173 register_channel 'gnu', type => 'warning';
174 register_channel 'obsolete', type => 'warning';
175 register_channel 'override', type => 'warning', silent => 1;
176 register_channel 'portability', type => 'warning', silent => 1;
177 register_channel 'extra-portability', type => 'warning', silent => 1;
178 register_channel 'portability-recursive', type => 'warning', silent => 1;
179 register_channel 'syntax', type => 'warning';
180 register_channel 'unsupported', type => 'warning';
181
182 register_channel 'verb', type => 'debug', silent => 1, uniq_part => UP_NONE,
183 ordered => 0;
184 register_channel 'note', type => 'debug', silent => 0;
185
186 setup_channel_type 'warning', header => 'warning: ';
187 setup_channel_type 'error', header => 'error: ';
188 setup_channel_type 'fatal', header => 'error: ';
189
190 =head2 FUNCTIONS
191
192 =over 4
193
194 =item C<usage ()>
195
196 Return the warning category descriptions.
197
198 =cut
199
200 sub usage ()
201 {
202 return "Warning categories are:
203 cross cross compilation issues
204 gnu GNU coding standards (default in gnu and gnits modes)
205 obsolete obsolete features or constructions (default)
206 override user redefinitions of Automake rules or variables
207 portability portability issues (default in gnu and gnits modes)
208 portability-recursive nested Make variables (default with -Wportability)
209 extra-portability extra portability issues related to obscure tools
210 syntax dubious syntactic constructs (default)
211 unsupported unsupported or incomplete features (default)
212
213 -W also understands:
214 all turn on all the warnings
215 none turn off all the warnings
216 no-CATEGORY turn off warnings in CATEGORY
217 error treat all enabled warnings as errors";
218 }
219
220 =item C<prog_error ($MESSAGE, [%OPTIONS])>
221
222 Signal a programming error (on channel C<automake>),
223 display C<$MESSAGE>, and exit 1.
224
225 =cut
226
227 sub prog_error ($;%)
228 {
229 my ($msg, %opts) = @_;
230 msg 'automake', '', $msg, %opts;
231 }
232
233 =item C<error ($WHERE, $MESSAGE, [%OPTIONS])>
234
235 =item C<error ($MESSAGE)>
236
237 Uncategorized errors.
238
239 =cut
240
241 sub error ($;$%)
242 {
243 my ($where, $msg, %opts) = @_;
244 msg ('error', $where, $msg, %opts);
245 }
246
247 =item C<fatal ($WHERE, $MESSAGE, [%OPTIONS])>
248
249 =item C<fatal ($MESSAGE)>
250
251 Fatal errors.
252
253 =cut
254
255 sub fatal ($;$%)
256 {
257 my ($where, $msg, %opts) = @_;
258 msg ('fatal', $where, $msg, %opts);
259 }
260
261 =item C<verb ($MESSAGE, [%OPTIONS])>
262
263 C<--verbose> messages.
264
265 =cut
266
267 sub verb ($;%)
268 {
269 my ($msg, %opts) = @_;
270 $msg = "thread " . threads->tid . ": " . $msg
271 if $perl_threads;
272 msg 'verb', '', $msg, %opts;
273 }
274
275 =item C<switch_warning ($CATEGORY)>
276
277 If C<$CATEGORY> is C<mumble>, turn on channel C<mumble>.
278 If it is C<no-mumble>, turn C<mumble> off.
279 Else handle C<all> and C<none> for completeness.
280
281 =cut
282
283 sub switch_warning ($)
284 {
285 my ($cat) = @_;
286 my $has_no = 0;
287
288 if ($cat =~ /^no-(.*)$/)
289 {
290 $cat = $1;
291 $has_no = 1;
292 }
293
294 if ($cat eq 'all')
295 {
296 setup_channel_type 'warning', silent => $has_no;
297 }
298 elsif ($cat eq 'none')
299 {
300 setup_channel_type 'warning', silent => ! $has_no;
301 }
302 elsif ($cat eq 'error')
303 {
304 $warnings_are_errors = ! $has_no;
305 # Set exit code if Perl warns about something
306 # (like uninitialized variables).
307 $SIG{"__WARN__"} =
308 $has_no ? 'DEFAULT' : sub { print STDERR @_; $exit_code = 1; };
309 }
310 elsif (channel_type ($cat) eq 'warning')
311 {
312 setup_channel $cat, silent => $has_no;
313 #
314 # Handling of portability warnings is trickier. For relevant tests,
315 # see 'dollarvar2', 'extra-portability' and 'extra-portability3'.
316 #
317 # -Wportability-recursive and -Wno-portability-recursive should not
318 # have any effect on other 'portability' or 'extra-portability'
319 # warnings, so there's no need to handle them separately or ad-hoc.
320 #
321 if ($cat eq 'extra-portability' && ! $has_no) # -Wextra-portability
322 {
323 # -Wextra-portability must enable 'portability' and
324 # 'portability-recursive' warnings.
325 setup_channel 'portability', silent => 0;
326 setup_channel 'portability-recursive', silent => 0;
327 }
328 if ($cat eq 'portability') # -Wportability or -Wno-portability
329 {
330 if ($has_no) # -Wno-portability
331 {
332 # -Wno-portability must disable 'extra-portability' and
333 # 'portability-recursive' warnings.
334 setup_channel 'portability-recursive', silent => 1;
335 setup_channel 'extra-portability', silent => 1;
336 }
337 else # -Wportability
338 {
339 # -Wportability must enable 'portability-recursive'
340 # warnings. But it should have no influence over the
341 # 'extra-portability' warnings.
342 setup_channel 'portability-recursive', silent => 0;
343 }
344 }
345 }
346 else
347 {
348 return 1;
349 }
350 return 0;
351 }
352
353 =item C<parse_WARNINGS ()>
354
355 Parse the WARNINGS environment variable.
356
357 =cut
358
359 # Used to communicate from parse_WARNINGS to parse_warnings.
360 our $_werror = 0;
361
362 sub parse_WARNINGS ()
363 {
364 if (exists $ENV{'WARNINGS'})
365 {
366 # Ignore unknown categories. This is required because WARNINGS
367 # should be honored by many tools.
368 # For the same reason, do not turn on -Werror at this point, just
369 # record that we saw it; parse_warnings will turn on -Werror after
370 # the command line has been processed.
371 foreach (split (',', $ENV{'WARNINGS'}))
372 {
373 if (/^(no-)?error$/)
374 {
375 $_werror = !defined $1;
376 }
377 else
378 {
379 switch_warning $_;
380 }
381 }
382 }
383 }
384
385 =item C<parse_warnings (@CATEGORIES)>
386
387 Parse the argument of C<--warning=CATEGORY> or C<-WCATEGORY>.
388 C<@CATEGORIES> is the accumulated set of warnings categories.
389 Use like this:
390
391 Autom4te::GetOpt::parse_options (
392 # ...
393 'W|warnings=s' => \@warnings,
394 )
395 # possibly call set_strictness here
396 parse_warnings @warnings;
397
398 =cut
399
400 sub parse_warnings (@)
401 {
402 foreach my $cat (map { split ',' } @_)
403 {
404 if ($cat =~ /^(no-)?error$/)
405 {
406 $_werror = !defined $1;
407 }
408 elsif (switch_warning $cat)
409 {
410 msg 'unsupported', "unknown warning category '$cat'";
411 }
412 }
413
414 switch_warning ($_werror ? 'error' : 'no-error');
415 }
416
417 =item C<merge_WARNINGS (@CATEGORIES)>
418
419 Merge the warnings categories in the environment variable C<WARNINGS>
420 with the warnings categories in C<@CATEGORIES>, and return a new
421 value for C<WARNINGS>. Values in C<@CATEGORIES> take precedence.
422 Use like this:
423
424 local $ENV{WARNINGS} = merge_WARNINGS @additional_warnings;
425
426 =cut
427
428 sub merge_WARNINGS (@)
429 {
430 my $werror = '';
431 my $all_or_none = '';
432 my %warnings;
433
434 my @categories = split /,/, $ENV{WARNINGS} || '';
435 push @categories, @_;
436
437 foreach (@categories)
438 {
439 if (/^(?:no-)?error$/)
440 {
441 $werror = $_;
442 }
443 elsif (/^(?:all|none)$/)
444 {
445 $all_or_none = $_;
446 }
447 else
448 {
449 # The character class in the second match group is ASCII \S minus
450 # comma. We are generous with this because category values may come
451 # from WARNINGS and we don't want to assume other programs'
452 # syntax for warnings categories.
453 /^(no-|)([\w\[\]\/\\!"#$%&'()*+-.:;<=>?@^`{|}~]+)$/
454 or die "Invalid warnings category: $_";
455 $warnings{$2} = $1;
456 }
457 }
458
459 my @final_warnings;
460 if ($all_or_none)
461 {
462 push @final_warnings, $all_or_none;
463 }
464 else
465 {
466 foreach (sort keys %warnings)
467 {
468 push @final_warnings, $warnings{$_} . $_;
469 }
470 }
471 if ($werror)
472 {
473 push @final_warnings, $werror;
474 }
475
476 return join (',', @final_warnings);
477 }
478
479 =item C<set_strictness ($STRICTNESS_NAME)>
480
481 Configure channels for strictness C<$STRICTNESS_NAME>.
482
483 =cut
484
485 sub set_strictness ($)
486 {
487 my ($name) = @_;
488
489 if ($name eq 'gnu')
490 {
491 setup_channel 'error-gnu', silent => 0;
492 setup_channel 'error-gnu/warn', silent => 0, type => 'error';
493 setup_channel 'error-gnits', silent => 1;
494 setup_channel 'portability', silent => 0;
495 setup_channel 'extra-portability', silent => 1;
496 setup_channel 'gnu', silent => 0;
497 }
498 elsif ($name eq 'gnits')
499 {
500 setup_channel 'error-gnu', silent => 0;
501 setup_channel 'error-gnu/warn', silent => 0, type => 'error';
502 setup_channel 'error-gnits', silent => 0;
503 setup_channel 'portability', silent => 0;
504 setup_channel 'extra-portability', silent => 1;
505 setup_channel 'gnu', silent => 0;
506 }
507 elsif ($name eq 'foreign')
508 {
509 setup_channel 'error-gnu', silent => 1;
510 setup_channel 'error-gnu/warn', silent => 0, type => 'warning';
511 setup_channel 'error-gnits', silent => 1;
512 setup_channel 'portability', silent => 1;
513 setup_channel 'extra-portability', silent => 1;
514 setup_channel 'gnu', silent => 1;
515 }
516 else
517 {
518 prog_error "level '$name' not recognized";
519 }
520 }
521
522 =back
523
524 =head1 SEE ALSO
525
526 L<Autom4te::Channels>
527
528 =head1 HISTORY
529
530 Written by Alexandre Duret-Lutz E<lt>F<[email protected]>E<gt>.
531
532 =cut
533
534 1;
535
536 ### Setup "GNU" style for perl-mode and cperl-mode.
537 ## Local Variables:
538 ## perl-indent-level: 2
539 ## perl-continued-statement-offset: 2
540 ## perl-continued-brace-offset: 0
541 ## perl-brace-offset: 0
542 ## perl-brace-imaginary-offset: 0
543 ## perl-label-offset: -2
544 ## cperl-indent-level: 2
545 ## cperl-brace-offset: 0
546 ## cperl-continued-brace-offset: 0
547 ## cperl-label-offset: -2
548 ## cperl-extra-newline-before-brace: t
549 ## cperl-merge-trailing-else: nil
550 ## cperl-continued-statement-offset: 2
551 ## End: