]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
gccrs: Add origin location to expanded tokens
authorPierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
Thu, 18 Sep 2025 15:47:54 +0000 (17:47 +0200)
committerArthur Cohen <arthur.cohen@embecosm.com>
Thu, 30 Oct 2025 20:30:53 +0000 (21:30 +0100)
gcc/rust/ChangeLog:

* expand/rust-macro-expand.cc: Forward invocation tree locus to
substitution context.
* expand/rust-macro-substitute-ctx.cc: Use origin location for expanded
tokens.
* expand/rust-macro-substitute-ctx.h (class SubstituteCtx): Save
invocation location.

gcc/testsuite/ChangeLog:

* rust/compile/macros/mbe/macro58.rs: New test.

Signed-off-by: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
gcc/rust/expand/rust-macro-expand.cc
gcc/rust/expand/rust-macro-substitute-ctx.cc
gcc/rust/expand/rust-macro-substitute-ctx.h
gcc/testsuite/rust/compile/macros/mbe/macro58.rs [new file with mode: 0644]

index ec4f666dc7b3d0a79c344c22294a090cddccf28f..52f8e2b10e33e86f18d1447c8d53882cb15da77a 100644 (file)
@@ -1108,8 +1108,9 @@ MacroExpander::transcribe_rule (
   auto invoc_stream = invoc_token_tree.to_token_stream ();
   auto macro_rule_tokens = transcribe_tree.to_token_stream ();
 
-  auto substitute_context = SubstituteCtx (invoc_stream, macro_rule_tokens,
-                                          matched_fragments, definition);
+  auto substitute_context
+    = SubstituteCtx (invoc_stream, macro_rule_tokens, matched_fragments,
+                    definition, invoc_token_tree.get_locus ());
   std::vector<std::unique_ptr<AST::Token>> substituted_tokens
     = substitute_context.substitute_tokens ();
 
index 0b78fa535cb7f89dfa5c9138db873b4d4c138a46..36bae5bf45ac4ec33427e051c11732de8c69ccc2 100644 (file)
@@ -40,7 +40,7 @@ SubstituteCtx::substitute_dollar_crate (
   if (*def_crate == current_crate)
     {
       expanded.push_back (std::make_unique<AST::Token> (
-       Rust::Token::make_identifier (UNKNOWN_LOCATION, "crate")));
+       Rust::Token::make_identifier (origin, "crate")));
     }
   else
     {
@@ -49,9 +49,9 @@ SubstituteCtx::substitute_dollar_crate (
       rust_assert (name);
 
       expanded.push_back (std::make_unique<AST::Token> (
-       Rust::Token::make (SCOPE_RESOLUTION, UNKNOWN_LOCATION)));
+       Rust::Token::make (SCOPE_RESOLUTION, origin)));
       expanded.push_back (std::make_unique<AST::Token> (
-       Rust::Token::make_identifier (UNKNOWN_LOCATION, std::string (*name))));
+       Rust::Token::make_identifier (origin, std::string (*name))));
     }
 
   return true;
@@ -237,7 +237,7 @@ SubstituteCtx::substitute_repetition (
        }
 
       auto substitute_context
-       = SubstituteCtx (input, new_macro, sub_map, definition);
+       = SubstituteCtx (input, new_macro, sub_map, definition, origin);
       auto new_tokens = substitute_context.substitute_tokens ();
 
       // Skip the first repetition, but add the separator to the expanded
index c5c4956bc8e8aaafe48168ef7d75ae55c2ffb9a6..3829a5a81fa047afa43c4908ccd8e7df31562c4a 100644 (file)
@@ -27,6 +27,8 @@ class SubstituteCtx
   std::vector<std::unique_ptr<AST::Token>> &macro;
   std::map<std::string, MatchedFragmentContainer *> &fragments;
   AST::MacroRulesDefinition &definition;
+  // Macro invocation location
+  location_t origin;
 
   /**
    * Find the repetition amount to use when expanding a repetition, and
@@ -43,9 +45,9 @@ public:
   SubstituteCtx (std::vector<std::unique_ptr<AST::Token>> &input,
                 std::vector<std::unique_ptr<AST::Token>> &macro,
                 std::map<std::string, MatchedFragmentContainer *> &fragments,
-                AST::MacroRulesDefinition &definition)
+                AST::MacroRulesDefinition &definition, location_t origin)
     : input (input), macro (macro), fragments (fragments),
-      definition (definition)
+      definition (definition), origin (origin)
   {}
 
   /**
diff --git a/gcc/testsuite/rust/compile/macros/mbe/macro58.rs b/gcc/testsuite/rust/compile/macros/mbe/macro58.rs
new file mode 100644 (file)
index 0000000..d8f7599
--- /dev/null
@@ -0,0 +1,12 @@
+pub fn print(a: *const u8) {}
+#[macro_export]
+macro_rules! pr_warn (
+    ($($arg:tt)*) => (
+        $($crate::print($arg))*
+    )
+);
+
+fn main() {
+    pr_warn!("test\0", "test\0");
+    // { dg-error "expecting .;. but .identifier. found" "" { target *-*-* } .-1 }
+}