| 1 | #!/usr/bin/perl -w |
| 2 | use strict; |
| 3 | use warnings; |
| 4 | use 5.010; |
| 5 | use File::Find; |
| 6 | use IO::File; |
| 7 | use Getopt::Long; |
| 8 | use Pod::Usage; |
| 9 | |
| 10 | my %limits = ( |
| 11 | c90 => { |
| 12 | 'logical-source-line-length' => 509, |
| 13 | }, |
| 14 | c99 => { |
| 15 | 'logical-source-line-length' => 4095, |
| 16 | }, |
| 17 | ); |
| 18 | |
| 19 | my %opt = ( |
| 20 | std => 'c99', |
| 21 | ); |
| 22 | |
| 23 | GetOptions(\%opt, qw( logical-source-line-length=i std=s )) |
| 24 | && @ARGV && exists $limits{$opt{std}} |
| 25 | or pod2usage(2); |
| 26 | |
| 27 | for my $k (keys %{$limits{$opt{std}}}) { |
| 28 | $opt{$k} //= $limits{$opt{std}}{$k}; |
| 29 | } |
| 30 | |
| 31 | { |
| 32 | my $num = 1; |
| 33 | |
| 34 | sub report |
| 35 | { |
| 36 | my $msg = shift; |
| 37 | my $info = join '', @_; |
| 38 | |
| 39 | if ($info) { |
| 40 | $info =~ s/\R+$//; |
| 41 | $info =~ s/^/ #|\t/mg; |
| 42 | $info = "\n$info\n\n"; |
| 43 | } |
| 44 | |
| 45 | warn sprintf "[%d] %s(%d): %s\n%s", |
| 46 | $num++, $File::Find::name, $., $msg, $info; |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | find(sub { |
| 51 | /\.([ch]|xs)$/ or return; |
| 52 | |
| 53 | my $fh = IO::File->new($_, 'r') or die "$_: $!\n"; |
| 54 | my $ll = ''; |
| 55 | |
| 56 | while (defined(my $line = <$fh>)) { |
| 57 | report("trailing whitespace after backslash", $line) |
| 58 | if $line =~ /\\[[:blank:]]+$/; |
| 59 | |
| 60 | $ll .= $line; |
| 61 | |
| 62 | unless ($ll =~ /\\$/) { |
| 63 | if (length $ll > $opt{'logical-source-line-length'}) { |
| 64 | report(sprintf("logical source line too long (%d > %d)", |
| 65 | length $ll, $opt{'logical-source-line-length'}), $ll); |
| 66 | } |
| 67 | $ll = ''; |
| 68 | } |
| 69 | } |
| 70 | }, @ARGV); |
| 71 | |
| 72 | __END__ |
| 73 | |
| 74 | =head1 NAME |
| 75 | |
| 76 | checkansi.pl - Check source code for ANSI-C violations |
| 77 | |
| 78 | =head1 SYNOPSIS |
| 79 | |
| 80 | checkansi.pl [B<--std>=c90|c99] |
| 81 | [B<--logical-source-line-length>=I<num>] |
| 82 | <path> ... |
| 83 | |
| 84 | =head1 DESCRIPTION |
| 85 | |
| 86 | B<checkansi.pl> searches |
| 87 | |
| 88 | =head1 OPTIONS |
| 89 | |
| 90 | =over 4 |
| 91 | |
| 92 | =item B<--std>=c90|c99 |
| 93 | |
| 94 | Choose the ANSI/ISO standard against which shall be checked. |
| 95 | Defaults to C<c99>. |
| 96 | |
| 97 | =item B<--logical-source-line-length>=I<number> |
| 98 | |
| 99 | Maximum length of a logical source line. Overrides the default |
| 100 | given by the chosen standard. |
| 101 | |
| 102 | =back |
| 103 | |
| 104 | =head1 COPYRIGHT |
| 105 | |
| 106 | Copyright 2007 by Marcus Holland-Moritz <mhx@cpan.org>. |
| 107 | |
| 108 | This program is free software; you may redistribute it |
| 109 | and/or modify it under the same terms as Perl itself. |
| 110 | |
| 111 | =cut |