Automake/ChannelDefs.pm000064400000025007151054467430011037 0ustar00# Copyright (C) 2002-2020 Free Software Foundation, Inc. # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2, or (at your option) # any later version. # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # You should have received a copy of the GNU General Public License # along with this program. If not, see . package Automake::ChannelDefs; use Automake::Config; BEGIN { if ($perl_threads) { require threads; import threads; } } use Automake::Channels; =head1 NAME Automake::ChannelDefs - channel definitions for Automake and helper functions =head1 SYNOPSIS use Automake::ChannelDefs; Automake::ChannelDefs::usage (); prog_error ($MESSAGE, [%OPTIONS]); error ($WHERE, $MESSAGE, [%OPTIONS]); error ($MESSAGE); fatal ($WHERE, $MESSAGE, [%OPTIONS]); fatal ($MESSAGE); verb ($MESSAGE, [%OPTIONS]); switch_warning ($CATEGORY); parse_WARNINGS (); parse_warnings ($OPTION, $ARGUMENT); Automake::ChannelDefs::set_strictness ($STRICTNESS_NAME); =head1 DESCRIPTION This packages defines channels that can be used in Automake to output diagnostics and other messages (via C). It also defines some helper function to enable or disable these channels, and some shorthand function to output on specific channels. =cut use 5.006; use strict; use Exporter; use vars qw (@ISA @EXPORT); @ISA = qw (Exporter); @EXPORT = qw (&prog_error &error &fatal &verb &switch_warning &parse_WARNINGS &parse_warnings); =head2 CHANNELS The following channels can be used as the first argument of C. For some of them we list a shorthand function that makes the code more readable. =over 4 =item C Fatal errors. Use C<&fatal> to send messages over this channel. =item C Common errors. Use C<&error> to send messages over this channel. =item C Errors related to GNU Standards. =item C Errors related to GNU Standards that should be warnings in 'foreign' mode. =item C Errors related to GNITS Standards (silent by default). =item C Internal errors. Use C<&prog_error> to send messages over this channel. =item C Warnings related to GNU Coding Standards. =item C Warnings about obsolete features (silent by default). =item C Warnings about user redefinitions of Automake rules or variables (silent by default). =item C Warnings about non-portable constructs. =item C Extra warnings about non-portable constructs covering obscure tools. =item C Warnings about weird syntax, unused variables, typos... =item C Warnings about unsupported (or mis-supported) features. =item C Messages output in C<--verbose> mode. Use C<&verb> to send such messages. =item C Informative messages. =back =cut # Initialize our list of error/warning channels. # Do not forget to update &usage and the manual # if you add or change a warning channel. register_channel 'fatal', type => 'fatal', uniq_part => UP_NONE, ordered => 0; register_channel 'error', type => 'error'; register_channel 'error-gnu', type => 'error'; register_channel 'error-gnu/warn', type => 'error'; register_channel 'error-gnits', type => 'error', silent => 1; register_channel 'automake', type => 'fatal', backtrace => 1, header => ("####################\n" . "## Internal Error ##\n" . "####################\n"), footer => "\nPlease contact <$PACKAGE_BUGREPORT>.", uniq_part => UP_NONE, ordered => 0; register_channel 'extra-portability', type => 'warning', silent => 1; register_channel 'gnu', type => 'warning'; register_channel 'obsolete', type => 'warning'; register_channel 'override', type => 'warning', silent => 1; register_channel 'portability', type => 'warning', silent => 1; register_channel 'portability-recursive', type => 'warning', silent => 1; register_channel 'syntax', type => 'warning'; register_channel 'unsupported', type => 'warning'; register_channel 'verb', type => 'debug', silent => 1, uniq_part => UP_NONE, ordered => 0; register_channel 'note', type => 'debug', silent => 0; setup_channel_type 'warning', header => 'warning: '; setup_channel_type 'error', header => 'error: '; setup_channel_type 'fatal', header => 'error: '; =head2 FUNCTIONS =over 4 =item C Display warning categories. =cut sub usage () { print < Signal a programming error (on channel C), display C<$MESSAGE>, and exit 1. =cut sub prog_error ($;%) { my ($msg, %opts) = @_; msg 'automake', '', $msg, %opts; } =item C =item C Uncategorized errors. =cut sub error ($;$%) { my ($where, $msg, %opts) = @_; msg ('error', $where, $msg, %opts); } =item C =item C Fatal errors. =cut sub fatal ($;$%) { my ($where, $msg, %opts) = @_; msg ('fatal', $where, $msg, %opts); } =item C C<--verbose> messages. =cut sub verb ($;%) { my ($msg, %opts) = @_; $msg = "thread " . threads->tid . ": " . $msg if $perl_threads; msg 'verb', '', $msg, %opts; } =item C If C<$CATEGORY> is C, turn on channel C. If it's C, turn C off. Else handle C and C for completeness. =cut sub switch_warning ($) { my ($cat) = @_; my $has_no = 0; if ($cat =~ /^no-(.*)$/) { $cat = $1; $has_no = 1; } if ($cat eq 'all') { setup_channel_type 'warning', silent => $has_no; } elsif ($cat eq 'none') { setup_channel_type 'warning', silent => ! $has_no; } elsif ($cat eq 'error') { $warnings_are_errors = ! $has_no; # Set exit code if Perl warns about something # (like uninitialized variables). $SIG{"__WARN__"} = $has_no ? 'DEFAULT' : sub { print STDERR @_; $exit_code = 1; }; } elsif (channel_type ($cat) eq 'warning') { setup_channel $cat, silent => $has_no; # # Handling of portability warnings is trickier. For relevant tests, # see 'dollarvar2', 'extra-portability' and 'extra-portability3'. # # -Wportability-recursive and -Wno-portability-recursive should not # have any effect on other 'portability' or 'extra-portability' # warnings, so there's no need to handle them separately or ad-hoc. # if ($cat eq 'extra-portability' && ! $has_no) # -Wextra-portability { # -Wextra-portability must enable 'portability' and # 'portability-recursive' warnings. setup_channel 'portability', silent => 0; setup_channel 'portability-recursive', silent => 0; } if ($cat eq 'portability') # -Wportability or -Wno-portability { if ($has_no) # -Wno-portability { # -Wno-portability must disable 'extra-portability' and # 'portability-recursive' warnings. setup_channel 'portability-recursive', silent => 1; setup_channel 'extra-portability', silent => 1; } else # -Wportability { # -Wportability must enable 'portability-recursive' # warnings. But it should have no influence over the # 'extra-portability' warnings. setup_channel 'portability-recursive', silent => 0; } } } else { return 1; } return 0; } =item C Parse the WARNINGS environment variable. =cut sub parse_WARNINGS () { if (exists $ENV{'WARNINGS'}) { # Ignore unknown categories. This is required because WARNINGS # should be honored by many tools. switch_warning $_ foreach (split (',', $ENV{'WARNINGS'})); } } =item C Parse the argument of C<--warning=CATEGORY> or C<-WCATEGORY>. C<$OPTIONS> is C<"--warning"> or C<"-W">, C<$ARGUMENT> is C. This is meant to be used as an argument to C. =cut sub parse_warnings ($$) { my ($opt, $categories) = @_; foreach my $cat (split (',', $categories)) { msg 'unsupported', "unknown warning category '$cat'" if switch_warning $cat; } } =item C Configure channels for strictness C<$STRICTNESS_NAME>. =cut sub set_strictness ($) { my ($name) = @_; if ($name eq 'gnu') { setup_channel 'error-gnu', silent => 0; setup_channel 'error-gnu/warn', silent => 0, type => 'error'; setup_channel 'error-gnits', silent => 1; setup_channel 'portability', silent => 0; setup_channel 'extra-portability', silent => 1; setup_channel 'gnu', silent => 0; } elsif ($name eq 'gnits') { setup_channel 'error-gnu', silent => 0; setup_channel 'error-gnu/warn', silent => 0, type => 'error'; setup_channel 'error-gnits', silent => 0; setup_channel 'portability', silent => 0; setup_channel 'extra-portability', silent => 1; setup_channel 'gnu', silent => 0; } elsif ($name eq 'foreign') { setup_channel 'error-gnu', silent => 1; setup_channel 'error-gnu/warn', silent => 0, type => 'warning'; setup_channel 'error-gnits', silent => 1; setup_channel 'portability', silent => 1; setup_channel 'extra-portability', silent => 1; setup_channel 'gnu', silent => 1; } else { prog_error "level '$name' not recognized"; } } =back =head1 SEE ALSO L =head1 HISTORY Written by Alexandre Duret-Lutz EFE. =cut 1; Automake/Channels.pm000064400000047771151054467430010434 0ustar00# Copyright (C) 2002-2020 Free Software Foundation, Inc. # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2, or (at your option) # any later version. # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # You should have received a copy of the GNU General Public License # along with this program. If not, see . ############################################################### # The main copy of this file is in Automake's git repository. # # Updates should be sent to automake-patches@gnu.org. # ############################################################### package Automake::Channels; =head1 NAME Automake::Channels - support functions for error and warning management =head1 SYNOPSIS use Automake::Channels; # Register a channel to output warnings about unused variables. register_channel 'unused', type => 'warning'; # Register a channel for system errors. register_channel 'system', type => 'error', exit_code => 4; # Output a message on channel 'unused'. msg 'unused', "$file:$line", "unused variable '$var'"; # Make the 'unused' channel silent. setup_channel 'unused', silent => 1; # Turn on all channels of type 'warning'. setup_channel_type 'warning', silent => 0; # Redirect all channels to push messages on a Thread::Queue using # the specified serialization key. setup_channel_queue $queue, $key; # Output a message pending in a Thread::Queue. pop_channel_queue $queue; # Treat all warnings as errors. $warnings_are_errors = 1; # Exit with the greatest exit code encountered so far. exit $exit_code; =head1 DESCRIPTION This perl module provides support functions for handling diagnostic channels in programs. Channels can be registered to convey fatal, error, warning, or debug messages. Each channel has various options (e.g. is the channel silent, should duplicate messages be removed, etc.) that can also be overridden on a per-message basis. =cut use 5.006; use strict; use Exporter; use Carp; use File::Basename; use vars qw (@ISA @EXPORT %channels $me); @ISA = qw (Exporter); @EXPORT = qw ($exit_code $warnings_are_errors &reset_local_duplicates &reset_global_duplicates ®ister_channel &msg &exists_channel &channel_type &setup_channel &setup_channel_type &dup_channel_setup &drop_channel_setup &buffer_messages &flush_messages &setup_channel_queue &pop_channel_queue US_GLOBAL US_LOCAL UP_NONE UP_TEXT UP_LOC_TEXT); $me = basename $0; =head2 Global Variables =over 4 =item C<$exit_code> The greatest exit code seen so far. C<$exit_code> is updated from the C options of C and C channels. =cut use vars qw ($exit_code); $exit_code = 0; =item C<$warnings_are_errors> Set this variable to 1 if warning messages should be treated as errors (i.e. if they should update C<$exit_code>). =cut use vars qw ($warnings_are_errors); $warnings_are_errors = 0; =back =head2 Constants =over 4 =item C, C, C Possible values for the C options. This selects the part of the message that should be considered when filtering out duplicates. If C is used, the location and the explanation message are used for filtering. If C is used, only the explanation message is used (so the same message will be filtered out if it appears at different locations). C means that duplicate messages should be output. =cut use constant UP_NONE => 0; use constant UP_TEXT => 1; use constant UP_LOC_TEXT => 2; =item C, C Possible values for the C options. Use C for error messages that should be printed only once during the execution of the program, C for message that should be printed only once per file. (Actually, C does not do this now when files are changed, it relies on you calling C when this happens.) =cut # possible values for uniq_scope use constant US_LOCAL => 0; use constant US_GLOBAL => 1; =back =head2 Options Channels accept the options described below. These options can be passed as a hash to the C, C, and C functions. The possible keys, with their default value are: =over =item C 'warning'> The type of the channel. One of C<'debug'>, C<'warning'>, C<'error'>, or C<'fatal'>. Fatal messages abort the program when they are output. Error messages update the exit status. Debug and warning messages are harmless, except that warnings are treated as errors if C<$warnings_are_errors> is set. =item C 1> The value to update C<$exit_code> with when a fatal or error message is emitted. C<$exit_code> is also updated for warnings output when C<$warnings_are_errors> is set. =item C \*STDERR> The file where the error should be output. =item C 0> Whether the channel should be silent. Use this do disable a category of warning, for instance. =item C 1> Whether, with multi-threaded execution, the message should be queued for ordered output. =item C UP_LOC_TEXT> The part of the message subject to duplicate filtering. See the documentation for the C, C, and C constants above. C can also be set to an arbitrary string that will be used instead of the message when considering duplicates. =item C US_LOCAL> The scope of duplicate filtering. See the documentation for the C, and C constants above. =item C
''> A string to prepend to each message emitted through this channel. With partial messages, only the first part will have C
prepended. =item C