1
Fork 0

Don't reset the chpos/byte_pos to 0 in new_parser_from_source_str.

This correctly fixes issue #1362.

chpos/byte_pos are now the offsets within a particular file, but
rather the offsets within a virtual file with is formed by combing all
of the modules within a crate.  Thus, resetting them to 0 causes an
overlap and hence, bogus source locations.

Fix #1362 by moving chpos/byte_pos to parse_sess so that
new_parser_from_source_str has access to them and hence can chose an
initial value that is not already been used in the crate.

Note that the trigger for bug 1361 was that syntax/ext/expand.rs calls
parse_expr_from_source_str (which calls new_parser_from_source_str)
using the same codemap as the current crate (and hence causing overlap
with files in the crate as new_parser_from_source_str resets the
chpos/byte_pos to 0).
This commit is contained in:
Kevin Atkinson 2012-01-22 17:24:55 -07:00 committed by Brian Anderson
parent 355edf13e7
commit ad21d9c64a
7 changed files with 47 additions and 26 deletions

View file

@ -105,7 +105,9 @@ fn load_pkg(filename: str) -> option::t<pkg> {
let sess = @{
cm: cm,
mutable next_id: 1,
diagnostic: diagnostic::mk_handler(cm, none)
diagnostic: diagnostic::mk_handler(cm, none),
mutable chpos: 0u,
mutable byte_pos: 0u
};
let c = parser::parse_crate_from_crate_file(filename, [], sess);

View file

@ -488,7 +488,9 @@ fn build_session(sopts: @session::options, input: str,
parse_sess: @{
cm: codemap,
mutable next_id: 1,
diagnostic: diagnostic_handler
diagnostic: diagnostic_handler,
mutable chpos: 0u,
mutable byte_pos: 0u
},
codemap: codemap,
// For a library crate, this is always none

View file

@ -14,8 +14,6 @@ export eval_crate_directives_to_mod;
type ctx =
@{p: parser,
sess: parser::parse_sess,
mutable chpos: uint,
mutable byte_pos: uint,
cfg: ast::crate_cfg};
fn eval_crate_directives(cx: ctx, cdirs: [@ast::crate_directive], prefix: str,
@ -76,12 +74,12 @@ fn parse_companion_mod(cx: ctx, prefix: str, suffix: option::t<str>)
if file_exists(modpath) {
#debug("found companion mod");
let p0 = new_parser_from_file(cx.sess, cx.cfg, modpath,
cx.chpos, cx.byte_pos, SOURCE_FILE);
SOURCE_FILE);
let inner_attrs = parse_inner_attrs_and_next(p0);
let first_item_outer_attrs = inner_attrs.next;
let m0 = parse_mod_items(p0, token::EOF, first_item_outer_attrs);
cx.chpos = p0.reader.chpos;
cx.byte_pos = p0.reader.pos;
cx.sess.chpos = p0.reader.chpos;
cx.sess.byte_pos = p0.reader.pos;
ret (m0.view_items, m0.items, inner_attrs.inner);
} else {
ret ([], [], []);
@ -108,8 +106,7 @@ fn eval_crate_directive(cx: ctx, cdir: @ast::crate_directive, prefix: str,
file_path
} else { prefix + std::fs::path_sep() + file_path };
let p0 =
new_parser_from_file(cx.sess, cx.cfg, full_path, cx.chpos,
cx.byte_pos, SOURCE_FILE);
new_parser_from_file(cx.sess, cx.cfg, full_path, SOURCE_FILE);
let inner_attrs = parse_inner_attrs_and_next(p0);
let mod_attrs = attrs + inner_attrs.inner;
let first_item_outer_attrs = inner_attrs.next;
@ -119,8 +116,8 @@ fn eval_crate_directive(cx: ctx, cdir: @ast::crate_directive, prefix: str,
syntax::parse::parser::mk_item(p0, cdir.span.lo, cdir.span.hi, id,
ast::item_mod(m0), mod_attrs);
// Thread defids, chpos and byte_pos through the parsers
cx.chpos = p0.reader.chpos;
cx.byte_pos = p0.reader.pos;
cx.sess.chpos = p0.reader.chpos;
cx.sess.byte_pos = p0.reader.pos;
items += [i];
}
ast::cdir_dir_mod(id, cdirs, attrs) {

View file

@ -25,7 +25,10 @@ enum file_type { CRATE_FILE, SOURCE_FILE, }
type parse_sess = @{
cm: codemap::codemap,
mutable next_id: node_id,
diagnostic: diagnostic::handler
diagnostic: diagnostic::handler,
// these two must be kept up to date
mutable chpos: uint,
mutable byte_pos: uint
};
fn next_node_id(sess: parse_sess) -> node_id {
@ -91,7 +94,7 @@ impl parser for parser {
}
fn new_parser_from_file(sess: parse_sess, cfg: ast::crate_cfg, path: str,
chpos: uint, byte_pos: uint, ftype: file_type) ->
ftype: file_type) ->
parser {
let src = alt io::read_whole_file_str(path) {
result::ok(src) {
@ -102,7 +105,7 @@ fn new_parser_from_file(sess: parse_sess, cfg: ast::crate_cfg, path: str,
sess.diagnostic.fatal(e)
}
};
let filemap = codemap::new_filemap(path, chpos, byte_pos);
let filemap = codemap::new_filemap(path, 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.diagnostic,
@ -113,7 +116,7 @@ fn new_parser_from_file(sess: parse_sess, cfg: ast::crate_cfg, path: str,
fn new_parser_from_source_str(sess: parse_sess, cfg: ast::crate_cfg,
name: str, source: str) -> parser {
let ftype = SOURCE_FILE;
let filemap = codemap::new_filemap(name, 0u, 0u);
let filemap = codemap::new_filemap(name, 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.diagnostic,
@ -2482,21 +2485,30 @@ fn parse_native_view(p: parser) -> [@ast::view_item] {
fn parse_crate_from_source_file(input: str, cfg: ast::crate_cfg,
sess: parse_sess) -> @ast::crate {
let p = new_parser_from_file(sess, cfg, input, 0u, 0u, SOURCE_FILE);
ret parse_crate_mod(p, cfg);
let p = new_parser_from_file(sess, cfg, input, SOURCE_FILE);
let r = parse_crate_mod(p, cfg);
sess.chpos = p.reader.chpos;
sess.byte_pos = p.reader.pos;
ret r;
}
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);
ret parse_expr(p);
let r = parse_expr(p);
sess.chpos = p.reader.chpos;
sess.byte_pos = p.reader.pos;
ret r;
}
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);
ret parse_crate_mod(p, cfg);
let r = parse_crate_mod(p, cfg);
sess.chpos = p.reader.chpos;
sess.byte_pos = p.reader.pos;
ret r;
}
// Parses a source module as a crate
@ -2589,18 +2601,18 @@ fn parse_crate_directives(p: parser, term: token::token,
fn parse_crate_from_crate_file(input: str, cfg: ast::crate_cfg,
sess: parse_sess) -> @ast::crate {
let p = new_parser_from_file(sess, cfg, input, 0u, 0u, CRATE_FILE);
let p = new_parser_from_file(sess, cfg, input, CRATE_FILE);
let lo = p.span.lo;
let prefix = std::fs::dirname(p.reader.filemap.name);
let leading_attrs = parse_inner_attrs_and_next(p);
let crate_attrs = leading_attrs.inner;
let first_cdir_attr = leading_attrs.next;
let cdirs = parse_crate_directives(p, token::EOF, first_cdir_attr);
sess.chpos = p.reader.chpos;
sess.byte_pos = p.reader.pos;
let cx =
@{p: p,
sess: sess,
mutable chpos: p.reader.chpos,
mutable byte_pos: p.reader.pos,
cfg: p.cfg};
let (companionmod, _) = fs::splitext(fs::basename(input));
let (m, attrs) = eval::eval_crate_directives_to_mod(

View file

@ -419,7 +419,9 @@ fn parse_and_print(code: str) -> str {
let sess = @{
cm: cm,
mutable next_id: 0,
diagnostic: diagnostic::mk_handler(cm, none)
diagnostic: diagnostic::mk_handler(cm, none),
mutable chpos: 0u,
mutable byte_pos: 0u
};
write_file(filename, code);
let crate = parser::parse_crate_from_source_str(
@ -566,7 +568,9 @@ fn check_variants(files: [str], cx: context) {
let sess = @{
cm: cm,
mutable next_id: 0,
diagnostic: diagnostic::mk_handler(cm, none)
diagnostic: diagnostic::mk_handler(cm, none),
mutable chpos: 0u,
mutable byte_pos: 0u
};
let crate =
parser::parse_crate_from_source_str(

View file

@ -289,7 +289,9 @@ mod test {
let parse_sess = @{
cm: cm,
mutable next_id: 0,
diagnostic: diagnostic::mk_handler(cm, none)
diagnostic: diagnostic::mk_handler(cm, none),
mutable chpos: 0u,
mutable byte_pos: 0u
};
let parser = parser::new_parser_from_source_str(
parse_sess, [], "-", source);

View file

@ -12,7 +12,9 @@ fn new_parse_sess() -> parser::parse_sess {
let sess = @{
cm: cm,
mutable next_id: 1,
diagnostic: diagnostic::mk_handler(cm, none)
diagnostic: diagnostic::mk_handler(cm, none),
mutable chpos: 0u,
mutable byte_pos: 0u
};
ret sess;
}