medfall

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs

commit b8733d61093a2963bf91694197bdfc225fb06bd5
parent 8e369caeb240febddc789fc1dca7b4f7c48a4a70
Author: Michael Savage <mikejsavage@gmail.com>
Date:   Sun Jan 31 11:55:05 +0000

Add leakcheck.lua

Diffstat:
analysis/leakcheck.lua | 68++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 68 insertions(+), 0 deletions(-)
diff --git a/analysis/leakcheck.lua b/analysis/leakcheck.lua @@ -0,0 +1,68 @@ +#! /usr/bin/lua + +local lpeg = require( "lpeg" ) +local json = require( "cjson.safe" ) + +-- b_ = begin +-- e_ = end +-- ne_ = not end +-- slc = single line comment +-- mlc = multiline comment +local b_slc = lpeg.P( "//" ) +local e_slc = lpeg.P( "\n" ) +local ne_slc = ( 1 - e_slc ) ^ 0 +local slc = b_slc * ne_slc + +local b_mlc = lpeg.P( "/*" ) +local e_mlc = lpeg.P( "*/" ) +local ne_mlc = ( 1 - e_mlc ) ^ 0 +local mlc = b_mlc * ne_mlc * e_mlc + +local quote = lpeg.P( "\"" ) +local escaped = lpeg.P( "\\" ) * 1 +local string = quote * ( escaped + ( 1 - quote ) ) ^ 0 * quote + +local comments = slc + mlc +local none = 1 - ( string + comments ) + +local just_code = ( comments + string + lpeg.C( none ^ 1 ) ) ^ 0 + +local f = io.open( arg[ 1 ] ) +local contents = f:read( "*all" ) +local stripped = table.concat( { just_code:match( contents ) } ) + +local function check_leaks( leaks, levels, depth, body ) + for code, block in ( body .. "{}" ):gmatch( "(.-)(%b{})" ) do + if code == "" and block == "{}" then + break + end + + for line in code:gmatch( "([^\n]+)" ) do + if line:find( "memarena_push" ) then + if not levels[ depth ] then + table.insert( leaks, line ) + end + end + + if line:find( "MEMARENA_SCOPED_CHECKPOINT" ) then + levels[ depth ] = true + end + end + + if not cp then + levels[ depth + 1 ] = false + check_leaks( leaks, levels, depth + 1, block:sub( 2, -2 ) ) + levels[ depth + 1 ] = nil + end + end +end + +for ret, name, args, body in stripped:gmatch( "([^\n]-)%s*([^%s]+)(%b())\n? *(%b{})" ) do + local leaks = { } + check_leaks( leaks, { false }, 1, body ) + + if #leaks > 0 then + print( name ) + print( table.concat( leaks, "\n" ) ) + end +end