summaryrefslogtreecommitdiff
path: root/contrib/check_warning_flags.sh
blob: d9c5735abf81f039d0e2f01be1bbcff975032f81 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
#! /bin/sh
#
# Check that the warning flags documented in invoke.texi match up
# with what the compiler accepts.
#
# Copyright (C) 2008, 2009, 2010 Free Software Foundation, Inc.
# Written by Ralf Wildenhues <Ralf.Wildenhues@gmx.de>.
#
# This script is Free Software, and it can be copied, distributed and
# modified as defined in the GNU General Public License.  A copy of
# its license can be downloaded from http://www.gnu.org/copyleft/gpl.html
#
# Call this script as
#    check_warning_flags.sh path/to/invoke.texi
# with CC set to the compiler to be tested.
# The script scribbles in the current directory.

progname=`echo "$0" | sed 's,.*/,,'`
usage ()
{
  echo "usage: $progname path/to/gcc/doc"
  echo "set \$CC to the compiler to be checked"
  exit 1
}

ret=0
LC_ALL=C
export LC_ALL
: ${CC=gcc}
test $# = 1 || usage
gcc_docdir=$1
invoke_texi=$gcc_docdir/invoke.texi
test -r "$invoke_texi" || {
  echo "$progname: error: cannot read '$invoke_texi'" >&2
  usage
}
filebase=check_warning_flags_file$$
stderr=check_warning_flags_stderr$$

remove_problematic_flags='
  /-Wlarger-than-/d
  /-Wframe-larger-than/d
  /-Wdisallowed-function-list/d
  /-W[alp],/d
  /-Werror/d
  /-Wpadded/d
  /pedantic-ms-format/d
  /=/d'

# Ensure that indexed warnings are accepted.
set x `sed '/^@opindex W/{
  s/^@opindex /-/
  '"$remove_problematic_flags"'
  /-W[alp]$/d
  p
}
d' <"$invoke_texi"`
shift
: >$filebase.c
$CC -c $filebase.c "$@" 2>&1 |
  grep -v 'command line option.*is valid for.*but not for' >$stderr
if test -s $stderr; then
  echo "options listed in @opindex but not accepted by the compiler:" >&2
  cat $stderr >&2
  ret=1
fi
rm -f $filebase.c $stderr

# Check documentation of warning options.
for lang in c c++ objc obj-c++; do
  case $lang in
  c)       ext=c; langmatch='[^-]C[^+].*only' ;;
  c++)     ext=C; langmatch='[^-]C++.*only' ;;
  objc)    ext=m; langmatch='Objective-C[^+].*only' ;;
  obj-c++) ext=M; langmatch='Objective-C++.*only' ;;
  esac
  file=$filebase.$ext
  : >$file
  $CC -c $file 2>$stderr
  if grep 'not installed on this system' $stderr >/dev/null ||
    grep 'installation problem, cannot exec' $stderr >/dev/null ||
    grep 'error trying to exec' $stderr >/dev/null
  then
    echo "$progname: $CC is not configured for language $lang, skipping checks" >&2
    rm -f $file $filebase.o $filebase.obj $stderr
    continue
  fi

  # Verify good warning flags.
  set x `sed '
    t a
    :a
    /^@item -W/{
      /'"$langmatch"'/b x
      / only)/d
      b x
    }
    d
    :x
    '"$remove_problematic_flags"'
    s/^@item //
    s/ .*//
    ' <"$invoke_texi"`
  shift
  $CC -c $file -O "$@" 2>$stderr
  if test -s $stderr; then
    echo failures:  >&2
    cat $stderr >&2
    ret=1
  fi

  # Verify bad warning flags.
  set x `sed '
    t a
    :a
    /^@item -W/{
      / only)/!d
      /'"$langmatch"'/d
      b x
    }
    d
    :x
    '"$remove_problematic_flags"'
    s/^@item //
    s/ .*//
    ' <"$invoke_texi"`
  shift
  $CC -c $file -O "$@" 2>$stderr
  # cat $stderr >&2
  test $# = `grep 'command line option.*valid.*but not for' <$stderr | wc -l` || {
    for warning
    do
      grep "command line option.*$warning.*valid" <$stderr >&2 ||
	echo "valid for $lang but not annotated as such: $warning"
    done
    ret=1
  }
  rm -f $file $filebase.o $filebase.obj $stderr
done


remove_problematic_help_flags='
  /^W$/d
  /^W[alp]$/d
  /^Werror-implicit-function-declaration$/d
  /^Wsynth$/d
  /-$/d
  /=/d'
help_flags=`
  $CC --help -v 2>/dev/null | tr ' ' '\012' |
    sed -n '
      b a
      :a
      s/^-\(W[^<,]*\).*/\1/
      t x
      d
      :x
      '"$remove_problematic_help_flags"'
      p' | sort -u`
: >$filebase.c
for flag in $help_flags; do
  $CC -c $filebase.c -$flag 2>/dev/null || {
    echo "warning -$flag not supported" >&2
    ret=1
  }
  grep "@item.*$flag" $gcc_docdir/../*/*.texi >/dev/null || {
    # For @item, we are satisfied with either -Wfoo or -Wno-foo.
    inverted_flag=`echo "$flag" | sed '
      s/^Wno-/W/
      t
      s/^W/Wno-/'`
    grep "@item.*$inverted_flag" $gcc_docdir/../*/*.texi >/dev/null || {
      echo "warning -$flag not documented in $gcc_docdir/../*/*.texi" >&2
      ret=1
    }
  }
done
rm -f $filebase.c $filebase.o

exit $ret