]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
gccrs: Make key and key/value config options seperate
authorOwen Avery <powerboat9.gamer@gmail.com>
Fri, 19 May 2023 04:05:19 +0000 (00:05 -0400)
committerArthur Cohen <arthur.cohen@embecosm.com>
Tue, 16 Jan 2024 17:37:22 +0000 (18:37 +0100)
gcc/rust/ChangeLog:

* rust-session-manager.h:
Include "rust-optional.h".
(struct TargetOptions):
Store values in config key/value pairs as Optional<std::string> rather than std::string.
* rust-session-manager.cc
(TargetOptions::dump_target_options):
Handle Optional<std::string> values.

gcc/testsuite/ChangeLog:

* rust/compile/cfg6.rs: New test.

Signed-off-by: Owen Avery <powerboat9.gamer@gmail.com>
gcc/rust/rust-session-manager.cc
gcc/rust/rust-session-manager.h
gcc/testsuite/rust/compile/cfg6.rs [new file with mode: 0644]

index 860abab2932a84aa68e67b7f557a8737370c5977..c7bbe89b281cc4abb61739e70272a6d2e05e5b11 100644 (file)
@@ -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 ();
index 7d8b14e57669d2a53199948390e5d007965ee617..99abb3cad50e58fe5bd9f21f749f0065e7cadb4f 100644 (file)
@@ -30,6 +30,8 @@
 #include "coretypes.h"
 #include "options.h"
 
+#include "rust-optional.h"
+
 namespace Rust {
 // parser forward decl
 template <typename ManagedTokenSource> class Parser;
@@ -49,13 +51,17 @@ struct TargetOptions
 {
   /* TODO: maybe make private and access through helpers to allow changes to
    * impl */
-  std::unordered_map<std::string, std::unordered_set<std::string> > features;
+  std::unordered_map<std::string, std::unordered_set<Optional<std::string>>>
+    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<std::string>::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<std::string>::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<std::string> get_values_for_key (std::string key) const
   {
+    std::unordered_set<std::string> 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<std::string> ()))
-      .second;
+    auto it = features.find (key);
+
+    if (it == features.end ())
+      it = features
+            .insert (
+              std::make_pair (std::move (key),
+                              std::unordered_set<Optional<std::string>> ()))
+            .first;
+
+    return it->second.insert (Optional<std::string>::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<Optional<std::string>> ()))
+            .first;
+
+    it->second.insert (Optional<std::string>::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 (file)
index 0000000..19a4990
--- /dev/null
@@ -0,0 +1,4 @@
+// { dg-additional-options "-frust-cfg=A=\"B\"" }
+#[cfg(A)]
+pub fn foo() {}
+pub fn foo() {}