aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorbors[bot] <26634292+bors[bot]@users.noreply.github.com>2022-07-22 09:22:07 +0000
committerGitHub <noreply@github.com>2022-07-22 09:22:07 +0000
commit4a8c733b470eb6b7b52722c8694af9395d7cffe1 (patch)
tree2466283a70318113d3c2a5e35a5f790425cc497d
parent3f5fc214f4a53ac6c90e421814fcf049e56d3dc9 (diff)
parent3ce22036e48a4f8805440abcd291ecd35d2ca468 (diff)
downloadgcc-4a8c733b470eb6b7b52722c8694af9395d7cffe1.zip
gcc-4a8c733b470eb6b7b52722c8694af9395d7cffe1.tar.gz
gcc-4a8c733b470eb6b7b52722c8694af9395d7cffe1.tar.bz2
Merge #1401
1401: macros: properly handles recursive macros r=CohenArthur a=liushuyu - macros: properly handles recursive macros Fixes #1084 Co-authored-by: liushuyu <liushuyu011@gmail.com>
-rw-r--r--gcc/rust/expand/rust-attribute-visitor.cc18
-rw-r--r--gcc/rust/expand/rust-macro-expand.cc55
-rw-r--r--gcc/rust/expand/rust-macro-expand.h3
-rw-r--r--gcc/testsuite/rust/compile/macro_return.rs (renamed from gcc/testsuite/rust/compile/xfail/macro_return.rs)2
-rw-r--r--gcc/testsuite/rust/compile/torture/macro_as_expr.rs14
5 files changed, 38 insertions, 54 deletions
diff --git a/gcc/rust/expand/rust-attribute-visitor.cc b/gcc/rust/expand/rust-attribute-visitor.cc
index 9332bb6..bbcf2cb 100644
--- a/gcc/rust/expand/rust-attribute-visitor.cc
+++ b/gcc/rust/expand/rust-attribute-visitor.cc
@@ -402,10 +402,7 @@ AttrVisitor::visit (AST::MacroInvocation &macro_invoc)
// I don't think any macro token trees can be stripped in any way
// TODO: maybe have cfg! macro stripping behaviour here?
- if (macro_invoc.has_semicolon ())
- expander.expand_invoc_semi (macro_invoc);
- else
- expander.expand_invoc (macro_invoc);
+ expander.expand_invoc (macro_invoc, macro_invoc.has_semicolon ());
}
void
@@ -3434,8 +3431,17 @@ void
AttrVisitor::maybe_expand_expr (std::unique_ptr<AST::Expr> &expr)
{
auto fragment = expander.take_expanded_fragment (*this);
- if (fragment.should_expand ())
- expr = fragment.take_expression_fragment ();
+ unsigned int original_depth = expander.expansion_depth;
+ while (fragment.should_expand ())
+ {
+ expr = fragment.take_expression_fragment ();
+ expander.expansion_depth++;
+ auto new_fragment = expander.take_expanded_fragment (*this);
+ if (new_fragment.is_error ())
+ break;
+ fragment = std::move (new_fragment);
+ }
+ expander.expansion_depth = original_depth;
}
void
diff --git a/gcc/rust/expand/rust-macro-expand.cc b/gcc/rust/expand/rust-macro-expand.cc
index 189d377..e27adf5 100644
--- a/gcc/rust/expand/rust-macro-expand.cc
+++ b/gcc/rust/expand/rust-macro-expand.cc
@@ -106,7 +106,7 @@ MacroExpander::expand_decl_macro (Location invoc_locus,
}
void
-MacroExpander::expand_invoc (AST::MacroInvocation &invoc)
+MacroExpander::expand_invoc (AST::MacroInvocation &invoc, bool has_semicolon)
{
if (depth_exceeds_recursion_limit ())
{
@@ -136,54 +136,19 @@ MacroExpander::expand_invoc (AST::MacroInvocation &invoc)
// lookup the rules for this macro
NodeId resolved_node = UNKNOWN_NODEID;
- bool found = resolver->get_macro_scope ().lookup (
- Resolver::CanonicalPath::new_seg (invoc.get_pattern_node_id (),
- invoc_data.get_path ().as_string ()),
- &resolved_node);
- if (!found)
- {
- rust_error_at (invoc.get_locus (), "unknown macro 1");
- return;
- }
-
- // lookup the rules
- AST::MacroRulesDefinition *rules_def = nullptr;
- bool ok = mappings->lookup_macro_def (resolved_node, &rules_def);
- rust_assert (ok);
-
- auto fragment = AST::ASTFragment::create_error ();
-
- if (rules_def->is_builtin ())
- fragment
- = rules_def->get_builtin_transcriber () (invoc.get_locus (), invoc_data);
+ NodeId source_node = UNKNOWN_NODEID;
+ if (has_semicolon)
+ source_node = invoc.get_macro_node_id ();
else
- fragment
- = expand_decl_macro (invoc.get_locus (), invoc_data, *rules_def, false);
-
- set_expanded_fragment (std::move (fragment));
-}
-
-// FIXME: Arthur: Refactor these two functions, they're really similar
-void
-MacroExpander::expand_invoc_semi (AST::MacroInvocation &invoc)
-{
- if (depth_exceeds_recursion_limit ())
- {
- rust_error_at (invoc.get_locus (), "reached recursion limit");
- return;
- }
-
- AST::MacroInvocData &invoc_data = invoc.get_invoc_data ();
-
- // lookup the rules for this macro
- NodeId resolved_node = UNKNOWN_NODEID;
+ source_node = invoc.get_pattern_node_id ();
auto seg
- = Resolver::CanonicalPath::new_seg (invoc.get_macro_node_id (),
+ = Resolver::CanonicalPath::new_seg (source_node,
invoc_data.get_path ().as_string ());
+
bool found = resolver->get_macro_scope ().lookup (seg, &resolved_node);
if (!found)
{
- rust_error_at (invoc.get_locus (), "unknown macro 2: [%s]",
+ rust_error_at (invoc.get_locus (), "unknown macro: [%s]",
seg.get ().c_str ());
return;
}
@@ -199,8 +164,8 @@ MacroExpander::expand_invoc_semi (AST::MacroInvocation &invoc)
fragment
= rules_def->get_builtin_transcriber () (invoc.get_locus (), invoc_data);
else
- fragment
- = expand_decl_macro (invoc.get_locus (), invoc_data, *rules_def, true);
+ fragment = expand_decl_macro (invoc.get_locus (), invoc_data, *rules_def,
+ has_semicolon);
set_expanded_fragment (std::move (fragment));
}
diff --git a/gcc/rust/expand/rust-macro-expand.h b/gcc/rust/expand/rust-macro-expand.h
index a582524..2c3380b 100644
--- a/gcc/rust/expand/rust-macro-expand.h
+++ b/gcc/rust/expand/rust-macro-expand.h
@@ -213,8 +213,7 @@ struct MacroExpander
/* Expands a macro invocation - possibly make both
* have similar duck-typed interface and use templates?*/
// should this be public or private?
- void expand_invoc (AST::MacroInvocation &invoc);
- void expand_invoc_semi (AST::MacroInvocation &invoc);
+ void expand_invoc (AST::MacroInvocation &invoc, bool has_semicolon);
// Expands a single declarative macro.
AST::ASTFragment expand_decl_macro (Location locus,
diff --git a/gcc/testsuite/rust/compile/xfail/macro_return.rs b/gcc/testsuite/rust/compile/macro_return.rs
index c335929..8b06f87 100644
--- a/gcc/testsuite/rust/compile/xfail/macro_return.rs
+++ b/gcc/testsuite/rust/compile/macro_return.rs
@@ -1,4 +1,4 @@
-// { dg-excess-errors "...." { xfail *-*-*-* *-*-* } }
+// { dg-additional-options "-w" }
macro_rules! add {
($a:expr) => { $a };
diff --git a/gcc/testsuite/rust/compile/torture/macro_as_expr.rs b/gcc/testsuite/rust/compile/torture/macro_as_expr.rs
new file mode 100644
index 0000000..b0084e7
--- /dev/null
+++ b/gcc/testsuite/rust/compile/torture/macro_as_expr.rs
@@ -0,0 +1,14 @@
+// { dg-additional-options "-w" }
+
+macro_rules! add {
+ ($a:expr) => { $a };
+ ($a:expr, $($b:expr),+) => { $a + add!($($b),*) }
+}
+
+fn main() -> i32 {
+ if add!(add!(1, 2)) > add!(5) {
+ add!(1, add!(2, 3), add!(4))
+ } else {
+ add!(5, add!(6, 7), add!(8), 9) + 10
+ }
+}