diff --git a/vlib/v/builder/cbuilder/cbuilder.v b/vlib/v/builder/cbuilder/cbuilder.v index e3f5e974cf4bb3..64768b795b400a 100644 --- a/vlib/v/builder/cbuilder/cbuilder.v +++ b/vlib/v/builder/cbuilder/cbuilder.v @@ -1,6 +1,7 @@ module cbuilder import os +import strings import v.pref import v.util import v.builder @@ -60,17 +61,17 @@ pub fn build_c(mut b builder.Builder, v_files []string, out_file string) { if b.pref.is_vlines { output2 = c.fix_reset_dbg_line(output2, out_file) } - os.write_file(out_file, output2) or { panic(err) } + os.write_file_array(out_file, output2) or { panic(err) } if b.pref.is_stats { - b.stats_lines = output2.count('\n') + 1 + b.stats_lines = output2.count(it == `\n`) + 1 b.stats_bytes = output2.len } } -pub fn gen_c(mut b builder.Builder, v_files []string) string { +pub fn gen_c(mut b builder.Builder, v_files []string) strings.Builder { b.front_and_middle_stages(v_files) or { if err.code() > 7000 { - return '' + return []u8{} } builder.verror(err.msg()) } @@ -86,5 +87,5 @@ pub fn gen_c(mut b builder.Builder, v_files []string) string { util.timing_measure('Parallel C compilation') } - return result.res + return result.res_builder } diff --git a/vlib/v/gen/c/cgen.v b/vlib/v/gen/c/cgen.v index 9fd80c4ba032f4..0461a3f22ec191 100644 --- a/vlib/v/gen/c/cgen.v +++ b/vlib/v/gen/c/cgen.v @@ -276,12 +276,12 @@ mut: @[heap] pub struct GenOutput { pub: - header string // produced output for out.h (-parallel-cc) - res string // produced output (complete) - out_str string // produced output from g.out - out0_str string // helpers output (auto fns, dump fns) for out_0.c (-parallel-cc) - extern_str string // extern chunk for (-parallel-cc) - out_fn_start_pos []int // fn decl positions + header string // produced output for out.h (-parallel-cc) + res_builder strings.Builder // produced output (complete) + out_str string // produced output from g.out + out0_str string // helpers output (auto fns, dump fns) for out_0.c (-parallel-cc) + extern_str string // extern chunk for (-parallel-cc) + out_fn_start_pos []int // fn decl positions } pub fn gen(files []&ast.File, mut table ast.Table, pref_ &pref.Preferences) GenOutput { @@ -723,18 +723,18 @@ pub fn gen(files []&ast.File, mut table ast.Table, pref_ &pref.Preferences) GenO } // End of out_0.c + shelpers := helpers.str() + if !g.pref.parallel_cc { - b.write_string(helpers.str()) + b.write_string(shelpers) } // The rest of the output out_str := g.out.str() - out0_str := helpers.str() extern_out_str := g.extern_out.str() b.write_string(out_str) b.writeln('// THE END.') util.timing_measure('cgen common') - res := b.str() $if trace_all_generic_fn_keys ? { gkeys := g.table.fn_generic_types.keys() for gkey in gkeys { @@ -742,15 +742,14 @@ pub fn gen(files []&ast.File, mut table ast.Table, pref_ &pref.Preferences) GenO } } out_fn_start_pos := g.out_fn_start_pos.clone() - unsafe { b.free() } unsafe { helpers.free() } unsafe { g.free_builders() } return GenOutput{ header: header - res: res + res_builder: b out_str: out_str - out0_str: out0_str + out0_str: shelpers extern_str: extern_out_str out_fn_start_pos: out_fn_start_pos } diff --git a/vlib/v/gen/c/cmain.v b/vlib/v/gen/c/cmain.v index 5c2c32ddd1e6e8..0d169b05610e45 100644 --- a/vlib/v/gen/c/cmain.v +++ b/vlib/v/gen/c/cmain.v @@ -47,7 +47,7 @@ fn (mut g Gen) gen_vlines_reset() { } } -pub fn fix_reset_dbg_line(src string, out_file string) string { +pub fn fix_reset_dbg_line(src strings.Builder, out_file string) strings.Builder { util.timing_start(@FN) defer { util.timing_measure(@FN) @@ -60,7 +60,7 @@ pub fn fix_reset_dbg_line(src string, out_file string) string { for idx, ob in src { if ob == `\n` { lines++ - if unsafe { vmemcmp(src.str + idx + 1, reset_dbg_line.str, reset_dbg_line.len) } == 0 { + if unsafe { vmemcmp(&u8(src.data) + idx + 1, reset_dbg_line.str, reset_dbg_line.len) } == 0 { dbg_reset_line_idx = idx + 1 break } @@ -69,7 +69,7 @@ pub fn fix_reset_dbg_line(src string, out_file string) string { // find the position of the "..\..\..\src.tmp.c": mut first_quote_idx := 0 for idx := dbg_reset_line_idx; idx < src.len; idx++ { - if unsafe { src.str[idx] } == `"` { + if unsafe { &u8(src.data)[idx] } == `"` { first_quote_idx = idx break } @@ -78,15 +78,15 @@ pub fn fix_reset_dbg_line(src string, out_file string) string { // before and after it unchanged: mut sb := strings.new_builder(src.len) unsafe { - sb.write_ptr(src.str, dbg_reset_line_idx) + sb.write_ptr(&u8(src.data), dbg_reset_line_idx) sb.write_string('#line ') sb.write_decimal(lines) - sb.write_ptr(src.str + first_quote_idx - 1, src.len - first_quote_idx) + sb.write_ptr(&u8(src.data) + first_quote_idx - 1, src.len - first_quote_idx) } $if trace_reset_dbg_line ? { eprintln('> reset_dbg_line: ${out_file}:${lines} | first_quote_idx: ${first_quote_idx} | src.len: ${src.len} | sb.len: ${sb.len} | sb.cap: ${sb.cap}') } - return sb.str() + return sb } fn (mut g Gen) gen_c_main_function_only_header() { diff --git a/vlib/v/parser/v_parser_test.v b/vlib/v/parser/v_parser_test.v index 4ee7dd4a65e00d..52c36b5f86d6ae 100644 --- a/vlib/v/parser/v_parser_test.v +++ b/vlib/v/parser/v_parser_test.v @@ -110,7 +110,7 @@ fn test_one() { mut checker_ := checker.new_checker(table, vpref) checker_.check(mut program) result := c.gen([program], mut table, vpref) - res := result.res.replace('\n', '').trim_space().after('#endif') + res := result.res_builder.bytestr().replace('\n', '').trim_space().after('#endif') println(res) ok := expected == res println(res) @@ -153,7 +153,7 @@ fn test_parse_expr() { } chk.check(mut program) result := c.gen([program], mut table, vpref) - res := result.res.after('#endif') + res := result.res_builder.bytestr().after('#endif') println('========') println(res) println('========')