public inbox for gcc-cvs@sourceware.org
help / color / mirror / Atom feed
* [gcc/devel/rust/master] Add support for key = value pairs on -frust-cfg=
@ 2022-06-08 12:04 Thomas Schwinge
  0 siblings, 0 replies; only message in thread
From: Thomas Schwinge @ 2022-06-08 12:04 UTC (permalink / raw)
  To: gcc-cvs

https://gcc.gnu.org/g:9173f062a9f79d9dfe7076063a2e5b1977c4485f

commit 9173f062a9f79d9dfe7076063a2e5b1977c4485f
Author: Philip Herron <philip.herron@embecosm.com>
Date:   Fri Feb 4 17:31:49 2022 +0000

    Add support for key = value pairs on -frust-cfg=
    
    This adds in a basic parser to parse out key value pairs for the config
    option it needs to be tested poperly once the self-test framework is
    merged in.
    
    Fixes #889

Diff:
---
 gcc/rust/rust-session-manager.cc   | 89 ++++++++++++++++++++++++++++++++++++--
 gcc/testsuite/rust/compile/cfg5.rs | 11 +++++
 2 files changed, 96 insertions(+), 4 deletions(-)

diff --git a/gcc/rust/rust-session-manager.cc b/gcc/rust/rust-session-manager.cc
index 83f35ffd727..0150a487c68 100644
--- a/gcc/rust/rust-session-manager.cc
+++ b/gcc/rust/rust-session-manager.cc
@@ -394,11 +394,92 @@ Session::handle_option (
 }
 
 bool
-Session::handle_cfg_option (const std::string &value)
+Session::handle_cfg_option (const std::string &input)
 {
-  // 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);
+  std::string key;
+  std::string value;
+
+  enum pstate
+  {
+    KEY,
+    EQ,
+    VAL,
+    DONE,
+    ERROR
+  };
+
+  // FIXME
+  // we need to use the GCC self_test framework to unit-test this its
+  // likely got a bunch of bugs. This simple parser could be extracted to a
+  // helper function to be more easily unit-tested or it could be tested via
+  // checking what the target_options contain
+  bool expect_quote = false;
+  pstate s = KEY;
+  for (const auto &ch : input)
+    {
+      if (ch == ' ')
+	{
+	  if (!key.empty ())
+	    s = EQ;
+	  else if (!value.empty ())
+	    s = DONE;
+	  else
+	    {
+	      s = ERROR;
+	      break;
+	    }
+	}
+      else if (ch == '"')
+	{
+	  expect_quote = !expect_quote;
+	}
+      else if (ch == '=')
+	{
+	  if (key.empty ())
+	    {
+	      s = ERROR;
+	      break;
+	    }
+
+	  s = VAL;
+	}
+      else
+	{
+	  if (s == KEY)
+	    key.push_back (ch);
+	  else if (s == VAL)
+	    value.push_back (ch);
+	  else
+	    {
+	      s = ERROR;
+	      break;
+	    }
+	}
+    }
+
+  if (key.empty () && value.empty ())
+    s = ERROR;
+
+  if (expect_quote)
+    s = ERROR;
+
+  if (s == ERROR)
+    {
+      rust_error_at (Location (),
+		     "invalid --frust-cfg= option expected %<key%> or "
+		     "key=%<value%> got %<%s%>",
+		     input.c_str ());
+      return false;
+    }
+
+  if (value.empty ())
+    {
+      // rustc does not seem to error on dup key
+      options.target_data.insert_key (key);
+      return true;
+    }
+
+  options.target_data.insert_key_value_pair (key, value);
   return true;
 }
 
diff --git a/gcc/testsuite/rust/compile/cfg5.rs b/gcc/testsuite/rust/compile/cfg5.rs
new file mode 100644
index 00000000000..927c4974ff6
--- /dev/null
+++ b/gcc/testsuite/rust/compile/cfg5.rs
@@ -0,0 +1,11 @@
+// { dg-additional-options "-w -frust-cfg=A=B" }
+struct Foo;
+impl Foo {
+    #[cfg(A = "B")]
+    fn test(&self) {}
+}
+
+fn main() {
+    let a = Foo;
+    a.test();
+}


^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2022-06-08 12:04 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-06-08 12:04 [gcc/devel/rust/master] Add support for key = value pairs on -frust-cfg= Thomas Schwinge

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).