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.
15 --remove) REMOVE
=1; shift;;
16 --help) echo "$HELP"; exit 0;;
17 --*) echo "$0: unknown option \"$1\"; try \"--help\""; exit 1;;
20 # Set the positional parameters to the default files if none specified
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:
31 # - "(identifier)" (used in some typedefs)
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
49 SUBST
="s/$PREFIX$ATTRIB_RE $RET_TYPE$SUFFIX/\1\2\3/"
51 SUBST
="s/$PREFIX$RET_TYPE$SUFFIX/\1$ATTRIB_STR \2\3/"
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
61 # If anything went wrong or no change was made, remove the temporary file