1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

fmt: process C function declarations correctly

This commit is contained in:
Alexey 2020-04-04 16:36:46 +03:00 committed by GitHub
parent 1960c6f4cb
commit 6455e57e82
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 16 additions and 4 deletions

View File

@ -25,6 +25,9 @@ pub fn (node &FnDecl) str(t &table.Table) string {
receiver = '($node.receiver.name $m$name) ' receiver = '($node.receiver.name $m$name) '
} }
name := node.name.after('.') name := node.name.after('.')
if node.is_c {
name = 'C.$name'
}
f.write('fn ${receiver}${name}(') f.write('fn ${receiver}${name}(')
for i, arg in node.args { for i, arg in node.args {
// skip receiver // skip receiver

View File

@ -173,9 +173,13 @@ fn (f mut Fmt) stmt(node ast.Stmt) {
s := it.str(f.table) s := it.str(f.table)
// f.write(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.write(s.replace(f.cur_mod + '.', '')) // `Expr` instead of `ast.Expr` in mod ast
f.writeln(' {') if !it.is_c {
f.stmts(it.stmts) f.writeln(' {')
f.writeln('}\n') f.stmts(it.stmts)
f.writeln('}\n')
} else {
f.writeln('\n')
}
} }
ast.ForInStmt { ast.ForInStmt {
f.write('for ') f.write('for ')

View File

@ -2,6 +2,7 @@ import (
os os
term term
benchmark benchmark
v.ast
v.fmt v.fmt
v.parser v.parser
v.table v.table
@ -44,7 +45,7 @@ fn test_fmt() {
continue continue
} }
table := table.new_table() 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) result_ocontent := fmt.fmt(file_ast, table)
if expected_ocontent != result_ocontent { if expected_ocontent != result_ocontent {
fmt_bench.fail() fmt_bench.fail()

View File

@ -1,3 +1,5 @@
fn C.func(arg int) int
fn fn_variadic(arg int, args ...string) { fn fn_variadic(arg int, args ...string) {
println('Do nothing') println('Do nothing')
} }

View File

@ -1,3 +1,5 @@
fn C.func(arg int) int
fn fn_variadic(arg int, args... string) { fn fn_variadic(arg int, args... string) {
println('Do nothing') println('Do nothing')
} }