Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: infrastructure for testing save-analysis #34522

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
166 changes: 166 additions & 0 deletions src/test/save-analysis/functions.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
{
"prelude": {
"crate_name": "functions",
"span": {
"file_name": "functions.rs",
"byte_start": 0,
"byte_end": 149,
"line_start": 1,
"line_end": 18,
"column_start": 1,
"column_end": 1
}
},
"imports": [
{
"kind": "Use",
"span": {
"file_name": "functions.rs",
"byte_start": 9,
"byte_end": 12,
"line_start": 1,
"line_end": 1,
"column_start": 10,
"column_end": 13
},
"name": "bar"
}
],
"defs": [
{
"kind": "Mod",
"span": {
"file_name": "functions.rs",
"byte_start": 21,
"byte_end": 24,
"line_start": 3,
"line_end": 3,
"column_start": 5,
"column_end": 8
},
"name": "foo",
"qualname": "::foo",
"value": "functions.rs"
},
{
"kind": "Function",
"span": {
"file_name": "functions.rs",
"byte_start": 39,
"byte_end": 42,
"line_start": 4,
"line_end": 4,
"column_start": 12,
"column_end": 15
},
"name": "bar",
"qualname": "::foo::bar",
},
{
"kind": "Mod",
"span": {
"file_name": "functions.rs",
"byte_start": 66,
"byte_end": 69,
"line_start": 9,
"line_end": 9,
"column_start": 5,
"column_end": 8
},
"name": "baz",
"qualname": "::baz",
"value": "functions.rs"
},
{
"kind": "Function",
"span": {
"file_name": "functions.rs",
"byte_start": 84,
"byte_end": 87,
"line_start": 10,
"line_end": 10,
"column_start": 12,
"column_end": 15
},
"name": "bar",
"qualname": "::baz::bar",
},
{
"kind": "Function",
"span": {
"file_name": "functions.rs",
"byte_start": 110,
"byte_end": 114,
"line_start": 15,
"line_end": 15,
"column_start": 4,
"column_end": 8
},
"name": "main",
"qualname": "::main",
"value": "()"
}
],
"refs": [
{
"kind": "Function",
"span": {
"file_name": "functions.rs",
"byte_start": 9,
"byte_end": 12,
"line_start": 1,
"line_end": 1,
"column_start": 10,
"column_end": 13
},
},
{
"kind": "Mod",
"span": {
"file_name": "functions.rs",
"byte_start": 4,
"byte_end": 7,
"line_start": 1,
"line_end": 1,
"column_start": 5,
"column_end": 8
},
},
{
"kind": "Function",
"span": {
"file_name": "functions.rs",
"byte_start": 124,
"byte_end": 127,
"line_start": 16,
"line_end": 16,
"column_start": 5,
"column_end": 8
},
},
{
"kind": "Function",
"span": {
"file_name": "functions.rs",
"byte_start": 141,
"byte_end": 144,
"line_start": 17,
"line_end": 17,
"column_start": 10,
"column_end": 13
},
},
{
"kind": "Mod",
"span": {
"file_name": "functions.rs",
"byte_start": 136,
"byte_end": 139,
"line_start": 17,
"line_end": 17,
"column_start": 5,
"column_end": 8
},
}
]
}
18 changes: 18 additions & 0 deletions src/test/save-analysis/functions.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
use foo::bar;

mod foo {
pub fn bar() {

}
}

mod baz {
pub fn bar() {

}
}

fn main() {
bar();
baz::bar();
}
90 changes: 90 additions & 0 deletions src/tools/compiletest/src/analysisdiff.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
use rustc_serialize::json::{self, Json};
use std::fs::File;
use std::path::Path;

pub struct Mismatch {
pub path: Vec<String>,
pub expected: Json,
pub found: Option<Json>
}

pub fn compare_analysis(output: &Path, expected: &Path) -> Vec<Mismatch> {
let mut output = json::from_reader(&mut File::open(output).unwrap()).expect("Invalid JSON");
let expected = json::from_reader(&mut File::open(expected).unwrap()).expect("Invalid JSON");
check_matching(vec![], &mut output, &expected)
}

// Returns a vector containing mismatches between the keys in `expected` and those
// in `output`. Keys that are present in `output` but not in `expected` are ignored.
fn check_matching(mut path: Vec<String>, output: &mut Json, expected: &Json) -> Vec<Mismatch> {
match (output, expected) {
(&mut Json::Object(ref mut o_obj), &Json::Object(ref e_obj)) => {
let mut mismatches = vec![];

// Check that all keys in `e_obj` contain the same values as the keys in
// `o_obj`
for (k, e_v) in e_obj {
let mut new_path = path.clone();
new_path.push(k.clone());

// If both keys exist, check recursively. Otherwise, signal a mismatch.
if let Some(o_v) = o_obj.get_mut(k) {
mismatches.extend(check_matching(new_path, o_v, &e_v));
} else {
mismatches.push(Mismatch { path: new_path, expected: e_v.clone(), found: None });
}
}

mismatches
}
(&mut Json::Array(ref mut o_arr), &Json::Array(ref e_arr)) => {
let mut mismatches = vec![];
let mut o_arr = o_arr.clone();
path.push("<list-element>".to_string());

// Each element in `e_arr` is compared against each element in `e_obj`,
// until a match has been found.
for e in e_arr {
let mut matches = None;

// Find a match between `e` and any `o`
for (i, o) in o_arr.iter_mut().enumerate() {
// Note: we can use the empty string as path, since we are not going to use
// the mismatches
if check_matching(Vec::new(), o, &e).len() == 0 {
matches = Some(i);
break;
}
}

if let Some(i) = matches {
// Remove the element from `o_arr`, to prevent double matches
o_arr.swap_remove(i);
} else {
// No `o` could match `e`
mismatches.push(Mismatch { path: path.clone(), expected: e.clone(), found: None });
}
}

mismatches
}

// Mismatched types or scalar values
(output, expected) => {
if expected == output {
vec![]
} else {
vec![Mismatch {
path: path,
expected: expected.clone(),
found: Some(output.clone())
}]
}
}
}

// Enumerate all keys in expected
// Retrieve the current key from output:
// * If present, compare values
// * If absent,
}
3 changes: 3 additions & 0 deletions src/tools/compiletest/src/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ pub enum Mode {
CodegenUnits,
Incremental,
RunMake,
SaveAnalysis,
Ui,
}

Expand All @@ -48,6 +49,7 @@ impl FromStr for Mode {
"codegen-units" => Ok(CodegenUnits),
"incremental" => Ok(Incremental),
"run-make" => Ok(RunMake),
"save-analysis" => Ok(SaveAnalysis),
"ui" => Ok(Ui),
_ => Err(()),
}
Expand All @@ -70,6 +72,7 @@ impl fmt::Display for Mode {
CodegenUnits => "codegen-units",
Incremental => "incremental",
RunMake => "run-make",
SaveAnalysis => "save-analysis",
Ui => "ui",
}, f)
}
Expand Down
30 changes: 14 additions & 16 deletions src/tools/compiletest/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,16 @@ use util::logv;

use self::header::EarlyProps;

pub mod procsrv;
pub mod util;
mod json;
pub mod header;
pub mod runtest;
mod analysisdiff;
pub mod common;
pub mod errors;
pub mod header;
mod json;
mod raise_fd_limit;
pub mod runtest;
pub mod procsrv;
mod uidiff;
pub mod util;

fn main() {
#[cfg(cargobuild)]
Expand Down Expand Up @@ -86,7 +87,8 @@ pub fn parse_config(args: Vec<String> ) -> Config {
reqopt("", "stage-id", "the target-stage identifier", "stageN-TARGET"),
reqopt("", "mode", "which sort of compile tests to run",
"(compile-fail|parse-fail|run-fail|run-pass|\
run-pass-valgrind|pretty|debug-info|incremental)"),
run-pass-valgrind|pretty|debug-info|incremental|\
save-analysis)"),
optflag("", "ignored", "run tests marked as ignored"),
optopt("", "runtool", "supervisor program to run tests under \
(eg. emulator, valgrind)", "PROGRAM"),
Expand Down Expand Up @@ -252,14 +254,11 @@ pub fn run_tests(config: &Config) {
env::set_var("RUST_TEST_THREADS","1");
}

match config.mode {
DebugInfoLldb => {
// Some older versions of LLDB seem to have problems with multiple
// instances running in parallel, so only run one test thread at a
// time.
env::set_var("RUST_TEST_THREADS", "1");
}
_ => { /* proceed */ }
if let DebugInfoLldb = config.mode {
// Some older versions of LLDB seem to have problems with multiple
// instances running in parallel, so only run one test thread at a
// time.
env::set_var("RUST_TEST_THREADS", "1");
}

// FIXME(#33435) Avoid spurious failures in codegen-units/partitioning tests.
Expand Down Expand Up @@ -303,8 +302,7 @@ pub fn test_opts(config: &Config) -> test::TestOpts {
}

pub fn make_tests(config: &Config) -> Vec<test::TestDescAndFn> {
debug!("making tests from {:?}",
config.src_base.display());
debug!("making tests from {:?}", config.src_base.display());
let mut tests = Vec::new();
collect_tests_from_dir(config,
&config.src_base,
Expand Down
Loading