Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
felipensp committed May 23, 2024
1 parent 8344d02 commit 0adcd12
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 9 deletions.
8 changes: 4 additions & 4 deletions vlib/v/gen/c/cgen.v
Original file line number Diff line number Diff line change
Expand Up @@ -565,10 +565,6 @@ pub fn gen(files []&ast.File, mut table ast.Table, pref_ &pref.Preferences) (str
b.writeln('\n// V profile counters:')
b.write_string(g.pcs_declarations.str())
}
if g.pref.is_coverage {
b.writeln('\n// V coverage:')
b.write_string(g.cov_declarations.str())
}
b.writeln('\n// V includes:')
b.write_string(g.includes.str())
b.writeln('\n// Enum definitions:')
Expand Down Expand Up @@ -645,6 +641,10 @@ pub fn gen(files []&ast.File, mut table ast.Table, pref_ &pref.Preferences) (str
b.writeln(fn_def)
}
}
if g.pref.is_coverage {
b.writeln('\n// V coverage:')
b.write_string(g.cov_declarations.str())
}
b.writeln('\n// end of V out')
mut header := b.last_n(b.len)
header = '#ifndef V_HEADER_FILE\n#define V_HEADER_FILE' + header
Expand Down
38 changes: 33 additions & 5 deletions vlib/v/gen/c/coverage.v
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,21 @@ module c

import v.token

@[inline]
fn current_time() u64 {
unsafe {
$if windows {
tm := u64(0)
C.QueryPerformanceCounter(&tm)
return tm
} $else {
ts := C.timespec{}
C.clock_gettime(C.CLOCK_MONOTONIC, &ts)
return u64(ts.tv_sec) * 1000000000 + u64(ts.tv_nsec)
}
}
}

@[inline]
fn (mut g Gen) write_coverage_point(pos token.Pos) {
if g.unique_file_path_hash !in g.coverage_files {
Expand All @@ -24,15 +39,26 @@ fn (mut g Gen) write_coverage_point(pos token.Pos) {
}

fn (mut g Gen) write_coverage_stats() {
is_stdout := g.pref.coverage_file == '-'
is_stdout := false // g.pref.coverage_file == '-'

g.cov_declarations.writeln('char* vcoverage_fnv1a(const u8* data) {')
g.cov_declarations.writeln('\tu32 h = 2166136261UL;')
g.cov_declarations.writeln('\tsize_t size = strlen(data);')
g.cov_declarations.writeln('\tfor (size_t i = 0; i < size; i++) {')
g.cov_declarations.writeln('\th ^= data[i];')
g.cov_declarations.writeln('\th *= 16777619;')
g.cov_declarations.writeln('\t}')
g.cov_declarations.writeln('\treturn u32_str(h).str;')
g.cov_declarations.writeln('}')
g.cov_declarations.writeln('')
g.cov_declarations.writeln('void vprint_coverage_stats() {')
if is_stdout {
g.cov_declarations.writeln('\tprintf("V coverage\\n");')
g.cov_declarations.writeln('\tprintf("${'-':50r}\\n");')
} else {
g.cov_declarations.writeln('\tFILE *fp = stdout;')
g.cov_declarations.writeln('\tfp = fopen ("${g.pref.coverage_file}", "w+");')
g.cov_declarations.writeln('time_t rawtime;')
g.cov_declarations.writeln('time(&rawtime);')
g.cov_declarations.writeln('\tFILE *fp = fopen(vcoverage_fnv1a(asctime(localtime(&rawtime))), "w+");')
g.cov_declarations.writeln('\tfprintf(fp, "V coverage\\n");')
g.cov_declarations.writeln('\tfprintf(fp, "${'-':50r}\\n");')
}
Expand All @@ -47,11 +73,13 @@ fn (mut g Gen) write_coverage_stats() {
g.cov_declarations.writeln('\t\tfor (int i = 0, 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) {')
if is_stdout {
g.cov_declarations.writeln('\t\tprintf("[%7.2f%%] %4d / ${nr_points:4d} | ${cov.file.path}\\n", ${nr_points} > 0 ? ((double)counter/${nr_points})*100 : 0, counter);')
g.cov_declarations.writeln('\t\t\tprintf("[%7.2f%%] %4d / ${nr_points:4d} | ${cov.file.path}\\n", ${nr_points} > 0 ? ((double)counter/${nr_points})*100 : 0, counter);')
} else {
g.cov_declarations.writeln('\t\tfprintf(fp, "[%7.2f%%] %4d / ${nr_points:4d} | ${cov.file.path}\\n", ${nr_points} > 0 ? ((double)counter/${nr_points})*100 : 0, counter);')
g.cov_declarations.writeln('\t\t\tfprintf(fp, "[%7.2f%%] %4d / ${nr_points:4d} | ${cov.file.path}\\n", ${nr_points} > 0 ? ((double)counter/${nr_points})*100 : 0, counter);')
}
g.cov_declarations.writeln('\t\t}')
g.cov_declarations.writeln('\t}')
last_offset += nr_points
}
Expand Down

0 comments on commit 0adcd12

Please sign in to comment.