diff --git a/cranelift/tests/cfg/loop.cton b/cranelift/filetests/cfg/loop.cton similarity index 98% rename from cranelift/tests/cfg/loop.cton rename to cranelift/filetests/cfg/loop.cton index fd6e4fa6b9..5732ac593a 100644 --- a/cranelift/tests/cfg/loop.cton +++ b/cranelift/filetests/cfg/loop.cton @@ -1,4 +1,5 @@ ; For testing cfg generation. This code is nonsense. +test print-cfg function nonsense(i32, i32) -> f32 { ; check: digraph nonsense { diff --git a/cranelift/tests/cfg/traps_early.cton b/cranelift/filetests/cfg/traps_early.cton similarity index 96% rename from cranelift/tests/cfg/traps_early.cton rename to cranelift/filetests/cfg/traps_early.cton index 71070f4b15..a52cac1ba4 100644 --- a/cranelift/tests/cfg/traps_early.cton +++ b/cranelift/filetests/cfg/traps_early.cton @@ -1,5 +1,6 @@ ; For testing cfg generation. This code explores the implications of encountering ; a terminating instruction before any connections have been made. +test print-cfg function nonsense(i32) { ; check: digraph nonsense { diff --git a/cranelift/tests/cfg/unused_node.cton b/cranelift/filetests/cfg/unused_node.cton similarity index 97% rename from cranelift/tests/cfg/unused_node.cton rename to cranelift/filetests/cfg/unused_node.cton index 693196ccde..d4be2deddd 100644 --- a/cranelift/tests/cfg/unused_node.cton +++ b/cranelift/filetests/cfg/unused_node.cton @@ -1,4 +1,5 @@ ; For testing cfg generation where some block is never reached. +test print-cfg function not_reached(i32) -> i32 { ; check: digraph not_reached { diff --git a/cranelift/src/tools/filetest/subtest.rs b/cranelift/src/tools/filetest/subtest.rs index 69ca47aa64..f87eda9b98 100644 --- a/cranelift/src/tools/filetest/subtest.rs +++ b/cranelift/src/tools/filetest/subtest.rs @@ -11,8 +11,10 @@ pub type Result = result::Result; /// Create a new subcommand trait object to match `parsed.command`. pub fn new(parsed: &TestCommand) -> Result> { use cat; + use print_cfg; match parsed.command { "cat" => cat::subtest(parsed), + "print-cfg" => print_cfg::subtest(parsed), _ => Err(format!("unknown test command '{}'", parsed.command)), } } diff --git a/cranelift/src/tools/print_cfg.rs b/cranelift/src/tools/print_cfg.rs index d4d6ccf79f..858ef4de23 100644 --- a/cranelift/src/tools/print_cfg.rs +++ b/cranelift/src/tools/print_cfg.rs @@ -2,14 +2,17 @@ //! //! Read a series of Cretonne IL files and print their control flow graphs //! in graphviz format. + +use std::borrow::Cow; use std::fmt::{Result, Write, Display, Formatter}; use CommandResult; use utils::read_to_string; +use filetest::subtest::{self, SubTest, Context, Result as STResult}; use cretonne::ir::Function; use cretonne::cfg::ControlFlowGraph; use cretonne::ir::instructions::BranchInfo; -use cton_reader::parse_functions; +use cton_reader::{parse_functions, TestCommand}; pub fn run(files: Vec) -> CommandResult { for (i, f) in files.into_iter().enumerate() { @@ -100,3 +103,29 @@ fn print_cfg(filename: String) -> CommandResult { Ok(()) } + +/// Object implementing the `test print-cfg` sub-test. +struct TestPrintCfg; + +pub fn subtest(parsed: &TestCommand) -> STResult> { + assert_eq!(parsed.command, "print-cfg"); + if !parsed.options.is_empty() { + Err(format!("No options allowed on {}", parsed)) + } else { + Ok(Box::new(TestPrintCfg)) + } +} + +impl SubTest for TestPrintCfg { + fn name(&self) -> Cow { + Cow::from("print-cfg") + } + + fn needs_verifier(&self) -> bool { + false + } + + fn run(&self, func: Cow, context: &Context) -> STResult<()> { + subtest::run_filecheck(&CFGPrinter::new(&func).to_string(), context) + } +} diff --git a/cranelift/test-all.sh b/cranelift/test-all.sh index 6c8406493a..87aea4573a 100755 --- a/cranelift/test-all.sh +++ b/cranelift/test-all.sh @@ -62,9 +62,4 @@ cd "$topdir" banner "File tests" "$CTONUTIL" test filetests -# Run the parser tests. -cd "$topdir/tests" -banner "CFG tests" -cfg/run.sh - banner "OK" diff --git a/cranelift/tests/cfg/README.rst b/cranelift/tests/cfg/README.rst deleted file mode 100644 index c1ee4b1fa3..0000000000 --- a/cranelift/tests/cfg/README.rst +++ /dev/null @@ -1,14 +0,0 @@ -CFG tests -============ - -This directory contains test cases for the Cretonne cfg printer. - -Each test case consists of a `foo.cton` input file annotated with its expected connections. -Annotations are comments of the form: `ebbx:insty -> ebbz` where ebbx is connected to ebbz via -a branch or jump instruction at line y. Instructions are labeled by line number starting from zero: `inst0` .. `instn`. - - -Each input file is run through the `cton-util print-cfg` command and the -output is compared against the specially formatted comments to ensure that -expected connections exist. This scheme allows for changes to graph style -without the need to update tests. diff --git a/cranelift/tests/cfg/run.sh b/cranelift/tests/cfg/run.sh deleted file mode 100755 index cd324cc73f..0000000000 --- a/cranelift/tests/cfg/run.sh +++ /dev/null @@ -1,36 +0,0 @@ -#!/bin/bash - -# Go to tests directory. -cd $(dirname "$0")/.. - -# The path to cton-util should be in $CTONUTIL. -if [ -z "$CTONUTIL" ]; then - CTONUTIL=../src/tools/target/debug/cton-util -fi - -if [ ! -x "$CTONUTIL" ]; then - echo "Can't fund executable cton-util: $CTONUTIL" 1>&2 - exit 1 -fi - -declare -a fails - -for testcase in $(find cfg -name '*.cton'); do - if "${CTONUTIL}" print-cfg "$testcase" | "${CTONUTIL}" filecheck "$testcase"; then - echo OK $testcase - else - fails=(${fails[@]} "$testcase") - echo FAIL $testcase - fi -done - -if [ ${#fails[@]} -ne 0 ]; then - echo - echo Failures: - for f in "${fails[@]}"; do - echo " $f" - done - exit 1 -else - echo "All passed" -fi