]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
gccrs: expand: Add stringify macro
authorPierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
Mon, 6 Mar 2023 08:58:02 +0000 (09:58 +0100)
committerArthur Cohen <arthur.cohen@embecosm.com>
Tue, 16 Jan 2024 17:21:08 +0000 (18:21 +0100)
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 0b21d42f57064d6a367eda23f819e991d5512ed2..b558969eba4c672066aa22dad7affa8920401483 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 0e6b8e319e10d903cda36891dd7fce9107de65f0..2ea42e451ad2c01d54c87d8397ee4f4bb095d1a7 100644 (file)
@@ -589,6 +589,7 @@ enum class BuiltinMacro
   Column,
   IncludeBytes,
   IncludeStr,
+  Stringify,
   CompileError,
   Concat,
   Env,
index 2bd812085667840af3d908b0f3b7b172c78d6d25..2c6856a4434ea48f10a1885f5dcff11b5cc5d972 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 0cab9b146d69f021349bcd75fc8a041e5e475979..bd4f1f95b741f54e8d71c9683f688a8298a047d5 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 f78ef93f9b37df88fdbb589b2493818caef7256f..535731b48f219edf80ee6b3a4018888377e0d34c 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
+}