From 7ad1441c8162d0e95f219d52924b6fa2afb043df Mon Sep 17 00:00:00 2001 From: Alexander Medvednikov Date: Sat, 21 Mar 2020 11:47:23 +0100 Subject: [PATCH] cgen: fix the tests --- vlib/v/gen/cgen.v | 7 +++---- vlib/v/gen/cgen_test.v | 18 +++++++++++++++++- vlib/v/gen/cheaders.v | 1 + vlib/v/gen/tests/2.c | 6 ++++++ vlib/v/gen/tests/3.c | 6 ++++++ vlib/v/gen/tests/4.c | 7 ++++++- 6 files changed, 39 insertions(+), 6 deletions(-) diff --git a/vlib/v/gen/cgen.v b/vlib/v/gen/cgen.v index 1f4f79880a..b6c4258a3e 100644 --- a/vlib/v/gen/cgen.v +++ b/vlib/v/gen/cgen.v @@ -43,7 +43,7 @@ pub fn cgen(files []ast.File, table &table.Table) string { g.stmts(file.stmts) } g.write_variadic_types() - g.write_str_definitions() + // g.write_str_definitions() g.write_init_function() return g.typedefs.str() + g.definitions.str() + g.out.str() } @@ -57,6 +57,7 @@ pub fn (g mut Gen) init() { g.definitions.writeln('\nstring _STR_TMP(const char*, ...);\n') g.write_builtin_types() g.write_typedef_types() + g.write_str_definitions() g.write_sorted_types() g.write_multi_return_types() g.definitions.writeln('// end of definitions #endif') @@ -1591,13 +1592,12 @@ string _STR_TMP(const char *fmt, ...) { //puts(g_str_buf); #endif return tos2(g_str_buf); -} +} // endof _STR_TMP ') } const ( -// TODO all builtin types must be lowercase builtins = ['string', 'array', 'KeyValue', 'DenseArray', 'map', 'Option'] ) @@ -1625,7 +1625,6 @@ fn (g mut Gen) write_sorted_types() { types_sorted := g.sort_structs(types) // Generate C code g.definitions.writeln('// builtin types:') - // g.write_types(builtin_types) g.definitions.writeln('//------------------ #endbuiltin') g.write_types(types_sorted) } diff --git a/vlib/v/gen/cgen_test.v b/vlib/v/gen/cgen_test.v index e81ee5dc4b..956c07d75f 100644 --- a/vlib/v/gen/cgen_test.v +++ b/vlib/v/gen/cgen_test.v @@ -23,7 +23,14 @@ fn test_c_files() { ctext = ctext // unused warn mut b := builder.new_builder(pref.Preferences{}) b.module_search_paths = ['$vroot/vlib/v/gen/tests/'] - res := b.gen_c([path]).after('#endbuiltin') + mut res := b.gen_c([path]).after('#endbuiltin') + if res.contains('string _STR') { + pos := res.index('string _STR') or { + -1 + } + end := res.index_after('endof _STR_TMP', pos) + res = res[..pos] + res[end + 15..] + } if compare_texts(res, ctext, path) { println('${term_ok} ${i}') } @@ -47,6 +54,15 @@ fn compare_texts(a, b, path string) bool { */ for i, line_a in lines_a { + // mut j := 0 + // for i := 0; i < lines_a.len; i++ { + // line_a := lines_a[i] + // if line_a.contains('string _STR') { + // println(' SKIPPING!!') + // for !lines_a[i].contains('}') { + // i++ + // } + // } if i >= lines_b.len { println(line_a) return false diff --git a/vlib/v/gen/cheaders.v b/vlib/v/gen/cheaders.v index 504a7fde85..0b0f4dafee 100644 --- a/vlib/v/gen/cheaders.v +++ b/vlib/v/gen/cheaders.v @@ -304,6 +304,7 @@ typedef array array_u64; typedef map map_int; typedef map map_string; typedef byte array_fixed_byte_300 [300]; +typedef byte array_fixed_byte_400 [400]; #ifndef bool typedef int bool; #define true 1 diff --git a/vlib/v/gen/tests/2.c b/vlib/v/gen/tests/2.c index b3a9ca4741..6734f7d99c 100644 --- a/vlib/v/gen/tests/2.c +++ b/vlib/v/gen/tests/2.c @@ -76,7 +76,13 @@ void end() { int main(int argc, char** argv) { + _init(); + os__args = os__init_os_args(argc, (byteptr*)argv); return 0; } +void _init() { + +} + diff --git a/vlib/v/gen/tests/3.c b/vlib/v/gen/tests/3.c index 9b83706b46..1937054934 100644 --- a/vlib/v/gen/tests/3.c +++ b/vlib/v/gen/tests/3.c @@ -63,6 +63,8 @@ void handle_expr(Expr e) { } int main(int argc, char** argv) { + _init(); + os__args = os__init_os_args(argc, (byteptr*)argv); User user = (User){ 0}; user.age = 10; @@ -74,3 +76,7 @@ int main(int argc, char** argv) { 0}); return 0; } + +void _init() { + +} diff --git a/vlib/v/gen/tests/4.c b/vlib/v/gen/tests/4.c index 6fe08c2aea..316dd7260f 100644 --- a/vlib/v/gen/tests/4.c +++ b/vlib/v/gen/tests/4.c @@ -22,7 +22,9 @@ int Foo_testa(Foo* f); int Foo_testb(Foo* f); int Bar_testa(Bar* b); -int main() { +int main(int argc, char** argv) { + _init(); + os__args = os__init_os_args(argc, (byteptr*)argv); Bar b = (Bar){ .a = 122, }; @@ -91,3 +93,6 @@ int Foo_testb(Foo* f) { int Bar_testa(Bar* b) { return 4; } + +void _init() { +}