From fb4f7b6827220664fd2e98d4a11681da1daaca1b Mon Sep 17 00:00:00 2001 From: Delyan Angelov Date: Thu, 26 Mar 2020 10:20:55 +0200 Subject: [PATCH] v2: enable compilation of short programs with no 'import os' --- vlib/v/gen/cgen.v | 17 +++++++++++++---- vlib/v/gen/cgen_test.v | 6 +++--- vlib/v/gen/tests/1.c | 1 - vlib/v/gen/tests/2.c | 1 - vlib/v/gen/tests/3.c | 1 - vlib/v/gen/tests/4.c | 1 - 6 files changed, 16 insertions(+), 11 deletions(-) diff --git a/vlib/v/gen/cgen.v b/vlib/v/gen/cgen.v index c36a953faf..dd6d4930db 100644 --- a/vlib/v/gen/cgen.v +++ b/vlib/v/gen/cgen.v @@ -665,10 +665,12 @@ fn (g mut Gen) gen_fn_decl(it ast.FnDecl) { } if is_main { g.writeln('_vinit();') - if g.autofree { - g.writeln('free(_const_os__args.data); // empty, inited in _vinit()') + if g.is_importing_os() { + if g.autofree { + g.writeln('free(_const_os__args.data); // empty, inited in _vinit()') + } + g.writeln('_const_os__args = os__init_os_args(argc, (byteptr*)argv);') } - g.writeln('_const_os__args = os__init_os_args(argc, (byteptr*)argv);') } g.stmts(it.stmts) // //////////// @@ -1794,7 +1796,10 @@ fn (g mut Gen) write_init_function() { if g.autofree { g.writeln('void _vcleanup() {') g.writeln('puts("cleaning up...");') - g.writeln('free(_const_os__args.data);') + if g.is_importing_os() { + g.writeln('free(_const_os__args.data);') + g.writeln('string_free(_const_os__wd_at_startup);') + } g.writeln('free(_const_strconv__ftoa__powers_of_10.data);') g.writeln('}') } @@ -2318,3 +2323,7 @@ pub fn (g mut Gen) write_tests_main() { } g.writeln('return 0; }') } + +fn (g &Gen) is_importing_os() bool { + return 'os' in g.table.imports +} diff --git a/vlib/v/gen/cgen_test.v b/vlib/v/gen/cgen_test.v index f6fcbab0f4..eb54d174f2 100644 --- a/vlib/v/gen/cgen_test.v +++ b/vlib/v/gen/cgen_test.v @@ -62,9 +62,9 @@ fn compare_texts(a, b, path string) bool { line_b := lines_b[i] if line_a.trim_space() != line_b.trim_space() { println('${path}: Got\n$a') - println('${term_fail} near line: ${i}') - println(term.red('actual :${line_a}')) - println(term.red('expected:${line_b}')) + println('${path}:${i}: ${term_fail}') + println(term.bold(term.bright_yellow('actual : ')) + line_a) + println(term.green('expected: ') + line_b) println(lines_b[i + 1]) println(lines_b[i + 2]) // exit(1) diff --git a/vlib/v/gen/tests/1.c b/vlib/v/gen/tests/1.c index d912bf1b1f..b8f2f5817d 100644 --- a/vlib/v/gen/tests/1.c +++ b/vlib/v/gen/tests/1.c @@ -48,7 +48,6 @@ struct varg_int { // int main(int argc, char** argv) { _vinit(); - _const_os__args = os__init_os_args(argc, (byteptr*)argv); int a = 10; a++; int negative = -a; diff --git a/vlib/v/gen/tests/2.c b/vlib/v/gen/tests/2.c index 63e4d1d8a0..8317138d83 100644 --- a/vlib/v/gen/tests/2.c +++ b/vlib/v/gen/tests/2.c @@ -78,7 +78,6 @@ void end() { int main(int argc, char** argv) { _vinit(); - _const_os__args = os__init_os_args(argc, (byteptr*)argv); return 0; } diff --git a/vlib/v/gen/tests/3.c b/vlib/v/gen/tests/3.c index 508dfa7083..64e970c8b7 100644 --- a/vlib/v/gen/tests/3.c +++ b/vlib/v/gen/tests/3.c @@ -64,7 +64,6 @@ void handle_expr(Expr e) { int main(int argc, char** argv) { _vinit(); - _const_os__args = os__init_os_args(argc, (byteptr*)argv); User user = (User){ .age = 0, .name = tos3(""), diff --git a/vlib/v/gen/tests/4.c b/vlib/v/gen/tests/4.c index edeb62626a..c61f05df33 100644 --- a/vlib/v/gen/tests/4.c +++ b/vlib/v/gen/tests/4.c @@ -24,7 +24,6 @@ int Bar_testa(Bar* b); int main(int argc, char** argv) { _vinit(); - _const_os__args = os__init_os_args(argc, (byteptr*)argv); Bar b = (Bar){ .a = 122, };