builder: skip warnings; pref: move pref creation to pref.v

pull/4923/head
Alexander Medvednikov 2020-05-16 22:45:38 +02:00
parent 3e4d99a0e3
commit 0845b2cfbe
5 changed files with 186 additions and 171 deletions

169
cmd/v/v.v
View File

@ -5,7 +5,6 @@ module main
import help
import os
import os.cmdline
import v.table
import v.doc
import v.pref
@ -21,10 +20,6 @@ const (
'build-examples', 'build-vbinaries', 'setup-freetype'
]
list_of_flags_that_allow_duplicates = ['cc', 'd', 'define', 'cf', 'cflags']
list_of_flags_with_param = [
'o'
'output', 'd', 'define', 'b', 'backend', 'cc', 'os', 'target-os', 'arch', 'csource'
'cf', 'cflags', 'path']
)
fn main() {
@ -49,7 +44,7 @@ fn main_v() {
return
}
args_and_flags := util.join_env_vflags_and_os_args()[1..]
prefs, command := parse_args(args_and_flags)
prefs, command := pref.parse_args(args_and_flags)
if prefs.is_verbose {
println('command = "$command"')
println(util.full_v_version())
@ -115,168 +110,6 @@ fn main_v() {
exit(1)
}
fn parse_args(args []string) (&pref.Preferences, string) {
mut res := &pref.Preferences{}
mut command := ''
mut command_pos := 0
// for i, arg in args {
for i := 0; i < args.len; i++ {
arg := args[i]
current_args := args[i..]
match arg {
'-v' {
res.is_verbose = true
}
'-silent' {
res.output_mode = .silent
}
'-cg' {
res.is_debug = true
}
'-repl' {
res.is_repl = true
}
'-live' {
res.is_livemain = true
}
'-sharedlive' {
res.is_liveshared = true
res.is_shared = true
}
'-shared' {
res.is_shared = true
}
'--enable-globals' {
res.enable_globals = true
}
'-autofree' {
res.autofree = true
}
'-compress' {
res.compress = true
}
'-freestanding' {
res.is_bare = true
}
'-prof', '-profile' {
res.profile_file = cmdline.option(current_args, '-profile', '-')
res.is_prof = true
i++
}
'-profile-no-inline' {
res.profile_no_inline = true
}
'-prod' {
res.is_prod = true
}
'-stats' {
res.is_stats = true
}
'-obfuscate' {
res.obfuscate = true
}
'-translated' {
res.translated = true
}
'-showcc' {
res.show_cc = true
}
'-usecache' {
res.use_cache = true
}
'-keepc' {
res.keep_c = true
}
'-x64' {
res.backend = .x64
}
'-print_v_files' {
res.print_v_files = true
}
'-os' {
target_os := cmdline.option(current_args, '-os', '')
i++
target_os_kind := pref.os_from_string(target_os) or {
if target_os == 'cross' {
res.output_cross_c = true
continue
}
println('unknown operating system target `$target_os`')
exit(1)
}
res.os = target_os_kind
}
'-printfn' {
res.printfn_list << cmdline.option(current_args, '-printfn', '')
i++
}
'-cflags' {
res.cflags += ' ' + cmdline.option(current_args, '-cflags', '')
i++
}
'-define', '-d' {
if current_args.len > 1 {
define := current_args[1]
parse_define(mut res, define)
}
i++
}
'-cc' {
res.ccompiler = cmdline.option(current_args, '-cc', 'cc')
i++
}
'-o' {
res.out_name = cmdline.option(current_args, '-o', '')
i++
}
'-b' {
b := pref.backend_from_string(cmdline.option(current_args, '-b', 'c')) or {
continue
}
res.backend = b
i++
}
else {
mut should_continue := false
for flag_with_param in list_of_flags_with_param {
if '-$flag_with_param' == arg {
should_continue = true
i++
break
}
}
if should_continue {
continue
}
if !arg.starts_with('-') && command == '' {
command = arg
command_pos = i
}
}
}
}
if command.ends_with('.v') || os.exists(command) {
res.path = command
} else if command == 'run' {
res.is_run = true
if command_pos > args.len {
eprintln('v run: no v files listed')
exit(1)
}
res.path = args[command_pos + 1]
res.run_args = args[command_pos + 2..]
}
if command == 'build-module' {
res.build_mode = .build_module
res.path = args[command_pos + 1]
}
if res.is_verbose {
println('setting pref.path to "$res.path"')
}
res.fill_with_defaults()
return res, command
}
fn invoke_help_and_exit(remaining []string) {
match remaining.len {
0, 1 { help.print_and_exit('default') }

View File

@ -218,7 +218,7 @@ fn (b &Builder) print_warnings_and_errors() {
if b.pref.is_verbose && b.checker.nr_warnings > 1 {
println('$b.checker.nr_warnings warnings')
}
if b.checker.nr_warnings > 0 {
if b.checker.nr_warnings > 0 && !b.pref.skip_warnings {
for i, err in b.checker.warnings {
kind := if b.pref.is_verbose { '$err.reporter warning #$b.checker.nr_warnings:' } else { 'warning:' }
ferror := util.formatted_error(kind, err.message, err.file_path, err.pos)

View File

@ -2264,7 +2264,7 @@ fn (mut c Checker) warn_or_error(message string, pos token.Position, warn bool)
// if c.pref.is_verbose {
// print_backtrace()
// }
if warn {
if warn && !c.pref.skip_warnings {
c.nr_warnings++
wrn := errors.Warning{
reporter: errors.Reporter.checker
@ -2274,7 +2274,9 @@ fn (mut c Checker) warn_or_error(message string, pos token.Position, warn bool)
}
c.file.warnings << wrn
c.warnings << wrn
} else {
return
}
if !warn {
c.nr_errors++
if pos.line_nr !in c.error_lines {
err := errors.Error{

View File

@ -631,6 +631,9 @@ pub fn (mut p Parser) error_with_pos(s string, pos token.Position) {
}
pub fn (mut p Parser) warn_with_pos(s string, pos token.Position) {
if p.pref.skip_warnings {
return
}
if p.pref.output_mode == .stdout {
ferror := util.formatted_error('warning:', s, p.file_name, pos)
eprintln(ferror)

View File

@ -3,6 +3,9 @@
// that can be found in the LICENSE file.
module pref
import os.cmdline
import os
pub enum BuildMode {
// `v program.v'
// Build user code only, and add pre-compiled vlib (`cc program.o builtin.o os.o...`)
@ -23,6 +26,13 @@ pub enum Backend {
x64 // The x64 backend
}
const (
list_of_flags_with_param = [
'o'
'output', 'd', 'define', 'b', 'backend', 'cc', 'os', 'target-os', 'arch', 'csource'
'cf', 'cflags', 'path']
)
pub struct Preferences {
pub mut:
os OS // the OS to compile for
@ -90,8 +100,175 @@ pub mut:
printfn_list []string // a list of generated function names, whose source should be shown, for debugging
print_v_files bool // when true, just print the list of all parsed .v files then stop.
skip_running bool // when true, do no try to run the produced file (set by b.cc(), when -o x.c or -o x.js)
skip_warnings bool // like C's "-w"
}
pub fn parse_args(args []string) (&Preferences, string) {
mut res := &pref.Preferences{}
mut command := ''
mut command_pos := 0
// for i, arg in args {
for i := 0; i < args.len; i++ {
arg := args[i]
current_args := args[i..]
match arg {
'-v' {
res.is_verbose = true
}
'-silent' {
res.output_mode = .silent
}
'-cg' {
res.is_debug = true
}
'-repl' {
res.is_repl = true
}
'-live' {
res.is_livemain = true
}
'-sharedlive' {
res.is_liveshared = true
res.is_shared = true
}
'-shared' {
res.is_shared = true
}
'--enable-globals' {
res.enable_globals = true
}
'-autofree' {
res.autofree = true
}
'-compress' {
res.compress = true
}
'-freestanding' {
res.is_bare = true
}
'-prof', '-profile' {
res.profile_file = cmdline.option(current_args, '-profile', '-')
res.is_prof = true
i++
}
'-profile-no-inline' {
res.profile_no_inline = true
}
'-prod' {
res.is_prod = true
}
'-stats' {
res.is_stats = true
}
'-obfuscate' {
res.obfuscate = true
}
'-translated' {
res.translated = true
}
'-showcc' {
res.show_cc = true
}
'-usecache' {
res.use_cache = true
}
'-keepc' {
res.keep_c = true
}
'-x64' {
res.backend = .x64
}
'-w' {
res.skip_warnings = true
}
'-print_v_files' {
res.print_v_files = true
}
'-os' {
target_os := cmdline.option(current_args, '-os', '')
i++
target_os_kind := pref.os_from_string(target_os) or {
if target_os == 'cross' {
res.output_cross_c = true
continue
}
println('unknown operating system target `$target_os`')
exit(1)
}
res.os = target_os_kind
}
'-printfn' {
res.printfn_list << cmdline.option(current_args, '-printfn', '')
i++
}
'-cflags' {
res.cflags += ' ' + cmdline.option(current_args, '-cflags', '')
i++
}
'-define', '-d' {
if current_args.len > 1 {
define := current_args[1]
parse_define(mut res, define)
}
i++
}
'-cc' {
res.ccompiler = cmdline.option(current_args, '-cc', 'cc')
i++
}
'-o' {
res.out_name = cmdline.option(current_args, '-o', '')
i++
}
'-b' {
b := pref.backend_from_string(cmdline.option(current_args, '-b', 'c')) or {
continue
}
res.backend = b
i++
}
else {
mut should_continue := false
for flag_with_param in list_of_flags_with_param {
if '-$flag_with_param' == arg {
should_continue = true
i++
break
}
}
if should_continue {
continue
}
if !arg.starts_with('-') && command == '' {
command = arg
command_pos = i
}
}
}
}
if command.ends_with('.v') || os.exists(command) {
res.path = command
} else if command == 'run' {
res.is_run = true
if command_pos > args.len {
eprintln('v run: no v files listed')
exit(1)
}
res.path = args[command_pos + 1]
res.run_args = args[command_pos + 2..]
}
if command == 'build-module' {
res.build_mode = .build_module
res.path = args[command_pos + 1]
}
if res.is_verbose {
println('setting pref.path to "$res.path"')
}
res.fill_with_defaults()
return res, command
}
pub fn backend_from_string(s string) ?Backend {
match s {
'c' {