check-spacing.pl 7.4 KB
Newer Older
1
#!/usr/bin/env perl
2
#
3
# check-spacing.pl: Report any usage of 'function (..args..)'
4
# Also check for other syntax issues, such as correct use of ';'
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library.  If not, see
# <http://www.gnu.org/licenses/>.
#
# Authors:
#     Daniel P. Berrange <berrange@redhat.com>

use strict;
use warnings;

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
#
# CheckFunctionBody:
# $_[0]: $data(in)
# $_[1]: $location(in), which format is file-path:line-num:line-code
# $_[2]: $fn_linenum(inout), maintains start line-num of function body
# Returns 0 in case of success or 1 on failure
#
# Check incorrect indentation and blank first line in function body.
# For efficiency, it only checks the first line of function body.
# But it's enough for most cases.
# (It could be better that we use *state* to declare @fn_linenum and
#  move it into this subroutine. But *state* requires version >= v5.10.)
#
sub CheckFunctionBody {
    my $ret = 0;
    my ($data, $location, $fn_linenum) = @_;

    # Check first line of function block
    if ($$fn_linenum) {
        if ($$data =~ /^\s*$/) {
            print "Blank line before content in function body:\n$$location";
            $ret = 1;
        } elsif ($$data !~ /^[ ]{4}\S/) {
            unless ($$data =~ /^[ ]\w+:$/ || $$data =~ /^}/) {
                print "Incorrect indentation in function body:\n$$location";
                $ret = 1;
            }
        }
        $$fn_linenum = 0;
    }

    # Detect start of function block
    if ($$data =~ /^{$/) {
        $$fn_linenum = $.;
    }

    return $ret;
}

65 66 67 68
my $ret = 0;
my $incomment = 0;

foreach my $file (@ARGV) {
69 70 71 72
    # Per-file variables for multiline Curly Bracket (cb_) check
    my $cb_linenum = 0;
    my $cb_code = "";
    my $cb_scolon = 0;
73
    my $fn_linenum = 0;
74

75 76 77 78
    open FILE, $file;

    while (defined (my $line = <FILE>)) {
        my $data = $line;
79
        my $location = "$file:$.:\n$line";
80 81
        # For temporary modifications
        my $tmpdata;
82

83 84
        # Kill any quoted , ; = or "
        $data =~ s/'[";,=]'/'X'/g;
85

86
        # Kill any quoted strings
87
        $data =~ s,"(?:[^\\\"]|\\.)*","XXX",g;
88 89 90 91 92 93

        # Kill any C++ style comments
        $data =~ s,//.*$,//,;

        next if $data =~ /^#/;

94
        $ret = 1 if CheckFunctionBody(\$data, \$location, \$fn_linenum);
95

96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
        # Kill contents of multi-line comments
        # and detect end of multi-line comments
        if ($incomment) {
            if ($data =~ m,\*/,) {
                $incomment = 0;
                $data =~ s,^.*\*/,*/,;
            } else {
                $data = "";
            }
        }

        # Kill single line comments, and detect
        # start of multi-line comments
        if ($data =~ m,/\*.*\*/,) {
            $data =~ s,/\*.*\*/,/* */,;
        } elsif ($data =~ m,/\*,) {
            $incomment = 1;
            $data =~ s,/\*.*,/*,;
        }

        # We need to match things like
        #
        #  int foo (int bar, bool wizz);
        #  foo (bar, wizz);
        #
        # but not match things like:
        #
        #  typedef int (*foo)(bar wizz)
        #
        # we can't do this (efficiently) without
        # missing things like
        #
        #  foo (*bar, wizz);
        #
130 131 132 133
        # We also don't want to spoil the $data so it can be used
        # later on.
        $tmpdata = $data;
        while ($tmpdata =~ /(\w+)\s\((?!\*)/) {
134 135 136
            my $kw = $1;

            # Allow space after keywords only
137 138
            if ($kw =~ /^(?:if|for|while|switch|return)$/) {
                $tmpdata =~ s/(?:$kw\s\()/XXX(/;
139
            } else {
140
                print "Whitespace after non-keyword:\n";
141 142 143 144 145 146
                print "$file:$.: $line";
                $ret = 1;
                last;
            }
        }

147
        # Require whitespace immediately after keywords
148
        if ($data =~ /\b(?:if|for|while|switch|return)\(/) {
149
            print "No whitespace after keyword:\n";
150 151 152 153 154
            print "$file:$.: $line";
            $ret = 1;
        }

        # Forbid whitespace between )( of a function typedef
155
        if ($data =~ /\(\*\w+\)\s+\(/) {
156
            print "Whitespace between ')' and '(':\n";
157 158 159 160 161
            print "$file:$.: $line";
            $ret = 1;
        }

        # Forbid whitespace following ( or prior to )
162 163 164
        # but allow whitespace before ) on a single line
        # (optionally followed by a semicolon)
        if (($data =~ /\s\)/ && not $data =~ /^\s+\);?$/) ||
165
            $data =~ /\((?!$)\s/) {
166
            print "Whitespace after '(' or before ')':\n";
167 168 169
            print "$file:$.: $line";
            $ret = 1;
        }
170

E
Eric Blake 已提交
171
        # Forbid whitespace before ";" or ",". Things like below are allowed:
172 173 174 175 176 177 178 179 180
        #
        # 1) The expression is empty for "for" loop. E.g.
        #   for (i = 0; ; i++)
        #
        # 2) An empty statement. E.g.
        #   while (write(statuswrite, &status, 1) == -1 &&
        #          errno == EINTR)
        #       ;
        #
181 182 183
        if ($data =~ /\s[;,]/) {
            unless ($data =~ /\S; ; / ||
                    $data =~ /^\s+;/) {
J
Ján Tomko 已提交
184
                print "Whitespace before semicolon or comma:\n";
185 186 187
                print "$file:$.: $line";
                $ret = 1;
            }
188
        }
189 190 191

        # Require EOL, macro line continuation, or whitespace after ";".
        # Allow "for (;;)" as an exception.
192
        if ($data =~ /;[^	 \\\n;)]/) {
193
            print "Invalid character after semicolon:\n";
194 195 196
            print "$file:$.: $line";
            $ret = 1;
        }
E
Eric Blake 已提交
197 198

        # Require EOL, space, or enum/struct end after comma.
199
        if ($data =~ /,[^ \\\n)}]/) {
200
            print "Invalid character after comma:\n";
E
Eric Blake 已提交
201 202 203
            print "$file:$.: $line";
            $ret = 1;
        }
204

205
        # Require spaces around assignment '=', compounds and '=='
206 207
        if ($data =~ /[^ ]\b[!<>&|\-+*\/%\^=]?=/ ||
            $data =~ /=[^= \\\n]/) {
208
            print "Spacing around '=' or '==':\n";
209 210 211
            print "$file:$.: $line";
            $ret = 1;
        }
212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242

        # One line conditional statements with one line bodies should
        # not use curly brackets.
        if ($data =~ /^\s*(if|while|for)\b.*\{$/) {
            $cb_linenum = $.;
            $cb_code = $line;
            $cb_scolon = 0;
        }

        # We need to check for exactly one semicolon inside the body,
        # because empty statements (e.g. with comment only) are
        # allowed
        if ($cb_linenum == $. - 1 && $data =~ /^[^;]*;[^;]*$/) {
            $cb_code .= $line;
            $cb_scolon = 1;
        }

        if ($data =~ /^\s*}\s*$/ &&
            $cb_linenum == $. - 2 &&
            $cb_scolon) {

            print "Curly brackets around single-line body:\n";
            print "$file:$cb_linenum-$.:\n$cb_code$line";
            $ret = 1;

            # There _should_ be no need to reset the values; but to
            # keep my inner peace...
            $cb_linenum = 0;
            $cb_scolon = 0;
            $cb_code = "";
        }
243 244 245 246 247
    }
    close FILE;
}

exit $ret;