Refactor to use new protocol module and sipo.
[bertos.git] / test / codingstyle.pl
1 #!/usr/bin/perl -w
2 #
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.
7 #
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.
12 #
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
16 #
17 #  Based on checkpatch.pl from Linux
18 #
19 #  (c) 2010, Andrea Righi <arighi@develer.com>
20
21 use strict;
22
23 my $count = 0;
24 my $error = 0;
25 my $warn = 0;
26
27 while (my $line = <STDIN>) {
28         chomp($line);
29         $count++;
30
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);
36         }
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"
42                         . $line, $count);
43         }
44 # check for RCS/CVS revision markers
45         if ($line =~ /^\+.*\$(Revision|Log|Id)(?:\$|)/) {
46                 WARN("CVS style keyword markers should be removed\n"
47                         . $line, $count);
48         }
49 # check for braces in loops, conditions, etc.
50         if ($line =~ /.*(for|while|if|else|switch|struct|enum|union).*{/ &&
51                         $line !~ /#define/) {
52                 ERROR("if/while/etc brace must go on next line\n".
53                         $line, $count);
54         }
55 # check for malformed paths in #include statements
56         if ($line =~ m{^.\s*\#\s*include\s+[<"](.*)[">]}) {
57                 my $path = $1;
58                 if ($path =~ m{//}) {
59                         ERROR("malformed #include filename\n" .
60                                 $line, $count);
61                 }
62         }
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" .
66                         $line, $count);
67         }
68 # closing brace should have a space following it when it has anything
69 # on the line
70         if ($line =~ /}(?!(?:,|;|\)))\S/) {
71                 ERROR("space required after close brace '}'\n" .
72                         $line, $count);
73         }
74 # check spacing on square brackets
75         if ($line =~ /\[\s/ && $line !~ /\[\s*$/) {
76                 ERROR("space prohibited after open square bracket '['\n" .
77                         $line, $count);
78         }
79         if ($line =~ /\s\]/) {
80                 ERROR("space prohibited before close square bracket ']'\n"
81                         . $line, $count);
82         }
83 # check spacing on parentheses
84         if ($line =~ /\(\s/ && $line !~ /\(\s*(?:\\)?$/ &&
85                         $line !~ /for\s*\(\s+;/) {
86                 ERROR("space prohibited after open parenthesis '('\n" .
87                         $line, $count);
88         }
89         if ($line =~ /(\s+)\)/ && $line !~ /^.\s*\)/ &&
90                         $line !~ /for\s*\(.*;\s+\)/ &&
91                         $line !~ /:\s+\)/) {
92                 ERROR("space prohibited before close parenthesis ')'\n" .
93                         $line, $count);
94         }
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" .
98                         $line, $count);
99         }
100 # warn about #if 0
101         if ($line =~ /^.\s*\#\s*if\s+0\b/) {
102                 WARN("if this code is redundant consider removing it\n" .
103                         $line, $count);
104         }
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" .
108                         $line, $count);
109         }
110 # check for gcc specific __FUNCTION__
111         if ($line =~ /__FUNCTION__/) {
112                 ERROR("use __func__ instead of gcc specific __FUNCTION__\n" .
113                         $line, $count);
114         }
115 }
116
117 print "\ntotal: $error errors, $warn warnings, $count lines checked\n";
118
119 sub ERROR {
120         print STDERR "ERROR at line $_[1]: $_[0]\n";
121         $error++;
122 }
123
124 sub WARN {
125         print STDERR "WARNING at line $_[1]: $_[0]\n";
126         $warn++;
127 }