From 0f3f56695a87202b014f1a921c93c23a6d10ac62 Mon Sep 17 00:00:00 2001 From: Lubomir Rintel Date: Tue, 10 Jul 2018 10:40:25 +0200 Subject: [PATCH] contrib: add checkpatch.pl A naive code compliance checker. Invoke directly: contrib/scripts/checkpatch.pl 0001-switch-comments-to-klingon.patch contrib/scripts/checkpatch.pl hello.[ch] world.c Use from a commit hook: echo 'git format-patch --stdout -1 |contrib/scripts/checkpatch.pl || :>' \ >.git/hooks/post-commit Or view the documentation with "perldoc contrib/scripts/checkpatch.pl" --- contrib/scripts/checkpatch.pl | 224 ++++++++++++++++++++++++++++++++++ 1 file changed, 224 insertions(+) create mode 100755 contrib/scripts/checkpatch.pl diff --git a/contrib/scripts/checkpatch.pl b/contrib/scripts/checkpatch.pl new file mode 100755 index 000000000..f32fe2793 --- /dev/null +++ b/contrib/scripts/checkpatch.pl @@ -0,0 +1,224 @@ +#!/usr/bin/perl -n + +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2 of the License, or +# (at your option) any later version. +# +# This program 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 General Public License for more details. +# +# You should have received a copy of the GNU General Public License along +# with this program; if not, write to the Free Software Foundation, Inc., +# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. +# +# Copyright 2018 Red Hat, Inc. + +# $ perldoc checkpatch.pl for eye-pleasing view of the manual: + +=head1 NAME + +checkpatch.pl - emulate a serial modem + +=head1 SYNOPSIS + +checkpatch.pl [ ...] + +=head1 DESCRIPTION + +B checks source files or patches for common mistakes. + +=head1 OPTIONS + +=over 4 + +=item B<< >> + +A C source file or an unified diff. + +=back + +=cut + +use strict; +use warnings; + +chomp; + +our $is_patch; +our $is_file; + +our $seen_error; +our $line; # Current line +our $check_line; # Complain if errors are found on this line + +our @functions_seen; +our $type; +our $filename; + +sub new_hunk +{ + $type = undef; +} + +sub new_file +{ + $filename = shift; + @functions_seen = (); +} + +sub complain +{ + my $message = shift; + + return unless $check_line; + warn "$ARGV:$.: $message:\n"; + warn "> $line\n\n"; + $seen_error = 1; +} + +if ($is_patch) { + # This is a line of an unified diff + /^@@/ and new_hunk; + if (/^\+\+\+ (.*)/) { + new_file ($1); + next; + } + /^([ \+])(.*)/ or next; + $check_line = $1 eq '+'; + $line = $2; +} elsif ($is_file) { + # This is a line from full C file + $check_line = 1; + $line = $_; +} else { + # We don't handle these yet + /^diff --cc/ and exit 0; + # We don't know if we're dealing with a patch or a C file yet + /^#/ and $is_file = 1; + /^---/ and $is_patch = 1; + $filename = ''; + next; +} + +if ($is_file and $filename ne $ARGV) { + new_file ($ARGV); + new_hunk; +} + +next unless $filename =~ /\.[ch]$/; + +complain ('Tab following a space') if $line =~ / \t/; +complain ('Trailing whitespace') if $line =~ /[ \t]$/; + +# Further on we process stuff without comments. +$_ = $line; +s/\s*\/\*.*\*\///; +s/\s*\/\*.*//; +s/\s*\/\/.*//; +/^\s* \* / and next; +new_hunk if $_ eq ''; + +if (/^typedef*/) { + # We expect the { on the same line as the typedef. Otherwise it + # looks too much like a function declaration + complain ('Unexpected line break following a typedef') unless /[;{,]$/; + next; +} elsif (/^[A-Za-z_][A-Za-z0-9_ ]*\*?$/ and /[a-z]/) { + # A function type + $type = $_; + #print "TYPE: >>> $line <<<\n"; + next; +} elsif ($type and /^([A-Za-z_][A-Za-z0-9_]*)(\s*)\(/) { + my @order = qw/^get_property$ ^set_property$ (? + +Check a single file. + +=item B + +Check the currently staged changes. + +=item B + +A F<.git/hooks/post-commit> oneliner that, wisely, tolerates failures while +still providing advice. + +=back + +=head1 BUGS + +Proabably too many. + +=head1 SEE ALSO + +F + +=head1 COPYRIGHT + +Copyright 2018 Red Hat + +This program is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2 of the License, or +(at your option) any later version. + +=head1 AUTHOR + +Lubomir Rintel C + +=cut