parser,checker: support `[generated] module xyz` (turns off notices). Use it for `infix.v`.

pull/12802/head^2
Delyan Angelov 2021-12-12 00:58:38 +02:00
parent 9b7a50b1a2
commit 79de408ef0
No known key found for this signature in database
GPG Key ID: 66886C0F12D595ED
6 changed files with 18 additions and 7 deletions

View File

@ -668,6 +668,7 @@ pub:
mod Module // the module of the source file (from `module xyz` at the top) mod Module // the module of the source file (from `module xyz` at the top)
global_scope &Scope global_scope &Scope
is_test bool // true for _test.v files is_test bool // true for _test.v files
is_generated bool // true for `[generated] module xyz` files; turn off notices
pub mut: pub mut:
path string // absolute path of the source file - '/projects/v/file.v' path string // absolute path of the source file - '/projects/v/file.v'
path_base string // file name - 'file.v' (useful for tracing) path_base string // file name - 'file.v' (useful for tracing)

View File

@ -5,7 +5,6 @@ module checker
import v.ast import v.ast
import v.token import v.token
import os
// TODO: promote(), check_types(), symmetric_check() and check() overlap - should be rearranged // TODO: promote(), check_types(), symmetric_check() and check() overlap - should be rearranged
pub fn (mut c Checker) check_types(got ast.Type, expected ast.Type) bool { pub fn (mut c Checker) check_types(got ast.Type, expected ast.Type) bool {
@ -260,10 +259,6 @@ pub fn (mut c Checker) check_matching_function_symbols(got_type_sym &ast.TypeSym
return true return true
} }
// TODO: remove this exception, by using a more general mechanism for opting out
// generated code, from warnings/notices, that are targeted at humans:
const infix_v_exception = os.join_path('vlib', 'v', 'eval', 'infix.v')
fn (mut c Checker) check_shift(mut node ast.InfixExpr, left_type ast.Type, right_type ast.Type) ast.Type { fn (mut c Checker) check_shift(mut node ast.InfixExpr, left_type ast.Type, right_type ast.Type) ast.Type {
if !left_type.is_int() { if !left_type.is_int() {
left_sym := c.table.get_type_symbol(left_type) left_sym := c.table.get_type_symbol(left_type)
@ -323,7 +318,7 @@ fn (mut c Checker) check_shift(mut node ast.InfixExpr, left_type ast.Type, right
left_sym_final := c.table.get_final_type_symbol(left_type) left_sym_final := c.table.get_final_type_symbol(left_type)
left_type_final := ast.Type(left_sym_final.idx) left_type_final := ast.Type(left_sym_final.idx)
if node.op == .left_shift && left_type_final.is_signed() && !(c.inside_unsafe if node.op == .left_shift && left_type_final.is_signed() && !(c.inside_unsafe
&& c.file.path.ends_with(checker.infix_v_exception)) { && c.is_generated) {
c.note('shifting a value from a signed type `$left_sym_final.name` can change the sign', c.note('shifting a value from a signed type `$left_sym_final.name` can change the sign',
node.left.position()) node.left.position())
} }

View File

@ -73,6 +73,7 @@ pub mut:
scope_returns bool scope_returns bool
mod string // current module name mod string // current module name
is_builtin_mod bool // true inside the 'builtin', 'os' or 'strconv' modules; TODO: remove the need for special casing this is_builtin_mod bool // true inside the 'builtin', 'os' or 'strconv' modules; TODO: remove the need for special casing this
is_generated bool // true for `[generated] module xyz` .v files
inside_unsafe bool // true inside `unsafe {}` blocks inside_unsafe bool // true inside `unsafe {}` blocks
inside_const bool // true inside `const ( ... )` blocks inside_const bool // true inside `const ( ... )` blocks
inside_anon_fn bool // true inside `fn() { ... }()` inside_anon_fn bool // true inside `fn() { ... }()`
@ -248,6 +249,7 @@ pub fn (mut c Checker) change_current_file(file &ast.File) {
c.file = unsafe { file } c.file = unsafe { file }
c.vmod_file_content = '' c.vmod_file_content = ''
c.mod = file.mod.name c.mod = file.mod.name
c.is_generated = file.is_generated
} }
pub fn (mut c Checker) check_files(ast_files []&ast.File) { pub fn (mut c Checker) check_files(ast_files []&ast.File) {
@ -4707,6 +4709,9 @@ pub fn (mut c Checker) note(message string, pos token.Position) {
c.should_abort = true c.should_abort = true
return return
} }
if c.is_generated {
return
}
mut details := '' mut details := ''
if c.error_details.len > 0 { if c.error_details.len > 0 {
details = c.error_details.join('\n') details = c.error_details.join('\n')

View File

@ -5,7 +5,8 @@ import strings
import os import os
const ( const (
header = 'module eval header = '[generated]
module eval
import v.token import v.token
import v.ast import v.ast
fn(e Eval)infix_expr(left Object,right Object,op token.Kind,expecting ast.Type)Object{match op{' fn(e Eval)infix_expr(left Object,right Object,op token.Kind,expecting ast.Type)Object{match op{'

View File

@ -1,3 +1,4 @@
[generated]
module eval module eval
import v.token import v.token

View File

@ -51,6 +51,7 @@ mut:
mod string // current module name mod string // current module name
is_manualfree bool // true when `[manualfree] module abc`, makes *all* fns in the current .v file, opt out of autofree is_manualfree bool // true when `[manualfree] module abc`, makes *all* fns in the current .v file, opt out of autofree
has_globals bool // `[has_globals] module abc` - allow globals declarations, even without -enable-globals, in that single .v file __only__ has_globals bool // `[has_globals] module abc` - allow globals declarations, even without -enable-globals, in that single .v file __only__
is_generated bool // `[generated] module abc` - turn off compiler notices for that single .v file __only__.
attrs []ast.Attr // attributes before next decl stmt attrs []ast.Attr // attributes before next decl stmt
expr_mod string // for constructing full type names in parse_type() expr_mod string // for constructing full type names in parse_type()
scope &ast.Scope scope &ast.Scope
@ -310,6 +311,7 @@ pub fn (mut p Parser) parse() &ast.File {
path: p.file_name path: p.file_name
path_base: p.file_base path_base: p.file_base
is_test: p.inside_test_file is_test: p.inside_test_file
is_generated: p.is_generated
nr_lines: p.scanner.line_nr nr_lines: p.scanner.line_nr
nr_bytes: p.scanner.text.len nr_bytes: p.scanner.text.len
mod: module_decl mod: module_decl
@ -1760,6 +1762,9 @@ pub fn (mut p Parser) note_with_pos(s string, pos token.Position) {
if p.pref.skip_warnings { if p.pref.skip_warnings {
return return
} }
if p.is_generated {
return
}
if p.pref.output_mode == .stdout && !p.pref.check_only { if p.pref.output_mode == .stdout && !p.pref.check_only {
ferror := util.formatted_error('notice:', s, p.file_name, pos) ferror := util.formatted_error('notice:', s, p.file_name, pos)
eprintln(ferror) eprintln(ferror)
@ -2844,6 +2849,9 @@ fn (mut p Parser) module_decl() ast.Module {
'manualfree' { 'manualfree' {
p.is_manualfree = true p.is_manualfree = true
} }
'generated' {
p.is_generated = true
}
'has_globals' { 'has_globals' {
if p.inside_vlib_file { if p.inside_vlib_file {
p.has_globals = true p.has_globals = true