style.test 3.2 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
    sed 's,//.*,,' < $file > $tmp
38

39
40
    grep '[	 ]if(' $tmp &&
      diag 'Missing space after "if"'
41

42
43
    grep '[	 ]if (.*).*{' $tmp &&
      diag 'Opening { should be on its own line.'
44

45
46
    grep '[	 ]if (.*).*;' $tmp &&
      diag 'if body should be on another line.'
47

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

51
52
    grep '[	 ]while(' $tmp &&
      diag 'Missing space after "while"'
53

54
55
    grep '[	 ]while (.*).*{' $tmp &&
      diag 'Opening { should be on its own line.'
56

57
58
    grep '[	 ]while (.*).*[^)];' $tmp &&
      diag 'while body should be on another line.'
59

60
61
    grep '[	 ]for(' $tmp &&
      diag 'Missing space after "for"'
62

63
64
    grep '[	 ]for (.*).*{' $tmp &&
      diag 'Opening { should be on its own line.'
65

66
67
    grep '[	 ]for (.*;.*;.*).*;' $tmp &&
      diag 'for body should be on another line.'
68

69
70
    grep '[	 ]switch(' $tmp &&
      diag 'Missing space after "switch"'
71

72
73
    grep '[	 ]switch (.*).*{' $tmp &&
      diag 'Opening { should be on its own line.'
74

75
76
    grep '( ' $tmp &&
      diag 'No space after opening (.'
77

78
79
    grep ' )' $tmp &&
      diag 'No space before closing ).'
80

81
82
    grep '! ' $tmp &&
      diag 'No space after unary operators (!).'
83

84
    grep ',[^ "%]' $tmp &&
85
      diag 'Space after coma.'
86

87
88
    grep '[^	 ]&&[^	 ]' $tmp &&
      diag 'Space arround binary operators.'
89

90
91
    grep '[^	 ]||[^	 ]' $tmp &&
      diag 'Space arround binary operators.'
92

93
94
    grep '[	 ]default:[^:].*;' $tmp &&
      diag 'Label should be on their own line.'
95

96
97
    grep '[	 ]case.*:[^:].*;' $tmp &&
      diag 'Label should be on their own line.'
98

99
100
    grep '[	 ];' $tmp &&
      diag 'No space before semicolon.'
101

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

105
106
107
108
109
110
    grep '}.*}' $tmp &&
      diag 'No two } on the same line.'

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

111
112
113
114
    grep -e 'delete[	 ]*[(][^(]*[)];' $tmp &&
      diag 'No useless parentheses after delete.'

    grep -e 'return[	 ]*[(][^(]*[)];' $tmp &&
115
      diag 'No useless parentheses after return.'
116

117
    grep 'NULL' $tmp &&
118
119
      diag 'Use 0 instead of NULL.  NULL is not portable.'

120
121
122
123
124
125
126
127
128
129
130
131
    case $file in
      *.hh | *.hxx)
        if grep -e '(cout|cerr|clog)' $tmp >/dev/null; then
          :
	else
          grep '#.*include.*<iostream>' $tmp &&
            diag 'Avoid <iostream> in headers, better use <iosfwd>.'
        fi
        ;;
    esac


132
133
    $fail && echo "$file" >>failures
  done
134
135
136
137
138
139
140
141
done

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