2020-06-27 15:50:04 +03:00
|
|
|
// Copyright (c) 2019-2020 Alexander Medvednikov. All rights reserved.
|
|
|
|
// Use of this source code is governed by an MIT license that can be found in the LICENSE file.
|
|
|
|
module main
|
|
|
|
|
|
|
|
import v.vet
|
|
|
|
import v.pref
|
|
|
|
import v.parser
|
|
|
|
import v.util
|
|
|
|
import v.table
|
|
|
|
import os
|
2020-07-04 15:29:00 +03:00
|
|
|
import os.cmdline
|
|
|
|
|
|
|
|
struct VetOptions {
|
|
|
|
is_verbose bool
|
2020-10-06 08:07:50 +03:00
|
|
|
mut:
|
2021-01-09 17:11:49 +03:00
|
|
|
errors []vet.Error
|
2020-07-04 15:29:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
fn (vet_options &VetOptions) vprintln(s string) {
|
|
|
|
if !vet_options.is_verbose {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
println(s)
|
|
|
|
}
|
2020-06-27 15:50:04 +03:00
|
|
|
|
|
|
|
fn main() {
|
|
|
|
args := util.join_env_vflags_and_os_args()
|
2020-07-04 15:29:00 +03:00
|
|
|
paths := cmdline.only_non_options(cmdline.options_after(args, ['vet']))
|
2020-10-06 08:07:50 +03:00
|
|
|
mut vet_options := VetOptions{
|
2020-07-04 15:29:00 +03:00
|
|
|
is_verbose: '-verbose' in args || '-v' in args
|
2020-06-27 15:50:04 +03:00
|
|
|
}
|
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
|
|
|
|
}
|
2021-01-09 17:11:49 +03:00
|
|
|
if path.ends_with('_test.v') ||
|
|
|
|
(path.contains('/tests/') && !path.contains('cmd/tools/vvet/tests/')) {
|
2020-08-12 20:33:51 +03:00
|
|
|
eprintln('skipping $path')
|
2020-08-12 19:56:06 +03:00
|
|
|
continue
|
|
|
|
}
|
2020-08-25 18:46:33 +03:00
|
|
|
if path.ends_with('.v') || path.ends_with('.vv') {
|
2020-07-04 15:29:00 +03:00
|
|
|
vet_options.vet_file(path)
|
|
|
|
} else if os.is_dir(path) {
|
|
|
|
vet_options.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 {
|
2020-08-12 19:56:06 +03:00
|
|
|
if file.ends_with('_test.v') || file.contains('/tests/') { // TODO copy pasta
|
|
|
|
continue
|
|
|
|
}
|
2020-07-04 15:29:00 +03:00
|
|
|
vet_options.vet_file(file)
|
|
|
|
}
|
2020-06-27 15:50:04 +03:00
|
|
|
}
|
|
|
|
}
|
2020-07-19 20:58:34 +03:00
|
|
|
if vet_options.errors.len > 0 {
|
2021-01-09 17:11:49 +03:00
|
|
|
for err in vet_options.errors.filter(it.kind == .error) {
|
|
|
|
eprintln('$err.file_path:$err.pos.line_nr: $err.message')
|
2020-07-19 20:58:34 +03:00
|
|
|
}
|
|
|
|
eprintln('NB: You can run `v fmt -w file.v` to fix these automatically')
|
2021-01-09 17:11:49 +03:00
|
|
|
/*
|
|
|
|
for err in vet_options.errors.filter(it.kind == .warning) {
|
|
|
|
eprintln('$err.file_path:$err.pos.line_nr: err.message')
|
|
|
|
}
|
|
|
|
*/
|
2020-07-19 20:58:34 +03:00
|
|
|
exit(1)
|
|
|
|
}
|
2020-06-27 15:50:04 +03:00
|
|
|
}
|
|
|
|
|
2020-10-06 08:07:50 +03:00
|
|
|
fn (mut vet_options VetOptions) vet_file(path string) {
|
2020-07-04 15:29:00 +03:00
|
|
|
mut prefs := pref.new_preferences()
|
|
|
|
prefs.is_vet = true
|
|
|
|
table := table.new_table()
|
|
|
|
vet_options.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
|
2020-10-06 10:02:35 +03:00
|
|
|
vet_options.errors << errors
|
2020-06-27 15:50:04 +03:00
|
|
|
}
|