rust/src/fuzzer/fuzzer.rs

300 lines
8.6 KiB
Rust
Raw Normal View History

2011-07-08 02:16:46 -07:00
use std;
use rustc;
2011-05-20 21:06:05 -04:00
import std::fs;
import std::getopts;
import std::getopts::optopt;
import std::getopts::opt_present;
import std::getopts::opt_str;
import std::ioivec;
import std::ioivec::stdout;
2011-05-20 21:06:05 -04:00
import std::vec;
2011-07-08 02:16:46 -07:00
import std::ivec;
import std::str;
import std::uint;
import std::option;
2011-05-20 21:06:05 -04:00
2011-07-08 02:16:46 -07:00
import rustc::syntax::ast;
import rustc::syntax::fold;
import rustc::syntax::walk;
import rustc::syntax::codemap;
import rustc::syntax::parse::parser;
import rustc::syntax::print::pprust;
/*
// Imports for "the rest of driver::compile_input"
2011-07-08 02:16:46 -07:00
import driver = rustc::driver::rustc; // see https://github.com/graydon/rust/issues/624
import rustc::back::link;
import rustc::driver::rustc::time;
2011-07-08 02:16:46 -07:00
import rustc::driver::session;
2011-05-20 21:06:05 -04:00
import rustc::metadata::creader;
import rustc::metadata::cstore;
import rustc::syntax::parse::parser;
import rustc::syntax::parse::token;
import rustc::front;
import rustc::front::attr;
import rustc::middle;
import rustc::middle::trans;
import rustc::middle::resolve;
import rustc::middle::ty;
import rustc::middle::typeck;
import rustc::middle::tstate::ck;
import rustc::syntax::print::pp;
import rustc::util::ppaux;
import rustc::lib::llvm;
*/
2011-05-20 21:06:05 -04:00
2011-07-27 14:19:39 +02:00
fn read_whole_file(filename: &str) -> str {
str::unsafe_from_bytes_ivec(ioivec::file_reader(filename).read_whole_stream())
}
2011-07-27 14:19:39 +02:00
fn write_file(filename: &str, content: &str) {
ioivec::file_writer(filename,
~[ioivec::create,
ioivec::truncate]).write_str(content);
}
2011-07-27 14:19:39 +02:00
fn file_contains(filename: &str, needle: &str) -> bool {
let contents = read_whole_file(filename);
ret str::find(contents, needle) != -1;
}
2011-07-27 14:19:39 +02:00
fn contains(haystack: &str, needle: &str) -> bool {
str::find(haystack, needle) != -1
}
2011-07-27 14:19:39 +02:00
fn find_rust_files(files: &mutable str[], path: str) {
if str::ends_with(path, ".rs") {
if file_contains(path, "xfail-stage1") {
//log_err "Skipping " + path + " because it is marked as xfail-stage1";
2011-07-27 14:19:39 +02:00
} else { files += ~[path]; }
} else if (fs::file_is_dir(path) && str::find(path, "compile-fail") == -1)
{
for p: str in fs::list_dir(path) { find_rust_files(files, p); }
}
}
2011-07-27 14:19:39 +02:00
fn safe_to_steal(e: ast::expr_) -> bool {
alt e {
// pretty-printer precedence issues -- https://github.com/graydon/rust/issues/670
ast::expr_unary(_, _) {
false
}
ast::expr_lit(lit) {
alt lit.node {
ast::lit_str(_, _) { true }
ast::lit_char(_) { true }
ast::lit_int(_) { false }
ast::lit_uint(_) { false }
ast::lit_mach_int(_, _) { false }
ast::lit_float(_) { false }
ast::lit_mach_float(_, _) { false }
ast::lit_nil. { true }
ast::lit_bool(_) { true }
}
2011-07-27 14:19:39 +02:00
}
ast::expr_cast(_, _) { false }
ast::expr_send(_, _) { false }
ast::expr_recv(_, _) { false }
ast::expr_assert(_) { false }
ast::expr_binary(_, _, _) { false }
ast::expr_assign(_, _) { false }
ast::expr_assign_op(_, _, _) { false }
// https://github.com/graydon/rust/issues/676
ast::expr_ret(option::none.) {
false
}
ast::expr_put(option::none.) { false }
_ {
true
}
}
}
2011-07-27 14:19:39 +02:00
fn steal_exprs(crate: &ast::crate) -> ast::expr[] {
let exprs: @mutable ast::expr[] = @mutable ~[];
// "Stash" is not type-parameterized because of the need for safe_to_steal
2011-07-27 14:19:39 +02:00
fn stash_expr(es: @mutable ast::expr[], e: &@ast::expr) {
if safe_to_steal(e.node) {
*es += ~[*e];
} else {/* now my indices are wrong :( */ }
}
let v =
{visit_expr_pre: bind stash_expr(exprs, _)
with walk::default_visitor()};
walk::walk_crate(v, crate);
*exprs
}
// https://github.com/graydon/rust/issues/652
2011-07-27 14:19:39 +02:00
fn safe_to_replace(e: ast::expr_) -> bool {
alt e {
ast::expr_if(_, _, _) { false }
ast::expr_block(_) { false }
_ { true }
}
}
// Replace the |i|th expr (in fold order) of |crate| with |newexpr|.
2011-07-27 14:19:39 +02:00
fn replace_expr_in_crate(crate: &ast::crate, i: uint, newexpr: ast::expr_) ->
ast::crate {
let j: @mutable uint = @mutable 0u;
fn fold_expr_rep(j_: @mutable uint, i_: uint, newexpr_: &ast::expr_,
original: &ast::expr_, fld: fold::ast_fold) ->
ast::expr_ {
*j_ += 1u;
if i_ + 1u == *j_ && safe_to_replace(original) {
newexpr_
} else { fold::noop_fold_expr(original, fld) }
}
2011-07-27 14:19:39 +02:00
let afp =
{fold_expr: bind fold_expr_rep(j, i, newexpr, _, _)
with *fold::default_ast_fold()};
let af = fold::make_fold(afp);
let crate2: @ast::crate = @af.fold_crate(crate);
fold::dummy_out(af); // work around a leak (https://github.com/graydon/rust/issues/651)
*crate2
}
2011-07-27 14:19:39 +02:00
iter under(n: uint) -> uint {
let i: uint = 0u;
while i < n { put i; i += 1u; }
}
fn devnull() -> ioivec::writer { std::ioivec::string_writer().get_writer() }
2011-07-27 14:19:39 +02:00
fn as_str(f: fn(ioivec::writer) ) -> str {
let w = std::ioivec::string_writer();
2011-07-25 17:10:00 -07:00
f(w.get_writer());
ret w.get_str();
}
/*
fn pp_variants(&ast::crate crate, &codemap::codemap cmap, &str filename) {
auto exprs = steal_exprs(crate);
auto exprsL = ivec::len(exprs);
if (exprsL < 100u) {
for each (uint i in under(uint::min(exprsL, 20u))) {
log_err "Replacing... " + pprust::expr_to_str(@exprs.(i));
for each (uint j in under(uint::min(exprsL, 5u))) {
log_err "With... " + pprust::expr_to_str(@exprs.(j));
auto crate2 = @replace_expr_in_crate(crate, i, exprs.(j).node);
check_roundtrip(crate2, cmap, filename + ".4.rs");
}
2011-05-20 21:06:05 -04:00
}
}
2011-07-08 02:16:46 -07:00
}
*/
2011-05-20 21:06:05 -04:00
2011-07-27 14:19:39 +02:00
fn parse_and_print(code: &str) -> str {
let filename = "";
let codemap = codemap::new_codemap();
let crate =
parser::parse_crate_from_source_str(filename, code, ~[], codemap);
2011-07-25 17:10:00 -07:00
ret as_str(bind pprust::print_crate(codemap, crate, filename,
2011-07-27 14:19:39 +02:00
ioivec::string_reader(code), _,
pprust::no_ann()));
2011-07-25 17:10:00 -07:00
}
2011-07-27 14:19:39 +02:00
fn content_is_confusing(code: &str) -> bool {
let // https://github.com/graydon/rust/issues/671
// https://github.com/graydon/rust/issues/669
// https://github.com/graydon/rust/issues/669
// https://github.com/graydon/rust/issues/669
// crazy rules enforced by parser rather than typechecker?
// more precedence issues
// more precedence issues?
confusing_patterns =
["#macro", "][]", "][mutable]", "][mutable ]", "self", "spawn",
"bind"];
for p: str in confusing_patterns { if contains(code, p) { ret true; } }
2011-07-25 17:10:00 -07:00
ret false;
}
2011-07-27 14:19:39 +02:00
fn file_is_confusing(filename: &str) -> bool {
let
2011-07-25 17:10:00 -07:00
2011-07-27 14:19:39 +02:00
// https://github.com/graydon/rust/issues/674
2011-07-25 17:10:00 -07:00
2011-07-27 14:19:39 +02:00
// something to do with () as a lone pattern
2011-07-26 15:38:48 -07:00
2011-07-27 14:19:39 +02:00
// an issue where -2147483648 gains an
// extra negative sign each time through,
// which i can't reproduce using "rustc
// --pretty normal"???
confusing_files =
["block-expr-precedence.rs", "nil-pattern.rs",
"syntax-extension-fmt.rs"];
2011-07-25 17:10:00 -07:00
2011-07-27 14:19:39 +02:00
for f: str in confusing_files { if contains(filename, f) { ret true; } }
2011-07-25 17:10:00 -07:00
ret false;
}
2011-07-27 14:19:39 +02:00
fn check_roundtrip_convergence(code: &str) {
2011-07-25 17:10:00 -07:00
2011-07-27 14:19:39 +02:00
let i = 0;
let new = code;
let old = code;
2011-07-25 17:10:00 -07:00
2011-07-27 14:19:39 +02:00
while i < 10 {
2011-07-25 17:10:00 -07:00
old = new;
new = parse_and_print(old);
2011-07-27 14:19:39 +02:00
if content_is_confusing(new) { ret; }
2011-07-25 17:10:00 -07:00
i += 1;
2011-07-26 15:38:48 -07:00
log #fmt("cycle %d", i);
2011-07-25 17:10:00 -07:00
}
2011-07-27 14:19:39 +02:00
2011-07-25 17:10:00 -07:00
if old != new {
write_file("round-trip-a.rs", old);
write_file("round-trip-b.rs", new);
2011-07-27 14:19:39 +02:00
std::run::run_program("kdiff3",
["round-trip-a.rs", "round-trip-b.rs"]);
2011-07-25 17:10:00 -07:00
fail "Mismatch";
}
}
2011-07-27 14:19:39 +02:00
fn check_convergence(files: &str[]) {
2011-07-26 15:38:48 -07:00
log_err #fmt("pp convergence tests: %u files", ivec::len(files));
2011-07-27 14:19:39 +02:00
for file: str in files {
2011-07-26 15:38:48 -07:00
log_err #fmt("pp converge: %s", file);
2011-07-27 14:19:39 +02:00
if !file_is_confusing(file) {
let s = read_whole_file(file);
if !content_is_confusing(s) { check_roundtrip_convergence(s); }
}
2011-07-25 17:10:00 -07:00
//pprust::print_crate(cm, crate, file, devnull(), pprust::no_ann());
2011-07-12 12:07:03 -07:00
// Currently hits https://github.com/graydon/rust/issues/675
//pp_variants(*crate, cm, file);
2011-07-08 02:16:46 -07:00
}
2011-07-27 14:19:39 +02:00
}
2011-07-26 15:38:48 -07:00
2011-07-27 14:19:39 +02:00
fn main(args: vec[str]) {
if vec::len(args) != 2u {
2011-07-26 15:38:48 -07:00
log_err #fmt("usage: %s <testdir>", args.(0));
ret;
}
2011-07-27 14:19:39 +02:00
let files = ~[];
let root = args.(1);
2011-07-26 15:38:48 -07:00
find_rust_files(files, root);
check_convergence(files);
2011-05-20 21:06:05 -04:00
}
// Local Variables:
// mode: rust;
// fill-column: 78;
// indent-tabs-mode: nil
// c-basic-offset: 4
// buffer-file-coding-system: utf-8-unix
// compile-command: "make -k -C $RBUILD 2>&1 | sed -e 's/\\/x\\//x:\\//g'";
// End: