Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix code coverage in specific path mode #44625

Merged
merged 5 commits into from
Mar 21, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion base/compiler/optimize.jl
Original file line number Diff line number Diff line change
Expand Up @@ -558,12 +558,21 @@ function convert_to_ircode(ci::CodeInfo, sv::OptimizationState)
idx = 1
oldidx = 1
changemap = fill(0, length(code))
labelmap = coverage ? fill(0, length(code)) : changemap
prevloc = zero(eltype(ci.codelocs))
stmtinfo = sv.stmt_info
codelocs = ci.codelocs
ssavaluetypes = ci.ssavaluetypes::Vector{Any}
ssaflags = ci.ssaflags
if !coverage && JLOptions().code_coverage == 3
for codeloc in codelocs
if codeloc != 0 && is_file_tracked(ci.linetable[codeloc].file)
IanButterworth marked this conversation as resolved.
Show resolved Hide resolved
# if any line falls in a tracked file enable coverage for all
coverage = true
break
end
end
end
labelmap = coverage ? fill(0, length(code)) : changemap
while idx <= length(code)
codeloc = codelocs[idx]
if coverage && codeloc != prevloc && codeloc != 0
Expand Down
5 changes: 5 additions & 0 deletions base/compiler/ssair/inlining.jl
Original file line number Diff line number Diff line change
Expand Up @@ -319,10 +319,15 @@ function ir_inline_item!(compact::IncrementalCompact, idx::Int, argexprs::Vector
inlined_at = Int(compact.result[idx][:line])
topline::Int32 = linetable_offset + Int32(1)
coverage = coverage_enabled(def.module)
coverage_by_path = JLOptions().code_coverage == 3
push!(linetable, LineInfoNode(def.module, def.name, def.file, Int(def.line), inlined_at))
oldlinetable = spec.ir.linetable
for oldline in 1:length(oldlinetable)
entry = oldlinetable[oldline]
if !coverage && coverage_by_path && 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
4 changes: 4 additions & 0 deletions base/options.jl
Original file line number Diff line number Diff line change
Expand Up @@ -89,3 +89,7 @@ function unsafe_load_commands(v::Ptr{Ptr{UInt8}})
end
return cmds
end

function is_file_tracked(file::Symbol)
return ccall(:jl_is_file_tracked, Int, (Any,), file) == 1
IanButterworth marked this conversation as resolved.
Show resolved Hide resolved
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