Skip to content

Commit b826062

Browse files
authored
vfmt: add support for 64bit int with -new_int (part 2) (#25298)
1 parent c221b32 commit b826062

13 files changed

Lines changed: 272 additions & 39 deletions

‎cmd/tools/vfmt.v‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ struct FormatOptions {
2727
is_worker bool // true *only* in the worker processes. Note: workers can crash.
2828
is_backup bool // make a `file.v.bak` copy *before* overwriting a `file.v` in place with `-w`
2929
in_process bool // do not fork a worker process; potentially faster, but more prone to crashes for invalid files
30+
is_new_int bool // Forcefully cast the `int` type in @[translated] modules or in the definition of `C.func` to the `i32` type.
3031
mut:
3132
diff_cmd string // filled in when -diff or -verify is passed
3233
}
@@ -55,6 +56,7 @@ fn main() {
5556
is_verify: '-verify' in args
5657
is_backup: '-backup' in args
5758
in_process: '-inprocess' in args
59+
is_new_int: '-new_int' in args
5860
}
5961
if term_colors {
6062
os.setenv('VCOLORS', 'always', true)
@@ -184,6 +186,7 @@ fn (foptions &FormatOptions) vlog(msg string) {
184186
fn (foptions &FormatOptions) formated_content_from_file(prefs &pref.Preferences, file string) string {
185187
mut table := ast.new_table()
186188
file_ast := parser.parse_file(file, mut table, .parse_comments, prefs)
189+
table.new_int = foptions.is_new_int
187190
formated_content := fmt.fmt(file_ast, mut table, prefs, foptions.is_debug)
188191
return formated_content
189192
}
@@ -202,6 +205,7 @@ fn (foptions &FormatOptions) format_file(file string) {
202205
prefs, mut table := setup_preferences_and_table()
203206
file_ast := parser.parse_file(file, mut table, .parse_comments, prefs)
204207
// checker.new_checker(table, prefs).check(file_ast)
208+
table.new_int = foptions.is_new_int
205209
formatted_content := fmt.fmt(file_ast, mut table, prefs, foptions.is_debug)
206210
os.write_file(vfmt_output_path, formatted_content) or { panic(err) }
207211
foptions.vlog('fmt.fmt worked and ${formatted_content.len} bytes were written to ${vfmt_output_path} .')
@@ -214,6 +218,7 @@ fn (foptions &FormatOptions) format_pipe() {
214218
input_text := os.get_raw_lines_joined()
215219
file_ast := parser.parse_text(input_text, '', mut table, .parse_comments, prefs)
216220
// checker.new_checker(table, prefs).check(file_ast)
221+
table.new_int = foptions.is_new_int
217222
formatted_content := fmt.fmt(file_ast, mut table, prefs, foptions.is_debug,
218223
source_text: input_text
219224
)

‎vlib/v/ast/str.v‎

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,9 @@ pub fn (t &Table) stringify_fn_decl(node &FnDecl, cur_mod string, m2a map[string
119119
}
120120
f.write_string(node.receiver.name + ' ')
121121
styp = util.no_cur_mod(styp, cur_mod)
122+
if t.new_int_fmt_fix && styp == 'int' {
123+
styp = 'i32'
124+
}
122125
f.write_string(styp + ') ')
123126
} else if node.is_static_type_method {
124127
mut styp := util.no_cur_mod(t.type_to_code(node.receiver.typ.clear_flag(.shared_f)),
@@ -176,6 +179,7 @@ fn (t &Table) stringify_fn_after_name(node &FnDecl, mut f strings.Builder, cur_m
176179
if param.is_hidden {
177180
continue
178181
}
182+
param_typ := if t.new_int_fmt_fix && param.typ == int_type { i32_type } else { param.typ }
179183
is_last_param := i == node.params.len - 1
180184
is_type_only := param.name == ''
181185
if param.on_newline {
@@ -187,19 +191,24 @@ fn (t &Table) stringify_fn_after_name(node &FnDecl, mut f strings.Builder, cur_m
187191
f.write_string(' ')
188192
}
189193
if param.is_mut {
190-
f.write_string(param.typ.share().str() + ' ')
194+
f.write_string(param_typ.share().str() + ' ')
191195
}
192196
f.write_string(param.name)
193-
param_sym := t.sym(param.typ)
197+
param_sym := t.sym(param_typ)
194198
if param_sym.info is Struct && param_sym.info.is_anon {
195-
if param.typ.has_flag(.option) {
199+
if param_typ.has_flag(.option) {
196200
f.write_string(' ?')
197201
} else {
198202
f.write_string(' ')
199203
}
200204
f.write_string('struct {')
201205
for field in param_sym.info.fields {
202-
f.write_string(' ${field.name} ${t.type_to_str(field.typ)}')
206+
field_typ := if t.new_int_fmt_fix && field.typ == int_type {
207+
i32_type
208+
} else {
209+
field.typ
210+
}
211+
f.write_string(' ${field.name} ${t.type_to_str(field_typ)}')
203212
if field.has_default_expr {
204213
f.write_string(' = ${field.default_expr}')
205214
}
@@ -209,14 +218,14 @@ fn (t &Table) stringify_fn_after_name(node &FnDecl, mut f strings.Builder, cur_m
209218
}
210219
f.write_string('}')
211220
} else {
212-
mut s := t.type_to_str(param.typ.clear_flag(.shared_f))
221+
mut s := t.type_to_str(param_typ.clear_flag(.shared_f))
213222
if param.is_mut {
214223
if s.starts_with('&') && ((!param_sym.is_number() && param_sym.kind != .bool)
215224
|| node.language != .v
216-
|| (param.typ.is_ptr() && param_sym.kind == .struct)) {
225+
|| (param_typ.is_ptr() && param_sym.kind == .struct)) {
217226
s = s[1..]
218-
} else if param.typ.is_ptr() && param_sym.kind == .struct && !s.contains('[') {
219-
s = t.type_to_str(param.typ.clear_flag(.shared_f).deref())
227+
} else if param_typ.is_ptr() && param_sym.kind == .struct && !s.contains('[') {
228+
s = t.type_to_str(param_typ.clear_flag(.shared_f).deref())
220229
}
221230
}
222231
s = util.no_cur_mod(s, cur_mod)
@@ -239,7 +248,12 @@ fn (t &Table) stringify_fn_after_name(node &FnDecl, mut f strings.Builder, cur_m
239248
}
240249
f.write_string(')')
241250
if node.return_type != void_type {
242-
sreturn_type := util.no_cur_mod(t.type_to_str(node.return_type), cur_mod)
251+
return_type := if t.new_int_fmt_fix && node.return_type == int_type {
252+
i32_type
253+
} else {
254+
node.return_type
255+
}
256+
sreturn_type := util.no_cur_mod(t.type_to_str(return_type), cur_mod)
243257
short_sreturn_type := shorten_full_name_based_on_aliases(sreturn_type, m2a)
244258
f.write_string(' ${short_sreturn_type}')
245259
}

‎vlib/v/ast/table.v‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,8 @@ pub mut:
9999
anon_union_names map[string]int // anon union name -> union sym idx
100100
anon_union_counter int
101101
comptime_is_true map[string]ComptTimeCondResult // The evaluate cond results for different generic types combination, such as `comptime_is_true['T=int,X=string|main.v|pos ...'] = {true, '!DEFINED(WINDOWS)'}`
102+
new_int bool // use 64bit/32bit platform dependent `int`
103+
new_int_fmt_fix bool // vfmt will fix `int` to `i32`
102104
}
103105

104106
pub struct ComptTimeCondResult {

‎vlib/v/fmt/fm_new_int_test.v‎

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
// Copyright (c) 2019-2024 Alexander Medvednikov. All rights reserved.
2+
// Use of this source code is governed by an MIT license
3+
// that can be found in the LICENSE file.
4+
import os
5+
import term
6+
import benchmark
7+
import v.ast
8+
import v.fmt
9+
import v.parser
10+
import v.pref
11+
import v.util.diff
12+
import v.util.vtest
13+
14+
const vroot = @VEXEROOT
15+
const tdir = os.join_path(vroot, 'vlib', 'v', 'fmt')
16+
const fpref = &pref.Preferences{
17+
is_fmt: true
18+
}
19+
20+
fn run_fmt(mut input_files []string) {
21+
fmt_message := 'vfmt new_int tests'
22+
eprintln(term.header(fmt_message, '-'))
23+
tmpfolder := os.temp_dir()
24+
assert input_files.len > 0
25+
input_files = vtest.filter_vtest_only(input_files)
26+
if input_files.len == 0 {
27+
// No need to produce a failing test here.
28+
eprintln('no tests found with VTEST_ONLY filter set to: ' + os.getenv('VTEST_ONLY'))
29+
exit(0)
30+
}
31+
mut fmt_bench := benchmark.new_benchmark()
32+
fmt_bench.set_total_expected_steps(input_files.len)
33+
for istep, ipath in input_files {
34+
fmt_bench.cstep = istep
35+
fmt_bench.step()
36+
opath := ipath.replace('_input.vv', '_expected_new_int.vv')
37+
if !os.exists(opath) {
38+
// skip not exist files
39+
continue
40+
}
41+
expected_ocontent := os.read_file(opath) or {
42+
fmt_bench.fail()
43+
eprintln(fmt_bench.step_message_fail('cannot read from ${opath}'))
44+
continue
45+
}
46+
mut table := ast.new_table()
47+
file_ast := parser.parse_file(ipath, mut table, .parse_comments, fpref)
48+
table.new_int = true
49+
result_ocontent := fmt.fmt(file_ast, mut table, fpref, false)
50+
if expected_ocontent != result_ocontent {
51+
fmt_bench.fail()
52+
eprintln(fmt_bench.step_message_fail('file ${ipath} after formatting, does not look as expected.'))
53+
vfmt_result_file := os.join_path(tmpfolder, 'vfmt_run_over_${os.file_name(ipath)}')
54+
os.write_file(vfmt_result_file, result_ocontent) or { panic(err) }
55+
println(diff.compare_files(opath, vfmt_result_file) or { err.msg() })
56+
continue
57+
}
58+
fmt_bench.ok()
59+
eprintln(fmt_bench.step_message_ok(ipath))
60+
}
61+
fmt_bench.stop()
62+
eprintln(term.h_divider('-'))
63+
eprintln(fmt_bench.total_message(fmt_message))
64+
assert fmt_bench.nfail == 0
65+
}
66+
67+
fn test_new_int_fmt() {
68+
mut input_files := os.walk_ext(os.join_path(tdir, 'tests'), '_input.vv')
69+
run_fmt(mut input_files)
70+
}

0 commit comments

Comments
 (0)