style.test 7.73 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#! /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.
17
18
19
20
21
22
if (grep --color=auto -n --version)>/dev/null 2>&1; then
  GREP_OPTIONS='--color=auto -n'
  GREP_COLOR='1;31'
  export GREP_OPTIONS
  export GREP_COLOR
fi
23

24
25
26
27
28
29
30
# Reset the locale, so for instance we don't get bugs because sed is
# expecting utf-8 and we feed him latin-1.  The C locale should be OK,
# because we do not treat extended characters specifically in the
# following style rules.
LC_ALL=C
export LC_ALL

31
32
tmp=incltest.tmp

33
34
for dir in "${INCDIR-..}" "${INCDIR-..}"/../iface; do

35
36
  find "$dir" \(    -name "${1-*}.hh" \
                 -o -name "${1-*}.hxx" \
37
38
                 -o -name "${1-*}.cc" \
                 -o -name "${1-*}.test" \) \
39
	      -a -type f -a -print |
40
  while read file; do
41

42
    if grep 'GNU Bison' "$file" >/dev/null ||
43
44
      grep 'generated by flex' "$file" >/dev/null ; then
      continue
45
    fi
46

47
48
49
50
51
    # Skip the files used by sanity.
    case $file in
	*incltest.cc) continue;;
    esac

52
    fail=false
53

54
55
56
57
    # Check this before stripping comments and strings.
    grep -i 'accepting cond' $file &&
      diag 'accepting -> acceptance'

58
59
60
61
62
    grep -i 'version 2 of the License' $file &&
      diag 'license text should refer to version 2'
    grep -i 'Temple Place' $file &&
      diag 'license text should give a url instead of an address'

63
64
65
    grep Copyright $file >/dev/null ||
      diag "missing copyright"

66
67
68
69
70
71
72
73
74
75
76
77
    # A doxygen comments such as
    #
    # | \brief foo
    # | \ingroup bar
    # |
    # | baz
    #
    # will be output as "foobaz."  But if the first two lines
    # are reversed, it's output correctly.
    perl -ne '/(.*\\brief.*\n.*\\ingroup.*)/ && print("$1\n") && exit(1)' \
      -0777 $file || diag "always put 'ingroup' before 'brief'"

78
    # Strip comments and strings.
79
80
81
82
83
84
85
86
87
88
    #
    # Multi-line comments of the form
    # /* Line 1
    #    Line 2
    #    Line 3 */
    # are replaced by
    # //
    # //
    # //
    # to keep the line numbers correct in the diagnostics.
89
90
    #
    # Also get read of the SPOT_API tags.
91
92
93
    perl -pe 'sub f {my $a = shift; $a =~ s:[^\n]*://:g; return "$a"}
              s,/\*(.*?)\*/,f($1),sge;
              s,//.*?\n,//\n,g;
94
95
              s,"[^"\n]*","",g;
              s,SPOT_API ,,g' -0777 <$file >$tmp
96
97

    grep '[	 ]$' $tmp &&
98
      diag 'Trailing whitespace.'
99

100
101
102
    case $file in
    *.test);;
    *)
103
104
105
      grep -E '[^<]class[ \t]+[A-Z]' $tmp &&
        diag 'Use lower case class names.'

106
107
      grep '[	 ]if(' $tmp &&
	diag 'Missing space after "if"'
108

109
110
      grep '[	 ]if (.*).*{' $tmp &&
	diag 'Opening { should be on its own line.'
111

112
113
      grep '[	 ]if (.*).*;' $tmp &&
	diag 'if body should be on another line.'
114

115
116
      grep '[	 ]else.*;' $tmp &&
	diag 'else body should be on another line.'
117

118
119
      grep '[	 ]while(' $tmp &&
	diag 'Missing space after "while"'
120

121
122
      grep '[	 ]while (.*).*{' $tmp &&
	diag 'Opening { should be on its own line.'
123

124
125
      grep '[	 ]while (.*).*[^)];' $tmp &&
	diag 'while body should be on another line.'
126

127
128
      grep '[	 ]for(' $tmp &&
	diag 'Missing space after "for"'
129

130
131
      grep '[	 ]for (.*).*{' $tmp &&
	diag 'Opening { should be on its own line.'
132

133
134
      grep '[	 ]for (.*;.*;.*).*;' $tmp &&
	diag 'for body should be on another line.'
135

136
137
      grep '[	 ]switch(' $tmp &&
	diag 'Missing space after "switch"'
138

139
140
      grep '[	 ]switch (.*).*{' $tmp &&
	diag 'Opening { should be on its own line.'
141

142
143
      grep 'namespace .*{' $tmp &&
	diag 'Opening { should be on its own line.'
144

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

148
149
      grep '( ' $tmp &&
	diag 'No space after opening (.'
150

151
152
      grep ' )' $tmp &&
	diag 'No space before closing ).'
153

154
155
      grep '! ' $tmp &&
	diag 'No space after unary operators (!).'
156

157
      grep ",[^ \"	%'\\\\]" $tmp &&
158
	diag 'Space after coma.'
159

Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
160
161
      # The 'r' allows operator&&
      grep '[^	 r]&&[^	 ]' $tmp &&
Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
162
	diag 'Space around binary operators.'
163

Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
164
165
      # The 'r' allows operator||
      grep '[^	 r]||[^	 ]' $tmp &&
Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
166
	diag 'Space around binary operators.'
Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
167
168

      # The 'r' allows operator==
Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
169
170
171
172
173
174
175
176
177
178
179
180
      grep '[^	 r<>][!<>=]=[^	 ]' $tmp &&
	diag 'Space around binary operators.'

      # The 'r' allows operator<<=
      grep '[^	 r][<>][<>]=[^	 ]' $tmp &&
	diag 'Space around binary operators.'

      grep 'operator[^a-zA-Z0-9_(]*[	 ][^a-zA-Z0-9_(]*(' $tmp &&
        diag 'Write operatorXX(...) without spaces around XX.'

      grep 'operator[^(]* (' $tmp &&
        diag 'No space before ('
181

182
183
      grep '[	 ]default:[^:].*;' $tmp &&
	diag 'Label should be on their own line.'
184

185
186
      grep '[	 ]case.*:[^:].*;' $tmp &&
	diag 'Label should be on their own line.'
187

188
189
      grep '[	 ];' $tmp &&
	diag 'No space before semicolon.'
190

191
192
      grep -v 'for (.*;;)' $tmp | grep ';[^	 ")]' &&
	diag 'Must have space or newline after semicolon.'
193

194
195
      grep '}.*}' $tmp &&
	diag 'No two } on the same line.'
196

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

200
201
      grep 'delete[	 ]*[(][^(]*[)];' $tmp &&
	diag 'No useless parentheses after delete.'
202

203
204
      grep 'return[	 ]*[(][^(]*[)];' $tmp &&
	diag 'No useless parentheses after return.'
205

206
207
      grep 'NULL' $tmp &&
	diag 'Use 0 instead of NULL.  NULL is not portable.'
208

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

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

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

220
221
      grep 'std::hash' $tmp &&
	diag 'use Sgi:: for hash and hash_map'
222

223
224
      grep 'Sgi::[^h]' $tmp &&
	diag 'Sgi:: is for hash and hash_map only'
225

226
227
228
      egrep 'bdd_(false|true)[	]*\(' $tmp &&
	diag 'Use bddfalse and bddtrue instead of bdd_false() and bdd_true()'

229
230
231
232
233
234
235
      res=`perl -ne '$/ = undef;
	print "$&\n"
	while /if \((.*)(\s*==\s*0)?\)\s*delete(\[\])?\s+\1;(?!\s+else)/g' $tmp`
      if test -n "$res"; then
	 echo "$res"
	 diag 'No "if (x)" required before "delete x;".'
      fi
236

237
238
      case $file in
	*.hh | *.hxx)
239
	  if egrep '(<<|>>)' $tmp >/dev/null; then
240
241
242
243
244
	    :
	  else
	    grep '#.*include.*<iostream>' $tmp &&
	      diag 'Avoid <iostream> in headers, better use <iosfwd>.'
	  fi
245
246
247
248
249
250
	  # Headers from src/priv/ are not installed, so may only be
	  # included from *.cc files or from other src/priv/ headers
	  # (in the latter case they do not have to specify the priv/
	  # directory, so they will not match this regex).
	  grep '#.*include.*priv/' $tmp &&
	      diag 'Do not include private headers in public headers.'
251
252
253
254
255
256
257
258
259
260
261
262
	  ;;
	*.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
	  ;;
      esac
      ;;
263
264
    esac

265

266
    $fail && echo "$file" >>failures
267
268

  done || : # Make sure sh does not abort when read exits with false.
269
270
done

Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291

# Rules for Makefiles.
for dir in "${INCDIR-..}" "${INCDIR-..}"/../iface; do

  find "$dir" -name "Makefile.am" -a -type f -a -print |
  while read file; do
    fail=false

    # Strip comments.
    sed 's,#.*,,' < $file > $tmp

    grep '[	 ]$' $tmp &&
      diag 'Trailing whitespace.'

    grep '\.libs/' $tmp &&
	diag "Don't reference files in .libs/, use Libtool instead."

    $fail && echo "$file" >>failures
  done || : # Make sure sh does not abort when read exits with false.
done

292
293
294
295
296
297
if test -f failures; then
   echo "The following files contain style errors:"
   cat failures
   rm failures
   exit 1;
fi
298
299

exit 0