Skip to content

Commit 133beb6

Browse files
Merge #955
955: matched_fragment: Track and set fragment match amount r=CohenArthur a=CohenArthur Co-authored-by: Arthur Cohen <[email protected]>
2 parents 7964655 + 91aca2c commit 133beb6

File tree

2 files changed

+36
-7
lines changed

2 files changed

+36
-7
lines changed

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

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3127,6 +3127,10 @@ MacroExpander::expand_decl_macro (Location invoc_locus,
31273127

31283128
if (did_match_rule)
31293129
{
3130+
for (auto &frag : matched_fragments)
3131+
rust_debug ("matched fragment: %s",
3132+
frag.second.as_string ().c_str ());
3133+
31303134
matched_rule = &rule;
31313135
break;
31323136
}
@@ -3533,7 +3537,7 @@ MacroExpander::match_matcher (Parser<MacroInvocLexer> &parser,
35333537
size_t offs_end = source.get_offs ();
35343538
sub_stack.peek ().insert (
35353539
{fragment->get_ident (),
3536-
{fragment->get_ident (), offs_begin, offs_end}});
3540+
MatchedFragment (fragment->get_ident (), offs_begin, offs_end)});
35373541
}
35383542
break;
35393543

@@ -3607,6 +3611,7 @@ MacroExpander::match_n_matches (
36073611
match_amount = 0;
36083612

36093613
const MacroInvocLexer &source = parser.get_token_source ();
3614+
std::vector<std::string> fragment_identifiers;
36103615
while (true)
36113616
{
36123617
// If the current token is a closing macro delimiter, break away.
@@ -3630,7 +3635,10 @@ MacroExpander::match_n_matches (
36303635
size_t offs_end = source.get_offs ();
36313636
sub_stack.peek ().insert (
36323637
{fragment->get_ident (),
3633-
{fragment->get_ident (), offs_begin, offs_end}});
3638+
MatchedFragment (fragment->get_ident (), offs_begin,
3639+
offs_end)});
3640+
3641+
fragment_identifiers.emplace_back (fragment->get_ident ());
36343642
}
36353643
break;
36363644

@@ -3669,10 +3677,21 @@ MacroExpander::match_n_matches (
36693677

36703678
// Check if the amount of matches we got is valid: Is it more than the lower
36713679
// bound and less than the higher bound?
3672-
if (!hi_bound) // infinite amount, no upper bound
3673-
return match_amount >= lo_bound;
3674-
else
3675-
return match_amount >= lo_bound && match_amount <= hi_bound;
3680+
auto result = hi_bound ? match_amount >= lo_bound && match_amount <= hi_bound
3681+
: match_amount >= lo_bound;
3682+
3683+
// We can now set the amount to each fragment we matched in the substack
3684+
auto &stack_map = sub_stack.peek ();
3685+
for (auto &fragment_id : fragment_identifiers)
3686+
{
3687+
auto it = stack_map.find (fragment_id);
3688+
3689+
rust_assert (it != stack_map.end ());
3690+
3691+
it->second.set_match_amount (match_amount);
3692+
}
3693+
3694+
return result;
36763695
}
36773696

36783697
bool

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

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,12 +99,22 @@ struct MatchedFragment
9999
std::string fragment_ident;
100100
size_t token_offset_begin;
101101
size_t token_offset_end;
102+
size_t match_amount;
103+
104+
MatchedFragment (std::string identifier, size_t token_offset_begin,
105+
size_t token_offset_end, size_t match_amount = 0)
106+
: fragment_ident (identifier), token_offset_begin (token_offset_begin),
107+
token_offset_end (token_offset_end), match_amount (match_amount)
108+
{}
102109

103110
std::string as_string () const
104111
{
105112
return fragment_ident + "=" + std::to_string (token_offset_begin) + ":"
106-
+ std::to_string (token_offset_end);
113+
+ std::to_string (token_offset_end) + " (matched "
114+
+ std::to_string (match_amount) + " times)";
107115
}
116+
117+
void set_match_amount (size_t new_amount) { match_amount = new_amount; }
108118
};
109119

110120
class SubstitutionScope

0 commit comments

Comments
 (0)