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

[PATCH] Script for warning about error leaks

From: Julian Foad <julianfoad_at_btopenworld.com>
Date: 2006-02-06 01:49:59 CET

My thread about error leaks and how GCC could warn about them ("More error
leaks") concluded with a good idea from David James that we could write a
script to add the attributes that allow GCC to warn, since we didn't much like
the idea of permanently including the annotations in our source files.

Here is such a script, proposed for addition to the "tools/dev/" directory.
It's a shell script that calls perl; it doesn't currently need to be more
portable because it is only relevant to GCC users, but of course if someone
wants to make it more portable that's fine. (Especially tell me if I've used
any non-portable shell or perl syntax.)

This works for me. The most important thing is that the result should compile;
it's not so important if it misses some places that it could have hit.

Please try it and comment.

- 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,56 @@
+#!/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).
+
+# Perl regular expressions
+# Note: take care to match 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="__attribute__\(\(warn_unused_result\)\)"
+ATTRIB_STR="__attribute__((warn_unused_result))" # plain text string
+
+for F in "$@"; do
+ # Edit the file, leaving a backup suffixed with a tilde.
+ if [ $REMOVE ]; then
+ perl -pi~ -e "s/$PREFIX$ATTRIB $RET_TYPE$SUFFIX/\1\2\3/" "$F"
+ else
+ perl -pi~ -e "s/$PREFIX$RET_TYPE$SUFFIX/\1$ATTRIB_STR \2\3/" "$F"
+ fi
+ # If not changed, Perl still touched the file, but we will put it back.
+ if cmp -s "$F" "$F~"; then
+ mv "$F~" "$F"
+ 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 01:50:18 2006

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

This site is subject to the Apache Privacy Policy and the Apache Public Forum Archive Policy.