Skip to content

Commit 0343d4b

Browse files
committed
support rm code coverage effect
1 parent 6d6493a commit 0343d4b

File tree

2 files changed

+62
-5
lines changed

2 files changed

+62
-5
lines changed

src/utils.jl

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -79,34 +79,62 @@ macro test_codeinfo(ci, ex)
7979
esc(test_codeinfo_m(ci, ex))
8080
end
8181

82+
function rm_code_coverage_effect(ci::CodeInfo)
83+
# make sure we don't change original one
84+
new = NewCodeInfo(copy(ci))
85+
for (v, stmt) in new
86+
if Meta.isexpr(stmt, :code_coverage_effect)
87+
delete!(new, v)
88+
end
89+
end
90+
new_ci = finish(new)
91+
92+
if ci.ssavaluetypes isa Vector
93+
types = []
94+
for (v, stmt) in enumerate(ci.code)
95+
if !Meta.isexpr(stmt, :code_coverage_effect)
96+
push!(types, ci.ssavaluetypes[v])
97+
end
98+
end
99+
100+
new_ci.ssavaluetypes = types
101+
new_ci.inferred = ci.inferred
102+
end
103+
return new_ci
104+
end
105+
82106
function test_codeinfo_m(ci, ex::Expr)
83107
Meta.isexpr(ex, :block) || error("expect a begin ... end")
84-
ret = Expr(:block)
108+
@gensym nocodedov_ci
109+
110+
ret = quote
111+
$nocodedov_ci = $CompilerPluginTools.rm_code_coverage_effect($ci)
112+
end
85113
stmt_count = 1
86114
for each in ex.args
87115
@switch each begin
88116
@case :($stmt::$type)
89117
push!(ret.args, quote
90-
@test $MLStyle.@match $ci.code[$stmt_count] begin
118+
@test $MLStyle.@match $nocodedov_ci.code[$stmt_count] begin
91119
$stmt => true
92120
_ => false
93121
end
94122
end)
95123
push!(ret.args, quote
96-
@test $ci.ssavaluetypes[$stmt_count] == $type
124+
@test $nocodedov_ci.ssavaluetypes[$stmt_count] == $type
97125
end)
98126
stmt_count += 1
99127
@case ::LineNumberNode
100128
continue
101129
@case _
102130
push!(ret.args, quote
103-
@test $MLStyle.@match $ci.code[$stmt_count] begin
131+
@test $MLStyle.@match $nocodedov_ci.code[$stmt_count] begin
104132
$each => true
105133
_ => false
106134
end
107135
end)
108136
stmt_count += 1
109-
end
137+
end
110138
end
111139
return ret
112140
end

test/codeinfo.jl

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,3 +127,32 @@ end
127127
@test test_ci.code[9] == :(1 + $(SSAValue(8)))
128128
end
129129
end
130+
131+
@testset "rm_code_coverage_effect" begin
132+
ci = @make_codeinfo begin
133+
#=%1 =# Expr(:code_coverage_effect)::Nothing
134+
#=%2 =# QuoteNode(1.0)::Float64
135+
#=%3 =# Expr(:call, sin, SSAValue(2))::Float64
136+
#=%4 =# Expr(:code_coverage_effect)::Nothing
137+
#=%5 =# QuoteNode(2.0)::Float64
138+
#=%6 =# Expr(:call, sin, SSAValue(5))::Float64
139+
#=%7 =# Expr(:code_coverage_effect)::Nothing
140+
#=%8 =# Expr(:call, <, SSAValue(6), QuoteNode(1.0))::Bool
141+
#=%9 =# GotoIfNot(SSAValue(8), 10)
142+
#=%10=# Expr(:call, +, SSAValue(3), SSAValue(6))::Float64
143+
#=%11=# ReturnNode(SSAValue(10))::Float64
144+
end
145+
146+
test_ci = CompilerPluginTools.rm_code_coverage_effect(ci)
147+
148+
@test_codeinfo test_ci begin
149+
#=%1=# QuoteNode(1.0)::Float64
150+
#=%2=# Expr(:call, sin, SSAValue(1))::Float64
151+
#=%3=# QuoteNode(2.0)::Float64
152+
#=%4=# Expr(:call, sin, SSAValue(3))::Float64
153+
#=%5=# Expr(:call, <, SSAValue(4), QuoteNode(1.0))::Bool
154+
#=%6=# GotoIfNot(SSAValue(5), 7)
155+
#=%7=# Expr(:call, +, SSAValue(2), SSAValue(4))::Float64
156+
#=%8=# ReturnNode(SSAValue(7))::Float64
157+
end
158+
end

0 commit comments

Comments
 (0)