From db54a15a51a53da3b81dc074df3f4e417ba8396a Mon Sep 17 00:00:00 2001 From: Francois Perrad Date: Mon, 28 Jan 2019 17:40:13 +0100 Subject: [PATCH 1/6] luacheck infrastructure --- .luacheckrc | 47 +++++++++++++++++++++++++++++++++++++++++++++++ Makefile | 3 +++ 2 files changed, 50 insertions(+) create mode 100644 .luacheckrc diff --git a/.luacheckrc b/.luacheckrc new file mode 100644 index 00000000..b2ee724c --- /dev/null +++ b/.luacheckrc @@ -0,0 +1,47 @@ + +codes = true + +std = 'luajit' + +max_line_length = 200 + +ignore = { + '611', -- line contains only whitespace + '612', -- line contains trailing whitespace + '613', -- trailing whitespace in a string + '614', -- trailing whitespace in a comment + '621', -- inconsistent indentation (SPACE followed by TAB) +} + +globals = { + 'terra', + 'terralib', + 'cudalib', +} + +self = false + +files['src/cudalib.lua'].read_globals = { 'opaque', 'uint' } +files['src/cudalib.lua'].ignore = { + '212/cudahome', -- unused argument cudahome +} +files['src/strict.lua'].max_line_length = 400 +files['src/strict.lua'].ignore = { '111/Strict' } -- setting non-standard global variable Strict +files['src/terralib.lua'].max_line_length = 300 +files['src/terralib.lua'].ignore = { + '113/Strict', -- accessing undefined variable Strict + '122/debug', -- setting read-only field traceback of global debug + '142/package', -- setting undefined field terrapath of global package + '143/package', -- accessing undefined field terrapath.gmatch of global package + '211/meet', -- unused variable meet + '211/orig', -- unused variable orig + '212', -- unused argument + '421/result', -- shadowing definition of variable result + '422/e', -- shadowing definition of argument e + '422/v', -- shadowing definition of argument v + '423/i', -- shadowing definition of loop variable i + '431', -- shadowing upvalue + '432', -- shadowing upvalue argument + '511', -- unreachable code + '542', -- empty if branch +} diff --git a/Makefile b/Makefile index 84982cde..c1101f06 100644 --- a/Makefile +++ b/Makefile @@ -191,6 +191,9 @@ all: $(EXECUTABLE) $(DYNLIBRARY) test: all (cd tests; ./run) +luacheck: + luacheck --config .luacheckrc src + variants: $(LIBRARY_VARIANTS) build/%.o: src/%.cpp $(PACKAGE_DEPS) From b650bec077a335a54054f666573312636cc5904c Mon Sep 17 00:00:00 2001 From: Francois Perrad Date: Mon, 28 Jan 2019 17:22:51 +0100 Subject: [PATCH 2/6] unused variable --- src/genclangpaths.lua | 1 - src/terralib.lua | 2 -- 2 files changed, 3 deletions(-) diff --git a/src/genclangpaths.lua b/src/genclangpaths.lua index 8b6a9810..0d14c84c 100644 --- a/src/genclangpaths.lua +++ b/src/genclangpaths.lua @@ -1,6 +1,5 @@ --See Copyright Notice in ../LICENSE.txt --usage: genclangpaths.lua output /path/to/clang [addition args to parse] -local ffi = require("ffi") local outputfile,clang = unpack(arg) local handle = assert(io.popen(clang .. " -v src/dummy.c -o build/dummy.o 2>&1", "r")) local theline diff --git a/src/terralib.lua b/src/terralib.lua index 351238da..ba2008c4 100644 --- a/src/terralib.lua +++ b/src/terralib.lua @@ -1023,7 +1023,6 @@ end function terra.defineobjects(fmt,envfn,...) local cmds = terralib.newlist() - local nargs = 2 for i = 1, #fmt do --collect declaration/definition commands local c = fmt:sub(i,i) local name,tree = select(2*i - 1,...) @@ -3744,7 +3743,6 @@ function prettystring(toptree,breaklines) local function makeprectable(...) local lst = {...} - local sz = #lst local tbl = {} for i = 1,#lst,2 do tbl[lst[i]] = lst[i+1] From 4906832f2c449223cf63a734aa5c0bbb17d7a687 Mon Sep 17 00:00:00 2001 From: Francois Perrad Date: Mon, 28 Jan 2019 17:23:43 +0100 Subject: [PATCH 3/6] use variable --- src/terralib.lua | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/terralib.lua b/src/terralib.lua index ba2008c4..abc425c1 100644 --- a/src/terralib.lua +++ b/src/terralib.lua @@ -1944,7 +1944,7 @@ local function semanticcheck(diag,parameters,block) checkdeferredpassed(e,scopeposition,state.position) else assert(state.kind == "undefinedlabel") state.gotos:insert(e) - state.positions:insert(getscopeposition()) + state.positions:insert(position) end labelstates[label] = state elseif e:is "breakstat" then @@ -3405,7 +3405,7 @@ function terra.includecstring(code,cargs,target) assert(terra.istarget(target),"expected a target or nil to specify the native target") local result = terra.registercfile(target,code,args,headerprovider) local general,tagged,errors,macros = result.general,result.tagged,result.errors,result.macros - local mt = { __index = includetableindex, errors = result.errors } + local mt = { __index = includetableindex, errors = errors } local function addtogeneral(tbl) for k,v in pairs(tbl) do if not general[k] then From fe53d7fbe22bfcbc9ee6bf6627da3ff6428dc703 Mon Sep 17 00:00:00 2001 From: Francois Perrad Date: Mon, 28 Jan 2019 17:29:45 +0100 Subject: [PATCH 4/6] table.unpack comes with Lua 5.2 --- src/unpacklibraries.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/unpacklibraries.lua b/src/unpacklibraries.lua index ff884d22..b2c595c0 100644 --- a/src/unpacklibraries.lua +++ b/src/unpacklibraries.lua @@ -4,7 +4,7 @@ local function exe(cmd,...) cmd = string.format(cmd,...) local res = { os.execute(cmd) } if type(res[1]) == 'number' and res[1] ~= 0 or not res[1] then - print('Error during '..cmd..':', table.unpack(res)) + print('Error during '..cmd..':', unpack(res)) error("command failed: "..cmd) end end From 3253cdba5f01c7fa90c3ea8b50c6bc87dcab2942 Mon Sep 17 00:00:00 2001 From: Francois Perrad Date: Mon, 28 Jan 2019 17:04:12 +0100 Subject: [PATCH 5/6] unused loop variable --- src/asdl.lua | 20 +++++------ src/cudalib.lua | 2 +- src/geninternalizedfiles.lua | 2 +- src/terralib.lua | 66 ++++++++++++++++++------------------ 4 files changed, 45 insertions(+), 45 deletions(-) diff --git a/src/asdl.lua b/src/asdl.lua index 5c2c4b3f..e433ea68 100644 --- a/src/asdl.lua +++ b/src/asdl.lua @@ -28,7 +28,7 @@ function List:map(fn,...) return l end function List:insertall(elems) - for i,e in ipairs(elems) do + for _,e in ipairs(elems) do self:insert(e) end end @@ -150,9 +150,9 @@ local function parseAll(text) until not nextif("|") if nextif("attributes") then local attributes = parseFields() - for i,ctor in ipairs(sum.constructors) do + for _,ctor in ipairs(sum.constructors) do ctor.fields = ctor.fields or List() - for i,a in ipairs(attributes) do + for _,a in ipairs(attributes) do ctor.fields:insert(a) end end @@ -314,7 +314,7 @@ function Context:DefineClass(name,unique,fields) local names = List() local checks = List() local tns = List() - for i,f in ipairs(fields) do + for _,f in ipairs(fields) do names:insert(f.name) tns:insert(f.list and f.type.."*" or f.type) checks:insert(self:GetCheckForField(unique,f)) @@ -368,11 +368,11 @@ function Context:DefineClass(name,unique,fields) end function class:__tostring() local members = List() - for i,f in ipairs(fields) do + for _,f in ipairs(fields) do local v,r = self[f.name] if f.list then local elems = List() - for i,e in ipairs(self[f.name]) do + for _,e in ipairs(self[f.name]) do elems:insert(tostring(e)) end r = "{"..elems:concat(",").."}" @@ -410,18 +410,18 @@ end function Context:Define(text) local defs = parseAll(text) -- register all new type names - for i,d in ipairs(defs) do + for _,d in ipairs(defs) do self:DeclareClass(d.name) if d.type.kind == "sum" then - for i,c in ipairs(d.type.constructors) do + for _,c in ipairs(d.type.constructors) do self:DeclareClass(c.name) end end end - for i,d in ipairs(defs) do + for _,d in ipairs(defs) do if d.type.kind == "sum" then local parent = self:DefineClass(d.name,false,nil) - for i,c in ipairs(d.type.constructors) do + for _,c in ipairs(d.type.constructors) do local child = self:DefineClass(c.name,c.unique,c.fields) parent.members[child] = true --mark that any subclass is a member of its parent child.kind = basename(c.name) diff --git a/src/cudalib.lua b/src/cudalib.lua index 75ee97fc..446e1afb 100644 --- a/src/cudalib.lua +++ b/src/cudalib.lua @@ -58,7 +58,7 @@ function cudalib.toptx(module,dumpmodule,version) elseif type(v) == "table" and terra.isfunction(v.kernel) then -- annotated kernel addkernel(k,v.kernel) if v.annotations then - for i,a in pairs(v.annotations) do + for _,a in pairs(v.annotations) do annotations:insert({k,tostring(a[1]),tonumber(a[2])}) end end diff --git a/src/geninternalizedfiles.lua b/src/geninternalizedfiles.lua index 9a17df52..a4953058 100644 --- a/src/geninternalizedfiles.lua +++ b/src/geninternalizedfiles.lua @@ -55,7 +55,7 @@ local function EmitRegister(name,contents) table.insert(sizes,("\n%d"):format(#contents)) end -for i,entry in ipairs(listoffiles) do +for _,entry in ipairs(listoffiles) do local file = io.open(entry.path) local contents = file:read("*all") file:close() diff --git a/src/terralib.lua b/src/terralib.lua index abc425c1..e3c74ee3 100644 --- a/src/terralib.lua +++ b/src/terralib.lua @@ -251,7 +251,7 @@ local function copyobject(ref,newfields) -- copy an object, extracting any new r end end local r = handlefield(#fields) - for k,v in pairs(newfields) do + for k in pairs(newfields) do error("unused field in copy: "..tostring(k)) end return r @@ -501,7 +501,7 @@ local function readytocompile(root) end gv.type:completefunction() if gv.definition.kind == "functiondef" then - for i,g in ipairs(gv.definition.globalsused) do + for _,g in ipairs(gv.definition.globalsused) do visit(g) end end @@ -832,7 +832,7 @@ function terra.israwlist(l) elseif type(l) == "table" and not getmetatable(l) then local sz = #l local i = 0 - for k,v in pairs(l) do + for _ in pairs(l) do i = i + 1 end return i == sz --table only has integer keys and no other keys, we treat it as a list @@ -1206,7 +1206,7 @@ do local types = {} local defaultproperties = { "name", "tree", "undefined", "incomplete", "convertible", "cachedcstring", "llvm_definingfunction" } - for i,dp in ipairs(defaultproperties) do + for _,dp in ipairs(defaultproperties) do T.Type[dp] = false end T.Type.__index = nil -- force overrides @@ -1264,7 +1264,7 @@ do function T.Type:isunit() return types.unit == self end local applies_to_vectors = {"isprimitive","isintegral","isarithmetic","islogical", "canbeord"} - for i,n in ipairs(applies_to_vectors) do + for _,n in ipairs(applies_to_vectors) do T.Type[n.."orvector"] = function(self) return self[n](self) or (self:isvector() and self.type[n](self.type)) end @@ -1285,7 +1285,7 @@ do if self:isstruct() then parts:insert(":") local layout = self:getlayout() - for i,e in ipairs(layout.entries) do + for _,e in ipairs(layout.entries) do indent() parts:insert(tostring(e.key)..": ") print(e.type,d+1) @@ -1486,7 +1486,7 @@ do return elseif terra.israwlist(e) then local union = terra.newlist() - for i,se in ipairs(e) do checkentry(se,union) end + for _,se in ipairs(e) do checkentry(se,union) end results:insert(union) return end @@ -1494,7 +1494,7 @@ do erroratlocation(self.anchor,"expected either a field type pair (e.g. { field = , type = } or {,} ), or a list of valid entries representing a union") end local checkedentries = terra.newlist() - for i,e in ipairs(entries) do checkentry(e,checkedentries) end + for _,e in ipairs(entries) do checkentry(e,checkedentries) end return checkedentries end } @@ -1556,7 +1556,7 @@ do end end local function addentrylist(entries) - for i,e in ipairs(entries) do + for _,e in ipairs(entries) do if terra.islist(e) then beginunion() addentrylist(e) @@ -1577,7 +1577,7 @@ do end; } function T.functype:completefunction() - for i,p in ipairs(self.parameters) do p:complete() end + for _,p in ipairs(self.parameters) do p:complete() end self.returntype:complete() return self end @@ -1595,7 +1595,7 @@ do self.incomplete = nil --static initializers run only once --if one of the members of this struct recursively --calls complete on this type, then it will return before the static initializer has run - for i,e in ipairs(layout.entries) do + for _,e in ipairs(layout.entries) do e.type:complete() end if type(self.metamethods.__staticinitialize) == "function" then @@ -1801,7 +1801,7 @@ function evaltype(diag,env,typ) local v = evalluaexpression(env,typ) if terra.types.istype(v) then return v end if terra.israwlist(v) then - for i,t in ipairs(v) do + for _,t in ipairs(v) do if not terra.types.istype(t) then diag:reporterror(typ,"expected a type but found ",terra.type(v)) return terra.types.error @@ -1815,7 +1815,7 @@ end function evaluateparameterlist(diag, env, paramlist, requiretypes) local result = List() - for i,p in ipairs(paramlist) do + for _,p in ipairs(paramlist) do if p.kind == "unevaluatedparam" then if p.name.kind == "namedident" then local typ = p.type and evaltype(diag,env,p.type) @@ -1827,7 +1827,7 @@ function evaluateparameterlist(diag, env, paramlist, requiretypes) diag:reporterror(p,"expected a symbol or string but found nil") end local symlist = (terra.israwlist(value) and value) or List { value } - for i,entry in ipairs(symlist) do + for _,entry in ipairs(symlist) do if terra.issymbol(entry) then result:insert(newobject(p,T.concreteparam, entry.type, tostring(entry),entry,false)) else @@ -1839,7 +1839,7 @@ function evaluateparameterlist(diag, env, paramlist, requiretypes) result:insert(p) end end - for i,entry in ipairs(result) do + for _,entry in ipairs(result) do assert(entry.type == nil or terra.types.istype(entry.type)) if requiretypes and not entry.type then diag:reporterror(entry,"type must be specified for parameters and uninitialized variables") @@ -2233,7 +2233,7 @@ function typecheck(topexp,luaenv,simultaneousdefinitions) addcasts(typ) local errormsgs = terra.newlist() - for i,__cast in ipairs(cast_fns) do + for _,__cast in ipairs(cast_fns) do local quotedexp = terra.newquote(exp) local success,result = invokeuserfunction(exp, "invoking __cast", true,__cast,exp.type,typ,quotedexp) if success then @@ -2249,7 +2249,7 @@ function typecheck(topexp,luaenv,simultaneousdefinitions) if not speculative then diag:reporterror(exp,"invalid conversion from ",exp.type," to ",typ) - for i,e in ipairs(errormsgs) do + for _,e in ipairs(errormsgs) do diag:reporterror(exp,"user-defined cast failed: ",e) end end @@ -2520,7 +2520,7 @@ function typecheck(topexp,luaenv,simultaneousdefinitions) local operands = ee.operands:map(checkexp) local overloads = terra.newlist() - for i,e in ipairs(operands) do + for _,e in ipairs(operands) do if e.type:isstruct() then local overloadmethod = (#operands == 1 and unaryoverloadmethod) or genericoverloadmethod local overload = e.type.metamethods[overloadmethod] --TODO: be more intelligent here about merging overloaded functions so that all possibilities are considered @@ -2552,7 +2552,7 @@ function typecheck(topexp,luaenv,simultaneousdefinitions) local function checkexpressions(expressions,location) local nes = terra.newlist() - for i,e in ipairs(expressions) do + for _,e in ipairs(expressions) do local ne = checkexp(e,location) if ne:is "letin" and not ne.hasstatements then nes:insertall(ne.expressions) @@ -2745,7 +2745,7 @@ function typecheck(topexp,luaenv,simultaneousdefinitions) --macro and record it as themacro local terrafunctions = terra.newlist() local themacro = nil - for i,fn in ipairs(fnlikelist) do + for _,fn in ipairs(fnlikelist) do if fn:is "luaobject" then if terra.ismacro(fn.value) then themacro = fn.value @@ -2760,7 +2760,7 @@ function typecheck(topexp,luaenv,simultaneousdefinitions) if #fn.value:getdefinitions() == 0 then diag:reporterror(anchor,"attempting to call overloaded function without definitions") end - for i,v in ipairs(fn.value:getdefinitions()) do + for _,v in ipairs(fn.value:getdefinitions()) do local fnlit = createfunctionreference(anchor,v) if fnlit.type ~= terra.types.error then terrafunctions:insert( fnlit ) @@ -2790,7 +2790,7 @@ function typecheck(topexp,luaenv,simultaneousdefinitions) if not fntyp.isvararg then return fntyp.parameters end local vatypes = terra.newlist() vatypes:insertall(fntyp.parameters) - for i = 1,#paramlist - #fntyp.parameters do + for _ = 1,#paramlist - #fntyp.parameters do vatypes:insert("vararg") end return vatypes @@ -2948,7 +2948,7 @@ function typecheck(topexp,luaenv,simultaneousdefinitions) --figure out what type this vector has typ = entries[1].type - for i,e2 in ipairs(entries) do + for _,e2 in ipairs(entries) do typ = typemeet(e,typ,e2.type) end end @@ -2994,7 +2994,7 @@ function typecheck(topexp,luaenv,simultaneousdefinitions) elseif e:is "constructoru" then local paramlist = terra.newlist() local named = 0 - for i,f in ipairs(e.records) do + for _,f in ipairs(e.records) do local value = checkexp(f.value) named = named + (f.key and 1 or 0) if not f.key and value:is "letin" and not value.hasstatements then @@ -3063,7 +3063,7 @@ function typecheck(topexp,luaenv,simultaneousdefinitions) local function checkformalparameterlist(paramlist, requiretypes) local evalparams = evaluateparameterlist(diag,env:combinedenv(),paramlist,requiretypes) local result = List() - for i,p in ipairs(evalparams) do + for _,p in ipairs(evalparams) do if p.isnamed then local lenv = env:localenv() if rawget(lenv,p.name) then @@ -3637,7 +3637,7 @@ function prettystring(toptree,breaklines) local implicitblock = { repeatstat = true, fornum = true, fornumu = true} local emitStmt, emitExp,emitParamList,emitLetIn local function emitStmtList(lst) --nested Blocks (e.g. from quotes need "do" appended) - for i,ss in ipairs(lst) do + for _,ss in ipairs(lst) do if ss:is "block" and not (#ss.statements == 1 and implicitblock[ss.statements[1].kind]) then begin(ss,"do\n") emitStmt(ss) @@ -4252,7 +4252,7 @@ function terra.importlanguage(languages,entrypoints,langstring) if not lang[field] then error(field .. " expected to be list of "..typ) end - for i,k in ipairs(lang[field]) do + for _,k in ipairs(lang[field]) do if type(k) ~= typ then error(field .. " expected to be list of "..typ.." but found "..type(k)) end @@ -4261,7 +4261,7 @@ function terra.importlanguage(languages,entrypoints,langstring) haslist("keywords","string") haslist("entrypoints","string") - for i,e in ipairs(lang.entrypoints) do + for _,e in ipairs(lang.entrypoints) do if entrypoints[e] then error(("language '%s' uses entrypoint '%s' already defined by language '%s'"):format(lang.name,e,entrypoints[e].name),-1) end @@ -4269,19 +4269,19 @@ function terra.importlanguage(languages,entrypoints,langstring) end if not lang.keywordtable then lang.keywordtable = {} --keyword => true - for i,k in ipairs(lang.keywords) do + for _,k in ipairs(lang.keywords) do lang.keywordtable[k] = true end - for i,k in ipairs(lang.entrypoints) do + for _,k in ipairs(lang.entrypoints) do lang.keywordtable[k] = true end end table.insert(languages,lang) end function terra.unimportlanguages(languages,N,entrypoints) - for i = 1,N do + for _ = 1,N do local lang = table.remove(languages) - for i,e in ipairs(lang.entrypoints) do + for _,e in ipairs(lang.entrypoints) do entrypoints[e] = nil end end @@ -4295,7 +4295,7 @@ do local special = { "name", "string", "number", "eof", "default" } --note: default is not a tokentype but can be used in libraries to match --a token that is not another type - for i,k in ipairs(special) do + for _,k in ipairs(special) do local name = "<" .. k .. ">" local tbl = setmetatable({ name = name }, terra.languageextension.tokentype ) From 8b75de0d6c318255c546a62f4ab67ee1dce4e511 Mon Sep 17 00:00:00 2001 From: Francois Perrad Date: Mon, 28 Jan 2019 18:49:09 +0100 Subject: [PATCH 6/6] inconsistent indentation (SPACE followed by TAB) --- .luacheckrc | 1 - src/terralib.lua | 6 +++--- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/.luacheckrc b/.luacheckrc index b2ee724c..cc748aab 100644 --- a/.luacheckrc +++ b/.luacheckrc @@ -10,7 +10,6 @@ ignore = { '612', -- line contains trailing whitespace '613', -- trailing whitespace in a string '614', -- trailing whitespace in a comment - '621', -- inconsistent indentation (SPACE followed by TAB) } globals = { diff --git a/src/terralib.lua b/src/terralib.lua index e3c74ee3..668b4229 100644 --- a/src/terralib.lua +++ b/src/terralib.lua @@ -3391,8 +3391,8 @@ function terra.includecstring(code,cargs,target) args:insert(clangresourcedirectory.."/include") end for _,path in ipairs(terra.systemincludes) do - args:insert("-internal-isystem") - args:insert(path) + args:insert("-internal-isystem") + args:insert(path) end if cargs then @@ -4059,7 +4059,7 @@ terra.systemincludes = List() if ffi.os == "Windows" then -- this is the reason we can't have nice things local function registrystring(key,value,default) - local F = io.popen( ([[reg query "%s" /v "%s"]]):format(key,value) ) + local F = io.popen( ([[reg query "%s" /v "%s"]]):format(key,value) ) local result = F and F:read("*all"):match("REG_SZ%W*([^\n]*)\n") return result or default end