Skip to content

Commit

Permalink
coverage: ensure definition line is also counted
Browse files Browse the repository at this point in the history
Closes #36825

(cherry picked from commit 8aaf971)
  • Loading branch information
vtjnash authored and vchuravy committed Jan 22, 2021
1 parent c25af17 commit 790298e
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 4 deletions.
7 changes: 7 additions & 0 deletions src/codegen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5956,6 +5956,13 @@ static std::pair<std::unique_ptr<Module>, jl_llvm_functions_t>
Value *sync_bytes = nullptr;
if (do_malloc_log(true))
sync_bytes = ctx.builder.CreateCall(prepare_call(diff_gc_total_bytes_func), {});
{ // coverage for the function definition line number
const auto &topinfo = linetable.at(0);
if (topinfo == linetable.at(1))
current_lineinfo.push_back(1);
if (do_coverage(topinfo.is_user_code))
coverageVisitLine(ctx, topinfo.file, topinfo.line);
}

find_next_stmt(0);
while (cursor != -1) {
Expand Down
7 changes: 6 additions & 1 deletion test/cmdlineargs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,9 @@ let exename = `$(Base.julia_cmd()) --startup-file=no`
mktempdir() do dir
helperdir = joinpath(@__DIR__, "testhelpers")
inputfile = joinpath(helperdir, "coverage_file.jl")
expected = replace(read(joinpath(helperdir, "coverage_file.info"), String),
expected = replace(read(joinpath(helperdir, "coverage_file.info.bad"), String),
"<FILENAME>" => realpath(inputfile))
expected_good = replace(read(joinpath(helperdir, "coverage_file.info"), String),
"<FILENAME>" => realpath(inputfile))
covfile = replace(joinpath(dir, "coverage.info"), "%" => "%%")
@test !isfile(covfile)
Expand All @@ -274,18 +276,21 @@ let exename = `$(Base.julia_cmd()) --startup-file=no`
got = read(covfile, String)
rm(covfile)
@test occursin(expected, got) || (expected, got)
@test_broken occursin(expected_good, got)
@test readchomp(`$exename -E "Base.JLOptions().code_coverage" -L $inputfile
--code-coverage=$covfile --code-coverage=user`) == "1"
@test isfile(covfile)
got = read(covfile, String)
rm(covfile)
@test occursin(expected, got) || (expected, got)
@test_broken occursin(expected_good, got)
@test readchomp(`$exename -E "Base.JLOptions().code_coverage" -L $inputfile
--code-coverage=$covfile --code-coverage=all`) == "2"
@test isfile(covfile)
got = read(covfile, String)
rm(covfile)
@test occursin(expected, got) || (expected, got)
@test_broken occursin(expected_good, got)
end

# --track-allocation
Expand Down
8 changes: 6 additions & 2 deletions test/testhelpers/coverage_file.info
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
SF:<FILENAME>
DA:3,1
DA:4,1
DA:5,0
DA:7,1
Expand All @@ -8,6 +9,9 @@ DA:11,1
DA:12,1
DA:14,0
DA:17,1
LH:7
LF:9
DA:19,1
DA:20,1
DA:22,1
LH:10
LF:13
end_of_record
19 changes: 19 additions & 0 deletions test/testhelpers/coverage_file.info.bad
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
SF:<FILENAME>
DA:3,1
DA:4,1
DA:5,0
DA:7,1
DA:8,1
DA:9,5
DA:11,1
DA:12,1
DA:14,0
DA:17,1
DA:18,0
DA:19,1
DA:20,1
DA:22,1
DA:1234,0
LH:11
LF:15
end_of_record
8 changes: 7 additions & 1 deletion test/testhelpers/coverage_file.jl
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,13 @@ function code_coverage_test()
not_reached
end

short_form_func_coverage_test(x) = x*x
@eval short_form_func_coverage_test(x) = begin
$(Expr(:line, 1234))
y() = begin
x
end
x * y()
end

success = code_coverage_test() == [1, 2, 3] &&
short_form_func_coverage_test(2) == 4
Expand Down

0 comments on commit 790298e

Please sign in to comment.