2012-10-17 09:25:35 +00:00
|
|
|
#!/usr/bin/perl
|
|
|
|
#
|
|
|
|
# bracket-spacing.pl: Report any usage of 'function (..args..)'
|
2013-05-24 16:43:45 +00:00
|
|
|
# Also check for other syntax issues, such as correct use of ';'
|
2012-10-17 09:25:35 +00:00
|
|
|
#
|
|
|
|
# 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;
|
|
|
|
|
|
|
|
my $ret = 0;
|
|
|
|
my $incomment = 0;
|
|
|
|
|
|
|
|
foreach my $file (@ARGV) {
|
|
|
|
open FILE, $file;
|
|
|
|
|
|
|
|
while (defined (my $line = <FILE>)) {
|
|
|
|
my $data = $line;
|
2014-11-13 14:04:52 +00:00
|
|
|
# For temporary modifications
|
|
|
|
my $tmpdata;
|
2012-10-17 09:25:35 +00:00
|
|
|
|
2014-08-11 23:04:19 +00:00
|
|
|
# Kill any quoted , ; = or "
|
|
|
|
$data =~ s/'[";,=]'/'X'/g;
|
2013-05-24 16:43:45 +00:00
|
|
|
|
2013-05-24 08:50:42 +00:00
|
|
|
# Kill any quoted strings
|
|
|
|
$data =~ s,"([^\\\"]|\\.)*","XXX",g;
|
2012-10-17 09:25:35 +00:00
|
|
|
|
|
|
|
# Kill any C++ style comments
|
|
|
|
$data =~ s,//.*$,//,;
|
|
|
|
|
|
|
|
next if $data =~ /^#/;
|
|
|
|
|
|
|
|
# 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);
|
|
|
|
#
|
2014-11-13 14:04:52 +00:00
|
|
|
# We also don't want to spoil the $data so it can be used
|
|
|
|
# later on.
|
|
|
|
$tmpdata = $data;
|
|
|
|
while ($tmpdata =~ /(\w+)\s\((?!\*)/) {
|
2012-10-17 09:25:35 +00:00
|
|
|
my $kw = $1;
|
|
|
|
|
|
|
|
# Allow space after keywords only
|
|
|
|
if ($kw =~ /^(if|for|while|switch|return)$/) {
|
2014-11-13 14:04:52 +00:00
|
|
|
$tmpdata =~ s/($kw\s\()/XXX(/;
|
2012-10-17 09:25:35 +00:00
|
|
|
} else {
|
|
|
|
print "$file:$.: $line";
|
|
|
|
$ret = 1;
|
|
|
|
last;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
# Require whitespace immediately after keywords,
|
|
|
|
# but none after the opening bracket
|
2013-07-18 09:54:21 +00:00
|
|
|
while ($data =~ /\b(if|for|while|switch|return)\(/ ||
|
|
|
|
$data =~ /\b(if|for|while|switch|return)\s+\(\s/) {
|
2012-10-17 09:25:35 +00:00
|
|
|
print "$file:$.: $line";
|
|
|
|
$ret = 1;
|
|
|
|
last;
|
|
|
|
}
|
|
|
|
|
|
|
|
# Forbid whitespace between )( of a function typedef
|
|
|
|
while ($data =~ /\(\*\w+\)\s+\(/) {
|
|
|
|
print "$file:$.: $line";
|
|
|
|
$ret = 1;
|
|
|
|
last;
|
|
|
|
}
|
|
|
|
|
|
|
|
# Forbid whitespace following ( or prior to )
|
|
|
|
while ($data =~ /\S\s+\)/ ||
|
|
|
|
$data =~ /\(\s+\S/) {
|
|
|
|
print "$file:$.: $line";
|
|
|
|
$ret = 1;
|
|
|
|
last;
|
|
|
|
}
|
2013-05-21 10:01:01 +00:00
|
|
|
|
2013-11-19 21:29:44 +00:00
|
|
|
# Forbid whitespace before ";" or ",". Things like below are allowed:
|
2013-05-21 10:01:01 +00:00
|
|
|
#
|
|
|
|
# 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)
|
|
|
|
# ;
|
|
|
|
#
|
2013-11-19 21:29:44 +00:00
|
|
|
while ($data =~ /[^;\s]\s+[;,]/) {
|
2013-05-24 08:50:42 +00:00
|
|
|
print "$file:$.: $line";
|
|
|
|
$ret = 1;
|
2013-05-21 10:01:01 +00:00
|
|
|
last;
|
|
|
|
}
|
2013-05-24 16:43:45 +00:00
|
|
|
|
|
|
|
# Require EOL, macro line continuation, or whitespace after ";".
|
|
|
|
# Allow "for (;;)" as an exception.
|
|
|
|
while ($data =~ /;[^ \\\n;)]/) {
|
|
|
|
print "$file:$.: $line";
|
|
|
|
$ret = 1;
|
|
|
|
last;
|
|
|
|
}
|
2013-11-19 21:29:44 +00:00
|
|
|
|
|
|
|
# Require EOL, space, or enum/struct end after comma.
|
|
|
|
while ($data =~ /,[^ \\\n)}]/) {
|
|
|
|
print "$file:$.: $line";
|
|
|
|
$ret = 1;
|
|
|
|
last;
|
|
|
|
}
|
2014-01-20 11:27:28 +00:00
|
|
|
|
2014-03-17 09:38:38 +00:00
|
|
|
# Require spaces around assignment '=', compounds and '=='
|
|
|
|
# with the exception of virAssertCmpInt()
|
2014-11-13 14:04:52 +00:00
|
|
|
$tmpdata = $data;
|
|
|
|
$tmpdata =~ s/(virAssertCmpInt\(.* ).?=,/$1op,/;
|
|
|
|
while ($tmpdata =~ /[^ ]\b[!<>&|\-+*\/%\^=]?=[^=]/ ||
|
|
|
|
$tmpdata =~ /=[^= \\\n]/) {
|
2014-01-20 11:27:28 +00:00
|
|
|
print "$file:$.: $line";
|
|
|
|
$ret = 1;
|
|
|
|
last;
|
|
|
|
}
|
2012-10-17 09:25:35 +00:00
|
|
|
}
|
|
|
|
close FILE;
|
|
|
|
}
|
|
|
|
|
|
|
|
exit $ret;
|