2021-01-18 15:20:06 +03:00
|
|
|
// Copyright (c) 2019-2021 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
|
|
|
|
import v.table
|
2021-01-10 19:48:39 +03:00
|
|
|
import v.token
|
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
|
|
|
|
file string
|
|
|
|
}
|
|
|
|
|
|
|
|
struct Options {
|
|
|
|
is_verbose bool
|
2020-07-04 15:29:00 +03:00
|
|
|
}
|
|
|
|
|
2021-01-10 19:48:39 +03:00
|
|
|
fn (vet &Vet) vprintln(s string) {
|
|
|
|
if !vet.opt.is_verbose {
|
2020-07-04 15:29:00 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
println(s)
|
|
|
|
}
|
2020-06-27 15:50:04 +03:00
|
|
|
|
2021-01-10 19:48:39 +03:00
|
|
|
const vet_options = cmdline.options_after(os.args, ['vet'])
|
|
|
|
|
|
|
|
const is_force = '-force' in vet_options
|
|
|
|
|
|
|
|
const is_verbose = '-verbose' in vet_options || '-v' in vet_options
|
|
|
|
|
|
|
|
const show_warnings = '-hide-warnings' !in vet_options
|
|
|
|
|
2020-06-27 15:50:04 +03:00
|
|
|
fn main() {
|
2021-01-10 19:48:39 +03:00
|
|
|
opt := Options{
|
2021-01-25 14:08:43 +03:00
|
|
|
is_verbose: is_verbose
|
2020-06-27 15:50:04 +03:00
|
|
|
}
|
2021-01-10 19:48:39 +03:00
|
|
|
mut vet := Vet{
|
|
|
|
opt: opt
|
|
|
|
}
|
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) {
|
|
|
|
eprintln('File/folder $path does not exist')
|
|
|
|
continue
|
|
|
|
}
|
2020-08-25 18:46:33 +03:00
|
|
|
if path.ends_with('.v') || path.ends_with('.vv') {
|
2021-01-10 19:48:39 +03:00
|
|
|
if path.contains('cmd/tools/vvet/tests/') {
|
2021-01-25 14:08:43 +03:00
|
|
|
if is_force || paths.len == 1 {
|
2021-01-10 19:48:39 +03:00
|
|
|
vet.vet_file(path, true)
|
|
|
|
continue
|
|
|
|
} else {
|
|
|
|
// The .vv files in that folder, are regression tests
|
|
|
|
// for `v vet` itself and thus are known to fail in
|
|
|
|
// a predictable way. They are run 1 by 1 by vet_test.v.
|
|
|
|
// They *should be skipped*, when run by more general
|
|
|
|
// invocations like for example `v vet cmd/tools`
|
|
|
|
eprintln("skipping vvet regression file: '$path' ...")
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if os.is_dir(path) {
|
|
|
|
vet.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-01-25 14:08:43 +03:00
|
|
|
if !is_force && file.ends_with('.vv') && file.contains('cmd/tools/vvet/tests/') {
|
2020-08-12 19:56:06 +03:00
|
|
|
continue
|
|
|
|
}
|
2021-01-10 19:48:39 +03:00
|
|
|
vet.vet_file(file, false)
|
2020-07-04 15:29:00 +03:00
|
|
|
}
|
2020-06-27 15:50:04 +03:00
|
|
|
}
|
|
|
|
}
|
2021-01-10 19:48:39 +03:00
|
|
|
//
|
|
|
|
warnings := vet.errors.filter(it.kind == .warning)
|
|
|
|
errors := vet.errors.filter(it.kind == .error)
|
|
|
|
errors_vfmt := vet.errors.filter(it.kind == .error && it.fix == .vfmt)
|
2021-01-25 14:08:43 +03:00
|
|
|
if show_warnings {
|
2021-01-10 19:48:39 +03:00
|
|
|
for err in warnings {
|
|
|
|
eprintln('$err.file_path:$err.pos.line_nr: warning: $err.message')
|
2020-07-19 20:58:34 +03:00
|
|
|
}
|
2021-01-10 19:48:39 +03:00
|
|
|
}
|
|
|
|
for err in errors {
|
|
|
|
eprintln('$err.file_path:$err.pos.line_nr: error: $err.message')
|
|
|
|
}
|
|
|
|
if errors_vfmt.len > 0 {
|
2020-07-19 20:58:34 +03:00
|
|
|
eprintln('NB: You can run `v fmt -w file.v` to fix these automatically')
|
2021-01-10 19:48:39 +03:00
|
|
|
}
|
|
|
|
if 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
|
|
|
fn (mut v Vet) error(msg string, line int, fix vet.FixKind) {
|
|
|
|
pos := token.Position{
|
|
|
|
line_nr: line + 1
|
|
|
|
}
|
|
|
|
v.errors << vet.Error{
|
|
|
|
message: msg
|
|
|
|
file_path: v.file
|
|
|
|
pos: pos
|
|
|
|
kind: .error
|
|
|
|
fix: fix
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn (mut v Vet) warn(msg string, line int, fix vet.FixKind) {
|
|
|
|
pos := token.Position{
|
|
|
|
line_nr: line + 1
|
|
|
|
}
|
|
|
|
v.errors << vet.Error{
|
|
|
|
message: msg
|
|
|
|
file_path: v.file
|
|
|
|
pos: pos
|
|
|
|
kind: .warning
|
|
|
|
fix: fix
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// vet_file vets the file read from `path`.
|
|
|
|
fn (mut vt Vet) vet_file(path string, is_regression_test bool) {
|
|
|
|
if path.contains('/tests/') && !is_regression_test {
|
|
|
|
// skip all /tests/ files, since usually their content is not
|
|
|
|
// important enough to be documented/vetted, and they may even
|
|
|
|
// contain intentionally invalid code.
|
|
|
|
eprintln("skipping test file: '$path' ...")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
vt.file = path
|
2020-07-04 15:29:00 +03:00
|
|
|
mut prefs := pref.new_preferences()
|
|
|
|
prefs.is_vet = true
|
|
|
|
table := table.new_table()
|
2021-01-10 19:48:39 +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`.
|
|
|
|
fn (mut vet Vet) vet_line(lines []string, line string, lnumber int) {
|
|
|
|
// Vet public functions
|
2021-01-23 11:33:22 +03:00
|
|
|
if line.starts_with('pub fn') || (line.starts_with('fn ') && !(line.starts_with('fn C.')
|
|
|
|
|| line.starts_with('fn main'))) {
|
2021-01-10 19:48:39 +03:00
|
|
|
// Scan function declarations for missing documentation
|
|
|
|
is_pub_fn := line.starts_with('pub fn')
|
|
|
|
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 '' }
|
2021-01-21 14:46:50 +03:00
|
|
|
if line.len < fn_idx + 5 {
|
|
|
|
return ''
|
|
|
|
}
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
2021-01-21 14:46:50 +03:00
|
|
|
if tokens.len > 0 {
|
|
|
|
return tokens[0].all_before('(')
|
|
|
|
}
|
|
|
|
return ''
|
2021-01-10 19:48:39 +03:00
|
|
|
}
|
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if grab {
|
|
|
|
clean_line := line.all_before_last('{').trim(' ')
|
|
|
|
if is_pub_fn {
|
|
|
|
vet.warn('Function documentation seems to be missing for "$clean_line".',
|
|
|
|
lnumber, .doc)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
fn_name := ident_fn_name(line)
|
|
|
|
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('// $fn_name ') {
|
|
|
|
grab = false
|
|
|
|
break
|
2021-01-21 14:46:50 +03:00
|
|
|
} else if prev_line.starts_with('// $fn_name') {
|
|
|
|
grab = false
|
|
|
|
if is_pub_fn {
|
|
|
|
clean_line := line.all_before_last('{').trim(' ')
|
|
|
|
vet.warn('The documentation for "$clean_line" seems incomplete.',
|
|
|
|
lnumber, .doc)
|
|
|
|
}
|
|
|
|
break
|
2021-01-10 19:48:39 +03:00
|
|
|
} else if prev_line.starts_with('[') {
|
|
|
|
tags << collect_tags(prev_line)
|
|
|
|
continue
|
|
|
|
} else if prev_line.starts_with('//') { // Single-line comment
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if grab {
|
|
|
|
clean_line := line.all_before_last('{').trim(' ')
|
|
|
|
if is_pub_fn {
|
|
|
|
vet.warn('A function name is missing from the documentation of "$clean_line".',
|
|
|
|
lnumber, .doc)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-06-27 15:50:04 +03:00
|
|
|
}
|