| 1 | import os |
| 2 | |
| 3 | const vexe = @VEXE |
| 4 | const tests_dir = os.dir(@FILE) |
| 5 | const v3_dir = os.dir(tests_dir) |
| 6 | const vlib_dir = os.dir(v3_dir) |
| 7 | const v3_src = os.join_path(v3_dir, 'v3.v') |
| 8 | |
| 9 | fn test_c_identifier_hygiene_for_escaped_names_unix_and_main_const() { |
| 10 | pid := os.getpid() |
| 11 | v3_bin := os.join_path(os.temp_dir(), 'v3_c_identifier_hygiene_test_${pid}') |
| 12 | os.rm(v3_bin) or {} |
| 13 | build := |
| 14 | os.execute('${vexe} -gc none -path "${vlib_dir}|@vlib|@vmodules" -o ${v3_bin} ${v3_src}') |
| 15 | assert build.exit_code == 0, build.output |
| 16 | |
| 17 | src := os.join_path(os.temp_dir(), 'v3_c_identifier_hygiene_input_${pid}.v') |
| 18 | os.write_file(src, 'const block_size = 4 |
| 19 | |
| 20 | struct Token { |
| 21 | mut: |
| 22 | @type int |
| 23 | unix i64 |
| 24 | block_size int |
| 25 | } |
| 26 | |
| 27 | fn bump(unix i64) i64 { |
| 28 | return unix + 1 |
| 29 | } |
| 30 | |
| 31 | fn rewrite(mut item Token, unix i64) { |
| 32 | item.@type = item.@type + 1 |
| 33 | item.unix = unix + item.unix |
| 34 | } |
| 35 | |
| 36 | fn main() { |
| 37 | mut @type := 1 |
| 38 | mut unix := i64(2) |
| 39 | mut item := Token{ |
| 40 | @type: @type |
| 41 | unix: unix |
| 42 | block_size: block_size |
| 43 | } |
| 44 | item.@type += 3 |
| 45 | item.unix = bump(item.unix) |
| 46 | rewrite(mut item, 5) |
| 47 | item.block_size = block_size |
| 48 | mut block_size := 1 |
| 49 | println(int_str(item.@type + int(item.unix) + item.block_size + block_size)) |
| 50 | } |
| 51 | ') or { |
| 52 | panic(err) |
| 53 | } |
| 54 | bin := os.join_path(os.temp_dir(), 'v3_c_identifier_hygiene_input_${pid}') |
| 55 | os.rm(bin) or {} |
| 56 | os.rm(bin + '.c') or {} |
| 57 | compile := os.execute('${v3_bin} ${src} -b c -o ${bin}') |
| 58 | assert compile.exit_code == 0, compile.output |
| 59 | |
| 60 | c_code := os.read_file(bin + '.c') or { panic(err) } |
| 61 | assert !c_code.contains('@type'), c_code |
| 62 | assert !c_code.contains('i64 unix'), c_code |
| 63 | assert !c_code.contains('.unix'), c_code |
| 64 | assert !c_code.contains('->unix'), c_code |
| 65 | assert !c_code.contains('#define block_size'), c_code |
| 66 | assert c_code.contains('i64 v_unix'), c_code |
| 67 | assert c_code.contains('.v_unix'), c_code |
| 68 | assert c_code.contains('->v_unix'), c_code |
| 69 | assert c_code.contains('#define main__block_size'), c_code |
| 70 | assert c_code.contains('.block_size = main__block_size'), c_code |
| 71 | |
| 72 | run := os.execute(bin) |
| 73 | assert run.exit_code == 0, run.output |
| 74 | assert run.output.trim_space() == '18' |
| 75 | |
| 76 | csym_src := os.join_path(os.temp_dir(), 'v3_c_identifier_hygiene_csym_${pid}.v') |
| 77 | os.write_file(csym_src, 'fn C.unix(i64) i64 |
| 78 | |
| 79 | fn main() { |
| 80 | println(int_str(int(C.unix(10)))) |
| 81 | } |
| 82 | ') or { |
| 83 | panic(err) |
| 84 | } |
| 85 | csym_c := os.join_path(os.temp_dir(), 'v3_c_identifier_hygiene_csym_${pid}.c') |
| 86 | os.rm(csym_c) or {} |
| 87 | csym_compile := os.execute('${v3_bin} ${csym_src} -b c -o ${csym_c}') |
| 88 | assert csym_compile.exit_code == 0, csym_compile.output |
| 89 | csym_code := os.read_file(csym_c) or { panic(err) } |
| 90 | assert csym_code.contains('unix(10)'), csym_code |
| 91 | assert !csym_code.contains('v_unix(10)'), csym_code |
| 92 | assert !csym_code.contains('C.unix'), csym_code |
| 93 | } |
| 94 | |