From 28bfd02af512f572aaa1255332ca95bf363da05e Mon Sep 17 00:00:00 2001 From: Alexander Medvednikov Date: Wed, 25 Mar 2026 16:42:24 +0300 Subject: [PATCH] cgen: fix compilation with -freestanding missing main (fixes #20227) --- vlib/builtin/linux_bare/libc_impl.v | 2 +- vlib/v/gen/c/fn.v | 17 ++++++++++++++++- ...freestanding_memcpy_declarations.c.must_have | 6 ++++++ .../freestanding_memcpy_declarations.vv | 2 ++ 4 files changed, 25 insertions(+), 2 deletions(-) create mode 100644 vlib/v/gen/c/testdata/freestanding_memcpy_declarations.c.must_have create mode 100644 vlib/v/gen/c/testdata/freestanding_memcpy_declarations.vv diff --git a/vlib/builtin/linux_bare/libc_impl.v b/vlib/builtin/linux_bare/libc_impl.v index 7a1cc2a4e..3054a44fa 100644 --- a/vlib/builtin/linux_bare/libc_impl.v +++ b/vlib/builtin/linux_bare/libc_impl.v @@ -25,7 +25,7 @@ fn __malloc(n usize) voidptr { @[export: 'strlen'] @[unsafe] -fn strlen(const_s &char) usize { +fn strlen(const_s voidptr) usize { s := unsafe { &u8(const_s) } mut i := 0 for ; unsafe { s[i] } != 0; i++ {} diff --git a/vlib/v/gen/c/fn.v b/vlib/v/gen/c/fn.v index 568a78f5e..5ee0e8923 100644 --- a/vlib/v/gen/c/fn.v +++ b/vlib/v/gen/c/fn.v @@ -1505,7 +1505,12 @@ fn (mut g Gen) fn_decl_params(params []ast.Param, scope &ast.Scope, is_variadic } else { '' } - s := '${const_prefix}${param_type_name} ${var_name_prefix}${caname}' + const_param_type_name := if const_prefix != '' { + g.const_pointer_param_type_name(typ, param_type_name) + } else { + param_type_name + } + s := '${const_prefix}${const_param_type_name} ${var_name_prefix}${caname}' if !g.inside_c_extern { g.write(s) } @@ -1530,6 +1535,16 @@ fn (mut g Gen) fn_decl_params(params []ast.Param, scope &ast.Scope, is_variadic return fparams, fparamtypes, heap_promoted } +fn (mut g Gen) const_pointer_param_type_name(typ ast.Type, param_type_name string) string { + unwrapped_typ := g.unwrap_generic(typ) + return match unwrapped_typ { + ast.voidptr_type { 'void*' } + ast.byteptr_type { 'u8*' } + ast.charptr_type { 'char*' } + else { param_type_name } + } +} + fn (mut g Gen) get_anon_fn_type_name(mut node ast.AnonFn, var_name string) string { mut builder := strings.new_builder(64) return_styp := g.styp(node.decl.return_type) diff --git a/vlib/v/gen/c/testdata/freestanding_memcpy_declarations.c.must_have b/vlib/v/gen/c/testdata/freestanding_memcpy_declarations.c.must_have new file mode 100644 index 000000000..dc0952adb --- /dev/null +++ b/vlib/v/gen/c/testdata/freestanding_memcpy_declarations.c.must_have @@ -0,0 +1,6 @@ +void *memcpy(void *dest, const void *src, size_t n); +void *memmove(void *dest, const void *src, size_t n); +VV_EXP voidptr memcpy(voidptr dest, const void* const_src, usize n); +VV_EXP voidptr memmove(voidptr dest, const void* const_src, usize n); +VV_EXP int memcmp(const void* const_a, const void* const_b, usize n); +VV_EXP usize strlen(const void* const_s); diff --git a/vlib/v/gen/c/testdata/freestanding_memcpy_declarations.vv b/vlib/v/gen/c/testdata/freestanding_memcpy_declarations.vv new file mode 100644 index 000000000..e59812a3b --- /dev/null +++ b/vlib/v/gen/c/testdata/freestanding_memcpy_declarations.vv @@ -0,0 +1,2 @@ +// vtest vflags: -freestanding +fn main() {} -- 2.39.5