2 # Copyright (C) 2006-2010, Parrot Foundation.
8 use lib qw( . lib ../lib ../../lib );
9 use Test::More tests => 3;
10 use Parrot::Distribution;
15 t/codingstd/c_parens.t - checks for rules related to parentheses in C source
20 % prove t/codingstd/c_parens.t
23 % perl t/codingstd/c_parens.t src/foo.c include/parrot/bar.h
27 Checks that all C language source files have the proper use of parentheses,
28 as specified in PDD07.
32 L<docs/pdds/pdd07_codingstd.pod>
36 my $keywords = join '|' => sort { length $a <=> length $b } qw/
37 auto double int struct INTVAL
38 break else long switch UINTVAL
39 case enum register typedef FLOATVAL
40 char extern return union PIOOFF_T
41 const float short unsigned
42 continue for signed void
43 default goto sizeof volatile opcode_t
44 do if static while size_t
46 my $DIST = Parrot::Distribution->new;
47 my @files = @ARGV ? <@ARGV> : $DIST->get_c_language_files();
54 my $parser = Pod::Simple->new();
56 $parser->output_string( \$non_pod_buf );
57 # set up a code handler to get at the non-pod
58 # thanks to Thomas Klausner's Pod::Strip for the inspiration
59 $parser->code_handler(
61 print {$_[2]{output_fh}} $_[0], "\n";
63 $parser->parse_string_document( $buf );
70 my @non_keyword_paren;
71 my @space_between_parens;
73 foreach my $file (@_) {
74 my $path = @ARGV ? $file : $file->path();
76 my $buf = $DIST->slurp($path);
78 # only strip pod from .ops files
79 if ( $path =~ m/\.ops$/ ) {
80 $buf = strip_pod($buf);
83 # strip ', ", and C comments
85 (?: (') (?: \\\\ | \\' | [^'] )* (') ) # remove ' string
86 | (?: (") (?: \\\\ | \\" | [^"] )* (") ) # remove " string
87 | /(\*) .*? (\*)/ # remove C comment
89 }{defined $1 ? "$1$2" : defined $3 ? "$3$4" : "$5$6"}egsx;
91 my @lines = split( /\n/, $buf );
92 for my $line (@lines) {
93 # skip #defines and typedefs
94 next if $line =~ m{(?:(#\s*define|^\s*typedef))};
95 if ( $line =~ m{ ( (?<!\w) (?:$keywords) (?: \( ) ) }xo ) {
98 # ops use the same names as some C keywords, so skip
99 next if $line =~ m{^op};
100 push @keyword_paren => "$path: $paren";
102 if ( $line =~ m{ ( (?<!\w) (?!(?:$keywords)\W) \w+ \s+ \( ) }xo ) {
103 push @non_keyword_paren => "$path: $1";
105 if ( $line =~ m{ ( \( [ \t]+ [^\n] | [^\n] [ \t]+ \) ) }x ) {
106 push @space_between_parens => "$path: $1";
111 ## L<PDD07/Code Formatting/"there should be at least one space between a C keyword and any subsequent open parenthesis">
112 is( join("\n",@keyword_paren), "", <<END_DESCRIPTION);
113 there should be at least one space between a C
114 keyword and any subsequent open parenthesis
117 ## L<PDD07/Code Formatting/"There should be no space between a function name and the following open parenthesis">
118 is( join("\n",@non_keyword_paren), "", <<END_DESCRIPTION);
119 There should be no space between a function name
120 and the following open parenthesis
123 ## L<PDD07/Code Formatting/"parentheses should not have space immediately after the opening parenthesis nor immediately before the closing parenthesis">
124 is( join("\n",@space_between_parens), "", <<END_DESCRIPTION);
125 parentheses should not have space immediately
126 after the opening parenthesis nor immediately
127 before the closing parenthesis
134 # cperl-indent-level: 4
137 # vim: expandtab shiftwidth=4: