1#!/bin/sh
2
3# Checks some of the GNU style formatting rules in a set of patches.
4# Copyright (C) 2010, 2012  Free Software Foundation, Inc.
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
21usage() {
22    cat <<EOF
23check_GNU_style.sh [patch]...
24
25    Checks the patches for some of the GNU style formatting problems.
26    When FILE is -, read standard input.
27
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
34EOF
35    exit 1
36}
37
38test $# -eq 0 && usage
39
40inp=check_GNU_style.inp
41tmp=check_GNU_style.tmp
42
43# Remove $tmp on exit and various signals.
44trap "rm -f $inp $tmp" 0
45trap "rm -f $inp $tmp ; exit 1" 1 2 3 5 9 13 15
46
47grep -nH '^+' $* \
48	| grep -v ':+++' \
49	> $inp
50
51# Grep
52g (){
53    msg="$1"
54    arg="$2"
55    cat $inp \
56	| egrep --color=always -- "$arg" \
57	> $tmp && printf "\n$msg\n"
58    cat $tmp
59}
60
61# And Grep
62ag (){
63    msg="$1"
64    arg1="$2"
65    arg2="$3"
66    cat $inp \
67	| egrep --color=always -- "$arg1" \
68	| egrep --color=always -- "$arg2" \
69	> $tmp && printf "\n$msg\n"
70    cat $tmp
71}
72
73# reVerse Grep
74vg (){
75    msg="$1"
76    varg="$2"
77    arg="$3"
78    cat $inp \
79	| egrep -v -- "$varg" \
80	| egrep --color=always -- "$arg" \
81	> $tmp && printf "\n$msg\n"
82    cat $tmp
83}
84
85col (){
86    msg="$1"
87    cat $inp \
88	| awk -F':\\+' '{ if (length($2) > 80) print $0}' \
89	> $tmp
90    if [ -s $tmp ]; then
91	printf "\n$msg\n"
92	cat $tmp
93    fi
94}
95
96col 'Lines should not exceed 80 characters.'
97
98g 'Blocks of 8 spaces should be replaced with tabs.' \
99    ' {8}'
100
101g 'Trailing whitespace.' \
102    '[[:space:]]$'
103
104g 'Space before dot.' \
105    '[[:alnum:]][[:blank:]]+\.'
106
107g 'Dot, space, space, new sentence.' \
108    '[[:alnum:]]\.([[:blank:]]|[[:blank:]]{3,})[A-Z0-9]'
109
110g 'Dot, space, space, end of comment.' \
111    '[[:alnum:]]\.([[:blank:]]{0,1}|[[:blank:]]{3,})\*/'
112
113g 'Sentences should end with a dot.  Dot, space, space, end of the comment.' \
114    '[[:alnum:]][[:blank:]]*\*/'
115
116vg 'There should be exactly one space between function name and parentheses.' \
117    '\#define' '[[:alnum:]]([[:blank:]]{2,})?\('
118
119g 'There should be no space before closing parentheses.' \
120    '[[:graph:]][[:blank:]]+\)'
121
122ag 'Braces should be on a separate line.' \
123    '\{' 'if[[:blank:]]\(|while[[:blank:]]\(|switch[[:blank:]]\('
124
125