From cb718b869c555ae8874c549e7bd9a01033ee446c Mon Sep 17 00:00:00 2001 From: Jakob Stoklund Olesen Date: Fri, 4 Nov 2016 10:49:09 -0700 Subject: [PATCH] TestFile preamble comments apply to all functions. Include the test file preamble comments when building a filecheck instance for every function in the file. This makes it possible to define common regex variables in the preamble and use these definitions for all the functions. --- cranelift/docs/testing.rst | 4 ++++ cranelift/filetests/isa/riscv/legalize-i64.cton | 9 +++------ cranelift/src/filetest/runone.rs | 1 + cranelift/src/filetest/subtest.rs | 17 ++++++++++++----- 4 files changed, 20 insertions(+), 11 deletions(-) diff --git a/cranelift/docs/testing.rst b/cranelift/docs/testing.rst index 419f51ff22..d52b180c99 100644 --- a/cranelift/docs/testing.rst +++ b/cranelift/docs/testing.rst @@ -150,6 +150,10 @@ use filecheck will extract comments associated with each function (or its entities) and scan them for filecheck directives. The test output for each function is then matched against the filecheck directives for that function. +Comments appearing before the first function in a file apply to every function. +This is useful for defining common regular expression variables with the +``regex:`` directive, for example. + Note that LLVM's file tests don't separate filecheck directives by their associated function. It verifies the concatenated output against all filecheck directives in the test file. LLVM's :command:`FileCheck` command has a diff --git a/cranelift/filetests/isa/riscv/legalize-i64.cton b/cranelift/filetests/isa/riscv/legalize-i64.cton index 916986fe68..d4f3e6b05e 100644 --- a/cranelift/filetests/isa/riscv/legalize-i64.cton +++ b/cranelift/filetests/isa/riscv/legalize-i64.cton @@ -2,13 +2,14 @@ test legalizer isa riscv supports_m=1 +; regex: V=v\d+ +; regex: VX=vx\d+ + function bitwise_and(i64, i64) -> i64 { ebb0(v1: i64, v2: i64): v3 = band v1, v2 return v3 } -; regex: V=v\d+ -; regex: VX=vx\d+ ; check: $(v1l=$V), $(v1h=$VX) = isplit_lohi $v1 ; check: $(v2l=$V), $(v2h=$VX) = isplit_lohi $v2 ; check: [R#ec @@ -22,8 +23,6 @@ ebb0(v1: i64, v2: i64): v3 = bor v1, v2 return v3 } -; regex: V=v\d+ -; regex: VX=vx\d+ ; check: $(v1l=$V), $(v1h=$VX) = isplit_lohi $v1 ; check: $(v2l=$V), $(v2h=$VX) = isplit_lohi $v2 ; check: [R#cc @@ -37,8 +36,6 @@ ebb0(v1: i64, v2: i64): v3 = bxor v1, v2 return v3 } -; regex: V=v\d+ -; regex: VX=vx\d+ ; check: $(v1l=$V), $(v1h=$VX) = isplit_lohi $v1 ; check: $(v2l=$V), $(v2h=$VX) = isplit_lohi $v2 ; check: [R#8c diff --git a/cranelift/src/filetest/runone.rs b/cranelift/src/filetest/runone.rs index d0199c45cf..d84f0f7041 100644 --- a/cranelift/src/filetest/runone.rs +++ b/cranelift/src/filetest/runone.rs @@ -50,6 +50,7 @@ pub fn run(path: &Path) -> TestResult { for (func, details) in testfile.functions { let mut context = Context { + preamble_comments: &testfile.preamble_comments, details: details, verified: false, flags: flags, diff --git a/cranelift/src/filetest/subtest.rs b/cranelift/src/filetest/subtest.rs index fee9bbef85..f78ac7a6b1 100644 --- a/cranelift/src/filetest/subtest.rs +++ b/cranelift/src/filetest/subtest.rs @@ -5,13 +5,16 @@ use std::borrow::Cow; use cretonne::ir::Function; use cretonne::isa::TargetIsa; use cretonne::settings::Flags; -use cton_reader::Details; +use cton_reader::{Details, Comment}; use filecheck::{self, CheckerBuilder, Checker, Value as FCValue}; pub type Result = result::Result; /// Context for running a a test on a single function. pub struct Context<'a> { + /// Comments from the preamble f the test file. These apply to all functions. + pub preamble_comments: &'a [Comment<'a>], + /// Additional details about the function from the parser. pub details: Details<'a>, @@ -69,7 +72,7 @@ impl<'a> filecheck::VariableMap for Context<'a> { /// Run filecheck on `text`, using directives extracted from `context`. pub fn run_filecheck(text: &str, context: &Context) -> Result<()> { - let checker = try!(build_filechecker(&context.details)); + let checker = try!(build_filechecker(context)); if try!(checker.check(&text, context).map_err(|e| format!("filecheck: {}", e))) { Ok(()) } else { @@ -80,10 +83,14 @@ pub fn run_filecheck(text: &str, context: &Context) -> Result<()> { } } -/// Build a filechecker using the directives in the function's comments. -pub fn build_filechecker(details: &Details) -> Result { +/// Build a filechecker using the directives in the file preamble and the function's comments. +pub fn build_filechecker(context: &Context) -> Result { let mut builder = CheckerBuilder::new(); - for comment in &details.comments { + // Preamble comments apply to all functions. + for comment in context.preamble_comments { + try!(builder.directive(comment.text).map_err(|e| format!("filecheck: {}", e))); + } + for comment in &context.details.comments { try!(builder.directive(comment.text).map_err(|e| format!("filecheck: {}", e))); } let checker = builder.finish();