Fix compiler warning due to missing function prototype.
[svn.git] / tools / dev / warn-ignored-err.sh
blob98d94fd07d001d8f07eb57c55a7ccb26cabaf1a7
1 #!/bin/sh
2 HELP="\
3 Usage: $0 [--remove] [FILE...]
5 Insert or remove the GCC attribute \"warn_unused_result\" on each function
6 that returns a Subversion error, in the specified files or, by default,
7 *.h and *.c in the ./subversion and ./tools trees.
10 LC_ALL=C
12 # Parse options
13 REMOVE=
14 case "$1" in
15 --remove) REMOVE=1; shift;;
16 --help) echo "$HELP"; exit 0;;
17 --*) echo "$0: unknown option \"$1\"; try \"--help\""; exit 1;;
18 esac
20 # Set the positional parameters to the default files if none specified
21 if [ $# = 0 ]; then
22 set -- `find subversion/ tools/ -name '*.[ch]'`
25 # A line that declares a function return type of "svn_error_t *" looks like:
26 # - Possibly leading whitespace, though not often.
27 # - Possibly "static" or "typedef".
28 # - The return type "svn_error_t *".
29 # - Possibly a function or pointer-to-function declarator:
30 # - "identifier"
31 # - "(identifier)" (used in some typedefs)
32 # - "(*identifier)"
33 # with either nothing more, or a "(" next (especially not "," or ";" or "="
34 # which all indicate a variable rather than a function).
36 # Regular expressions for "sed"
37 # Note: take care in matching back-reference numbers to parentheses
38 PREFIX="^\( *\| *static *\| *typedef *\)"
39 RET_TYPE="\(svn_error_t *\* *\)"
40 IDENT="[a-zA-Z_][a-zA-Z0-9_]*"
41 DECLR="\($IDENT\|( *\(\*\|\) *$IDENT *)\)"
42 SUFFIX="\($DECLR *\((.*\|\)\|\)$"
44 # The attribute string to be inserted or removed
45 ATTRIB_RE="__attribute__((warn_unused_result))" # regex version of it
46 ATTRIB_STR="__attribute__((warn_unused_result))" # plain text version of it
48 if [ $REMOVE ]; then
49 SUBST="s/$PREFIX$ATTRIB_RE $RET_TYPE$SUFFIX/\1\2\3/"
50 else
51 SUBST="s/$PREFIX$RET_TYPE$SUFFIX/\1$ATTRIB_STR \2\3/"
54 for F do
55 # Edit the file, leaving a backup suffixed with a tilde
56 { sed -e "$SUBST" "$F" > "$F~1" &&
57 { ! cmp -s "$F" "$F~1"; } &&
58 mv "$F" "$F~" && # F is briefly absent now; a copy could avoid this
59 mv "$F~1" "$F"
60 } ||
61 # If anything went wrong or no change was made, remove the temporary file
62 rm "$F~1"
63 done