Skip to content

Commit

Permalink
cgen: cleanup extra whitespaces and unneeded comments (#23122)
Browse files Browse the repository at this point in the history
  • Loading branch information
felipensp authored Dec 10, 2024
1 parent 7d47219 commit 6db0a8b
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 34 deletions.
72 changes: 48 additions & 24 deletions vlib/v/gen/c/cgen.v
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,7 @@ pub fn gen(files []&ast.File, mut table ast.Table, pref_ &pref.Preferences) (str
is_cc_msvc: pref_.ccompiler == 'msvc'
use_segfault_handler: !('no_segfault_handler' in pref_.compile_defines
|| pref_.os in [.wasm32, .wasm32_emscripten])
static_modifier: if pref_.parallel_cc { 'static' } else { '' }
static_modifier: if pref_.parallel_cc { 'static ' } else { '' }
has_reflection: 'v.reflection' in table.modules
has_debugger: 'v.debug' in table.modules
reflection_strings: &reflection_strings
Expand Down Expand Up @@ -584,16 +584,34 @@ pub fn gen(files []&ast.File, mut table ast.Table, pref_ &pref.Preferences) (str
}
}
}
b.write_string2('\n// Enum definitions:\n', g.enum_typedefs.str())
b.write_string2('\n// Thread definitions:\n', g.thread_definitions.str())
b.write_string2('\n// V type definitions:\n', g.type_definitions.str())
b.write_string2('\n// V alias definitions:\n', g.alias_definitions.str())
b.write_string2('\n// V shared types:\n', g.shared_types.str())
b.write_string2('\n// V Option_xxx definitions:\n', g.out_options.str())
b.write_string2('\n// V result_xxx definitions:\n', g.out_results.str())
b.write_string2('\n// V json forward decls:\n', g.json_forward_decls.str())
if g.enum_typedefs.len > 0 {
b.write_string2('\n// Enum definitions:\n', g.enum_typedefs.str())
}
if g.thread_definitions.len > 0 {
b.write_string2('\n// Thread definitions:\n', g.thread_definitions.str())
}
if g.type_definitions.len > 0 {
b.write_string2('\n// V type definitions:\n', g.type_definitions.str())
}
if g.alias_definitions.len > 0 {
b.write_string2('\n// V alias definitions:\n', g.alias_definitions.str())
}
if g.shared_types.len > 0 {
b.write_string2('\n// V shared types:\n', g.shared_types.str())
}
if g.out_options.len > 0 {
b.write_string2('\n// V Option_xxx definitions:\n', g.out_options.str())
}
if g.out_results.len > 0 {
b.write_string2('\n// V result_xxx definitions:\n', g.out_results.str())
}
if g.json_forward_decls.len > 0 {
b.write_string2('\n// V json forward decls:\n', g.json_forward_decls.str())
}
b.write_string2('\n// V definitions:\n', g.definitions.str())
b.write_string2('\n// V sort fn definitions:\n', g.sort_fn_definitions.str())
if g.sort_fn_definitions.len > 0 {
b.write_string2('\n// V sort fn definitions:\n', g.sort_fn_definitions.str())
}
b.writeln('\n// V global/const non-precomputed definitions:')
for var_name in g.sorted_global_const_names {
if var := g.global_const_defs[var_name] {
Expand Down Expand Up @@ -1777,14 +1795,16 @@ pub fn (mut g Gen) write_fn_typesymbol_declaration(sym ast.TypeSymbol) {
}

pub fn (mut g Gen) write_array_fixed_return_types() {
fixed_arr_rets := g.table.type_symbols.filter(it.info is ast.ArrayFixed && it.info.is_fn_ret
&& !it.info.elem_type.has_flag(.generic))
if fixed_arr_rets.len == 0 {
return
}

g.typedefs.writeln('\n// BEGIN_array_fixed_return_typedefs')
g.type_definitions.writeln('\n// BEGIN_array_fixed_return_structs')

for sym in g.table.type_symbols {
if sym.kind != .array_fixed || (sym.info as ast.ArrayFixed).elem_type.has_flag(.generic)
|| !(sym.info as ast.ArrayFixed).is_fn_ret {
continue
}
for sym in fixed_arr_rets {
info := sym.info as ast.ArrayFixed
mut fixed_elem_name := g.styp(info.elem_type.set_nr_muls(0))
if info.elem_type.is_ptr() {
Expand Down Expand Up @@ -6342,7 +6362,7 @@ fn (mut g Gen) const_decl_write_precomputed(mod string, styp string, cname strin
}
g.global_const_defs[util.no_dots(field_name)] = GlobalConstDef{
mod: mod
def: '${g.static_modifier} const ${styp} ${cname} = ${ct_value}; // precomputed2'
def: '${g.static_modifier}const ${styp} ${cname} = ${ct_value}; // precomputed2'
// is_precomputed: true
}
}
Expand Down Expand Up @@ -6473,7 +6493,7 @@ fn (mut g Gen) global_decl(node ast.GlobalDecl) {
&& !util.should_bundle_module(node.mod) {
'extern '
} else {
'${g.static_modifier} ' // TODO: used to be '' before parallel_cc, may cause issues
g.static_modifier // TODO: used to be '' before parallel_cc, may cause issues
}
// should the global be initialized now, not later in `vinit()`
cinit := node.attrs.contains('cinit')
Expand Down Expand Up @@ -6524,7 +6544,7 @@ fn (mut g Gen) global_decl(node ast.GlobalDecl) {
mut init := ''
extern := if cextern { 'extern ' } else { '' }
modifier := if field.is_volatile { ' volatile ' } else { '' }
def_builder.write_string('${extern}${visibility_kw}${modifier}${styp} ${attributes} ${field.name}')
def_builder.write_string('${extern}${visibility_kw}${modifier}${styp} ${attributes}${field.name}')
if cextern {
def_builder.writeln('; // global5')
g.global_const_defs[util.no_dots(field.name)] = GlobalConstDef{
Expand Down Expand Up @@ -7951,12 +7971,12 @@ fn (mut g Gen) interface_table() string {
iname_table_length := inter_info.types.len
if iname_table_length == 0 {
// msvc can not process `static struct x[0] = {};`
methods_struct.writeln('${g.static_modifier} ${methods_struct_name} ${interface_name}_name_table[1];')
methods_struct.writeln('${g.static_modifier}${methods_struct_name} ${interface_name}_name_table[1];')
} else {
if g.pref.build_mode != .build_module {
methods_struct.writeln('${g.static_modifier} ${methods_struct_name} ${interface_name}_name_table[${iname_table_length}] = {')
methods_struct.writeln('${g.static_modifier}${methods_struct_name} ${interface_name}_name_table[${iname_table_length}] = {')
} else {
methods_struct.writeln('${g.static_modifier} ${methods_struct_name} ${interface_name}_name_table[${iname_table_length}];')
methods_struct.writeln('${g.static_modifier}${methods_struct_name} ${interface_name}_name_table[${iname_table_length}];')
}
}
mut cast_functions := strings.new_builder(100)
Expand Down Expand Up @@ -8211,7 +8231,7 @@ static inline __shared__${interface_name} ${shared_fn_name}(__shared__${cctype}*
}
iin_idx := already_generated_mwrappers[interface_index_name] - iinidx_minimum_base
if g.pref.build_mode != .build_module {
sb.writeln('${g.static_modifier} const int ${interface_index_name} = ${iin_idx};')
sb.writeln('${g.static_modifier}const int ${interface_index_name} = ${iin_idx};')
} else {
sb.writeln('extern const int ${interface_index_name};')
}
Expand Down Expand Up @@ -8262,9 +8282,13 @@ static inline __shared__${interface_name} ${shared_fn_name}(__shared__${cctype}*
sb.writeln2(methods_wrapper.str(), methods_struct_def.str())
sb.writeln(methods_struct.str())
}
sb.writeln(cast_functions.str())
if cast_functions.len > 0 {
sb.writeln(cast_functions.str())
}
}
if conversion_functions.len > 0 {
sb.writeln(conversion_functions.str())
}
sb.writeln(conversion_functions.str())
return sb.str()
}

Expand Down
6 changes: 4 additions & 2 deletions vlib/v/gen/c/dumpexpr.v
Original file line number Diff line number Diff line change
Expand Up @@ -258,8 +258,10 @@ fn (mut g Gen) dump_expr_definitions() {
for tdef, _ in dump_typedefs {
g.definitions.writeln(tdef)
}
g.definitions.writeln(dump_fn_defs.str())
g.dump_funcs.writeln(dump_fns.str())
if dump_fn_defs.len > 0 {
g.definitions.writeln(dump_fn_defs.str())
g.dump_funcs.writeln(dump_fns.str())
}
}

fn (mut g Gen) writeln_fn_header(s string, mut sb strings.Builder) bool {
Expand Down
2 changes: 1 addition & 1 deletion vlib/v/gen/c/testdata/global_export_nix.c.must_have
Original file line number Diff line number Diff line change
@@ -1 +1 @@
string VV_EXPORTED_SYMBOL global_exported = _SLIT("barqux");
string VV_EXPORTED_SYMBOL global_exported = _SLIT("barqux");
2 changes: 1 addition & 1 deletion vlib/v/gen/c/testdata/global_initializer_nix.c.must_have
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
Array_fixed_main__Foo_3 g_test_foo = {
Array_fixed_main__Foo_3 g_test_foo = {
.foo = 0,.bar = {.a = 0,.b = 0,}
2 changes: 1 addition & 1 deletion vlib/v/gen/c/testdata/global_initializer_windows.must_have
Original file line number Diff line number Diff line change
@@ -1 +1 @@
Array_fixed_main__Foo_3 g_test_foo = {(main__Foo){.foo = 0,.bar = (main___VAnonStruct1){.a = 0,.b = 0,},}, (main__Foo){.foo = 0,.bar = (main___VAnonStruct1){.a = 0,.b = 0,},}, (main__Foo){.foo = 0,.bar = (main___VAnonStruct1){.a = 0,.b = 0,},}}; // global4
Array_fixed_main__Foo_3 g_test_foo = {(main__Foo){.foo = 0,.bar = (main___VAnonStruct1){.a = 0,.b = 0,},}, (main__Foo){.foo = 0,.bar = (main___VAnonStruct1){.a = 0,.b = 0,},}, (main__Foo){.foo = 0,.bar = (main___VAnonStruct1){.a = 0,.b = 0,},}}; // global4
8 changes: 4 additions & 4 deletions vlib/v/gen/c/testdata/globals_with_weak_tag.c.must_have
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
u64 VWEAK abc = ((u64)(1U)); // global
u64 xyz = ((u64)(2U)); // global
u64 VWEAK weak_1 = ((u64)(4U)); // global
u64 VWEAK weak_2 = ((u64)(5U)); // global
u64 VWEAK abc = ((u64)(1U)); // global
u64 xyz = ((u64)(2U)); // global
u64 VWEAK weak_1 = ((u64)(4U)); // global
u64 VWEAK weak_2 = ((u64)(5U)); // global
VV_LOCAL_SYMBOL int main__a_weak_function(void);
VV_LOCAL_SYMBOL void main__main(void);

Expand Down
2 changes: 1 addition & 1 deletion vlib/v/gen/c/testdata/translated_module.c.must_have
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
int ExternalSymbol(char* );
int a = ExternalSymbol("hello");
extern struct my_struct my_instance;
extern struct my_struct my_instance;

0 comments on commit 6db0a8b

Please sign in to comment.