Skip to content

Commit 32ce955

Browse files
committed
Add origin location to expanded tokens
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 <[email protected]>
1 parent 5c80bfc commit 32ce955

File tree

4 files changed

+23
-8
lines changed

4 files changed

+23
-8
lines changed

gcc/rust/expand/rust-macro-expand.cc

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1108,8 +1108,9 @@ MacroExpander::transcribe_rule (
11081108
auto invoc_stream = invoc_token_tree.to_token_stream ();
11091109
auto macro_rule_tokens = transcribe_tree.to_token_stream ();
11101110

1111-
auto substitute_context = SubstituteCtx (invoc_stream, macro_rule_tokens,
1112-
matched_fragments, definition);
1111+
auto substitute_context
1112+
= SubstituteCtx (invoc_stream, macro_rule_tokens, matched_fragments,
1113+
definition, invoc_token_tree.get_locus ());
11131114
std::vector<std::unique_ptr<AST::Token>> substituted_tokens
11141115
= substitute_context.substitute_tokens ();
11151116

gcc/rust/expand/rust-macro-substitute-ctx.cc

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ SubstituteCtx::substitute_dollar_crate (
4040
if (*def_crate == current_crate)
4141
{
4242
expanded.push_back (std::make_unique<AST::Token> (
43-
Rust::Token::make_identifier (UNKNOWN_LOCATION, "crate")));
43+
Rust::Token::make_identifier (origin, "crate")));
4444
}
4545
else
4646
{
@@ -49,9 +49,9 @@ SubstituteCtx::substitute_dollar_crate (
4949
rust_assert (name);
5050

5151
expanded.push_back (std::make_unique<AST::Token> (
52-
Rust::Token::make (SCOPE_RESOLUTION, UNKNOWN_LOCATION)));
52+
Rust::Token::make (SCOPE_RESOLUTION, origin)));
5353
expanded.push_back (std::make_unique<AST::Token> (
54-
Rust::Token::make_identifier (UNKNOWN_LOCATION, std::string (*name))));
54+
Rust::Token::make_identifier (origin, std::string (*name))));
5555
}
5656

5757
return true;
@@ -237,7 +237,7 @@ SubstituteCtx::substitute_repetition (
237237
}
238238

239239
auto substitute_context
240-
= SubstituteCtx (input, new_macro, sub_map, definition);
240+
= SubstituteCtx (input, new_macro, sub_map, definition, origin);
241241
auto new_tokens = substitute_context.substitute_tokens ();
242242

243243
// Skip the first repetition, but add the separator to the expanded

gcc/rust/expand/rust-macro-substitute-ctx.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ class SubstituteCtx
2727
std::vector<std::unique_ptr<AST::Token>> &macro;
2828
std::map<std::string, MatchedFragmentContainer *> &fragments;
2929
AST::MacroRulesDefinition &definition;
30+
// Macro invocation location
31+
location_t origin;
3032

3133
/**
3234
* Find the repetition amount to use when expanding a repetition, and
@@ -43,9 +45,9 @@ class SubstituteCtx
4345
SubstituteCtx (std::vector<std::unique_ptr<AST::Token>> &input,
4446
std::vector<std::unique_ptr<AST::Token>> &macro,
4547
std::map<std::string, MatchedFragmentContainer *> &fragments,
46-
AST::MacroRulesDefinition &definition)
48+
AST::MacroRulesDefinition &definition, location_t origin)
4749
: input (input), macro (macro), fragments (fragments),
48-
definition (definition)
50+
definition (definition), origin (origin)
4951
{}
5052

5153
/**
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
pub fn print(a: *const u8) {}
2+
#[macro_export]
3+
macro_rules! pr_warn (
4+
($($arg:tt)*) => (
5+
$($crate::print($arg))*
6+
)
7+
);
8+
9+
fn main() {
10+
pr_warn!("test\0", "test\0");
11+
// { dg-error "expecting .;. but .identifier. found" "" { target *-*-* } .-1 }
12+
}

0 commit comments

Comments
 (0)