2015-07-26 12:55:25 +02:00
|
|
|
// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
|
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
2017-03-21 16:00:33 -04:00
|
|
|
use std::collections::BTreeMap;
|
2017-11-06 21:45:54 +09:00
|
|
|
use std::io;
|
2017-12-24 00:28:58 +09:00
|
|
|
use std::path::{Path, PathBuf};
|
2015-07-26 12:55:25 +02:00
|
|
|
|
|
|
|
use syntax::ast;
|
2018-05-04 11:51:06 +02:00
|
|
|
use syntax::parse::{parser, DirectoryOwnership};
|
2018-08-23 17:14:19 -04:00
|
|
|
use syntax::source_map;
|
2018-08-06 22:28:05 +09:00
|
|
|
use syntax_pos::symbol::Symbol;
|
2015-07-26 12:55:25 +02:00
|
|
|
|
2019-02-04 13:30:43 +03:00
|
|
|
use crate::config::FileName;
|
|
|
|
use crate::utils::contains_skip;
|
2017-07-13 18:42:14 +09:00
|
|
|
|
2015-07-26 12:55:25 +02:00
|
|
|
/// List all the files containing modules of a crate.
|
|
|
|
/// If a file is used twice in a crate, it appears only once.
|
2017-06-12 15:58:58 +12:00
|
|
|
pub fn list_files<'a>(
|
|
|
|
krate: &'a ast::Crate,
|
2018-08-23 17:10:46 -04:00
|
|
|
source_map: &source_map::SourceMap,
|
2017-12-08 14:16:47 +01:00
|
|
|
) -> Result<BTreeMap<FileName, &'a ast::Mod>, io::Error> {
|
2017-03-21 16:00:33 -04:00
|
|
|
let mut result = BTreeMap::new(); // Enforce file order determinism
|
2018-08-23 17:10:46 -04:00
|
|
|
let root_filename = source_map.span_to_filename(krate.span);
|
2017-12-08 14:16:47 +01:00
|
|
|
{
|
|
|
|
let parent = match root_filename {
|
2018-08-23 17:10:46 -04:00
|
|
|
source_map::FileName::Real(ref path) => path.parent().unwrap(),
|
2017-12-08 14:16:47 +01:00
|
|
|
_ => Path::new(""),
|
|
|
|
};
|
2018-08-23 17:10:46 -04:00
|
|
|
list_submodules(&krate.module, parent, None, source_map, &mut result)?;
|
2017-12-08 14:16:47 +01:00
|
|
|
}
|
2018-05-21 12:01:31 +12:00
|
|
|
result.insert(root_filename.into(), &krate.module);
|
2017-11-06 21:45:54 +09:00
|
|
|
Ok(result)
|
2015-07-26 12:55:25 +02:00
|
|
|
}
|
|
|
|
|
2018-08-06 22:28:05 +09:00
|
|
|
fn path_value(attr: &ast::Attribute) -> Option<Symbol> {
|
|
|
|
if attr.name() == "path" {
|
|
|
|
attr.value_str()
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-06 22:34:58 +09:00
|
|
|
// 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.
|
2018-08-06 22:28:05 +09:00
|
|
|
fn find_path_value(attrs: &[ast::Attribute]) -> Option<Symbol> {
|
|
|
|
attrs.iter().flat_map(path_value).next()
|
|
|
|
}
|
|
|
|
|
2015-07-26 12:55:25 +02:00
|
|
|
/// Recursively list all external modules included in a module.
|
2017-06-12 15:58:58 +12:00
|
|
|
fn list_submodules<'a>(
|
|
|
|
module: &'a ast::Mod,
|
|
|
|
search_dir: &Path,
|
2018-05-04 11:51:06 +02:00
|
|
|
relative: Option<ast::Ident>,
|
2018-08-23 17:10:46 -04:00
|
|
|
source_map: &source_map::SourceMap,
|
2017-12-08 14:16:47 +01:00
|
|
|
result: &mut BTreeMap<FileName, &'a ast::Mod>,
|
2017-11-06 21:45:54 +09:00
|
|
|
) -> Result<(), io::Error> {
|
2015-07-26 12:55:25 +02:00
|
|
|
debug!("list_submodules: search_dir: {:?}", search_dir);
|
2015-09-04 23:39:33 +02:00
|
|
|
for item in &module.items {
|
2016-03-01 17:27:19 -05:00
|
|
|
if let ast::ItemKind::Mod(ref sub_mod) = item.node {
|
2017-07-13 18:42:14 +09:00
|
|
|
if !contains_skip(&item.attrs) {
|
2018-08-23 17:14:19 -04:00
|
|
|
let is_internal = source_map.span_to_filename(item.span)
|
|
|
|
== source_map.span_to_filename(sub_mod.inner);
|
2018-05-04 11:51:06 +02:00
|
|
|
let (dir_path, relative) = if is_internal {
|
2018-08-06 22:28:05 +09:00
|
|
|
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)
|
|
|
|
}
|
2015-07-26 12:55:25 +02:00
|
|
|
} else {
|
2018-05-04 11:51:06 +02:00
|
|
|
let (mod_path, relative) =
|
2018-08-23 17:10:46 -04:00
|
|
|
module_file(item.ident, &item.attrs, search_dir, relative, source_map)?;
|
2015-07-26 12:55:25 +02:00
|
|
|
let dir_path = mod_path.parent().unwrap().to_owned();
|
2017-12-08 14:16:47 +01:00
|
|
|
result.insert(FileName::Real(mod_path), sub_mod);
|
2018-05-04 11:51:06 +02:00
|
|
|
(dir_path, relative)
|
2015-07-26 12:55:25 +02:00
|
|
|
};
|
2018-08-23 17:10:46 -04:00
|
|
|
list_submodules(sub_mod, &dir_path, relative, source_map, result)?;
|
2015-07-26 12:55:25 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-11-06 21:45:54 +09:00
|
|
|
Ok(())
|
2015-07-26 12:55:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Find the file corresponding to an external mod
|
2017-06-12 15:58:58 +12:00
|
|
|
fn module_file(
|
|
|
|
id: ast::Ident,
|
|
|
|
attrs: &[ast::Attribute],
|
|
|
|
dir_path: &Path,
|
2018-05-04 11:51:06 +02:00
|
|
|
relative: Option<ast::Ident>,
|
2018-08-23 17:10:46 -04:00
|
|
|
source_map: &source_map::SourceMap,
|
2018-05-04 11:51:06 +02:00
|
|
|
) -> Result<(PathBuf, Option<ast::Ident>), io::Error> {
|
2016-08-23 23:14:45 +09:00
|
|
|
if let Some(path) = parser::Parser::submod_path_from_attr(attrs, dir_path) {
|
2018-05-04 11:51:06 +02:00
|
|
|
return Ok((path, None));
|
2015-07-26 12:55:25 +02:00
|
|
|
}
|
|
|
|
|
2018-08-23 17:10:46 -04:00
|
|
|
match parser::Parser::default_submod_path(id, relative, dir_path, source_map).result {
|
2018-05-04 11:51:06 +02:00
|
|
|
Ok(parser::ModulePathSuccess {
|
|
|
|
path,
|
|
|
|
directory_ownership,
|
|
|
|
..
|
|
|
|
}) => {
|
|
|
|
let relative = if let DirectoryOwnership::Owned { relative } = directory_ownership {
|
|
|
|
relative
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
};
|
|
|
|
Ok((path, relative))
|
|
|
|
}
|
2017-11-06 21:45:54 +09:00
|
|
|
Err(_) => Err(io::Error::new(
|
|
|
|
io::ErrorKind::Other,
|
|
|
|
format!("Couldn't find module {}", id),
|
|
|
|
)),
|
2015-07-26 12:55:25 +02:00
|
|
|
}
|
|
|
|
}
|