From: "jerry gay" <[EMAIL PROTECTED]> Date: Sat, 28 Jul 2007 08:35:01 -0700
On 7/28/07, Bob Rogers <[EMAIL PROTECTED]> wrote: [sorry; forgot to attach the attachment.] > As a step in that direction, is there an easy way to find out which > coding standards tests should be applied to a given source file? Some > have "c_*" names, which makes it seem pretty obvious . . . until you > consider *.pmc and *.ops files, which are "sorta C." > > I am asking because I just sat down to write a > M-x parrot-check-coding-standards command for Emacs, but realized that I > didn't know which tests to run. I could read the test code and hardwire > a mapping, but that would be wicked fragile. Is a naming convention > sufficient, or would we need a metacomment in the test file? Parrot::Distribution offers a number of subroutines which allow one to specify which subset of parrot files you wish to select for testing. this is built into the tests, and if everything is working as it should (and i believe it is,) it means that calling the proper function from this distribution will provide the proper subset of files (e.g. c_language_files.) I want to go in the other direction, i.e. what tests should I apply for a specific C source file, along the lines of the attached patch. It needs doc, of course, and the correctness of %tests_from_source_category is debatable, but that's the general idea. Beyond that, it would be nice if the tests themselves output something close to the standard "grep -n" output, i.e. "file:line:" at the start of each "hit" line. That way, if run under Emacs, the resulting output buffer could be put into grep-mode, which would make it easy to visit these "hits." Finally, it would be really nice if this could be run automatically from Emacs every time I save a Parrot source file. But it would have to run faster for that to work. > P.S. I have been having problems with getting parrot-porters to accept > my posts, so I don't expect this to appear on the list. received just fine :) Huh!? How could it be that you get a copy from the list and not me? I seem to be getting everybody else's posts . . . -- Bob
* tools/dev/check-coding-stds.pl (added): + Check files specified on the command line for coding standard violations. All appropriate tests are run for the * lib/Parrot/Distribution.pm: + (classify_file): Return source category for a given file. Diffs between last version checked in and current workfile(s): Index: tools/dev/check-coding-stds.pl =================================================================== --- tools/dev/check-coding-stds.pl (revision 0) +++ tools/dev/check-coding-stds.pl (revision 0) @@ -0,0 +1,61 @@ +#! /usr/bin/perl -w + +use strict; +use warnings; + +use lib 'lib'; + +use Parrot::Distribution; +use Data::Dumper; + +my $dist = Parrot::Distribution->new(); + +die "Usage: $0 file ...\n" + unless @ARGV; + +# print Dumper($dist); + +### Subroutines. + +my @common_tests = qw(fixme.t); +my @c_common_tests = (@common_tests, + qw(c_code_coda.t c_indent.t c_struct.t cppcomments.t + tabs.t c_parens.t trailing_space.t)); +my %tests_from_source_category + = (c_source => [ @c_common_tests, + qw(check_toxxx.t check_isxxx.t cuddled_else.t) ], + c_header => [ @c_common_tests, qw(c_header_guards.t) ], + perl_source => [ @common_tests, qw(perlcritic.t) ], + pir_source => [ @common_tests, qw(pir_code_coda.t) ], + pmc_source => [ @common_tests ], + yacc_source => [ @common_tests ], + lex_source => [ @common_tests ], + ops_source => [ @common_tests ], + ); +my $t_codingstd_dir = 't/codingstd'; + +# die Dumper(\%tests_from_source_category); + +sub test_files_for_category { + my $category = shift; + + my $tests = $tests_from_source_category{$category}; + return + unless $tests; + return map { "$t_codingstd_dir/$_"; } @$tests; +} + +### Main code. + +for my $file (@ARGV) { + my $class = $dist->classify_file($file); + if (! $class) { + warn "Can't classify '$file'"; + next; + } + print "$file => $class\n"; + for my $test (test_files_for_category($class)) { + # print " got test $test\n"; + system('perl', $test, $file); + } +} Property changes on: tools/dev/check-coding-stds.pl ___________________________________________________________________ Name: svn:executable + * Index: lib/Parrot/Distribution.pm =================================================================== --- lib/Parrot/Distribution.pm (revision 20226) +++ lib/Parrot/Distribution.pm (working copy) @@ -178,6 +178,7 @@ =cut BEGIN { + # NB: This hash gets decorated in the loop below. my %file_class = ( source => { c => { file_exts => ['c'] }, @@ -217,9 +218,10 @@ '|' => map { qr{\b$_\b} } map { quotemeta($_) } @ignore_dirs, @exceptions; + # Save these for future use. + $file_class{$class}{$type}{extension_filter_re} = $filter_ext; + $file_class{$class}{$type}{directory_filter_re} = $filter_dir; - next unless $method; - *{ $method . '_file_directories' } = sub { my $self = shift; @@ -276,6 +278,25 @@ }; } } + + sub classify_file { + my ($self, $file) = @_; + + my @result; + for my $class ( keys %file_class ) { + for my $type ( keys %{ $file_class{$class} } ) { + my $filter_ext + = $file_class{$class}{$type}{extension_filter_re}; + next + unless $file =~ m|(?i)(?:$filter_ext)|; + my $filter_dir + = $file_class{$class}{$type}{directory_filter_re}; + next + if $file =~ m|(?:$filter_dir)|; + return join('_', $type, $class); + } + } + } } =item C<get_c_language_files()> End of diffs.