Skip to content

Commit 217b191

Browse files
authored
builtin: support -d builtin_print_use_fprintf, make the C fn declarations stricter (#22137)
1 parent 515b106 commit 217b191

File tree

4 files changed

+44
-24
lines changed

4 files changed

+44
-24
lines changed

vlib/builtin/builtin.c.v

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,10 @@ pub fn eprintln(s string) {
181181
eprintln('eprintln(NIL)')
182182
return
183183
}
184+
$if builtin_print_use_fprintf ? {
185+
C.fprintf(C.stderr, c'%.*s\n', s.len, s.str)
186+
return
187+
}
184188
$if freestanding {
185189
// flushing is only a thing with C.FILE from stdio.h, not on the syscall level
186190
bare_eprint(s.str, u64(s.len))
@@ -205,6 +209,10 @@ pub fn eprint(s string) {
205209
eprint('eprint(NIL)')
206210
return
207211
}
212+
$if builtin_print_use_fprintf ? {
213+
C.fprintf(C.stderr, c'%.*s', s.len, s.str)
214+
return
215+
}
208216
$if freestanding {
209217
// flushing is only a thing with C.FILE from stdio.h, not on the syscall level
210218
bare_eprint(s.str, u64(s.len))
@@ -267,6 +275,10 @@ pub fn unbuffer_stdout() {
267275
// print prints a message to stdout. Note that unlike `eprint`, stdout is not automatically flushed.
268276
@[manualfree]
269277
pub fn print(s string) {
278+
$if builtin_print_use_fprintf ? {
279+
C.fprintf(C.stdout, c'%.*s', s.len, s.str)
280+
return
281+
}
270282
$if android && !termux {
271283
C.android_print(C.stdout, c'%.*s\n', s.len, s.str)
272284
} $else $if ios {
@@ -289,6 +301,10 @@ pub fn println(s string) {
289301
$if noprintln ? {
290302
return
291303
}
304+
$if builtin_print_use_fprintf ? {
305+
C.fprintf(C.stdout, c'%.*s\n', s.len, s.str)
306+
return
307+
}
292308
$if android && !termux {
293309
C.android_print(C.stdout, c'%.*s\n', s.len, s.str)
294310
return

vlib/builtin/builtin_nix.c.v

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@
44
module builtin
55

66
fn builtin_init() {
7-
$if !gc_warn_on_stderr ? {
8-
gc_set_warn_proc(internal_gc_warn_proc_none)
7+
$if gcboehm ? {
8+
$if !gc_warn_on_stderr ? {
9+
gc_set_warn_proc(internal_gc_warn_proc_none)
10+
}
911
}
1012
}
1113

vlib/builtin/builtin_windows.c.v

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,10 @@ const enable_wrap_at_eol_output = 2
4848
const evable_virtual_terminal_processing = 4
4949

5050
fn builtin_init() {
51-
$if !gc_warn_on_stderr ? {
52-
gc_set_warn_proc(internal_gc_warn_proc_none)
51+
$if gcboehm ? {
52+
$if !gc_warn_on_stderr ? {
53+
gc_set_warn_proc(internal_gc_warn_proc_none)
54+
}
5355
}
5456
g_original_codepage = C.GetConsoleOutputCP()
5557
C.SetConsoleOutputCP(cp_utf8)

vlib/builtin/cfns.c.v

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,8 @@ fn C.exit(code int)
2828

2929
fn C.qsort(base voidptr, items usize, item_size usize, cb C.qsort_callback_func)
3030

31-
fn C.sprintf(a ...voidptr) int
32-
3331
fn C.strlen(s &char) int
3432

35-
fn C.sscanf(&u8, &u8, ...&u8) int
36-
3733
@[trusted]
3834
fn C.isdigit(c int) bool
3935

@@ -43,21 +39,31 @@ fn C.popen(c &char, t &char) voidptr
4339
// <libproc.h>
4440
pub fn proc_pidpath(int, voidptr, int) int
4541

46-
fn C.realpath(&char, &char) &char
42+
fn C.realpath(const_path &char, resolved_path &char) &char
4743

4844
// fn C.chmod(byteptr, mode_t) int
49-
fn C.chmod(&char, u32) int
45+
fn C.chmod(path &char, mode u32) int
5046

51-
fn C.printf(&char, ...voidptr) int
47+
fn C.printf(const_format &char, opt ...voidptr) int
48+
fn C.dprintf(fd int, const_format &char, opt ...voidptr) int
49+
fn C.fprintf(fstream &C.FILE, const_format &char, opt ...voidptr) int
50+
fn C.sprintf(str &char, const_format &char, opt ...voidptr) int
51+
fn C.snprintf(str &char, size usize, const_format &char, opt ...voidptr) int
52+
fn C.wprintf(const_format &u16, opt ...voidptr) int
53+
54+
// used by Android for (e)println to output to the Android log system / logcat
55+
pub fn C.android_print(fstream voidptr, format &char, opt ...voidptr)
5256

53-
fn C.scanf(&char, ...voidptr) int
57+
fn C.sscanf(str &char, const_format &char, opt ...voidptr) int
58+
fn C.scanf(const_format &char, opt ...voidptr) int
5459

55-
fn C.puts(&char) int
60+
fn C.puts(msg &char) int
61+
@[trusted]
5662
fn C.abs(f64) f64
5763

58-
fn C.fputs(str &char, stream &C.FILE) int
64+
fn C.fputs(msg &char, fstream &C.FILE) int
5965

60-
fn C.fflush(&C.FILE) int
66+
fn C.fflush(fstream &C.FILE) int
6167

6268
// TODO: define args in these functions
6369
fn C.fseek(stream &C.FILE, offset int, whence int) int
@@ -81,8 +87,10 @@ fn C.strchr(s &char, c int) &char
8187
@[trusted]
8288
fn C.getpid() int
8389

90+
@[trusted]
8491
fn C.getuid() int
8592

93+
@[trusted]
8694
fn C.geteuid() int
8795

8896
fn C.system(cmd &char) int
@@ -203,10 +211,6 @@ fn C.strncmp(s &char, s2 &char, n int) int
203211
@[trusted]
204212
fn C.strerror(int) &char
205213

206-
fn C.snprintf(str &char, size usize, format &char, opt ...voidptr) int
207-
208-
fn C.fprintf(voidptr, &char, ...voidptr)
209-
210214
@[trusted]
211215
fn C.WIFEXITED(status int) bool
212216

@@ -295,8 +299,6 @@ fn C.GetConsoleMode(voidptr, &u32) bool
295299
@[trusted]
296300
fn C.GetCurrentProcessId() u32
297301

298-
fn C.wprintf()
299-
300302
// fn C.setbuf()
301303
fn C.setbuf(voidptr, &char)
302304

@@ -504,8 +506,6 @@ fn C.glTexImage2D()
504506
// used by ios for println
505507
fn C.WrappedNSLog(str &u8)
506508

507-
// used by Android for (e)println to output to the Android log system / logcat
508-
pub fn C.android_print(voidptr, &char, ...voidptr)
509-
510509
// absolute value
510+
@[trusted]
511511
fn C.abs(number int) int

0 commit comments

Comments
 (0)