style.test 10.8 KB
Newer Older
1
#! /bin/sh
2
# -*- coding: utf-8 -*-
3 4
# Copyright (C) 2009-2017 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 88 89 90
    # Check this before stripping comments and strings.
    $GREP -i 'dictionnar[yi]' $file &&
      diag 'dictionnary -> dictionary'

91
    $GREP -i 'version 2 of the License' $file &&
92
      diag 'license text should refer to version 2'
93
    $GREP -i 'Temple Place' $file &&
94 95
      diag 'license text should give a url instead of an address'

96 97 98
    $GREP -q 'coding: utf-8' $file ||
      diag 'missing -*- coding: utf-8 -*-'

99
    $GREP Copyright $file >/dev/null ||
100 101
      diag "missing copyright"

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

109 110 111 112 113 114 115 116 117 118 119 120
    # 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'"

121 122 123 124 125 126 127
    # 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

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

147
    $GREP '[	 ]$' $tmp &&
148
      diag 'Trailing whitespace.'
149

150 151 152
    case $file in
    *.test);;
    *)
153
      $GREP -E '[^<]class[ \t]+[A-Z]' $tmp &&
154 155
        diag 'Use lower case class names.'

156
      $GREP '[	 ]if(' $tmp &&
157
	diag 'Missing space after "if"'
158

159
      $GREP '[	 ]if (.*).*{' $tmp &&
160
	diag 'Opening { should be on its own line.'
161

162
      $GREP '[	 ]if (.*).*;' $tmp &&
163
	diag 'if body should be on another line.'
164

165
      $GREP '[	 ]else.*;' $tmp &&
166
	diag 'else body should be on another line.'
167

168
      $GREP '[	 ]while(' $tmp &&
169
	diag 'Missing space after "while"'
170

171
      $GREP '[	 ]while (.*).*{' $tmp &&
172
	diag 'Opening { should be on its own line.'
173

174
      $GREP '[	 ]while (.*).*[^)];' $tmp &&
175
	diag 'while body should be on another line.'
176

177
      $GREP '[	 ]for(' $tmp &&
178
	diag 'Missing space after "for"'
179

180
      $GREP '[	 ]for (.*).*{' $tmp &&
181
	diag 'Opening { should be on its own line.'
182

183
      $GREP '[	 ]for (.*;.*;.*).*;' $tmp &&
184
	diag 'for body should be on another line.'
185

186
      $GREP '[	 ]switch(' $tmp &&
187
	diag 'Missing space after "switch"'
188

189
      $GREP '[	 ]switch (.*).*{' $tmp &&
190
	diag 'Opening { should be on its own line.'
191

192
      $GREP 'namespace .*{' $tmp &&
193
	diag 'Opening { should be on its own line.'
194

195
      $GREP 'class .*{' $tmp | $GREP -v enum &&
196
	diag 'Opening { should be on its own line.'
197

198
      $GREP '( ' $tmp &&
199
	diag 'No space after opening (.'
200

201
      $GREP ' )' $tmp &&
202
	diag 'No space before closing ).'
203

204
      $GREP '! ' $tmp &&
205
	diag 'No space after unary operators (!).'
206

207
      $GREP ",[^ \"	%'\\\\]" $tmp &&
208
	diag 'Space after coma.'
209

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

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

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

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

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

230
      $GREP 'operator[^(]* (' $tmp &&
Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
231
        diag 'No space before ('
232

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

236
      $GREP '[	 ]case.*:[^:].*;' $tmp &&
237
	diag 'Label should be on their own line.'
238

239
      $GREP '[	 ];' $tmp &&
240
	diag 'No space before semicolon.'
241

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

245 246 247 248 249
      # 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 &&
250
	diag 'No two } on the same line.'
251

252
      $GREP '^[^()]{[^()]*{[^()]$' $tmp &&
253
	diag 'No two { on the same line.'
254

Maximilien Colange's avatar
Maximilien Colange committed
255 256
      $GREP 'delete[	 ]*[(][^(]*[)];' $tmp |
      $GREP -v 'operator[	 ]*delete[	 ]*[(][^(]*[)];' &&
257
	diag 'No useless parentheses after delete.'
258

259
      $GREP 'return[	 ]*[(][^(]*[)];' $tmp &&
260
	diag 'No useless parentheses after return.'
261

262
      $GREP 'NULL' $tmp &&
263
	diag 'Use nullptr instead of NULL.'
264

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

270
      e$GREP 'assert\((0|!".*")\)' $tmp | $GREP -v 'static_assert' &&
271 272
        diag 'Prefer SPOT_UNREACHABLE or SPOT_UNIMPLEMENTED.'

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

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

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

283 284 285 286 287 288 289
      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
290

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

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

340

341
    $fail && echo "$file" >>failures.style
342 343

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

Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
346 347

# Rules for Makefiles.
348
for dir in "${INCDIR-..}" "${INCDIR-..}/../bin" "${INCDIR-..}/../tests"; do
Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
349 350 351 352 353 354 355 356

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

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

357
    $GREP '[	 ]$' $tmp &&
Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
358 359
      diag 'Trailing whitespace.'

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

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

367
if test -f failures.style; then
368
   echo "The following files contain style errors:"
369 370
   cat failures.style
   rm failures.style
371 372
   exit 1;
fi
373 374

exit 0