summaryrefslogtreecommitdiff
path: root/bin
diff options
context:
space:
mode:
authorNoel Grandin <noel@peralex.com>2014-12-23 09:47:48 +0200
committerNoel Grandin <noel@peralex.com>2015-01-08 10:39:36 +0200
commit45ec1d9b56cec41596f806ebf845ebfcd30c9f46 (patch)
treee368da6d3ae2c8a5c8e214b99dd0f95861d0fc78 /bin
parent0748a5f8298e2ea817e131234f5e99300ed7f9f7 (diff)
brute-force find-and-remove of unused #define constants.
Change-Id: I7223530ae37297a76654cd00cc1fedb56dbe3adb
Diffstat (limited to 'bin')
-rw-r--r--bin/find-unused-defines.awk18
-rwxr-xr-xbin/find-unused-defines.sh27
2 files changed, 45 insertions, 0 deletions
diff --git a/bin/find-unused-defines.awk b/bin/find-unused-defines.awk
new file mode 100644
index 000000000000..2649e41d0534
--- /dev/null
+++ b/bin/find-unused-defines.awk
@@ -0,0 +1,18 @@
+{
+ x++
+ y=$0
+}
+
+END {
+ tmp = substr(y, 0, index(y, ":"))
+ if (x==1) print "sed -i '/[[:space:]]" p1 "[[:space:]]/d' " tmp
+}
+
+
+
+
+# | xargs -P 4 -Ixxx sh -c "git grep -w 'xxx' | awk '{ x++; y=\$0 } END { if (x==1) print y }' && echo \"xxx\" 1>&2"
+
+# sed -i '' '/pattern/d'
+# | awk 'arr[$0]++ END { for (i in arr) { if(arr[i]==1) print i } }' \
+# | awk -f find-unused-defines.awk
diff --git a/bin/find-unused-defines.sh b/bin/find-unused-defines.sh
new file mode 100755
index 000000000000..6c07de4aeea5
--- /dev/null
+++ b/bin/find-unused-defines.sh
@@ -0,0 +1,27 @@
+#
+# This is a pretty brute-force approach. It takes several hours to run on a top-spec MacbookAir.
+# It also produces some false positives, so it requires careful examination and testing of the results.
+#
+# Algorithm Summary:
+# First we find all #defines,
+# then we search for each of them in turn,
+# and if we find only one instance of a #define, we print it out.
+#
+# Algorithm Detail:
+# (1) find #defines, excluding the externals folder
+# (2) extract just the constant name from the search results
+# (3) trim blank lines
+# (4) sort the results, mostly so I have an idea how far along the process is
+# (5) for each result:
+# (6) grep for the constant
+# (7) use awk to to check if only one match for a given constant was found
+# (8) if so, generate a sed command to remove the #define
+#
+git grep -P '^#define\s+\w+\s+\w' -- "[!e][!x][!t]*" \
+ | cut -s -d ' ' -f 2 \
+ | sed '/^$/d' \
+ | sort \
+ | xargs -Ixxx sh -c \
+ "git grep -w 'xxx' | awk -f bin/find-unused-defines.awk -v p1=xxx && echo \"xxx\" 1>&2"
+
+