ChannelDefs.pm 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  1. # Copyright (C) 2002-2003, 2006, 2008-2012 Free Software Foundation,
  2. # Inc.
  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 3 of the License, or
  6. # (at your option) any later version.
  7. # This program is distributed in the hope that it will be useful,
  8. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. # GNU General Public License for more details.
  11. # You should have received a copy of the GNU General Public License
  12. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  13. package Autom4te::ChannelDefs;
  14. use Autom4te::Channels;
  15. =head1 NAME
  16. Autom4te::ChannelDefs - channel definitions for Automake and helper functions
  17. =head1 SYNOPSIS
  18. use Autom4te::ChannelDefs;
  19. print Autom4te::ChannelDefs::usage (), "\n";
  20. prog_error ($MESSAGE, [%OPTIONS]);
  21. error ($WHERE, $MESSAGE, [%OPTIONS]);
  22. error ($MESSAGE);
  23. fatal ($WHERE, $MESSAGE, [%OPTIONS]);
  24. fatal ($MESSAGE);
  25. verb ($MESSAGE, [%OPTIONS]);
  26. switch_warning ($CATEGORY);
  27. parse_WARNINGS ();
  28. parse_warnings ($OPTION, @ARGUMENT);
  29. Autom4te::ChannelDefs::set_strictness ($STRICTNESS_NAME);
  30. =head1 DESCRIPTION
  31. This package defines channels that can be used in Automake to
  32. output diagnostics and other messages (via C<msg()>). It also defines
  33. some helper function to enable or disable these channels, and some
  34. shorthand function to output on specific channels.
  35. =cut
  36. use 5.006;
  37. use strict;
  38. use Exporter;
  39. use vars qw (@ISA @EXPORT);
  40. @ISA = qw (Exporter);
  41. @EXPORT = qw (&prog_error &error &fatal &verb
  42. &switch_warning &parse_WARNINGS &parse_warnings);
  43. =head2 CHANNELS
  44. The following channels can be used as the first argument of
  45. C<Autom4te::Channel::msg>. For some of them we list a shorthand
  46. function that makes the code more readable.
  47. =over 4
  48. =item C<fatal>
  49. Fatal errors. Use C<&fatal> to send messages over this channel.
  50. =item C<error>
  51. Common errors. Use C<&error> to send messages over this channel.
  52. =item C<error-gnu>
  53. Errors related to GNU Standards.
  54. =item C<error-gnu/warn>
  55. Errors related to GNU Standards that should be warnings in "foreign" mode.
  56. =item C<error-gnits>
  57. Errors related to GNITS Standards (silent by default).
  58. =item C<automake>
  59. Internal errors. Use C<&prog_error> to send messages over this channel.
  60. =item C<cross>
  61. Constructs compromising the cross-compilation of the package.
  62. =item C<gnu>
  63. Warnings related to GNU Coding Standards.
  64. =item C<obsolete>
  65. Warnings about obsolete features (silent by default).
  66. =item C<override>
  67. Warnings about user redefinitions of Automake rules or
  68. variables (silent by default).
  69. =item C<portability>
  70. Warnings about non-portable constructs.
  71. =item C<syntax>
  72. Warnings about weird syntax, unused variables, typos ...
  73. =item C<unsupported>
  74. Warnings about unsupported (or mis-supported) features.
  75. =item C<verb>
  76. Messages output in C<--verbose> mode. Use C<&verb> to send such messages.
  77. =item C<note>
  78. Informative messages.
  79. =back
  80. =cut
  81. # Initialize our list of error/warning channels.
  82. # Do not forget to update &usage and the manual
  83. # if you add or change a warning channel.
  84. register_channel 'fatal', type => 'fatal', ordered => 0;
  85. register_channel 'error', type => 'error';
  86. register_channel 'error-gnu', type => 'error';
  87. register_channel 'error-gnu/warn', type => 'error';
  88. register_channel 'error-gnits', type => 'error', silent => 1;
  89. register_channel 'automake', type => 'fatal', backtrace => 1,
  90. header => ("####################\n" .
  91. "## Internal Error ##\n" .
  92. "####################\n"),
  93. footer => "\nPlease contact <bug-automake\@gnu.org>.",
  94. ordered => 0;
  95. register_channel 'cross', type => 'warning', silent => 1;
  96. register_channel 'gnu', type => 'warning';
  97. register_channel 'obsolete', type => 'warning', silent => 1;
  98. register_channel 'override', type => 'warning', silent => 1;
  99. register_channel 'portability', type => 'warning', silent => 1;
  100. register_channel 'syntax', type => 'warning';
  101. register_channel 'unsupported', type => 'warning';
  102. register_channel 'verb', type => 'debug', silent => 1, ordered => 0;
  103. register_channel 'note', type => 'debug', silent => 0;
  104. =head2 FUNCTIONS
  105. =over 4
  106. =item C<usage ()>
  107. Return the warning category descriptions.
  108. =cut
  109. sub usage ()
  110. {
  111. return "Warning categories include:
  112. `cross' cross compilation issues
  113. `gnu' GNU coding standards (default in gnu and gnits modes)
  114. `obsolete' obsolete features or constructions
  115. `override' user redefinitions of Automake rules or variables
  116. `portability' portability issues (default in gnu and gnits modes)
  117. `syntax' dubious syntactic constructs (default)
  118. `unsupported' unsupported or incomplete features (default)
  119. `all' all the warnings
  120. `no-CATEGORY' turn off warnings in CATEGORY
  121. `none' turn off all the warnings
  122. `error' treat warnings as errors";
  123. }
  124. =item C<prog_error ($MESSAGE, [%OPTIONS])>
  125. Signal a programming error (on channel C<automake>),
  126. display C<$MESSAGE>, and exit 1.
  127. =cut
  128. sub prog_error ($;%)
  129. {
  130. my ($msg, %opts) = @_;
  131. msg 'automake', '', $msg, %opts;
  132. }
  133. =item C<error ($WHERE, $MESSAGE, [%OPTIONS])>
  134. =item C<error ($MESSAGE)>
  135. Uncategorized errors.
  136. =cut
  137. sub error ($;$%)
  138. {
  139. my ($where, $msg, %opts) = @_;
  140. msg ('error', $where, $msg, %opts);
  141. }
  142. =item C<fatal ($WHERE, $MESSAGE, [%OPTIONS])>
  143. =item C<fatal ($MESSAGE)>
  144. Fatal errors.
  145. =cut
  146. sub fatal ($;$%)
  147. {
  148. my ($where, $msg, %opts) = @_;
  149. msg ('fatal', $where, $msg, %opts);
  150. }
  151. =item C<verb ($MESSAGE, [%OPTIONS])>
  152. C<--verbose> messages.
  153. =cut
  154. sub verb ($;%)
  155. {
  156. my ($msg, %opts) = @_;
  157. msg 'verb', '', $msg, %opts;
  158. }
  159. =item C<switch_warning ($CATEGORY)>
  160. If C<$CATEGORY> is C<mumble>, turn on channel C<mumble>.
  161. If it is C<no-mumble>, turn C<mumble> off.
  162. Else handle C<all> and C<none> for completeness.
  163. =cut
  164. sub switch_warning ($)
  165. {
  166. my ($cat) = @_;
  167. my $has_no = 0;
  168. if ($cat =~ /^no-(.*)$/)
  169. {
  170. $cat = $1;
  171. $has_no = 1;
  172. }
  173. if ($cat eq 'all')
  174. {
  175. setup_channel_type 'warning', silent => $has_no;
  176. }
  177. elsif ($cat eq 'none')
  178. {
  179. setup_channel_type 'warning', silent => ! $has_no;
  180. }
  181. elsif ($cat eq 'error')
  182. {
  183. $warnings_are_errors = ! $has_no;
  184. # Set exit code if Perl warns about something
  185. # (like uninitialized variables).
  186. $SIG{"__WARN__"} =
  187. $has_no ? 'DEFAULT' : sub { print STDERR @_; $exit_code = 1; };
  188. }
  189. elsif (channel_type ($cat) eq 'warning')
  190. {
  191. setup_channel $cat, silent => $has_no;
  192. }
  193. else
  194. {
  195. return 1;
  196. }
  197. return 0;
  198. }
  199. =item C<parse_WARNINGS ()>
  200. Parse the WARNINGS environment variable.
  201. =cut
  202. sub parse_WARNINGS ()
  203. {
  204. if (exists $ENV{'WARNINGS'})
  205. {
  206. # Ignore unknown categories. This is required because WARNINGS
  207. # should be honored by many tools.
  208. switch_warning $_ foreach (split (',', $ENV{'WARNINGS'}));
  209. }
  210. }
  211. =item C<parse_warnings ($OPTION, @ARGUMENT)>
  212. Parse the argument of C<--warning=CATEGORY> or C<-WCATEGORY>.
  213. C<$OPTIONS> is C<"--warning"> or C<"-W">, C<@ARGUMENT> is a list of
  214. C<CATEGORY>.
  215. This can be used as an argument to C<Getopt>.
  216. =cut
  217. sub parse_warnings ($@)
  218. {
  219. my ($opt, @categories) = @_;
  220. foreach my $cat (map { split ',' } @categories)
  221. {
  222. msg 'unsupported', "unknown warning category `$cat'"
  223. if switch_warning $cat;
  224. }
  225. }
  226. =item C<set_strictness ($STRICTNESS_NAME)>
  227. Configure channels for strictness C<$STRICTNESS_NAME>.
  228. =cut
  229. sub set_strictness ($)
  230. {
  231. my ($name) = @_;
  232. if ($name eq 'gnu')
  233. {
  234. setup_channel 'error-gnu', silent => 0;
  235. setup_channel 'error-gnu/warn', silent => 0, type => 'error';
  236. setup_channel 'error-gnits', silent => 1;
  237. setup_channel 'portability', silent => 0;
  238. setup_channel 'gnu', silent => 0;
  239. }
  240. elsif ($name eq 'gnits')
  241. {
  242. setup_channel 'error-gnu', silent => 0;
  243. setup_channel 'error-gnu/warn', silent => 0, type => 'error';
  244. setup_channel 'error-gnits', silent => 0;
  245. setup_channel 'portability', silent => 0;
  246. setup_channel 'gnu', silent => 0;
  247. }
  248. elsif ($name eq 'foreign')
  249. {
  250. setup_channel 'error-gnu', silent => 1;
  251. setup_channel 'error-gnu/warn', silent => 0, type => 'warning';
  252. setup_channel 'error-gnits', silent => 1;
  253. setup_channel 'portability', silent => 1;
  254. setup_channel 'gnu', silent => 1;
  255. }
  256. else
  257. {
  258. prog_error "level `$name' not recognized\n";
  259. }
  260. }
  261. =back
  262. =head1 SEE ALSO
  263. L<Autom4te::Channels>
  264. =head1 HISTORY
  265. Written by Alexandre Duret-Lutz E<lt>F<adl@gnu.org>E<gt>.
  266. =cut
  267. ### Setup "GNU" style for perl-mode and cperl-mode.
  268. ## Local Variables:
  269. ## perl-indent-level: 2
  270. ## perl-continued-statement-offset: 2
  271. ## perl-continued-brace-offset: 0
  272. ## perl-brace-offset: 0
  273. ## perl-brace-imaginary-offset: 0
  274. ## perl-label-offset: -2
  275. ## cperl-indent-level: 2
  276. ## cperl-brace-offset: 0
  277. ## cperl-continued-brace-offset: 0
  278. ## cperl-label-offset: -2
  279. ## cperl-extra-newline-before-brace: t
  280. ## cperl-merge-trailing-else: nil
  281. ## cperl-continued-statement-offset: 2
  282. ## End: