Keep source file around after parsing.
Specifically box the string (to avoid unnecessary copies) and store it in codemap::filemap. Remove the hack in driver::diagnostic that rereads the source from the file and instead just get the source from the filemap. (This commit is also a prerequisite for issue #1612)
This commit is contained in:
parent
746fa27988
commit
c5e03e0e59
@ -193,15 +193,6 @@ fn highlight_lines(cm: codemap::codemap, sp: span,
|
||||
// pull out the lines
|
||||
if lines.name == "-" { ret; }
|
||||
|
||||
// FIXME: reading in the entire file is the worst possible way to
|
||||
// get access to the necessary lines.
|
||||
let file = alt io::read_whole_file_str(lines.name) {
|
||||
result::ok(file) { file }
|
||||
result::err(e) {
|
||||
// Hard to report errors while reporting an error
|
||||
ret;
|
||||
}
|
||||
};
|
||||
let fm = codemap::get_filemap(cm, lines.name);
|
||||
|
||||
// arbitrarily only print up to six lines of the error
|
||||
@ -215,7 +206,7 @@ fn highlight_lines(cm: codemap::codemap, sp: span,
|
||||
// Print the offending lines
|
||||
for line: uint in display_lines {
|
||||
io::stdout().write_str(#fmt["%s:%u ", fm.name, line + 1u]);
|
||||
let s = codemap::get_line(fm, line as int, file);
|
||||
let s = codemap::get_line(fm, line as int);
|
||||
if !str::ends_with(s, "\n") { s += "\n"; }
|
||||
io::stdout().write_str(s);
|
||||
}
|
||||
|
@ -77,7 +77,7 @@ fn parse_cfgspecs(cfgspecs: [str]) -> ast::crate_cfg {
|
||||
fn input_is_stdin(filename: str) -> bool { filename == "-" }
|
||||
|
||||
fn parse_input(sess: session, cfg: ast::crate_cfg, input: str)
|
||||
-> {crate: @ast::crate, src: str} {
|
||||
-> {crate: @ast::crate, src: @str} {
|
||||
let src = get_input_str(sess, input);
|
||||
let crate = if !input_is_stdin(input) {
|
||||
parser::parse_crate_from_file(input, cfg, sess.parse_sess)
|
||||
@ -87,7 +87,7 @@ fn parse_input(sess: session, cfg: ast::crate_cfg, input: str)
|
||||
{crate: crate, src: src}
|
||||
}
|
||||
|
||||
fn get_input_str(sess: session, infile: str) -> str {
|
||||
fn get_input_str(sess: session, infile: str) -> @str {
|
||||
let stream = if !input_is_stdin(infile) {
|
||||
alt io::file_reader(infile) {
|
||||
result::ok(reader) { reader }
|
||||
@ -96,7 +96,7 @@ fn get_input_str(sess: session, infile: str) -> str {
|
||||
}
|
||||
}
|
||||
} else { io::stdin() };
|
||||
str::unsafe_from_bytes(stream.read_whole_stream())
|
||||
@str::unsafe_from_bytes(stream.read_whole_stream())
|
||||
}
|
||||
|
||||
fn time<T>(do_it: bool, what: str, thunk: fn@() -> T) -> T {
|
||||
@ -141,7 +141,7 @@ enum compile_upto {
|
||||
fn compile_upto(sess: session, cfg: ast::crate_cfg,
|
||||
input: str, upto: compile_upto,
|
||||
outputs: option::t<output_filenames>)
|
||||
-> {crate: @ast::crate, tcx: option::t<ty::ctxt>, src: str} {
|
||||
-> {crate: @ast::crate, tcx: option::t<ty::ctxt>, src: @str} {
|
||||
let time_passes = sess.opts.time_passes;
|
||||
let {crate, src} =
|
||||
time(time_passes, "parsing", bind parse_input(sess, cfg, input));
|
||||
@ -300,7 +300,7 @@ fn pretty_print_input(sess: session, cfg: ast::crate_cfg, input: str,
|
||||
ppm_expanded | ppm_normal {}
|
||||
}
|
||||
pprust::print_crate(sess.codemap, sess.span_diagnostic, crate, input,
|
||||
io::string_reader(src), io::stdout(), ann);
|
||||
io::string_reader(*src), io::stdout(), ann);
|
||||
}
|
||||
|
||||
fn get_os(triple: str) -> option<session::os> {
|
||||
|
@ -11,7 +11,8 @@ type file_pos = {ch: uint, byte: uint};
|
||||
* compiler.
|
||||
*/
|
||||
type filemap =
|
||||
@{name: filename, start_pos: file_pos, mutable lines: [file_pos]};
|
||||
@{name: filename, src: @str,
|
||||
start_pos: file_pos, mutable lines: [file_pos]};
|
||||
|
||||
type codemap = @{mutable files: [filemap]};
|
||||
|
||||
@ -19,9 +20,10 @@ type loc = {filename: filename, line: uint, col: uint};
|
||||
|
||||
fn new_codemap() -> codemap { ret @{mutable files: []}; }
|
||||
|
||||
fn new_filemap(filename: filename, start_pos_ch: uint, start_pos_byte: uint)
|
||||
fn new_filemap(filename: filename, src: @str,
|
||||
start_pos_ch: uint, start_pos_byte: uint)
|
||||
-> filemap {
|
||||
ret @{name: filename,
|
||||
ret @{name: filename, src: src,
|
||||
start_pos: {ch: start_pos_ch, byte: start_pos_byte},
|
||||
mutable lines: [{ch: start_pos_ch, byte: start_pos_byte}]};
|
||||
}
|
||||
@ -106,7 +108,7 @@ fn span_to_lines(sp: span, cm: codemap::codemap) -> @file_lines {
|
||||
ret @{name: lo.filename, lines: lines};
|
||||
}
|
||||
|
||||
fn get_line(fm: filemap, line: int, file: str) -> str {
|
||||
fn get_line(fm: filemap, line: int) -> str {
|
||||
let begin: uint = fm.lines[line].byte - fm.start_pos.byte;
|
||||
let end: uint;
|
||||
if line as uint < vec::len(fm.lines) - 1u {
|
||||
@ -115,12 +117,12 @@ fn get_line(fm: filemap, line: int, file: str) -> str {
|
||||
// If we're not done parsing the file, we're at the limit of what's
|
||||
// parsed. If we just slice the rest of the string, we'll print out
|
||||
// the remainder of the file, which is undesirable.
|
||||
end = str::byte_len(file);
|
||||
let rest = str::slice(file, begin, end);
|
||||
end = str::byte_len(*fm.src);
|
||||
let rest = str::slice(*fm.src, begin, end);
|
||||
let newline = str::index(rest, '\n' as u8);
|
||||
if newline != -1 { end = begin + (newline as uint); }
|
||||
}
|
||||
ret str::slice(file, begin, end);
|
||||
ret str::slice(*fm.src, begin, end);
|
||||
}
|
||||
|
||||
fn get_filemap(cm: codemap, filename: str) -> filemap {
|
||||
|
@ -75,7 +75,7 @@ fn expand_crate(sess: session::session, c: @crate) -> @crate {
|
||||
{fold_expr: bind expand_expr(exts, cx, _, _, _, afp.fold_expr)
|
||||
with *afp};
|
||||
let f = make_fold(f_pre);
|
||||
let cm = parse_expr_from_source_str("<anon>", core_macros(),
|
||||
let cm = parse_expr_from_source_str("<anon>", @core_macros(),
|
||||
sess.opts.cfg,
|
||||
sess.parse_sess);
|
||||
|
||||
|
@ -11,7 +11,7 @@ import driver::diagnostic;
|
||||
type reader = @{
|
||||
cm: codemap::codemap,
|
||||
span_diagnostic: diagnostic::span_handler,
|
||||
src: str,
|
||||
src: @str,
|
||||
len: uint,
|
||||
mutable col: uint,
|
||||
mutable pos: uint,
|
||||
@ -27,11 +27,11 @@ impl reader for reader {
|
||||
fn get_str_from(start: uint) -> str {
|
||||
// I'm pretty skeptical about this subtraction. What if there's a
|
||||
// multi-byte character before the mark?
|
||||
ret str::slice(self.src, start - 1u, self.pos - 1u);
|
||||
ret str::slice(*self.src, start - 1u, self.pos - 1u);
|
||||
}
|
||||
fn next() -> char {
|
||||
if self.pos < self.len {
|
||||
ret str::char_at(self.src, self.pos);
|
||||
ret str::char_at(*self.src, self.pos);
|
||||
} else { ret -1 as char; }
|
||||
}
|
||||
fn bump() {
|
||||
@ -43,7 +43,7 @@ impl reader for reader {
|
||||
self.filemap.start_pos.byte);
|
||||
self.col = 0u;
|
||||
}
|
||||
let next = str::char_range_at(self.src, self.pos);
|
||||
let next = str::char_range_at(*self.src, self.pos);
|
||||
self.pos = next.next;
|
||||
self.curr = next.ch;
|
||||
} else { self.curr = -1 as char; }
|
||||
@ -57,16 +57,16 @@ impl reader for reader {
|
||||
|
||||
fn new_reader(cm: codemap::codemap,
|
||||
span_diagnostic: diagnostic::span_handler,
|
||||
src: str, filemap: codemap::filemap,
|
||||
filemap: codemap::filemap,
|
||||
itr: @interner::interner<str>) -> reader {
|
||||
let r = @{cm: cm,
|
||||
span_diagnostic: span_diagnostic,
|
||||
src: src, len: str::byte_len(src),
|
||||
src: filemap.src, len: str::byte_len(*filemap.src),
|
||||
mutable col: 0u, mutable pos: 0u, mutable curr: -1 as char,
|
||||
mutable chpos: filemap.start_pos.ch, mutable strs: [],
|
||||
filemap: filemap, interner: itr};
|
||||
if r.pos < r.len {
|
||||
let next = str::char_range_at(r.src, r.pos);
|
||||
let next = str::char_range_at(*r.src, r.pos);
|
||||
r.pos = next.next;
|
||||
r.curr = next.ch;
|
||||
}
|
||||
@ -672,10 +672,10 @@ fn gather_comments_and_literals(cm: codemap::codemap,
|
||||
path: str,
|
||||
srdr: io::reader) ->
|
||||
{cmnts: [cmnt], lits: [lit]} {
|
||||
let src = str::unsafe_from_bytes(srdr.read_whole_stream());
|
||||
let src = @str::unsafe_from_bytes(srdr.read_whole_stream());
|
||||
let itr = @interner::mk::<str>(str::hash, str::eq);
|
||||
let rdr = new_reader(cm, span_diagnostic, src,
|
||||
codemap::new_filemap(path, 0u, 0u), itr);
|
||||
let rdr = new_reader(cm, span_diagnostic,
|
||||
codemap::new_filemap(path, src, 0u, 0u), itr);
|
||||
let comments: [cmnt] = [];
|
||||
let literals: [lit] = [];
|
||||
let first_read: bool = true;
|
||||
|
@ -98,27 +98,28 @@ fn new_parser_from_file(sess: parse_sess, cfg: ast::crate_cfg, path: str,
|
||||
let src = alt io::read_whole_file_str(path) {
|
||||
result::ok(src) {
|
||||
// FIXME: This copy is unfortunate
|
||||
src
|
||||
@src
|
||||
}
|
||||
result::err(e) {
|
||||
sess.span_diagnostic.handler().fatal(e)
|
||||
}
|
||||
};
|
||||
let filemap = codemap::new_filemap(path, sess.chpos, sess.byte_pos);
|
||||
let filemap = codemap::new_filemap(path, src,
|
||||
sess.chpos, sess.byte_pos);
|
||||
sess.cm.files += [filemap];
|
||||
let itr = @interner::mk(str::hash, str::eq);
|
||||
let rdr = lexer::new_reader(sess.cm, sess.span_diagnostic, src, filemap,
|
||||
itr);
|
||||
let rdr = lexer::new_reader(sess.cm, sess.span_diagnostic, filemap, itr);
|
||||
ret new_parser(sess, cfg, rdr, ftype);
|
||||
}
|
||||
|
||||
fn new_parser_from_source_str(sess: parse_sess, cfg: ast::crate_cfg,
|
||||
name: str, source: str) -> parser {
|
||||
name: str, source: @str) -> parser {
|
||||
let ftype = SOURCE_FILE;
|
||||
let filemap = codemap::new_filemap(name, sess.chpos, sess.byte_pos);
|
||||
let filemap = codemap::new_filemap(name, source,
|
||||
sess.chpos, sess.byte_pos);
|
||||
sess.cm.files += [filemap];
|
||||
let itr = @interner::mk(str::hash, str::eq);
|
||||
let rdr = lexer::new_reader(sess.cm, sess.span_diagnostic, source,
|
||||
let rdr = lexer::new_reader(sess.cm, sess.span_diagnostic,
|
||||
filemap, itr);
|
||||
ret new_parser(sess, cfg, rdr, ftype);
|
||||
}
|
||||
@ -2462,7 +2463,7 @@ fn parse_crate_from_source_file(input: str, cfg: ast::crate_cfg,
|
||||
}
|
||||
|
||||
|
||||
fn parse_expr_from_source_str(name: str, source: str, cfg: ast::crate_cfg,
|
||||
fn parse_expr_from_source_str(name: str, source: @str, cfg: ast::crate_cfg,
|
||||
sess: parse_sess) -> @ast::expr {
|
||||
let p = new_parser_from_source_str(sess, cfg, name, source);
|
||||
let r = parse_expr(p);
|
||||
@ -2471,7 +2472,7 @@ fn parse_expr_from_source_str(name: str, source: str, cfg: ast::crate_cfg,
|
||||
ret r;
|
||||
}
|
||||
|
||||
fn parse_crate_from_source_str(name: str, source: str, cfg: ast::crate_cfg,
|
||||
fn parse_crate_from_source_str(name: str, source: @str, cfg: ast::crate_cfg,
|
||||
sess: parse_sess) -> @ast::crate {
|
||||
let p = new_parser_from_source_str(sess, cfg, name, source);
|
||||
let r = parse_crate_mod(p, cfg);
|
||||
|
@ -261,7 +261,7 @@ fn check_variants_T<T: copy>(
|
||||
// string for stability is easier and ok for now.
|
||||
let handler = diagnostic::mk_handler(none);
|
||||
let str3 =
|
||||
as_str(bind pprust::print_crate(
|
||||
@as_str(bind pprust::print_crate(
|
||||
codemap,
|
||||
diagnostic::mk_span_handler(handler, codemap),
|
||||
crate2,
|
||||
@ -274,8 +274,8 @@ fn check_variants_T<T: copy>(
|
||||
}
|
||||
tm_run {
|
||||
let file_label = #fmt("rusttmp/%s_%s_%u_%u", last_part(filename), thing_label, i, j);
|
||||
let safe_to_run = !(content_is_dangerous_to_run(str3) || has_raw_pointers(*crate2));
|
||||
check_whole_compiler(str3, file_label, safe_to_run);
|
||||
let safe_to_run = !(content_is_dangerous_to_run(*str3) || has_raw_pointers(*crate2));
|
||||
check_whole_compiler(*str3, file_label, safe_to_run);
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -414,7 +414,7 @@ fn check_compiling(filename: str) -> happiness {
|
||||
}
|
||||
|
||||
|
||||
fn parse_and_print(code: str) -> str {
|
||||
fn parse_and_print(code: @str) -> str {
|
||||
let filename = "tmp.rs";
|
||||
let cm = codemap::new_codemap();
|
||||
let handler = diagnostic::mk_handler(none);
|
||||
@ -425,14 +425,14 @@ fn parse_and_print(code: str) -> str {
|
||||
mutable chpos: 0u,
|
||||
mutable byte_pos: 0u
|
||||
};
|
||||
write_file(filename, code);
|
||||
write_file(filename, *code);
|
||||
let crate = parser::parse_crate_from_source_str(
|
||||
filename, code, [], sess);
|
||||
ret as_str(bind pprust::print_crate(sess.cm,
|
||||
sess.span_diagnostic,
|
||||
crate,
|
||||
filename,
|
||||
io::string_reader(code), _,
|
||||
io::string_reader(*code), _,
|
||||
pprust::no_ann()));
|
||||
}
|
||||
|
||||
@ -506,7 +506,7 @@ fn file_might_not_converge(filename: str) -> bool {
|
||||
ret false;
|
||||
}
|
||||
|
||||
fn check_roundtrip_convergence(code: str, maxIters: uint) {
|
||||
fn check_roundtrip_convergence(code: @str, maxIters: uint) {
|
||||
|
||||
let i = 0u;
|
||||
let new = code;
|
||||
@ -514,8 +514,8 @@ fn check_roundtrip_convergence(code: str, maxIters: uint) {
|
||||
|
||||
while i < maxIters {
|
||||
old = new;
|
||||
if content_might_not_converge(old) { ret; }
|
||||
new = parse_and_print(old);
|
||||
if content_might_not_converge(*old) { ret; }
|
||||
new = @parse_and_print(old);
|
||||
if old == new { break; }
|
||||
i += 1u;
|
||||
}
|
||||
@ -524,8 +524,8 @@ fn check_roundtrip_convergence(code: str, maxIters: uint) {
|
||||
#error("Converged after %u iterations", i);
|
||||
} else {
|
||||
#error("Did not converge after %u iterations!", i);
|
||||
write_file("round-trip-a.rs", old);
|
||||
write_file("round-trip-b.rs", new);
|
||||
write_file("round-trip-a.rs", *old);
|
||||
write_file("round-trip-b.rs", *new);
|
||||
std::run::run_program("diff",
|
||||
["-w", "-u", "round-trip-a.rs",
|
||||
"round-trip-b.rs"]);
|
||||
@ -537,8 +537,8 @@ fn check_convergence(files: [str]) {
|
||||
#error("pp convergence tests: %u files", vec::len(files));
|
||||
for file in files {
|
||||
if !file_might_not_converge(file) {
|
||||
let s = result::get(io::read_whole_file_str(file));
|
||||
if !content_might_not_converge(s) {
|
||||
let s = @result::get(io::read_whole_file_str(file));
|
||||
if !content_might_not_converge(*s) {
|
||||
#error("pp converge: %s", file);
|
||||
// Change from 7u to 2u once https://github.com/graydon/rust/issues/850 is fixed
|
||||
check_roundtrip_convergence(s, 7u);
|
||||
@ -554,14 +554,14 @@ fn check_variants(files: [str], cx: context) {
|
||||
cont;
|
||||
}
|
||||
|
||||
let s = result::get(io::read_whole_file_str(file));
|
||||
if contains(s, "#") {
|
||||
let s = @result::get(io::read_whole_file_str(file));
|
||||
if contains(*s, "#") {
|
||||
cont; // Macros are confusing
|
||||
}
|
||||
if cx.mode == tm_converge && content_might_not_converge(s) {
|
||||
if cx.mode == tm_converge && content_might_not_converge(*s) {
|
||||
cont;
|
||||
}
|
||||
if cx.mode == tm_run && content_is_dangerous_to_compile(s) {
|
||||
if cx.mode == tm_run && content_is_dangerous_to_compile(*s) {
|
||||
cont;
|
||||
}
|
||||
|
||||
@ -584,7 +584,7 @@ fn check_variants(files: [str], cx: context) {
|
||||
sess.span_diagnostic,
|
||||
crate,
|
||||
file,
|
||||
io::string_reader(s), _,
|
||||
io::string_reader(*s), _,
|
||||
pprust::no_ann())));
|
||||
check_variants_of_ast(*crate, sess.cm, file, cx);
|
||||
}
|
||||
|
@ -352,7 +352,7 @@ mod test {
|
||||
mutable byte_pos: 0u
|
||||
};
|
||||
let parser = parser::new_parser_from_source_str(
|
||||
parse_sess, [], "-", source);
|
||||
parse_sess, [], "-", @source);
|
||||
|
||||
parser::parse_outer_attributes(parser)
|
||||
}
|
||||
|
@ -27,5 +27,5 @@ fn from_file(file: str) -> @ast::crate {
|
||||
|
||||
fn from_str(source: str) -> @ast::crate {
|
||||
parser::parse_crate_from_source_str(
|
||||
"-", source, [], new_parse_sess())
|
||||
"-", @source, [], new_parse_sess())
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user