Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

statically analysing Lua code for potential errors

I'm using a closed-source application that loads Lua scripts and allows some customization through modifying these scripts. Unfortunately that application is not very good at generating useful log output (all I get is 'script failed') if something goes wrong in one of the Lua scripts.

I realize that dynamic languages are pretty much resistant to static code analysis in the way C++ code can be analyzed for example.

I was hoping though, there would be a tool that runs through a Lua script and e.g. warns about variables that have not been defined in the context of a particular script.

Essentially what I'm looking for is a tool that for a script:

local a
print b

would output:

warning: script.lua(1): local 'a' is not used'
warning: script.lua(2): 'b' may not be defined'

It can only really be warnings for most things but that would still be useful! Does such a tool exist? Or maybe a Lua IDE with a feature like that build in?

Thanks, Chris

like image 212
Chris Avatar asked May 14 '09 22:05

Chris


People also ask

How can you assess the quality of code using static analysis?

Once the code is written, a static code analyzer should be run to look over the code. It will check against defined coding rules from standards or custom predefined rules. Once the code is run through the static code analyzer, the analyzer will have identified whether or not the code complies with the set rules.

What is static code analysis in Devops?

A static code analysis tool inspects your codebase through the development cycle, and it's able to identify bugs, vulnerabilities, and compliance issues without actually running the program. The code analysis may help to ensure that your software is secure, reliable, and compliant.

Which of the following is a type of CC static code analysis tool?

Helix QAC is an excellent static analysis testing tool for C and C++ code from Perforce (formerly PRQA). The tool comes with a single installer and supports platforms like Windows 7, Linex Rhel 5 and Solaris 10.


2 Answers

Automated static code analysis for Lua is not an easy task in general. However, for a limited set of practical problems it is quite doable.

Quick googling for "lua lint" yields these two tools: lua-checker and Lua lint.

You may want to roll your own tool for your specific needs however.

Metalua is one of the most powerful tools for static Lua code analysis. For example, please see metalint, the tool for global variable usage analysis.

Please do not hesitate to post your question on Metalua mailing list. People there are usually very helpful.

like image 161
Alexander Gladysh Avatar answered Oct 30 '22 13:10

Alexander Gladysh


There is also lua-inspect, which is based on metalua that was already mentioned. I've integrated it into ZeroBrane Studio IDE, which generates an output very similar to what you'd expect. See this SO answer for details: https://stackoverflow.com/a/11789348/1442917.

like image 44
Paul Kulchenko Avatar answered Oct 30 '22 14:10

Paul Kulchenko