[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: how to find unused local variables?
- From: Luiz Henrique de Figueiredo <lhf@...>
- Date: Sun, 18 May 2008 20:38:40 -0300
> I seem to remember that once upon a time LHF posted a script
> that could be run over the output of luac to find unused local variables,
> but I am not having any luck finding something in the archives.
Neither am I. But I did find a script that does that though I'm not sure it
does actually work... It's attached. Please let me know how it does :-)
--lhf
-- find unused local vars
-- typical usage: luac -p -l *.lua | lua locals.lua
local R,W
local where
local OP={}
local function set(x)
x=tonumber(x)
W[x]=true
end
local function get(x)
x=tonumber(x)
if x>=0 and not W[x] then
print("local",x,"undefined",where)
end
R[x]=true
end
function OP.MOVE(a,b,c)
get(b)
set(a)
end
function OP.LOADK(a,b,c)
set(a)
end
function OP.LOADBOOL(a,b,c)
set(a)
end
function OP.LOADNIL(a,b,c)
do return end
for i=a,b do
set(i)
end
end
function OP.GETUPVAL(a,b,c)
set(a)
end
function OP.GETGLOBAL(a,b,c)
set(a)
end
function OP.GETTABLE(a,b,c)
get(b)
set(a)
end
function OP.SETGLOBAL(a,b,c)
get(a)
end
function OP.SETUPVAL(a,b,c)
get(a)
end
function OP.SETTABLE(a,b,c)
get(a)
get(b)
get(c)
end
function OP.NEWTABLE(a,b,c)
set(a)
end
function OP.SELF(a,b,c)
get(b)
set(a+1)
get(c)
set(a)
end
function OP.ADD(a,b,c)
get(b)
get(c)
set(a)
end
function OP.SUB(a,b,c)
get(b)
get(c)
set(a)
end
function OP.MUL(a,b,c)
get(b)
get(c)
set(a)
end
function OP.DIV(a,b,c)
get(b)
get(c)
set(a)
end
function OP.MOD(a,b,c)
get(b)
get(c)
set(a)
end
function OP.POW(a,b,c)
get(b)
get(c)
set(a)
end
function OP.UNM(a,b,c)
get(b)
set(a)
end
function OP.NOT(a,b,c)
get(b)
set(a)
end
function OP.LEN(a,b,c)
get(b)
set(a)
end
function OP.CONCAT(a,b,c)
for i=b,c do
get(i)
end
set(a)
end
function OP.JMP(a,b,c)
end
function OP.EQ(a,b,c)
get(b)
get(c)
end
function OP.LT(a,b,c)
get(b)
get(c)
end
function OP.LE(a,b,c)
get(b)
get(c)
end
function OP.TEST(a,b,c)
get(a)
end
function OP.TESTSET(a,b,c)
get(b)
set(a)
end
function OP.CALL(a,b,c)
for i=a,a+b-1 do
get(i)
end
for i=a,a+c-2 do
set(i)
end
end
function OP.TAILCALL(a,b,c)
for i=a,a+b-1 do
get(i)
end
end
function OP.RETURN(a,b,c)
for i=a,a+b-2 do
get(i)
end
end
function OP.FORLOOP(a,b,c)
get(a)
get(a+2)
set(a)
get(a+1)
get(a)
set(a+3)
end
function OP.FORPREP(a,b,c)
get(a)
get(a+2)
set(a)
end
function OP.TFORLOOP(a,b,c)
for i=a,a+2 do
get(i)
end
for i=a+3,a+c+2 do
set(i)
end
end
function OP.SETLIST(a,b,c)
get(a)
for i=a+1,a+b do
get(i)
end
end
function OP.CLOSE(a,b,c)
end
function OP.CLOSURE(a,b,c)
set(a)
end
function OP.VARARG(a,b,c)
for i=a,a+b-1 do
set(i)
end
end
local function report()
if R~=nil then
for i=0,table.maxn(R) do
--for i in pairs(R) do
if R[i]==nil then
print("local",i,"unused",where)
end
end
end
end
local S={}
local G={}
local F
while true do
local s=io.read()
if s==nil then break end
--print(s)
local ok,_,f=string.find(s,"^[mf].-<(.-):[%d,]+>")
if ok then F=f
report()
R={}
W={}
end
s=s.." -1"
local ok,_,l,op,a,b,c=string.find(s,"%[%-?(%d*)%]%s*(%S+)%s+(%S+)%s+(%S+)%s+(%S+)")
--print(ok,_,l,op,a,b,c)
if ok then
where=F..":"..l
OP[op](a,b,c)
end
end
report()