ci: fix v vet call; fmt: fix SizeOf

pull/5612/head
Alexander Medvednikov 2020-07-01 20:07:33 +02:00
parent 66f36f6dcc
commit 2716a37916
5 changed files with 30 additions and 25 deletions

View File

@ -42,7 +42,7 @@ jobs:
run: ./v test-fixed run: ./v test-fixed
- name: Test building v tools - name: Test building v tools
run: ./v build-tools run: ./v build-tools
- name: v vet - name: ./v vet
run: | run: |
v vet vlib/v/parser v vet vlib/v/parser
v vet vlib/v/ast v vet vlib/v/ast

View File

@ -186,6 +186,7 @@ pub struct StructInitField {
pub: pub:
expr Expr expr Expr
pos token.Position pos token.Position
comment Comment
pub mut: pub mut:
name string name string
typ table.Type typ table.Type
@ -1011,6 +1012,7 @@ pub fn (stmt Stmt) position() token.Position {
// UnsafeStmt { // UnsafeStmt {
// } // }
*/ */
//
else { return token.Position{} } else { return token.Position{} }
} }
} }

View File

@ -145,13 +145,14 @@ pub fn (mut f Fmt) writeln(s string) {
fn (mut f Fmt) adjust_complete_line() { fn (mut f Fmt) adjust_complete_line() {
for i, buf in f.expr_bufs { for i, buf in f.expr_bufs {
// search for low penalties // search for low penalties
if i == 0 || f.penalties[i-1] <= 1 { if i == 0 || f.penalties[i - 1] <= 1 {
precedence := if i == 0 { -1 } else { f.precedences[i-1] } precedence := if i == 0 { -1 } else { f.precedences[i - 1] }
mut len_sub_expr := if i == 0 { buf.len + f.line_len } else { buf.len } mut len_sub_expr := if i == 0 { buf.len + f.line_len } else { buf.len }
mut sub_expr_end_idx := f.penalties.len mut sub_expr_end_idx := f.penalties.len
// search for next position with low penalty and same precedence to form subexpression // search for next position with low penalty and same precedence to form subexpression
for j in i..f.penalties.len { for j in i .. f.penalties.len {
if f.penalties[j] <= 1 && f.precedences[j] == precedence && len_sub_expr >= max_len[1] { if f.penalties[j] <= 1 &&
f.precedences[j] == precedence && len_sub_expr >= max_len[1] {
sub_expr_end_idx = j sub_expr_end_idx = j
break break
} else if f.precedences[j] < precedence { } else if f.precedences[j] < precedence {
@ -159,16 +160,16 @@ fn (mut f Fmt) adjust_complete_line() {
len_sub_expr = C.INT32_MAX len_sub_expr = C.INT32_MAX
break break
} else { } else {
len_sub_expr += f.expr_bufs[j+1].len len_sub_expr += f.expr_bufs[j + 1].len
} }
} }
// if subexpression would fit in single line adjust penalties to actually do so // if subexpression would fit in single line adjust penalties to actually do so
if len_sub_expr <= max_len[max_len.len-1] { if len_sub_expr <= max_len[max_len.len - 1] {
for j in i..sub_expr_end_idx { for j in i .. sub_expr_end_idx {
f.penalties[j] = max_len.len-1 f.penalties[j] = max_len.len - 1
} }
if i > 0 { if i > 0 {
f.penalties[i-1] = 0 f.penalties[i - 1] = 0
} }
if sub_expr_end_idx < f.penalties.len { if sub_expr_end_idx < f.penalties.len {
f.penalties[sub_expr_end_idx] = 0 f.penalties[sub_expr_end_idx] = 0
@ -176,8 +177,8 @@ fn (mut f Fmt) adjust_complete_line() {
} }
} }
// emergency fallback: decrease penalty in front of long unbreakable parts // emergency fallback: decrease penalty in front of long unbreakable parts
if i > 0 && buf.len > max_len[3] - max_len[1] && f.penalties[i-1] > 0 { if i > 0 && buf.len > max_len[3] - max_len[1] && f.penalties[i - 1] > 0 {
f.penalties[i-1] = if buf.len >= max_len[2] { 0 } else { 1 } f.penalties[i - 1] = if buf.len >= max_len[2] { 0 } else { 1 }
} }
} }
} }
@ -892,7 +893,7 @@ pub fn (mut f Fmt) expr(node ast.Expr) {
if node.is_type { if node.is_type {
f.write('sizeof(') f.write('sizeof(')
if node.type_name != '' { if node.type_name != '' {
f.write(node.type_name) f.write(f.short_module(node.type_name))
} else { } else {
f.write(f.type_to_str(node.typ)) f.write(f.type_to_str(node.typ))
} }
@ -1136,8 +1137,7 @@ pub fn (mut f Fmt) short_module(name string) string {
pub fn (mut f Fmt) if_expr(it ast.IfExpr) { pub fn (mut f Fmt) if_expr(it ast.IfExpr) {
single_line := it.branches.len == 2 && it.has_else && single_line := it.branches.len == 2 && it.has_else &&
it.branches[0].stmts.len == 1 && it.branches[0].stmts.len == 1 && it.branches[1].stmts.len == 1 &&
it.branches[1].stmts.len == 1 &&
(it.is_expr || f.is_assign) (it.is_expr || f.is_assign)
f.single_line_if = single_line f.single_line_if = single_line
for i, branch in it.branches { for i, branch in it.branches {
@ -1435,10 +1435,10 @@ pub fn (mut f Fmt) array_init(it ast.ArrayInit) {
if last_line_nr < line_nr { if last_line_nr < line_nr {
penalty-- penalty--
} }
if i == 0 || it.exprs[i - 1] is ast.ArrayInit || if i == 0 ||
it.exprs[i - 1] is ast.ArrayInit ||
it.exprs[i - 1] is ast.StructInit || it.exprs[i - 1] is ast.StructInit ||
it.exprs[i - 1] is ast.MapInit || it.exprs[i - 1] is ast.MapInit || it.exprs[i - 1] is ast.CallExpr {
it.exprs[i - 1] is ast.CallExpr {
penalty-- penalty--
} }
if expr is ast.ArrayInit || if expr is ast.ArrayInit ||

View File

@ -5,8 +5,12 @@ struct User {
} }
fn main() { fn main() {
u := User{
name: 'Peter'
}
if true { if true {
} }
n := sizeof(User)
// else // else
// else { // else {
// } // }

View File

@ -32,12 +32,12 @@ fn (mut p Parser) struct_decl() ast.StructDecl {
p.next() // C || JS p.next() // C || JS
p.next() // . p.next() // .
} }
is_typedef := 'typedef' in p.attrs is_typedef := 'typedef' in p.attrs
end_pos := p.tok.position() end_pos := p.tok.position()
mut name := p.check_name() mut name := p.check_name()
if name.len == 1 && name[0].is_capital() { if name.len == 1 && name[0].is_capital() {
p.error_with_pos('single letter capital names are reserved for generic template types.', end_pos) p.error_with_pos('single letter capital names are reserved for generic template types.',
end_pos)
} }
mut generic_types := []table.Type{} mut generic_types := []table.Type{}
if p.tok.kind == .lt { if p.tok.kind == .lt {
@ -51,12 +51,10 @@ fn (mut p Parser) struct_decl() ast.StructDecl {
} }
p.check(.gt) p.check(.gt)
} }
no_body := p.tok.kind != .lcbr no_body := p.tok.kind != .lcbr
if language == .v && no_body { if language == .v && no_body {
p.error('`$p.tok.lit` lacks body') p.error('`$p.tok.lit` lacks body')
} }
if language == .v && p.mod != 'builtin' && name.len > 0 && !name[0].is_capital() { if language == .v && p.mod != 'builtin' && name.len > 0 && !name[0].is_capital() {
p.error_with_pos('struct name `$name` must begin with capital letter', end_pos) p.error_with_pos('struct name `$name` must begin with capital letter', end_pos)
} }
@ -282,7 +280,7 @@ fn (mut p Parser) struct_init(short_syntax bool) ast.StructInit {
saved_is_amp := p.is_amp saved_is_amp := p.is_amp
p.is_amp = false p.is_amp = false
for p.tok.kind != .rcbr && p.tok.kind != .rpar { for p.tok.kind != .rcbr && p.tok.kind != .rpar {
p.check_comment() comment := p.check_comment()
mut field_name := '' mut field_name := ''
if no_keys { if no_keys {
expr := p.expr(0) expr := p.expr(0)
@ -290,6 +288,7 @@ fn (mut p Parser) struct_init(short_syntax bool) ast.StructInit {
fields << ast.StructInitField{ fields << ast.StructInitField{
expr: expr expr: expr
pos: expr.position() pos: expr.position()
comment: comment
} }
} else { } else {
first_field_pos := p.tok.position() first_field_pos := p.tok.position()