]>
Commit | Line | Data |
---|---|---|
f3e3b476 SP |
1 | #!/bin/sh |
2 | ||
3 | # Checks some of the GNU style formatting rules in a set of patches. | |
90d04a44 | 4 | # Copyright (C) 2010, 2012 Free Software Foundation, Inc. |
f3e3b476 SP |
5 | # Contributed by Sebastian Pop <sebastian.pop@amd.com> |
6 | ||
7 | # This program is free software; you can redistribute it and/or modify | |
8 | # it under the terms of the GNU General Public License as published by | |
9 | # the Free Software Foundation; either version 3 of the License, or | |
10 | # (at your option) any later version. | |
11 | ||
12 | # This program is distributed in the hope that it will be useful, | |
13 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | # GNU General Public License for more details. | |
16 | ||
17 | # You should have received a copy of the GNU General Public License | |
18 | # along with this program; if not, write to the Free Software | |
19 | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | |
20 | ||
21 | usage() { | |
22 | cat <<EOF | |
23 | check_GNU_style.sh [patch]... | |
24 | ||
25 | Checks the patches for some of the GNU style formatting problems. | |
6e7bdfac YG |
26 | When FILE is -, read standard input. |
27 | ||
f3e3b476 SP |
28 | Please note that these checks are not always accurate, and |
29 | complete. The reference documentation of the GNU Coding Standards | |
30 | can be found here: http://www.gnu.org/prep/standards_toc.html | |
31 | and there are also some additional coding conventions for GCC: | |
32 | http://gcc.gnu.org/codingconventions.html | |
33 | ||
34 | EOF | |
35 | exit 1 | |
36 | } | |
37 | ||
38 | test $# -eq 0 && usage | |
4171ffe9 TV |
39 | nfiles=$# |
40 | files="$*" | |
f3e3b476 | 41 | |
7f167be6 | 42 | stdin=false |
a22e33d0 | 43 | stdin_tmp="" |
7f167be6 TV |
44 | if [ $nfiles -eq 1 ] && [ "$files" = "-" ]; then |
45 | stdin=true | |
a22e33d0 TV |
46 | |
47 | # By putting stdin into a temp file, we can handle it just like any other | |
48 | # file. F.i., we can cat it twice, which we can't do with stdin. | |
49 | stdin_tmp=check_GNU_style.stdin | |
50 | cat - > $stdin_tmp | |
51 | files=$stdin_tmp | |
7f167be6 TV |
52 | else |
53 | for f in $files; do | |
54 | if [ "$f" = "-" ]; then | |
55 | # Let's keep things simple. Either we read from stdin, or we read | |
56 | # from files specified on the command line, not both. | |
57 | usage | |
58 | fi | |
59 | if [ ! -f "$f" ]; then | |
60 | echo "error: could not read file: $f" | |
61 | exit 1 | |
62 | fi | |
63 | done | |
64 | fi | |
0648b807 | 65 | |
6e7bdfac | 66 | inp=check_GNU_style.inp |
f3e3b476 SP |
67 | tmp=check_GNU_style.tmp |
68 | ||
c8ae7abf | 69 | # Remove $tmp on exit and various signals. |
a22e33d0 TV |
70 | trap "rm -f $inp $tmp $stdin_tmp" 0 |
71 | trap "rm -f $inp $tmp $stdin_tmp; exit 1" 1 2 3 5 9 13 15 | |
6e7bdfac | 72 | |
4171ffe9 TV |
73 | if [ $nfiles -eq 1 ]; then |
74 | # There's no need for the file prefix if we're dealing only with one file. | |
75 | format="-n" | |
76 | else | |
77 | format="-nH" | |
78 | fi | |
79 | grep $format '^+' $files \ | |
80 | | grep -v ':+++' \ | |
81 | > $inp | |
c8ae7abf | 82 | |
f3e3b476 SP |
83 | # Grep |
84 | g (){ | |
847b6e15 TV |
85 | local msg="$1" |
86 | local arg="$2" | |
6e7bdfac | 87 | cat $inp \ |
f3e3b476 SP |
88 | | egrep --color=always -- "$arg" \ |
89 | > $tmp && printf "\n$msg\n" | |
90 | cat $tmp | |
91 | } | |
92 | ||
93 | # And Grep | |
94 | ag (){ | |
847b6e15 TV |
95 | local msg="$1" |
96 | local arg1="$2" | |
97 | local arg2="$3" | |
6e7bdfac | 98 | cat $inp \ |
f3e3b476 SP |
99 | | egrep --color=always -- "$arg1" \ |
100 | | egrep --color=always -- "$arg2" \ | |
101 | > $tmp && printf "\n$msg\n" | |
102 | cat $tmp | |
103 | } | |
104 | ||
105 | # reVerse Grep | |
106 | vg (){ | |
847b6e15 TV |
107 | local msg="$1" |
108 | local varg="$2" | |
109 | local arg="$3" | |
6e7bdfac | 110 | cat $inp \ |
f3e3b476 SP |
111 | | egrep -v -- "$varg" \ |
112 | | egrep --color=always -- "$arg" \ | |
113 | > $tmp && printf "\n$msg\n" | |
114 | cat $tmp | |
115 | } | |
116 | ||
117 | col (){ | |
847b6e15 | 118 | local msg="$1" |
cc1e0483 TV |
119 | local first=true |
120 | local f | |
121 | for f in $files; do | |
122 | local prefix="" | |
123 | if [ $nfiles -ne 1 ]; then | |
124 | prefix="$f:" | |
125 | fi | |
126 | ||
127 | # Don't reuse $inp, which may be generated using -H and thus contain a | |
128 | # file prefix. | |
129 | grep -n '^+' $f \ | |
130 | | grep -v ':+++' \ | |
131 | > $tmp | |
132 | ||
133 | cat $tmp | while IFS= read -r line; do | |
134 | local longline | |
135 | # Filter out the line number prefix and the patch line modifier '+' | |
136 | # to obtain the bare line, before we use expand. | |
137 | longline=$(echo "$line" \ | |
138 | | sed 's/^[0-9]*:+//' \ | |
139 | | expand \ | |
140 | | awk '{ if (length($0) > 80) print $0}') | |
141 | if [ "$longline" != "" ]; then | |
142 | if $first; then | |
143 | printf "\n$msg\n" | |
144 | first=false | |
145 | fi | |
146 | echo "$prefix$line" | |
147 | fi | |
148 | done | |
149 | done | |
f3e3b476 SP |
150 | } |
151 | ||
6e7bdfac YG |
152 | col 'Lines should not exceed 80 characters.' |
153 | ||
154 | g 'Blocks of 8 spaces should be replaced with tabs.' \ | |
155 | ' {8}' | |
f3e3b476 SP |
156 | |
157 | g 'Trailing whitespace.' \ | |
6e7bdfac | 158 | '[[:space:]]$' |
f3e3b476 SP |
159 | |
160 | g 'Space before dot.' \ | |
6e7bdfac | 161 | '[[:alnum:]][[:blank:]]+\.' |
f3e3b476 SP |
162 | |
163 | g 'Dot, space, space, new sentence.' \ | |
6e7bdfac | 164 | '[[:alnum:]]\.([[:blank:]]|[[:blank:]]{3,})[A-Z0-9]' |
f3e3b476 SP |
165 | |
166 | g 'Dot, space, space, end of comment.' \ | |
6e7bdfac | 167 | '[[:alnum:]]\.([[:blank:]]{0,1}|[[:blank:]]{3,})\*/' |
f3e3b476 SP |
168 | |
169 | g 'Sentences should end with a dot. Dot, space, space, end of the comment.' \ | |
6e7bdfac | 170 | '[[:alnum:]][[:blank:]]*\*/' |
f3e3b476 SP |
171 | |
172 | vg 'There should be exactly one space between function name and parentheses.' \ | |
7e425ad6 TV |
173 | '\#define' \ |
174 | '[[:alnum:]]([[:blank:]]{2,})?\(' | |
f3e3b476 SP |
175 | |
176 | g 'There should be no space before closing parentheses.' \ | |
6e7bdfac | 177 | '[[:graph:]][[:blank:]]+\)' |
f3e3b476 SP |
178 | |
179 | ag 'Braces should be on a separate line.' \ | |
7e425ad6 TV |
180 | '\{' \ |
181 | 'if[[:blank:]]\(|while[[:blank:]]\(|switch[[:blank:]]\(' |