Skip to content

Commit af789a7

Browse files
committed
macros: Add test cases for repetitions
1 parent ad7e4bb commit af789a7

File tree

6 files changed

+106
-0
lines changed

6 files changed

+106
-0
lines changed

gcc/testsuite/rust/compile/macro6.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
macro_rules! zero_or_one {
2+
($($a:literal)?) => { // { dg-error "invalid amount of matches for macro invocation. Expected between 0 and 1, got 2" }
3+
f()
4+
}
5+
}
6+
7+
fn main() {
8+
zero_or_one!();
9+
zero_or_one!(14);
10+
zero_or_one!(125 12 "gcc"); // { dg-error "Failed to match any rule within macro" }
11+
}

gcc/testsuite/rust/compile/macro7.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
fn f() {}
2+
3+
macro_rules! one_or_more {
4+
($($a:literal)+) => { // { dg-error "invalid amount of matches for macro invocation" }
5+
f()
6+
}
7+
}
8+
9+
fn main() {
10+
one_or_more!(1 1 1 1 1 1 1 1 1 1 1 "rust" 'c');
11+
one_or_more!(1);
12+
one_or_more!(); // { dg-error "Failed to match any rule within macro" }
13+
}

gcc/testsuite/rust/compile/macro8.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
fn f() {}
2+
3+
macro_rules! expr {
4+
($($a:expr)?) => {
5+
f()
6+
}
7+
}
8+
9+
fn main() {
10+
expr!();
11+
expr!(14);
12+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// { dg-output "any\nany\nany\nany\nany\n" }
2+
extern "C" {
3+
fn printf(s: *const i8, ...);
4+
}
5+
6+
fn f() {
7+
let r_s = "any\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! any {
15+
($($a:expr)*) => {
16+
$($a;)*
17+
}
18+
}
19+
20+
fn main() {
21+
any!(); // valid, but does not print anything
22+
any!(f() f());
23+
any!(f() f() f());
24+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// { dg-output "zo1\nzo1\n" }
2+
extern "C" {
3+
fn printf(s: *const i8, ...);
4+
}
5+
6+
fn f() {
7+
let r_s = "zo1\n\0";
8+
let s_p = r_s as *const str;
9+
let c_p = s_p as *const i8;
10+
11+
unsafe { printf(c_p); }
12+
}
13+
14+
macro_rules! zero_or_one {
15+
($($a:expr)?) => {
16+
f()
17+
}
18+
}
19+
20+
fn main() {
21+
zero_or_one!();
22+
zero_or_one!(f());
23+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// { dg-output "oom\noom\noom\n" }
2+
extern "C" {
3+
fn printf(s: *const i8, ...);
4+
}
5+
6+
fn f() {
7+
let r_s = "oom\n\0";
8+
let s_p = r_s as *const str;
9+
let c_p = s_p as *const i8;
10+
11+
unsafe { printf(c_p); }
12+
}
13+
14+
macro_rules! one_or_more {
15+
($($a:expr)+) => {
16+
$($a;)+
17+
}
18+
}
19+
20+
fn main() {
21+
one_or_more!(f());
22+
one_or_more!(f() f());
23+
}

0 commit comments

Comments
 (0)