Skip to content

Commit

Permalink
fix bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
spytheman committed Nov 23, 2024
1 parent f017fec commit 1106ca9
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 18 deletions.
2 changes: 1 addition & 1 deletion command_test.v
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import os

fn testsuite_begin() {
os.chdir(os.dir(@FILE))!
os.chdir(os.dir(@FILE))!
}

fn test_run_tests() {
Expand Down
20 changes: 13 additions & 7 deletions src/c2v.v
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,8 @@ mut:
files []string // all files' names used in current file, include header files' names
used_fn datatypes.Set[string] // used fn in current .c file
used_global datatypes.Set[string] // used global in current .c file
seen_ids map[string]&Node
generated_declarations map[string]bool // prevent duplicate generations
}

fn empty_toml_doc() toml.Doc {
Expand Down Expand Up @@ -2510,15 +2512,19 @@ fn (mut c2v C2V) translate_file(path string) {
c_file := path
c2v.add_file(ast_path, out_v, c_file)

// preparation pass, fill in the Node redeclarations field:
mut seen_ids := map[string]&Node{}
// preparation pass, fill all seen_ids ...
c2v.seen_ids = {}
for i, mut node in c2v.tree.inner {
c2v.node_i = i
seen_ids[node.id] = unsafe { node }
if node.previous_declaration != '' {
if mut pnode := seen_ids[node.previous_declaration] {
pnode.redeclarations_count++
}
c2v.seen_ids[node.id] = unsafe { node }
}
// preparation pass part 2, fill in the Node redeclarations field, based on *all* seen nodes
for _, mut node in c2v.tree.inner {
if node.previous_declaration == '' {
continue
}
if mut pnode := c2v.seen_ids[node.previous_declaration] {
pnode.redeclarations_count++
}
}

Expand Down
11 changes: 6 additions & 5 deletions src/struct.v
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,12 @@ fn (mut c C2V) record_decl(node &Node) {
c.last_declared_type_name = c_name
}
if c_name !in ['struct', 'union'] {
// in case typedef was already generated
if c_name in c.types {
c.types.delete(c_name)
}
v_name := c.add_struct_name(mut c.types, c_name)
// prevent duplicate generations:
if v_name in c.generated_declarations {
return
}
c.generated_declarations[v_name] = true
if node.tags.contains('union') {
c.genln('union ${v_name} { ')
} else {
Expand Down Expand Up @@ -135,7 +136,7 @@ fn (mut c C2V) typedef_decl(node &Node) {
return
}

v_alias_name := c.add_var_func_name(mut c.types, c_alias_name)
v_alias_name := c.add_struct_name(mut c.types, c_alias_name)

if typ.starts_with('struct ') && typ.ends_with(' *') {
// Opaque pointer, for example: typedef struct TSTexture_t *TSTexture;
Expand Down
2 changes: 1 addition & 1 deletion tests/17.partial_struct.out
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
@[translated]
module main

type tst_exture = voidptr
type TSTexture = voidptr
7 changes: 3 additions & 4 deletions tests/run_tests.vsh
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ fn run_tests(test_file_extension string, c2v_opts string, filter string) bool {
result := get_result_file_content(generated_file, test_file_extension)

if expected != result {
print_test_fail_details(expected, result)
print_test_fail_details(expected, result, c2v_cmd)
return false
} else {
do_post_test_cleanup(generated_file)
Expand Down Expand Up @@ -171,7 +171,7 @@ fn get_result_file_content(file string, test_file_extension string) string {
return file_content.after('// vstart').trim_space()
}

fn print_test_fail_details(expected string, got string) {
fn print_test_fail_details(expected string, got string, cmd string) {
eprintln(term.red('\nFAIL'))
eprintln('expected:')
eprintln(expected)
Expand All @@ -187,8 +187,7 @@ fn print_test_fail_details(expected string, got string) {

diff := execute('diff -u ${expected_file_form} ${got_file_form}')
eprintln(diff.output)

eprintln('\n')
eprintln('>>>>>>>>>>>>>>>>>> Failed cmd: ${cmd}')
}

fn do_post_test_cleanup(generated_file string) {
Expand Down

0 comments on commit 1106ca9

Please sign in to comment.