From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 7905) id BDE99385828F; Tue, 16 Jan 2024 17:46:27 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org BDE99385828F DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1705427187; bh=pDj1MTMjuAU4xAD32evI+N2fhXRjpUr6X2hl2xBeKAo=; h=From:To:Subject:Date:From; b=OGWp1jv4WJtEbE9KDNnjdIi9owADqh70orAPcJvfaIJkX/AjUMqVn+o2PO9ub3hNo BMUeiK1Mxcww2aX/PEpdxo43F0HUJPz/0YRvfZXYBmgnFhgwWA3nIjxLxNdPqjwiwi TZPdwdhIdFSem1B73VlkqUy4a9OP7JCPx6ra39mA= MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset="utf-8" From: Arthur Cohen To: gcc-cvs@gcc.gnu.org Subject: [gcc r14-7627] gccrs: Make key and key/value config options seperate X-Act-Checkin: gcc X-Git-Author: Owen Avery X-Git-Refname: refs/heads/trunk X-Git-Oldrev: 1b34e40e5e41bdcb227b40523cd3a64c6f2fde00 X-Git-Newrev: fc281773251e2c565c0fbf3872d240615cae021a Message-Id: <20240116174627.BDE99385828F@sourceware.org> Date: Tue, 16 Jan 2024 17:46:27 +0000 (GMT) List-Id: https://gcc.gnu.org/g:fc281773251e2c565c0fbf3872d240615cae021a commit r14-7627-gfc281773251e2c565c0fbf3872d240615cae021a Author: Owen Avery Date: Fri May 19 00:05:19 2023 -0400 gccrs: Make key and key/value config options seperate gcc/rust/ChangeLog: * rust-session-manager.h: Include "rust-optional.h". (struct TargetOptions): Store values in config key/value pairs as Optional rather than std::string. * rust-session-manager.cc (TargetOptions::dump_target_options): Handle Optional values. gcc/testsuite/ChangeLog: * rust/compile/cfg6.rs: New test. Signed-off-by: Owen Avery Diff: --- gcc/rust/rust-session-manager.cc | 10 ++++--- gcc/rust/rust-session-manager.h | 55 ++++++++++++++++++++++++++++---------- gcc/testsuite/rust/compile/cfg6.rs | 4 +++ 3 files changed, 51 insertions(+), 18 deletions(-) diff --git a/gcc/rust/rust-session-manager.cc b/gcc/rust/rust-session-manager.cc index 860abab2932..c7bbe89b281 100644 --- a/gcc/rust/rust-session-manager.cc +++ b/gcc/rust/rust-session-manager.cc @@ -1090,10 +1090,12 @@ TargetOptions::dump_target_options () const for (const auto &pairs : features) { for (const auto &value : pairs.second) - out << pairs.first + ": \"" + value + "\"\n"; - - if (pairs.second.empty ()) - out << pairs.first + "\n"; + { + if (value.is_some ()) + out << pairs.first + ": \"" + value.get () + "\"\n"; + else + out << pairs.first + "\n"; + } } out.close (); diff --git a/gcc/rust/rust-session-manager.h b/gcc/rust/rust-session-manager.h index 7d8b14e5766..99abb3cad50 100644 --- a/gcc/rust/rust-session-manager.h +++ b/gcc/rust/rust-session-manager.h @@ -30,6 +30,8 @@ #include "coretypes.h" #include "options.h" +#include "rust-optional.h" + namespace Rust { // parser forward decl template class Parser; @@ -49,13 +51,17 @@ struct TargetOptions { /* TODO: maybe make private and access through helpers to allow changes to * impl */ - std::unordered_map > features; + std::unordered_map>> + features; public: // Returns whether a key is defined in the feature set. bool has_key (std::string key) const { - return features.find (key) != features.end (); + auto it = features.find (key); + return it != features.end () + && it->second.find (Optional::none ()) + != it->second.end (); } // Returns whether a key exists with the given value in the feature set. @@ -65,7 +71,7 @@ public: if (it != features.end ()) { auto set = it->second; - auto it2 = set.find (value); + auto it2 = set.find (Optional::some (value)); if (it2 != set.end ()) return true; } @@ -80,8 +86,8 @@ public: if (it != features.end ()) { auto set = it->second; - if (set.size () == 1) - return *set.begin (); + if (set.size () == 1 && set.begin ()->is_some ()) + return set.begin ()->get (); } return ""; } @@ -90,10 +96,17 @@ public: * set if no key is found. */ std::unordered_set get_values_for_key (std::string key) const { + std::unordered_set ret; + auto it = features.find (key); - if (it != features.end ()) - return it->second; - return {}; + if (it == features.end ()) + return {}; + + for (auto &val : it->second) + if (val.is_some ()) + ret.insert (val.get ()); + + return ret; } /* Inserts a key (no value) into the feature set. This will do nothing if @@ -101,17 +114,31 @@ public: * (i.e. whether key already existed). */ bool insert_key (std::string key) { - return features - .insert (std::make_pair (key, std::unordered_set ())) - .second; + auto it = features.find (key); + + if (it == features.end ()) + it = features + .insert ( + std::make_pair (std::move (key), + std::unordered_set> ())) + .first; + + return it->second.insert (Optional::none ()).second; } // Inserts a key-value pair into the feature set. void insert_key_value_pair (std::string key, std::string value) { - auto existing_set = get_values_for_key (key); - existing_set.insert (std::move (value)); - features[std::move (key)] = std::move (existing_set); + auto it = features.find (key); + + if (it == features.end ()) + it = features + .insert ( + std::make_pair (std::move (key), + std::unordered_set> ())) + .first; + + it->second.insert (Optional::some (std::move (value))); } // Dump all target options to stderr. diff --git a/gcc/testsuite/rust/compile/cfg6.rs b/gcc/testsuite/rust/compile/cfg6.rs new file mode 100644 index 00000000000..19a4990c8da --- /dev/null +++ b/gcc/testsuite/rust/compile/cfg6.rs @@ -0,0 +1,4 @@ +// { dg-additional-options "-frust-cfg=A=\"B\"" } +#[cfg(A)] +pub fn foo() {} +pub fn foo() {}