From: Siraaj Khandkar Date: Mon, 1 Mar 2021 17:02:01 +0000 (-0500) Subject: Add erlcode-find-calls-to-module X-Git-Url: https://git.xandkar.net/?a=commitdiff_plain;h=2b05907bb8c2ed2bd6b40375aedfb089416c968a;p=khome.git Add erlcode-find-calls-to-module --- diff --git a/home/bin/erlcode-find-calls-to-module b/home/bin/erlcode-find-calls-to-module new file mode 100755 index 0000000..8e4d9e5 --- /dev/null +++ b/home/bin/erlcode-find-calls-to-module @@ -0,0 +1,46 @@ +#! /bin/bash + +target_module="$1" +shift +dirs=$@ + +find $dirs -type f -name '*.erl' -exec grep -Hn "\<$target_module\>:" '{}' \; \ +| sed 's/%.*$//g' \ +| awk -F "${target_module}:" -v target_module="$target_module" ' + $1 && $2 { + caller_module_file = $1 + sub(":.*$", "", caller_module_file) + + called_function = $2 + sub("\\(.*$", "", called_function) + + if (called_function ~ /^[a-z][a-zA-Z_0-9]+$/) { + Calls[called_function]++ + Calls_from[caller_module_file, called_function]++ + Caller_modules[caller_module_file]++ + } else { + printf \ + "[WARN] skipped an invalid erlang function name. File: \"%s\", function: \"%s\", original line: \"%s\"\n", \ + caller_module_file, called_function, $0 \ + > "/dev/stderr" + } + } + + END { + indent = " " + for (caller_module_file in Caller_modules) { + print caller_module_file; + for (cf in Calls_from) { + split(cf, call, SUBSEP); + if (call[1] == caller_module_file) + printf "%s%s\n", indent, call[2] | "sort"; + } + close("sort") + } + + print "" + printf "Inferred API of %s:\n", target_module + for (called_function in Calls) + printf "%s%s\n", indent, called_function | "sort" + close("sort") + }'