[libvirt] [PATCH 2/2] Add syntax-check rule to look for 'function (...args...)' violations

Daniel P. Berrange berrange at redhat.com
Wed Oct 17 09:31:56 UTC 2012


From: "Daniel P. Berrange" <berrange at redhat.com>

Checking for 'function (...args...)' is quite difficult since it
needs to ignore valid usage with keywords like 'if (...test...)'
and while/for/switch. It must also ignore source comments and
quoted strings.

It is not possible todo this with a simple regex in the normal
syntax-check style. So a short Perl script is created instead
to analyse the source. In practice this works well enough. The
only thing it can't cope with is multi-line quoted strings of
the form

 "start of string\
more lines\
more line\
the end"

but this can and should be written as

 "start of string"
 "more lines"
 "more line"
 "the end"

with this simple change, the bracket checking script does not
have any false positives across libvirt source, provided it
is only run against .c files.
---
 build-aux/bracket-spacing.pl | 75 ++++++++++++++++++++++++++++++++++++++++++++
 cfg.mk                       |  7 ++++-
 2 files changed, 81 insertions(+), 1 deletion(-)
 create mode 100755 build-aux/bracket-spacing.pl

diff --git a/build-aux/bracket-spacing.pl b/build-aux/bracket-spacing.pl
new file mode 100755
index 0000000..65f500e
--- /dev/null
+++ b/build-aux/bracket-spacing.pl
@@ -0,0 +1,75 @@
+#!/usr/bin/perl
+#
+# bracket-spacing.pl: Report any usage of 'function (..args..)'
+#
+# 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 at 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;
+
+        # Kill any quoted strongs
+        $data =~ s,".*?","XXX",g;
+
+        # Kill any C++ style comments
+        $data =~ s,//.*$,,;
+
+        next if $data =~ /^#/;
+
+        # Kill C style multi-line comments
+        if ($incomment) {
+            if ($data =~ m,\*/,) {
+                $incomment = 0;
+                $data =~ s,^.*\*/,,;
+            } else {
+                $data = "";
+            }
+        }
+
+        if ($data =~ m,/\*.*\*/,) {
+            $data =~ s,/\*.*\*/,,;
+        } elsif ($data =~ m,/\*,) {
+            $incomment = 1;
+            $data =~ s,/\*.*,,;
+        }
+
+        while ($data =~ /(\w+)\s\((?!\*)/) {
+
+            my $kw = $1;
+            # Allow space after keywords only
+            if ($kw =~ /^(if|for|while|switch|return)$/) {
+                $data =~ s/($kw\s\()/XXX(/;
+            } else {
+                print "$file:$.: $line";
+                $ret = 1;
+                last;
+            }
+        }
+    }
+    close FILE;
+}
+
+exit $ret;
diff --git a/cfg.mk b/cfg.mk
index e1fbf4f..10d1d13 100644
--- a/cfg.mk
+++ b/cfg.mk
@@ -726,7 +726,12 @@ sc_check_author_list:
 	test $$fail = 0
 
 # regenerate HACKING as part of the syntax-check
-syntax-check: $(top_srcdir)/HACKING
+syntax-check: $(top_srcdir)/HACKING bracket-spacing-check
+
+bracket-spacing-check:
+	$(AM_V_GEN)files=`$(VC_LIST) | grep '\.c$$'`; \
+	$(PERL) $(top_srcdir)/build-aux/bracket-spacing.pl $$files || \
+          (echo $(ME): no whitespace allowed between function name and '(' && exit 1)
 
 # sc_po_check can fail if generated files are not built first
 sc_po_check: \
-- 
1.7.11.2




More information about the libvir-list mailing list