| 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::Channels; |
| 22 |
|
| 23 |
=head1 NAME |
| 24 |
|
| 25 |
Autom4te::Channels - support functions for error and warning management |
| 26 |
|
| 27 |
=head1 SYNOPSIS |
| 28 |
|
| 29 |
use Autom4te::Channels; |
| 30 |
|
| 31 |
# Register a channel to output warnings about unused variables. |
| 32 |
register_channel 'unused', type => 'warning'; |
| 33 |
|
| 34 |
# Register a channel for system errors. |
| 35 |
register_channel 'system', type => 'error', exit_code => 4; |
| 36 |
|
| 37 |
# Output a message on channel 'unused'. |
| 38 |
msg 'unused', "$file:$line", "unused variable '$var'"; |
| 39 |
|
| 40 |
# Make the 'unused' channel silent. |
| 41 |
setup_channel 'unused', silent => 1; |
| 42 |
|
| 43 |
# Turn on all channels of type 'warning'. |
| 44 |
setup_channel_type 'warning', silent => 0; |
| 45 |
|
| 46 |
# Redirect all channels to push messages on a Thread::Queue using |
| 47 |
# the specified serialization key. |
| 48 |
setup_channel_queue $queue, $key; |
| 49 |
|
| 50 |
# Output a message pending in a Thread::Queue. |
| 51 |
pop_channel_queue $queue; |
| 52 |
|
| 53 |
# Treat all warnings as errors. |
| 54 |
$warnings_are_errors = 1; |
| 55 |
|
| 56 |
# Exit with the greatest exit code encountered so far. |
| 57 |
exit $exit_code; |
| 58 |
|
| 59 |
=head1 DESCRIPTION |
| 60 |
|
| 61 |
This perl module provides support functions for handling diagnostic |
| 62 |
channels in programs. Channels can be registered to convey fatal, |
| 63 |
error, warning, or debug messages. Each channel has various options |
| 64 |
(e.g. is the channel silent, should duplicate messages be removed, |
| 65 |
etc.) that can also be overridden on a per-message basis. |
| 66 |
|
| 67 |
=cut |
| 68 |
|
| 69 |
use 5.006; use strict; use warnings; |
| 70 |
use Carp; |
| 71 |
use Exporter; |
| 72 |
use File::Basename; |
| 73 |
|
| 74 |
our @ISA = qw (Exporter); |
| 75 |
our @EXPORT = qw ($exit_code $warnings_are_errors |
| 76 |
&reset_local_duplicates &reset_global_duplicates |
| 77 |
®ister_channel &msg &exists_channel &channel_type |
| 78 |
&setup_channel &setup_channel_type |
| 79 |
&dup_channel_setup &drop_channel_setup |
| 80 |
&buffer_messages &flush_messages |
| 81 |
&setup_channel_queue &pop_channel_queue |
| 82 |
US_GLOBAL US_LOCAL |
| 83 |
UP_NONE UP_TEXT UP_LOC_TEXT); |
| 84 |
|
| 85 |
our %channels; |
| 86 |
our $me = basename $0; |
| 87 |
|
| 88 |
=head2 Global Variables |
| 89 |
|
| 90 |
=over 4 |
| 91 |
|
| 92 |
=item C<$exit_code> |
| 93 |
|
| 94 |
The greatest exit code seen so far. C<$exit_code> is updated from |
| 95 |
the C<exit_code> options of C<fatal> and C<error> channels. |
| 96 |
|
| 97 |
=cut |
| 98 |
|
| 99 |
our $exit_code = 0; |
| 100 |
|
| 101 |
=item C<$warnings_are_errors> |
| 102 |
|
| 103 |
Set this variable to 1 if warning messages should be treated as |
| 104 |
errors (i.e. if they should update C<$exit_code>). |
| 105 |
|
| 106 |
=cut |
| 107 |
|
| 108 |
our $warnings_are_errors = 0; |
| 109 |
|
| 110 |
=back |
| 111 |
|
| 112 |
=head2 Constants |
| 113 |
|
| 114 |
=over 4 |
| 115 |
|
| 116 |
=item C<UP_NONE>, C<UP_TEXT>, C<UP_LOC_TEXT> |
| 117 |
|
| 118 |
Possible values for the C<uniq_part> options. This selects the part |
| 119 |
of the message that should be considered when filtering out duplicates. |
| 120 |
If C<UP_LOC_TEXT> is used, the location and the explanation message |
| 121 |
are used for filtering. If C<UP_TEXT> is used, only the explanation |
| 122 |
message is used (so the same message will be filtered out if it appears |
| 123 |
at different locations). C<UP_NONE> means that duplicate messages |
| 124 |
should be output. |
| 125 |
|
| 126 |
=cut |
| 127 |
|
| 128 |
use constant UP_NONE => 0; |
| 129 |
use constant UP_TEXT => 1; |
| 130 |
use constant UP_LOC_TEXT => 2; |
| 131 |
|
| 132 |
=item C<US_LOCAL>, C<US_GLOBAL> |
| 133 |
|
| 134 |
Possible values for the C<uniq_scope> options. |
| 135 |
Use C<US_GLOBAL> for error messages that should be printed only |
| 136 |
once during the execution of the program, C<US_LOCAL> for message that |
| 137 |
should be printed only once per file. (Actually, C<Channels> does not |
| 138 |
do this now when files are changed, it relies on you calling |
| 139 |
C<reset_local_duplicates> when this happens.) |
| 140 |
|
| 141 |
=cut |
| 142 |
|
| 143 |
# possible values for uniq_scope |
| 144 |
use constant US_LOCAL => 0; |
| 145 |
use constant US_GLOBAL => 1; |
| 146 |
|
| 147 |
=back |
| 148 |
|
| 149 |
=head2 Options |
| 150 |
|
| 151 |
Channels accept the options described below. These options can be |
| 152 |
passed as a hash to the C<register_channel>, C<setup_channel>, and C<msg> |
| 153 |
functions. The possible keys, with their default value are: |
| 154 |
|
| 155 |
=over |
| 156 |
|
| 157 |
=item C<type =E<gt> 'warning'> |
| 158 |
|
| 159 |
The type of the channel. One of C<'debug'>, C<'warning'>, C<'error'>, or |
| 160 |
C<'fatal'>. Fatal messages abort the program when they are output. |
| 161 |
Error messages update the exit status. Debug and warning messages are |
| 162 |
harmless, except that warnings are treated as errors if |
| 163 |
C<$warnings_are_errors> is set. |
| 164 |
|
| 165 |
=item C<exit_code =E<gt> 1> |
| 166 |
|
| 167 |
The value to update C<$exit_code> with when a fatal or error message |
| 168 |
is emitted. C<$exit_code> is also updated for warnings output |
| 169 |
when C<$warnings_are_errors> is set. |
| 170 |
|
| 171 |
=item C<file =E<gt> \*STDERR> |
| 172 |
|
| 173 |
The file where the error should be output. |
| 174 |
|
| 175 |
=item C<silent =E<gt> 0> |
| 176 |
|
| 177 |
Whether the channel should be silent. Use this do disable a |
| 178 |
category of warning, for instance. |
| 179 |
|
| 180 |
=item C<ordered =E<gt> 1> |
| 181 |
|
| 182 |
Whether, with multi-threaded execution, the message should be queued |
| 183 |
for ordered output. |
| 184 |
|
| 185 |
=item C<uniq_part =E<gt> UP_LOC_TEXT> |
| 186 |
|
| 187 |
The part of the message subject to duplicate filtering. See the |
| 188 |
documentation for the C<UP_NONE>, C<UP_TEXT>, and C<UP_LOC_TEXT> |
| 189 |
constants above. |
| 190 |
|
| 191 |
C<uniq_part> can also be set to an arbitrary string that will be used |
| 192 |
instead of the message when considering duplicates. |
| 193 |
|
| 194 |
=item C<uniq_scope =E<gt> US_LOCAL> |
| 195 |
|
| 196 |
The scope of duplicate filtering. See the documentation for the |
| 197 |
C<US_LOCAL>, and C<US_GLOBAL> constants above. |
| 198 |
|
| 199 |
=item C<header =E<gt> ''> |
| 200 |
|
| 201 |
A string to prepend to each message emitted through this channel. |
| 202 |
With partial messages, only the first part will have C<header> |
| 203 |
prepended. |
| 204 |
|
| 205 |
=item C<footer =E<gt> ''> |
| 206 |
|
| 207 |
A string to append to each message emitted through this channel. |
| 208 |
With partial messages, only the final part will have C<footer> |
| 209 |
appended. |
| 210 |
|
| 211 |
=item C<backtrace =E<gt> 0> |
| 212 |
|
| 213 |
Die with a stack backtrace after displaying the message. |
| 214 |
|
| 215 |
=item C<partial =E<gt> 0> |
| 216 |
|
| 217 |
When set, indicates a partial message that should |
| 218 |
be output along with the next message with C<partial> unset. |
| 219 |
Several partial messages can be stacked this way. |
| 220 |
|
| 221 |
Duplicate filtering will apply to the I<global> message resulting from |
| 222 |
all I<partial> messages, using the options from the last (non-partial) |
| 223 |
message. Linking associated messages is the main reason to use this |
| 224 |
option. |
| 225 |
|
| 226 |
For instance the following messages |
| 227 |
|
| 228 |
msg 'channel', 'foo:2', 'redefinition of A ...'; |
| 229 |
msg 'channel', 'foo:1', '... A previously defined here'; |
| 230 |
msg 'channel', 'foo:3', 'redefinition of A ...'; |
| 231 |
msg 'channel', 'foo:1', '... A previously defined here'; |
| 232 |
|
| 233 |
will result in |
| 234 |
|
| 235 |
foo:2: redefinition of A ... |
| 236 |
foo:1: ... A previously defined here |
| 237 |
foo:3: redefinition of A ... |
| 238 |
|
| 239 |
where the duplicate "I<... A previously defined here>" has been |
| 240 |
filtered out. |
| 241 |
|
| 242 |
Linking these messages using C<partial> as follows will prevent the |
| 243 |
fourth message to disappear. |
| 244 |
|
| 245 |
msg 'channel', 'foo:2', 'redefinition of A ...', partial => 1; |
| 246 |
msg 'channel', 'foo:1', '... A previously defined here'; |
| 247 |
msg 'channel', 'foo:3', 'redefinition of A ...', partial => 1; |
| 248 |
msg 'channel', 'foo:1', '... A previously defined here'; |
| 249 |
|
| 250 |
Note that because the stack of C<partial> messages is printed with the |
| 251 |
first non-C<partial> message, most options of C<partial> messages will |
| 252 |
be ignored. |
| 253 |
|
| 254 |
=back |
| 255 |
|
| 256 |
=cut |
| 257 |
|
| 258 |
# Default options for a channel. |
| 259 |
our %_default_options = |
| 260 |
( |
| 261 |
type => 'warning', |
| 262 |
exit_code => 1, |
| 263 |
file => \*STDERR, |
| 264 |
silent => 0, |
| 265 |
ordered => 1, |
| 266 |
queue => 0, |
| 267 |
queue_key => undef, |
| 268 |
uniq_scope => US_LOCAL, |
| 269 |
uniq_part => UP_LOC_TEXT, |
| 270 |
header => '', |
| 271 |
footer => '', |
| 272 |
backtrace => 0, |
| 273 |
partial => 0, |
| 274 |
); |
| 275 |
|
| 276 |
# Filled with output messages as keys, to detect duplicates. |
| 277 |
# The value associated with each key is the number of occurrences |
| 278 |
# filtered out. |
| 279 |
our %_local_duplicate_messages = (); |
| 280 |
our %_global_duplicate_messages = (); |
| 281 |
|
| 282 |
sub _reset_duplicates (\%) |
| 283 |
{ |
| 284 |
my ($ref) = @_; |
| 285 |
my $dup = 0; |
| 286 |
foreach my $k (keys %$ref) # sort keys not needed |
| 287 |
{ |
| 288 |
$dup += $ref->{$k}; |
| 289 |
} |
| 290 |
%$ref = (); |
| 291 |
return $dup; |
| 292 |
} |
| 293 |
|
| 294 |
|
| 295 |
=head2 Functions |
| 296 |
|
| 297 |
=over 4 |
| 298 |
|
| 299 |
=item C<reset_local_duplicates ()> |
| 300 |
|
| 301 |
Reset local duplicate messages (see C<US_LOCAL>), and |
| 302 |
return the number of messages that have been filtered out. |
| 303 |
|
| 304 |
=cut |
| 305 |
|
| 306 |
sub reset_local_duplicates () |
| 307 |
{ |
| 308 |
return _reset_duplicates %_local_duplicate_messages; |
| 309 |
} |
| 310 |
|
| 311 |
=item C<reset_global_duplicates ()> |
| 312 |
|
| 313 |
Reset local duplicate messages (see C<US_GLOBAL>), and |
| 314 |
return the number of messages that have been filtered out. |
| 315 |
|
| 316 |
=cut |
| 317 |
|
| 318 |
sub reset_global_duplicates () |
| 319 |
{ |
| 320 |
return _reset_duplicates %_global_duplicate_messages; |
| 321 |
} |
| 322 |
|
| 323 |
sub _merge_options (\%%) |
| 324 |
{ |
| 325 |
my ($hash, %options) = @_; |
| 326 |
local $_; |
| 327 |
|
| 328 |
foreach (sort keys %options) |
| 329 |
{ |
| 330 |
if (exists $hash->{$_}) |
| 331 |
{ |
| 332 |
$hash->{$_} = $options{$_} |
| 333 |
} |
| 334 |
else |
| 335 |
{ |
| 336 |
confess "unknown option '$_'"; |
| 337 |
} |
| 338 |
} |
| 339 |
if ($hash->{'ordered'}) |
| 340 |
{ |
| 341 |
confess "fatal messages cannot be ordered" |
| 342 |
if $hash->{'type'} eq 'fatal'; |
| 343 |
confess "backtrace cannot be output on ordered messages" |
| 344 |
if $hash->{'backtrace'}; |
| 345 |
} |
| 346 |
} |
| 347 |
|
| 348 |
=item C<register_channel ($name, [%options])> |
| 349 |
|
| 350 |
Declare channel C<$name>, and override the default options |
| 351 |
with those listed in C<%options>. |
| 352 |
|
| 353 |
=cut |
| 354 |
|
| 355 |
sub register_channel ($;%) |
| 356 |
{ |
| 357 |
my ($name, %options) = @_; |
| 358 |
my %channel_opts = %_default_options; |
| 359 |
_merge_options %channel_opts, %options; |
| 360 |
$channels{$name} = \%channel_opts; |
| 361 |
} |
| 362 |
|
| 363 |
=item C<exists_channel ($name)> |
| 364 |
|
| 365 |
Returns true iff channel C<$name> has been registered. |
| 366 |
|
| 367 |
=cut |
| 368 |
|
| 369 |
sub exists_channel ($) |
| 370 |
{ |
| 371 |
my ($name) = @_; |
| 372 |
return exists $channels{$name}; |
| 373 |
} |
| 374 |
|
| 375 |
=item C<channel_type ($name)> |
| 376 |
|
| 377 |
Returns the type of channel C<$name> if it has been registered. |
| 378 |
Returns the empty string otherwise. |
| 379 |
|
| 380 |
=cut |
| 381 |
|
| 382 |
sub channel_type ($) |
| 383 |
{ |
| 384 |
my ($name) = @_; |
| 385 |
return $channels{$name}{'type'} if exists_channel $name; |
| 386 |
return ''; |
| 387 |
} |
| 388 |
|
| 389 |
# _format_sub_message ($LEADER, $MESSAGE) |
| 390 |
# --------------------------------------- |
| 391 |
# Split $MESSAGE at new lines and add $LEADER to each line. |
| 392 |
sub _format_sub_message ($$) |
| 393 |
{ |
| 394 |
my ($leader, $message) = @_; |
| 395 |
return $leader . join ("\n" . $leader, split ("\n", $message)) . "\n"; |
| 396 |
} |
| 397 |
|
| 398 |
# Store partial messages here. (See the 'partial' option.) |
| 399 |
our $partial = ''; |
| 400 |
|
| 401 |
# _format_message ($LOCATION, $MESSAGE, %OPTIONS) |
| 402 |
# ----------------------------------------------- |
| 403 |
# Format the message. Return a string ready to print. |
| 404 |
sub _format_message ($$%) |
| 405 |
{ |
| 406 |
my ($location, $message, %opts) = @_; |
| 407 |
my $msg = ($partial eq '' ? $opts{'header'} : '') . $message |
| 408 |
. ($opts{'partial'} ? '' : $opts{'footer'}); |
| 409 |
if (ref $location) |
| 410 |
{ |
| 411 |
# If $LOCATION is a reference, assume it's an instance of the |
| 412 |
# Autom4te::Location class and display contexts. |
| 413 |
my $loc = $location->get || $me; |
| 414 |
$msg = _format_sub_message ("$loc: ", $msg); |
| 415 |
for my $pair ($location->get_contexts) |
| 416 |
{ |
| 417 |
$msg .= _format_sub_message ($pair->[0] . ": ", $pair->[1]); |
| 418 |
} |
| 419 |
} |
| 420 |
else |
| 421 |
{ |
| 422 |
$location ||= $me; |
| 423 |
$msg = _format_sub_message ("$location: ", $msg); |
| 424 |
} |
| 425 |
return $msg; |
| 426 |
} |
| 427 |
|
| 428 |
# _enqueue ($QUEUE, $KEY, $UNIQ_SCOPE, $TO_FILTER, $MSG, $FILE) |
| 429 |
# ------------------------------------------------------------- |
| 430 |
# Push message on a queue, to be processed by another thread. |
| 431 |
sub _enqueue ($$$$$$) |
| 432 |
{ |
| 433 |
my ($queue, $key, $uniq_scope, $to_filter, $msg, $file) = @_; |
| 434 |
$queue->enqueue ($key, $msg, $to_filter, $uniq_scope); |
| 435 |
confess "message queuing works only for STDERR" |
| 436 |
if $file ne \*STDERR; |
| 437 |
} |
| 438 |
|
| 439 |
# _dequeue ($QUEUE) |
| 440 |
# ----------------- |
| 441 |
# Pop a message from a queue, and print, similarly to how |
| 442 |
# _print_message would do it. Return 0 if the queue is |
| 443 |
# empty. Note that the key has already been dequeued. |
| 444 |
sub _dequeue ($) |
| 445 |
{ |
| 446 |
my ($queue) = @_; |
| 447 |
my $msg = $queue->dequeue || return 0; |
| 448 |
my $to_filter = $queue->dequeue; |
| 449 |
my $uniq_scope = $queue->dequeue; |
| 450 |
my $file = \*STDERR; |
| 451 |
|
| 452 |
if ($to_filter ne '') |
| 453 |
{ |
| 454 |
# Do we want local or global uniqueness? |
| 455 |
my $dups; |
| 456 |
if ($uniq_scope == US_LOCAL) |
| 457 |
{ |
| 458 |
$dups = \%_local_duplicate_messages; |
| 459 |
} |
| 460 |
elsif ($uniq_scope == US_GLOBAL) |
| 461 |
{ |
| 462 |
$dups = \%_global_duplicate_messages; |
| 463 |
} |
| 464 |
else |
| 465 |
{ |
| 466 |
confess "unknown value for uniq_scope: " . $uniq_scope; |
| 467 |
} |
| 468 |
|
| 469 |
# Update the hash of messages. |
| 470 |
if (exists $dups->{$to_filter}) |
| 471 |
{ |
| 472 |
++$dups->{$to_filter}; |
| 473 |
return 1; |
| 474 |
} |
| 475 |
else |
| 476 |
{ |
| 477 |
$dups->{$to_filter} = 0; |
| 478 |
} |
| 479 |
} |
| 480 |
print $file $msg; |
| 481 |
return 1; |
| 482 |
} |
| 483 |
|
| 484 |
|
| 485 |
# _print_message ($LOCATION, $MESSAGE, %OPTIONS) |
| 486 |
# ---------------------------------------------- |
| 487 |
# Format the message, check duplicates, and print it. |
| 488 |
sub _print_message ($$%) |
| 489 |
{ |
| 490 |
my ($location, $message, %opts) = @_; |
| 491 |
|
| 492 |
return 0 if ($opts{'silent'}); |
| 493 |
|
| 494 |
my $msg = _format_message ($location, $message, %opts); |
| 495 |
if ($opts{'partial'}) |
| 496 |
{ |
| 497 |
# Incomplete message. Store, don't print. |
| 498 |
$partial .= $msg; |
| 499 |
return; |
| 500 |
} |
| 501 |
else |
| 502 |
{ |
| 503 |
# Prefix with any partial message send so far. |
| 504 |
$msg = $partial . $msg; |
| 505 |
$partial = ''; |
| 506 |
} |
| 507 |
|
| 508 |
msg ('note', '', 'warnings are treated as errors', uniq_scope => US_GLOBAL) |
| 509 |
if ($opts{'type'} eq 'warning' && $warnings_are_errors); |
| 510 |
|
| 511 |
# Check for duplicate message if requested. |
| 512 |
my $to_filter; |
| 513 |
if ($opts{'uniq_part'} ne UP_NONE) |
| 514 |
{ |
| 515 |
# Which part of the error should we match? |
| 516 |
if ($opts{'uniq_part'} eq UP_TEXT) |
| 517 |
{ |
| 518 |
$to_filter = $message; |
| 519 |
} |
| 520 |
elsif ($opts{'uniq_part'} eq UP_LOC_TEXT) |
| 521 |
{ |
| 522 |
$to_filter = $msg; |
| 523 |
} |
| 524 |
else |
| 525 |
{ |
| 526 |
$to_filter = $opts{'uniq_part'}; |
| 527 |
} |
| 528 |
|
| 529 |
# Do we want local or global uniqueness? |
| 530 |
my $dups; |
| 531 |
if ($opts{'uniq_scope'} == US_LOCAL) |
| 532 |
{ |
| 533 |
$dups = \%_local_duplicate_messages; |
| 534 |
} |
| 535 |
elsif ($opts{'uniq_scope'} == US_GLOBAL) |
| 536 |
{ |
| 537 |
$dups = \%_global_duplicate_messages; |
| 538 |
} |
| 539 |
else |
| 540 |
{ |
| 541 |
confess "unknown value for uniq_scope: " . $opts{'uniq_scope'}; |
| 542 |
} |
| 543 |
|
| 544 |
# Update the hash of messages. |
| 545 |
if (exists $dups->{$to_filter}) |
| 546 |
{ |
| 547 |
++$dups->{$to_filter}; |
| 548 |
return 0; |
| 549 |
} |
| 550 |
else |
| 551 |
{ |
| 552 |
$dups->{$to_filter} = 0; |
| 553 |
} |
| 554 |
} |
| 555 |
my $file = $opts{'file'}; |
| 556 |
if ($opts{'ordered'} && $opts{'queue'}) |
| 557 |
{ |
| 558 |
_enqueue ($opts{'queue'}, $opts{'queue_key'}, $opts{'uniq_scope'}, |
| 559 |
$to_filter, $msg, $file); |
| 560 |
} |
| 561 |
else |
| 562 |
{ |
| 563 |
print $file $msg; |
| 564 |
} |
| 565 |
return 1; |
| 566 |
} |
| 567 |
|
| 568 |
=item C<msg ($channel, $location, $message, [%options])> |
| 569 |
|
| 570 |
Emit a message on C<$channel>, overriding some options of the channel with |
| 571 |
those specified in C<%options>. Obviously C<$channel> must have been |
| 572 |
registered with C<register_channel>. |
| 573 |
|
| 574 |
C<$message> is the text of the message, and C<$location> is a location |
| 575 |
associated to the message. |
| 576 |
|
| 577 |
For instance to complain about some unused variable C<mumble> |
| 578 |
declared at line 10 in F<foo.c>, one could do: |
| 579 |
|
| 580 |
msg 'unused', 'foo.c:10', "unused variable 'mumble'"; |
| 581 |
|
| 582 |
If channel C<unused> is not silent (and if this message is not a duplicate), |
| 583 |
the following would be output: |
| 584 |
|
| 585 |
foo.c:10: unused variable 'mumble' |
| 586 |
|
| 587 |
C<$location> can also be an instance of C<Autom4te::Location>. In this |
| 588 |
case, the stack of contexts will be displayed in addition. |
| 589 |
|
| 590 |
If C<$message> contains newline characters, C<$location> is prepended |
| 591 |
to each line. For instance, |
| 592 |
|
| 593 |
msg 'error', 'somewhere', "1st line\n2nd line"; |
| 594 |
|
| 595 |
becomes |
| 596 |
|
| 597 |
somewhere: 1st line |
| 598 |
somewhere: 2nd line |
| 599 |
|
| 600 |
If C<$location> is an empty string, it is replaced by the name of the |
| 601 |
program. Actually, if you don't use C<%options>, you can even |
| 602 |
elide the empty C<$location>. Thus |
| 603 |
|
| 604 |
msg 'fatal', '', 'fatal error'; |
| 605 |
msg 'fatal', 'fatal error'; |
| 606 |
|
| 607 |
both print |
| 608 |
|
| 609 |
progname: fatal error |
| 610 |
|
| 611 |
=cut |
| 612 |
|
| 613 |
|
| 614 |
# See buffer_messages() and flush_messages() below. |
| 615 |
our %buffering = (); # The map of channel types to buffer. |
| 616 |
our @backlog = (); # The buffer of messages. |
| 617 |
|
| 618 |
sub msg ($$;$%) |
| 619 |
{ |
| 620 |
my ($channel, $location, $message, %options) = @_; |
| 621 |
|
| 622 |
if (! defined $message) |
| 623 |
{ |
| 624 |
$message = $location; |
| 625 |
$location = ''; |
| 626 |
} |
| 627 |
|
| 628 |
if (!exists $channels{$channel}) |
| 629 |
{ |
| 630 |
# This can happen as a result of e.g. m4_warn([nonsense], [message]) |
| 631 |
# so it should not crash. |
| 632 |
report_bad_channel($channel, $location); |
| 633 |
$channel = 'syntax'; |
| 634 |
} |
| 635 |
|
| 636 |
my %opts = %{$channels{$channel}}; |
| 637 |
_merge_options (%opts, %options); |
| 638 |
|
| 639 |
if (exists $buffering{$opts{'type'}}) |
| 640 |
{ |
| 641 |
push @backlog, [$channel, $location->clone, $message, %options]; |
| 642 |
return; |
| 643 |
} |
| 644 |
|
| 645 |
# Print the message if needed. |
| 646 |
if (_print_message ($location, $message, %opts)) |
| 647 |
{ |
| 648 |
# Adjust exit status. |
| 649 |
if ($opts{'type'} eq 'error' |
| 650 |
|| $opts{'type'} eq 'fatal' |
| 651 |
|| ($opts{'type'} eq 'warning' && $warnings_are_errors)) |
| 652 |
{ |
| 653 |
my $es = $opts{'exit_code'}; |
| 654 |
$exit_code = $es if $es > $exit_code; |
| 655 |
} |
| 656 |
|
| 657 |
# Die on fatal messages. |
| 658 |
confess if $opts{'backtrace'}; |
| 659 |
if ($opts{'type'} eq 'fatal') |
| 660 |
{ |
| 661 |
# flush messages explicitly here, needed in worker threads. |
| 662 |
STDERR->flush; |
| 663 |
exit $exit_code; |
| 664 |
} |
| 665 |
} |
| 666 |
} |
| 667 |
|
| 668 |
sub report_bad_channel ($$) |
| 669 |
{ |
| 670 |
my ($channel, $location) = @_; |
| 671 |
my $message; |
| 672 |
my $report_as = 'error'; |
| 673 |
|
| 674 |
# quotemeta is both too aggressive (e.g. it escapes '-') and |
| 675 |
# too generous (it turns control characters into \ + themselves, |
| 676 |
# not into symbolic escapes). |
| 677 |
my $q_channel = $channel; |
| 678 |
$q_channel =~ s/(?=[\"\$\'\@\`\\])/\\/g; |
| 679 |
$q_channel =~ s/([^\x20-\x7e])/sprintf('\\x%02X', ord $1)/eg; |
| 680 |
$q_channel = '"' . $q_channel . '"'; |
| 681 |
|
| 682 |
if ($channel eq '' || $channel eq 'all') |
| 683 |
{ |
| 684 |
# Prior to version 2.70, the Autoconf manual said it was valid to use |
| 685 |
# "all" and the empty string as the category argument to m4_warn, so |
| 686 |
# don't treat those cases as errors. |
| 687 |
$report_as = 'obsolete'; |
| 688 |
$message = "use of $q_channel as a diagnostic category is obsolete\n"; |
| 689 |
$message .= "(see automake --help for a list of valid categories)"; |
| 690 |
} |
| 691 |
elsif ($channel eq 'none' |
| 692 |
|| ($channel =~ /^no-/ && exists $channels{substr($channel, 3)})) |
| 693 |
{ |
| 694 |
# Also recognize "none" and "no-[category]", as someone might have |
| 695 |
# thought anything acceptable to -W is also acceptable to m4_warn. |
| 696 |
# Note: m4_warn([error], [...]) does actually issue an error. |
| 697 |
$message = "-W accepts $q_channel, but it is not a diagnostic category"; |
| 698 |
} |
| 699 |
else |
| 700 |
{ |
| 701 |
$message = "unknown diagnostic category " . $q_channel; |
| 702 |
} |
| 703 |
|
| 704 |
msg $report_as, $location, $message; |
| 705 |
} |
| 706 |
|
| 707 |
|
| 708 |
=item C<setup_channel ($channel, %options)> |
| 709 |
|
| 710 |
Override the options of C<$channel> with those specified by C<%options>. |
| 711 |
|
| 712 |
=cut |
| 713 |
|
| 714 |
sub setup_channel ($%) |
| 715 |
{ |
| 716 |
my ($name, %opts) = @_; |
| 717 |
confess "unknown channel $name" unless exists $channels{$name}; |
| 718 |
_merge_options %{$channels{$name}}, %opts; |
| 719 |
} |
| 720 |
|
| 721 |
=item C<setup_channel_type ($type, %options)> |
| 722 |
|
| 723 |
Override the options of any channel of type C<$type> |
| 724 |
with those specified by C<%options>. |
| 725 |
|
| 726 |
=cut |
| 727 |
|
| 728 |
sub setup_channel_type ($%) |
| 729 |
{ |
| 730 |
my ($type, %opts) = @_; |
| 731 |
foreach my $channel (sort keys %channels) |
| 732 |
{ |
| 733 |
setup_channel $channel, %opts |
| 734 |
if $channels{$channel}{'type'} eq $type; |
| 735 |
} |
| 736 |
} |
| 737 |
|
| 738 |
=item C<dup_channel_setup ()>, C<drop_channel_setup ()> |
| 739 |
|
| 740 |
Sometimes it is necessary to make temporary modifications to channels. |
| 741 |
For instance one may want to disable a warning while processing a |
| 742 |
particular file, and then restore the initial setup. These two |
| 743 |
functions make it easy: C<dup_channel_setup ()> saves a copy of the |
| 744 |
current configuration for later restoration by |
| 745 |
C<drop_channel_setup ()>. |
| 746 |
|
| 747 |
You can think of this as a stack of configurations whose first entry |
| 748 |
is the active one. C<dup_channel_setup ()> duplicates the first |
| 749 |
entry, while C<drop_channel_setup ()> just deletes it. |
| 750 |
|
| 751 |
=cut |
| 752 |
|
| 753 |
our @_saved_channels = (); |
| 754 |
our @_saved_werrors = (); |
| 755 |
|
| 756 |
sub dup_channel_setup () |
| 757 |
{ |
| 758 |
my %channels_copy; |
| 759 |
foreach my $k1 (keys %channels) # sort keys not needed |
| 760 |
{ |
| 761 |
$channels_copy{$k1} = {%{$channels{$k1}}}; |
| 762 |
} |
| 763 |
push @_saved_channels, \%channels_copy; |
| 764 |
push @_saved_werrors, $warnings_are_errors; |
| 765 |
} |
| 766 |
|
| 767 |
sub drop_channel_setup () |
| 768 |
{ |
| 769 |
my $saved = pop @_saved_channels; |
| 770 |
%channels = %$saved; |
| 771 |
$warnings_are_errors = pop @_saved_werrors; |
| 772 |
} |
| 773 |
|
| 774 |
=item C<buffer_messages (@types)>, C<flush_messages ()> |
| 775 |
|
| 776 |
By default, when C<msg> is called, messages are processed immediately. |
| 777 |
|
| 778 |
Sometimes it is necessary to delay the output of messages. |
| 779 |
For instance you might want to make diagnostics before |
| 780 |
channels have been completely configured. |
| 781 |
|
| 782 |
After C<buffer_messages(@types)> has been called, messages sent with |
| 783 |
C<msg> to a channel whose type is listed in C<@types> will be stored in a |
| 784 |
list for later processing. |
| 785 |
|
| 786 |
This backlog of messages is processed when C<flush_messages> is |
| 787 |
called, with the current channel options (not the options in effect, |
| 788 |
at the time of C<msg>). So for instance, if some channel was silenced |
| 789 |
in the meantime, messages to this channel will not be printed. |
| 790 |
|
| 791 |
C<flush_messages> cancels the effect of C<buffer_messages>. Following |
| 792 |
calls to C<msg> are processed immediately as usual. |
| 793 |
|
| 794 |
=cut |
| 795 |
|
| 796 |
sub buffer_messages (@) |
| 797 |
{ |
| 798 |
foreach my $type (@_) |
| 799 |
{ |
| 800 |
$buffering{$type} = 1; |
| 801 |
} |
| 802 |
} |
| 803 |
|
| 804 |
sub flush_messages () |
| 805 |
{ |
| 806 |
%buffering = (); |
| 807 |
foreach my $args (@backlog) |
| 808 |
{ |
| 809 |
&msg (@$args); |
| 810 |
} |
| 811 |
@backlog = (); |
| 812 |
} |
| 813 |
|
| 814 |
=item C<setup_channel_queue ($queue, $key)> |
| 815 |
|
| 816 |
Set the queue to fill for each channel that is ordered, |
| 817 |
and the key to use for serialization. |
| 818 |
|
| 819 |
=cut |
| 820 |
sub setup_channel_queue ($$) |
| 821 |
{ |
| 822 |
my ($queue, $key) = @_; |
| 823 |
foreach my $channel (sort keys %channels) |
| 824 |
{ |
| 825 |
setup_channel $channel, queue => $queue, queue_key => $key |
| 826 |
if $channels{$channel}{'ordered'}; |
| 827 |
} |
| 828 |
} |
| 829 |
|
| 830 |
=item C<pop_channel_queue ($queue)> |
| 831 |
|
| 832 |
pop a message off the $queue; the key has already been popped. |
| 833 |
|
| 834 |
=cut |
| 835 |
sub pop_channel_queue ($) |
| 836 |
{ |
| 837 |
my ($queue) = @_; |
| 838 |
return _dequeue ($queue); |
| 839 |
} |
| 840 |
|
| 841 |
=back |
| 842 |
|
| 843 |
=head1 SEE ALSO |
| 844 |
|
| 845 |
L<Autom4te::Location> |
| 846 |
|
| 847 |
=head1 HISTORY |
| 848 |
|
| 849 |
Written by Alexandre Duret-Lutz E<lt>F<[email protected]>E<gt>. |
| 850 |
|
| 851 |
=cut |
| 852 |
|
| 853 |
1; |