Skip to content

Commit 2b1cb4b

Browse files
bors[bot]antego
andauthored
Merge #1161
1161: Implement macro expansion for ComparisonExpr, LazyBooleanExpr, AssignmentExpr r=CohenArthur a=antego Following up on #1141 Currently the macro expansion doesn't work for ComparisonExpr, LazyBooleanExpr, AssignmentExpr. To fix this, I just copied the code from the `ArithmeticOrLogicalExpr` and it seemed to work. I don't like the code duplication, happy to try refactoring it into a separate function. Will work on the macro expansion in the `if` expressions next. Co-authored-by: antego <[email protected]>
2 parents d54ca71 + e26b95f commit 2b1cb4b

File tree

4 files changed

+103
-6
lines changed

4 files changed

+103
-6
lines changed

gcc/rust/expand/rust-attribute-visitor.cc

Lines changed: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -643,10 +643,19 @@ AttrVisitor::visit (AST::ComparisonExpr &expr)
643643

644644
/* should have no possibility for outer attrs as would be parsed
645645
* with outer expr */
646-
expr.get_left_expr ()->accept_vis (*this);
646+
auto &l_expr = expr.get_left_expr ();
647+
l_expr->accept_vis (*this);
648+
auto l_fragment = expander.take_expanded_fragment (*this);
649+
if (l_fragment.should_expand ())
650+
l_expr = l_fragment.take_expression_fragment ();
651+
647652
/* should syntactically not have outer attributes, though this may
648653
* not have worked in practice */
649-
expr.get_right_expr ()->accept_vis (*this);
654+
auto &r_expr = expr.get_right_expr ();
655+
r_expr->accept_vis (*this);
656+
auto r_fragment = expander.take_expanded_fragment (*this);
657+
if (r_fragment.should_expand ())
658+
r_expr = r_fragment.take_expression_fragment ();
650659

651660
// ensure that they are not marked for strip
652661
if (expr.get_left_expr ()->is_marked_for_strip ())
@@ -667,10 +676,19 @@ AttrVisitor::visit (AST::LazyBooleanExpr &expr)
667676

668677
/* should have no possibility for outer attrs as would be parsed
669678
* with outer expr */
670-
expr.get_left_expr ()->accept_vis (*this);
679+
auto &l_expr = expr.get_left_expr ();
680+
l_expr->accept_vis (*this);
681+
auto l_fragment = expander.take_expanded_fragment (*this);
682+
if (l_fragment.should_expand ())
683+
l_expr = l_fragment.take_expression_fragment ();
684+
671685
/* should syntactically not have outer attributes, though this may
672686
* not have worked in practice */
673-
expr.get_right_expr ()->accept_vis (*this);
687+
auto &r_expr = expr.get_right_expr ();
688+
r_expr->accept_vis (*this);
689+
auto r_fragment = expander.take_expanded_fragment (*this);
690+
if (r_fragment.should_expand ())
691+
r_expr = r_fragment.take_expression_fragment ();
674692

675693
// ensure that they are not marked for strip
676694
if (expr.get_left_expr ()->is_marked_for_strip ())
@@ -718,10 +736,19 @@ AttrVisitor::visit (AST::AssignmentExpr &expr)
718736

719737
/* should have no possibility for outer attrs as would be parsed
720738
* with outer expr */
721-
expr.get_left_expr ()->accept_vis (*this);
739+
auto &l_expr = expr.get_left_expr ();
740+
l_expr->accept_vis (*this);
741+
auto l_fragment = expander.take_expanded_fragment (*this);
742+
if (l_fragment.should_expand ())
743+
l_expr = l_fragment.take_expression_fragment ();
744+
722745
/* should syntactically not have outer attributes, though this may
723746
* not have worked in practice */
724-
expr.get_right_expr ()->accept_vis (*this);
747+
auto &r_expr = expr.get_right_expr ();
748+
r_expr->accept_vis (*this);
749+
auto r_fragment = expander.take_expanded_fragment (*this);
750+
if (r_fragment.should_expand ())
751+
r_expr = r_fragment.take_expression_fragment ();
725752

726753
// ensure that they are not marked for strip
727754
if (expr.get_left_expr ()->is_marked_for_strip ())
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// { dg-output "1\n" }
2+
macro_rules! concat {
3+
() => {{}};
4+
}
5+
6+
extern "C" {
7+
fn printf(fmt: *const i8, ...);
8+
}
9+
10+
fn print(s: u32) {
11+
printf("%u\n\0" as *const str as *const i8, s);
12+
}
13+
14+
fn main () -> i32 {
15+
let res = concat!("test2") == "test3";
16+
if !res {
17+
print(1);
18+
}
19+
20+
0
21+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// { dg-output "1\n" }
2+
macro_rules! concat {
3+
() => {{}};
4+
}
5+
6+
extern "C" {
7+
fn printf(fmt: *const i8, ...);
8+
}
9+
10+
fn print(s: u32) {
11+
printf("%u\n\0" as *const str as *const i8, s);
12+
}
13+
14+
fn main () -> i32 {
15+
let mut x = concat!("x");
16+
x = concat!("y");
17+
if x == "y" {
18+
print(1);
19+
}
20+
21+
0
22+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// { dg-additional-options "-w -frust-cfg=A" }
2+
// { dg-output "A\nB\n" }
3+
macro_rules! cfg {
4+
() => {{}};
5+
}
6+
7+
extern "C" {
8+
fn printf(fmt: *const i8, ...);
9+
}
10+
11+
fn print(s: &str) {
12+
printf("%s\n" as *const str as *const i8, s as *const str as *const i8);
13+
}
14+
15+
16+
fn main() -> i32 {
17+
let cfg = cfg!(A) || cfg!(B);
18+
if cfg {
19+
print("A");
20+
}
21+
let cfg = cfg!(A) && cfg!(B);
22+
if !cfg {
23+
print("B");
24+
}
25+
26+
0
27+
}

0 commit comments

Comments
 (0)