2023-03-28 23:55:57 +03:00
|
|
|
// Copyright (c) 2019-2023 Alexander Medvednikov. All rights reserved.
|
2020-06-27 15:50:04 +03:00
|
|
|
// Use of this source code is governed by an MIT license that can be found in the LICENSE file.
|
|
|
|
module main
|
|
|
|
|
2021-01-10 19:48:39 +03:00
|
|
|
import os
|
|
|
|
import os.cmdline
|
2020-06-27 15:50:04 +03:00
|
|
|
import v.vet
|
|
|
|
import v.pref
|
|
|
|
import v.parser
|
2021-01-10 19:48:39 +03:00
|
|
|
import v.token
|
2021-04-02 01:57:09 +03:00
|
|
|
import v.ast
|
2021-03-24 13:39:09 +03:00
|
|
|
import term
|
2020-07-04 15:29:00 +03:00
|
|
|
|
2021-01-10 19:48:39 +03:00
|
|
|
struct Vet {
|
2021-01-12 06:38:43 +03:00
|
|
|
opt Options
|
2020-10-06 08:07:50 +03:00
|
|
|
mut:
|
2021-01-10 19:48:39 +03:00
|
|
|
errors []vet.Error
|
2021-03-25 00:53:44 +03:00
|
|
|
warns []vet.Error
|
2021-01-10 19:48:39 +03:00
|
|
|
file string
|
|
|
|
}
|
|
|
|
|
|
|
|
struct Options {
|
2022-04-30 16:01:35 +03:00
|
|
|
is_force bool
|
|
|
|
is_werror bool
|
|
|
|
is_verbose bool
|
|
|
|
show_warnings bool
|
|
|
|
use_color bool
|
|
|
|
doc_private_fns_too bool
|
2020-07-04 15:29:00 +03:00
|
|
|
}
|
2020-06-27 15:50:04 +03:00
|
|
|
|
2021-04-20 17:20:50 +03:00
|
|
|
const term_colors = term.can_show_color_on_stderr()
|
2021-01-10 19:48:39 +03:00
|
|
|
|
2020-06-27 15:50:04 +03:00
|
|
|
fn main() {
|
2021-04-20 17:20:50 +03:00
|
|
|
vet_options := cmdline.options_after(os.args, ['vet'])
|
2021-03-25 00:53:44 +03:00
|
|
|
mut vt := Vet{
|
|
|
|
opt: Options{
|
|
|
|
is_force: '-force' in vet_options
|
|
|
|
is_werror: '-W' in vet_options
|
|
|
|
is_verbose: '-verbose' in vet_options || '-v' in vet_options
|
2021-04-20 17:20:50 +03:00
|
|
|
show_warnings: '-hide-warnings' !in vet_options && '-w' !in vet_options
|
|
|
|
use_color: '-color' in vet_options || (term_colors && '-nocolor' !in vet_options)
|
2022-04-30 16:01:35 +03:00
|
|
|
doc_private_fns_too: '-p' in vet_options
|
2021-03-25 00:53:44 +03:00
|
|
|
}
|
2021-01-10 19:48:39 +03:00
|
|
|
}
|
2021-01-25 14:08:43 +03:00
|
|
|
mut paths := cmdline.only_non_options(vet_options)
|
2021-01-10 19:48:39 +03:00
|
|
|
vtmp := os.getenv('VTMP')
|
|
|
|
if vtmp != '' {
|
|
|
|
// `v test-cleancode` passes also `-o tmpfolder` as well as all options in VFLAGS
|
|
|
|
paths = paths.filter(!it.starts_with(vtmp))
|
|
|
|
}
|
2020-07-04 15:29:00 +03:00
|
|
|
for path in paths {
|
2020-08-12 20:33:51 +03:00
|
|
|
if !os.exists(path) {
|
2022-11-15 16:53:13 +03:00
|
|
|
eprintln('File/folder ${path} does not exist')
|
2020-08-12 20:33:51 +03:00
|
|
|
continue
|
|
|
|
}
|
2021-02-04 18:23:58 +03:00
|
|
|
if os.is_file(path) {
|
2021-03-25 00:53:44 +03:00
|
|
|
vt.vet_file(path)
|
2021-02-04 18:23:58 +03:00
|
|
|
}
|
2021-01-10 19:48:39 +03:00
|
|
|
if os.is_dir(path) {
|
2022-11-15 16:53:13 +03:00
|
|
|
vt.vprintln("vetting folder: '${path}' ...")
|
2020-08-25 18:46:33 +03:00
|
|
|
vfiles := os.walk_ext(path, '.v')
|
|
|
|
vvfiles := os.walk_ext(path, '.vv')
|
|
|
|
mut files := []string{}
|
|
|
|
files << vfiles
|
|
|
|
files << vvfiles
|
2020-07-04 15:29:00 +03:00
|
|
|
for file in files {
|
2021-03-25 00:53:44 +03:00
|
|
|
vt.vet_file(file)
|
2020-07-04 15:29:00 +03:00
|
|
|
}
|
2020-06-27 15:50:04 +03:00
|
|
|
}
|
|
|
|
}
|
2021-03-25 00:53:44 +03:00
|
|
|
vfmt_err_count := vt.errors.filter(it.fix == .vfmt).len
|
|
|
|
if vt.opt.show_warnings {
|
|
|
|
for w in vt.warns {
|
|
|
|
eprintln(vt.e2string(w))
|
2020-07-19 20:58:34 +03:00
|
|
|
}
|
2021-01-10 19:48:39 +03:00
|
|
|
}
|
2021-03-25 00:53:44 +03:00
|
|
|
for err in vt.errors {
|
|
|
|
eprintln(vt.e2string(err))
|
2021-01-10 19:48:39 +03:00
|
|
|
}
|
2021-03-25 00:53:44 +03:00
|
|
|
if vfmt_err_count > 0 {
|
2022-03-06 20:01:22 +03:00
|
|
|
eprintln('Note: You can run `v fmt -w file.v` to fix these errors automatically')
|
2021-01-10 19:48:39 +03:00
|
|
|
}
|
2021-04-03 19:30:15 +03:00
|
|
|
if vt.errors.len > 0 {
|
2020-07-19 20:58:34 +03:00
|
|
|
exit(1)
|
|
|
|
}
|
2020-06-27 15:50:04 +03:00
|
|
|
}
|
|
|
|
|
2021-01-10 19:48:39 +03:00
|
|
|
// vet_file vets the file read from `path`.
|
2021-03-25 00:53:44 +03:00
|
|
|
fn (mut vt Vet) vet_file(path string) {
|
2023-01-10 00:47:03 +03:00
|
|
|
if !vt.opt.is_force && (path.contains('/tests/') || path.contains('/slow_tests/')) {
|
2021-01-10 19:48:39 +03:00
|
|
|
// skip all /tests/ files, since usually their content is not
|
|
|
|
// important enough to be documented/vetted, and they may even
|
|
|
|
// contain intentionally invalid code.
|
2022-11-15 16:53:13 +03:00
|
|
|
vt.vprintln("skipping test file: '${path}' ...")
|
2021-01-10 19:48:39 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
vt.file = path
|
2020-07-04 15:29:00 +03:00
|
|
|
mut prefs := pref.new_preferences()
|
|
|
|
prefs.is_vet = true
|
2021-05-04 18:25:56 +03:00
|
|
|
prefs.is_vsh = path.ends_with('.vsh')
|
2021-04-02 01:57:09 +03:00
|
|
|
table := ast.new_table()
|
2022-11-15 16:53:13 +03:00
|
|
|
vt.vprintln("vetting file '${path}'...")
|
2021-01-09 20:41:30 +03:00
|
|
|
_, errors := parser.parse_vet_file(path, table, prefs)
|
2021-01-09 17:11:49 +03:00
|
|
|
// Transfer errors from scanner and parser
|
2021-01-10 19:48:39 +03:00
|
|
|
vt.errors << errors
|
|
|
|
// Scan each line in file for things to improve
|
|
|
|
source_lines := os.read_lines(vt.file) or { []string{} }
|
|
|
|
for lnumber, line in source_lines {
|
|
|
|
vt.vet_line(source_lines, line, lnumber)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// vet_line vets the contents of `line` from `vet.file`.
|
2021-03-25 00:53:44 +03:00
|
|
|
fn (mut vt Vet) vet_line(lines []string, line string, lnumber int) {
|
2022-04-30 16:01:35 +03:00
|
|
|
vt.vet_fn_documentation(lines, line, lnumber)
|
|
|
|
}
|
|
|
|
|
|
|
|
// vet_fn_documentation ensures that functions are documented
|
|
|
|
fn (mut vt Vet) vet_fn_documentation(lines []string, line string, lnumber int) {
|
|
|
|
if line.starts_with('fn C.') {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
is_pub_fn := line.starts_with('pub fn ')
|
|
|
|
is_fn := is_pub_fn || line.starts_with('fn ')
|
|
|
|
if !is_fn {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if line.starts_with('fn main') {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if !(is_pub_fn || vt.opt.doc_private_fns_too) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
// Scan function declarations for missing documentation
|
|
|
|
if lnumber > 0 {
|
|
|
|
collect_tags := fn (line string) []string {
|
|
|
|
mut cleaned := line.all_before('/')
|
|
|
|
cleaned = cleaned.replace_each(['[', '', ']', '', ' ', ''])
|
|
|
|
return cleaned.split(',')
|
|
|
|
}
|
|
|
|
ident_fn_name := fn (line string) string {
|
|
|
|
mut fn_idx := line.index(' fn ') or { return '' }
|
|
|
|
if line.len < fn_idx + 5 {
|
2021-01-21 14:46:50 +03:00
|
|
|
return ''
|
2021-01-10 19:48:39 +03:00
|
|
|
}
|
2022-04-30 16:01:35 +03:00
|
|
|
mut tokens := line[fn_idx + 4..].split(' ')
|
|
|
|
// Skip struct identifier
|
|
|
|
if tokens.first().starts_with('(') {
|
|
|
|
fn_idx = line.index(')') or { return '' }
|
|
|
|
tokens = line[fn_idx..].split(' ')
|
|
|
|
if tokens.len > 1 {
|
|
|
|
tokens = [tokens[1]]
|
2021-01-10 19:48:39 +03:00
|
|
|
}
|
2022-04-30 16:01:35 +03:00
|
|
|
}
|
|
|
|
if tokens.len > 0 {
|
2023-06-16 09:44:25 +03:00
|
|
|
function_name_with_generic_parameters := tokens[0].all_before('(')
|
|
|
|
return function_name_with_generic_parameters.all_before('[')
|
2022-04-30 16:01:35 +03:00
|
|
|
}
|
|
|
|
return ''
|
|
|
|
}
|
|
|
|
mut line_above := lines[lnumber - 1]
|
|
|
|
mut tags := []string{}
|
|
|
|
if !line_above.starts_with('//') {
|
|
|
|
mut grab := true
|
|
|
|
for j := lnumber - 1; j >= 0; j-- {
|
|
|
|
prev_line := lines[j]
|
|
|
|
if prev_line.contains('}') { // We've looked back to the above scope, stop here
|
|
|
|
break
|
|
|
|
} else if prev_line.starts_with('[') {
|
|
|
|
tags << collect_tags(prev_line)
|
|
|
|
continue
|
|
|
|
} else if prev_line.starts_with('//') { // Single-line comment
|
|
|
|
grab = false
|
|
|
|
break
|
2021-01-10 19:48:39 +03:00
|
|
|
}
|
2022-04-30 16:01:35 +03:00
|
|
|
}
|
|
|
|
if grab {
|
|
|
|
clean_line := line.all_before_last('{').trim(' ')
|
2022-11-15 16:53:13 +03:00
|
|
|
vt.warn('Function documentation seems to be missing for "${clean_line}".',
|
2022-04-30 16:01:35 +03:00
|
|
|
lnumber, .doc)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
fn_name := ident_fn_name(line)
|
|
|
|
mut grab := true
|
|
|
|
for j := lnumber - 1; j >= 0; j-- {
|
2022-05-16 12:24:21 +03:00
|
|
|
mut prev_prev_line := ''
|
|
|
|
if j - 1 >= 0 {
|
|
|
|
prev_prev_line = lines[j - 1]
|
|
|
|
}
|
2022-04-30 16:01:35 +03:00
|
|
|
prev_line := lines[j]
|
2023-03-25 22:51:45 +03:00
|
|
|
|
|
|
|
if prev_line.starts_with('//') {
|
|
|
|
if prev_line.starts_with('// ${fn_name} ') {
|
|
|
|
grab = false
|
|
|
|
break
|
|
|
|
} else if prev_line.starts_with('// ${fn_name}')
|
|
|
|
&& !prev_prev_line.starts_with('//') {
|
|
|
|
grab = false
|
|
|
|
clean_line := line.all_before_last('{').trim(' ')
|
|
|
|
vt.warn('The documentation for "${clean_line}" seems incomplete.',
|
|
|
|
lnumber, .doc)
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2022-04-30 16:01:35 +03:00
|
|
|
if prev_line.contains('}') { // We've looked back to the above scope, stop here
|
|
|
|
break
|
|
|
|
} else if prev_line.starts_with('[') {
|
|
|
|
tags << collect_tags(prev_line)
|
|
|
|
continue
|
2021-01-10 19:48:39 +03:00
|
|
|
}
|
|
|
|
}
|
2022-04-30 16:01:35 +03:00
|
|
|
if grab {
|
|
|
|
clean_line := line.all_before_last('{').trim(' ')
|
2022-11-15 16:53:13 +03:00
|
|
|
vt.warn('A function name is missing from the documentation of "${clean_line}".',
|
2022-04-30 16:01:35 +03:00
|
|
|
lnumber, .doc)
|
|
|
|
}
|
2021-01-10 19:48:39 +03:00
|
|
|
}
|
|
|
|
}
|
2020-06-27 15:50:04 +03:00
|
|
|
}
|
2021-03-25 00:53:44 +03:00
|
|
|
|
|
|
|
fn (vt &Vet) vprintln(s string) {
|
|
|
|
if !vt.opt.is_verbose {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
println(s)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn (vt &Vet) e2string(err vet.Error) string {
|
2022-11-15 16:53:13 +03:00
|
|
|
mut kind := '${err.kind}:'
|
|
|
|
mut location := '${err.file_path}:${err.pos.line_nr}:'
|
2021-03-25 00:53:44 +03:00
|
|
|
if vt.opt.use_color {
|
|
|
|
kind = match err.kind {
|
|
|
|
.warning { term.magenta(kind) }
|
|
|
|
.error { term.red(kind) }
|
|
|
|
}
|
|
|
|
kind = term.bold(kind)
|
|
|
|
location = term.bold(location)
|
|
|
|
}
|
2022-11-15 16:53:13 +03:00
|
|
|
return '${location} ${kind} ${err.message}'
|
2021-03-25 00:53:44 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
fn (mut vt Vet) error(msg string, line int, fix vet.FixKind) {
|
2022-01-26 13:36:28 +03:00
|
|
|
pos := token.Pos{
|
2021-03-25 00:53:44 +03:00
|
|
|
line_nr: line + 1
|
|
|
|
}
|
|
|
|
vt.errors << vet.Error{
|
|
|
|
message: msg
|
|
|
|
file_path: vt.file
|
|
|
|
pos: pos
|
|
|
|
kind: .error
|
|
|
|
fix: fix
|
2021-04-09 13:22:14 +03:00
|
|
|
typ: .default
|
2021-03-25 00:53:44 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn (mut vt Vet) warn(msg string, line int, fix vet.FixKind) {
|
2022-01-26 13:36:28 +03:00
|
|
|
pos := token.Pos{
|
2021-03-25 00:53:44 +03:00
|
|
|
line_nr: line + 1
|
|
|
|
}
|
2021-04-03 19:30:15 +03:00
|
|
|
mut w := vet.Error{
|
2021-03-25 00:53:44 +03:00
|
|
|
message: msg
|
|
|
|
file_path: vt.file
|
|
|
|
pos: pos
|
|
|
|
kind: .warning
|
|
|
|
fix: fix
|
2021-04-09 13:22:14 +03:00
|
|
|
typ: .default
|
2021-03-25 00:53:44 +03:00
|
|
|
}
|
2021-04-03 19:30:15 +03:00
|
|
|
if vt.opt.is_werror {
|
|
|
|
w.kind = .error
|
|
|
|
vt.errors << w
|
|
|
|
} else {
|
|
|
|
vt.warns << w
|
|
|
|
}
|
2021-03-25 00:53:44 +03:00
|
|
|
}
|