Skip to content

Commit 3aad0e2

Browse files
pnkfelixalexcrichton
authored andcommitted
Unit tests for flowgraph pretty printing.
Each test works by rendering the flowgraph for the last identified block we see in expanded pretty-printed output, and comparing it (via `diff`) against a checked in "foo.dot-expected.dot" file. Each test post-processes the output to remove NodeIds ` (id=NUM)` so that the expected output is somewhat stable (or at least independent of how we assign NodeIds) and easier for a human to interpret when looking at the expected output file itself. ---- Test writing style notes: I usually tried to write the tests in a way that would avoid duplicate labels in the output rendered flow graph, when possible. The tests that have string literals "unreachable" in the program text are deliberately written that way to remind the reader that the unreachable nodes in the resulting graph are not an error in the control flow computation, but rather a natural consequence of its construction.
1 parent aaf398f commit 3aad0e2

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

47 files changed

+1190
-0
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
-include ../tools.mk
2+
3+
FILES=f00.rs f01.rs f02.rs f03.rs f04.rs f05.rs f06.rs f07.rs \
4+
f08.rs f09.rs f10.rs f11.rs f12.rs f13.rs f14.rs f15.rs \
5+
f16.rs f17.rs f18.rs f19.rs f20.rs f21.rs f22.rs
6+
7+
8+
# all: $(patsubst %.rs,$(TMPDIR)/%.dot,$(FILES)) $(patsubst %.rs,$(TMPDIR)/%.pp,$(FILES))
9+
all: $(patsubst %.rs,$(TMPDIR)/%.check,$(FILES))
10+
11+
12+
RUSTC_LIB=$(RUSTC) --crate-type=lib
13+
14+
define FIND_LAST_BLOCK
15+
LASTBLOCKNUM_$(1) := $(shell $(RUSTC_LIB) --pretty=expanded,identified $(1) \
16+
| grep block
17+
| tail -1
18+
| sed -e 's@.*/\* block \([0-9]*\) \*/.*@\1@')
19+
endef
20+
21+
ifeq ($(findstring rustc,$(RUSTC)),)
22+
$(error Must set RUSTC)
23+
endif
24+
25+
$(TMPDIR)/%.pp: %.rs
26+
$(RUSTC_LIB) --pretty=expanded,identified $< -o $@
27+
28+
$(TMPDIR)/%.dot: %.rs
29+
$(eval $(call FIND_LAST_BLOCK,$<))
30+
$(RUSTC_LIB) --pretty flowgraph=$(LASTBLOCKNUM_$<) $< -o $@.tmp
31+
cat $@.tmp | sed -e 's@ (id=[0-9]*)@@g' \
32+
-e 's@\[label=""\]@@' \
33+
-e 's@digraph [a-zA-Z0-9_]* @digraph block @' \
34+
> $@
35+
36+
$(TMPDIR)/%.check: %.rs $(TMPDIR)/%.dot
37+
diff -u $(patsubst %.rs,$(TMPDIR)/%.dot,$<) $(patsubst %.rs,%.dot-expected.dot,$<)
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
digraph block {
2+
N0[label="entry"];
3+
N1[label="exit"];
4+
N2[label="block { }"];
5+
N0 -> N2;
6+
N2 -> N1;
7+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
pub fn empty_0() {
12+
13+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
digraph block {
2+
N0[label="entry"];
3+
N1[label="exit"];
4+
N2[label="expr 1"];
5+
N3[label="block { 1; }"];
6+
N0 -> N2;
7+
N2 -> N3;
8+
N3 -> N1;
9+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
pub fn lit_1() {
12+
1;
13+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
digraph block {
2+
N0[label="entry"];
3+
N1[label="exit"];
4+
N2[label="local _x"];
5+
N3[label="block { let _x: int; }"];
6+
N0 -> N2;
7+
N2 -> N3;
8+
N3 -> N1;
9+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
pub fn decl_x_2() {
12+
let _x : int;
13+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
digraph block {
2+
N0[label="entry"];
3+
N1[label="exit"];
4+
N2[label="expr 3"];
5+
N3[label="expr 33"];
6+
N4[label="expr 3 + 33"];
7+
N5[label="block { 3 + 33; }"];
8+
N0 -> N2;
9+
N2 -> N3;
10+
N3 -> N4;
11+
N4 -> N5;
12+
N5 -> N1;
13+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
pub fn expr_add_3() {
12+
3 + 33;
13+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
digraph block {
2+
N0[label="entry"];
3+
N1[label="exit"];
4+
N2[label="expr 4"];
5+
N3[label="local _x"];
6+
N4[label="block { let _x = 4; }"];
7+
N0 -> N2;
8+
N2 -> N3;
9+
N3 -> N4;
10+
N4 -> N1;
11+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
pub fn pat_id_4() {
12+
let _x = 4;
13+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
digraph block {
2+
N0[label="entry"];
3+
N1[label="exit"];
4+
N2[label="expr 5"];
5+
N3[label="expr 55"];
6+
N4[label="expr (5, 55)"];
7+
N5[label="local _x"];
8+
N6[label="local _y"];
9+
N7[label="pat (_x, _y)"];
10+
N8[label="block { let (_x, _y) = (5, 55); }"];
11+
N0 -> N2;
12+
N2 -> N3;
13+
N3 -> N4;
14+
N4 -> N5;
15+
N5 -> N6;
16+
N6 -> N7;
17+
N7 -> N8;
18+
N8 -> N1;
19+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
pub fn pat_tup_5() {
12+
let (_x, _y) = (5, 55);
13+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
digraph block {
2+
N0[label="entry"];
3+
N1[label="exit"];
4+
N2[label="expr 6"];
5+
N3[label="expr S6{val: 6,}"];
6+
N4[label="local _x"];
7+
N5[label="pat S6{val: _x}"];
8+
N6[label="block { let S6{val: _x} = S6{val: 6,}; }"];
9+
N0 -> N2;
10+
N2 -> N3;
11+
N3 -> N4;
12+
N4 -> N5;
13+
N5 -> N6;
14+
N6 -> N1;
15+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
struct S6 { val: int }
12+
pub fn pat_struct_6() {
13+
let S6 { val: _x } = S6{ val: 6 };
14+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
digraph block {
2+
N0[label="entry"];
3+
N1[label="exit"];
4+
N2[label="expr 7"];
5+
N3[label="expr 77"];
6+
N4[label="expr 777"];
7+
N5[label="expr 7777"];
8+
N6[label="expr [7, 77, 777, 7777]"];
9+
N7[label="expr match [7, 77, 777, 7777] { [x, y, ..] => x + y }"];
10+
N8[label="local x"];
11+
N9[label="local y"];
12+
N10[label="pat .."];
13+
N11[label="pat [x, y, ..]"];
14+
N12[label="expr x"];
15+
N13[label="expr y"];
16+
N14[label="expr x + y"];
17+
N15[label="block { match [7, 77, 777, 7777] { [x, y, ..] => x + y }; }"];
18+
N0 -> N2;
19+
N2 -> N3;
20+
N3 -> N4;
21+
N4 -> N5;
22+
N5 -> N6;
23+
N6 -> N8;
24+
N8 -> N9;
25+
N9 -> N10;
26+
N10 -> N11;
27+
N11 -> N12;
28+
N12 -> N13;
29+
N13 -> N14;
30+
N14 -> N7;
31+
N7 -> N15;
32+
N15 -> N1;
33+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
pub fn pat_vec_7() {
12+
match [7, 77, 777, 7777] {
13+
[x, y, ..] => x + y
14+
};
15+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
digraph block {
2+
N0[label="entry"];
3+
N1[label="exit"];
4+
N2[label="expr 8"];
5+
N3[label="local x"];
6+
N4[label="local _y"];
7+
N5[label="expr x"];
8+
N6[label="expr 88"];
9+
N7[label="expr x > 88"];
10+
N8[label="expr 888"];
11+
N9[label="expr _y"];
12+
N10[label="expr _y = 888"];
13+
N11[label="block { _y = 888; }"];
14+
N12[label="expr if x > 88 { _y = 888; }"];
15+
N13[label="block { let x = 8; let _y; if x > 88 { _y = 888; } }"];
16+
N0 -> N2;
17+
N2 -> N3;
18+
N3 -> N4;
19+
N4 -> N5;
20+
N5 -> N6;
21+
N6 -> N7;
22+
N7 -> N8;
23+
N8 -> N9;
24+
N9 -> N10;
25+
N10 -> N11;
26+
N7 -> N12;
27+
N11 -> N12;
28+
N12 -> N13;
29+
N13 -> N1;
30+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
pub fn expr_if_onearm_8() {
12+
let x = 8; let _y;
13+
if x > 88 {
14+
_y = 888;
15+
}
16+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
digraph block {
2+
N0[label="entry"];
3+
N1[label="exit"];
4+
N2[label="expr 91"];
5+
N3[label="local x"];
6+
N4[label="local _y"];
7+
N5[label="expr x"];
8+
N6[label="expr 92"];
9+
N7[label="expr x > 92"];
10+
N8[label="expr 93"];
11+
N9[label="expr _y"];
12+
N10[label="expr _y = 93"];
13+
N11[label="block { _y = 93; }"];
14+
N12[label="expr 94"];
15+
N13[label="expr 95"];
16+
N14[label="expr 94 + 95"];
17+
N15[label="expr _y"];
18+
N16[label="expr _y = 94 + 95"];
19+
N17[label="block { _y = 94 + 95; }"];
20+
N18[label="expr { _y = 94 + 95; }"];
21+
N19[label="expr if x > 92 { _y = 93; } else { _y = 94 + 95; }"];
22+
N20[label="block { let x = 91; let _y; if x > 92 { _y = 93; } else { _y = 94 + 95; } }"];
23+
N0 -> N2;
24+
N2 -> N3;
25+
N3 -> N4;
26+
N4 -> N5;
27+
N5 -> N6;
28+
N6 -> N7;
29+
N7 -> N8;
30+
N8 -> N9;
31+
N9 -> N10;
32+
N10 -> N11;
33+
N7 -> N12;
34+
N12 -> N13;
35+
N13 -> N14;
36+
N14 -> N15;
37+
N15 -> N16;
38+
N16 -> N17;
39+
N17 -> N18;
40+
N11 -> N19;
41+
N18 -> N19;
42+
N19 -> N20;
43+
N20 -> N1;
44+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
pub fn expr_if_twoarm_9() {
12+
let x = 91; let _y;
13+
if x > 92 {
14+
_y = 93;
15+
} else {
16+
_y = 94+95;
17+
}
18+
}

0 commit comments

Comments
 (0)