3 # This program is free software; you can redistribute it and/or modify
4 # it under the terms of the GNU General Public License as published by
5 # the Free Software Foundation; either version 2 of the License, or
6 # (at your option) any later version.
8 # This program is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # GNU General Public License for more details.
13 # You should have received a copy of the GNU General Public License
14 # along with this program; if not, write to the Free Software
15 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 # Based on checkpatch.pl from Linux
19 # (c) 2010, Andrea Righi <arighi@develer.com>
27 while (my $line = <STDIN>) {
31 #trailing whitespace or DOS ^M
32 if ($line =~ /^.*\015/) {
33 ERROR("DOS line endings\n" . $line, $count);
34 } elsif ($line =~ /^.*\S\s+$/ || $line =~ /^\s+$/) {
35 WARN("trailing whitespace\n" . $line, $count);
37 # at the beginning of a line any tabs must come first and anything
38 # more than 8 must use tabs.
39 if ($line =~ /^\+\s* \t\s*\S/ ||
40 $line =~ /^\+\s* \s*/) {
41 ERROR("code indent should use tabs where possible\n"
44 # check for RCS/CVS revision markers
45 if ($line =~ /^\+.*\$(Revision|Log|Id)(?:\$|)/) {
46 WARN("CVS style keyword markers should be removed\n"
49 # check for braces in loops, conditions, etc.
50 if ($line =~ /.*(for|while|if|else|switch|struct|enum|union).*{/ &&
52 ERROR("if/while/etc brace must go on next line\n".
55 # check for malformed paths in #include statements
56 if ($line =~ m{^.\s*\#\s*include\s+[<"](.*)[">]}) {
59 ERROR("malformed #include filename\n" .
63 # check for static initialisers.
64 if ($line =~ /\bstatic\s.*=\s*(0|NULL|false)\s*;/) {
65 WARN("do not initialise statics to 0 or NULL\n" .
68 # closing brace should have a space following it when it has anything
70 if ($line =~ /}(?!(?:,|;|\)))\S/) {
71 ERROR("space required after close brace '}'\n" .
74 # check spacing on square brackets
75 if ($line =~ /\[\s/ && $line !~ /\[\s*$/) {
76 ERROR("space prohibited after open square bracket '['\n" .
79 if ($line =~ /\s\]/) {
80 ERROR("space prohibited before close square bracket ']'\n"
83 # check spacing on parentheses
84 if ($line =~ /\(\s/ && $line !~ /\(\s*(?:\\)?$/ &&
85 $line !~ /for\s*\(\s+;/) {
86 ERROR("space prohibited after open parenthesis '('\n" .
89 if ($line =~ /(\s+)\)/ && $line !~ /^.\s*\)/ &&
90 $line !~ /for\s*\(.*;\s+\)/ &&
92 ERROR("space prohibited before close parenthesis ')'\n" .
95 # Need a space before open parenthesis after if, while etc
96 if ($line =~ /\b(if|while|for|switch)\(/) {
97 ERROR("space required before the open parenthesis '('\n" .
101 if ($line =~ /^.\s*\#\s*if\s+0\b/) {
102 WARN("if this code is redundant consider removing it\n" .
105 # warn about spacing in #ifdefs
106 if ($line =~ /^.\s*\#\s*(ifdef|ifndef|elif)\s\s+/) {
107 ERROR("exactly one space required after that #$1\n" .
110 # check for gcc specific __FUNCTION__
111 if ($line =~ /__FUNCTION__/) {
112 ERROR("use __func__ instead of gcc specific __FUNCTION__\n" .
117 print "\ntotal: $error errors, $warn warnings, $count lines checked\n";
120 print STDERR "ERROR at line $_[1]: $_[0]\n";
125 print STDERR "WARNING at line $_[1]: $_[0]\n";