style.test 10.7 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

# Ensure consistent style by catching common improper constructs.

set -e
27
set +x
28
29
30
31
32
33
34
35

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

36
rm -f failures.style
37

38
39
GREP=grep

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

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

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

57
58
59
# 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.
60
61
TOP=${srcdir-.}/../..
for dir in "$TOP/spot" "$TOP/bin" "$TOP/tests"; do
62

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

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

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

81
    fail=false
82

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

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

92
93
94
    $GREP -q 'coding: utf-8' $file ||
      diag 'missing -*- coding: utf-8 -*-'

95
    $GREP Copyright $file >/dev/null ||
96
97
      diag "missing copyright"

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

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

117
118
119
120
121
122
123
    # 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

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

143
    $GREP '[	 ]$' $tmp &&
144
      diag 'Trailing whitespace.'
145

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

152
      $GREP '[	 ]if(' $tmp &&
153
	diag 'Missing space after "if"'
154

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

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

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

164
      $GREP '[	 ]while(' $tmp &&
165
	diag 'Missing space after "while"'
166

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

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

173
      $GREP '[	 ]for(' $tmp &&
174
	diag 'Missing space after "for"'
175

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

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

182
      $GREP '[	 ]switch(' $tmp &&
183
	diag 'Missing space after "switch"'
184

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

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

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

194
      $GREP '( ' $tmp &&
195
	diag 'No space after opening (.'
196

197
      $GREP ' )' $tmp &&
198
	diag 'No space before closing ).'
199

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

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

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

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

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

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

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

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

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

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

235
      $GREP '[	 ];' $tmp &&
236
	diag 'No space before semicolon.'
237

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

241
242
243
244
245
      # 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 &&
246
	diag 'No two } on the same line.'
247

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

Maximilien Colange's avatar
Maximilien Colange committed
251
252
      $GREP 'delete[	 ]*[(][^(]*[)];' $tmp |
      $GREP -v 'operator[	 ]*delete[	 ]*[(][^(]*[)];' &&
253
	diag 'No useless parentheses after delete.'
254

255
      $GREP 'return[	 ]*[(][^(]*[)];' $tmp &&
256
	diag 'No useless parentheses after return.'
257

258
      $GREP 'NULL' $tmp &&
259
	diag 'Use nullptr instead of NULL.'
260

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

266
      e$GREP 'assert\((0|!".*")\)' $tmp &&
267
268
        diag 'Prefer SPOT_UNREACHABLE or SPOT_UNIMPLEMENTED.'

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

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

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

279
280
281
282
283
284
285
      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
286

287
288
      case $file in
	*.hh | *.hxx)
289
	  if e$GREP '(<<|>>)' $tmp >/dev/null; then
290
291
	    :
	  else
292
	    $GREP '#.*include.*<iostream>' $tmp &&
293
294
	      diag 'Avoid <iostream> in headers, better use <iosfwd>.'
	  fi
Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
295
296
          $GREP '^[ ]*	' $tmp &&
              diag 'Use spaces instead of tabs.'
297
298
	  # Headers from spot/priv/ are not installed, so may only be
	  # included from *.cc files or from other spot/priv/ headers
299
300
	  # (in the latter case they do not have to specify the priv/
	  # directory, so they will not match this regex).
301
302
303
304
	  case $file in
	    */priv/*|*/bin/*);;
	    *)
	      $GREP '#.*include.*priv/' $tmp &&
305
306
307
		diag 'Do not include private headers in public headers.'
              $GREP -v '#' $tmp | $GREP 'assert[	 ]*(.*)' &&
                diag 'Use SPOT_ASSERT() instead of assert() in public headers.'
308
	      ;;
309

310
	  esac
311
312
	  ;;
	*.cc)
313
	  if $GREP 'namespace$' $tmp >/dev/null; then
314
315
316
	    :
	  else
	    # We only check classes, but the rule should apply to functions too
317
	    $GREP '^[	 ]*class[	 ]' $tmp &&
318
319
	      diag 'Private definitions must be in anonymous namespace.'
	  fi
320
321
          e$GREP '	' $tmp &&
	      diag 'Use spaces instead of tabs.'
322
323
	  ;;
      esac
324
325
326
327
328
329
330
331
332
      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
333
      ;;
334
335
    esac

336

337
    $fail && echo "$file" >>failures.style
338
339

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

Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
342
343

# Rules for Makefiles.
344
for dir in "${INCDIR-..}" "${INCDIR-..}/../bin" "${INCDIR-..}/../tests"; do
Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
345
346
347
348
349
350
351
352

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

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

353
    $GREP '[	 ]$' $tmp &&
Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
354
355
      diag 'Trailing whitespace.'

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

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

363
if test -f failures.style; then
364
   echo "The following files contain style errors:"
365
366
   cat failures.style
   rm failures.style
367
368
   exit 1;
fi
369
370

exit 0