Author: coke
Date: Thu Feb 21 07:26:25 2008
New Revision: 25932

Added:
   trunk/t/codingstd/c_macro_args.t   (contents, props changed)
Modified:
   trunk/MANIFEST

Log:
[codingstd]

Add a (naive?) test to check that args in macro definitions are wrapped.

RT #47109

There are currently 2 failures when running this test



Modified: trunk/MANIFEST
==============================================================================
--- trunk/MANIFEST      (original)
+++ trunk/MANIFEST      Thu Feb 21 07:26:25 2008
@@ -1,7 +1,7 @@
 # ex: set ro:
 # $Id$
 #
-# generated by tools/dev/mk_manifest_and_skip.pl Thu Feb 21 05:26:40 2008 UT
+# generated by tools/dev/mk_manifest_and_skip.pl Thu Feb 21 14:47:09 2008 UT
 #
 # See tools/dev/install_files.pl for documentation on the
 # format of this file.
@@ -2914,6 +2914,7 @@
 t/codingstd/c_function_docs.t                               []
 t/codingstd/c_header_guards.t                               []
 t/codingstd/c_indent.t                                      []
+t/codingstd/c_macro_args.t                                  []
 t/codingstd/c_operator.t                                    []
 t/codingstd/c_parens.t                                      []
 t/codingstd/c_returns.t                                     []

Added: trunk/t/codingstd/c_macro_args.t
==============================================================================
--- (empty file)
+++ trunk/t/codingstd/c_macro_args.t    Thu Feb 21 07:26:25 2008
@@ -0,0 +1,79 @@
+#! perl
+# Copyright (C) 2008, The Perl Foundation.
+# $Id $
+
+use strict;
+use warnings;
+
+use lib qw( . lib ../lib ../../lib );
+use Test::More tests => 1;
+use Parrot::Distribution;
+
+=head1 NAME
+
+t/codingstd/c_macro_args.t
+
+=head1 SYNOPSIS
+
+    # test all files
+    % prove t/codingstd/c_macro_args.t
+
+    # test specific files
+    % perl t/codingstd/c_macro_args.t src/foo.t include/parrot/bar.h
+
+=head1 DESCRIPTION
+
+Checks that no C source file in the distribution has unsafe macro args.
+
+=head1 SEE ALSO
+
+L<docs/pdds/pdd07_codingstd.pod>
+
+=cut
+
+my $DIST = Parrot::Distribution->new();
+my @files = @ARGV ? @ARGV : $DIST->get_c_language_files();
+
+check_macro_args(@files);
+
+sub check_macro_args {
+    my @files = @_;
+
+    my @defines;
+    foreach my $file (@files) {
+        my $path = @ARGV ? $file : $file->path();
+        my $buf = $DIST->slurp($path);
+        $buf =~ s{ (?:
+                       (?: ' (?: \\\\ | \\' | [^'] )* ' )  # remove ' string
+                     | (?: " (?: \\\\ | \\" | [^"] )* " )  # remove " string
+                     | /\* .*? \*/                         # remove C comment
+                   )
+                }{}gsx;
+
+        # combine lines extended with \\n
+        $buf =~ s/\\\n//g;  
+ 
+        if ( $buf =~ m{ ^ \s* \#define \s+ ([a-zA-Z_]+)  \( ([^)]+) \)  
([^\n]*) }smx ) {
+            my ($macro,$args,$definition) = ($1, $2, $3);
+            # for each of these args, verify it's wrapped in parens each time
+            # it's referenced in the definition.
+            if ($definition ne "") {
+                foreach my $arg (split /\s*,\s*/, $args) {
+                    if ($definition =~ m/\b[^(]$arg[^)]\b/) {
+                        push (@defines, "$path: $macro has unwrapped arg: 
$arg\n");
+                    }
+                }
+            }
+        }
+    }
+
+    ok( !scalar(@defines), 'unwrapped macro args' )
+        or diag( scalar @defines . " unsafe macro args found:[EMAIL 
PROTECTED]" );
+}
+
+# Local Variables:
+#   mode: cperl
+#   cperl-indent-level: 4
+#   fill-column: 100
+# End:
+# vim: expandtab shiftwidth=4:

Reply via email to