Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
function find_globals () {
# Find all the global declarations
globals=$(echo "$cut_content" | grep '^[[:space:]]global ' | sed 's/\s*global //')
if test -z "$globals"; then
# Join all the global declarations into one line.
globals=$(echo "$globals" | paste -sd ' ')
for global in $globals; do
# Check if this global is used in the funtction.
# We don't want to check on declaration lines.
usage=$(echo "$cut_content" | grep -v '^[[:space:]]*global ' | grep "$global")
# echo "Usage = $usage"
if test -z "$usage"; then
echo "Global $global declared but not used. Function start at $2:$3"
files=$(git ls-files | grep 'tcl$')
for file in $files; do
content=$(< $file)
# Look for the start of a procedure. But instead of writing a proper parser,
# just look for '^proc', which will catch almost all cases.
procs=$(grep -n '^proc' $file | cut -f1 -d:)
for start_line in $procs; do
# Find the end of this proc. The hack we use here is to look for '^}'
# after the start of the proc.
cut_content=$(tail -n +$start_line $file)
# Find out the line at which the procedure ends.
end_line=$(echo "$cut_content" | grep -n '^}' | head -n 1 | cut -f1 -d:)
cut_content=$(echo "$cut_content" | head -n $end_line)
find_globals "$cut_content" $start_line $file
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment