Skip to content

Commit

Permalink
fix code cov for specific path
Browse files Browse the repository at this point in the history
  • Loading branch information
IanButterworth committed Mar 16, 2022
1 parent ac1d693 commit 82a316e
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 10 deletions.
11 changes: 7 additions & 4 deletions base/compiler/optimize.jl
Original file line number Diff line number Diff line change
Expand Up @@ -552,21 +552,22 @@ end

function convert_to_ircode(ci::CodeInfo, sv::OptimizationState)
code = copy_exprargs(ci.code)
coverage = coverage_enabled(sv.mod)
mod_coverage = coverage_enabled(sv.mod)
# Go through and add an unreachable node after every
# Union{} call. Then reindex labels.
idx = 1
oldidx = 1
changemap = fill(0, length(code))
labelmap = coverage ? fill(0, length(code)) : changemap
labelmap = fill(0, length(code))
prevloc = zero(eltype(ci.codelocs))
stmtinfo = sv.stmt_info
codelocs = ci.codelocs
ssavaluetypes = ci.ssavaluetypes::Vector{Any}
ssaflags = ci.ssaflags
while idx <= length(code)
codeloc = codelocs[idx]
if coverage && codeloc != prevloc && codeloc != 0
file_coverage = mod_coverage || (codeloc != 0 && is_file_tracked(ci.linetable[codeloc].file))
if file_coverage && codeloc != prevloc && codeloc != 0
# insert a side-effect instruction before the current instruction in the same basic block
insert!(code, idx, Expr(:code_coverage_effect))
insert!(codelocs, idx, codeloc)
Expand All @@ -579,6 +580,8 @@ function convert_to_ircode(ci::CodeInfo, sv::OptimizationState)
end
idx += 1
prevloc = codeloc
else
labelmap[oldidx] = changemap[oldidx]
end
if code[idx] isa Expr && ssavaluetypes[idx] === Union{}
if !(idx < length(code) && isa(code[idx + 1], ReturnNode) && !isdefined((code[idx + 1]::ReturnNode), :val))
Expand All @@ -590,7 +593,7 @@ function convert_to_ircode(ci::CodeInfo, sv::OptimizationState)
insert!(ssaflags, idx + 1, ssaflags[idx])
if oldidx < length(changemap)
changemap[oldidx + 1] += 1
coverage && (labelmap[oldidx + 1] += 1)
file_coverage && (labelmap[oldidx + 1] += 1)
end
idx += 1
end
Expand Down
4 changes: 4 additions & 0 deletions base/compiler/ssair/inlining.jl
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,10 @@ function ir_inline_item!(compact::IncrementalCompact, idx::Int, argexprs::Vector
oldlinetable = spec.ir.linetable
for oldline in 1:length(oldlinetable)
entry = oldlinetable[oldline]
if !coverage && is_file_tracked(entry.file)
# include topline coverage entry if in path-specific coverage mode, and any file falls under path
coverage = true
end
newentry = LineInfoNode(entry.module, entry.method, entry.file, entry.line,
(entry.inlined_at > 0 ? entry.inlined_at + linetable_offset + (oldline == 1) : inlined_at))
if oldline == 1
Expand Down
4 changes: 2 additions & 2 deletions base/compiler/utilities.jl
Original file line number Diff line number Diff line change
Expand Up @@ -359,12 +359,12 @@ inlining_enabled() = (JLOptions().can_inline == 1)
function coverage_enabled(m::Module)
ccall(:jl_generating_output, Cint, ()) == 0 || return false # don't alter caches
cov = JLOptions().code_coverage
if cov == 1
if cov == 1 # user
m = moduleroot(m)
m === Core && return false
isdefined(Main, :Base) && m === Main.Base && return false
return true
elseif cov == 2
elseif cov == 2 # all
return true
end
return false
Expand Down
5 changes: 5 additions & 0 deletions base/options.jl
Original file line number Diff line number Diff line change
Expand Up @@ -89,3 +89,8 @@ function unsafe_load_commands(v::Ptr{Ptr{UInt8}})
end
return cmds
end

function is_file_tracked(file::Symbol)
JLOptions().code_coverage == 3 || return false
return ccall(:jl_is_file_tracked, Int, (Any,), file) == 0
end
7 changes: 7 additions & 0 deletions src/init.c
Original file line number Diff line number Diff line change
Expand Up @@ -597,6 +597,13 @@ static void jl_resolve_sysimg_location(JL_IMAGE_SEARCH rel)
}
}

JL_DLLEXPORT int jl_is_file_tracked(jl_sym_t *path)
{
const char* path_ = jl_symbol_name(path);
int tpath_len = strlen(jl_options.tracked_path);
return strlen(path_) >= tpath_len && (strncmp(path_, jl_options.tracked_path, tpath_len) == 0);
}

static void jl_set_io_wait(int v)
{
jl_task_t *ct = jl_current_task;
Expand Down
4 changes: 0 additions & 4 deletions test/cmdlineargs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -328,10 +328,6 @@ let exename = `$(Base.julia_cmd()) --startup-file=no --color=no`
@test_broken occursin(expected_good, got)

# Ask for coverage in specific file
# TODO: Figure out why asking for a specific file/dir means some lines are under-counted
# NOTE that a different expected reference is loaded here
expected = replace(read(joinpath(helperdir, "coverage_file.info.bad2"), String),
"<FILENAME>" => realpath(inputfile))
tfile = realpath(inputfile)
@test readchomp(`$exename -E "(Base.JLOptions().code_coverage, unsafe_string(Base.JLOptions().tracked_path))" -L $inputfile
--code-coverage=$covfile --code-coverage=@$tfile`) == "(3, $(repr(tfile)))"
Expand Down

0 comments on commit 82a316e

Please sign in to comment.