From 9fc6a27b5c6ea2c775646c4474b9084da76b1764 Mon Sep 17 00:00:00 2001 From: liushuyu Date: Tue, 2 Aug 2022 02:17:48 -0600 Subject: expand: correctly handles non-macro nodes ... ... when expanding macros recursively to avoid improperly stripping them Signed-off-by: Zixing Liu --- gcc/rust/expand/rust-macro-expand.h | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) (limited to 'gcc/rust') diff --git a/gcc/rust/expand/rust-macro-expand.h b/gcc/rust/expand/rust-macro-expand.h index 341c5a4..94d6702 100644 --- a/gcc/rust/expand/rust-macro-expand.h +++ b/gcc/rust/expand/rust-macro-expand.h @@ -321,12 +321,28 @@ struct MacroExpander AST::ASTFragment take_expanded_fragment (AST::ASTVisitor &vis) { AST::ASTFragment old_fragment = std::move (expanded_fragment); + auto accumulator = std::vector (); expanded_fragment = AST::ASTFragment::create_error (); for (auto &node : old_fragment.get_nodes ()) { expansion_depth++; node.accept_vis (vis); + // we'll decide the next move according to the outcome of the macro + // expansion + if (expanded_fragment.is_error ()) + accumulator.push_back (node); // if expansion fails, there might be a + // non-macro expression we need to keep + else + { + // if expansion succeeded, then we need to merge the fragment with + // the contents in the accumulator, so that our final expansion + // result will contain non-macro nodes as it should + auto new_nodes = expanded_fragment.get_nodes (); + std::move (new_nodes.begin (), new_nodes.end (), + std::back_inserter (accumulator)); + expanded_fragment = AST::ASTFragment (accumulator); + } expansion_depth--; } -- cgit v1.1