From 64bc2fb40a51b0c68e4f47ce0f6da8deb8e9af11 Mon Sep 17 00:00:00 2001 From: Delyan Angelov Date: Sun, 7 Mar 2021 11:09:17 +0200 Subject: [PATCH] v: fix `[if debug] fn abc(){} ... abc()` --- vlib/v/ast/ast.v | 1 + vlib/v/parser/fn.v | 6 ++++++ vlib/v/pref/pref.v | 3 +++ vlib/v/table/attr.v | 9 +++++++++ vlib/v/table/table.v | 11 ++++++----- 5 files changed, 25 insertions(+), 5 deletions(-) diff --git a/vlib/v/ast/ast.v b/vlib/v/ast/ast.v index 418cfd55a8..271e231e38 100644 --- a/vlib/v/ast/ast.v +++ b/vlib/v/ast/ast.v @@ -319,6 +319,7 @@ pub: is_manualfree bool // true, when [manualfree] is used on a fn is_main bool // true for `fn main()` is_test bool // true for `fn test_abcde` + is_conditional bool // true for `[if abc] fn abc(){}` receiver Field receiver_pos token.Position // `(u User)` in `fn (u User) name()` position is_method bool diff --git a/vlib/v/parser/fn.v b/vlib/v/parser/fn.v index ec6e3fc331..133049e95a 100644 --- a/vlib/v/parser/fn.v +++ b/vlib/v/parser/fn.v @@ -171,6 +171,7 @@ fn (mut p Parser) fn_decl() ast.FnDecl { is_manualfree := p.is_manualfree || p.attrs.contains('manualfree') is_deprecated := p.attrs.contains('deprecated') is_direct_arr := p.attrs.contains('direct_array_access') + is_conditional, conditional_ctdefine := p.attrs.has_comptime_define() mut is_unsafe := p.attrs.contains('unsafe') is_pub := p.tok.kind == .key_pub if is_pub { @@ -331,6 +332,8 @@ fn (mut p Parser) fn_decl() ast.FnDecl { is_unsafe: is_unsafe is_main: is_main is_test: is_test + is_conditional: is_conditional + ctdefine: conditional_ctdefine no_body: no_body mod: p.mod attrs: p.attrs @@ -358,6 +361,8 @@ fn (mut p Parser) fn_decl() ast.FnDecl { is_unsafe: is_unsafe is_main: is_main is_test: is_test + is_conditional: is_conditional + ctdefine: conditional_ctdefine no_body: no_body mod: p.mod attrs: p.attrs @@ -397,6 +402,7 @@ fn (mut p Parser) fn_decl() ast.FnDecl { is_variadic: is_variadic is_main: is_main is_test: is_test + is_conditional: is_conditional receiver: ast.Field{ name: rec.name typ: rec.typ diff --git a/vlib/v/pref/pref.v b/vlib/v/pref/pref.v index 90adf13484..24d0f0ab6c 100644 --- a/vlib/v/pref/pref.v +++ b/vlib/v/pref/pref.v @@ -451,6 +451,9 @@ pub fn parse_args(known_external_commands []string, args []string) (&Preferences } } } + if res.is_debug { + parse_define(mut res, 'debug') + } // res.use_cache = true if command != 'doc' && res.out_name.ends_with('.v') { eprintln('Cannot save output binary in a .v file.') diff --git a/vlib/v/table/attr.v b/vlib/v/table/attr.v index 106df986d9..3786f80efc 100644 --- a/vlib/v/table/attr.v +++ b/vlib/v/table/attr.v @@ -47,3 +47,12 @@ pub fn (attrs []Attr) contains(str string) bool { } return false } + +pub fn (attrs []Attr) has_comptime_define() (bool, string) { + for a in attrs { + if a.is_comptime_define { + return true, a.name + } + } + return false, '' +} diff --git a/vlib/v/table/table.v b/vlib/v/table/table.v index 0f5540854e..8149b86652 100644 --- a/vlib/v/table/table.v +++ b/vlib/v/table/table.v @@ -32,12 +32,13 @@ pub: language Language generic_names []string is_pub bool - is_deprecated bool - is_unsafe bool + is_deprecated bool // `[deprecated] fn abc(){}` + is_unsafe bool // `[unsafe] fn abc(){}` is_placeholder bool - is_main bool - is_test bool - no_body bool + is_main bool // `fn main(){}` + is_test bool // `fn test_abc(){}` + is_conditional bool // `[if abc]fn(){}` + no_body bool // a pure declaration like `fn abc(x int)`; used in .vh files, C./JS. fns. mod string ctdefine string // compile time define. "myflag", when [if myflag] tag attrs []Attr