Skip to content

Commit

Permalink
vfmt: fix const name was not prefixed with module name
Browse files Browse the repository at this point in the history
  • Loading branch information
yuyi98 committed Sep 9, 2024
1 parent 3300e59 commit 7631e3a
Show file tree
Hide file tree
Showing 137 changed files with 987 additions and 929 deletions.
19 changes: 17 additions & 2 deletions cmd/tools/vfmt.v
Original file line number Diff line number Diff line change
Expand Up @@ -181,9 +181,25 @@ fn (foptions &FormatOptions) vlog(msg string) {
}
}

fn parse_others_in_same_module(mod_name string, file string, mut table ast.Table, prefs &pref.Preferences) {
if mod_name != 'main' && prefs.should_compile_c(file) && !file.ends_with('.c.v')
&& !file.starts_with('.#') && !file.contains('_d_') && !file.contains('_notd_') {
// other files in the same module also need to be parsed
cur_file_dir := os.dir(file)
mut files := os.ls(cur_file_dir) or { panic(err) }
should_compile_files := prefs.should_compile_filtered_files(cur_file_dir, files)
for compile_file in should_compile_files {
if compile_file != file {
parser.parse_file(compile_file, mut table, .skip_comments, prefs)
}
}
}
}

fn (foptions &FormatOptions) formated_content_from_file(prefs &pref.Preferences, file string) string {
mut table := ast.new_table()
file_ast := parser.parse_file(file, mut table, .parse_comments, prefs)
parse_others_in_same_module(file_ast.mod.name, file, mut table, prefs)
formated_content := fmt.fmt(file_ast, mut table, prefs, foptions.is_debug)
return formated_content
}
Expand All @@ -201,7 +217,7 @@ fn (foptions &FormatOptions) format_file(file string) {
foptions.vlog('vfmt2 running fmt.fmt over file: ${file}')
prefs, mut table := setup_preferences_and_table()
file_ast := parser.parse_file(file, mut table, .parse_comments, prefs)
// checker.new_checker(table, prefs).check(file_ast)
parse_others_in_same_module(file_ast.mod.name, file, mut table, prefs)
formatted_content := fmt.fmt(file_ast, mut table, prefs, foptions.is_debug)
os.write_file(vfmt_output_path, formatted_content) or { panic(err) }
foptions.vlog('fmt.fmt worked and ${formatted_content.len} bytes were written to ${vfmt_output_path} .')
Expand All @@ -213,7 +229,6 @@ fn (foptions &FormatOptions) format_pipe() {
prefs, mut table := setup_preferences_and_table()
input_text := os.get_raw_lines_joined()
file_ast := parser.parse_text(input_text, '', mut table, .parse_comments, prefs)
// checker.new_checker(table, prefs).check(file_ast)
formatted_content := fmt.fmt(file_ast, mut table, prefs, foptions.is_debug,
source_text: input_text
)
Expand Down
14 changes: 7 additions & 7 deletions vlib/compress/gzip/gzip_test.v
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,15 @@ fn test_gzip_invalid_compression() {
fn test_gzip_with_ftext() {
uncompressed := 'Hello world!'
mut compressed := compress(uncompressed.bytes())!
compressed[3] |= ftext
compressed[3] |= gzip.ftext
decompressed := decompress(compressed)!
assert decompressed == uncompressed.bytes()
}

fn test_gzip_with_fname() {
uncompressed := 'Hello world!'
mut compressed := compress(uncompressed.bytes())!
compressed[3] |= fname
compressed[3] |= gzip.fname
compressed.insert(10, `h`)
compressed.insert(11, `i`)
compressed.insert(12, 0x00)
Expand All @@ -54,7 +54,7 @@ fn test_gzip_with_fname() {
fn test_gzip_with_fcomment() {
uncompressed := 'Hello world!'
mut compressed := compress(uncompressed.bytes())!
compressed[3] |= fcomment
compressed[3] |= gzip.fcomment
compressed.insert(10, `h`)
compressed.insert(11, `i`)
compressed.insert(12, 0x00)
Expand All @@ -65,7 +65,7 @@ fn test_gzip_with_fcomment() {
fn test_gzip_with_fname_fcomment() {
uncompressed := 'Hello world!'
mut compressed := compress(uncompressed.bytes())!
compressed[3] |= (fname | fcomment)
compressed[3] |= (gzip.fname | gzip.fcomment)
compressed.insert(10, `h`)
compressed.insert(11, `i`)
compressed.insert(12, 0x00)
Expand All @@ -79,7 +79,7 @@ fn test_gzip_with_fname_fcomment() {
fn test_gzip_with_fextra() {
uncompressed := 'Hello world!'
mut compressed := compress(uncompressed.bytes())!
compressed[3] |= fextra
compressed[3] |= gzip.fextra
compressed.insert(10, 2)
compressed.insert(11, `h`)
compressed.insert(12, `i`)
Expand All @@ -90,7 +90,7 @@ fn test_gzip_with_fextra() {
fn test_gzip_with_hcrc() {
uncompressed := 'Hello world!'
mut compressed := compress(uncompressed.bytes())!
compressed[3] |= fhcrc
compressed[3] |= gzip.fhcrc
checksum := crc32.sum(compressed[..10])
compressed.insert(10, u8(checksum >> 24))
compressed.insert(11, u8(checksum >> 16))
Expand All @@ -103,7 +103,7 @@ fn test_gzip_with_hcrc() {
fn test_gzip_with_invalid_hcrc() {
uncompressed := 'Hello world!'
mut compressed := compress(uncompressed.bytes())!
compressed[3] |= fhcrc
compressed[3] |= gzip.fhcrc
checksum := crc32.sum(compressed[..10])
compressed.insert(10, u8(checksum >> 24))
compressed.insert(11, u8(checksum >> 16))
Expand Down
14 changes: 7 additions & 7 deletions vlib/compress/zstd/zstd_test.v
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,8 @@ fn compress_file(fname string, oname string, params CompressParams) ! {
fout.close()
}

mut buf_in := []u8{len: buf_in_size}
mut buf_out := []u8{len: buf_out_size}
mut buf_in := []u8{len: zstd.buf_in_size}
mut buf_out := []u8{len: zstd.buf_out_size}

mut cctx := new_cctx(params)!
defer {
Expand All @@ -138,7 +138,7 @@ fn compress_file(fname string, oname string, params CompressParams) ! {
}
for !last_chunk {
read_len := fin.read(mut buf_in)!
last_chunk = read_len < buf_in_size
last_chunk = read_len < zstd.buf_in_size
mode := if last_chunk {
ZSTD_EndDirective.zstd_e_end
} else {
Expand All @@ -151,7 +151,7 @@ fn compress_file(fname string, oname string, params CompressParams) ! {
input.pos = 0
for !finished {
output.dst = buf_out.data
output.size = buf_out_size
output.size = zstd.buf_out_size
output.pos = 0
remaining := cctx.compress_stream2(output, input, mode)
check_zstd(remaining)!
Expand All @@ -173,8 +173,8 @@ fn decompress_file(fname string, oname string, params DecompressParams) ! {
fout.close()
}

mut buf_in := []u8{len: buf_in_size}
mut buf_out := []u8{len: buf_out_size}
mut buf_in := []u8{len: zstd.buf_in_size}
mut buf_out := []u8{len: zstd.buf_out_size}

mut dctx := new_dctx(params)!
defer {
Expand All @@ -200,7 +200,7 @@ fn decompress_file(fname string, oname string, params DecompressParams) ! {
input.pos = 0
for input.pos < input.size {
output.dst = buf_out.data
output.size = buf_out_size
output.size = zstd.buf_out_size
output.pos = 0
ret := dctx.decompress_stream(output, input)
check_zstd(ret)!
Expand Down
6 changes: 3 additions & 3 deletions vlib/context/cancel.v
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ pub fn with_cancel(mut parent Context) (Context, CancelFn) {
mut c := new_cancel_context(parent)
propagate_cancel(mut parent, mut c)
cancel_fn := fn [mut c] () {
c.cancel(true, canceled)
c.cancel(true, context.canceled)
}
return Context(c), CancelFn(cancel_fn)
}
Expand Down Expand Up @@ -74,7 +74,7 @@ pub fn (mut ctx CancelContext) err() IError {
}

pub fn (ctx &CancelContext) value(key Key) ?Any {
if key == cancel_context_key {
if key == context.cancel_context_key {
return ctx
}
return ctx.context.value(key)
Expand Down Expand Up @@ -158,7 +158,7 @@ fn parent_cancel_context(mut parent Context) ?&CancelContext {
if done.closed {
return none
}
mut p := parent.value(cancel_context_key)?
mut p := parent.value(context.cancel_context_key)?
match mut p {
CancelContext {
pdone := p.done()
Expand Down
8 changes: 4 additions & 4 deletions vlib/context/deadline.v
Original file line number Diff line number Diff line change
Expand Up @@ -43,22 +43,22 @@ pub fn with_deadline(mut parent Context, d time.Time) (Context, CancelFn) {
propagate_cancel(mut parent, mut ctx)
dur := d - time.now()
if dur.nanoseconds() <= 0 {
ctx.cancel(true, deadline_exceeded) // deadline has already passed
ctx.cancel(true, context.deadline_exceeded) // deadline has already passed
cancel_fn := fn [mut ctx] () {
ctx.cancel(true, canceled)
ctx.cancel(true, context.canceled)
}
return Context(ctx), CancelFn(cancel_fn)
}

if ctx.err() is none {
spawn fn (mut ctx TimerContext, dur time.Duration) {
time.sleep(dur)
ctx.cancel(true, deadline_exceeded)
ctx.cancel(true, context.deadline_exceeded)
}(mut ctx, dur)
}

cancel_fn := fn [mut ctx] () {
ctx.cancel(true, canceled)
ctx.cancel(true, context.canceled)
}
return Context(ctx), CancelFn(cancel_fn)
}
Expand Down
38 changes: 19 additions & 19 deletions vlib/crypto/aes/block_generic.v
Original file line number Diff line number Diff line change
Expand Up @@ -59,21 +59,21 @@ fn encrypt_block_generic(xk []u32, mut dst []u8, src []u8) {
mut t2 := u32(0)
mut t3 := u32(0)
for _ in 0 .. nr {
t0 = xk[k + 0] ^ te0[u8(s0 >> 24)] ^ te1[u8(s1 >> 16)] ^ te2[u8(s2 >> 8)] ^ u32(te3[u8(s3)])
t1 = xk[k + 1] ^ te0[u8(s1 >> 24)] ^ te1[u8(s2 >> 16)] ^ te2[u8(s3 >> 8)] ^ u32(te3[u8(s0)])
t2 = xk[k + 2] ^ te0[u8(s2 >> 24)] ^ te1[u8(s3 >> 16)] ^ te2[u8(s0 >> 8)] ^ u32(te3[u8(s1)])
t3 = xk[k + 3] ^ te0[u8(s3 >> 24)] ^ te1[u8(s0 >> 16)] ^ te2[u8(s1 >> 8)] ^ u32(te3[u8(s2)])
t0 = xk[k + 0] ^ aes.te0[u8(s0 >> 24)] ^ aes.te1[u8(s1 >> 16)] ^ aes.te2[u8(s2 >> 8)] ^ u32(aes.te3[u8(s3)])
t1 = xk[k + 1] ^ aes.te0[u8(s1 >> 24)] ^ aes.te1[u8(s2 >> 16)] ^ aes.te2[u8(s3 >> 8)] ^ u32(aes.te3[u8(s0)])
t2 = xk[k + 2] ^ aes.te0[u8(s2 >> 24)] ^ aes.te1[u8(s3 >> 16)] ^ aes.te2[u8(s0 >> 8)] ^ u32(aes.te3[u8(s1)])
t3 = xk[k + 3] ^ aes.te0[u8(s3 >> 24)] ^ aes.te1[u8(s0 >> 16)] ^ aes.te2[u8(s1 >> 8)] ^ u32(aes.te3[u8(s2)])
k += 4
s0 = t0
s1 = t1
s2 = t2
s3 = t3
}
// Last round uses s-box directly and XORs to produce output.
s0 = u32(s_box0[t0 >> 24]) << 24 | u32(s_box0[t1 >> 16 & 0xff]) << 16 | u32(s_box0[t2 >> 8 & 0xff]) << 8 | u32(s_box0[t3 & u32(0xff)])
s1 = u32(s_box0[t1 >> 24]) << 24 | u32(s_box0[t2 >> 16 & 0xff]) << 16 | u32(s_box0[t3 >> 8 & 0xff]) << 8 | u32(s_box0[t0 & u32(0xff)])
s2 = u32(s_box0[t2 >> 24]) << 24 | u32(s_box0[t3 >> 16 & 0xff]) << 16 | u32(s_box0[t0 >> 8 & 0xff]) << 8 | u32(s_box0[t1 & u32(0xff)])
s3 = u32(s_box0[t3 >> 24]) << 24 | u32(s_box0[t0 >> 16 & 0xff]) << 16 | u32(s_box0[t1 >> 8 & 0xff]) << 8 | u32(s_box0[t2 & u32(0xff)])
s0 = u32(aes.s_box0[t0 >> 24]) << 24 | u32(aes.s_box0[t1 >> 16 & 0xff]) << 16 | u32(aes.s_box0[t2 >> 8 & 0xff]) << 8 | u32(aes.s_box0[t3 & u32(0xff)])
s1 = u32(aes.s_box0[t1 >> 24]) << 24 | u32(aes.s_box0[t2 >> 16 & 0xff]) << 16 | u32(aes.s_box0[t3 >> 8 & 0xff]) << 8 | u32(aes.s_box0[t0 & u32(0xff)])
s2 = u32(aes.s_box0[t2 >> 24]) << 24 | u32(aes.s_box0[t3 >> 16 & 0xff]) << 16 | u32(aes.s_box0[t0 >> 8 & 0xff]) << 8 | u32(aes.s_box0[t1 & u32(0xff)])
s3 = u32(aes.s_box0[t3 >> 24]) << 24 | u32(aes.s_box0[t0 >> 16 & 0xff]) << 16 | u32(aes.s_box0[t1 >> 8 & 0xff]) << 8 | u32(aes.s_box0[t2 & u32(0xff)])
s0 ^= xk[k + 0]
s1 ^= xk[k + 1]
s2 ^= xk[k + 2]
Expand Down Expand Up @@ -107,21 +107,21 @@ fn decrypt_block_generic(xk []u32, mut dst []u8, src []u8) {
mut t2 := u32(0)
mut t3 := u32(0)
for _ in 0 .. nr {
t0 = xk[k + 0] ^ td0[u8(s0 >> 24)] ^ td1[u8(s3 >> 16)] ^ td2[u8(s2 >> 8)] ^ u32(td3[u8(s1)])
t1 = xk[k + 1] ^ td0[u8(s1 >> 24)] ^ td1[u8(s0 >> 16)] ^ td2[u8(s3 >> 8)] ^ u32(td3[u8(s2)])
t2 = xk[k + 2] ^ td0[u8(s2 >> 24)] ^ td1[u8(s1 >> 16)] ^ td2[u8(s0 >> 8)] ^ u32(td3[u8(s3)])
t3 = xk[k + 3] ^ td0[u8(s3 >> 24)] ^ td1[u8(s2 >> 16)] ^ td2[u8(s1 >> 8)] ^ u32(td3[u8(s0)])
t0 = xk[k + 0] ^ aes.td0[u8(s0 >> 24)] ^ aes.td1[u8(s3 >> 16)] ^ aes.td2[u8(s2 >> 8)] ^ u32(aes.td3[u8(s1)])
t1 = xk[k + 1] ^ aes.td0[u8(s1 >> 24)] ^ aes.td1[u8(s0 >> 16)] ^ aes.td2[u8(s3 >> 8)] ^ u32(aes.td3[u8(s2)])
t2 = xk[k + 2] ^ aes.td0[u8(s2 >> 24)] ^ aes.td1[u8(s1 >> 16)] ^ aes.td2[u8(s0 >> 8)] ^ u32(aes.td3[u8(s3)])
t3 = xk[k + 3] ^ aes.td0[u8(s3 >> 24)] ^ aes.td1[u8(s2 >> 16)] ^ aes.td2[u8(s1 >> 8)] ^ u32(aes.td3[u8(s0)])
k += 4
s0 = t0
s1 = t1
s2 = t2
s3 = t3
}
// Last round uses s-box directly and XORs to produce output.
s0 = u32(s_box1[t0 >> 24]) << 24 | u32(s_box1[t3 >> 16 & 0xff]) << 16 | u32(s_box1[t2 >> 8 & 0xff]) << 8 | u32(s_box1[t1 & u32(0xff)])
s1 = u32(s_box1[t1 >> 24]) << 24 | u32(s_box1[t0 >> 16 & 0xff]) << 16 | u32(s_box1[t3 >> 8 & 0xff]) << 8 | u32(s_box1[t2 & u32(0xff)])
s2 = u32(s_box1[t2 >> 24]) << 24 | u32(s_box1[t1 >> 16 & 0xff]) << 16 | u32(s_box1[t0 >> 8 & 0xff]) << 8 | u32(s_box1[t3 & u32(0xff)])
s3 = u32(s_box1[t3 >> 24]) << 24 | u32(s_box1[t2 >> 16 & 0xff]) << 16 | u32(s_box1[t1 >> 8 & 0xff]) << 8 | u32(s_box1[t0 & u32(0xff)])
s0 = u32(aes.s_box1[t0 >> 24]) << 24 | u32(aes.s_box1[t3 >> 16 & 0xff]) << 16 | u32(aes.s_box1[t2 >> 8 & 0xff]) << 8 | u32(aes.s_box1[t1 & u32(0xff)])
s1 = u32(aes.s_box1[t1 >> 24]) << 24 | u32(aes.s_box1[t0 >> 16 & 0xff]) << 16 | u32(aes.s_box1[t3 >> 8 & 0xff]) << 8 | u32(aes.s_box1[t2 & u32(0xff)])
s2 = u32(aes.s_box1[t2 >> 24]) << 24 | u32(aes.s_box1[t1 >> 16 & 0xff]) << 16 | u32(aes.s_box1[t0 >> 8 & 0xff]) << 8 | u32(aes.s_box1[t3 & u32(0xff)])
s3 = u32(aes.s_box1[t3 >> 24]) << 24 | u32(aes.s_box1[t2 >> 16 & 0xff]) << 16 | u32(aes.s_box1[t1 >> 8 & 0xff]) << 8 | u32(aes.s_box1[t0 & u32(0xff)])
s0 ^= xk[k + 0]
s1 ^= xk[k + 1]
s2 ^= xk[k + 2]
Expand All @@ -136,7 +136,7 @@ fn decrypt_block_generic(xk []u32, mut dst []u8, src []u8) {
// Apply s_box0 to each byte in w.
@[direct_array_access; inline]
fn subw(w u32) u32 {
return u32(s_box0[w >> 24]) << 24 | u32(s_box0[w >> 16 & 0xff]) << 16 | u32(s_box0[w >> 8 & 0xff]) << 8 | u32(s_box0[w & u32(0xff)])
return u32(aes.s_box0[w >> 24]) << 24 | u32(aes.s_box0[w >> 16 & 0xff]) << 16 | u32(aes.s_box0[w >> 8 & 0xff]) << 8 | u32(aes.s_box0[w & u32(0xff)])
}

// Rotate
Expand All @@ -161,7 +161,7 @@ fn expand_key_generic(key []u8, mut enc []u32, mut dec []u32) {
for i < enc.len {
mut t := enc[i - 1]
if i % nk == 0 {
t = subw(rotw(t)) ^ u32(pow_x[i / nk - 1]) << 24
t = subw(rotw(t)) ^ u32(aes.pow_x[i / nk - 1]) << 24
} else if nk > 6 && i % nk == 4 {
t = subw(t)
}
Expand All @@ -180,7 +180,7 @@ fn expand_key_generic(key []u8, mut enc []u32, mut dec []u32) {
for j in 0 .. 4 {
mut x := enc[ei + j]
if i > 0 && i + 4 < n {
x = td0[s_box0[x >> 24]] ^ td1[s_box0[x >> 16 & 0xff]] ^ td2[s_box0[x >> 8 & 0xff]] ^ td3[s_box0[x & u32(0xff)]]
x = aes.td0[aes.s_box0[x >> 24]] ^ aes.td1[aes.s_box0[x >> 16 & 0xff]] ^ aes.td2[aes.s_box0[x >> 8 & 0xff]] ^ aes.td3[aes.s_box0[x & u32(0xff)]]
}
dec[i + j] = x
}
Expand Down
34 changes: 17 additions & 17 deletions vlib/crypto/blake2b/blake2b_block_generic.v
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@ import math.bits
@[inline]
fn g(mut v []u64, a u8, b u8, c u8, d u8, x u64, y u64) {
v[a] = v[a] + v[b] + x
v[d] = bits.rotate_left_64((v[d] ^ v[a]), nr1)
v[d] = bits.rotate_left_64((v[d] ^ v[a]), blake2b.nr1)
v[c] = v[c] + v[d]
v[b] = bits.rotate_left_64((v[b] ^ v[c]), nr2)
v[b] = bits.rotate_left_64((v[b] ^ v[c]), blake2b.nr2)
v[a] = v[a] + v[b] + y
v[d] = bits.rotate_left_64((v[d] ^ v[a]), nr3)
v[d] = bits.rotate_left_64((v[d] ^ v[a]), blake2b.nr3)
v[c] = v[c] + v[d]
v[b] = bits.rotate_left_64((v[b] ^ v[c]), nr4)
v[b] = bits.rotate_left_64((v[b] ^ v[c]), blake2b.nr4)
}

// one complete mixing round with the function g
Expand All @@ -42,7 +42,7 @@ fn (mut d Digest) f(f bool) {
// initialize the working vector
mut v := []u64{len: 0, cap: 16}
v << d.h[..8]
v << iv[..8]
v << blake2b.iv[..8]

v[12] ^= d.t.lo
v[13] ^= d.t.hi
Expand All @@ -52,18 +52,18 @@ fn (mut d Digest) f(f bool) {
}

// go 12 rounds of cryptographic mixing
d.mixing_round(mut v, sigma[0])
d.mixing_round(mut v, sigma[1])
d.mixing_round(mut v, sigma[2])
d.mixing_round(mut v, sigma[3])
d.mixing_round(mut v, sigma[4])
d.mixing_round(mut v, sigma[5])
d.mixing_round(mut v, sigma[6])
d.mixing_round(mut v, sigma[7])
d.mixing_round(mut v, sigma[8])
d.mixing_round(mut v, sigma[9])
d.mixing_round(mut v, sigma[0])
d.mixing_round(mut v, sigma[1])
d.mixing_round(mut v, blake2b.sigma[0])
d.mixing_round(mut v, blake2b.sigma[1])
d.mixing_round(mut v, blake2b.sigma[2])
d.mixing_round(mut v, blake2b.sigma[3])
d.mixing_round(mut v, blake2b.sigma[4])
d.mixing_round(mut v, blake2b.sigma[5])
d.mixing_round(mut v, blake2b.sigma[6])
d.mixing_round(mut v, blake2b.sigma[7])
d.mixing_round(mut v, blake2b.sigma[8])
d.mixing_round(mut v, blake2b.sigma[9])
d.mixing_round(mut v, blake2b.sigma[0])
d.mixing_round(mut v, blake2b.sigma[1])

// combine internal hash state with both halves of the working vector

Expand Down
4 changes: 2 additions & 2 deletions vlib/crypto/blake2b/blake2b_block_test.v
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ fn test_mixing_function_g() {
// initialize the working vector from the digest and IV values
mut v := []u64{len: 0, cap: 16}
v << d.h[..8]
v << iv[..8]
v << blake2b.iv[..8]

// fold in the 128-bit message length
v[12] ^= d.t.lo
Expand All @@ -114,7 +114,7 @@ fn test_mixing_function_g() {
}

for r in 0 .. blake2b.expected_v_results.len {
d.mixing_round(mut v, sigma[r % 10])
d.mixing_round(mut v, blake2b.sigma[r % 10])

for i in 0 .. 16 {
assert v[i] == blake2b.expected_v_results[r][i], 'expeccted expected_v_results[${r}][${i}] ${blake2b.expected_v_results[r][i]:016x} actual v[${i}] ${v[i]:016x}'
Expand Down
Loading

0 comments on commit 7631e3a

Please sign in to comment.