Skip to content

Commit

Permalink
syntax: replace sess.span_diagnostic.cm with sess.codemap().
Browse files Browse the repository at this point in the history
  • Loading branch information
eddyb committed May 13, 2015
1 parent f786437 commit 6a59d18
Show file tree
Hide file tree
Showing 8 changed files with 18 additions and 28 deletions.
6 changes: 2 additions & 4 deletions src/grammar/verify.rs
Expand Up @@ -287,11 +287,9 @@ fn main() {
let options = config::basic_options();
let session = session::build_session(options, None,
syntax::diagnostics::registry::Registry::new(&[]));
let filemap = parse::string_to_filemap(&session.parse_sess,
code,
String::from_str("<n/a>"));
let filemap = session.parse_sess.codemap().new_filemap(String::from_str("<n/a>"), code);
let mut lexer = lexer::StringReader::new(session.diagnostic(), filemap);
let ref cm = lexer.span_diagnostic.cm;
let cm = session.codemap();

// ANTLR
let mut token_file = File::open(&Path::new(&args.next().unwrap())).unwrap();
Expand Down
2 changes: 1 addition & 1 deletion src/librustc/session/mod.rs
Expand Up @@ -231,7 +231,7 @@ impl Session {
&self.parse_sess.span_diagnostic
}
pub fn codemap<'a>(&'a self) -> &'a codemap::CodeMap {
&self.parse_sess.span_diagnostic.cm
self.parse_sess.codemap()
}
// This exists to help with refactoring to eliminate impossible
// cases later on
Expand Down
8 changes: 3 additions & 5 deletions src/librustdoc/html/highlight.rs
Expand Up @@ -25,9 +25,7 @@ use syntax::parse;
pub fn highlight(src: &str, class: Option<&str>, id: Option<&str>) -> String {
debug!("highlighting: ================\n{}\n==============", src);
let sess = parse::ParseSess::new();
let fm = parse::string_to_filemap(&sess,
src.to_string(),
"<stdin>".to_string());
let fm = sess.codemap().new_filemap("<stdin>".to_string(), src.to_string());

let mut out = Vec::new();
doit(&sess,
Expand Down Expand Up @@ -62,7 +60,7 @@ fn doit(sess: &parse::ParseSess, mut lexer: lexer::StringReader,
loop {
let next = lexer.next_token();

let snip = |sp| sess.span_diagnostic.cm.span_to_snippet(sp).unwrap();
let snip = |sp| sess.codemap().span_to_snippet(sp).unwrap();

if next.tok == token::Eof { break }

Expand Down Expand Up @@ -178,7 +176,7 @@ fn doit(sess: &parse::ParseSess, mut lexer: lexer::StringReader,

// as mentioned above, use the original source code instead of
// stringifying this token
let snip = sess.span_diagnostic.cm.span_to_snippet(next.sp).unwrap();
let snip = sess.codemap().span_to_snippet(next.sp).unwrap();
if klass == "" {
try!(write!(out, "{}", Escape(&snip)));
} else {
Expand Down
2 changes: 1 addition & 1 deletion src/libsyntax/ext/base.rs
Expand Up @@ -648,7 +648,7 @@ impl<'a> ExtCtxt<'a> {
parse::tts_to_parser(self.parse_sess, tts.to_vec(), self.cfg())
}

pub fn codemap(&self) -> &'a CodeMap { &self.parse_sess.span_diagnostic.cm }
pub fn codemap(&self) -> &'a CodeMap { self.parse_sess.codemap() }
pub fn parse_sess(&self) -> &'a parse::ParseSess { self.parse_sess }
pub fn cfg(&self) -> ast::CrateConfig { self.cfg.clone() }
pub fn call_site(&self) -> Span {
Expand Down
18 changes: 7 additions & 11 deletions src/libsyntax/parse/mod.rs
Expand Up @@ -58,6 +58,10 @@ impl ParseSess {
included_mod_stack: RefCell::new(vec![])
}
}

pub fn codemap(&self) -> &CodeMap {
&self.span_diagnostic.cm
}
}

// a bunch of utility functions of the form parse_<thing>_from_<source>
Expand Down Expand Up @@ -170,7 +174,7 @@ pub fn new_parser_from_source_str<'a>(sess: &'a ParseSess,
name: String,
source: String)
-> Parser<'a> {
filemap_to_parser(sess, string_to_filemap(sess, source, name), cfg)
filemap_to_parser(sess, sess.codemap().new_filemap(name, source), cfg)
}

/// Create a new parser, handling errors as appropriate
Expand Down Expand Up @@ -234,8 +238,7 @@ pub fn file_to_filemap(sess: &ParseSess, path: &Path, spanopt: Option<Span>)
};
match str::from_utf8(&bytes[..]).ok() {
Some(s) => {
string_to_filemap(sess, s.to_string(),
path.to_str().unwrap().to_string())
sess.codemap().new_filemap(path.to_str().unwrap().to_string(), s.to_string())
}
None => {
err(&format!("{:?} is not UTF-8 encoded", path.display()));
Expand All @@ -244,13 +247,6 @@ pub fn file_to_filemap(sess: &ParseSess, path: &Path, spanopt: Option<Span>)
}
}

/// Given a session and a string, add the string to
/// the session's codemap and return the new filemap
pub fn string_to_filemap(sess: &ParseSess, source: String, path: String)
-> Rc<FileMap> {
sess.span_diagnostic.cm.new_filemap(path, source)
}

/// Given a filemap, produce a sequence of token-trees
pub fn filemap_to_tts(sess: &ParseSess, filemap: Rc<FileMap>)
-> Vec<ast::TokenTree> {
Expand Down Expand Up @@ -1104,7 +1100,7 @@ mod tests {

let span = tts.iter().rev().next().unwrap().get_span();

match sess.span_diagnostic.cm.span_to_snippet(span) {
match sess.codemap().span_to_snippet(span) {
Ok(s) => assert_eq!(&s[..], "{ body }"),
Err(_) => panic!("could not get snippet"),
}
Expand Down
3 changes: 1 addition & 2 deletions src/libsyntax/parse/parser.rs
Expand Up @@ -4835,8 +4835,7 @@ impl<'a> Parser<'a> {
outer_attrs: &[ast::Attribute],
id_sp: Span)
-> PResult<(ast::Item_, Vec<ast::Attribute> )> {
let mut prefix = PathBuf::from(&self.sess.span_diagnostic.cm
.span_to_filename(self.span));
let mut prefix = PathBuf::from(&self.sess.codemap().span_to_filename(self.span));
prefix.pop();
let mut dir_path = prefix;
for part in &self.mod_path_stack {
Expand Down
2 changes: 1 addition & 1 deletion src/libsyntax/test.rs
Expand Up @@ -301,7 +301,7 @@ fn ignored_span(cx: &TestCtxt, sp: Span) -> Span {
allow_internal_unstable: true,
}
};
let expn_id = cx.sess.span_diagnostic.cm.record_expansion(info);
let expn_id = cx.sess.codemap().record_expansion(info);
let mut sp = sp;
sp.expn_id = expn_id;
return sp;
Expand Down
5 changes: 2 additions & 3 deletions src/libsyntax/util/parser_testing.rs
Expand Up @@ -9,7 +9,7 @@
// except according to those terms.

use ast;
use parse::{ParseSess,string_to_filemap,filemap_to_tts};
use parse::{ParseSess,filemap_to_tts};
use parse::new_parser_from_source_str;
use parse::parser::Parser;
use parse::token;
Expand All @@ -19,8 +19,7 @@ use str::char_at;
/// Map a string to tts, using a made-up filename:
pub fn string_to_tts(source_str: String) -> Vec<ast::TokenTree> {
let ps = ParseSess::new();
filemap_to_tts(&ps,
string_to_filemap(&ps, source_str, "bogofile".to_string()))
filemap_to_tts(&ps, ps.codemap().new_filemap("bogofile".to_string(), source_str))
}

/// Map string to parser (via tts)
Expand Down

0 comments on commit 6a59d18

Please sign in to comment.