style.test 4.15 KB
Newer Older
1

2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#! /bin/sh

# Ensure consistent style by catching common improper constructs.

set -e

diag()
{
  fail=:
  echo "$file:" "$@"
  echo ============================================================
}

rm -f failures

# Get some help from GNU grep.
GREP_OPTIONS='--color=auto -n'
GREP_COLOR='1;31'
export GREP_OPTIONS
export GREP_COLOR

tmp=incltest.tmp

25
26
27
28
29
30
for dir in "${INCDIR-..}" "${INCDIR-..}"/../iface; do

  find "$dir" \( -name "${1-*}.hh" -o -name "${1-*}.cc" \) \
		 -a -type f -a -print |
  while read file; do
    if grep 'GNU Bison' "$file" >/dev/null ||
31
32
      grep 'generated by flex' "$file" >/dev/null ; then
      continue
33
    fi
34

35
    fail=false
36

37
38
39
40
    sed 's,[	 ]*//.*,,' < $file > $tmp

    grep '[	 ]$' $tmp &&
      diag 'Trailing space or tab.'
41

42
43
    grep '[	 ]if(' $tmp &&
      diag 'Missing space after "if"'
44

45
46
    grep '[	 ]if (.*).*{' $tmp &&
      diag 'Opening { should be on its own line.'
47

48
49
    grep '[	 ]if (.*).*;' $tmp &&
      diag 'if body should be on another line.'
50

51
52
53
    grep '[	 ]else.*;' $tmp &&
      diag 'else body should be on another line.'

54
55
    grep '[	 ]while(' $tmp &&
      diag 'Missing space after "while"'
56

57
58
    grep '[	 ]while (.*).*{' $tmp &&
      diag 'Opening { should be on its own line.'
59

60
61
    grep '[	 ]while (.*).*[^)];' $tmp &&
      diag 'while body should be on another line.'
62

63
64
    grep '[	 ]for(' $tmp &&
      diag 'Missing space after "for"'
65

66
67
    grep '[	 ]for (.*).*{' $tmp &&
      diag 'Opening { should be on its own line.'
68

69
70
    grep '[	 ]for (.*;.*;.*).*;' $tmp &&
      diag 'for body should be on another line.'
71

72
73
    grep '[	 ]switch(' $tmp &&
      diag 'Missing space after "switch"'
74

75
76
    grep '[	 ]switch (.*).*{' $tmp &&
      diag 'Opening { should be on its own line.'
77

78
79
80
81
82
83
    grep 'namespace .*{' $tmp &&
      diag 'Opening { should be on its own line.'

    grep 'class .*{' $tmp &&
      diag 'Opening { should be on its own line.'

84
85
    grep '( ' $tmp &&
      diag 'No space after opening (.'
86

87
88
    grep ' )' $tmp &&
      diag 'No space before closing ).'
89

90
91
    grep '! ' $tmp &&
      diag 'No space after unary operators (!).'
92

93
    grep ',[^ "%]' $tmp &&
94
      diag 'Space after coma.'
95

96
97
    grep '[^	 ]&&[^	 ]' $tmp &&
      diag 'Space arround binary operators.'
98

99
100
    grep '[^	 ]||[^	 ]' $tmp &&
      diag 'Space arround binary operators.'
101

102
103
    grep '[	 ]default:[^:].*;' $tmp &&
      diag 'Label should be on their own line.'
104

105
106
    grep '[	 ]case.*:[^:].*;' $tmp &&
      diag 'Label should be on their own line.'
107

108
109
    grep '[	 ];' $tmp &&
      diag 'No space before semicolon.'
110

111
112
113
    grep -v 'for (;;)' $tmp | grep ';[^	 ")]' &&
      diag 'Must have space or newline after semicolon.'

114
115
116
117
118
119
    grep '}.*}' $tmp &&
      diag 'No two } on the same line.'

    grep '{.*{' $tmp &&
      diag 'No two { on the same line.'

120
    grep 'delete[	 ]*[(][^(]*[)];' $tmp &&
121
122
      diag 'No useless parentheses after delete.'

123
    grep 'return[	 ]*[(][^(]*[)];' $tmp &&
124
      diag 'No useless parentheses after return.'
125

126
    grep 'NULL' $tmp &&
127
128
      diag 'Use 0 instead of NULL.  NULL is not portable.'

129
130
    # std::list::size() can be O(n).  Better use empty() whenever
    # possible, even for other containers.
131
    egrep '(->|[.])size\(\) [=!]= 0|![a-zA-Z0-9_]*(->|[.])size\(\)|(if |while |assert)\([a-zA-Z0-9_]*(->|[.])size\(\)\)' $tmp &&
132
133
      diag 'Prefer empty() to check emptiness.'

134
135
136
    egrep '^[^=]*([+][+]|--);' $tmp &&
      diag 'Take good habits: use ++i instead of i++ when you have the choice.'

137
138
139
    grep '[^a-zA-Z0-9_](\*[a-zA-Z0-9_]*)\.' $tmp &&
      diag 'Use "x->y", not "(*x).y"'

140
141
    case $file in
      *.hh | *.hxx)
142
        if egrep '(cout|cerr|clog)' $tmp >/dev/null; then
143
144
145
146
147
148
          :
	else
          grep '#.*include.*<iostream>' $tmp &&
            diag 'Avoid <iostream> in headers, better use <iosfwd>.'
        fi
        ;;
149
150
151
152
153
154
155
156
157
      *.cc)
        if grep 'namespace$' $tmp >/dev/null; then
	  :
	else
	  # We only check classes, but the rule should apply to functions too
	  grep '^[	 ]*class[	 ]' $tmp &&
	    diag 'Private definitions must be in anonymous namespace.'
	fi
	;;
158
159
160
    esac


161
162
    $fail && echo "$file" >>failures
  done
163
164
165
166
167
168
169
170
done

if test -f failures; then
   echo "The following files contain style errors:"
   cat failures
   rm failures
   exit 1;
fi