]> git.ipfire.org Git - people/ms/gcc.git/commitdiff
expand: Add stringify macro
authorPierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
Mon, 6 Mar 2023 08:58:02 +0000 (09:58 +0100)
committerPhilip Herron <philip.herron@embecosm.com>
Tue, 7 Mar 2023 10:07:31 +0000 (10:07 +0000)
Add the stringify macro expansion as well as some tests.

gcc/rust/ChangeLog:

* ast/rust-macro.cc (builtin_macro_from_string): Add identifier
identification.
* ast/rust-macro.h (enum class): Add Stringify builtin macro
type.
* expand/rust-macro-builtins.cc (make_macro_path_str): Add path
for builtin stringify macro.
(MacroBuiltin::stringify_handler): Add handler for builtin
stringify macro.
* expand/rust-macro-builtins.h: Add stringify handler's
prototype.
* util/rust-hir-map.cc (Mappings::insert_macro_def): Add
stringify handler to builtin hir map.

gcc/testsuite/ChangeLog:

* rust/compile/stringify.rs: Add a basic test with some text.
* rust/execute/torture/builtin_macro_stringify.rs: Verify the
text is left as is without any other macro expansion.

Signed-off-by: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
gcc/rust/ast/rust-macro.cc
gcc/rust/ast/rust-macro.h
gcc/rust/expand/rust-macro-builtins.cc
gcc/rust/expand/rust-macro-builtins.h
gcc/rust/util/rust-hir-map.cc
gcc/testsuite/rust/compile/stringify.rs [new file with mode: 0644]
gcc/testsuite/rust/execute/torture/builtin_macro_stringify.rs [new file with mode: 0644]

index b6f8f6c059a73f2c2dc85662a8323b8a33f8083b..cb4bae71ef8b0fd68c00731c2a4542b9db29b75c 100644 (file)
@@ -42,6 +42,9 @@ builtin_macro_from_string (const std::string &identifier)
   if (identifier == "include_str")
     return BuiltinMacro::IncludeStr;
 
+  if (identifier == "stringify")
+    return BuiltinMacro::Stringify;
+
   if (identifier == "compile_error")
     return BuiltinMacro::CompileError;
 
index be8ed5609137e06b26ca0b60a27b1a063295b907..f667a0a57b1ee5f907bd08929f52e8f31b1fa5fd 100644 (file)
@@ -589,6 +589,7 @@ enum class BuiltinMacro
   Column,
   IncludeBytes,
   IncludeStr,
+  Stringify,
   CompileError,
   Concat,
   Env,
index 5f317a0a1dfc1ba794f7db04e8e10a5d9344b946..f30c963df35596830d3695fcd5af3c20579e6941 100644 (file)
@@ -58,6 +58,9 @@ make_macro_path_str (AST::BuiltinMacro kind)
     case AST::BuiltinMacro::IncludeStr:
       path_str = "include_str";
       break;
+    case AST::BuiltinMacro::Stringify:
+      path_str = "stringify";
+      break;
     case AST::BuiltinMacro::CompileError:
       path_str = "compile_error";
       break;
@@ -845,4 +848,34 @@ MacroBuiltin::line_handler (Location invoc_locus, AST::MacroInvocData &)
   return AST::Fragment ({line_no}, std::move (tok));
 }
 
+AST::Fragment
+MacroBuiltin::stringify_handler (Location invoc_locus,
+                                AST::MacroInvocData &invoc)
+{
+  std::string content;
+  auto invoc_token_tree = invoc.get_delim_tok_tree ();
+  auto tokens = invoc_token_tree.to_token_stream ();
+
+  // Tokens stream includes the first and last delimiter
+  // which we need to skip.
+  for (auto token = tokens.cbegin () + 1; token < tokens.cend () - 1; token++)
+    {
+      // Rust stringify format has no garantees but the reference compiler
+      // removes spaces before some tokens depending on the lexer's behavior,
+      // let's mimick some of those behaviors.
+      auto token_id = (*token)->get_id ();
+      if (token_id != RIGHT_PAREN && token_id != EXCLAM
+         && token != tokens.cbegin () + 1)
+       {
+         content.push_back (' ');
+       }
+      content += (*token)->as_string ();
+    }
+
+  auto node = AST::SingleASTNode (make_string (invoc_locus, content));
+  auto token
+    = make_token (Token::make_string (invoc_locus, std::move (content)));
+  return AST::Fragment ({node}, std::move (token));
+} // namespace Rust
+
 } // namespace Rust
index 6d7a0123ab34d9bd9ab0e8353c106a6e66b8c2b9..ea0f64a96a07359f00d7b77b10e071b9d3e6379f 100644 (file)
@@ -79,6 +79,9 @@ public:
   static AST::Fragment include_str_handler (Location invoc_locus,
                                            AST::MacroInvocData &invoc);
 
+  static AST::Fragment stringify_handler (Location invoc_locus,
+                                         AST::MacroInvocData &invoc);
+
   static AST::Fragment compile_error_handler (Location invoc_locus,
                                              AST::MacroInvocData &invoc);
 
index a968704014432d1d0f30d12b1235a3b6abcc44af..57c0a3c0d73626e384f5f81ce9ffc42e881d6c56 100644 (file)
@@ -856,6 +856,7 @@ Mappings::insert_macro_def (AST::MacroRulesDefinition *macro)
       {"column", MacroBuiltin::column_handler},
       {"include_bytes", MacroBuiltin::include_bytes_handler},
       {"include_str", MacroBuiltin::include_str_handler},
+      {"stringify", MacroBuiltin::stringify_handler},
       {"compile_error", MacroBuiltin::compile_error_handler},
       {"concat", MacroBuiltin::concat_handler},
       {"env", MacroBuiltin::env_handler},
diff --git a/gcc/testsuite/rust/compile/stringify.rs b/gcc/testsuite/rust/compile/stringify.rs
new file mode 100644 (file)
index 0000000..0350a3c
--- /dev/null
@@ -0,0 +1,10 @@
+#![feature(rustc_attrs)]
+
+#[rustc_builtin_macro]
+macro_rules! stringify {
+    () => {};
+}
+
+fn main() {
+    let _a = stringify!(sample text with parenthesis () and things! This will become a "string".);
+}
diff --git a/gcc/testsuite/rust/execute/torture/builtin_macro_stringify.rs b/gcc/testsuite/rust/execute/torture/builtin_macro_stringify.rs
new file mode 100644 (file)
index 0000000..e6f4a33
--- /dev/null
@@ -0,0 +1,34 @@
+// { dg-output "a! ()" }
+#![feature(rustc_attrs)]
+
+#[rustc_builtin_macro]
+macro_rules! stringify {
+    () => {};
+}
+
+macro_rules! a {
+    () => {
+        " foo"
+    };
+}
+
+extern "C" {
+    fn printf(fmt: *const i8, ...);
+}
+
+fn print(s: &str) {
+    unsafe {
+        printf(
+            "%s" as *const str as *const i8,
+            s as *const str as *const i8,
+        );
+    }
+}
+
+fn main() -> i32 {
+    let a = stringify!(a!());
+
+    print(a);
+
+    0
+}