X-Git-Url: https://git.lizzy.rs/?a=blobdiff_plain;f=src%2Fmodules.rs;h=aa36f4103e4231268738213bb5020765b457556a;hb=bb7442802abe354a0ed844ec237cd20969789224;hp=b50c9e26895938ba981950ef73e4383e6c28728b;hpb=f5ee0f0986f23acb27fabf257e11eb76c6981601;p=rust.git diff --git a/src/modules.rs b/src/modules.rs index b50c9e26895..aa36f4103e4 100644 --- a/src/modules.rs +++ b/src/modules.rs @@ -8,65 +8,114 @@ // option. This file may not be copied, modified, or distributed // except according to those terms. -use utils; - +use std::collections::BTreeMap; +use std::io; use std::path::{Path, PathBuf}; -use std::collections::HashMap; use syntax::ast; -use syntax::codemap; -use syntax::parse::parser; +use syntax::parse::{parser, DirectoryOwnership}; +use syntax::source_map; +use syntax_pos::symbol::Symbol; +use config::FileName; +use utils::contains_skip; /// List all the files containing modules of a crate. /// If a file is used twice in a crate, it appears only once. -pub fn list_files<'a>(krate: &'a ast::Crate, - codemap: &codemap::CodeMap) - -> HashMap { - let mut result = HashMap::new(); - let root_filename: PathBuf = codemap.span_to_filename(krate.span).into(); - list_submodules(&krate.module, root_filename.parent().unwrap(), codemap, &mut result); - result.insert(root_filename, &krate.module); - result +pub fn list_files<'a>( + krate: &'a ast::Crate, + source_map: &source_map::SourceMap, +) -> Result, io::Error> { + let mut result = BTreeMap::new(); // Enforce file order determinism + let root_filename = source_map.span_to_filename(krate.span); + { + let parent = match root_filename { + source_map::FileName::Real(ref path) => path.parent().unwrap(), + _ => Path::new(""), + }; + list_submodules(&krate.module, parent, None, source_map, &mut result)?; + } + result.insert(root_filename.into(), &krate.module); + Ok(result) +} + +fn path_value(attr: &ast::Attribute) -> Option { + if attr.name() == "path" { + attr.value_str() + } else { + None + } +} + +// N.B. Even when there are multiple `#[path = ...]` attributes, we just need to +// examine the first one, since rustc ignores the second and the subsequent ones +// as unused attributes. +fn find_path_value(attrs: &[ast::Attribute]) -> Option { + attrs.iter().flat_map(path_value).next() } /// Recursively list all external modules included in a module. -fn list_submodules<'a>(module: &'a ast::Mod, - search_dir: &Path, - codemap: &codemap::CodeMap, - result: &mut HashMap) { +fn list_submodules<'a>( + module: &'a ast::Mod, + search_dir: &Path, + relative: Option, + source_map: &source_map::SourceMap, + result: &mut BTreeMap, +) -> Result<(), io::Error> { debug!("list_submodules: search_dir: {:?}", search_dir); for item in &module.items { - if let ast::ItemMod(ref sub_mod) = item.node { - if !utils::contains_skip(&item.attrs) { - let is_internal = codemap.span_to_filename(item.span) == - codemap.span_to_filename(sub_mod.inner); - let dir_path = if is_internal { - search_dir.join(&item.ident.to_string()) + if let ast::ItemKind::Mod(ref sub_mod) = item.node { + if !contains_skip(&item.attrs) { + let is_internal = source_map.span_to_filename(item.span) + == source_map.span_to_filename(sub_mod.inner); + let (dir_path, relative) = if is_internal { + if let Some(path) = find_path_value(&item.attrs) { + (search_dir.join(&path.as_str()), None) + } else { + (search_dir.join(&item.ident.to_string()), None) + } } else { - let mod_path = module_file(item.ident, &item.attrs, search_dir, codemap); + let (mod_path, relative) = + module_file(item.ident, &item.attrs, search_dir, relative, source_map)?; let dir_path = mod_path.parent().unwrap().to_owned(); - result.insert(mod_path, sub_mod); - dir_path + result.insert(FileName::Real(mod_path), sub_mod); + (dir_path, relative) }; - list_submodules(sub_mod, &dir_path, codemap, result); + list_submodules(sub_mod, &dir_path, relative, source_map, result)?; } } } + Ok(()) } /// Find the file corresponding to an external mod -fn module_file(id: ast::Ident, - attrs: &[ast::Attribute], - dir_path: &Path, - codemap: &codemap::CodeMap) - -> PathBuf { - if let Some(path) = parser::Parser::submod_path_from_attr(attrs, &dir_path) { - return path; +fn module_file( + id: ast::Ident, + attrs: &[ast::Attribute], + dir_path: &Path, + relative: Option, + source_map: &source_map::SourceMap, +) -> Result<(PathBuf, Option), io::Error> { + if let Some(path) = parser::Parser::submod_path_from_attr(attrs, dir_path) { + return Ok((path, None)); } - match parser::Parser::default_submod_path(id, &dir_path, codemap).result { - Ok(parser::ModulePathSuccess { path, .. }) => path, - Err(_) => panic!("Couldn't find module {}", id), + match parser::Parser::default_submod_path(id, relative, dir_path, source_map).result { + Ok(parser::ModulePathSuccess { + path, + directory_ownership, + .. + }) => { + let relative = if let DirectoryOwnership::Owned { relative } = directory_ownership { + relative + } else { + None + }; + Ok((path, relative)) + } + Err(_) => Err(io::Error::new( + io::ErrorKind::Other, + format!("Couldn't find module {}", id), + )), } }