style.test 10.3 KB
Newer Older
1
#! /bin/sh
2
# -*- coding: utf-8 -*-
3
4
# Copyright (C) 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016
# Laboratoire de Recherche et Développement de l'Epita (LRDE).
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# Copyright (C) 2004, 2005 Laboratoire d'Informatique de Paris 6
# (LIP6), département Systèmes Répartis Coopératifs (SRC), Université
# Pierre et Marie Curie.
#
# This file is part of Spot, a model checking library.
#
# Spot is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# Spot is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
# or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public
# License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
23
24
25
26
27
28
29
30
31
32
33
34

# Ensure consistent style by catching common improper constructs.

set -e

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

35
rm -f failures.style
36

37
38
GREP=grep

39
# Get some help from GNU grep.
40
if (grep --color=auto -n --version)>/dev/null 2>&1; then
41
  GREP="$GREP --color=auto -n"
42
43
44
  GREP_COLOR='1;31'
  export GREP_COLOR
fi
45

46
# Reset the locale, so for instance we don't get bugs because sed is
47
# expecting utf-8 and we feed it latin-1.  The C locale should be OK,
48
49
50
51
52
# because we do not treat extended characters specifically in the
# following style rules.
LC_ALL=C
export LC_ALL

53
54
mkdir -p style.dir
tmp=style.dir/incltest.tmp.$$
55

56
57
58
# We used to loop over more directories before the source tree was
# rearranged.  So there is only one left today, but we keep the loop
# in case we want to add more in the future.
59
60
TOP=${srcdir-.}/../..
for dir in "$TOP/spot" "$TOP/bin" "$TOP/tests"; do
61

62
63
  find "$dir" \(    -name "${1-*}.hh" \
                 -o -name "${1-*}.hxx" \
64
65
                 -o -name "${1-*}.cc" \
                 -o -name "${1-*}.test" \) \
66
	      -a -type f -a -print |
67
  while read file; do
68

69
70
    if $GREP 'GNU Bison' "$file" >/dev/null ||
      $GREP 'generated by flex' "$file" >/dev/null ; then
71
      continue
72
    fi
73

74
75
76
77
78
    # Skip the files used by sanity.
    case $file in
	*incltest.cc) continue;;
    esac

79
    fail=false
80

81
    # Check this before stripping comments and strings.
82
    $GREP -i 'accepting cond' $file &&
83
84
      diag 'accepting -> acceptance'

85
    $GREP -i 'version 2 of the License' $file &&
86
      diag 'license text should refer to version 2'
87
    $GREP -i 'Temple Place' $file &&
88
89
      diag 'license text should give a url instead of an address'

90
91
92
    $GREP -q 'coding: utf-8' $file ||
      diag 'missing -*- coding: utf-8 -*-'

93
    $GREP Copyright $file >/dev/null ||
94
95
      diag "missing copyright"

96
    # If some grep implementation ignores LC_ALL=C, this rule might be
97
98
99
    # a problem on utf-8 characters such as "δ" which really
    # corresponds to multiple bytes, but might be matched as a single
    # character by grep.
100
    $GREP '<< *"\([^\\]\|\\.\)"' $file &&
101
102
      diag "Use << 'c' instead" 'of << "c".'

103
104
105
106
107
108
109
110
111
112
113
114
    # 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'"

115
116
117
118
119
120
121
    # Check this before we remove SPOT_API from the input.
    case $file in
	*.cc)
	    $GREP 'SPOT_API' $file &&
		diag 'use SPOT_API only in header files';;
    esac

122
    # Strip comments and strings.
123
124
125
126
127
128
129
130
131
132
    #
    # Multi-line comments of the form
    # /* Line 1
    #    Line 2
    #    Line 3 */
    # are replaced by
    # //
    # //
    # //
    # to keep the line numbers correct in the diagnostics.
133
    #
134
    # Also get rid of the SPOT_API tags.
135
136
137
    perl -pe 'sub f {my $a = shift; $a =~ s:[^\n]*://:g; return "$a"}
              s,/\*(.*?)\*/,f($1),sge;
              s,//.*?\n,//\n,g;
138
              s,(?<!include )"(\\.|[^"\\\n])*","",g;
139
              s,SPOT_API ,,g' -0777 <$file >$tmp
140

141
    $GREP '[	 ]$' $tmp &&
142
      diag 'Trailing whitespace.'
143

144
145
146
    case $file in
    *.test);;
    *)
147
      $GREP -E '[^<]class[ \t]+[A-Z]' $tmp &&
148
149
        diag 'Use lower case class names.'

150
      $GREP '[	 ]if(' $tmp &&
151
	diag 'Missing space after "if"'
152

153
      $GREP '[	 ]if (.*).*{' $tmp &&
154
	diag 'Opening { should be on its own line.'
155

156
      $GREP '[	 ]if (.*).*;' $tmp &&
157
	diag 'if body should be on another line.'
158

159
      $GREP '[	 ]else.*;' $tmp &&
160
	diag 'else body should be on another line.'
161

162
      $GREP '[	 ]while(' $tmp &&
163
	diag 'Missing space after "while"'
164

165
      $GREP '[	 ]while (.*).*{' $tmp &&
166
	diag 'Opening { should be on its own line.'
167

168
      $GREP '[	 ]while (.*).*[^)];' $tmp &&
169
	diag 'while body should be on another line.'
170

171
      $GREP '[	 ]for(' $tmp &&
172
	diag 'Missing space after "for"'
173

174
      $GREP '[	 ]for (.*).*{' $tmp &&
175
	diag 'Opening { should be on its own line.'
176

177
      $GREP '[	 ]for (.*;.*;.*).*;' $tmp &&
178
	diag 'for body should be on another line.'
179

180
      $GREP '[	 ]switch(' $tmp &&
181
	diag 'Missing space after "switch"'
182

183
      $GREP '[	 ]switch (.*).*{' $tmp &&
184
	diag 'Opening { should be on its own line.'
185

186
      $GREP 'namespace .*{' $tmp &&
187
	diag 'Opening { should be on its own line.'
188

189
      $GREP 'class .*{' $tmp | $GREP -v enum &&
190
	diag 'Opening { should be on its own line.'
191

192
      $GREP '( ' $tmp &&
193
	diag 'No space after opening (.'
194

195
      $GREP ' )' $tmp &&
196
	diag 'No space before closing ).'
197

198
      $GREP '! ' $tmp &&
199
	diag 'No space after unary operators (!).'
200

201
      $GREP ",[^ \"	%'\\\\]" $tmp &&
202
	diag 'Space after coma.'
203

Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
204
      # The 'r' allows operator&&
205
      # The '.' allows &&...
206
      $GREP '[^	 r]&&[^	 .]' $tmp &&
Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
207
	diag 'Space around binary operators.'
208

Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
209
      # The 'r' allows operator||
210
      $GREP '[^	 r]||[^	 ]' $tmp &&
Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
211
	diag 'Space around binary operators.'
Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
212
213

      # The 'r' allows operator==
214
      $GREP '[^	 r<>][!<>=]=[^	 ]' $tmp &&
Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
215
216
217
	diag 'Space around binary operators.'

      # The 'r' allows operator<<=
218
      $GREP '[^	 r][<>][<>]=[^	 ]' $tmp &&
Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
219
220
	diag 'Space around binary operators.'

221
      $GREP 'operator[^a-zA-Z0-9_(]*[	 ][^a-zA-Z0-9_(]*(' $tmp &&
Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
222
223
        diag 'Write operatorXX(...) without spaces around XX.'

224
      $GREP 'operator[^(]* (' $tmp &&
Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
225
        diag 'No space before ('
226

227
      $GREP '[	 ]default:[^:].*;' $tmp &&
228
	diag 'Label should be on their own line.'
229

230
      $GREP '[	 ]case.*:[^:].*;' $tmp &&
231
	diag 'Label should be on their own line.'
232

233
      $GREP '[	 ];' $tmp &&
234
	diag 'No space before semicolon.'
235

236
      $GREP -v 'for (.*;;)' $tmp | $GREP ';[^	 ")'"']" &&
237
	diag 'Must have space or newline after semicolon.'
238

239
240
241
242
243
      # Allow several { or } on the same line only if they are mixed
      # with parentheses, as this often occur with lambdas or
      # initializer lists.  What we want to forbid is cases where
      # multiple scopes are opened/closed on the same line.
      $GREP '^[^()]*}[^()]*}[^()]*$' $tmp &&
244
	diag 'No two } on the same line.'
245

246
      $GREP '^[^()]{[^()]*{[^()]$' $tmp &&
247
	diag 'No two { on the same line.'
248

249
      $GREP 'delete[	 ]*[(][^(]*[)];' $tmp &&
250
	diag 'No useless parentheses after delete.'
251

252
      $GREP 'return[	 ]*[(][^(]*[)];' $tmp &&
253
	diag 'No useless parentheses after return.'
254

255
      $GREP 'NULL' $tmp &&
256
	diag 'Use nullptr instead of NULL.'
257

258
259
      # std::list::size() can be O(n).  Better use empty() whenever
      # possible, even for other containers.
260
      e$GREP '(->|[.])size\(\) [=!]= 0|![a-zA-Z0-9_]*(->|[.])size\(\)|(if |while |assert)\([a-zA-Z0-9_]*(->|[.])size\(\)\)' $tmp &&
261
	diag 'Prefer empty() to check emptiness.'
262

263
      e$GREP 'assert\((0|!".*")\)' $tmp &&
264
265
        diag 'Prefer SPOT_UNREACHABLE or SPOT_UNIMPLEMENTED.'

266
      e$GREP '^[^=*<]*([+][+]|--);' $tmp &&
267
	diag 'Take good habits: use ++i instead of i++ when you have the choice.'
268

269
      $GREP '[^a-zA-Z0-9_](\*[a-zA-Z0-9_]*)\.' $tmp &&
270
	diag 'Use "x->y", not "(*x).y"'
271

272
      # we allow these functions only in ?...:...
273
      e$GREP 'bdd_(false|true)[	]*\(' $tmp | $GREP -v '[?:]' &&
274
275
	diag 'Use bddfalse and bddtrue instead of bdd_false() and bdd_true()'

276
277
278
279
280
281
282
      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
283

284
285
      case $file in
	*.hh | *.hxx)
286
	  if e$GREP '(<<|>>)' $tmp >/dev/null; then
287
288
	    :
	  else
289
	    $GREP '#.*include.*<iostream>' $tmp &&
290
291
	      diag 'Avoid <iostream> in headers, better use <iosfwd>.'
	  fi
292
293
	  # Headers from spot/priv/ are not installed, so may only be
	  # included from *.cc files or from other spot/priv/ headers
294
295
	  # (in the latter case they do not have to specify the priv/
	  # directory, so they will not match this regex).
296
297
298
299
300
301
302
	  case $file in
	    */priv/*|*/bin/*);;
	    *)
	      $GREP '#.*include.*priv/' $tmp &&
		  diag 'Do not include private headers in public headers.'
	      ;;
	  esac
303
304
	  ;;
	*.cc)
305
	  if $GREP 'namespace$' $tmp >/dev/null; then
306
307
308
	    :
	  else
	    # We only check classes, but the rule should apply to functions too
309
	    $GREP '^[	 ]*class[	 ]' $tmp &&
310
311
312
313
	      diag 'Private definitions must be in anonymous namespace.'
	  fi
	  ;;
      esac
314
315
316
317
318
319
320
321
322
      case $file in
	*/bin/*|*.cc);;
	*)
	  $GREP -v '#.*include.*priv/' $tmp | $GREP '#.*include.*"' &&
	      diag 'Use <header> instead of "header" for public headers.'
	  $GREP '#.*include.*<spot/priv/.*>' $tmp &&
	      diag 'Use "spot/priv/..." instead of <spot/priv/...>".'
	  ;;
      esac
323
      ;;
324
325
    esac

326

327
    $fail && echo "$file" >>failures.style
328
329

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

Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
332
333

# Rules for Makefiles.
334
for dir in "${INCDIR-..}" "${INCDIR-..}/../bin" "${INCDIR-..}/../tests"; do
Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
335
336
337
338
339
340
341
342

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

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

343
    $GREP '[	 ]$' $tmp &&
Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
344
345
      diag 'Trailing whitespace.'

346
    $GREP '\.libs/' $tmp &&
Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
347
348
	diag "Don't reference files in .libs/, use Libtool instead."

349
    $fail && echo "$file" >>failures.style
Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
350
351
352
  done || : # Make sure sh does not abort when read exits with false.
done

353
if test -f failures.style; then
354
   echo "The following files contain style errors:"
355
356
   cat failures.style
   rm failures.style
357
358
   exit 1;
fi
359
360

exit 0