build-aux:check-spacing: Add wrapper function of CheckWhiteSpaces

This patch adds CheckWhiteSpaces to simplify check-spacing.

Signed-off-by: Shi Lei <shi_lei@massclouds.com>
This commit is contained in:
Shi Lei 2018-09-19 16:38:17 +08:00 committed by Michal Privoznik
parent c3875129d9
commit 6225626b6f

View File

@ -96,38 +96,16 @@ sub KillComments {
return; return;
} }
my $ret = 0; #
# CheckWhiteSpaces:
foreach my $file (@ARGV) { # $_[0]: $data(in)
# Per-file variables for multiline Curly Bracket (cb_) check # $_[1]: $location(in), which format is file-path:line-num:line-code
my $cb_linenum = 0; #
my $cb_code = ""; # Check whitespaces according to code spec of libvirt.
my $cb_scolon = 0; #
my $fn_linenum = 0; sub CheckWhiteSpaces {
my $incomment = 0; my $ret = 0;
my ($data, $location) = @_;
open FILE, $file;
while (defined (my $line = <FILE>)) {
my $data = $line;
my $location = "$file:$.:\n$line";
# For temporary modifications
my $tmpdata;
# Kill any quoted , ; = or "
$data =~ s/'[";,=]'/'X'/g;
# Kill any quoted strings
$data =~ s,"(?:[^\\\"]|\\.)*","XXX",g;
# Kill any C++ style comments
$data =~ s,//.*$,//,;
next if $data =~ /^#/;
$ret = 1 if CheckFunctionBody(\$data, \$location, \$fn_linenum);
KillComments(\$data, \$incomment);
# We need to match things like # We need to match things like
# #
@ -145,7 +123,9 @@ foreach my $file (@ARGV) {
# #
# We also don't want to spoil the $data so it can be used # We also don't want to spoil the $data so it can be used
# later on. # later on.
$tmpdata = $data;
# For temporary modifications
my $tmpdata = $$data;
while ($tmpdata =~ /(\w+)\s\((?!\*)/) { while ($tmpdata =~ /(\w+)\s\((?!\*)/) {
my $kw = $1; my $kw = $1;
@ -153,34 +133,30 @@ foreach my $file (@ARGV) {
if ($kw =~ /^(?:if|for|while|switch|return)$/) { if ($kw =~ /^(?:if|for|while|switch|return)$/) {
$tmpdata =~ s/(?:$kw\s\()/XXX(/; $tmpdata =~ s/(?:$kw\s\()/XXX(/;
} else { } else {
print "Whitespace after non-keyword:\n"; print "Whitespace after non-keyword:\n$$location";
print "$file:$.: $line";
$ret = 1; $ret = 1;
last; last;
} }
} }
# Require whitespace immediately after keywords # Require whitespace immediately after keywords
if ($data =~ /\b(?:if|for|while|switch|return)\(/) { if ($$data =~ /\b(?:if|for|while|switch|return)\(/) {
print "No whitespace after keyword:\n"; print "No whitespace after keyword:\n$$location";
print "$file:$.: $line";
$ret = 1; $ret = 1;
} }
# Forbid whitespace between )( of a function typedef # Forbid whitespace between )( of a function typedef
if ($data =~ /\(\*\w+\)\s+\(/) { if ($$data =~ /\(\*\w+\)\s+\(/) {
print "Whitespace between ')' and '(':\n"; print "Whitespace between ')' and '(':\n$$location";
print "$file:$.: $line";
$ret = 1; $ret = 1;
} }
# Forbid whitespace following ( or prior to ) # Forbid whitespace following ( or prior to )
# but allow whitespace before ) on a single line # but allow whitespace before ) on a single line
# (optionally followed by a semicolon) # (optionally followed by a semicolon)
if (($data =~ /\s\)/ && not $data =~ /^\s+\);?$/) || if (($$data =~ /\s\)/ && not $$data =~ /^\s+\);?$/) ||
$data =~ /\((?!$)\s/) { $$data =~ /\((?!$)\s/) {
print "Whitespace after '(' or before ')':\n"; print "Whitespace after '(' or before ')':\n$$location";
print "$file:$.: $line";
$ret = 1; $ret = 1;
} }
@ -194,38 +170,70 @@ foreach my $file (@ARGV) {
# errno == EINTR) # errno == EINTR)
# ; # ;
# #
if ($data =~ /\s[;,]/) { if ($$data =~ /\s[;,]/) {
unless ($data =~ /\S; ; / || unless ($$data =~ /\S; ; / ||
$data =~ /^\s+;/) { $$data =~ /^\s+;/) {
print "Whitespace before semicolon or comma:\n"; print "Whitespace before semicolon or comma:\n$$location";
print "$file:$.: $line";
$ret = 1; $ret = 1;
} }
} }
# Require EOL, macro line continuation, or whitespace after ";". # Require EOL, macro line continuation, or whitespace after ";".
# Allow "for (;;)" as an exception. # Allow "for (;;)" as an exception.
if ($data =~ /;[^ \\\n;)]/) { if ($$data =~ /;[^ \\\n;)]/) {
print "Invalid character after semicolon:\n"; print "Invalid character after semicolon:\n$$location";
print "$file:$.: $line";
$ret = 1; $ret = 1;
} }
# Require EOL, space, or enum/struct end after comma. # Require EOL, space, or enum/struct end after comma.
if ($data =~ /,[^ \\\n)}]/) { if ($$data =~ /,[^ \\\n)}]/) {
print "Invalid character after comma:\n"; print "Invalid character after comma:\n$$location";
print "$file:$.: $line";
$ret = 1; $ret = 1;
} }
# Require spaces around assignment '=', compounds and '==' # Require spaces around assignment '=', compounds and '=='
if ($data =~ /[^ ]\b[!<>&|\-+*\/%\^=]?=/ || if ($$data =~ /[^ ]\b[!<>&|\-+*\/%\^=]?=/ ||
$data =~ /=[^= \\\n]/) { $$data =~ /=[^= \\\n]/) {
print "Spacing around '=' or '==':\n"; print "Spacing around '=' or '==':\n$$location";
print "$file:$.: $line";
$ret = 1; $ret = 1;
} }
return $ret;
}
my $ret = 0;
foreach my $file (@ARGV) {
# Per-file variables for multiline Curly Bracket (cb_) check
my $cb_linenum = 0;
my $cb_code = "";
my $cb_scolon = 0;
my $fn_linenum = 0;
my $incomment = 0;
open FILE, $file;
while (defined (my $line = <FILE>)) {
my $data = $line;
my $location = "$file:$.:\n$line";
# Kill any quoted , ; = or "
$data =~ s/'[";,=]'/'X'/g;
# Kill any quoted strings
$data =~ s,"(?:[^\\\"]|\\.)*","XXX",g;
# Kill any C++ style comments
$data =~ s,//.*$,//,;
next if $data =~ /^#/;
$ret = 1 if CheckFunctionBody(\$data, \$location, \$fn_linenum);
KillComments(\$data, \$incomment);
$ret = 1 if CheckWhiteSpaces(\$data, \$location);
# One line conditional statements with one line bodies should # One line conditional statements with one line bodies should
# not use curly brackets. # not use curly brackets.
if ($data =~ /^\s*(if|while|for)\b.*\{$/) { if ($data =~ /^\s*(if|while|for)\b.*\{$/) {