Skip to content

Instantly share code, notes, and snippets.

@Arcitec
Last active January 7, 2022 04:10
Show Gist options
  • Save Arcitec/e09055cc63ce48472909bcd7a97309b7 to your computer and use it in GitHub Desktop.
Save Arcitec/e09055cc63ce48472909bcd7a97309b7 to your computer and use it in GitHub Desktop.
Static analysis of Lua 5.1/5.2/5.3 source, finding all global references and making them local
# Based on http://lua-users.org/wiki/DetectingUndefinedVariables, but extended to automate the code generation.
# Lua 5.1
for f in *.lua; do luac-5.1 -p -l "$f" | grep ETGLOBAL | cut -d ';' -f 2 | sort | uniq | sed -E 's/^ (.+)$/local \1 = \1;/' > "_globals.${f}.txt"; done
# Lua 5.2 and 5.3
for f in *.lua; do luac-5.3 -p -l "$f" | grep 'ETTABUP.*_ENV' | cut -d ';' -f 2 | cut -d ' ' -f 1-3 | sort | uniq | sed -E 's/^ _ENV "(.+)"\s*$/local \1 = \1;/' > "_globals.${f}.txt"; done
# The resulting log files will be overzealous (any non-"local"-declared vars from the same file will be seen as globals),
# so you'll still have to do manual work to remove the things that aren't necessary. But it's a lot better than doing
# all of this work manually, trudging through thousands of lines of code to find all external function calls... hehe!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment