Skip to content

Commit

Permalink
improve
Browse files Browse the repository at this point in the history
  • Loading branch information
felipensp committed May 25, 2024
1 parent a561426 commit 01ed4d7
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 15 deletions.
52 changes: 44 additions & 8 deletions cmd/tools/vcover/vcover.v
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,24 @@ import arrays

const tmp_dir = os.join_path(os.vtmp_dir(), 'cover')

@[heap]
struct VCoverData {
file string // file name
hits u64 // file coverage hits
points u64 // file counter
file string // file name
mut:
hits u64 // file coverage hits
points u64 // file counter
}

fn report_file(covfile string) ! {
json_content := os.read_file(covfile)!
data := json.decode([]VCoverData, json_content)!
pub fn (covdata []&VCoverData) find(filename string) ?&VCoverData {
for data in covdata {
if data.file == filename {
return unsafe { data }
}
}
return none
}

fn display_result(data []VCoverData) ! {
hits := arrays.sum(data.map(it.hits))!
points := arrays.sum(data.map(it.points))!
for lineinfo in data {
Expand All @@ -27,13 +36,40 @@ fn report_file(covfile string) ! {
println('Total coverage: ${data.len} files, ${(f64(hits) / points) * 100:.2f}% coverage')
}

fn report_file(covfile string) ! {
json_content := os.read_file(covfile)!
data := json.decode([]VCoverData, json_content)!
display_result(data)!
}

fn summarize_coverage(covfile string, mut covdata []&VCoverData) ! {
json_content := os.read_file(covfile)!
data := json.decode([]VCoverData, json_content)!
for lineinfo in data {
if mut fileinfo := covdata.find(lineinfo.file) {
if fileinfo.hits < lineinfo.hits {
fileinfo.hits = lineinfo.hits
}
} else {
covdata << &VCoverData{
...lineinfo
}
}
}
}

fn main() {
args := cmdline.options_after(os.args, ['cover'])
covfile := os.real_path(cmdline.option(args, '-file', ''))
// covdir := os.real_path(cmdline.option(args, '-dir', ''))
// reportfile := os.real_path(cmdline.option(args, '-report', ''))
covdir := os.real_path(cmdline.option(args, '-dir', ''))

if covfile != '' {
report_file(covfile)!
} else if covdir != '' {
mut sum_data := []&VCoverData{}
for coverfile in os.glob(covdir + '/vcover.*')! {
summarize_coverage(coverfile, mut sum_data)!
}
display_result(sum_data.map(*it))!
}
}
2 changes: 1 addition & 1 deletion vlib/v/gen/c/cgen.v
Original file line number Diff line number Diff line change
Expand Up @@ -2107,7 +2107,7 @@ fn (mut g Gen) write_v_source_line_info_stmt(stmt ast.Stmt) {
&& stmt !in [ast.FnDecl, ast.ForCStmt, ast.ForInStmt, ast.ForStmt] {
if stmt is ast.ExprStmt {
if !g.inside_assign
&& stmt.expr !in [ast.StringInterLiteral, ast.StringLiteral, ast.Ident, ast.UnsafeExpr] {
&& stmt.expr !in [ast.IndexExpr, ast.StringInterLiteral, ast.StringLiteral, ast.Ident, ast.UnsafeExpr] {
g.write_coverage_point(stmt.pos)
}
} else {
Expand Down
10 changes: 4 additions & 6 deletions vlib/v/gen/c/coverage.v
Original file line number Diff line number Diff line change
Expand Up @@ -48,20 +48,18 @@ fn (mut g Gen) write_coverage_stats() {
g.cov_declarations.writeln('\tFILE *fp = fopen(cov_filename, "w+");')
g.cov_declarations.writeln('\tfprintf(fp, "[");')
g.cov_declarations.writeln('\tint t_counter = 0;')
g.cov_declarations.writeln('\tint is_first = 1;')
mut last_offset := 0
mut is_first := true
for k, cov in g.coverage_files {
nr_points := cov.points.len
g.cov_declarations.writeln('\t{')
g.cov_declarations.writeln('\t\tint counter = 0;')
g.cov_declarations.writeln('\t\tfor (int i = 0, offset = ${last_offset}; i < ${nr_points}; ++i)')
g.cov_declarations.writeln('\t\tfor (int i = 0, is_first = 1, offset = ${last_offset}; i < ${nr_points}; ++i)')
g.cov_declarations.writeln('\t\t\tif (_v_cov[_v_cov_file_offset_${k}+i]) counter++;')
g.cov_declarations.writeln('\t\tt_counter += counter;')
g.cov_declarations.writeln('\t\tif (counter) {')
g.cov_declarations.writeln("\t\t\tfprintf(fp, \"%s{\\\"file\\\":\\\"%s\\\",\\\"hits\\\":%d,\\\"points\\\":%d}\", ${int(is_first)} ? \"\" : \",\", \"${cov.file.path}\", counter, ${nr_points});")
if is_first {
is_first = !is_first
}
g.cov_declarations.writeln("\t\t\tfprintf(fp, \"%s{\\\"file\\\":\\\"%s\\\",\\\"hits\\\":%d,\\\"points\\\":%d}\", is_first ? \"\" : \",\", \"${cov.file.path}\", counter, ${nr_points});")
g.cov_declarations.writeln('\t\t\tis_first = 0;')
g.cov_declarations.writeln('\t\t}')
g.cov_declarations.writeln('\t}')
last_offset += nr_points
Expand Down

0 comments on commit 01ed4d7

Please sign in to comment.