Commit 7ddcc8fe authored by Daniel Stenberg's avatar Daniel Stenberg
Browse files

checksrc: scan many files, more checks

It now scans multiple files and outputs an error+warning count summary
at the end in case at least one was detected.

-D can be used to specify in which dir the files are located

The script now scans for conditions that starts with a space for
if/while/for lines.
parent 068d656c
Loading
Loading
Loading
Loading
+120 −75
Original line number Diff line number Diff line
#!/usr/bin/perl

my $file=$ARGV[0];
#***************************************************************************
#                                  _   _ ____  _
#  Project                     ___| | | |  _ \| |
#                             / __| | | | |_) | |
#                            | (__| |_| |  _ <| |___
#                             \___|\___/|_| \_\_____|
#
# Copyright (C) 2011, Daniel Stenberg, <daniel@haxx.se>, et al.
#
# This software is licensed as described in the file COPYING, which
# you should have received as part of this distribution. The terms
# are also available at http://curl.haxx.se/docs/copyright.html.
#
# You may opt to use, copy, modify, merge, publish, distribute and/or sell
# copies of the Software, and permit persons to whom the Software is
# furnished to do so, under the terms of the COPYING file.
#
# This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
# KIND, either express or implied.
#
###########################################################################

my $max_column = 79;
my $indent = 2;

my $warnings;
my $errors;
my $file;
my $dir=".";

sub checkwarn {
    my ($num, $col, $file, $line, $msg, $error) = @_;
@@ -30,14 +51,35 @@ sub checkwarn {
    }
}

$file = shift @ARGV;

if($file =~ /-D(.*)/) {
    $dir = $1;
    $file = shift @ARGV;
}

if(!$file) {
    print "checksrc.pl <single C or H file>\n";
    print "checksrc.pl [option] <file1> [file2] ...\n";
    print " Options:\n";
    print "  -D[DIR]   Directory to prepend file names\n";
    exit;
}

do {
    scanfile("$dir/$file");

    $file = shift @ARGV;

} while($file);


sub scanfile {
    my ($file) = @_;

    my $line = 1;
open(R, "<$file") || die;
    my $prevl;
    my $l;
    open(R, "<$file") || die "failed to open $file";

    my $copyright=0;

@@ -64,12 +106,6 @@ while(<R>) {
            checkwarn($line, length($1), $file, $l, "Trailing whitespace");
        }

    # detect return statements with parenthesis
    # doesn't really work unless we filter off typecasts
    #if($l =~ /(.*)return \(/) {
    #    checkwarn($line, length($1)+6, $file, $l, "return with paretheses");
    #}

        # check spaces after for/if/while
        if($l =~ /^(.*)(for|if|while) \(/) {
            if($1 =~ / *\#/) {
@@ -80,6 +116,18 @@ while(<R>) {
                          "$2 with space");
            }
        }

        # check spaces after open paren after for/if/while
        if($l =~ /^(.*)(for|if|while)\( /) {
            if($1 =~ / *\#/) {
                # this is a #if, treat it differently
            }
            else {
                checkwarn($line, length($1)+length($2)+1, $file, $l,
                          "$2 with space first in condition");
            }
        }

        # check for "} else"
        if($l =~ /^(.*)\} else/) {
            checkwarn($line, length($1), $file, $l, "else after closing brace on same line");
@@ -110,13 +158,6 @@ while(<R>) {
            }
        }

    # check for // letters, but skip them if a double quote or asterisk was
    # on the same line to avoid strings and comments. Not reliable.
    #if($l =~ /^([^\"*]*)\/\//) {
    #    checkwarn($line, length($1), $file, $l, "non-C89 compliant comment",
    #              1);
    #}

        $line++;
        $prevl = $l;
    }
@@ -127,6 +168,10 @@ if(!$copyright) {

    close(R);

}


if($errors || $warnings) {
    printf "checksrc: %d errors and %d warnings\n", $errors, $warnings;
    exit 5; # return failure
}