Fix: strip --strip-debug breaks relocations
[binutils-gdb.git] / gdb / contrib / words.sh
blob2b21fbd603fadb4d3a6afb4ac9dd632a9a05fdbc
1 #!/bin/sh
3 # Copyright (C) 2019-2023 Free Software Foundation, Inc.
4 # This program is free software; you can redistribute it and/or modify
5 # it under the terms of the GNU General Public License as published by
6 # the Free Software Foundation; either version 3 of the License, or
7 # (at your option) any later version.
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
14 # You should have received a copy of the GNU General Public License
15 # along with this program. If not, see <http://www.gnu.org/licenses/>.
17 # This script intends to facilitate spell checking of source/doc files.
18 # It:
19 # - transforms the files into a list of lowercase words
20 # - prefixes each word with the frequency
21 # - filters out words within a frequency range
22 # - sorts the words, longest first
24 # If '-c' is passed as option, it operates on the C comments only, rather than
25 # on the entire file.
27 # For:
28 # ...
29 # $ files=$(find gdb -type f -name "*.c" -o -name "*.h")
30 # $ ./gdb/contrib/words.sh -c $files
31 # ...
32 # it generates a list of ~15000 words prefixed with frequency.
34 # This could be used to generate a dictionary that is kept as part of the
35 # sources, against which new code can be checked, generating a warning or
36 # error. The hope is that misspellings would trigger this frequently, and rare
37 # words rarely, otherwise the burden of updating the dictionary would be too
38 # much.
40 # And for:
41 # ...
42 # $ files=$(find gdb -type f -name "*.c" -o -name "*.h")
43 # $ ./gdb/contrib/words.sh -c -f 1 $files
44 # ...
45 # it generates a list of ~5000 words with frequency 1.
47 # This can be used to scan for misspellings manually.
50 minfreq=
51 maxfreq=
52 c=false
53 while [ $# -gt 0 ]; do
54 case "$1" in
55 -c)
56 c=true
57 shift
59 --freq|-f)
60 minfreq=$2
61 maxfreq=$2
62 shift 2
64 --min)
65 minfreq=$2
66 if [ "$maxfreq" = "" ]; then
67 maxfreq=0
69 shift 2
71 --max)
72 maxfreq=$2
73 if [ "$minfreq" = "" ]; then
74 minfreq=0
76 shift 2
79 break;
81 esac
82 done
84 if [ "$minfreq" = "" ] && [ "$maxfreq" = "" ]; then
85 minfreq=0
86 maxfreq=0
89 awkfile=$(mktemp)
90 trap 'rm -f "$awkfile"' EXIT
92 cat > "$awkfile" <<EOF
93 BEGIN {
94 in_comment=0
97 // {
98 line=\$0
101 /\/\*/ {
102 in_comment=1
103 sub(/.*\/\*/, "", line)
106 /\*\// {
107 sub(/\*\/.*/, "", line)
108 in_comment=0
109 print line
110 next
113 // {
114 if (in_comment) {
115 print line
120 # Stabilize sort.
121 export LC_ALL=C
123 if $c; then
124 awk \
125 -f "$awkfile" \
126 -- "$@"
127 else
128 cat "$@"
129 fi \
130 | sed \
131 -e 's/[!"?;:%^$~#{}`&=@,. \t\/_()|<>\+\*-]/\n/g' \
132 -e 's/\[/\n/g' \
133 -e 's/\]/\n/g' \
134 -e "s/'/\n/g" \
135 -e 's/[0-9][0-9]*/\n/g' \
136 -e 's/[ \t]*//g' \
137 | tr '[:upper:]' '[:lower:]' \
138 | sort \
139 | uniq -c \
140 | awk "{ if (($minfreq == 0 || $minfreq <= \$1) \
141 && ($maxfreq == 0 || \$1 <= $maxfreq)) { print \$0; } }" \
142 | awk '{ print length($0) " " $0; }' \
143 | sort -n -r \
144 | cut -d ' ' -f 2-