-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmicrocoverage.jl
477 lines (416 loc) · 16.4 KB
/
microcoverage.jl
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
module microcoverage
## Utility function to construct an Expr from head and args.
function mkex(head::Symbol, args...)
v = Expr(head)
for arg in args
push!(v.args, arg)
end
v
end
## We annotate the source file with a sequence of ints and strings.
typealias AnnotationIndexType Array{Union(Int,ASCIIString), 1}
## Constructor for an annotation index.
initannotationindex() = (Union(Int,ASCIIString))[]
# This dictionary maps source file names to their
# annotation indices.
const annotationindexdict = Dict{ASCIIString, AnnotationIndexType}()
# Trackarray holds the counters that track the line
# numbers and calls.
const trackarray = (Int)[]
## incrtrackarray increments the appropriate entry of the
## track array
function incrtrackarray(subscr)
global trackarray::Array{Int,1}
trackarray[subscr] += 1
nothing
end
## makecallinst: Generate an expression that corresponds to the
## statement
## Main.microcoverage.incrtrackarray(trknum)
makecallinst(trknum::Int) =
mkex(:call,
mkex(:.,
mkex(:.,
:Main,
QuoteNode(:microcoverage)),
QuoteNode(:incrtrackarray)),
trknum)
## coverage_rewrite_recursive
## Take an expression inprogram (could be a whole program),
## the name of the tracking array, and an initial position
## in the tracking array.
##
## Produce an outprogram, which is the rewritten inprogram
## with calls to the coverage-statement incrementers.
## Also return the updated trknum (position in the tracking array).
## As a side effect, generate annotationindex, which tracks what should
## be printed in the output after coverage checking is complete.
##
## There are three versions of routine; the correct one is
## selected by multiple dispatch on the first argument.
function coverage_rewrite_recursive(inprogram::LineNumberNode,
starttrknum::Int,
linenumberoffset::Int,
annotationindex::AnnotationIndexType)
# If the argument is of type LineNumberNode, i.e.,
# a new line number in the source code,
# replace it with a block that consists of the
# the line number and a tracking array incrementer
trknum = starttrknum
newline = inprogram.line + linenumberoffset
push!(annotationindex, -newline, trknum)
outprogram = mkex(:block,
deepcopy(inprogram),
makecallinst(trknum))
trknum += 1
return outprogram, trknum
end
function coverage_rewrite_recursive(inprogram::Any,
starttrknum::Int,
linenumberoffset::Int,
::AnnotationIndexType)
## This is the default version of coverage_rewrite_recursive
## that doesn't do anything
return inprogram, starttrknum
end
function coverage_rewrite_recursive(inprogram::Expr,
starttrknum::Int,
linenumberoffset::Int,
annotationindex::AnnotationIndexType)
## This is the primary version of coverage_rewrite
## recursive. It takes an expression and inserts
## tracking statements for line numbers and internal branches
## in expressions.
trknum = starttrknum
if inprogram.head == :line
# If the expression is an expression of type :line, i.e.,
# a new line number in the source code,
# replace it with a block that consists of the
# the line number and a tracking array incrementer
newline = inprogram.args[1] + linenumberoffset
push!(annotationindex, -newline, trknum)
outprogram = mkex(:block,
deepcopy(inprogram),
makecallinst(trknum))
trknum += 1
elseif inprogram.head == :if && (!(typeof(inprogram.args[2]) <: Expr) ||
inprogram.args[2].head != :block)
# If the expression is of the form a? b : c
# then generate tracking statements for b and c
outprogram = Expr(:if)
outprogram1, trknum = coverage_rewrite_recursive(inprogram.args[1],
trknum,
linenumberoffset,
annotationindex)
push!(outprogram.args, outprogram1)
push!(annotationindex, " ? ")
@assert(length(inprogram.args) == 3)
for k = 2 : 3
if k > 2
push!(annotationindex, " : ")
end
a2 = inprogram.args[k]
push!(annotationindex, "(", trknum)
callinst = makecallinst(trknum)
trknum += 1
outprogram1, trknum = coverage_rewrite_recursive(a2,
trknum,
linenumberoffset,
annotationindex)
push!(outprogram.args, mkex(:block,
callinst,
outprogram1))
push!(annotationindex, ")")
end
elseif inprogram.head == :(=) && typeof(inprogram.args[1]) <: Expr &&
inprogram.args[1].head == :call && inprogram.args[1].args[1] != :eval
# If the line is a statement-function definition other than the
# definition of "eval", then insert
# a tracking statement into the function body.
@assert length(inprogram.args) == 2
outprogram1, trknum = coverage_rewrite_recursive(inprogram.args[1],
trknum,
linenumberoffset,
annotationindex)
savetrknum = trknum
callinst = makecallinst(trknum)
trknum += 1
outprogram2, trknum = coverage_rewrite_recursive(inprogram.args[2],
trknum,
linenumberoffset,
annotationindex)
push!(annotationindex, "(called", savetrknum, "time(s))")
outprogram = mkex(:(=), outprogram1, mkex(:block,
callinst,
outprogram2))
elseif inprogram.head == :|| || inprogram.head == :&&
## If the expression is the || or && operator, generate
## a tracking statement for each branch.
@assert length(inprogram.args) == 2
outprogram = Expr(inprogram.head)
for k = 1 : 2
callinst = makecallinst(trknum)
push!(annotationindex, "(", trknum)
trknum += 1
outprogram1, trknum = coverage_rewrite_recursive(inprogram.args[k],
trknum,
linenumberoffset,
annotationindex)
push!(outprogram.args, mkex(:block,
callinst,
outprogram1))
push!(annotationindex, ")")
if k == 1
if inprogram.head == :||
push!(annotationindex, " || ")
else
push!(annotationindex, " && ")
end
end
end
elseif inprogram.head == :global || inprogram.head == :import ||
inprogram.head == :importall || inprogram.head == :export ||
inprogram.head == :typealias || inprogram.head == :abstract ||
inprogram.head == :using
outprogram = inprogram
elseif inprogram.head == :immutable || inprogram.head == :type
outprogram = Expr(inprogram.head)
for expr1 in inprogram.args
if typeof(expr1) <: Expr &&
(expr1.head == :(=) || expr1.head == :function)
outprogram1, trknum = coverage_rewrite_recursive(expr1,
trknum,
linenumberoffset,
annotationindex)
push!(outprogram.args, outprogram1)
else
push!(outprogram.args, expr1)
end
end
else
## For all other expression types, just make the output same as
## the input (with recursive calls)
outprogram = Expr(inprogram.head)
for expr1 in inprogram.args
outprogram1, trknum = coverage_rewrite_recursive(expr1,
trknum,
linenumberoffset,
annotationindex)
push!(outprogram.args, outprogram1)
end
end
outprogram, trknum
end
function linecount(string)
count = 0
pos = 1
while true
a = search(string, '\n', pos)
a == 0 && break
count += 1
pos = a + 1
end
count
end
filepreamble = "# Automatically generated by microcoverage.jl-- will be automatically deleted upon completion"
## This function takes a sourcefile name. It renames
## it to <oldname>.orig. It parses the original file
## and inserts tracking statements.
## Then it generates a new
## sourcefile with the same name as the old. The new
## file eval's the parsed version of the old file with
## tracking statements.
function begintrack(sourcefilename::ASCIIString)
println("reading $sourcefilename")
src = ""
open(sourcefilename, "r") do h
src = convert(ASCIIString, readbytes(h))
end
annotationindex = initannotationindex()
global trackarray::Array{Int,1}
lasttrknum = length(trackarray)
initsize = lasttrknum
srcpos = 1
src_parse_rewrite = (Expr)[]
println("parsing")
linenumberoffset = 0
while srcpos <= length(src)
if isspace(src[srcpos])
if src[srcpos] == '\n'
linenumberoffset += 1
end
srcpos += 1
elseif src[srcpos] == '#'
eolpos = search(src, '\n', srcpos)
srcpos = eolpos + 1
linenumberoffset += 1
else
src_parse, srcposfinal = parse(src, srcpos)
rewrite1,lasttrknum = coverage_rewrite_recursive(src_parse,
lasttrknum + 1,
linenumberoffset,
annotationindex)
linenumberoffset += linecount(src[srcpos : srcposfinal - 1])
srcpos = srcposfinal
push!(src_parse_rewrite, rewrite1)
end
end
resize!(trackarray, lasttrknum)
for j = initsize + 1 : lasttrknum
trackarray[j] = 0
end
global annotationindexdict::Dict{ASCIIString,AnnotationIndexType}
annotationindexdict[sourcefilename] = annotationindex
renamed = sourcefilename * ".orig"
if stat(renamed).size > 0
error("Cannot rename original -- file already exists with the name $renamed")
end
println("renaming $sourcefilename to $renamed")
mv(sourcefilename, renamed)
println("saving machine-generated code in $sourcefilename")
open(sourcefilename,"w") do h2
global filepreamble::ASCIIString
println(h2, filepreamble)
for rewrite in src_parse_rewrite
ss = IOBuffer()
serialize(ss, rewrite)
ser_rewrite = takebuf_array(ss)
numbyte = length(ser_rewrite)
println(h2, "eval(deserialize(IOBuffer((UInt8)[")
for count = 1 : numbyte
byte = ser_rewrite[count]
show(h2, byte)
count < numbyte && print(h2, ", ")
count % 8 == 0 && println(h2)
end
println(h2, "])))")
end
end
end
# Small routine to make an ASCIIString consisting of i spaces
spaces(i::Int) = convert(ASCIIString, 32*ones(UInt8,i))
## The next four functions are handlers for items in annotationindex.
## This first one handles an integer-- dispatches on whether it is positive
## or negative
function printmcov(item::Int,
lastprint::Int,
curcol::Int,
newline::Int,
horig::IO,
hcov::IO)
if item < 0
printmcovli(-item, lastprint,curcol,newline,horig,hcov)
else
printmcovtn(item, lastprint,curcol,newline,horig,hcov)
end
end
## Prints a line number and source line and advances the file
## to that line number, printing more source lines as necessary.
function printmcovli(lineno::Int,
lastprint::Int,
curcol::Int,
newline::Int,
horig::IO,
hcov::IO)
oldnewline = newline
newline = lineno
if newline < lastprint
error("Line numbers out of order in tracking info")
end
for count = lastprint + 1 : newline - 1
s = chomp(readline(horig))
if curcol > 16
println(hcov)
curcol = 0
end
println(hcov, spaces(16-curcol), "* ", s)
curcol = 0
end
lastprint = newline - 1
if newline != oldnewline
nls = "$newline"
print(hcov, "L", nls, spaces(8 - length(nls)))
curcol += 9
end
lastprint, curcol, newline
end
## Print a tracking number
function printmcovtn(tracknum::Int,
lastprint::Int,
curcol::Int,
newline::Int,
::IO,
hcov::IO)
global trackarray::Array{Int,1}
cov = trackarray[tracknum]
cs = " $cov "
print(hcov, cs)
curcol += length(cs)
lastprint, curcol, newline
end
## Print a string
function printmcov(outstring::ASCIIString,
lastprint::Int,
curcol::Int,
newline::Int,
::IO,
hcov::IO)
print(hcov, outstring)
curcol += length(outstring)
lastprint, curcol, newline
end
## endtrack is called when the trackign is finished
## It produces a coverage report in a file called <origfilename>.mcov.
## Then it renames the files back to how they were before
## begintrack was called.
function endtrack(sourcefilename::ASCIIString)
renamed = sourcefilename * ".orig"
covfilename = sourcefilename * ".mcov"
open(renamed, "r") do horig
open(covfilename, "w") do hcov
println("Writing coverage information to $covfilename")
global annotationindexdict::Dict{ASCIIString,AnnotationIndexType}
lastprint = 0
curcol = 0
newline = -1
for item in annotationindexdict[sourcefilename]
lastprint, curcol, newline = printmcov(item,
lastprint,
curcol,
newline,
horig,
hcov)
end
if curcol > 16
curcol = 0
println(hcov)
end
while !eof(horig)
s = chomp(readline(horig))
println(hcov, spaces(16-curcol), "* ", s)
curcol = 0
end
end
end
restore(sourcefilename)
end
function restore(sourcefilename::ASCIIString)
renamed = sourcefilename * ".orig"
if stat(renamed).size == 0
error("No file called $renamed")
end
s = ""
open(sourcefilename, "r") do hrewr
s = chomp(readline(hrewr))
end
global filepreamble::ASCIIString
if s != filepreamble
error("Cannot overwrite $sourcefilename; missing preamble statement")
end
println("renaming $renamed to $sourcefilename; machine-generated $sourcefilename overwritten")
mv(renamed, sourcefilename, remove_destination=true)
end
export restore
export begintrack
export endtrack
end