From 6455e57e82a7db43fded1c46771a6516f170598a Mon Sep 17 00:00:00 2001 From: Alexey Date: Sat, 4 Apr 2020 16:36:46 +0300 Subject: [PATCH] fmt: process C function declarations correctly --- vlib/v/ast/str.v | 3 +++ vlib/v/fmt/fmt.v | 10 +++++++--- vlib/v/fmt/fmt_keep_test.v | 3 ++- vlib/v/fmt/tests/functions_expected.vv | 2 ++ vlib/v/fmt/tests/functions_input.vv | 2 ++ 5 files changed, 16 insertions(+), 4 deletions(-) diff --git a/vlib/v/ast/str.v b/vlib/v/ast/str.v index 8e5a946714..0ce56e6c2e 100644 --- a/vlib/v/ast/str.v +++ b/vlib/v/ast/str.v @@ -25,6 +25,9 @@ pub fn (node &FnDecl) str(t &table.Table) string { receiver = '($node.receiver.name $m$name) ' } name := node.name.after('.') + if node.is_c { + name = 'C.$name' + } f.write('fn ${receiver}${name}(') for i, arg in node.args { // skip receiver diff --git a/vlib/v/fmt/fmt.v b/vlib/v/fmt/fmt.v index 2aeca384eb..6c94cd0a74 100644 --- a/vlib/v/fmt/fmt.v +++ b/vlib/v/fmt/fmt.v @@ -173,9 +173,13 @@ fn (f mut Fmt) stmt(node ast.Stmt) { s := it.str(f.table) // f.write(it.str(f.table)) f.write(s.replace(f.cur_mod + '.', '')) // `Expr` instead of `ast.Expr` in mod ast - f.writeln(' {') - f.stmts(it.stmts) - f.writeln('}\n') + if !it.is_c { + f.writeln(' {') + f.stmts(it.stmts) + f.writeln('}\n') + } else { + f.writeln('\n') + } } ast.ForInStmt { f.write('for ') diff --git a/vlib/v/fmt/fmt_keep_test.v b/vlib/v/fmt/fmt_keep_test.v index 8ec0972489..506f5ad475 100644 --- a/vlib/v/fmt/fmt_keep_test.v +++ b/vlib/v/fmt/fmt_keep_test.v @@ -2,6 +2,7 @@ import ( os term benchmark + v.ast v.fmt v.parser v.table @@ -44,7 +45,7 @@ fn test_fmt() { continue } table := table.new_table() - file_ast := parser.parse_file(ipath, table, .parse_comments, &pref.Preferences{}) + file_ast := parser.parse_file(ipath, table, .parse_comments, &pref.Preferences{}, &ast.Scope{parent: 0}) result_ocontent := fmt.fmt(file_ast, table) if expected_ocontent != result_ocontent { fmt_bench.fail() diff --git a/vlib/v/fmt/tests/functions_expected.vv b/vlib/v/fmt/tests/functions_expected.vv index b22d8fc78f..394882c404 100644 --- a/vlib/v/fmt/tests/functions_expected.vv +++ b/vlib/v/fmt/tests/functions_expected.vv @@ -1,3 +1,5 @@ +fn C.func(arg int) int + fn fn_variadic(arg int, args ...string) { println('Do nothing') } diff --git a/vlib/v/fmt/tests/functions_input.vv b/vlib/v/fmt/tests/functions_input.vv index 041a01f5ee..6bec37d17c 100644 --- a/vlib/v/fmt/tests/functions_input.vv +++ b/vlib/v/fmt/tests/functions_input.vv @@ -1,3 +1,5 @@ +fn C.func(arg int) int + fn fn_variadic(arg int, args... string) { println('Do nothing') }