lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


Ivan Kolev wrote:
Hi,

I'm trying to use a line hook and the "activelines" table returned by "debug.getinfo( f, 'L' )" to calculate coverage of functions in unit tests. This table contains most (or maybe all) of the source lines which contain just the keyword 'end'. This implies that there are some VM instructions associated with the end keyword (I know almost nothing about the VM), which is OK. The problem is that whenever there's a 'return' or 'break' statement before 'end', the 'end'-code (or maybe just the line hook) never gets called, which results in many 'end'-only lines falsely appearing as "non-covered".

The 'end' statement is never executed in this case. You can see the
reason by viewing the compiled code with luac -l

Lua doesn't do dead code elimination, and there are a few cases
where dead code will be generated by the compiler. For example,
a function is always terminated with an OP_RETURN, even if the
last statement in the function is a return statement. The OP_RETURN
carries the line number of the 'end' statement, since that's where
it's generated.

You'd get similar issues with constructs like this, I believe:

while true do
  -- something
  break
end

And there are probably other cases. Short of implementing a dead-code
optimization in the code generator, I'm not sure how this code be
addressed.

I can see two workarounds - one is to modify the source code so that any 'end's after return/break are placed on the same line as the return/break; the other is to have the unit testing framework scan the source files and ignore the non-covered lines which contain 'end' only. I don't like both of them (especially the first). Is there something better that can be done to avoid this problem?

Personally, I'd go for the second option :)