summaryrefslogtreecommitdiffstats
path: root/crawl-ref/source/util/includes.sh
diff options
context:
space:
mode:
authorRobert Vollmert <rvollmert@gmx.net>2009-10-09 12:08:13 +0200
committerRobert Vollmert <rvollmert@gmx.net>2009-10-09 12:10:59 +0200
commitf4fbc401fe0032429008bcc541587f8f2d77cc08 (patch)
tree285fff4333dc4b308f04a53863fcf9fa4687e82f /crawl-ref/source/util/includes.sh
parent477b8798b39a01bf4a73697eefe0ae131a12d47d (diff)
downloadcrawl-ref-f4fbc401fe0032429008bcc541587f8f2d77cc08.tar.gz
crawl-ref-f4fbc401fe0032429008bcc541587f8f2d77cc08.zip
Add shell script to find unneeded includes.
Diffstat (limited to 'crawl-ref/source/util/includes.sh')
-rw-r--r--crawl-ref/source/util/includes.sh56
1 files changed, 56 insertions, 0 deletions
diff --git a/crawl-ref/source/util/includes.sh b/crawl-ref/source/util/includes.sh
new file mode 100644
index 0000000000..ac96baca10
--- /dev/null
+++ b/crawl-ref/source/util/includes.sh
@@ -0,0 +1,56 @@
+# A bunch of shell functions that help finding unnecessary includes.
+# Relies on appropriate versions of etags, grep, tr, awk and maybe bash.
+#
+# To use:
+# $ . util/include.sh
+# $ checkall
+
+
+# Tries to list all names defined in the header file $1.
+# Indented names are assumed to be members that don't need to be checked,
+# which will fail for indented declarations with #ifdefs.
+names ()
+{
+ b=$(basename $1 .h);
+ etags --declarations -D $1 -o - | grep '^[#a-z]' | grep -v '^'$b'\.h' \
+ | tr '\177' '\t' | awk '{ print $(NF-1) }'
+}
+
+# lists possible uses in $2 of names defined in $1
+mightuse ()
+{
+ for n in $(names $1);
+ do
+ if grep -F $n $2; then
+ return 0;
+ fi;
+ done;
+ return 1
+}
+
+# checks whether source file $2 #include's $1
+includes ()
+{
+ grep '#include "'$1'"' $2
+}
+
+# echo arguments if $2 includes $1 put shouldn't
+check ()
+{
+ if includes $1 $2 > /dev/null && ! mightuse $1 $2 > /dev/null; then
+ echo $hdr $src;
+ fi
+}
+
+# run check on all pairs -- should really cache the result of "names"
+checkall ()
+{
+ for hdr in *.h; do
+ if [ $hdr = AppHdr.h ]; then
+ continue;
+ fi
+ for src in *.cc; do
+ check $hdr $src
+ done
+ done
+}