Skip to content

Commit 330b265

Browse files
Merge #926
926: macros: Add multiple macro syntax tests r=CohenArthur a=CohenArthur This PR adds test cases for macros, including parsing and execution. I am unsure on how to check for proper execution: The solution I have chosen so far is to make sure that a correct amount of lines is printed, which I'm not entirely satisfied with. Another solution would be to increase a global integer to use when exiting, which we can then assert on using dejagnu, which is cleaner but relies on unsafe rust code. Co-authored-by: Arthur Cohen <[email protected]>
2 parents c7275a7 + 08dbb4e commit 330b265

File tree

8 files changed

+122
-0
lines changed

8 files changed

+122
-0
lines changed

gcc/testsuite/rust/compile/macro1.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
macro_rules! empty_parens {
2+
() => ();
3+
}

gcc/testsuite/rust/compile/macro2.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
macro_rules! empty_brackets {
2+
[] => [];
3+
}

gcc/testsuite/rust/compile/macro3.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
macro_rules! empty_curlies {
2+
{} => {};
3+
}

gcc/testsuite/rust/compile/macro4.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
macro_rules! one_keyword {
2+
(kw) => {};
3+
}

gcc/testsuite/rust/compile/macro5.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
macro_rules! rust_keyword {
2+
(fn) => {};
3+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// { dg-output "macro\nmacro\nmacro\nmacro\n" }
2+
extern "C" {
3+
fn printf(s: *const i8, ...);
4+
}
5+
6+
fn f() {
7+
let r_s = "macro\n\0";
8+
let s_p = r_s as *const str;
9+
let c_p = s_p as *const i8;
10+
11+
printf(c_p);
12+
}
13+
14+
macro_rules! empty0 {
15+
() => ( f() );
16+
}
17+
18+
macro_rules! empty1 {
19+
{} => { f() };
20+
}
21+
22+
macro_rules! empty2 {
23+
[] => [ f() ];
24+
}
25+
26+
// using multiple parens/brackets/curlies variants allows us to make sure we
27+
// parse everything properly
28+
fn main() {
29+
empty0!();
30+
empty1!{};
31+
empty2![];
32+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// { dg-output "arg\narg\n" }
2+
extern "C" {
3+
fn printf(s: *const i8, ...);
4+
}
5+
6+
fn f() {
7+
let r_s = "arg\n\0";
8+
let s_p = r_s as *const str;
9+
let c_p = s_p as *const i8;
10+
11+
printf(c_p);
12+
}
13+
14+
macro_rules! kw0 {
15+
(keyword) => { f() };
16+
}
17+
18+
macro_rules! kw1 {
19+
(fn) => { f() };
20+
}
21+
22+
macro_rules! kw2 {
23+
(kw0 kw1 kw3) => { f() };
24+
}
25+
26+
fn main() {
27+
kw0!(keyword);
28+
kw1!(fn);
29+
kw2!(kw0 kw1 kw3);
30+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// { dg-output "invok\ninvok\ninvok\ninvok\ninvok\n" }
2+
extern "C" {
3+
fn printf(s: *const i8, ...);
4+
}
5+
6+
fn f() {
7+
let r_s = "invok\n\0";
8+
let s_p = r_s as *const str;
9+
let c_p = s_p as *const i8;
10+
11+
printf(c_p);
12+
}
13+
14+
macro_rules! invocation0 {
15+
(valid) => { f() };
16+
() => { };
17+
}
18+
19+
macro_rules! invocation1 {
20+
(valid) => { };
21+
() => { f() };
22+
}
23+
24+
macro_rules! invocation2 {
25+
(valid) => { f() };
26+
(invalid) => { };
27+
}
28+
29+
macro_rules! invocation3 {
30+
(this is a valid invocation) => { f() };
31+
(not this one) => { };
32+
}
33+
34+
macro_rules! invocation4 {
35+
(fn f() {}) => { f() };
36+
(not a keyword) => { };
37+
}
38+
39+
fn main() {
40+
invocation0!(valid);
41+
invocation1!();
42+
invocation2!(valid);
43+
invocation3!(this is a valid invocation);
44+
invocation4!(fn f() {});
45+
}

0 commit comments

Comments
 (0)