[svn.haxx.se] · SVN Dev · SVN Users · SVN Org · TSVN Dev · TSVN Users · Subclipse Dev · Subclipse Users · this month's index

Re: [PATCH] Script for warning about error leaks

From: Julian Foad <julianfoad_at_btopenworld.com>
Date: 2006-02-06 18:23:13 CET

John Peacock wrote:
>
> I'm not sure I understand why you are wrapping Perl in a shell script.
> Couldn't you do this all from within a single Perl script (using
> File::Find)?

I didn't think of it as "wrapping" Perl, more that I'm just using Perl as a
utility for replacing strings in a file:

   perl -pi~ -e "s/RE/replacement/" FILE

where I could pretty much equally have used a different utility such as "sed"
(with different RE syntax and less-atomic file replacement):

   mv FILE FILE~
   sed "s/RE/replacement/" < FILE~ > FILE

I'm sure it could all be done in Perl but I don't know Perl at all. If we went
that route, I'd prefer Python which I do know somewhat, but not well enough to
quickly write the whole thing.

If anyone wants to translate it to Python that would be grand.

If not, perhaps I should make it use "sed" because it's more widely installed.
  (I switched to "Perl" earlier on because I thought it was useful to have it
modify potentially multiple files in place, but I ended up putting it in a loop
anyway because I didn't like it touching the unchanged files, so I'm really not
gaining any benefit from it now.)

Attached is a "sed" version. It's slightly less neat. Which do you think is
better?

- Julian

Add a shell script for helping to detect error leaks.

* tools/dev/warn-ignored-err.sh
  New file.

Index: tools/dev/warn-ignored-err.sh
===================================================================
--- tools/dev/warn-ignored-err.sh (revision 0)
+++ tools/dev/warn-ignored-err.sh (revision 0)
@@ -0,0 +1,61 @@
+#!/bin/sh
+HELP="\
+Usage: $0 [--remove] [FILE...]
+
+Insert or remove the GCC attribute \"warn_unused_result\" on each function
+that returns a Subversion error, in the specified files or, by default,
+*.h and *.c in the ./subversion and ./tools trees.
+"
+
+# Parse options
+REMOVE=
+case "$1" in
+--remove) REMOVE=1; shift;;
+--help) echo "$HELP"; exit 0;;
+--*) echo "$0: unknown option \"$1\"; try \"--help\""; exit 1;;
+esac
+
+# Set the positional parameters to the default files if none specified
+if [ $# = 0 ]; then
+ set -- `find subversion/ tools/ -name '*.[ch]'`
+fi
+
+# A line that declares a function return type of "svn_error_t *" looks like:
+# - Possibly leading whitespace, though not often.
+# - Possibly "static" or "typedef".
+# - The return type "svn_error_t *".
+# - Possibly a function or pointer-to-function declarator:
+# - "identifier"
+# - "(identifier)" (used in some typedefs)
+# - "(*identifier)"
+# with either nothing more, or a "(" next (especially not "," or ";" or "="
+# which all indicate a variable rather than a function).
+
+# Regular expressions for "sed"
+# Note: take care in matching back-reference numbers to parentheses
+PREFIX="^\( *\| *static *\| *typedef *\)"
+RET_TYPE="\(svn_error_t *\* *\)"
+IDENT="[a-zA-Z_][a-zA-Z0-9_]*"
+DECLR="\($IDENT\|( *\(\*\|\) *$IDENT *)\)"
+SUFFIX="\($DECLR *\((.*\|\)\|\)$"
+
+ATTRIB_RE="__attribute__((warn_unused_result))"
+ATTRIB_STR="${ATTRIB_RE//\\}" # plain text version of it: remove any backslashes
+
+for F in "$@"; do
+ if [ -f "$F" ]; then
+ # Edit the file, leaving a backup suffixed with a tilde
+ mv "$F" "$F~"
+ if [ $REMOVE ]; then
+ sed "s/$PREFIX$ATTRIB_RE $RET_TYPE$SUFFIX/\1\2\3/" < "$F~" > "$F"
+ else
+ sed "s/$PREFIX$RET_TYPE$SUFFIX/\1$ATTRIB_STR \2\3/" < "$F~" > "$F"
+ fi
+ # If not changed, put the untouched file back
+ if cmp -s "$F" "$F~"; then
+ mv "$F~" "$F"
+ fi
+ else
+ echo "$0: skipping \"$F\": is not a regular file"
+ fi
+done

Property changes on: tools/dev/warn-ignored-err.sh
___________________________________________________________________
Name: svn:executable
   + *
Name: svn:eol-style
   + native

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@subversion.tigris.org
For additional commands, e-mail: dev-help@subversion.tigris.org
Received on Mon Feb 6 18:23:55 2006

This is an archived mail posted to the Subversion Dev mailing list.