Skip to content
This repository was archived by the owner on May 6, 2020. It is now read-only.
Merged
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
5 changes: 2 additions & 3 deletions syntex/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ impl Registry {

let features = feature_gate::get_features(
&sess.span_diagnostic,
&krate);
&krate.attrs);

let krate = self.pre_expansion_passes.iter()
.fold(krate, |krate, f| (f)(krate));
Expand All @@ -205,9 +205,8 @@ impl Registry {
ecfg.features = Some(&features);

let cfg = Vec::new();
let mut gated_cfgs = Vec::new();
let mut macro_loader = SyntexMacroLoader::new(self.macros.clone());
let ecx = ExtCtxt::new(&sess, cfg, ecfg, &mut gated_cfgs, &mut macro_loader);
let ecx = ExtCtxt::new(&sess, cfg, ecfg, &mut macro_loader);

let (krate, _) = expand::expand_crate(ecx, self.syntax_exts, krate);

Expand Down
4 changes: 0 additions & 4 deletions syntex_syntax/src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,6 @@ impl Name {
pub fn as_str(self) -> token::InternedString {
token::InternedString::new_from_name(self)
}

pub fn unhygienize(self) -> Name {
token::intern(&self.as_str())
}
}

impl fmt::Debug for Name {
Expand Down
33 changes: 13 additions & 20 deletions syntex_syntax/src/attr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,11 @@ use ast::{Stmt, StmtKind, DeclKind};
use ast::{Expr, Item, Local, Decl};
use codemap::{Span, Spanned, spanned, dummy_spanned};
use codemap::BytePos;
use config::CfgDiag;
use errors::Handler;
use feature_gate::{GatedCfg, GatedCfgAttr};
use feature_gate::{Features, GatedCfg};
use parse::lexer::comments::{doc_comment_style, strip_doc_comment_decoration};
use parse::token::InternedString;
use parse::token;
use parse::{ParseSess, token};
use ptr::P;

use std::cell::{RefCell, Cell};
Expand Down Expand Up @@ -365,35 +364,29 @@ pub fn requests_inline(attrs: &[Attribute]) -> bool {
}

/// Tests if a cfg-pattern matches the cfg set
pub fn cfg_matches<T: CfgDiag>(cfgs: &[P<MetaItem>],
cfg: &ast::MetaItem,
diag: &mut T) -> bool {
pub fn cfg_matches(cfgs: &[P<MetaItem>], cfg: &ast::MetaItem,
sess: &ParseSess, features: Option<&Features>)
-> bool {
match cfg.node {
ast::MetaItemKind::List(ref pred, ref mis) if &pred[..] == "any" =>
mis.iter().any(|mi| cfg_matches(cfgs, &mi, diag)),
mis.iter().any(|mi| cfg_matches(cfgs, &mi, sess, features)),
ast::MetaItemKind::List(ref pred, ref mis) if &pred[..] == "all" =>
mis.iter().all(|mi| cfg_matches(cfgs, &mi, diag)),
mis.iter().all(|mi| cfg_matches(cfgs, &mi, sess, features)),
ast::MetaItemKind::List(ref pred, ref mis) if &pred[..] == "not" => {
if mis.len() != 1 {
diag.emit_error(|diagnostic| {
diagnostic.span_err(cfg.span, "expected 1 cfg-pattern");
});
sess.span_diagnostic.span_err(cfg.span, "expected 1 cfg-pattern");
return false;
}
!cfg_matches(cfgs, &mis[0], diag)
!cfg_matches(cfgs, &mis[0], sess, features)
}
ast::MetaItemKind::List(ref pred, _) => {
diag.emit_error(|diagnostic| {
diagnostic.span_err(cfg.span,
&format!("invalid predicate `{}`", pred));
});
sess.span_diagnostic.span_err(cfg.span, &format!("invalid predicate `{}`", pred));
false
},
ast::MetaItemKind::Word(_) | ast::MetaItemKind::NameValue(..) => {
diag.flag_gated(|feature_gated_cfgs| {
feature_gated_cfgs.extend(
GatedCfg::gate(cfg).map(GatedCfgAttr::GatedCfg));
});
if let (Some(features), Some(gated_cfg)) = (features, GatedCfg::gate(cfg)) {
gated_cfg.check_and_emit(sess, features);
}
contains(cfgs, cfg)
}
}
Expand Down
67 changes: 51 additions & 16 deletions syntex_syntax/src/codemap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,11 @@ pub use self::ExpnFormat::*;

use std::cell::{Cell, RefCell};
use std::ops::{Add, Sub};
use std::path::Path;
use std::path::{Path,PathBuf};
use std::rc::Rc;
use std::cmp;

use std::env;
use std::{fmt, fs};
use std::io::{self, Read};

Expand Down Expand Up @@ -508,6 +509,8 @@ pub struct FileMap {
/// originate from files has names between angle brackets by convention,
/// e.g. `<anon>`
pub name: FileName,
/// The absolute path of the file that the source came from.
pub abs_path: Option<FileName>,
/// The complete source code
pub src: Option<Rc<String>>,
/// The start position of this source in the CodeMap
Expand All @@ -522,11 +525,12 @@ pub struct FileMap {

impl Encodable for FileMap {
fn encode<S: Encoder>(&self, s: &mut S) -> Result<(), S::Error> {
s.emit_struct("FileMap", 5, |s| {
s.emit_struct("FileMap", 6, |s| {
try!(s.emit_struct_field("name", 0, |s| self.name.encode(s)));
try!(s.emit_struct_field("start_pos", 1, |s| self.start_pos.encode(s)));
try!(s.emit_struct_field("end_pos", 2, |s| self.end_pos.encode(s)));
try!(s.emit_struct_field("lines", 3, |s| {
try!(s.emit_struct_field("abs_path", 1, |s| self.abs_path.encode(s)));
try!(s.emit_struct_field("start_pos", 2, |s| self.start_pos.encode(s)));
try!(s.emit_struct_field("end_pos", 3, |s| self.end_pos.encode(s)));
try!(s.emit_struct_field("lines", 4, |s| {
let lines = self.lines.borrow();
// store the length
try!(s.emit_u32(lines.len() as u32));
Expand Down Expand Up @@ -572,7 +576,7 @@ impl Encodable for FileMap {

Ok(())
}));
s.emit_struct_field("multibyte_chars", 4, |s| {
s.emit_struct_field("multibyte_chars", 5, |s| {
(*self.multibyte_chars.borrow()).encode(s)
})
})
Expand All @@ -582,11 +586,13 @@ impl Encodable for FileMap {
impl Decodable for FileMap {
fn decode<D: Decoder>(d: &mut D) -> Result<FileMap, D::Error> {

d.read_struct("FileMap", 5, |d| {
d.read_struct("FileMap", 6, |d| {
let name: String = try!(d.read_struct_field("name", 0, |d| Decodable::decode(d)));
let start_pos: BytePos = try!(d.read_struct_field("start_pos", 1, |d| Decodable::decode(d)));
let end_pos: BytePos = try!(d.read_struct_field("end_pos", 2, |d| Decodable::decode(d)));
let lines: Vec<BytePos> = try!(d.read_struct_field("lines", 3, |d| {
let abs_path: Option<String> =
try!(d.read_struct_field("abs_path", 1, |d| Decodable::decode(d)));
let start_pos: BytePos = try!(d.read_struct_field("start_pos", 2, |d| Decodable::decode(d)));
let end_pos: BytePos = try!(d.read_struct_field("end_pos", 3, |d| Decodable::decode(d)));
let lines: Vec<BytePos> = try!(d.read_struct_field("lines", 4, |d| {
let num_lines: u32 = try!(Decodable::decode(d));
let mut lines = Vec::with_capacity(num_lines as usize);

Expand Down Expand Up @@ -615,9 +621,10 @@ impl Decodable for FileMap {
Ok(lines)
}));
let multibyte_chars: Vec<MultiByteChar> =
try!(d.read_struct_field("multibyte_chars", 4, |d| Decodable::decode(d)));
try!(d.read_struct_field("multibyte_chars", 5, |d| Decodable::decode(d)));
Ok(FileMap {
name: name,
abs_path: abs_path,
start_pos: start_pos,
end_pos: end_pos,
src: None,
Expand Down Expand Up @@ -703,6 +710,9 @@ pub trait FileLoader {
/// Query the existence of a file.
fn file_exists(&self, path: &Path) -> bool;

/// Return an absolute path to a file, if possible.
fn abs_path(&self, path: &Path) -> Option<PathBuf>;

/// Read the contents of an UTF-8 file into memory.
fn read_file(&self, path: &Path) -> io::Result<String>;
}
Expand All @@ -715,6 +725,16 @@ impl FileLoader for RealFileLoader {
fs::metadata(path).is_ok()
}

fn abs_path(&self, path: &Path) -> Option<PathBuf> {
if path.is_absolute() {
Some(path.to_path_buf())
} else {
env::current_dir()
.ok()
.map(|cwd| cwd.join(path))
}
}

fn read_file(&self, path: &Path) -> io::Result<String> {
let mut src = String::new();
try!(try!(fs::File::open(path)).read_to_string(&mut src));
Expand Down Expand Up @@ -755,7 +775,8 @@ impl CodeMap {

pub fn load_file(&self, path: &Path) -> io::Result<Rc<FileMap>> {
let src = try!(self.file_loader.read_file(path));
Ok(self.new_filemap(path.to_str().unwrap().to_string(), src))
let abs_path = self.file_loader.abs_path(path).map(|p| p.to_str().unwrap().to_string());
Ok(self.new_filemap(path.to_str().unwrap().to_string(), abs_path, src))
}

fn next_start_pos(&self) -> usize {
Expand All @@ -770,7 +791,8 @@ impl CodeMap {

/// Creates a new filemap without setting its line information. If you don't
/// intend to set the line information yourself, you should use new_filemap_and_lines.
pub fn new_filemap(&self, filename: FileName, mut src: String) -> Rc<FileMap> {
pub fn new_filemap(&self, filename: FileName, abs_path: Option<FileName>,
mut src: String) -> Rc<FileMap> {
let start_pos = self.next_start_pos();
let mut files = self.files.borrow_mut();

Expand All @@ -783,6 +805,7 @@ impl CodeMap {

let filemap = Rc::new(FileMap {
name: filename,
abs_path: abs_path,
src: Some(Rc::new(src)),
start_pos: Pos::from_usize(start_pos),
end_pos: Pos::from_usize(end_pos),
Expand All @@ -796,8 +819,11 @@ impl CodeMap {
}

/// Creates a new filemap and sets its line information.
pub fn new_filemap_and_lines(&self, filename: &str, src: &str) -> Rc<FileMap> {
let fm = self.new_filemap(filename.to_string(), src.to_owned());
pub fn new_filemap_and_lines(&self, filename: &str, abs_path: Option<&str>,
src: &str) -> Rc<FileMap> {
let fm = self.new_filemap(filename.to_string(),
abs_path.map(|s| s.to_owned()),
src.to_owned());
let mut byte_pos: u32 = fm.start_pos.0;
for line in src.lines() {
// register the start of this line
Expand All @@ -816,6 +842,7 @@ impl CodeMap {
/// information for things inlined from other crates.
pub fn new_imported_filemap(&self,
filename: FileName,
abs_path: Option<FileName>,
source_len: usize,
mut file_local_lines: Vec<BytePos>,
mut file_local_multibyte_chars: Vec<MultiByteChar>)
Expand All @@ -836,6 +863,7 @@ impl CodeMap {

let filemap = Rc::new(FileMap {
name: filename,
abs_path: abs_path,
src: None,
start_pos: start_pos,
end_pos: end_pos,
Expand Down Expand Up @@ -1422,6 +1450,7 @@ mod tests {
fn t1 () {
let cm = CodeMap::new();
let fm = cm.new_filemap("blork.rs".to_string(),
None,
"first line.\nsecond line".to_string());
fm.next_line(BytePos(0));
// Test we can get lines with partial line info.
Expand All @@ -1438,6 +1467,7 @@ mod tests {
fn t2 () {
let cm = CodeMap::new();
let fm = cm.new_filemap("blork.rs".to_string(),
None,
"first line.\nsecond line".to_string());
// TESTING *REALLY* BROKEN BEHAVIOR:
fm.next_line(BytePos(0));
Expand All @@ -1448,10 +1478,13 @@ mod tests {
fn init_code_map() -> CodeMap {
let cm = CodeMap::new();
let fm1 = cm.new_filemap("blork.rs".to_string(),
None,
"first line.\nsecond line".to_string());
let fm2 = cm.new_filemap("empty.rs".to_string(),
None,
"".to_string());
let fm3 = cm.new_filemap("blork2.rs".to_string(),
None,
"first line.\nsecond line".to_string());

fm1.next_line(BytePos(0));
Expand Down Expand Up @@ -1514,8 +1547,10 @@ mod tests {
// € is a three byte utf8 char.
let fm1 =
cm.new_filemap("blork.rs".to_string(),
None,
"fir€st €€€€ line.\nsecond line".to_string());
let fm2 = cm.new_filemap("blork2.rs".to_string(),
None,
"first line€€.\n€ second line".to_string());

fm1.next_line(BytePos(0));
Expand Down Expand Up @@ -1583,7 +1618,7 @@ mod tests {
let cm = CodeMap::new();
let inputtext = "aaaaa\nbbbbBB\nCCC\nDDDDDddddd\neee\n";
let selection = " \n ~~\n~~~\n~~~~~ \n \n";
cm.new_filemap_and_lines("blork.rs", inputtext);
cm.new_filemap_and_lines("blork.rs", None, inputtext);
let span = span_from_selection(inputtext, selection);

// check that we are extracting the text we thought we were extracting
Expand Down
Loading