diff --git a/src/tools/Cargo.lock b/src/tools/Cargo.lock index 251f074c32..2aca3c591b 100644 --- a/src/tools/Cargo.lock +++ b/src/tools/Cargo.lock @@ -5,6 +5,7 @@ dependencies = [ "cretonne 0.0.0", "cretonne-reader 0.0.0", "docopt 0.6.80 (registry+https://github.com/rust-lang/crates.io-index)", + "filecheck 0.0.0", "glob 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)", "regex 0.1.71 (registry+https://github.com/rust-lang/crates.io-index)", "rustc-serialize 0.3.19 (registry+https://github.com/rust-lang/crates.io-index)", @@ -42,6 +43,13 @@ dependencies = [ "strsim 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "filecheck" +version = "0.0.0" +dependencies = [ + "regex 0.1.71 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "glob" version = "0.2.11" diff --git a/src/tools/Cargo.toml b/src/tools/Cargo.toml index 8164a4e4bb..ba465f7d0e 100644 --- a/src/tools/Cargo.toml +++ b/src/tools/Cargo.toml @@ -12,6 +12,7 @@ path = "main.rs" [dependencies] cretonne = { path = "../libcretonne" } cretonne-reader = { path = "../libreader" } +filecheck = { path = "../libfilecheck" } docopt = "0.6.80" rustc-serialize = "0.3.19" regex = "0.1.71" diff --git a/src/tools/main.rs b/src/tools/main.rs index abad701af6..bd0999cd4e 100644 --- a/src/tools/main.rs +++ b/src/tools/main.rs @@ -3,6 +3,7 @@ extern crate cretonne; extern crate cton_reader; extern crate docopt; extern crate rustc_serialize; +extern crate filecheck; use cretonne::VERSION; use docopt::Docopt; @@ -12,26 +13,31 @@ use std::process; mod cat; mod print_cfg; +mod rsfilecheck; const USAGE: &'static str = " Cretonne code generator utility Usage: cton-util cat ... + cton-util filecheck [-v] cton-util print-cfg ... cton-util --help | --version Options: - -h, --help print this help message - --version print the Cretonne version + -v, --verbose be more verbose + -h, --help print this help message + --version print the Cretonne version "; #[derive(RustcDecodable, Debug)] struct Args { cmd_cat: bool, + cmd_filecheck: bool, cmd_print_cfg: bool, arg_file: Vec, + flag_verbose: bool, } /// A command either succeeds or fails with an error message. @@ -51,6 +57,8 @@ fn cton_util() -> CommandResult { // Find the sub-command to execute. if args.cmd_cat { cat::run(args.arg_file) + } else if args.cmd_filecheck { + rsfilecheck::run(args.arg_file, args.flag_verbose) } else if args.cmd_print_cfg { print_cfg::run(args.arg_file) } else { diff --git a/src/tools/rsfilecheck.rs b/src/tools/rsfilecheck.rs new file mode 100644 index 0000000000..bce661282c --- /dev/null +++ b/src/tools/rsfilecheck.rs @@ -0,0 +1,40 @@ +use CommandResult; +use filecheck::{CheckerBuilder, Checker, NO_VARIABLES}; +use std::fs::File; +use std::io::{self, Read}; + +pub fn run(files: Vec, verbose: bool) -> CommandResult { + if files.is_empty() { + return Err("No check files".to_string()); + } + let checker = try!(read_checkfile(&files[0])); + if checker.is_empty() { + return Err(format!("{}: no filecheck directives found", files[0])); + } + + // Print out the directives under --verbose. + if verbose { + println!("{}", checker); + } + + let mut buffer = String::new(); + try!(io::stdin().read_to_string(&mut buffer).map_err(|e| format!("stdin: {}", e))); + + if try!(checker.check(&buffer, NO_VARIABLES).map_err(|e| e.to_string())) { + Ok(()) + } else { + // TODO: We need to do better than this. + Err("Check failed".to_string()) + } +} + +fn read_checkfile(filename: &str) -> Result { + let mut file = try!(File::open(&filename).map_err(|e| format!("{}: {}", filename, e))); + let mut buffer = String::new(); + try!(file.read_to_string(&mut buffer) + .map_err(|e| format!("Couldn't read {}: {}", filename, e))); + + let mut builder = CheckerBuilder::new(); + try!(builder.text(&buffer).map_err(|e| format!("{}: {}", filename, e))); + Ok(builder.finish()) +} diff --git a/test-all.sh b/test-all.sh index 64fb83d3a8..cd3916d8c3 100755 --- a/test-all.sh +++ b/test-all.sh @@ -40,7 +40,7 @@ else echo "If a newer version of rustfmt is available, update this script." fi -PKGS="cretonne cretonne-reader cretonne-tools" +PKGS="cretonne cretonne-reader cretonne-tools filecheck" cd "$topdir/src/tools" for PKG in $PKGS do