Skip to content

Commit

Permalink
avoid warnings by the C compiler on unused local variables
Browse files Browse the repository at this point in the history
  • Loading branch information
mascarenhas committed Oct 20, 2017
1 parent 347edf8 commit eedd59a
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 22 deletions.
20 changes: 18 additions & 2 deletions spec/coder_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -420,9 +420,25 @@ describe("Titan code generator ", function()
local ok, err = checker.check(ast, code, "test.titan")
assert.truthy(ok, err)
assert.same("Exp_ToInt", ast[1].block.stats[2].exp._tag)
local ok, err = generate(ast, "titan_test1")
local ok, err = generate(ast, "titan_test")
assert.truthy(ok, err)
local ok, err = call("titan_test", "local x = titan_test.fn(); assert(math.type(x) == 'integer')")
assert.truthy(ok, err)
local ok, err = call("titan_test1", "local x = titan_test1.fn(); assert(math.type(x) == 'integer')")
end)

it("handles unused locals", function()
local code = [[
function fn(): nil
local f: float = 1.0
local i: integer = f
end
]]
local ast, err = parser.parse(code)
assert.truthy(ast, err)
local ok, err = checker.check(ast, code, "test.titan")
assert.truthy(ok, err)
assert.same("Exp_ToInt", ast[1].block.stats[2].exp._tag)
local ok, err = generate(ast, "titan_test")
assert.truthy(ok, err)
end)

Expand Down
2 changes: 1 addition & 1 deletion titan-compiler/checker.lua
Original file line number Diff line number Diff line change
Expand Up @@ -324,14 +324,14 @@ function checkexp(node, st, errors, context)
if tag == "Var_Name" then
local decl = st:find_symbol(node.name)
if not decl then
-- TODO generate better error messages when we have the line num
local msg = "variable '" .. node.name .. "' not declared"
typeerror(errors, msg, node._pos)
node._type = types.Integer
elseif decl._tag == "TopLevel_Func" then
typeerror(errors, "reference to function " .. node.name .. " outside of function call", decl._pos)
node._type = types.Integer
else
decl._used = true
node._decl = decl
node._type = decl._type
end
Expand Down
45 changes: 26 additions & 19 deletions titan-compiler/coder.lua
Original file line number Diff line number Diff line change
Expand Up @@ -434,28 +434,35 @@ function codestat(ctx, node)
local tag = node._tag
if tag == "Stat_Decl" then
local cstats, cexp = codeexp(ctx, node.exp)
local typ = node.decl._type
node.decl._cvar = "_local_" .. node.decl.name
local cdecl = ctype(typ) .. " " .. node.decl._cvar .. ";"
local cslot = ""
local cset = ""
if types.is_gc(typ) then
node.decl._slot = "_localslot_" .. node.decl.name
cslot = newslot(ctx, node.decl._slot);
cset = string.format([[
/* update slot */
if node.decl._used then
local typ = node.decl._type
node.decl._cvar = "_local_" .. node.decl.name
local cdecl = ctype(typ) .. " " .. node.decl._cvar .. ";"
local cslot = ""
local cset = ""
if types.is_gc(typ) then
node.decl._slot = "_localslot_" .. node.decl.name
cslot = newslot(ctx, node.decl._slot);
cset = string.format([[
/* update slot */
%s
]], setslot(typ, node.decl._slot, node.decl._cvar))
end
return string.format([[
%s
]], setslot(typ, node.decl._slot, node.decl._cvar))
end
return string.format([[
%s
%s
{
%s
%s = %s;
{
%s
%s = %s;
%s
}
]], cdecl, cslot, cstats, node.decl._cvar, cexp, cset)
else
return string.format([[
%s
}
]], cdecl, cslot, cstats, node.decl._cvar, cexp, cset)
((void)%s);
]], cstats, cexp)
end
elseif tag == "Stat_Block" then
return codeblock(ctx, node)
elseif tag == "Stat_While" then
Expand Down

0 comments on commit eedd59a

Please sign in to comment.