Skip to content

Commit b0a905b

Browse files
committed
builder: error on implicit_function_declaration
1 parent e32aa73 commit b0a905b

2 files changed

Lines changed: 46 additions & 0 deletions

File tree

vlib/v/builder/cc.v

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -511,6 +511,14 @@ fn (v &Builder) only_compile_args(ccoptions CcompilerOptions) []string {
511511
all << '-Werror=implicit-function-declaration'
512512
}
513513
}
514+
} $else {
515+
// On non-Windows platforms, always add -Werror=implicit-function-declaration
516+
// to catch implicit function declarations at compile time
517+
if ccoptions.cc != .msvc {
518+
if !v.pref.is_cstrict {
519+
all << '-Werror=implicit-function-declaration'
520+
}
521+
}
514522
}
515523
all << ccoptions.pre_args
516524
all << ccoptions.source_args
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// Test for implicit C function declaration detection
2+
// This test verifies that V correctly catches implicit C function declarations
3+
4+
module main
5+
6+
import os
7+
8+
// `test_imp_c_function.v` filename can't contains `implicit`
9+
const test_file = os.join_path(os.vtmp_dir(), 'test_imp_c_function.v')
10+
11+
fn testsuite_begin() {
12+
os.rm(test_file) or {}
13+
// Test code that should fail due to implicit C function declaration
14+
test_code := 'module main
15+
fn C.custom_undeclared_function()
16+
fn main() {
17+
C.custom_undeclared_function()
18+
}
19+
'
20+
// Write the test code to the temporary file
21+
os.write_file(test_file, test_code) or {
22+
eprintln('FAIL: Unable to write test file')
23+
exit(1)
24+
}
25+
}
26+
27+
fn testsuite_end() {
28+
os.rm(test_file) or { eprintln('Warning: Unable to delete temporary file') }
29+
}
30+
31+
fn test_implicit_c_function_declaration() {
32+
result := os.execute('v ${test_file}')
33+
assert result.exit_code != 0
34+
println(result.output)
35+
assert result.output.contains('custom_undeclared_function')
36+
// C4013 is for msvc
37+
assert result.output.contains('implicit') || result.output.contains('C4013')
38+
}

0 commit comments

Comments
 (0)