2
0

ChannelDefs.pm 11 KB

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