From e1655fff1f1db582d951e454396af11607e1dc7f Mon Sep 17 00:00:00 2001 From: Felipe Pena Date: Thu, 25 Jan 2024 11:58:52 -0300 Subject: [PATCH] v: enable `@[export]` for global variables too (#20649) --- vlib/v/ast/ast.v | 3 ++- vlib/v/gen/c/cgen.v | 3 +++ vlib/v/gen/c/testdata/global_export_nix.c.must_have | 1 + vlib/v/gen/c/testdata/global_export_nix.vv | 6 ++++++ vlib/v/markused/walker.v | 2 +- vlib/v/parser/parser.v | 3 +++ 6 files changed, 16 insertions(+), 2 deletions(-) create mode 100644 vlib/v/gen/c/testdata/global_export_nix.c.must_have create mode 100644 vlib/v/gen/c/testdata/global_export_nix.vv diff --git a/vlib/v/ast/ast.v b/vlib/v/ast/ast.v index f2309df35..06cc71d1d 100644 --- a/vlib/v/ast/ast.v +++ b/vlib/v/ast/ast.v @@ -865,8 +865,9 @@ pub: has_expr bool pos token.Pos typ_pos token.Pos - is_markused bool // an explicit `[markused]` tag; the global will NOT be removed by `-skip-unused` + is_markused bool // an explicit `@[markused]` tag; the global will NOT be removed by `-skip-unused` is_volatile bool + is_exported bool // an explicit `@[export]` tag; the global will NOT be removed by `-skip-unused` pub mut: expr Expr typ Type diff --git a/vlib/v/gen/c/cgen.v b/vlib/v/gen/c/cgen.v index a59017596..63d80db3b 100644 --- a/vlib/v/gen/c/cgen.v +++ b/vlib/v/gen/c/cgen.v @@ -5887,6 +5887,9 @@ fn (mut g Gen) global_decl(node ast.GlobalDecl) { if node.attrs.contains('weak') { attributes += 'VWEAK ' } + if node.attrs.contains('export') { + attributes += 'VV_EXPORTED_SYMBOL ' + } for field in node.fields { if g.pref.skip_unused { if field.name !in g.table.used_globals { diff --git a/vlib/v/gen/c/testdata/global_export_nix.c.must_have b/vlib/v/gen/c/testdata/global_export_nix.c.must_have new file mode 100644 index 000000000..422906736 --- /dev/null +++ b/vlib/v/gen/c/testdata/global_export_nix.c.must_have @@ -0,0 +1 @@ + string VV_EXPORTED_SYMBOL global_exported; // global4 \ No newline at end of file diff --git a/vlib/v/gen/c/testdata/global_export_nix.vv b/vlib/v/gen/c/testdata/global_export_nix.vv new file mode 100644 index 000000000..be3a8abe4 --- /dev/null +++ b/vlib/v/gen/c/testdata/global_export_nix.vv @@ -0,0 +1,6 @@ +// vtest vflags: -enable-globals + +const foo = 'bar' + +@[export] +__global global_exported = foo + 'qux' diff --git a/vlib/v/markused/walker.v b/vlib/v/markused/walker.v index 09a5d2011..4206d5110 100644 --- a/vlib/v/markused/walker.v +++ b/vlib/v/markused/walker.v @@ -91,7 +91,7 @@ pub fn (mut w Walker) mark_markused_consts() { pub fn (mut w Walker) mark_markused_globals() { for gkey, mut globalfield in w.all_globals { - if globalfield.is_markused { + if globalfield.is_markused || globalfield.is_exported { w.mark_global_as_used(gkey) } } diff --git a/vlib/v/parser/parser.v b/vlib/v/parser/parser.v index da9fbb4dc..10b8df5cd 100644 --- a/vlib/v/parser/parser.v +++ b/vlib/v/parser/parser.v @@ -3955,8 +3955,10 @@ fn (mut p Parser) global_decl() ast.GlobalDecl { } mut is_markused := false + mut is_exported := false for ga in attrs { match ga.name { + 'export' { is_exported = true } 'markused' { is_markused = true } else {} } @@ -4038,6 +4040,7 @@ fn (mut p Parser) global_decl() ast.GlobalDecl { comments: comments is_markused: is_markused is_volatile: is_volatile + is_exported: is_exported } fields << field p.table.global_scope.register(field) -- 2.39.5