Skip to content

Commit a9e9554

Browse files
authored
all: fix most C warnings (#6758)
1 parent 98e8894 commit a9e9554

File tree

16 files changed

+89
-90
lines changed

16 files changed

+89
-90
lines changed

vlib/builtin/builtin_nix.c.v

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,15 +88,15 @@ fn print_backtrace_skipping_top_frames_linux(skipframes int) bool {
8888
return false
8989
}
9090
buffer := [100]byteptr{}
91-
nr_ptrs := C.backtrace(buffer, 100)
91+
nr_ptrs := C.backtrace(voidptr(buffer), 100)
9292
if nr_ptrs < 2 {
9393
eprintln('C.backtrace returned less than 2 frames')
9494
return false
9595
}
9696
nr_actual_frames := nr_ptrs - skipframes
9797
mut sframes := []string{}
9898
//////csymbols := backtrace_symbols(*voidptr(&buffer[skipframes]), nr_actual_frames)
99-
csymbols := C.backtrace_symbols(&buffer[skipframes], nr_actual_frames)
99+
csymbols := C.backtrace_symbols(voidptr(&buffer[skipframes]), nr_actual_frames)
100100
for i in 0 .. nr_actual_frames {
101101
sframes << unsafe {tos2( byteptr(csymbols[i]) )}
102102
}

vlib/builtin/map.v

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ fn (d DenseArray) get(i int) voidptr {
153153
fn (mut d DenseArray) zeros_to_end() {
154154
mut tmp_value := malloc(d.value_bytes)
155155
mut count := u32(0)
156-
for i in 0 .. d.len {
156+
for i in 0 .. int(d.len) {
157157
if unsafe {d.keys[i]}.str != 0 {
158158
// swap keys
159159
unsafe {

vlib/builtin/option.v

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ struct OptionBase {
1919
ecode int
2020

2121
// Data is trailing after ecode
22-
// and is not included in here but in the
22+
// and is not included in here but in the
2323
// derived Option_xxx types
2424
}
2525

@@ -51,34 +51,18 @@ struct Option {
5151
is_none bool
5252
error string
5353
ecode int
54-
55-
data [400]byte
5654
}
5755

5856
pub fn (o Option) str() string {
5957
if o.ok && !o.is_none {
60-
return 'Option{ data: ' + o.data[0..32].hex() + ' }'
58+
return 'Option{ ok }'
6159
}
6260
if o.is_none {
6361
return 'Option{ none }'
6462
}
6563
return 'Option{ error: "${o.error}" }'
6664
}
6765

68-
// `fn foo() ?Foo { return foo }` => `fn foo() ?Foo { return opt_ok(foo); }`
69-
fn opt_ok(data voidptr, size int) Option {
70-
if size >= 400 {
71-
panic('option size too big: $size (max is 400), this is a temporary limit')
72-
}
73-
res := Option{
74-
ok: true
75-
}
76-
unsafe {
77-
C.memcpy(res.data, data, size)
78-
}
79-
return res
80-
}
81-
8266
// used internally when returning `none`
8367
fn opt_none() Option {
8468
return Option{

vlib/os/file.v

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ pub fn (mut f File) get_line() ?string {
142142
mut zblen := size_t(0)
143143
mut zx := 0
144144
unsafe {
145-
zx = C.getline(&zbuf, &zblen, f.cfile)
145+
zx = C.getline(&charptr(&zbuf), &zblen, f.cfile)
146146
if zx == -1 {
147147
C.free(zbuf)
148148
if C.errno == 0 {
@@ -160,7 +160,7 @@ pub fn (mut f File) get_line() ?string {
160160
//
161161
buf := [4096]byte{}
162162
mut res := strings.new_builder(1024)
163-
mut x := 0
163+
mut x := charptr(0)
164164
for {
165165
unsafe {
166166
x = C.fgets(charptr(buf), 4096, f.cfile)

vlib/os/os_nix.c.v

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,7 @@ pub fn is_writable_folder(folder string) ?bool {
252252
}
253253
tmp_perm_check := os.join_path(folder, 'XXXXXX')
254254
unsafe {
255-
x := C.mkstemp(tmp_perm_check.str)
255+
x := C.mkstemp(charptr(tmp_perm_check.str))
256256
if -1 == x {
257257
return error('folder `$folder` is not writable')
258258
}

vlib/strconv/format.v

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -439,7 +439,6 @@ pub fn v_sprintf(str string, pt ... voidptr) string{
439439
mut len1 := -1 // decimal part for floats
440440
def_len1 := 6 // default value for len1
441441
mut pad_ch := byte(` `) // pad char
442-
mut th_separator := false // thousands separator flag
443442

444443
// prefix chars for Length field
445444
mut ch1 := `0` // +1 char if present else `0`
@@ -453,7 +452,6 @@ pub fn v_sprintf(str string, pt ... voidptr) string{
453452
len0 = -1
454453
len1 = -1
455454
pad_ch = ` `
456-
th_separator = false
457455
status = .norm_char
458456
ch1 = `0`
459457
ch2 = `0`
@@ -518,7 +516,6 @@ pub fn v_sprintf(str string, pt ... voidptr) string{
518516
i++
519517
continue
520518
} else if ch == `'` {
521-
th_separator = true
522519
i++
523520
continue
524521
} else if ch == `.` && fc_ch1 >= `1` && fc_ch1 <= `9` {

vlib/time/parse.v

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ pub fn parse_iso8601(s string) ?Time {
6666
offset_hour := 0
6767
offset_min := 0
6868
count := unsafe {C.sscanf(charptr(s.str), '%4d-%2d-%2d%c%2d:%2d:%2d.%6d%c%2d:%2d',
69-
&year, &month, &day, &time_char, &hour, &minute, &second, &mic_second, &plus_min, &offset_hour,
69+
&year, &month, &day, charptr(&time_char), &hour, &minute, &second, &mic_second, charptr(&plus_min), &offset_hour,
7070
&offset_min)}
7171
if count != 11 {
7272
return error('Invalid 8601 format')

vlib/v/builder/cc.v

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,12 @@ fn (mut v Builder) find_win_cc() ? {
7676
v.pref.ccompiler_type = pref.cc_from_string(v.pref.ccompiler)
7777
}
7878

79+
fn (mut v Builder) show_c_compiler_output(res os.Result) {
80+
println('======== C Compiler output ========')
81+
println(res.output)
82+
println('=================================')
83+
}
84+
7985
fn (mut v Builder) post_process_c_compiler_output(res os.Result) {
8086
if res.exit_code == 0 {
8187
if v.pref.reuse_tmpc {
@@ -545,6 +551,9 @@ fn (mut v Builder) cc() {
545551
}
546552
diff := time.ticks() - ticks
547553
v.timing_message('C ${ccompiler:3}', diff)
554+
if v.pref.show_c_output {
555+
v.show_c_compiler_output(res)
556+
}
548557
if res.exit_code == 127 {
549558
// the command could not be found by the system
550559
$if linux {
@@ -559,7 +568,9 @@ fn (mut v Builder) cc() {
559568
'-----------------------------------------------------------\n' + 'Probably your C compiler is missing. \n' +
560569
'Please reinstall it, or make it available in your PATH.\n\n' + missing_compiler_info())
561570
}
562-
v.post_process_c_compiler_output(res)
571+
if !v.pref.show_c_output {
572+
v.post_process_c_compiler_output(res)
573+
}
563574
// Print the C command
564575
if v.pref.is_verbose {
565576
println('$ccompiler took $diff ms')

vlib/v/builder/msvc.v

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,11 @@ pub fn (mut v Builder) cc_msvc() {
303303
}
304304
diff := time.ticks() - ticks
305305
v.timing_message('C msvc', diff)
306-
v.post_process_c_compiler_output(res)
306+
if v.pref.show_c_output {
307+
v.show_c_compiler_output(res)
308+
} else {
309+
v.post_process_c_compiler_output(res)
310+
}
307311
// println(res)
308312
// println('C OUTPUT:')
309313
// Always remove the object file - it is completely unnecessary

vlib/v/gen/cgen.v

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1440,7 +1440,6 @@ fn (mut g Gen) gen_assign_stmt(assign_stmt ast.AssignStmt) {
14401440
// return;
14411441
// }
14421442
// int pos = *(int*)_t190.data;
1443-
mut gen_or := false
14441443
mut tmp_opt := ''
14451444
is_optional := g.pref.autofree &&
14461445
(assign_stmt.op in [.decl_assign, .assign]) && assign_stmt.left_types.len == 1 && assign_stmt.right[0] is
@@ -1453,7 +1452,6 @@ fn (mut g Gen) gen_assign_stmt(assign_stmt ast.AssignStmt) {
14531452
tmp_opt = g.new_tmp_var()
14541453
g.write('/*AF opt*/$styp $tmp_opt = ')
14551454
g.expr(assign_stmt.right[0])
1456-
gen_or = true
14571455
g.or_block(tmp_opt, call_expr.or_block, call_expr.return_type)
14581456
g.writeln('/*=============ret*/')
14591457
// if af && is_optional {
@@ -2128,7 +2126,11 @@ fn (mut g Gen) expr(node ast.Expr) {
21282126
g.write('))')
21292127
}
21302128
ast.CharLiteral {
2131-
g.write("'$node.val'")
2129+
if node.val == r'\`' {
2130+
g.write("'`'")
2131+
} else {
2132+
g.write("'$node.val'")
2133+
}
21322134
}
21332135
ast.AtExpr {
21342136
g.comp_at(node)
@@ -4662,7 +4664,7 @@ fn (mut g Gen) gen_array_sort(node ast.CallExpr) {
46624664
g.expr(node.left)
46634665
g.write('${deref}len, ')
46644666
g.expr(node.left)
4665-
g.writeln('${deref}element_size, $compare_fn);')
4667+
g.writeln('${deref}element_size, (int (*)(const void *, const void *))&$compare_fn);')
46664668
}
46674669

46684670
// `nums.filter(it % 2 == 0)`
@@ -5060,7 +5062,15 @@ fn c_name(name_ string) string {
50605062
return name
50615063
}
50625064

5063-
fn (mut g Gen) type_default(typ table.Type) string {
5065+
fn (mut g Gen) type_default(typ_ table.Type) string {
5066+
typ := g.unwrap_generic(typ_)
5067+
if typ.has_flag(.optional) {
5068+
return '{0}'
5069+
}
5070+
// Always set pointers to 0
5071+
if typ.is_ptr() {
5072+
return '0'
5073+
}
50645074
sym := g.table.get_type_symbol(typ)
50655075
if sym.kind == .array {
50665076
elem_sym := g.typ(sym.array_info().elem_type)
@@ -5074,10 +5084,6 @@ fn (mut g Gen) type_default(typ table.Type) string {
50745084
value_type_str := g.typ(sym.map_info().value_type)
50755085
return 'new_map_1(sizeof($value_type_str))'
50765086
}
5077-
// Always set pointers to 0
5078-
if typ.is_ptr() {
5079-
return '0'
5080-
}
50815087
// User struct defined in another module.
50825088
// if typ.contains('__') {
50835089
if sym.kind == .struct_ {
@@ -5105,7 +5111,8 @@ fn (mut g Gen) type_default(typ table.Type) string {
51055111
else {}
51065112
}
51075113
return match sym.kind {
5108-
.interface_, .sum_type, .array_fixed { '{0}' }
5114+
.interface_, .sum_type, .array_fixed, .multi_return { '{0}' }
5115+
.alias { g.type_default((sym.info as table.Alias).parent_type) }
51095116
else { '0' }
51105117
}
51115118
// TODO this results in

0 commit comments

Comments
 (0)