Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

vet: notice potentially fn to be inlined #23534

Merged
merged 7 commits into from
Jan 22, 2025
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions cmd/tools/vast/vast.v
Original file line number Diff line number Diff line change
Expand Up @@ -582,6 +582,7 @@ fn (t Tree) fn_decl(node ast.FnDecl) &Node {
obj.add('is_direct_arr', t.bool_node(node.is_direct_arr))
obj.add('ctdefine_idx', t.number_node(node.ctdefine_idx))
obj.add('pos', t.pos(node.pos))
obj.add('end_pos', t.pos(node.end_pos))
obj.add('body_pos', t.pos(node.body_pos))
obj.add('return_type_pos', t.pos(node.return_type_pos))
obj.add('file', t.string_node(node.file))
Expand Down
73 changes: 55 additions & 18 deletions cmd/tools/vvet/analyze.v
Original file line number Diff line number Diff line change
Expand Up @@ -4,29 +4,36 @@ module main

import v.ast
import v.token
import os
import arrays

// cutoffs
const indexexpr_cutoff = 10
const infixexpr_cutoff = 10
const selectorexpr_cutoff = 10
const callexpr_cutoff = 10
const stringinterliteral_cutoff = 10
const stringliteral_cutoff = 10
const ascast_cutoff = 10
const stringconcat_cutoff = 10
const indexexpr_cutoff = os.getenv_opt('VET_INDEXEXPR_CUTOFF') or { '10' }.int()
const infixexpr_cutoff = os.getenv_opt('VET_INFIXEXPR_CUTOFF') or { '10' }.int()
const selectorexpr_cutoff = os.getenv_opt('VET_SELECTOREXPR_CUTOFF') or { '10' }.int()
const callexpr_cutoff = os.getenv_opt('VET_CALLEXPR_CUTOFF') or { '10' }.int()
const stringinterliteral_cutoff = os.getenv_opt('STRINGINTERLITERAL_CUTOFF') or { '10' }.int()
const stringliteral_cutoff = os.getenv_opt('STRINGLITERAL_CUTOFF') or { '10' }.int()
const ascast_cutoff = os.getenv_opt('ASCAST_CUTOFF') or { '10' }.int()
const stringconcat_cutoff = os.getenv_opt('STRINGCONCAT_CUTOFF') or { '10' }.int()

// possibly inline fn cutoff
const fns_call_cutoff = os.getenv_opt('VET_FNS_CALL_CUTOFF') or { '10' }.int() // at least N calls
const short_fns_cutoff = os.getenv_opt('VET_SHORT_FNS_CUTOFF') or { '3' }.int() // lines

// minimum size for string literals
const stringliteral_min_size = 20
const stringliteral_min_size = os.getenv_opt('VET_STRINGLITERAL_MIN_SIZE') or { '20' }.int()

// long functions cutoff
const long_fns_cutoff = 300
const long_fns_cutoff = os.getenv_opt('VET_LONG_FNS_CUTOFF') or { '300' }.int()

struct VetAnalyze {
mut:
repeated_expr_cutoff shared map[string]int // repeated code cutoff
repeated_expr shared map[string]map[string]map[string][]token.Pos // repeated exprs in fn scope
cur_fn ast.FnDecl // current fn declaration
repeated_expr_cutoff shared map[string]int // repeated code cutoff
repeated_expr shared map[string]map[string]map[string][]token.Pos // repeated exprs in fn scope
potential_non_inlined shared map[string]map[string]token.Pos // fns might be inlined
call_counter shared map[string]int // fn call counter
cur_fn ast.FnDecl // current fn declaration
}

// stmt checks for repeated code in statements
Expand Down Expand Up @@ -80,9 +87,18 @@ fn (mut vt VetAnalyze) expr(vet &Vet, expr ast.Expr) {
}
ast.CallExpr {
if expr.is_static_method || expr.is_method {
vt.save_expr(callexpr_cutoff, '${expr.left}.${expr.name}(${expr.args.map(it.str()).join(', ')})',
left_str := expr.left.str()
lock vt.call_counter {
if vt.cur_fn.receiver.name == left_str {
vt.call_counter['${int(vt.cur_fn.receiver.typ)}.${expr.name}']++
}
}
vt.save_expr(callexpr_cutoff, '${left_str}.${expr.name}(${expr.args.map(it.str()).join(', ')})',
vet.file, expr.pos)
} else {
lock vt.call_counter {
vt.call_counter[expr.name]++
}
vt.save_expr(callexpr_cutoff, '${expr.name}(${expr.args.map(it.str()).join(', ')})',
vet.file, expr.pos)
}
Expand All @@ -104,10 +120,14 @@ fn (mut vt VetAnalyze) expr(vet &Vet, expr ast.Expr) {

// long_or_empty_fns checks for long or empty functions
fn (mut vt VetAnalyze) long_or_empty_fns(mut vet Vet, fn_decl ast.FnDecl) {
nr_lines := if fn_decl.stmts.len == 0 {
0
} else {
fn_decl.stmts.last().pos.line_nr - fn_decl.pos.line_nr
nr_lines := fn_decl.end_pos.line_nr - fn_decl.pos.line_nr - 2
if nr_lines < short_fns_cutoff {
attr := fn_decl.attrs.find_first('inline')
if attr == none {
lock vt.potential_non_inlined {
vt.potential_non_inlined[fn_decl.fkey()][vet.file] = fn_decl.pos
}
}
}
if nr_lines > long_fns_cutoff {
vet.notice('Long function - ${nr_lines} lines long.', fn_decl.pos.line_nr, .long_fns)
Expand Down Expand Up @@ -137,9 +157,26 @@ fn (mut vt VetAnalyze) vet_repeated_code(mut vet Vet) {
}
}

// vet_inlining_fn reports possible fn to be inlined
fn (mut vt VetAnalyze) vet_inlining_fn(mut vet Vet) {
for fn_name, info in vt.potential_non_inlined {
for file, pos in info {
calls := vt.call_counter[fn_name] or { 0 }
if calls < fns_call_cutoff {
continue
}
vet.notice_with_file(file, '${fn_name.all_after('.')} fn might be inlined (possibly called at least ${calls} times)',
pos.line_nr, .inline_fn)
}
}
}

// vet_code_analyze performs code analysis
fn (mut vt Vet) vet_code_analyze() {
if vt.opt.repeated_code {
vt.analyze.vet_repeated_code(mut vt)
}
if vt.opt.fn_sizing {
vt.analyze.vet_inlining_fn(mut vt)
}
}
2 changes: 1 addition & 1 deletion cmd/tools/vvet/errors.v
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@ pub enum FixKind {
repeated_code
long_fns
empty_fn
inline_fn
}

// ErrorType is used to filter out false positive errors under specific conditions
pub enum ErrorType {
default
space_indent
trailing_space
long_fns
}

@[minify]
Expand Down
1 change: 1 addition & 0 deletions vlib/v/ast/ast.v
Original file line number Diff line number Diff line change
Expand Up @@ -630,6 +630,7 @@ pub mut:
scope &Scope = unsafe { nil }
label_names []string
pos token.Pos // function declaration position
end_pos token.Pos // end position
//
is_expand_simple_interpolation bool // true, when @[expand_simple_interpolation] is used on a fn. It should have a single string argument.
}
Expand Down
3 changes: 2 additions & 1 deletion vlib/v/help/common/vet.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ Options:

-v, -verbose Enable verbose logging.

-F Report empty and long function declaration (>300 lines).
-F Report potential function to be inlined, empty, long function
declaration (>300 lines).
felipensp marked this conversation as resolved.
Show resolved Hide resolved

-p Report private functions with missing documentation too
(by default, only the `pub fn` functions will be reported).
Expand Down
1 change: 1 addition & 0 deletions vlib/v/parser/fn.v
Original file line number Diff line number Diff line change
Expand Up @@ -707,6 +707,7 @@ run them via `v file.v` instead',
language: language
no_body: no_body
pos: start_pos.extend_with_last_line(end_pos, p.prev_tok.line_nr)
end_pos: p.tok.pos()
name_pos: name_pos
body_pos: body_start_pos
file: p.file_path
Expand Down
Loading