diff options
author | Philip Herron <philip.herron@embecosm.com> | 2022-01-28 10:08:44 +0000 |
---|---|---|
committer | Philip Herron <philip.herron@embecosm.com> | 2022-01-28 10:08:44 +0000 |
commit | ebfbdf962fb3e65e17ad8a0477558549e924dd22 (patch) | |
tree | ab049e20f1637ab091367922befeeafcc6d539d1 /gcc | |
parent | 1fc2b540800d25af1c70e90439e8a9077c7d07f8 (diff) | |
download | gcc-ebfbdf962fb3e65e17ad8a0477558549e924dd22.zip gcc-ebfbdf962fb3e65e17ad8a0477558549e924dd22.tar.gz gcc-ebfbdf962fb3e65e17ad8a0477558549e924dd22.tar.bz2 |
Add -frust-cfg=value option for adding config options
This adds the initial support for config expansion on custom config values
it need support for parsing options such as feature=test with apropriate
error handling withing Session::handle_cfg_option(const std::string&).
This also applies the mark_for_strip checks only on AST::Functions and
will need applied to the rest of the crate in #872.
Addresses #889
Diffstat (limited to 'gcc')
-rw-r--r-- | gcc/rust/hir/rust-ast-lower-item.h | 12 | ||||
-rw-r--r-- | gcc/rust/lang.opt | 4 | ||||
-rw-r--r-- | gcc/rust/resolve/rust-ast-resolve-item.h | 3 | ||||
-rw-r--r-- | gcc/rust/resolve/rust-ast-resolve-toplevel.h | 3 | ||||
-rw-r--r-- | gcc/rust/rust-session-manager.cc | 21 | ||||
-rw-r--r-- | gcc/rust/rust-session-manager.h | 3 | ||||
-rw-r--r-- | gcc/testsuite/rust/compile/cfg1.rs | 31 | ||||
-rw-r--r-- | gcc/testsuite/rust/execute/torture/cfg1.rs | 32 |
8 files changed, 98 insertions, 11 deletions
diff --git a/gcc/rust/hir/rust-ast-lower-item.h b/gcc/rust/hir/rust-ast-lower-item.h index 23ff5b2..b67016c 100644 --- a/gcc/rust/hir/rust-ast-lower-item.h +++ b/gcc/rust/hir/rust-ast-lower-item.h @@ -44,15 +44,6 @@ public: { ASTLoweringItem resolver; item->accept_vis (resolver); - - // this is useful for debugging - // if (resolver.translated == nullptr) - // { - // rust_fatal_error (item->get_locus (), "failed to lower: %s", - // item->as_string ().c_str ()); - // return nullptr; - // } - return resolver.translated; } @@ -419,6 +410,9 @@ public: void visit (AST::Function &function) override { + if (function.is_marked_for_strip ()) + return; + std::vector<std::unique_ptr<HIR::WhereClauseItem>> where_clause_items; for (auto &item : function.get_where_clause ().get_items ()) { diff --git a/gcc/rust/lang.opt b/gcc/rust/lang.opt index 42a2301..ddd2868 100644 --- a/gcc/rust/lang.opt +++ b/gcc/rust/lang.opt @@ -59,6 +59,10 @@ Enum(frust_mangling) String(legacy) Value(0) EnumValue Enum(frust_mangling) String(v0) Value(1) +frust-cfg= +Rust Joined RejectNegative +-frust-cfg=<name> Set a config expansion option + o Rust Joined Separate ; Documented in common.opt diff --git a/gcc/rust/resolve/rust-ast-resolve-item.h b/gcc/rust/resolve/rust-ast-resolve-item.h index b92dc96..200c5ff 100644 --- a/gcc/rust/resolve/rust-ast-resolve-item.h +++ b/gcc/rust/resolve/rust-ast-resolve-item.h @@ -362,6 +362,9 @@ public: void visit (AST::Function &function) override { + if (function.is_marked_for_strip ()) + return; + NodeId scope_node_id = function.get_node_id (); resolver->get_name_scope ().push (scope_node_id); resolver->get_type_scope ().push (scope_node_id); diff --git a/gcc/rust/resolve/rust-ast-resolve-toplevel.h b/gcc/rust/resolve/rust-ast-resolve-toplevel.h index 16acf13..4993649 100644 --- a/gcc/rust/resolve/rust-ast-resolve-toplevel.h +++ b/gcc/rust/resolve/rust-ast-resolve-toplevel.h @@ -220,6 +220,9 @@ public: void visit (AST::Function &function) override { + if (function.is_marked_for_strip ()) + return; + auto path = prefix.append (ResolveFunctionItemToCanonicalPath::resolve (function)); resolver->get_name_scope ().insert ( diff --git a/gcc/rust/rust-session-manager.cc b/gcc/rust/rust-session-manager.cc index 9650086..c930f55 100644 --- a/gcc/rust/rust-session-manager.cc +++ b/gcc/rust/rust-session-manager.cc @@ -353,9 +353,11 @@ Session::handle_option ( case OPT_I: // TODO: add search path break; + case OPT_L: // TODO: add library link path or something break; + case OPT_frust_crate_: // set the crate name if (arg != nullptr) @@ -363,6 +365,7 @@ Session::handle_option ( else ret = false; break; + case OPT_frust_dump_: // enable dump and return whether this was successful if (arg != nullptr) @@ -374,17 +377,31 @@ Session::handle_option ( ret = false; } break; + case OPT_frust_mangling_: Compile::Mangler::set_mangling (flag_rust_mangling); - // no option handling for -o + break; + + case OPT_frust_cfg_: + ret = handle_cfg_option (std::string (arg)); + break; + default: - // return 1 to indicate option is valid break; } return ret; } +bool +Session::handle_cfg_option (const std::string &value) +{ + // rustc doesn't seem to error on any duplicate key + // TODO handle feature=bla and relevant error handling in parsing + options.target_data.insert_key (value); + return true; +} + /* Enables a certain dump depending on the name passed in. Returns true if name * is valid, false otherwise. */ bool diff --git a/gcc/rust/rust-session-manager.h b/gcc/rust/rust-session-manager.h index 90f7f4b..ea0523b 100644 --- a/gcc/rust/rust-session-manager.h +++ b/gcc/rust/rust-session-manager.h @@ -286,6 +286,9 @@ private: * macros, maybe build test harness in future, AST validation, maybe create * macro crate (if not rustdoc).*/ void expansion (AST::Crate &crate); + + // handle cfg_option + bool handle_cfg_option (const std::string &data); }; } // namespace Rust diff --git a/gcc/testsuite/rust/compile/cfg1.rs b/gcc/testsuite/rust/compile/cfg1.rs new file mode 100644 index 0000000..6984f04 --- /dev/null +++ b/gcc/testsuite/rust/compile/cfg1.rs @@ -0,0 +1,31 @@ +// { dg-additional-options "-w" } +extern "C" { + fn printf(s: *const i8, ...); +} + +#[cfg(A)] +fn test() { + unsafe { + let a = "test1\n\0"; + let b = a as *const str; + let c = b as *const i8; + + printf(c); + } +} + +#[cfg(B)] +fn test() { + unsafe { + let a = "test2\n\0"; + let b = a as *const str; + let c = b as *const i8; + + printf(c); + } +} + +fn main() { + test(); + // { dg-error "Cannot find path .test. in this scope" "" { target *-*-* } .-1 } +} diff --git a/gcc/testsuite/rust/execute/torture/cfg1.rs b/gcc/testsuite/rust/execute/torture/cfg1.rs new file mode 100644 index 0000000..d3c5629 --- /dev/null +++ b/gcc/testsuite/rust/execute/torture/cfg1.rs @@ -0,0 +1,32 @@ +// { dg-additional-options "-w -frust-cfg=A" } +// { dg-output "test1\n" } +extern "C" { + fn printf(s: *const i8, ...); +} + +#[cfg(A)] +fn test() { + unsafe { + let a = "test1\n\0"; + let b = a as *const str; + let c = b as *const i8; + + printf(c); + } +} + +#[cfg(B)] +fn test() { + unsafe { + let a = "test2\n\0"; + let b = a as *const str; + let c = b as *const i8; + + printf(c); + } +} + +fn main() -> i32 { + test(); + 0 +} |