-
Notifications
You must be signed in to change notification settings - Fork 11
/
covanalysis
executable file
·127 lines (108 loc) · 4.14 KB
/
covanalysis
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#!/usr/bin/env terra
-- The MIT License (MIT)
--
-- Copyright (c) 2015 Stanford University.
-- All rights reserved.
--
-- Permission is hereby granted, free of charge, to any person obtaining a
-- copy of this software and associated documentation files (the "Software"),
-- to deal in the Software without restriction, including without limitation
-- the rights to use, copy, modify, merge, publish, distribute, sublicense,
-- and/or sell copies of the Software, and to permit persons to whom the
-- Software is furnished to do so, subject to the following conditions:
--
-- The above copyright notice and this permission notice shall be included
-- in all copies or substantial portions of the Software.
--
-- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
-- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
-- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
-- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
-- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
-- FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
-- DEALINGS IN THE SOFTWARE.
local ffi = require 'ffi'
package.terrapath = package.terrapath .. ";include/?.t"
local PN = require 'ebb.lib.pathname'
local SCAN_DIR = PN.Pathname.new 'include'
SCAN_DIR = tostring(SCAN_DIR:abspath():cleanpath())
local ANALYSIS_DIR = PN.Pathname.new 'coverage_src'
ANALYSIS_DIR = tostring(ANALYSIS_DIR:abspath():cleanpath())
if ffi.os == 'Windows' then
print('TODO windows')
end
local lscmd = "find "..SCAN_DIR
-- load info
local covinfo = loadfile("coverageinfo.lua")()
-- rewrite the keys for covinfo
local copytbl = covinfo
covinfo = {}
for path,lines in pairs(copytbl) do
path = PN.Pathname.new(path:sub(2)):abspath():cleanpath():tostring()
covinfo[path] = lines
end
local function genReport(filename, lineinfo)
local srcfilename = filename
if filename:sub(1,#SCAN_DIR) ~= SCAN_DIR then error('bad prefix dir') end
local suffix = filename:sub(#SCAN_DIR + 1)
local dstfilename = ANALYSIS_DIR..suffix
-- use this line instead of rest of function to debug changes to
-- filepath mangling without accidentally damaging files on disk
--print('from/to', srcfilename..'\n', dstfilename)
-- make sure destination directory exists
assert(PN.Pathname.new(dstfilename):dirpath():mkpath(),
'failed to make path')
local srcfile = io.open(srcfilename, 'r')
local dstfile = io.open(dstfilename, 'w')
local linenum = 1
for line in srcfile:lines() do
local nvisits = lineinfo[linenum]
linenum = linenum + 1
-- If we visited this line, then output count
if nvisits then
dstfile:write(string.format("%5d: ",nvisits))
-- Filter out lines we couldn't possibly visit
elseif
string.match(line,"^ *%-%-") or -- comments
string.match(line,"^ *$") or -- blank lines
-- token only lines
string.match(line,"^ *end *$") or
string.match(line,"^ *} *$") or
string.match(line,"^ *then *$") or
string.match(line,"^ *else *$") or
string.match(line,"^ *local function") or
-- tokens with comments
string.match(line,"^ *end *%-%-$") or
string.match(line,"^ *} *%-%-") or
string.match(line,"^ *then *%-%-") or
string.match(line,"^ *else *%-%-")
-- random other lines
--string.match(line,"^ *end%) *$")
then
dstfile:write(" : ")
else
dstfile:write("#####: ")
end
dstfile:write(line)
dstfile:write("\n")
end
srcfile:close()
dstfile:close()
end
-- Scan all the source files
for line in io.popen(lscmd):lines() do
if ffi.os == "Windows" then error('TODO windows') end
local filename = line
-- filter out some files
if filename and filename:match('coverage%.t') then filename = nil end
if filename and filename:sub(-2) ~= '.t' then filename = nil end
if filename then
local lookupname = filename --'@./'..filename
local lineinfo = covinfo[lookupname]
if lineinfo then
genReport(filename, lineinfo)
else
print('NO COVERAGE INFO FOR '..filename)
end
end
end