Skip to content
This repository
tag: RELEASE_1_7_0
Fetching contributors…

Cannot retrieve contributors at this time

file 81 lines (57 sloc) 1.996 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
#! perl
# Copyright (C) 2006-2009, Parrot Foundation.
# $Id$

use strict;
use warnings;

use lib qw( . lib ../lib ../../lib );
use Test::More tests => 1;
use Parrot::Distribution;

=head1 NAME

t/codingstd/check_toxxx.t - checks that the toxxx() functions are passed
unsigned char

=head1 SYNOPSIS

# test all files
% prove t/codingstd/check_toxxx.t

# test specific files
% perl t/codingstd/check_toxxx.t src/foo.c include/parrot/bar.h

=head1 DESCRIPTION

Checks all C language files to make sure that arguments to the toxxx()
functions are explicitly cast to unsigned char.

=head1 SEE ALSO

L<docs/pdds/pdd07_codingstd.pod>

=cut

my $DIST = Parrot::Distribution->new;
my @files = @ARGV ? <@ARGV> : $DIST->get_c_language_files();
my @no_explicit_cast;
my $toxxx_functions = "toupper|tolower";

foreach my $file (@files) {

    # if we have command line arguments, the file is the full path
    # otherwise, use the relevant Parrot:: path method
    my $path = @ARGV ? $file : $file->path;

    my $buf = $DIST->slurp($path);

    my @buffer_lines = split( /\n/, $buf );

    # find out if toxxx() functions appear in the file
    my $num_toxxx = grep m/($toxxx_functions)\(/, @buffer_lines;

    # if so, check if the args are cast to unsigned char
    if ($num_toxxx) {

        # get the lines just matching toxxx
        my @toxxx_lines = grep m/($toxxx_functions)\(/, @buffer_lines;

        # find the instances without the explicit cast
        my $num_no_cast = grep !m/($toxxx_functions)\(\(unsigned char\)/, @toxxx_lines;

        $path .= "\n";
        push @no_explicit_cast, $path if $num_no_cast;
    }
    else {
        next;
    }
}

ok( !scalar(@no_explicit_cast), 'toxxx() functions cast correctly' )
    or diag( "toxxx() function not cast to unsigned char "
        . scalar @no_explicit_cast
        . " files:\n@no_explicit_cast" );

# Local Variables:
# mode: cperl
# cperl-indent-level: 4
# fill-column: 100
# End:
# vim: expandtab shiftwidth=4:
Something went wrong with that request. Please try again.